diff --git a/lib/libimhex/CMakeLists.txt b/lib/libimhex/CMakeLists.txt index db43e3684..0feb8833e 100644 --- a/lib/libimhex/CMakeLists.txt +++ b/lib/libimhex/CMakeLists.txt @@ -113,7 +113,7 @@ set(LIBIMHEX_SOURCES source/data_processor/node.cpp source/helpers/utils.cpp - source/helpers/paths.cpp + source/helpers/fs.cpp source/helpers/magic.cpp source/helpers/crypto.cpp source/helpers/net.cpp @@ -149,7 +149,7 @@ if (APPLE) endif () endif () - set(LIBIMHEX_SOURCES ${LIBIMHEX_SOURCES} source/helpers/paths_mac.mm) + set(LIBIMHEX_SOURCES ${LIBIMHEX_SOURCES} source/helpers/fs_macos.mm) endif () add_compile_definitions(IMHEX_PROJECT_NAME="${PROJECT_NAME}") diff --git a/lib/libimhex/include/hex/api/content_registry.hpp b/lib/libimhex/include/hex/api/content_registry.hpp index 99835995c..f4367cc54 100644 --- a/lib/libimhex/include/hex/api/content_registry.hpp +++ b/lib/libimhex/include/hex/api/content_registry.hpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include @@ -372,7 +372,7 @@ namespace hex { namespace impl { - using Callback = std::function; + using Callback = std::function; struct Entry { std::vector extensions; Callback callback; diff --git a/lib/libimhex/include/hex/api/event.hpp b/lib/libimhex/include/hex/api/event.hpp index cb638cbf2..d9c8508ac 100644 --- a/lib/libimhex/include/hex/api/event.hpp +++ b/lib/libimhex/include/hex/api/event.hpp @@ -8,7 +8,7 @@ #include #include -#include +#include #define EVENT_DEF(event_name, ...) \ struct event_name final : public hex::Event<__VA_ARGS__> { \ @@ -101,7 +101,7 @@ namespace hex { } /* Default Events */ - EVENT_DEF(EventFileLoaded, fs::path); + EVENT_DEF(EventFileLoaded, std::fs::path); EVENT_DEF(EventFileUnloaded); EVENT_DEF(EventDataChanged); EVENT_DEF(EventHighlightingChanged); @@ -124,7 +124,7 @@ namespace hex { EVENT_DEF(RequestSetPatternLanguageCode, std::string); EVENT_DEF(RequestChangeWindowTitle, std::string); EVENT_DEF(RequestCloseImHex, bool); - EVENT_DEF(RequestOpenFile, fs::path); + EVENT_DEF(RequestOpenFile, std::fs::path); EVENT_DEF(RequestChangeTheme, u32); EVENT_DEF(RequestOpenPopup, std::string); EVENT_DEF(RequestCreateProvider, std::string, hex::prv::Provider **); diff --git a/lib/libimhex/include/hex/api/plugin_manager.hpp b/lib/libimhex/include/hex/api/plugin_manager.hpp index a565dbe87..8ba02a031 100644 --- a/lib/libimhex/include/hex/api/plugin_manager.hpp +++ b/lib/libimhex/include/hex/api/plugin_manager.hpp @@ -3,7 +3,7 @@ #include #include -#include +#include #include @@ -13,7 +13,7 @@ namespace hex { class Plugin { public: - explicit Plugin(const fs::path &path); + explicit Plugin(const std::fs::path &path); Plugin(const Plugin &) = delete; Plugin(Plugin &&other) noexcept; ~Plugin(); @@ -26,7 +26,7 @@ namespace hex { void setImGuiContext(ImGuiContext *ctx) const; [[nodiscard]] bool isBuiltinPlugin() const; - [[nodiscard]] const fs::path &getPath() const; + [[nodiscard]] const std::fs::path &getPath() const; [[nodiscard]] bool isLoaded() const; @@ -40,7 +40,7 @@ namespace hex { using IsBuiltinPluginFunc = bool (*)(); void *m_handle = nullptr; - fs::path m_path; + std::fs::path m_path; mutable bool m_initialized = false; @@ -65,7 +65,7 @@ namespace hex { public: PluginManager() = delete; - static bool load(const fs::path &pluginFolder); + static bool load(const std::fs::path &pluginFolder); static void unload(); static void reload(); @@ -74,7 +74,7 @@ namespace hex { } private: - static fs::path s_pluginFolder; + static std::fs::path s_pluginFolder; static std::vector s_plugins; }; diff --git a/lib/libimhex/include/hex/helpers/encoding_file.hpp b/lib/libimhex/include/hex/helpers/encoding_file.hpp index 4d849f83d..b04eaaa66 100644 --- a/lib/libimhex/include/hex/helpers/encoding_file.hpp +++ b/lib/libimhex/include/hex/helpers/encoding_file.hpp @@ -6,18 +6,19 @@ #include #include -#include +#include namespace hex { class EncodingFile { public: - enum class Type { + enum class Type + { Thingy }; EncodingFile() = default; - EncodingFile(Type type, const fs::path &path); + EncodingFile(Type type, const std::fs::path &path); [[nodiscard]] std::pair getEncodingFor(const std::vector &buffer) const; [[nodiscard]] size_t getLongestSequence() const { return this->m_longestSequence; } diff --git a/lib/libimhex/include/hex/helpers/file.hpp b/lib/libimhex/include/hex/helpers/file.hpp index df0a623df..e278a589f 100644 --- a/lib/libimhex/include/hex/helpers/file.hpp +++ b/lib/libimhex/include/hex/helpers/file.hpp @@ -6,7 +6,7 @@ #include #include -#include +#include #if defined(OS_MACOS) #define off64_t off_t @@ -16,7 +16,7 @@ #define ftruncate64 ftruncate #endif -namespace hex { +namespace hex::fs { class File { public: @@ -27,7 +27,7 @@ namespace hex { Create }; - explicit File(const fs::path &path, Mode mode) noexcept; + explicit File(const std::fs::path &path, Mode mode) noexcept; File() noexcept; File(const File &) = delete; File(File &&other) noexcept; @@ -38,7 +38,7 @@ namespace hex { [[nodiscard]] bool isValid() const { - return this->m_file != nullptr && fs::exists(this->m_path) && !fs::is_directory(this->m_path); + return this->m_file != nullptr && fs::exists(this->m_path) && !fs::isDirectory(this->m_path); } void seek(u64 offset); @@ -59,13 +59,13 @@ namespace hex { bool remove(); auto getHandle() { return this->m_file; } - const fs::path &getPath() { return this->m_path; } + const std::fs::path &getPath() { return this->m_path; } void disableBuffering(); private: FILE *m_file; - fs::path m_path; + std::fs::path m_path; }; } \ No newline at end of file diff --git a/lib/libimhex/include/hex/helpers/fs.hpp b/lib/libimhex/include/hex/helpers/fs.hpp new file mode 100644 index 000000000..5b373084e --- /dev/null +++ b/lib/libimhex/include/hex/helpers/fs.hpp @@ -0,0 +1,83 @@ +#pragma once + +#include +#include +#include + +#include + +namespace std::fs { + using namespace std::filesystem; +} + +namespace hex::fs { + + static inline bool exists(const std::fs::path &path) { + std::error_code error; + return std::filesystem::exists(path, error) && !error; + } + + static inline bool createDirectories(const std::fs::path &path) { + std::error_code error; + return std::filesystem::create_directories(path, error) && !error; + } + + static inline bool isRegularFile(const std::fs::path &path) { + std::error_code error; + return std::filesystem::is_regular_file(path, error) && !error; + } + + static inline bool copyFile(const std::fs::path &from, const std::fs::path &to, std::fs::copy_options = std::fs::copy_options::none) { + std::error_code error; + return std::filesystem::copy_file(from, to, error) && !error; + } + + static inline bool isDirectory(const std::fs::path &path) { + std::error_code error; + return std::filesystem::is_directory(path, error) && !error; + } + + static inline bool remove(const std::fs::path &path) { + std::error_code error; + return std::filesystem::remove(path, error) && !error; + } + + static inline uintmax_t getFileSize(const std::fs::path &path) { + std::error_code error; + auto size = std::filesystem::file_size(path, error); + + if (error) return 0; + else return size; + } + + bool isPathWritable(std::fs::path path); + + enum class DialogMode + { + Open, + Save, + Folder + }; + + bool openFileBrowser(const std::string &title, DialogMode mode, const std::vector &validExtensions, const std::function &callback, const std::string &defaultPath = {}); + + enum class ImHexPath + { + Patterns, + PatternsInclude, + Magic, + Python, + Plugins, + Yara, + Config, + Resources, + Constants, + Encodings, + Logs + }; + + std::string getExecutablePath(); + + std::vector getDefaultPaths(ImHexPath path, bool listNonExisting = false); + +} \ No newline at end of file diff --git a/lib/libimhex/include/hex/helpers/paths_mac.h b/lib/libimhex/include/hex/helpers/fs_macos.h similarity index 100% rename from lib/libimhex/include/hex/helpers/paths_mac.h rename to lib/libimhex/include/hex/helpers/fs_macos.h diff --git a/lib/libimhex/include/hex/helpers/loader_script_handler.hpp b/lib/libimhex/include/hex/helpers/loader_script_handler.hpp index 874acff99..bf8b51c9c 100644 --- a/lib/libimhex/include/hex/helpers/loader_script_handler.hpp +++ b/lib/libimhex/include/hex/helpers/loader_script_handler.hpp @@ -3,7 +3,7 @@ #include #include -#include +#include struct _object; typedef struct _object PyObject; @@ -18,13 +18,13 @@ namespace hex { public: LoaderScript() = delete; - static bool processFile(const fs::path &scriptPath); + static bool processFile(const std::fs::path &scriptPath); - static void setFilePath(const fs::path &filePath) { LoaderScript::s_filePath = filePath; } + static void setFilePath(const std::fs::path &filePath) { LoaderScript::s_filePath = filePath; } static void setDataProvider(prv::Provider *provider) { LoaderScript::s_dataProvider = provider; } private: - static inline fs::path s_filePath; + static inline std::fs::path s_filePath; static inline prv::Provider *s_dataProvider; static PyObject *Py_getFilePath(PyObject *self, PyObject *args); diff --git a/lib/libimhex/include/hex/helpers/net.hpp b/lib/libimhex/include/hex/helpers/net.hpp index 8c5df86ce..b0daedbfd 100644 --- a/lib/libimhex/include/hex/helpers/net.hpp +++ b/lib/libimhex/include/hex/helpers/net.hpp @@ -12,7 +12,7 @@ #include #include -#include +#include using CURL = void; struct curl_slist; @@ -40,8 +40,8 @@ namespace hex { std::future> getString(const std::string &url, u32 timeout = DefaultTimeout); std::future> getJson(const std::string &url, u32 timeout = DefaultTimeout); - std::future> uploadFile(const std::string &url, const fs::path &filePath, u32 timeout = DefaultTimeout); - std::future> downloadFile(const std::string &url, const fs::path &filePath, u32 timeout = DefaultTimeout); + std::future> uploadFile(const std::string &url, const std::fs::path &filePath, u32 timeout = DefaultTimeout); + std::future> downloadFile(const std::string &url, const std::fs::path &filePath, u32 timeout = DefaultTimeout); [[nodiscard]] std::string encode(const std::string &input); diff --git a/lib/libimhex/include/hex/helpers/paths.hpp b/lib/libimhex/include/hex/helpers/paths.hpp deleted file mode 100644 index 7b0b6a136..000000000 --- a/lib/libimhex/include/hex/helpers/paths.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace hex { - - namespace fs = std::filesystem; - - enum class ImHexPath { - Patterns, - PatternsInclude, - Magic, - Python, - Plugins, - Yara, - Config, - Resources, - Constants, - Encodings, - Logs - }; - - std::string getExecutablePath(); - - std::vector getPath(ImHexPath path, bool listNonExisting = false); - -} \ No newline at end of file diff --git a/lib/libimhex/include/hex/helpers/project_file_handler.hpp b/lib/libimhex/include/hex/helpers/project_file_handler.hpp index 7dfabbb83..87edde6ac 100644 --- a/lib/libimhex/include/hex/helpers/project_file_handler.hpp +++ b/lib/libimhex/include/hex/helpers/project_file_handler.hpp @@ -8,7 +8,7 @@ #include #include -#include +#include namespace hex { @@ -16,8 +16,8 @@ namespace hex { public: ProjectFile() = delete; - static bool load(const fs::path &filePath); - static bool store(fs::path filePath = {}); + static bool load(const std::fs::path &filePath); + static bool store(std::fs::path filePath = {}); [[nodiscard]] static bool hasUnsavedChanges() { return ProjectFile::s_hasUnsavedChanged; @@ -29,10 +29,10 @@ namespace hex { ProjectFile::s_hasUnsavedChanged = true; if (setWindowTitle) - EventManager::post(fs::path(getFilePath()).filename().string()); + EventManager::post(std::fs::path(getFilePath()).filename().string()); } - [[nodiscard]] static const fs::path &getProjectFilePath() { + [[nodiscard]] static const std::fs::path &getProjectFilePath() { return ProjectFile::s_currProjectFilePath; } @@ -41,11 +41,11 @@ namespace hex { } - [[nodiscard]] static const fs::path &getFilePath() { + [[nodiscard]] static const std::fs::path &getFilePath() { return ProjectFile::s_filePath; } - static void setFilePath(const fs::path &filePath) { + static void setFilePath(const std::fs::path &filePath) { ProjectFile::s_filePath = filePath; EventManager::post(filePath.filename().string()); @@ -92,10 +92,10 @@ namespace hex { } private: - static fs::path s_currProjectFilePath; + static std::fs::path s_currProjectFilePath; static bool s_hasUnsavedChanged; - static fs::path s_filePath; + static std::fs::path s_filePath; static std::string s_pattern; static Patches s_patches; static std::list s_bookmarks; diff --git a/lib/libimhex/include/hex/helpers/utils.hpp b/lib/libimhex/include/hex/helpers/utils.hpp index bd042cf79..5046d8627 100644 --- a/lib/libimhex/include/hex/helpers/utils.hpp +++ b/lib/libimhex/include/hex/helpers/utils.hpp @@ -3,7 +3,7 @@ #include #include -#include +#include #include #include @@ -18,8 +18,6 @@ #include #include -#include - #define TOKEN_CONCAT_IMPL(x, y) x##y #define TOKEN_CONCAT(x, y) TOKEN_CONCAT_IMPL(x, y) #define ANONYMOUS_VARIABLE(prefix) TOKEN_CONCAT(prefix, __COUNTER__) @@ -44,8 +42,6 @@ namespace hex { std::string encodeByteString(const std::vector &bytes); std::vector decodeByteString(const std::string &string); - bool isPathWritable(fs::path path); - [[nodiscard]] constexpr inline u64 extract(u8 from, u8 to, const hex::unsigned_integral auto &value) { if (from < to) std::swap(from, to); @@ -268,15 +264,6 @@ namespace hex { trimRight(s); } - enum class DialogMode - { - Open, - Save, - Folder - }; - - bool openFileBrowser(const std::string &title, DialogMode mode, const std::vector &validExtensions, const std::function &callback, const std::string &defaultPath = {}); - float float16ToFloat32(u16 float16); inline bool equalsIgnoreCase(const std::string &left, const std::string &right) { diff --git a/lib/libimhex/include/hex/pattern_language/pattern_language.hpp b/lib/libimhex/include/hex/pattern_language/pattern_language.hpp index fec8277f8..393d9c925 100644 --- a/lib/libimhex/include/hex/pattern_language/pattern_language.hpp +++ b/lib/libimhex/include/hex/pattern_language/pattern_language.hpp @@ -35,7 +35,7 @@ namespace hex::pl { [[nodiscard]] std::optional>> parseString(const std::string &code); [[nodiscard]] bool executeString(prv::Provider *provider, const std::string &string, const std::map &envVars = {}, const std::map &inVariables = {}, bool checkResult = true); - [[nodiscard]] bool executeFile(prv::Provider *provider, const fs::path &path, const std::map &envVars = {}, const std::map &inVariables = {}); + [[nodiscard]] bool executeFile(prv::Provider *provider, const std::fs::path &path, const std::map &envVars = {}, const std::map &inVariables = {}); [[nodiscard]] std::pair> executeFunction(prv::Provider *provider, const std::string &code); [[nodiscard]] const std::vector> &getCurrentAST() const; diff --git a/lib/libimhex/include/hex/pattern_language/preprocessor.hpp b/lib/libimhex/include/hex/pattern_language/preprocessor.hpp index b92655a10..7786fabdf 100644 --- a/lib/libimhex/include/hex/pattern_language/preprocessor.hpp +++ b/lib/libimhex/include/hex/pattern_language/preprocessor.hpp @@ -9,7 +9,7 @@ #include #include -#include +#include #include @@ -41,7 +41,7 @@ namespace hex::pl { std::set> m_defines; std::set> m_pragmas; - std::set m_onceIncludedFiles; + std::set m_onceIncludedFiles; std::optional m_error; diff --git a/lib/libimhex/include/hex/providers/provider.hpp b/lib/libimhex/include/hex/providers/provider.hpp index 4c13a947a..3bb00121e 100644 --- a/lib/libimhex/include/hex/providers/provider.hpp +++ b/lib/libimhex/include/hex/providers/provider.hpp @@ -9,7 +9,7 @@ #include #include -#include +#include namespace hex::pl { class PatternLanguage; @@ -37,7 +37,7 @@ namespace hex::prv { virtual void insert(u64 offset, size_t size); virtual void save(); - virtual void saveAs(const fs::path &path); + virtual void saveAs(const std::fs::path &path); virtual void readRaw(u64 offset, void *buffer, size_t size) = 0; virtual void writeRaw(u64 offset, const void *buffer, size_t size) = 0; diff --git a/lib/libimhex/include/hex/ui/view.hpp b/lib/libimhex/include/hex/ui/view.hpp index 5719870b8..9d92a83cb 100644 --- a/lib/libimhex/include/hex/ui/view.hpp +++ b/lib/libimhex/include/hex/ui/view.hpp @@ -41,7 +41,7 @@ namespace hex { static void showFatalPopup(const std::string &errorMessage); static void showYesNoQuestionPopup(const std::string &message, const std::function &yesCallback, const std::function &noCallback); - static void showFileChooserPopup(const std::vector &paths, const std::vector &validExtensions, const std::function &callback); + static void showFileChooserPopup(const std::vector &paths, const std::vector &validExtensions, const std::function &callback); [[nodiscard]] virtual bool hasViewMenuItemEntry() const; [[nodiscard]] virtual ImVec2 getMinSize() const; @@ -76,8 +76,8 @@ namespace hex { static std::function s_yesCallback, s_noCallback; static u32 s_selectableFileIndex; - static std::vector s_selectableFiles; - static std::function s_selectableFileOpenCallback; + static std::vector s_selectableFiles; + static std::function s_selectableFileOpenCallback; static std::vector s_selectableFilesValidExtensions; static ImFontAtlas *s_fontAtlas; diff --git a/lib/libimhex/source/api/content_registry.cpp b/lib/libimhex/source/api/content_registry.cpp index 179610802..1d63773a9 100644 --- a/lib/libimhex/source/api/content_registry.cpp +++ b/lib/libimhex/source/api/content_registry.cpp @@ -1,6 +1,6 @@ #include -#include +#include #include #include @@ -16,7 +16,7 @@ namespace hex { void load() { bool loaded = false; - for (const auto &dir : hex::getPath(ImHexPath::Config)) { + for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Config)) { std::ifstream settingsFile(dir / "settings.json"); if (settingsFile.good()) { @@ -31,7 +31,7 @@ namespace hex { } void store() { - for (const auto &dir : hex::getPath(ImHexPath::Config)) { + for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Config)) { std::ofstream settingsFile(dir / "settings.json", std::ios::trunc); if (settingsFile.good()) { diff --git a/lib/libimhex/source/api/plugin_manager.cpp b/lib/libimhex/source/api/plugin_manager.cpp index 9c8b36a01..5ff04a89e 100644 --- a/lib/libimhex/source/api/plugin_manager.cpp +++ b/lib/libimhex/source/api/plugin_manager.cpp @@ -7,7 +7,7 @@ namespace hex { - Plugin::Plugin(const fs::path &path) : m_path(path) { + Plugin::Plugin(const std::fs::path &path) : m_path(path) { this->m_handle = dlopen(path.string().c_str(), RTLD_LAZY); if (this->m_handle == nullptr) { @@ -15,7 +15,7 @@ namespace hex { return; } - auto pluginName = fs::path(path).stem().string(); + auto pluginName = std::fs::path(path).stem().string(); this->m_initializePluginFunction = getPluginFunction("initializePlugin"); this->m_getPluginNameFunction = getPluginFunction("getPluginName"); @@ -110,7 +110,7 @@ namespace hex { return false; } - const fs::path &Plugin::getPath() const { + const std::fs::path &Plugin::getPath() const { return this->m_path; } @@ -124,16 +124,16 @@ namespace hex { } - fs::path PluginManager::s_pluginFolder; + std::fs::path PluginManager::s_pluginFolder; std::vector PluginManager::s_plugins; - bool PluginManager::load(const fs::path &pluginFolder) { + bool PluginManager::load(const std::fs::path &pluginFolder) { if (!fs::exists(pluginFolder)) return false; PluginManager::s_pluginFolder = pluginFolder; - for (auto &pluginPath : fs::directory_iterator(pluginFolder)) { + for (auto &pluginPath : std::fs::directory_iterator(pluginFolder)) { if (pluginPath.is_regular_file() && pluginPath.path().extension() == ".hexplug") PluginManager::s_plugins.emplace_back(pluginPath.path().string()); } diff --git a/lib/libimhex/source/helpers/encoding_file.cpp b/lib/libimhex/source/helpers/encoding_file.cpp index 0c9edaf18..84b6dd801 100644 --- a/lib/libimhex/source/helpers/encoding_file.cpp +++ b/lib/libimhex/source/helpers/encoding_file.cpp @@ -6,7 +6,7 @@ namespace hex { - EncodingFile::EncodingFile(Type type, const fs::path &path) { + EncodingFile::EncodingFile(Type type, const std::fs::path &path) { std::ifstream encodingFile(path.c_str()); switch (type) { diff --git a/lib/libimhex/source/helpers/file.cpp b/lib/libimhex/source/helpers/file.cpp index 79230fc07..c5eb7299d 100644 --- a/lib/libimhex/source/helpers/file.cpp +++ b/lib/libimhex/source/helpers/file.cpp @@ -1,9 +1,9 @@ #include #include -namespace hex { +namespace hex::fs { - File::File(const fs::path &path, Mode mode) noexcept : m_path(path) { + File::File(const std::fs::path &path, Mode mode) noexcept : m_path(path) { if (mode == File::Mode::Read) this->m_file = fopen64(path.string().c_str(), "rb"); else if (mode == File::Mode::Write) diff --git a/lib/libimhex/source/helpers/paths.cpp b/lib/libimhex/source/helpers/fs.cpp similarity index 80% rename from lib/libimhex/source/helpers/paths.cpp rename to lib/libimhex/source/helpers/fs.cpp index db2f5a36c..f26ce49f0 100644 --- a/lib/libimhex/source/helpers/paths.cpp +++ b/lib/libimhex/source/helpers/fs.cpp @@ -1,5 +1,6 @@ #include -#include +#include +#include #include @@ -14,7 +15,7 @@ #include #include -namespace hex { +namespace hex::fs { std::string getExecutablePath() { #if defined(OS_WINDOWS) @@ -34,8 +35,57 @@ namespace hex { #endif } - std::vector getPath(ImHexPath path, bool listNonExisting) { - std::vector result; + + bool isPathWritable(std::fs::path path) { + constexpr static auto TestFileName = "__imhex__tmp__"; + { + File file(path / TestFileName, File::Mode::Read); + if (file.isValid()) { + if (!file.remove()) + return false; + } + } + + File file(path / TestFileName, File::Mode::Create); + bool result = file.isValid(); + if (!file.remove()) + return false; + + return result; + } + + bool openFileBrowser(const std::string &title, DialogMode mode, const std::vector &validExtensions, const std::function &callback, const std::string &defaultPath) { + NFD::Init(); + + nfdchar_t *outPath; + nfdresult_t result; + switch (mode) { + case DialogMode::Open: + result = NFD::OpenDialog(outPath, validExtensions.data(), validExtensions.size(), defaultPath.c_str()); + break; + case DialogMode::Save: + result = NFD::SaveDialog(outPath, validExtensions.data(), validExtensions.size(), defaultPath.c_str()); + break; + case DialogMode::Folder: + result = NFD::PickFolder(outPath, defaultPath.c_str()); + break; + default: + __builtin_unreachable(); + } + + if (result == NFD_OKAY) { + callback(reinterpret_cast(outPath)); + NFD::FreePath(outPath); + } + + NFD::Quit(); + + return result == NFD_OKAY; + } + + + std::vector getDefaultPaths(ImHexPath path, bool listNonExisting) { + std::vector result; const auto exePath = getExecutablePath(); const std::string settingName { "hex.builtin.setting.folders" }; auto userDirs = ContentRegistry::Settings::read(settingName, settingName, std::vector {}); @@ -47,9 +97,9 @@ namespace hex { }; #if defined(OS_WINDOWS) - const auto parentDir = fs::path(exePath).parent_path(); + const auto parentDir = std::fs::path(exePath).parent_path(); - fs::path appDataDir; + std::fs::path appDataDir; { LPWSTR wAppDataPath = nullptr; if (!SUCCEEDED(SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, nullptr, &wAppDataPath))) @@ -59,7 +109,7 @@ namespace hex { CoTaskMemFree(wAppDataPath); } - std::vector paths = { appDataDir / "imhex", parentDir }; + std::vector paths = { appDataDir / "imhex", parentDir }; switch (path) { case ImHexPath::Patterns: @@ -126,9 +176,9 @@ namespace hex { } #elif defined(OS_MACOS) // Get path to special directories - const fs::path applicationSupportDir(getMacApplicationSupportDirectoryPath()); + const std::fs::path applicationSupportDir(getMacApplicationSupportDirectoryPath()); - std::vector paths = { applicationSupportDir, exePath }; + std::vector paths = { applicationSupportDir, exePath }; switch (path) { case ImHexPath::Patterns: @@ -170,8 +220,8 @@ namespace hex { __builtin_unreachable(); } #else - std::vector configDirs = xdg::ConfigDirs(); - std::vector dataDirs = xdg::DataDirs(); + std::vector configDirs = xdg::ConfigDirs(); + std::vector dataDirs = xdg::DataDirs(); configDirs.push_back(xdg::ConfigHomeDir()); dataDirs.push_back(xdg::DataHomeDir()); @@ -231,7 +281,7 @@ namespace hex { if (!listNonExisting) { result.erase(std::remove_if(result.begin(), result.end(), [](const auto &path) { - return !fs::is_directory(path); + return !fs::isDirectory(path); }), result.end()); } diff --git a/lib/libimhex/source/helpers/paths_mac.mm b/lib/libimhex/source/helpers/fs_macos.mm similarity index 100% rename from lib/libimhex/source/helpers/paths_mac.mm rename to lib/libimhex/source/helpers/fs_macos.mm diff --git a/lib/libimhex/source/helpers/loader_script_handler.cpp b/lib/libimhex/source/helpers/loader_script_handler.cpp index 9ecc0d6e2..9d58ed2a7 100644 --- a/lib/libimhex/source/helpers/loader_script_handler.cpp +++ b/lib/libimhex/source/helpers/loader_script_handler.cpp @@ -1,7 +1,7 @@ #include #include -#include +#include #include #include #include @@ -178,11 +178,11 @@ namespace hex { return createStructureType("union", args); } - bool LoaderScript::processFile(const fs::path &scriptPath) { + bool LoaderScript::processFile(const std::fs::path &scriptPath) { Py_SetProgramName(Py_DecodeLocale("ImHex", nullptr)); - for (const auto &dir : hex::getPath(ImHexPath::Python)) { - if (fs::exists(fs::path(dir / "lib" / "python" PYTHON_VERSION_MAJOR_MINOR))) { + for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Python)) { + if (fs::exists(std::fs::path(dir / "lib" / "python" PYTHON_VERSION_MAJOR_MINOR))) { Py_SetPythonHome(Py_DecodeLocale(dir.string().c_str(), nullptr)); break; } @@ -218,7 +218,7 @@ namespace hex { PyList_Insert(sysPath, 0, path); } - File scriptFile(scriptPath, File::Mode::Read); + fs::File scriptFile(scriptPath, fs::File::Mode::Read); PyRun_SimpleFile(scriptFile.getHandle(), scriptFile.getPath().string().c_str()); Py_Finalize(); diff --git a/lib/libimhex/source/helpers/logger.cpp b/lib/libimhex/source/helpers/logger.cpp index db6515bf5..0aba6b4e8 100644 --- a/lib/libimhex/source/helpers/logger.cpp +++ b/lib/libimhex/source/helpers/logger.cpp @@ -1,13 +1,13 @@ #include #include -#include +#include #include #include namespace hex::log { - static File g_loggerFile; + static fs::File g_loggerFile; FILE *getDestination() { if (g_loggerFile.isValid()) @@ -23,9 +23,9 @@ namespace hex::log { void redirectToFile() { if (g_loggerFile.isValid()) return; - for (const auto &path : hex::getPath(ImHexPath::Logs, true)) { - fs::create_directories(path); - g_loggerFile = File(path / hex::format("{0:%Y%m%d_%H%M%S}.log", fmt::localtime(std::chrono::system_clock::now())), File::Mode::Create); + for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Logs, true)) { + fs::createDirectories(path); + g_loggerFile = fs::File(path / hex::format("{0:%Y%m%d_%H%M%S}.log", fmt::localtime(std::chrono::system_clock::now())), fs::File::Mode::Create); g_loggerFile.disableBuffering(); if (g_loggerFile.isValid()) break; diff --git a/lib/libimhex/source/helpers/magic.cpp b/lib/libimhex/source/helpers/magic.cpp index eb768b95f..cde19afa3 100644 --- a/lib/libimhex/source/helpers/magic.cpp +++ b/lib/libimhex/source/helpers/magic.cpp @@ -1,7 +1,7 @@ #include #include -#include +#include #include @@ -24,8 +24,8 @@ namespace hex::magic { std::string magicFiles; std::error_code error; - for (const auto &dir : hex::getPath(ImHexPath::Magic)) { - for (const auto &entry : fs::directory_iterator(dir, error)) { + for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Magic)) { + for (const auto &entry : std::fs::directory_iterator(dir, error)) { if (entry.is_regular_file() && ((sourceFiles && entry.path().extension().empty()) || (!sourceFiles && entry.path().extension() == ".mgc"))) magicFiles += entry.path().string() + MAGIC_PATH_SEPARATOR; } diff --git a/lib/libimhex/source/helpers/net.cpp b/lib/libimhex/source/helpers/net.cpp index 3201f330b..a3043b4c2 100644 --- a/lib/libimhex/source/helpers/net.cpp +++ b/lib/libimhex/source/helpers/net.cpp @@ -171,7 +171,7 @@ namespace hex { }); } - std::future> Net::uploadFile(const std::string &url, const fs::path &filePath, u32 timeout) { + std::future> Net::uploadFile(const std::string &url, const std::fs::path &filePath, u32 timeout) { this->m_transmissionActive.lock(); return std::async(std::launch::async, [=, this] { @@ -179,7 +179,7 @@ namespace hex { ON_SCOPE_EXIT { this->m_transmissionActive.unlock(); }; - File file(filePath.string(), File::Mode::Read); + fs::File file(filePath.string(), fs::File::Mode::Read); if (!file.isValid()) return Response { 400, {} }; @@ -209,7 +209,7 @@ namespace hex { }); } - std::future> Net::downloadFile(const std::string &url, const fs::path &filePath, u32 timeout) { + std::future> Net::downloadFile(const std::string &url, const std::fs::path &filePath, u32 timeout) { this->m_transmissionActive.lock(); return std::async(std::launch::async, [=, this] { @@ -217,7 +217,7 @@ namespace hex { ON_SCOPE_EXIT { this->m_transmissionActive.unlock(); }; - File file(filePath.string(), File::Mode::Create); + fs::File file(filePath.string(), fs::File::Mode::Create); if (!file.isValid()) return Response { 400 }; diff --git a/lib/libimhex/source/helpers/project_file_handler.cpp b/lib/libimhex/source/helpers/project_file_handler.cpp index 85c6810ea..c3c71b0ce 100644 --- a/lib/libimhex/source/helpers/project_file_handler.cpp +++ b/lib/libimhex/source/helpers/project_file_handler.cpp @@ -9,10 +9,10 @@ using json = nlohmann::json; namespace hex { - fs::path ProjectFile::s_currProjectFilePath; + std::fs::path ProjectFile::s_currProjectFilePath; bool ProjectFile::s_hasUnsavedChanged = false; - fs::path ProjectFile::s_filePath; + std::fs::path ProjectFile::s_filePath; std::string ProjectFile::s_pattern; Patches ProjectFile::s_patches; std::list ProjectFile::s_bookmarks; @@ -46,7 +46,7 @@ namespace hex { } - bool ProjectFile::load(const fs::path &filePath) { + bool ProjectFile::load(const std::fs::path &filePath) { ProjectFile::s_hasUnsavedChanged = false; json projectFileData; @@ -55,7 +55,7 @@ namespace hex { std::ifstream projectFile(filePath.c_str()); projectFile >> projectFileData; - ProjectFile::s_filePath = fs::path(projectFileData["filePath"].get()); + ProjectFile::s_filePath = std::fs::path(projectFileData["filePath"].get()); ProjectFile::s_pattern = projectFileData["pattern"]; ProjectFile::s_patches = projectFileData["patches"].get(); ProjectFile::s_dataProcessorContent = projectFileData["dataProcessor"]; @@ -80,7 +80,7 @@ namespace hex { return true; } - bool ProjectFile::store(fs::path filePath) { + bool ProjectFile::store(std::fs::path filePath) { EventManager::post(); json projectFileData; diff --git a/lib/libimhex/source/helpers/utils.cpp b/lib/libimhex/source/helpers/utils.cpp index 3cecf2405..57272a74a 100644 --- a/lib/libimhex/source/helpers/utils.cpp +++ b/lib/libimhex/source/helpers/utils.cpp @@ -394,53 +394,6 @@ namespace hex { return result; } - bool isPathWritable(fs::path path) { - constexpr static auto TestFileName = "__imhex__tmp__"; - { - File file(path / TestFileName, File::Mode::Read); - if (file.isValid()) { - if (!file.remove()) - return false; - } - } - - File file(path / TestFileName, File::Mode::Create); - bool result = file.isValid(); - if (!file.remove()) - return false; - - return result; - } - - bool openFileBrowser(const std::string &title, DialogMode mode, const std::vector &validExtensions, const std::function &callback, const std::string &defaultPath) { - NFD::Init(); - - nfdchar_t *outPath; - nfdresult_t result; - switch (mode) { - case DialogMode::Open: - result = NFD::OpenDialog(outPath, validExtensions.data(), validExtensions.size(), defaultPath.c_str()); - break; - case DialogMode::Save: - result = NFD::SaveDialog(outPath, validExtensions.data(), validExtensions.size(), defaultPath.c_str()); - break; - case DialogMode::Folder: - result = NFD::PickFolder(outPath, defaultPath.c_str()); - break; - default: - __builtin_unreachable(); - } - - if (result == NFD_OKAY) { - callback(reinterpret_cast(outPath)); - NFD::FreePath(outPath); - } - - NFD::Quit(); - - return result == NFD_OKAY; - } - float float16ToFloat32(u16 float16) { u32 sign = float16 >> 15; u32 exponent = (float16 >> 10) & 0x1F; diff --git a/lib/libimhex/source/pattern_language/pattern_language.cpp b/lib/libimhex/source/pattern_language/pattern_language.cpp index 7e6db842a..2bf252a32 100644 --- a/lib/libimhex/source/pattern_language/pattern_language.cpp +++ b/lib/libimhex/source/pattern_language/pattern_language.cpp @@ -184,8 +184,8 @@ namespace hex::pl { return true; } - bool PatternLanguage::executeFile(prv::Provider *provider, const fs::path &path, const std::map &envVars, const std::map &inVariables) { - File file(path, File::Mode::Read); + bool PatternLanguage::executeFile(prv::Provider *provider, const std::fs::path &path, const std::map &envVars, const std::map &inVariables) { + fs::File file(path, fs::File::Mode::Read); return this->executeString(provider, file.readString(), envVars, inVariables, true); } diff --git a/lib/libimhex/source/pattern_language/preprocessor.cpp b/lib/libimhex/source/pattern_language/preprocessor.cpp index b603ca2a0..d374ff784 100644 --- a/lib/libimhex/source/pattern_language/preprocessor.cpp +++ b/lib/libimhex/source/pattern_language/preprocessor.cpp @@ -1,7 +1,7 @@ #include #include -#include +#include #include #include @@ -59,26 +59,26 @@ namespace hex::pl { } offset += 1; - fs::path includePath = includeFile; + std::fs::path includePath = includeFile; if (includeFile[0] != '/') { - for (const auto &dir : hex::getPath(ImHexPath::PatternsInclude)) { - fs::path tempPath = dir / includePath; - if (fs::is_regular_file(tempPath)) { + for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::PatternsInclude)) { + std::fs::path tempPath = dir / includePath; + if (fs::isRegularFile(tempPath)) { includePath = tempPath; break; } } } - if (!fs::is_regular_file(includePath)) { + if (!fs::isRegularFile(includePath)) { if (includePath.parent_path().filename().string() == "std") throwPreprocessorError(hex::format("{0}: No such file.\n\nThis file might be part of the standard library.\nYou can install the standard library though\nthe Content Store found under Help -> Content Store.", includeFile.c_str()), lineNumber); else throwPreprocessorError(hex::format("{0}: No such file", includeFile.c_str()), lineNumber); } - File file(includePath, File::Mode::Read); + fs::File file(includePath, fs::File::Mode::Read); if (!file.isValid()) { throwPreprocessorError(hex::format("{0}: Failed to open file", includeFile.c_str()), lineNumber); } diff --git a/lib/libimhex/source/providers/provider.cpp b/lib/libimhex/source/providers/provider.cpp index 98ad48509..b781d3070 100644 --- a/lib/libimhex/source/providers/provider.cpp +++ b/lib/libimhex/source/providers/provider.cpp @@ -37,7 +37,7 @@ namespace hex::prv { } void Provider::save() { } - void Provider::saveAs(const fs::path &path) { } + void Provider::saveAs(const std::fs::path &path) { } void Provider::resize(size_t newSize) { } diff --git a/lib/libimhex/source/ui/view.cpp b/lib/libimhex/source/ui/view.cpp index 849046525..8897ec8c9 100644 --- a/lib/libimhex/source/ui/view.cpp +++ b/lib/libimhex/source/ui/view.cpp @@ -13,8 +13,8 @@ namespace hex { std::function View::s_yesCallback, View::s_noCallback; u32 View::s_selectableFileIndex; - std::vector View::s_selectableFiles; - std::function View::s_selectableFileOpenCallback; + std::vector View::s_selectableFiles; + std::function View::s_selectableFileOpenCallback; std::vector View::s_selectableFilesValidExtensions; ImFontAtlas *View::s_fontAtlas; @@ -108,7 +108,7 @@ namespace hex { ImGui::SameLine(); if (ImGui::Button("hex.builtin.common.browse"_lang)) { - hex::openFileBrowser("hex.builtin.common.open"_lang, DialogMode::Open, View::s_selectableFilesValidExtensions, [](const auto &path) { + fs::openFileBrowser("hex.builtin.common.open"_lang, fs::DialogMode::Open, View::s_selectableFilesValidExtensions, [](const auto &path) { View::s_selectableFileOpenCallback(path); ImGui::CloseCurrentPopup(); }); @@ -145,9 +145,9 @@ namespace hex { ImHexApi::Tasks::doLater([] { ImGui::OpenPopup("hex.builtin.common.question"_lang); }); } - void View::showFileChooserPopup(const std::vector &paths, const std::vector &validExtensions, const std::function &callback) { + void View::showFileChooserPopup(const std::vector &paths, const std::vector &validExtensions, const std::function &callback) { if (paths.empty()) { - hex::openFileBrowser("hex.builtin.common.open"_lang, DialogMode::Open, validExtensions, [callback](const auto &path) { + fs::openFileBrowser("hex.builtin.common.open"_lang, fs::DialogMode::Open, validExtensions, [callback](const auto &path) { callback(path); }); } else { diff --git a/main/include/window.hpp b/main/include/window.hpp index 1c5901e14..666eaad0d 100644 --- a/main/include/window.hpp +++ b/main/include/window.hpp @@ -53,7 +53,7 @@ namespace hex { std::list m_popupsToOpen; std::vector m_pressedKeys; - fs::path m_imguiSettingsPath; + std::fs::path m_imguiSettingsPath; }; } \ No newline at end of file diff --git a/main/source/init/tasks.cpp b/main/source/init/tasks.cpp index 6d8c9b9cf..9144662a0 100644 --- a/main/source/init/tasks.cpp +++ b/main/source/init/tasks.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include @@ -60,23 +60,23 @@ namespace hex::init { bool result = true; constexpr std::array paths = { - ImHexPath::Patterns, - ImHexPath::PatternsInclude, - ImHexPath::Magic, - ImHexPath::Plugins, - ImHexPath::Resources, - ImHexPath::Config, - ImHexPath::Constants, - ImHexPath::Yara, - ImHexPath::Encodings, - ImHexPath::Python, - ImHexPath::Logs + fs::ImHexPath::Patterns, + fs::ImHexPath::PatternsInclude, + fs::ImHexPath::Magic, + fs::ImHexPath::Plugins, + fs::ImHexPath::Resources, + fs::ImHexPath::Config, + fs::ImHexPath::Constants, + fs::ImHexPath::Yara, + fs::ImHexPath::Encodings, + fs::ImHexPath::Python, + fs::ImHexPath::Logs }; for (auto path : paths) { - for (auto &folder : hex::getPath(path, true)) { + for (auto &folder : fs::getDefaultPaths(path, true)) { try { - fs::create_directories(folder); + fs::createDirectories(folder); } catch (...) { log::error("Failed to create folder {}!", folder.string()); result = false; @@ -94,11 +94,11 @@ namespace hex::init { auto fonts = IM_NEW(ImFontAtlas)(); ImFontConfig cfg = {}; - fs::path fontFile = ContentRegistry::Settings::read("hex.builtin.setting.font", "hex.builtin.setting.font.font_path", ""); + std::fs::path fontFile = ContentRegistry::Settings::read("hex.builtin.setting.font", "hex.builtin.setting.font.font_path", ""); // If no custom font has been specified, search for a file called "font.ttf" in one of the resource folders if (fontFile.empty()) { - for (const auto &dir : hex::getPath(ImHexPath::Resources)) { + for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Resources)) { auto path = dir / "font.ttf"; if (fs::exists(path)) { log::info("Loading custom front from {}", path.string()); @@ -218,7 +218,7 @@ namespace hex::init { } bool loadPlugins() { - for (const auto &dir : hex::getPath(ImHexPath::Plugins)) { + for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Plugins)) { PluginManager::load(dir); } diff --git a/main/source/window/window.cpp b/main/source/window/window.cpp index 350626a7c..3948289d8 100644 --- a/main/source/window/window.cpp +++ b/main/source/window/window.cpp @@ -7,7 +7,7 @@ #include #include -#include +#include #include #include @@ -123,8 +123,8 @@ namespace hex { if (!ProjectFile::hasUnsavedChanges()) return; - for (const auto &path : hex::getPath(ImHexPath::Config)) { - if (ProjectFile::store((fs::path(path) / CrashBackupFileName).string())) + for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Config)) { + if (ProjectFile::store((std::fs::path(path) / CrashBackupFileName).string())) break; } }); @@ -352,7 +352,7 @@ namespace hex { ImGui::TableHeadersRow(); - for (const auto &path : hex::getPath(ImHexPath::Plugins, true)) { + for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Plugins, true)) { ImGui::TableNextRow(); ImGui::TableNextColumn(); ImGui::TextUnformatted(path.string().c_str()); @@ -720,7 +720,7 @@ namespace hex { handler.UserData = this; ImGui::GetCurrentContext()->SettingsHandlers.push_back(handler); - for (const auto &dir : hex::getPath(ImHexPath::Config)) { + for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Config)) { if (std::filesystem::exists(dir)) { this->m_imguiSettingsPath = dir / "interface.ini"; break; diff --git a/plugins/builtin/include/content/providers/disk_provider.hpp b/plugins/builtin/include/content/providers/disk_provider.hpp index f87bdb249..a587ab143 100644 --- a/plugins/builtin/include/content/providers/disk_provider.hpp +++ b/plugins/builtin/include/content/providers/disk_provider.hpp @@ -27,7 +27,7 @@ namespace hex::plugin::builtin::prv { void writeRaw(u64 offset, const void *buffer, size_t size) override; [[nodiscard]] size_t getActualSize() const override; - void setPath(const fs::path &path); + void setPath(const std::fs::path &path); [[nodiscard]] bool open() override; void close() override; @@ -42,7 +42,7 @@ namespace hex::plugin::builtin::prv { void reloadDrives(); std::set m_availableDrives; - fs::path m_path; + std::fs::path m_path; #if defined(OS_WINDOWS) HANDLE m_diskHandle = INVALID_HANDLE_VALUE; diff --git a/plugins/builtin/include/content/providers/file_provider.hpp b/plugins/builtin/include/content/providers/file_provider.hpp index 62ef90a81..40ba13ab2 100644 --- a/plugins/builtin/include/content/providers/file_provider.hpp +++ b/plugins/builtin/include/content/providers/file_provider.hpp @@ -38,12 +38,12 @@ namespace hex::plugin::builtin::prv { [[nodiscard]] size_t getActualSize() const override; void save() override; - void saveAs(const fs::path &path) override; + void saveAs(const std::fs::path &path) override; [[nodiscard]] std::string getName() const override; [[nodiscard]] std::vector> getDataInformation() const override; - void setPath(const fs::path &path); + void setPath(const std::fs::path &path); [[nodiscard]] bool open() override; void close() override; @@ -56,7 +56,7 @@ namespace hex::plugin::builtin::prv { int m_file = -1; #endif - fs::path m_path; + std::fs::path m_path; void *m_mappedFile = nullptr; size_t m_fileSize = 0; diff --git a/plugins/builtin/include/content/providers/gdb_provider.hpp b/plugins/builtin/include/content/providers/gdb_provider.hpp index 819db97e1..667def707 100644 --- a/plugins/builtin/include/content/providers/gdb_provider.hpp +++ b/plugins/builtin/include/content/providers/gdb_provider.hpp @@ -29,7 +29,7 @@ namespace hex::plugin::builtin::prv { [[nodiscard]] size_t getActualSize() const override; void save() override; - void saveAs(const fs::path &path) override; + void saveAs(const std::fs::path &path) override; [[nodiscard]] std::string getName() const override; [[nodiscard]] std::vector> getDataInformation() const override; diff --git a/plugins/builtin/include/content/views/view_hex_editor.hpp b/plugins/builtin/include/content/views/view_hex_editor.hpp index d11a0d59a..2e2c2f32b 100644 --- a/plugins/builtin/include/content/views/view_hex_editor.hpp +++ b/plugins/builtin/include/content/views/view_hex_editor.hpp @@ -82,7 +82,7 @@ namespace hex::plugin::builtin { void drawGotoPopup(); void drawEditPopup(); - void openFile(const fs::path &path); + void openFile(const std::fs::path &path); void copyBytes() const; void pasteBytes() const; diff --git a/plugins/builtin/include/content/views/view_pattern_editor.hpp b/plugins/builtin/include/content/views/view_pattern_editor.hpp index 16d9d4a36..78a7bef07 100644 --- a/plugins/builtin/include/content/views/view_pattern_editor.hpp +++ b/plugins/builtin/include/content/views/view_pattern_editor.hpp @@ -26,7 +26,7 @@ namespace hex::plugin::builtin { private: pl::PatternLanguage *m_parserRuntime; - std::vector m_possiblePatternFiles; + std::vector m_possiblePatternFiles; u32 m_selectedPatternFile = 0; bool m_runAutomatically = false; @@ -77,7 +77,7 @@ namespace hex::plugin::builtin { void drawEnvVars(ImVec2 size); void drawVariableSettings(ImVec2 size); - void loadPatternFile(const fs::path &path); + void loadPatternFile(const std::fs::path &path); void clearPatterns(); void parsePattern(const std::string &code); diff --git a/plugins/builtin/include/content/views/view_store.hpp b/plugins/builtin/include/content/views/view_store.hpp index 3b5e260d5..faf07a029 100644 --- a/plugins/builtin/include/content/views/view_store.hpp +++ b/plugins/builtin/include/content/views/view_store.hpp @@ -4,7 +4,7 @@ #include #include -#include +#include #include #include @@ -44,7 +44,7 @@ namespace hex::plugin::builtin { Net m_net; std::future> m_apiRequest; std::future> m_download; - fs::path m_downloadPath; + std::fs::path m_downloadPath; std::vector m_patterns, m_includes, m_magics, m_constants, m_yara, m_encodings; @@ -53,8 +53,8 @@ namespace hex::plugin::builtin { void refresh(); void parseResponse(); - bool download(ImHexPath pathType, const std::string &fileName, const std::string &url, bool update); - bool remove(ImHexPath pathType, const std::string &fileName); + bool download(fs::ImHexPath pathType, const std::string &fileName, const std::string &url, bool update); + bool remove(fs::ImHexPath pathType, const std::string &fileName); }; } \ No newline at end of file diff --git a/plugins/builtin/source/content/pl_builtin_functions.cpp b/plugins/builtin/source/content/pl_builtin_functions.cpp index 8c3193165..c52dabc2a 100644 --- a/plugins/builtin/source/content/pl_builtin_functions.cpp +++ b/plugins/builtin/source/content/pl_builtin_functions.cpp @@ -254,29 +254,29 @@ namespace hex::plugin::builtin { ContentRegistry::PatternLanguage::Namespace nsStdFile = { "builtin", "std", "file" }; { static u32 fileCounter = 0; - static std::map openFiles; + static std::map openFiles; /* open(path, mode) */ ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "open", 2, [](Evaluator *ctx, auto params) -> std::optional { const auto path = Token::literalToString(params[0], false); const auto modeEnum = Token::literalToUnsigned(params[1]); - File::Mode mode; + fs::File::Mode mode; switch (modeEnum) { case 1: - mode = File::Mode::Read; + mode = fs::File::Mode::Read; break; case 2: - mode = File::Mode::Write; + mode = fs::File::Mode::Write; break; case 3: - mode = File::Mode::Create; + mode = fs::File::Mode::Create; break; default: LogConsole::abortEvaluation("invalid file open mode"); } - auto file = File(path, mode); + fs::File file(path, mode); if (!file.isValid()) LogConsole::abortEvaluation(hex::format("failed to open file {}", path)); diff --git a/plugins/builtin/source/content/providers/disk_provider.cpp b/plugins/builtin/source/content/providers/disk_provider.cpp index 22242e2fb..6915d02b1 100644 --- a/plugins/builtin/source/content/providers/disk_provider.cpp +++ b/plugins/builtin/source/content/providers/disk_provider.cpp @@ -60,7 +60,7 @@ namespace hex::plugin::builtin::prv { } - void DiskProvider::setPath(const fs::path &path) { + void DiskProvider::setPath(const std::fs::path &path) { this->m_path = path; } diff --git a/plugins/builtin/source/content/providers/file_provider.cpp b/plugins/builtin/source/content/providers/file_provider.cpp index a4e8a0942..757db4efc 100644 --- a/plugins/builtin/source/content/providers/file_provider.cpp +++ b/plugins/builtin/source/content/providers/file_provider.cpp @@ -1,6 +1,5 @@ #include "content/providers/file_provider.hpp" -#include #include #include @@ -83,8 +82,8 @@ namespace hex::plugin::builtin::prv { this->applyPatches(); } - void FileProvider::saveAs(const fs::path &path) { - File file(path, File::Mode::Create); + void FileProvider::saveAs(const std::fs::path &path) { + fs::File file(path, fs::File::Mode::Create); if (file.isValid()) { auto provider = ImHexApi::Provider::get(); @@ -106,7 +105,7 @@ namespace hex::plugin::builtin::prv { this->close(); { - auto file = File(this->m_path, File::Mode::Write); + fs::File file(this->m_path, fs::File::Mode::Write); file.setSize(newSize); this->m_fileSize = file.getSize(); @@ -141,7 +140,7 @@ namespace hex::plugin::builtin::prv { } std::string FileProvider::getName() const { - return fs::path(this->m_path).filename().string(); + return std::fs::path(this->m_path).filename().string(); } std::vector> FileProvider::getDataInformation() const { @@ -159,7 +158,7 @@ namespace hex::plugin::builtin::prv { return result; } - void FileProvider::setPath(const fs::path &path) { + void FileProvider::setPath(const std::fs::path &path) { this->m_path = path; } diff --git a/plugins/builtin/source/content/providers/gdb_provider.cpp b/plugins/builtin/source/content/providers/gdb_provider.cpp index 1a34f3a05..9d6cf4448 100644 --- a/plugins/builtin/source/content/providers/gdb_provider.cpp +++ b/plugins/builtin/source/content/providers/gdb_provider.cpp @@ -217,7 +217,7 @@ namespace hex::plugin::builtin::prv { this->applyPatches(); } - void GDBProvider::saveAs(const fs::path &path) { + void GDBProvider::saveAs(const std::fs::path &path) { } size_t GDBProvider::getActualSize() const { diff --git a/plugins/builtin/source/content/settings_entries.cpp b/plugins/builtin/source/content/settings_entries.cpp index fe05840b4..afce09630 100644 --- a/plugins/builtin/source/content/settings_entries.cpp +++ b/plugins/builtin/source/content/settings_entries.cpp @@ -238,10 +238,10 @@ namespace hex::plugin::builtin { ImGui::SameLine(); if (ImGui::IconButton(ICON_VS_FOLDER_OPENED, ImGui::GetStyleColorVec4(ImGuiCol_Text))) { - return hex::openFileBrowser("hex.builtin.setting.font.font_path", DialogMode::Open, { - {"TTF Font", "ttf"} + return fs::openFileBrowser("hex.builtin.setting.font.font_path", fs::DialogMode::Open, { + {"TTF Font", "ttf"} }, - [&](const fs::path &path) { + [&](const std::fs::path &path) { fontPath = path.string(); setting = fontPath; }); @@ -294,7 +294,7 @@ namespace hex::plugin::builtin { ImGui::BeginGroup(); if (ImGui::IconButton(ICON_VS_NEW_FOLDER, ImGui::GetCustomColorVec4(ImGuiCustomCol_DescButton), ImVec2(30, 30))) { - hex::openFileBrowser("Select include folder", hex::DialogMode::Folder, {}, [&](fs::path path) { + fs::openFileBrowser("Select include folder", fs::DialogMode::Folder, {}, [&](std::fs::path path) { auto pathStr = path.string(); if (std::find(folders.begin(), folders.end(), pathStr) == folders.end()) { diff --git a/plugins/builtin/source/content/tools_entries.cpp b/plugins/builtin/source/content/tools_entries.cpp index 7904add87..aba27ac17 100644 --- a/plugins/builtin/source/content/tools_entries.cpp +++ b/plugins/builtin/source/content/tools_entries.cpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include @@ -571,7 +571,7 @@ namespace hex::plugin::builtin { static hex::Net net; static std::future> uploadProcess; - static fs::path currFile; + static std::fs::path currFile; static std::vector links; bool uploading = uploadProcess.valid() && uploadProcess.wait_for(0s) != std::future_status::ready; @@ -579,7 +579,7 @@ namespace hex::plugin::builtin { ImGui::Header("hex.builtin.tools.file_uploader.control"_lang, true); if (!uploading) { if (ImGui::Button("hex.builtin.tools.file_uploader.upload"_lang)) { - hex::openFileBrowser("hex.builtin.tools.file_uploader.done"_lang, DialogMode::Open, {}, [&](auto path) { + fs::openFileBrowser("hex.builtin.tools.file_uploader.done"_lang, fs::DialogMode::Open, {}, [&](auto path) { uploadProcess = net.uploadFile("https://api.anonfiles.com/upload", path); currFile = path; }); @@ -744,7 +744,7 @@ namespace hex::plugin::builtin { ImGui::InputText("##path", selectedFile); ImGui::SameLine(); if (ImGui::Button("...")) { - hex::openFileBrowser("hex.builtin.tools.file_tools.shredder.picker"_lang, DialogMode::Open, {}, [](const auto &path) { + fs::openFileBrowser("hex.builtin.tools.file_tools.shredder.picker"_lang, fs::DialogMode::Open, {}, [](const auto &path) { selectedFile = path.string(); }); } @@ -768,7 +768,7 @@ namespace hex::plugin::builtin { shredding = false; selectedFile.clear(); }; - File file(selectedFile, File::Mode::Write); + fs::File file(selectedFile, fs::File::Mode::Write); if (!file.isValid()) { View::showErrorPopup("hex.builtin.tools.file_tools.shredder.error.open"_lang); @@ -886,7 +886,7 @@ namespace hex::plugin::builtin { ImGui::InputText("##path", selectedFile); ImGui::SameLine(); if (ImGui::Button("...##input")) { - hex::openFileBrowser("hex.builtin.tools.file_tools.splitter.picker.input"_lang, DialogMode::Open, {}, [](const auto &path) { + fs::openFileBrowser("hex.builtin.tools.file_tools.splitter.picker.input"_lang, fs::DialogMode::Open, {}, [](const auto &path) { selectedFile = path.string(); }); } @@ -896,7 +896,7 @@ namespace hex::plugin::builtin { ImGui::InputText("##base_path", baseOutputPath); ImGui::SameLine(); if (ImGui::Button("...##output")) { - hex::openFileBrowser("hex.builtin.tools.file_tools.splitter.picker.output"_lang, DialogMode::Save, {}, [](const auto &path) { + fs::openFileBrowser("hex.builtin.tools.file_tools.splitter.picker.output"_lang, fs::DialogMode::Save, {}, [](const auto &path) { baseOutputPath = path.string(); }); } @@ -934,7 +934,7 @@ namespace hex::plugin::builtin { selectedFile.clear(); baseOutputPath.clear(); }; - File file(selectedFile, File::Mode::Read); + fs::File file(selectedFile, fs::File::Mode::Read); if (!file.isValid()) { View::showErrorPopup("hex.builtin.tools.file_tools.splitter.error.open"_lang); @@ -951,7 +951,7 @@ namespace hex::plugin::builtin { for (u64 offset = 0; offset < file.getSize(); offset += splitSize) { task.update(offset); - File partFile(baseOutputPath + hex::format(".{:05}", index), File::Mode::Create); + fs::File partFile(baseOutputPath + hex::format(".{:05}", index), fs::File::Mode::Create); if (!partFile.isValid()) { View::showErrorPopup(hex::format("hex.builtin.tools.file_tools.splitter.error.create"_lang, index)); @@ -991,7 +991,7 @@ namespace hex::plugin::builtin { i32 index = 0; for (auto &file : files) { - if (ImGui::Selectable(fs::path(file).filename().string().c_str(), index == selectedIndex)) + if (ImGui::Selectable(std::fs::path(file).filename().string().c_str(), index == selectedIndex)) selectedIndex = index; index++; } @@ -1025,7 +1025,7 @@ namespace hex::plugin::builtin { ImGui::BeginDisabled(combining); { if (ImGui::Button("hex.builtin.tools.file_tools.combiner.add"_lang)) { - hex::openFileBrowser("hex.builtin.tools.file_tools.combiner.add.picker"_lang, DialogMode::Open, {}, [](const auto &path) { + fs::openFileBrowser("hex.builtin.tools.file_tools.combiner.add.picker"_lang, fs::DialogMode::Open, {}, [](const auto &path) { files.push_back(path.string()); }); } @@ -1049,7 +1049,7 @@ namespace hex::plugin::builtin { ImGui::InputText("##output_path", outputPath); ImGui::SameLine(); if (ImGui::Button("...")) { - hex::openFileBrowser("hex.builtin.tools.file_tools.combiner.output.picker"_lang, DialogMode::Save, {}, [](const auto &path) { + fs::openFileBrowser("hex.builtin.tools.file_tools.combiner.output.picker"_lang, fs::DialogMode::Save, {}, [](const auto &path) { outputPath = path.string(); }); } @@ -1069,7 +1069,7 @@ namespace hex::plugin::builtin { std::thread([] { ON_SCOPE_EXIT { combining = false; }; - File output(outputPath, File::Mode::Create); + fs::File output(outputPath, fs::File::Mode::Create); if (!output.isValid()) { View::showErrorPopup("hex.builtin.tools.file_tools.combiner.error.open_output"_lang); @@ -1083,9 +1083,9 @@ namespace hex::plugin::builtin { task.update(fileIndex); fileIndex++; - File input(file, File::Mode::Read); + fs::File input(file, fs::File::Mode::Read); if (!input.isValid()) { - View::showErrorPopup(hex::format("hex.builtin.tools.file_tools.combiner.open_input"_lang, fs::path(file).filename().string())); + View::showErrorPopup(hex::format("hex.builtin.tools.file_tools.combiner.open_input"_lang, std::fs::path(file).filename().string())); return; } diff --git a/plugins/builtin/source/content/ui_items.cpp b/plugins/builtin/source/content/ui_items.cpp index 3511ab2d9..f3e293fb8 100644 --- a/plugins/builtin/source/content/ui_items.cpp +++ b/plugins/builtin/source/content/ui_items.cpp @@ -104,7 +104,7 @@ namespace hex::plugin::builtin { // Save file as ImGui::Disabled([&provider] { if (ImGui::ToolBarButton(ICON_VS_SAVE_AS, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarBlue))) - hex::openFileBrowser("hex.builtin.view.hex_editor.save_as"_lang, DialogMode::Save, {}, [&provider](auto path) { + fs::openFileBrowser("hex.builtin.view.hex_editor.save_as"_lang, fs::DialogMode::Save, {}, [&provider](auto path) { provider->saveAs(path); }); }, diff --git a/plugins/builtin/source/content/views/view_constants.cpp b/plugins/builtin/source/content/views/view_constants.cpp index 486a1f1d9..91a138578 100644 --- a/plugins/builtin/source/content/views/view_constants.cpp +++ b/plugins/builtin/source/content/views/view_constants.cpp @@ -1,6 +1,6 @@ #include "content/views/view_constants.hpp" -#include +#include #include #include @@ -24,10 +24,11 @@ namespace hex::plugin::builtin { this->m_constants.clear(); this->m_filterIndices.clear(); - for (const auto &path : hex::getPath(ImHexPath::Constants)) { + for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Constants)) { if (!fs::exists(path)) continue; - for (auto &file : fs::directory_iterator(path)) { + std::error_code error; + for (auto &file : std::fs::directory_iterator(path, error)) { if (!file.is_regular_file()) continue; try { diff --git a/plugins/builtin/source/content/views/view_data_processor.cpp b/plugins/builtin/source/content/views/view_data_processor.cpp index bad771054..4680455f1 100644 --- a/plugins/builtin/source/content/views/view_data_processor.cpp +++ b/plugins/builtin/source/content/views/view_data_processor.cpp @@ -41,7 +41,7 @@ namespace hex::plugin::builtin { } }); - EventManager::subscribe(this, [this](const fs::path &path) { + EventManager::subscribe(this, [this](const std::fs::path &path) { for (auto &node : this->m_nodes) { node->setCurrentOverlay(nullptr); } @@ -54,22 +54,22 @@ namespace hex::plugin::builtin { ContentRegistry::Interface::addMenuItem("hex.builtin.menu.file", 3000, [&, this] { if (ImGui::MenuItem("hex.builtin.view.data_processor.menu.file.load_processor"_lang)) { - hex::openFileBrowser("hex.builtin.view.data_processor.menu.file.load_processor"_lang, DialogMode::Open, { - {"hex.builtin.view.data_processor.name"_lang, "hexnode"} + fs::openFileBrowser("hex.builtin.view.data_processor.menu.file.load_processor"_lang, fs::DialogMode::Open, { + {"hex.builtin.view.data_processor.name"_lang, "hexnode"} }, - [this](const fs::path &path) { - File file(path, File::Mode::Read); + [this](const std::fs::path &path) { + fs::File file(path, fs::File::Mode::Read); if (file.isValid()) this->loadNodes(file.readString()); }); } if (ImGui::MenuItem("hex.builtin.view.data_processor.menu.file.save_processor"_lang, nullptr, false, !this->m_nodes.empty())) { - hex::openFileBrowser("hex.builtin.view.data_processor.menu.file.save_processor"_lang, DialogMode::Save, { - {"hex.builtin.view.data_processor.name"_lang, "hexnode"} + fs::openFileBrowser("hex.builtin.view.data_processor.menu.file.save_processor"_lang, fs::DialogMode::Save, { + {"hex.builtin.view.data_processor.name"_lang, "hexnode"} }, - [this](const fs::path &path) { - File file(path, File::Mode::Create); + [this](const std::fs::path &path) { + fs::File file(path, fs::File::Mode::Create); if (file.isValid()) file.write(this->saveNodes()); }); @@ -77,7 +77,7 @@ namespace hex::plugin::builtin { }); ContentRegistry::FileHandler::add({ ".hexnode" }, [this](const auto &path) { - File file(path, File::Mode::Read); + fs::File file(path, fs::File::Mode::Read); if (!file.isValid()) return false; this->loadNodes(file.readString()); diff --git a/plugins/builtin/source/content/views/view_help.cpp b/plugins/builtin/source/content/views/view_help.cpp index f41b7adda..942e6a06e 100644 --- a/plugins/builtin/source/content/views/view_help.cpp +++ b/plugins/builtin/source/content/views/view_help.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include #include #include @@ -119,15 +119,15 @@ namespace hex::plugin::builtin { ImGui::TableSetupColumn("Type"); ImGui::TableSetupColumn("Paths"); - constexpr std::array, 8> PathTypes = { - {{ "Resources", ImHexPath::Resources }, - { "Config", ImHexPath::Config }, - { "Magic", ImHexPath::Magic }, - { "Patterns", ImHexPath::Patterns }, - { "Patterns Includes", ImHexPath::PatternsInclude }, - { "Plugins", ImHexPath::Plugins }, - { "Python Scripts", ImHexPath::Python }, - { "Yara Patterns", ImHexPath::Yara }} + constexpr std::array, 8> PathTypes = { + {{ "Resources", fs::ImHexPath::Resources }, + { "Config", fs::ImHexPath::Config }, + { "Magic", fs::ImHexPath::Magic }, + { "Patterns", fs::ImHexPath::Patterns }, + { "Patterns Includes", fs::ImHexPath::PatternsInclude }, + { "Plugins", fs::ImHexPath::Plugins }, + { "Python Scripts", fs::ImHexPath::Python }, + { "Yara Patterns", fs::ImHexPath::Yara }} }; ImGui::TableHeadersRow(); @@ -137,7 +137,7 @@ namespace hex::plugin::builtin { ImGui::TextUnformatted(name); ImGui::TableNextColumn(); - for (auto &path : hex::getPath(type)) + for (auto &path : fs::getDefaultPaths(type)) ImGui::TextUnformatted(path.string().c_str()); } diff --git a/plugins/builtin/source/content/views/view_hex_editor.cpp b/plugins/builtin/source/content/views/view_hex_editor.cpp index a03f82d11..344d9b2a0 100644 --- a/plugins/builtin/source/content/views/view_hex_editor.cpp +++ b/plugins/builtin/source/content/views/view_hex_editor.cpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include #include @@ -267,7 +267,7 @@ namespace hex::plugin::builtin { } static void saveAs() { - hex::openFileBrowser("hex.builtin.view.hex_editor.save_as"_lang, DialogMode::Save, {}, [](const auto &path) { + fs::openFileBrowser("hex.builtin.view.hex_editor.save_as"_lang, fs::DialogMode::Save, {}, [](const auto &path) { ImHexApi::Provider::get()->saveAs(path); }); } @@ -297,8 +297,8 @@ namespace hex::plugin::builtin { ImGui::InputText("##nolabel", this->m_loaderScriptScriptPath.data(), this->m_loaderScriptScriptPath.length(), ImGuiInputTextFlags_ReadOnly); ImGui::SameLine(); if (ImGui::Button("hex.builtin.view.hex_editor.script.script"_lang)) { - hex::openFileBrowser("hex.builtin.view.hex_editor.script.script.title"_lang, DialogMode::Open, { - {"Python Script", "py"} + fs::openFileBrowser("hex.builtin.view.hex_editor.script.script.title"_lang, fs::DialogMode::Open, { + {"Python Script", "py"} }, [this](const auto &path) { this->m_loaderScriptScriptPath = path.string(); @@ -307,7 +307,7 @@ namespace hex::plugin::builtin { ImGui::InputText("##nolabel", this->m_loaderScriptFilePath.data(), this->m_loaderScriptFilePath.length(), ImGuiInputTextFlags_ReadOnly); ImGui::SameLine(); if (ImGui::Button("hex.builtin.view.hex_editor.script.file"_lang)) { - hex::openFileBrowser("hex.builtin.view.hex_editor.script.file.title"_lang, DialogMode::Open, {}, [this](const auto &path) { + fs::openFileBrowser("hex.builtin.view.hex_editor.script.file.title"_lang, fs::DialogMode::Open, {}, [this](const auto &path) { this->m_loaderScriptFilePath = path.string(); }); } @@ -379,7 +379,7 @@ namespace hex::plugin::builtin { } } - void ViewHexEditor::openFile(const fs::path &path) { + void ViewHexEditor::openFile(const std::fs::path &path) { hex::prv::Provider *provider = nullptr; EventManager::post("hex.builtin.provider.file", &provider); @@ -863,10 +863,10 @@ namespace hex::plugin::builtin { } }); - EventManager::subscribe(this, [this](std::string name) { + EventManager::subscribe(this, [this](const std::string &name) { if (name == "Create File") { - hex::openFileBrowser("hex.builtin.view.hex_editor.create_file"_lang, DialogMode::Save, {}, [this](const auto &path) { - File file(path, File::Mode::Create); + fs::openFileBrowser("hex.builtin.view.hex_editor.create_file"_lang, fs::DialogMode::Save, {}, [this](const auto &path) { + fs::File file(path, fs::File::Mode::Create); if (!file.isValid()) { View::showErrorPopup("hex.builtin.view.hex_editor.error.create"_lang); @@ -879,13 +879,13 @@ namespace hex::plugin::builtin { this->getWindowOpenState() = true; }); } else if (name == "Open File") { - hex::openFileBrowser("hex.builtin.view.hex_editor.open_file"_lang, DialogMode::Open, {}, [this](const auto &path) { + fs::openFileBrowser("hex.builtin.view.hex_editor.open_file"_lang, fs::DialogMode::Open, {}, [this](const auto &path) { EventManager::post(path); this->getWindowOpenState() = true; }); } else if (name == "Open Project") { - hex::openFileBrowser("hex.builtin.view.hex_editor.open_project"_lang, DialogMode::Open, { - {"Project File", "hexproj"} + fs::openFileBrowser("hex.builtin.view.hex_editor.open_project"_lang, fs::DialogMode::Open, { + {"Project File", "hexproj"} }, [this](const auto &path) { ProjectFile::load(path); @@ -998,7 +998,7 @@ namespace hex::plugin::builtin { }); ShortcutManager::addShortcut(this, CTRL + Keys::O, [] { - hex::openFileBrowser("hex.builtin.view.hex_editor.open_file"_lang, DialogMode::Open, {}, [](const auto &path) { + fs::openFileBrowser("hex.builtin.view.hex_editor.open_file"_lang, fs::DialogMode::Open, {}, [](const auto &path) { EventManager::post(path); }); }); @@ -1057,8 +1057,8 @@ namespace hex::plugin::builtin { bool providerValid = ImHexApi::Provider::isValid(); if (ImGui::MenuItem("hex.builtin.view.hex_editor.menu.file.open_project"_lang, "")) { - hex::openFileBrowser("hex.builtin.view.hex_editor.menu.file.open_project"_lang, DialogMode::Open, { - {"Project File", "hexproj"} + fs::openFileBrowser("hex.builtin.view.hex_editor.menu.file.open_project"_lang, fs::DialogMode::Open, { + {"Project File", "hexproj"} }, [](const auto &path) { ProjectFile::load(path); @@ -1067,10 +1067,10 @@ namespace hex::plugin::builtin { if (ImGui::MenuItem("hex.builtin.view.hex_editor.menu.file.save_project"_lang, "", false, providerValid && provider->isWritable())) { if (ProjectFile::getProjectFilePath() == "") { - hex::openFileBrowser("hex.builtin.view.hex_editor.save_project"_lang, DialogMode::Save, { - {"Project File", "hexproj"} + fs::openFileBrowser("hex.builtin.view.hex_editor.save_project"_lang, fs::DialogMode::Save, { + {"Project File", "hexproj"} }, - [](fs::path path) { + [](std::fs::path path) { if (path.extension() != ".hexproj") { path.replace_extension(".hexproj"); } @@ -1082,9 +1082,10 @@ namespace hex::plugin::builtin { } if (ImGui::MenuItem("hex.builtin.view.hex_editor.menu.file.load_encoding_file"_lang)) { - std::vector paths; - for (const auto &path : hex::getPath(ImHexPath::Encodings)) { - for (const auto &entry : fs::recursive_directory_iterator(path)) { + std::vector paths; + for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Encodings)) { + std::error_code error; + for (const auto &entry : std::fs::recursive_directory_iterator(path, error)) { if (!entry.is_regular_file()) continue; paths.push_back(entry); @@ -1110,8 +1111,8 @@ namespace hex::plugin::builtin { if (ImGui::BeginMenu("hex.builtin.view.hex_editor.menu.file.import"_lang)) { if (ImGui::MenuItem("hex.builtin.view.hex_editor.menu.file.import.base64"_lang)) { - hex::openFileBrowser("hex.builtin.view.hex_editor.menu.file.import.base64"_lang, DialogMode::Open, {}, [this](const auto &path) { - File file(path, File::Mode::Read); + fs::openFileBrowser("hex.builtin.view.hex_editor.menu.file.import.base64"_lang, fs::DialogMode::Open, {}, [this](const auto &path) { + fs::File file(path, fs::File::Mode::Read); if (!file.isValid()) { View::showErrorPopup("hex.builtin.view.hex_editor.error.open"_lang); return; @@ -1135,12 +1136,12 @@ namespace hex::plugin::builtin { if (ImGui::MenuItem("hex.builtin.view.hex_editor.menu.file.import.ips"_lang, nullptr, false, !this->m_processingImportExport)) { - hex::openFileBrowser("hex.builtin.view.hex_editor.open_file"_lang, DialogMode::Open, {}, [this](const auto &path) { + fs::openFileBrowser("hex.builtin.view.hex_editor.open_file"_lang, fs::DialogMode::Open, {}, [this](const auto &path) { this->m_processingImportExport = true; std::thread([this, path] { auto task = ImHexApi::Tasks::createTask("hex.builtin.view.hex_editor.processing", 0); - auto patchData = File(path, File::Mode::Read).readBytes(); + auto patchData = fs::File(path, fs::File::Mode::Read).readBytes(); auto patch = hex::loadIPSPatch(patchData); task.setMaxValue(patch.size()); @@ -1163,12 +1164,12 @@ namespace hex::plugin::builtin { } if (ImGui::MenuItem("hex.builtin.view.hex_editor.menu.file.import.ips32"_lang, nullptr, false, !this->m_processingImportExport)) { - hex::openFileBrowser("hex.builtin.view.hex_editor.open_file"_lang, DialogMode::Open, {}, [this](const auto &path) { + fs::openFileBrowser("hex.builtin.view.hex_editor.open_file"_lang, fs::DialogMode::Open, {}, [this](const auto &path) { this->m_processingImportExport = true; std::thread([this, path] { auto task = ImHexApi::Tasks::createTask("hex.builtin.view.hex_editor.processing", 0); - auto patchData = File(path, File::Mode::Read).readBytes(); + auto patchData = fs::File(path, fs::File::Mode::Read).readBytes(); auto patch = hex::loadIPS32Patch(patchData); task.setMaxValue(patch.size()); @@ -1218,8 +1219,8 @@ namespace hex::plugin::builtin { this->m_processingImportExport = false; ImHexApi::Tasks::doLater([this] { - hex::openFileBrowser("hex.builtin.view.hex_editor.menu.file.export.title"_lang, DialogMode::Save, {}, [this](const auto &path) { - auto file = File(path, File::Mode::Create); + fs::openFileBrowser("hex.builtin.view.hex_editor.menu.file.export.title"_lang, fs::DialogMode::Save, {}, [this](const auto &path) { + auto file = fs::File(path, fs::File::Mode::Create); if (!file.isValid()) { View::showErrorPopup("hex.builtin.view.hex_editor.error.create"_lang); return; @@ -1247,8 +1248,8 @@ namespace hex::plugin::builtin { this->m_processingImportExport = false; ImHexApi::Tasks::doLater([this] { - hex::openFileBrowser("hex.builtin.view.hex_editor.menu.file.export.title"_lang, DialogMode::Save, {}, [this](const auto &path) { - auto file = File(path, File::Mode::Create); + fs::openFileBrowser("hex.builtin.view.hex_editor.menu.file.export.title"_lang, fs::DialogMode::Save, {}, [this](const auto &path) { + auto file = fs::File(path, fs::File::Mode::Create); if (!file.isValid()) { View::showErrorPopup("hex.builtin.view.hex_editor.error.create"_lang); return; diff --git a/plugins/builtin/source/content/views/view_information.cpp b/plugins/builtin/source/content/views/view_information.cpp index 12675f186..10a6b535d 100644 --- a/plugins/builtin/source/content/views/view_information.cpp +++ b/plugins/builtin/source/content/views/view_information.cpp @@ -3,7 +3,7 @@ #include #include -#include +#include #include #include @@ -46,9 +46,8 @@ namespace hex::plugin::builtin { }); ContentRegistry::FileHandler::add({ ".mgc" }, [](const auto &path) { - for (const auto &destPath : hex::getPath(ImHexPath::Magic)) { - std::error_code error; - if (fs::copy_file(path, destPath / path.filename(), fs::copy_options::overwrite_existing, error)) { + for (const auto &destPath : fs::getDefaultPaths(fs::ImHexPath::Magic)) { + if (fs::copyFile(path, destPath / path.filename(), std::fs::copy_options::overwrite_existing)) { View::showMessagePopup("hex.builtin.view.information.magic_db_added"_lang); return true; } diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index 2958c63f8..3d8bddd95 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include #include @@ -100,7 +100,7 @@ namespace hex::plugin::builtin { this->m_textEditor.InsertText(code); }); - EventManager::subscribe(this, [this](const fs::path &path) { + EventManager::subscribe(this, [this](const std::fs::path &path) { if (!ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.auto_load_patterns", 1)) return; @@ -124,13 +124,13 @@ namespace hex::plugin::builtin { this->m_possiblePatternFiles.clear(); std::error_code errorCode; - for (const auto &dir : hex::getPath(ImHexPath::Patterns)) { - for (auto &entry : fs::directory_iterator(dir, errorCode)) { + for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Patterns)) { + for (auto &entry : std::fs::directory_iterator(dir, errorCode)) { foundCorrectType = false; if (!entry.is_regular_file()) continue; - File file(entry.path().string(), File::Mode::Read); + fs::File file(entry.path().string(), fs::File::Mode::Read); if (!file.isValid()) continue; @@ -181,8 +181,8 @@ namespace hex::plugin::builtin { }); } - ContentRegistry::FileHandler::add({ ".hexpat", ".pat" }, [](const fs::path &path) -> bool { - File file(path.string(), File::Mode::Read); + ContentRegistry::FileHandler::add({ ".hexpat", ".pat" }, [](const std::fs::path &path) -> bool { + fs::File file(path.string(), fs::File::Mode::Read); if (file.isValid()) { EventManager::post(file.readString()); @@ -195,12 +195,13 @@ namespace hex::plugin::builtin { ContentRegistry::Interface::addMenuItem("hex.builtin.menu.file", 2000, [&, this] { if (ImGui::MenuItem("hex.builtin.view.pattern_editor.menu.file.load_pattern"_lang)) { - std::vector paths; + std::vector paths; - for (const auto &imhexPath : hex::getPath(ImHexPath::Patterns)) { + for (const auto &imhexPath : fs::getDefaultPaths(fs::ImHexPath::Patterns)) { if (!fs::exists(imhexPath)) continue; - for (auto &entry : fs::recursive_directory_iterator(imhexPath)) { + std::error_code error; + for (auto &entry : std::fs::recursive_directory_iterator(imhexPath, error)) { if (entry.is_regular_file() && entry.path().extension() == ".hexpat") { paths.push_back(entry.path()); } @@ -210,17 +211,17 @@ namespace hex::plugin::builtin { View::showFileChooserPopup(paths, { {"Pattern File", "hexpat"} }, - [this](const fs::path &path) { + [this](const std::fs::path &path) { this->loadPatternFile(path); }); } if (ImGui::MenuItem("hex.builtin.view.pattern_editor.menu.file.save_pattern"_lang)) { - hex::openFileBrowser("hex.builtin.view.pattern_editor.menu.file.save_pattern"_lang, DialogMode::Save, { - {"Pattern", "hexpat"} + fs::openFileBrowser("hex.builtin.view.pattern_editor.menu.file.save_pattern"_lang, fs::DialogMode::Save, { + {"Pattern", "hexpat"} }, [this](const auto &path) { - File file(path, File::Mode::Create); + fs::File file(path, fs::File::Mode::Create); file.write(this->m_textEditor.GetText()); }); @@ -549,7 +550,7 @@ namespace hex::plugin::builtin { entries.resize(this->m_possiblePatternFiles.size()); for (u32 i = 0; i < entries.size(); i++) { - entries[i] = fs::path(this->m_possiblePatternFiles[i]).filename().string(); + entries[i] = std::fs::path(this->m_possiblePatternFiles[i]).filename().string(); } if (ImGui::BeginListBox("##patterns_accept", ImVec2(-FLT_MIN, 0))) { @@ -580,8 +581,8 @@ namespace hex::plugin::builtin { } - void ViewPatternEditor::loadPatternFile(const fs::path &path) { - File file(path, File::Mode::Read); + void ViewPatternEditor::loadPatternFile(const std::fs::path &path) { + fs::File file(path, fs::File::Mode::Read); if (file.isValid()) { auto code = file.readString(); diff --git a/plugins/builtin/source/content/views/view_store.cpp b/plugins/builtin/source/content/views/view_store.cpp index 2030bfc2b..9f3455779 100644 --- a/plugins/builtin/source/content/views/view_store.cpp +++ b/plugins/builtin/source/content/views/view_store.cpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include @@ -44,7 +44,7 @@ namespace hex::plugin::builtin { this->refresh(); } - auto drawTab = [this](auto title, ImHexPath pathType, auto &content, const std::function &downloadDoneCallback) { + auto drawTab = [this](auto title, fs::ImHexPath pathType, auto &content, const std::function &downloadDoneCallback) { if (ImGui::BeginTabItem(title)) { if (ImGui::BeginTable("##pattern_language", 3, ImGuiTableFlags_ScrollY | ImGuiTableFlags_Borders | ImGuiTableFlags_SizingStretchSame | ImGuiTableFlags_RowBg)) { ImGui::TableSetupScrollFreeze(0, 1); @@ -84,14 +84,14 @@ namespace hex::plugin::builtin { mtar_header_t header; auto extractBasePath = this->m_downloadPath.parent_path() / this->m_downloadPath.stem(); while (mtar_read_header(&ctx, &header) != MTAR_ENULLRECORD) { - auto filePath = extractBasePath / fs::path(header.name); + auto filePath = extractBasePath / std::fs::path(header.name); if (filePath.filename() == "@PaxHeader") { mtar_next(&ctx); continue; } - fs::create_directories(filePath.parent_path()); - File outputFile(filePath.string(), File::Mode::Create); + fs::createDirectories(filePath.parent_path()); + fs::File outputFile(filePath.string(), fs::File::Mode::Create); std::vector buffer(0x10000); for (u64 offset = 0; offset < header.size; offset += buffer.size()) { @@ -142,12 +142,12 @@ namespace hex::plugin::builtin { }; if (ImGui::BeginTabBar("storeTabs")) { - drawTab("hex.builtin.view.store.tab.patterns"_lang, ImHexPath::Patterns, this->m_patterns, [](auto) {}); - drawTab("hex.builtin.view.store.tab.libraries"_lang, ImHexPath::PatternsInclude, this->m_includes, [](auto) {}); - drawTab("hex.builtin.view.store.tab.magics"_lang, ImHexPath::Magic, this->m_magics, [](auto) { magic::compile(); }); - drawTab("hex.builtin.view.store.tab.constants"_lang, ImHexPath::Constants, this->m_constants, [](auto) {}); - drawTab("hex.builtin.view.store.tab.encodings"_lang, ImHexPath::Encodings, this->m_encodings, [](auto) {}); - drawTab("hex.builtin.view.store.tab.yara"_lang, ImHexPath::Yara, this->m_yara, [](auto) {}); + drawTab("hex.builtin.view.store.tab.patterns"_lang, fs::ImHexPath::Patterns, this->m_patterns, [](auto) {}); + drawTab("hex.builtin.view.store.tab.libraries"_lang, fs::ImHexPath::PatternsInclude, this->m_includes, [](auto) {}); + drawTab("hex.builtin.view.store.tab.magics"_lang, fs::ImHexPath::Magic, this->m_magics, [](auto) { magic::compile(); }); + drawTab("hex.builtin.view.store.tab.constants"_lang, fs::ImHexPath::Constants, this->m_constants, [](auto) {}); + drawTab("hex.builtin.view.store.tab.encodings"_lang, fs::ImHexPath::Encodings, this->m_encodings, [](auto) {}); + drawTab("hex.builtin.view.store.tab.yara"_lang, fs::ImHexPath::Yara, this->m_yara, [](auto) {}); ImGui::EndTabBar(); } @@ -169,7 +169,7 @@ namespace hex::plugin::builtin { if (response.code == 200) { auto json = nlohmann::json::parse(response.body); - auto parseStoreEntries = [](auto storeJson, const std::string &name, ImHexPath pathType, std::vector &results) { + auto parseStoreEntries = [](auto storeJson, const std::string &name, fs::ImHexPath pathType, std::vector &results) { // Check if the response handles the type of files if (storeJson.contains(name)) { @@ -182,15 +182,15 @@ namespace hex::plugin::builtin { StoreEntry storeEntry = { entry["name"], entry["desc"], entry["file"], entry["url"], entry["hash"], entry["folder"], false, false, false }; // Check if file is installed already or has an update available - for (const auto &folder : hex::getPath(pathType)) { + for (const auto &folder : fs::getDefaultPaths(pathType)) { - auto path = folder / fs::path(storeEntry.fileName); + auto path = folder / std::fs::path(storeEntry.fileName); - if (fs::exists(path) && hex::isPathWritable(folder)) { + if (fs::exists(path) && fs::isPathWritable(folder)) { storeEntry.installed = true; std::ifstream file(path, std::ios::in | std::ios::binary); - std::vector data(fs::file_size(path), 0x00); + std::vector data(fs::getFileSize(path), 0x00); file.read(reinterpret_cast(data.data()), data.size()); auto fileHash = crypt::sha256(data); @@ -207,12 +207,12 @@ namespace hex::plugin::builtin { } }; - parseStoreEntries(json, "patterns", ImHexPath::Patterns, this->m_patterns); - parseStoreEntries(json, "includes", ImHexPath::PatternsInclude, this->m_includes); - parseStoreEntries(json, "magic", ImHexPath::Magic, this->m_magics); - parseStoreEntries(json, "constants", ImHexPath::Constants, this->m_constants); - parseStoreEntries(json, "yara", ImHexPath::Yara, this->m_yara); - parseStoreEntries(json, "encodings", ImHexPath::Encodings, this->m_encodings); + parseStoreEntries(json, "patterns", fs::ImHexPath::Patterns, this->m_patterns); + parseStoreEntries(json, "includes", fs::ImHexPath::PatternsInclude, this->m_includes); + parseStoreEntries(json, "magic", fs::ImHexPath::Magic, this->m_magics); + parseStoreEntries(json, "constants", fs::ImHexPath::Constants, this->m_constants); + parseStoreEntries(json, "yara", fs::ImHexPath::Yara, this->m_yara); + parseStoreEntries(json, "encodings", fs::ImHexPath::Encodings, this->m_encodings); } this->m_apiRequest = {}; } @@ -234,13 +234,13 @@ namespace hex::plugin::builtin { } } - bool ViewStore::download(ImHexPath pathType, const std::string &fileName, const std::string &url, bool update) { + bool ViewStore::download(fs::ImHexPath pathType, const std::string &fileName, const std::string &url, bool update) { bool downloading = false; - for (const auto &path : hex::getPath(pathType)) { - if (!hex::isPathWritable(path)) + for (const auto &path : fs::getDefaultPaths(pathType)) { + if (!fs::isPathWritable(path)) continue; - auto fullPath = path / fs::path(fileName); + auto fullPath = path / std::fs::path(fileName); if (!update || fs::exists(fullPath)) { downloading = true; @@ -258,13 +258,11 @@ namespace hex::plugin::builtin { return true; } - bool ViewStore::remove(ImHexPath pathType, const std::string &fileName) { + bool ViewStore::remove(fs::ImHexPath pathType, const std::string &fileName) { bool removed = false; - for (const auto &path : hex::getPath(pathType)) { - std::error_code error; - - bool removedFile = fs::remove(path / fs::path(fileName), error); - bool removedFolder = fs::remove(path / fs::path(fileName).stem(), error); + for (const auto &path : fs::getDefaultPaths(pathType)) { + bool removedFile = fs::remove(path / std::fs::path(fileName)); + bool removedFolder = fs::remove(path / std::fs::path(fileName).stem()); removed = removed || removedFile || removedFolder; } diff --git a/plugins/builtin/source/content/views/view_yara.cpp b/plugins/builtin/source/content/views/view_yara.cpp index 29f87b388..ad4f124be 100644 --- a/plugins/builtin/source/content/views/view_yara.cpp +++ b/plugins/builtin/source/content/views/view_yara.cpp @@ -5,14 +5,14 @@ #include #include #include -#include +#include #include #include #include #include -#include +#include namespace hex::plugin::builtin { @@ -22,9 +22,8 @@ namespace hex::plugin::builtin { this->reloadRules(); ContentRegistry::FileHandler::add({ ".yar" }, [](const auto &path) { - for (const auto &destPath : hex::getPath(ImHexPath::Yara)) { - std::error_code error; - if (fs::copy_file(path, destPath / path.filename(), fs::copy_options::overwrite_existing, error)) { + for (const auto &destPath : fs::getDefaultPaths(fs::ImHexPath::Yara)) { + if (fs::copyFile(path, destPath / path.filename(), std::fs::copy_options::overwrite_existing)) { View::showMessagePopup("hex.builtin.view.yara.rule_added"_lang); return true; } @@ -134,13 +133,14 @@ namespace hex::plugin::builtin { void ViewYara::reloadRules() { this->m_rules.clear(); - for (const auto path : hex::getPath(ImHexPath::Yara)) { + for (const auto path : fs::getDefaultPaths(fs::ImHexPath::Yara)) { if (!fs::exists(path)) continue; - for (const auto &entry : fs::recursive_directory_iterator(path)) { + std::error_code error; + for (const auto &entry : std::fs::recursive_directory_iterator(path, error)) { if (entry.is_regular_file() && entry.path().extension() == ".yar") { - this->m_rules.push_back({ fs::relative(entry.path(), fs::path(path)).string(), entry.path().string() }); + this->m_rules.push_back({ std::fs::relative(entry.path(), std::fs::path(path)).string(), entry.path().string() }); } } } @@ -172,7 +172,7 @@ namespace hex::plugin::builtin { [](const char *includeName, const char *callingRuleFileName, const char *callingRuleNamespace, void *userData) -> const char * { auto currFilePath = static_cast(userData); - File file((fs::path(currFilePath).parent_path() / includeName).string(), File::Mode::Read); + fs::File file((std::fs::path(currFilePath).parent_path() / includeName).string(), fs::File::Mode::Read); if (!file.isValid()) return nullptr; @@ -189,7 +189,7 @@ namespace hex::plugin::builtin { this->m_rules[this->m_selectedRule].second.data()); - File file(this->m_rules[this->m_selectedRule].second, File::Mode::Read); + fs::File file(this->m_rules[this->m_selectedRule].second, fs::File::Mode::Read); if (!file.isValid()) return; if (yr_compiler_add_file(compiler, file.getHandle(), nullptr, nullptr) != 0) { diff --git a/plugins/builtin/source/content/welcome_screen.cpp b/plugins/builtin/source/content/welcome_screen.cpp index ef8bd68ed..8012aa9a4 100644 --- a/plugins/builtin/source/content/welcome_screen.cpp +++ b/plugins/builtin/source/content/welcome_screen.cpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include @@ -28,9 +28,9 @@ namespace hex::plugin::builtin { static bool s_layoutConfigured = false; static ImGui::Texture s_bannerTexture; static std::string s_bannerTextureName; - static std::list s_recentFilePaths; + static std::list s_recentFilePaths; - static fs::path s_safetyBackupPath; + static std::fs::path s_safetyBackupPath; static std::string s_tipOfTheDay; @@ -153,7 +153,7 @@ namespace hex::plugin::builtin { ImGui::SetCursorPosY(ImGui::GetCursorPosY() + 5_scaled); { for (auto &path : s_recentFilePaths) { - if (ImGui::BulletHyperlink(fs::path(path).filename().string().c_str())) { + if (ImGui::BulletHyperlink(std::fs::path(path).filename().string().c_str())) { EventManager::post(path); break; } @@ -384,7 +384,7 @@ namespace hex::plugin::builtin { s_recentFilePaths.push_front(path); { - std::list uniques; + std::list uniques; for (auto &file : s_recentFilePaths) { bool exists = false; @@ -420,16 +420,16 @@ namespace hex::plugin::builtin { ContentRegistry::Interface::addMenuItem("hex.builtin.menu.file", 1050, [&] { if (ImGui::MenuItem("hex.builtin.view.hex_editor.menu.file.open_file"_lang, "CTRL + O")) { - hex::openFileBrowser("hex.builtin.view.hex_editor.open_file"_lang, DialogMode::Open, {}, [](const auto &path) { + fs::openFileBrowser("hex.builtin.view.hex_editor.open_file"_lang, fs::DialogMode::Open, {}, [](const auto &path) { EventManager::post(path); }); } if (ImGui::BeginMenu("hex.builtin.view.hex_editor.menu.file.open_recent"_lang, !s_recentFilePaths.empty())) { - // Copy to avoid chaning list while iteration - std::list recentFilePaths = s_recentFilePaths; + // Copy to avoid changing list while iteration + std::list recentFilePaths = s_recentFilePaths; for (auto &path : recentFilePaths) { - auto filename = fs::path(path).filename().string(); + auto filename = std::fs::path(path).filename().string(); if (ImGui::MenuItem(filename.c_str())) { EventManager::post(path); } @@ -461,8 +461,8 @@ namespace hex::plugin::builtin { constexpr auto CrashBackupFileName = "crash_backup.hexproj"; - for (const auto &path : hex::getPath(ImHexPath::Config)) { - if (auto filePath = fs::path(path) / CrashBackupFileName; fs::exists(filePath)) { + for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Config)) { + if (auto filePath = std::fs::path(path) / CrashBackupFileName; fs::exists(filePath)) { s_safetyBackupPath = filePath; ImHexApi::Tasks::doLater([] { ImGui::OpenPopup("hex.builtin.welcome.safety_backup.title"_lang); }); } diff --git a/tests/helpers/source/file.cpp b/tests/helpers/source/file.cpp index e27df5dd1..38f379b7d 100644 --- a/tests/helpers/source/file.cpp +++ b/tests/helpers/source/file.cpp @@ -1,7 +1,7 @@ #include #include -#include +#include using namespace std::literals::string_literals; diff --git a/tests/helpers/source/net.cpp b/tests/helpers/source/net.cpp index 4c746aa05..5bff6c674 100644 --- a/tests/helpers/source/net.cpp +++ b/tests/helpers/source/net.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include using namespace std::literals::string_literals;