From 57a62d05443aaf5dbd00d410b3bd1cafd301caa4 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 21 Mar 2023 15:33:43 +0100 Subject: [PATCH] impr: Clean up entire API and added doc comments --- .../include/hex/api/content_registry.hpp | 453 +++++++++++++---- lib/libimhex/include/hex/api/event.hpp | 102 ++-- lib/libimhex/include/hex/api/imhex_api.hpp | 275 +++++++++- lib/libimhex/include/hex/api/keybinding.hpp | 40 ++ .../include/hex/api/project_file_manager.hpp | 74 ++- lib/libimhex/include/hex/api/task.hpp | 69 ++- .../include/hex/api/theme_manager.hpp | 26 + lib/libimhex/source/api/content_registry.cpp | 469 ++++++++++-------- lib/libimhex/source/api/imhex_api.cpp | 21 +- lib/libimhex/source/api/localization.cpp | 4 +- lib/libimhex/source/api/task.cpp | 2 +- main/source/init/tasks.cpp | 54 +- main/source/window/win_window.cpp | 4 +- main/source/window/window.cpp | 28 +- .../content/command_palette_commands.cpp | 6 +- .../source/content/main_menu_items.cpp | 10 +- .../source/content/settings_entries.cpp | 18 +- .../builtin/source/content/tools_entries.cpp | 4 +- plugins/builtin/source/content/ui_items.cpp | 6 +- .../content/views/view_command_palette.cpp | 4 +- .../content/views/view_data_inspector.cpp | 2 +- .../content/views/view_data_processor.cpp | 4 +- .../source/content/views/view_hex_editor.cpp | 2 +- .../content/views/view_pattern_data.cpp | 6 +- .../content/views/view_pattern_editor.cpp | 15 +- .../source/content/views/view_settings.cpp | 8 +- .../source/content/views/view_tools.cpp | 2 +- .../builtin/source/content/welcome_screen.cpp | 38 +- plugins/builtin/source/ui/hex_editor.cpp | 67 +-- plugins/windows/source/plugin_windows.cpp | 4 +- 30 files changed, 1283 insertions(+), 534 deletions(-) diff --git a/lib/libimhex/include/hex/api/content_registry.hpp b/lib/libimhex/include/hex/api/content_registry.hpp index 315fb5ac1..23cb7fb3a 100644 --- a/lib/libimhex/include/hex/api/content_registry.hpp +++ b/lib/libimhex/include/hex/api/content_registry.hpp @@ -41,108 +41,206 @@ namespace hex { /* Settings Registry. Allows adding of new entries into the ImHex preferences window. */ namespace Settings { - using Callback = std::function; - struct Entry { - std::string name; - bool requiresRestart; - Callback callback; - }; + namespace impl { + using Callback = std::function; - struct Category { - std::string name; - size_t slot = 0; + struct Entry { + std::string name; + bool requiresRestart; + Callback callback; + }; - bool operator<(const Category &other) const { - return name < other.name; - } + struct Category { + std::string name; + size_t slot = 0; - explicit operator const std::string &() const { - return name; - } - }; + bool operator<(const Category &other) const { + return name < other.name; + } - void load(); - void store(); - void clear(); + explicit operator const std::string &() const { + return name; + } + }; - void add(const std::string &unlocalizedCategory, const std::string &unlocalizedName, i64 defaultValue, const Callback &callback, bool requiresRestart = false); - void add(const std::string &unlocalizedCategory, const std::string &unlocalizedName, const std::string &defaultValue, const Callback &callback, bool requiresRestart = false); - void add(const std::string &unlocalizedCategory, const std::string &unlocalizedName, const std::vector &defaultValue, const Callback &callback, bool requiresRestart = false); + void load(); + void store(); + void clear(); + std::map> &getEntries(); + std::map &getCategoryDescriptions(); + nlohmann::json getSetting(const std::string &unlocalizedCategory, const std::string &unlocalizedName); + nlohmann::json &getSettingsData(); + } + + + /** + * @brief Adds a new integer setting entry + * @param unlocalizedCategory The category of the setting + * @param unlocalizedName The name of the setting + * @param defaultValue The default value of the setting + * @param callback The callback that will be called when the settings item in the preferences window is rendered + * @param requiresRestart Whether the setting requires a restart to take effect + */ + void add(const std::string &unlocalizedCategory, const std::string &unlocalizedName, i64 defaultValue, const impl::Callback &callback, bool requiresRestart = false); + + /** + * @brief Adds a new string setting entry + * @param unlocalizedCategory The category of the setting + * @param unlocalizedName The name of the setting + * @param defaultValue The default value of the setting + * @param callback The callback that will be called when the settings item in the preferences window is rendered + * @param requiresRestart Whether the setting requires a restart to take effect + */ + void add(const std::string &unlocalizedCategory, const std::string &unlocalizedName, const std::string &defaultValue, const impl::Callback &callback, bool requiresRestart = false); + + /** + * @brief Adds a new string list setting entry + * @param unlocalizedCategory The category of the setting + * @param unlocalizedName The name of the setting + * @param defaultValue The default value of the setting + * @param callback The callback that will be called when the settings item in the preferences window is rendered + * @param requiresRestart Whether the setting requires a restart to take effect + */ + void add(const std::string &unlocalizedCategory, const std::string &unlocalizedName, const std::vector &defaultValue, const impl::Callback &callback, bool requiresRestart = false); + + /** + * @brief Adds a description to a given category + * @param unlocalizedCategory The name of the category + * @param unlocalizedCategoryDescription The description of the category + */ void addCategoryDescription(const std::string &unlocalizedCategory, const std::string &unlocalizedCategoryDescription); + /** + * @brief Writes a integer value to the settings file + * @param unlocalizedCategory The category of the setting + * @param unlocalizedName The name of the setting + * @param value The value to write + */ void write(const std::string &unlocalizedCategory, const std::string &unlocalizedName, i64 value); + + /** + * @brief Writes a string value to the settings file + * @param unlocalizedCategory The category of the setting + * @param unlocalizedName The name of the setting + * @param value The value to write + */ void write(const std::string &unlocalizedCategory, const std::string &unlocalizedName, const std::string &value); + + /** + * @brief Writes a string list value to the settings file + * @param unlocalizedCategory The category of the setting + * @param unlocalizedName The name of the setting + * @param value The value to write + */ void write(const std::string &unlocalizedCategory, const std::string &unlocalizedName, const std::vector &value); + /** + * @brief Reads an integer value from the settings file + * @param unlocalizedCategory The category of the setting + * @param unlocalizedName The name of the setting + * @param defaultValue The default value of the setting + * @return The value of the setting + */ i64 read(const std::string &unlocalizedCategory, const std::string &unlocalizedName, i64 defaultValue); + + /** + * @brief Reads a string value from the settings file + * @param unlocalizedCategory The category of the setting + * @param unlocalizedName The name of the setting + * @param defaultValue The default value of the setting + * @return The value of the setting + */ std::string read(const std::string &unlocalizedCategory, const std::string &unlocalizedName, const std::string &defaultValue); + + /** + * @brief Reads a string list value from the settings file + * @param unlocalizedCategory The category of the setting + * @param unlocalizedName The name of the setting + * @param defaultValue The default value of the setting + * @return The value of the setting + */ std::vector read(const std::string &unlocalizedCategory, const std::string &unlocalizedName, const std::vector &defaultValue = {}); - std::map> &getEntries(); - std::map &getCategoryDescriptions(); - nlohmann::json getSetting(const std::string &unlocalizedCategory, const std::string &unlocalizedName); - nlohmann::json &getSettingsData(); } /* Command Palette Command Registry. Allows adding of new commands to the command palette */ namespace CommandPaletteCommands { - enum class Type : u32 - { + enum class Type : u32 { SymbolCommand, KeywordCommand }; - struct QueryResult { - std::string name; - std::function callback; - }; + namespace impl { - using DisplayCallback = std::function; - using ExecuteCallback = std::function; - using QueryCallback = std::function(std::string)>; + struct QueryResult { + std::string name; + std::function callback; + }; - struct Entry { - Type type; - std::string command; - std::string unlocalizedDescription; - DisplayCallback displayCallback; - ExecuteCallback executeCallback; - }; + using DisplayCallback = std::function; + using ExecuteCallback = std::function; + using QueryCallback = std::function(std::string)>; - struct Handler { - Type type; - std::string command; - QueryCallback queryCallback; - DisplayCallback displayCallback; - }; + struct Entry { + Type type; + std::string command; + std::string unlocalizedDescription; + DisplayCallback displayCallback; + ExecuteCallback executeCallback; + }; + struct Handler { + Type type; + std::string command; + QueryCallback queryCallback; + DisplayCallback displayCallback; + }; + + std::vector &getEntries(); + std::vector &getHandlers(); + + } + + /** + * @brief Adds a new command to the command palette + * @param type The type of the command + * @param command The command to add + * @param unlocalizedDescription The description of the command + * @param displayCallback The callback that will be called when the command is displayed in the command palette + * @param executeCallback The callback that will be called when the command is executed + */ void add( Type type, const std::string &command, const std::string &unlocalizedDescription, - const DisplayCallback &displayCallback, - const ExecuteCallback &executeCallback = [](auto) {}); + const impl::DisplayCallback &displayCallback, + const impl::ExecuteCallback &executeCallback = [](auto) {}); + /** + * @brief Adds a new command handler to the command palette + * @param type The type of the command + * @param command The command to add + * @param unlocalizedDescription The description of the command + * @param queryCallback The callback that will be called when the command palette wants to load the name and callback items + * @param displayCallback The callback that will be called when the command is displayed in the command palette + */ void addHandler( Type type, const std::string &command, - const QueryCallback &queryCallback, - const DisplayCallback &displayCallback); - - std::vector &getEntries(); - std::vector &getHandlers(); + const impl::QueryCallback &queryCallback, + const impl::DisplayCallback &displayCallback); } /* Pattern Language Function Registry. Allows adding of new functions that may be used inside the pattern language */ namespace PatternLanguage { - using VisualizerFunctionCallback = std::function)>; - namespace impl { + using VisualizerFunctionCallback = std::function)>; + struct FunctionDefinition { pl::api::Namespace ns; std::string name; @@ -159,20 +257,52 @@ namespace hex { }; std::map &getVisualizers(); + std::map &getPragmas(); + std::vector &getFunctions(); } + /** + * @brief Configures the pattern language runtime using ImHex's default settings + * @param runtime The pattern language runtime to configure + * @param provider The provider to use for data access + */ void configureRuntime(pl::PatternLanguage &runtime, prv::Provider *provider); + /** + * @brief Adds a new pragma to the pattern language + * @param name The name of the pragma + * @param handler The handler that will be called when the pragma is encountered + */ void addPragma(const std::string &name, const pl::api::PragmaHandler &handler); + /** + * @brief Adds a new function to the pattern language + * @param ns The namespace of the function + * @param name The name of the function + * @param parameterCount The amount of parameters the function takes + * @param func The function callback + */ void addFunction(const pl::api::Namespace &ns, const std::string &name, pl::api::FunctionParameterCount parameterCount, const pl::api::FunctionCallback &func); + + /** + * @brief Adds a new dangerous function to the pattern language + * @note Dangerous functions are functions that require the user to explicitly allow them to be used + * @param ns The namespace of the function + * @param name The name of the function + * @param parameterCount The amount of parameters the function takes + * @param func The function callback + */ void addDangerousFunction(const pl::api::Namespace &ns, const std::string &name, pl::api::FunctionParameterCount parameterCount, const pl::api::FunctionCallback &func); - void addVisualizer(const std::string &name, const VisualizerFunctionCallback &func, u32 parameterCount); - - std::map &getPragmas(); - std::vector &getFunctions(); + /** + * @brief Adds a new visualizer to the pattern language + * @note Visualizers are extensions to the [[hex::visualize]] attribute, used to visualize data + * @param name The name of the visualizer + * @param func The function callback + * @param parameterCount The amount of parameters the function takes + */ + void addVisualizer(const std::string &name, const impl::VisualizerFunctionCallback &func, u32 parameterCount); } @@ -182,18 +312,27 @@ namespace hex { namespace impl { void add(View *view); + std::map &getEntries(); } + /** + * @brief Adds a new view to ImHex + * @tparam T The custom view class that extends View + * @tparam Args Arguments types + * @param args Arguments passed to the constructor of the view + */ template T, typename... Args> void add(Args &&...args) { return impl::add(new T(std::forward(args)...)); } - std::map &getEntries(); - + /** + * @brief Gets a view by its unlocalized name + * @param unlocalizedName The unlocalized name of the view + * @return The view if it exists, nullptr otherwise + */ View *getViewByName(const std::string &unlocalizedName); - } /* Tools Registry. Allows adding new entries to the tools window */ @@ -209,11 +348,16 @@ namespace hex { bool detached; }; + std::vector &getEntries(); + } + /** + * @brief Adds a new tool to the tools window + * @param unlocalizedName The unlocalized name of the tool + * @param function The function that will be called to draw the tool + */ void add(const std::string &unlocalizedName, const impl::Callback &function); - - std::vector &getEntries(); } /* Data Inspector Registry. Allows adding of new types to the data inspector */ @@ -240,12 +384,28 @@ namespace hex { std::optional editingFunction; }; + std::vector &getEntries(); + } + /** + * @brief Adds a new entry to the data inspector + * @param unlocalizedName The unlocalized name of the entry + * @param requiredSize The minimum required number of bytes available for the entry to appear + * @param displayGeneratorFunction The function that will be called to generate the display function + * @param editingFunction The function that will be called to edit the data + */ void add(const std::string &unlocalizedName, size_t requiredSize, impl::GeneratorFunction displayGeneratorFunction, std::optional editingFunction = std::nullopt); - void add(const std::string &unlocalizedName, size_t requiredSize, size_t maxSize, impl::GeneratorFunction displayGeneratorFunction, std::optional editingFunction = std::nullopt); - std::vector &getEntries(); + /** + * @brief Adds a new entry to the data inspector + * @param unlocalizedName The unlocalized name of the entry + * @param requiredSize The minimum required number of bytes available for the entry to appear + * @param maxSize The maximum number of bytes to read from the data + * @param displayGeneratorFunction The function that will be called to generate the display function + * @param editingFunction The function that will be called to edit the data + */ + void add(const std::string &unlocalizedName, size_t requiredSize, size_t maxSize, impl::GeneratorFunction displayGeneratorFunction, std::optional editingFunction = std::nullopt); } /* Data Processor Node Registry. Allows adding new processor nodes to be used in the data processor */ @@ -263,9 +423,18 @@ namespace hex { void add(const Entry &entry); + std::vector &getEntries(); } + /** + * @brief Adds a new node to the data processor + * @tparam T The custom node class that extends dp::Node + * @tparam Args Arguments types + * @param unlocalizedCategory The unlocalized category name of the node + * @param unlocalizedName The unlocalized name of the node + * @param args Arguments passed to the constructor of the node + */ template T, typename... Args> void add(const std::string &unlocalizedCategory, const std::string &unlocalizedName, Args &&...args) { add(impl::Entry { @@ -279,18 +448,29 @@ namespace hex { }); } + /** + * @brief Adds a separator to the data processor right click menu + */ void addSeparator(); - std::vector &getEntries(); - } /* Language Registry. Allows together with the LangEntry class and the _lang user defined literal to add new languages */ namespace Language { + + /** + * @brief Loads localization information from json data + * @param data The language data + */ void addLocalization(const nlohmann::json &data); - std::map &getLanguages(); - std::map> &getLanguageDefinitions(); + namespace impl { + + std::map &getLanguages(); + std::map> &getLanguageDefinitions(); + + } + } /* Interface Registry. Allows adding new items to various interfaces */ @@ -334,31 +514,96 @@ namespace hex { constexpr static auto SeparatorValue = "$SEPARATOR$"; constexpr static auto SubMenuValue = "$SUBMENU$"; + std::multimap &getMainMenuItems(); + std::multimap &getMenuItems(); + + std::vector &getWelcomeScreenEntries(); + std::vector &getFooterItems(); + std::vector &getToolbarItems(); + std::vector &getSidebarItems(); + std::vector &getTitleBarButtons(); + + std::vector &getLayouts(); + } + /** + * @brief Adds a new top-level main menu entry + * @param unlocalizedName The unlocalized name of the entry + * @param priority The priority of the entry. Lower values are displayed first + */ void registerMainMenuItem(const std::string &unlocalizedName, u32 priority); + + /** + * @brief Adds a new main menu entry + * @param unlocalizedMainMenuNames The unlocalized names of the main menu entries + * @param priority The priority of the entry. Lower values are displayed first + * @param shortcut The shortcut to use for the entry + * @param function The function to call when the entry is clicked + * @param enabledCallback The function to call to determine if the entry is enabled + * @param view The view to use for the entry. If nullptr, the shortcut will work globally + */ void addMenuItem(const std::vector &unlocalizedMainMenuNames, u32 priority, const Shortcut &shortcut, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback = []{ return true; }, View *view = nullptr); + + /** + * @brief Adds a new main menu sub-menu entry + * @param unlocalizedMainMenuNames The unlocalized names of the main menu entries + * @param priority The priority of the entry. Lower values are displayed first + * @param function The function to call when the entry is clicked + * @param enabledCallback The function to call to determine if the entry is enabled + * @param view The view to use for the entry. If nullptr, the shortcut will work globally + */ void addMenuItemSubMenu(std::vector unlocalizedMainMenuNames, u32 priority, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback = []{ return true; }); + + /** + * @brief Adds a new main menu separator + * @param unlocalizedMainMenuNames The unlocalized names of the main menu entries + * @param priority The priority of the entry. Lower values are displayed first + */ void addMenuItemSeparator(std::vector unlocalizedMainMenuNames, u32 priority); + + /** + * @brief Adds a new welcome screen entry + * @param function The function to call to draw the entry + */ void addWelcomeScreenEntry(const impl::DrawCallback &function); + + /** + * @brief Adds a new footer item + * @param function The function to call to draw the item + */ void addFooterItem(const impl::DrawCallback &function); + + /** + * @brief Adds a new toolbar item + * @param function The function to call to draw the item + */ void addToolbarItem(const impl::DrawCallback &function); + + /** + * @brief Adds a new sidebar item + * @param icon The icon to use for the item + * @param function The function to call to draw the item + */ void addSidebarItem(const std::string &icon, const impl::DrawCallback &function); + + /** + * @brief Adds a new title bar button + * @param icon The icon to use for the button + * @param unlocalizedTooltip The unlocalized tooltip to use for the button + * @param function The function to call when the button is clicked + */ void addTitleBarButton(const std::string &icon, const std::string &unlocalizedTooltip, const impl::ClickCallback &function); + + /** + * @brief Adds a new layout definition to the Layout menu + * @param unlocalizedName The unlocalized name of the layout + * @param function The function to call to setup the layout + */ void addLayout(const std::string &unlocalizedName, const impl::LayoutFunction &function); - std::multimap &getMainMenuItems(); - std::multimap &getMenuItems(); - - std::vector &getWelcomeScreenEntries(); - std::vector &getFooterItems(); - std::vector &getToolbarItems(); - std::vector &getSidebarItems(); - std::vector &getTitleBarButtons(); - - std::vector &getLayouts(); } /* Provider Registry. Allows adding new data providers to be created from the UI */ @@ -368,8 +613,15 @@ namespace hex { void addProviderName(const std::string &unlocalizedName); + std::vector &getEntries(); + } + /** + * @brief Adds a new provider to the list of providers + * @tparam T The provider type that extends hex::prv::Provider + * @param addToList Whether to display the provider in the Other Providers list in the welcome screen and File menu + */ template T> void add(bool addToList = true) { auto typeName = T().getTypeName(); @@ -389,10 +641,9 @@ namespace hex { impl::addProviderName(typeName); } - std::vector &getEntries(); - } + /* Data Formatter Registry. Allows adding formatters that are used in the Copy-As menu for example */ namespace DataFormatter { namespace impl { @@ -403,14 +654,21 @@ namespace hex { Callback callback; }; + std::vector &getEntries(); + } - void add(const std::string &unlocalizedName, const impl::Callback &callback); - std::vector &getEntries(); + /** + * @brief Adds a new data formatter + * @param unlocalizedName The unlocalized name of the formatter + * @param callback The function to call to format the data + */ + void add(const std::string &unlocalizedName, const impl::Callback &callback); } + /* File Handler Registry. Allows adding handlers for opening files specific file types */ namespace FileHandler { namespace impl { @@ -421,14 +679,20 @@ namespace hex { Callback callback; }; + std::vector &getEntries(); + } + /** + * @brief Adds a new file handler + * @param extensions The file extensions to handle + * @param callback The function to call to handle the file + */ void add(const std::vector &extensions, const impl::Callback &callback); - std::vector &getEntries(); - } + /* Hex Editor Registry. Allows adding new functionality to the hex editor */ namespace HexEditor { class DataVisualizer { @@ -462,6 +726,12 @@ namespace hex { } + /** + * @brief Adds a new cell data visualizer + * @tparam T The data visualizer type that extends hex::DataVisualizer + * @param unlocalizedName The unlocalized name of the data visualizer + * @param args The arguments to pass to the constructor of the data visualizer + */ template T, typename... Args> void addDataVisualizer(const std::string &unlocalizedName, Args &&...args) { return impl::addDataVisualizer(unlocalizedName, new T(std::forward(args)...)); @@ -469,6 +739,7 @@ namespace hex { } + /* Hash Registry. Allows adding new hashes to the Hash view */ namespace Hashes { class Hash { @@ -534,6 +805,12 @@ namespace hex { void add(Hash* hash); } + + /** + * @brief Adds a new hash + * @tparam T The hash type that extends hex::Hash + * @param args The arguments to pass to the constructor of the hash + */ template void add(Args && ... args) { impl::add(new T(std::forward(args)...)); diff --git a/lib/libimhex/include/hex/api/event.hpp b/lib/libimhex/include/hex/api/event.hpp index 7f29f49cf..91a90d517 100644 --- a/lib/libimhex/include/hex/api/event.hpp +++ b/lib/libimhex/include/hex/api/event.hpp @@ -10,68 +10,98 @@ #include #include -#define EVENT_DEF(event_name, ...) \ - struct event_name final : public hex::Event<__VA_ARGS__> { \ - constexpr static auto id = [] { return hex::EventId(); }(); \ - explicit event_name(Callback func) noexcept : Event(std::move(func)) { } \ +#define EVENT_DEF(event_name, ...) \ + struct event_name final : public hex::impl::Event<__VA_ARGS__> { \ + constexpr static auto id = [] { return hex::impl::EventId(); }(); \ + explicit event_name(Callback func) noexcept : Event(std::move(func)) { } \ } struct GLFWwindow; namespace hex { - class EventId { - public: - explicit constexpr EventId(const char *func = __builtin_FUNCTION(), u32 line = __builtin_LINE()) { - this->m_hash = line ^ 987654321; - for (auto c : std::string_view(func)) { - this->m_hash = (this->m_hash >> 5) | (this->m_hash << 27); - this->m_hash ^= c; + namespace impl { + + class EventId { + public: + explicit constexpr EventId(const char *func = __builtin_FUNCTION(), u32 line = __builtin_LINE()) { + this->m_hash = line ^ 987654321; + for (auto c : std::string_view(func)) { + this->m_hash = (this->m_hash >> 5) | (this->m_hash << 27); + this->m_hash ^= c; + } } - } - constexpr bool operator==(const EventId &rhs) const = default; + constexpr bool operator==(const EventId &rhs) const = default; - private: - u32 m_hash; - }; + private: + u32 m_hash; + }; - struct EventBase { - EventBase() noexcept = default; - }; + struct EventBase { + EventBase() noexcept = default; + }; - template - struct Event : public EventBase { - using Callback = std::function; + template + struct Event : public EventBase { + using Callback = std::function; - explicit Event(Callback func) noexcept : m_func(std::move(func)) { } + explicit Event(Callback func) noexcept : m_func(std::move(func)) { } - void operator()(Params... params) const noexcept { - this->m_func(params...); - } + void operator()(Params... params) const noexcept { + this->m_func(params...); + } - private: - Callback m_func; - }; + private: + Callback m_func; + }; + } + + + /** + * @brief The EventManager allows subscribing to and posting events to different parts of the program. + * To create a new event, use the EVENT_DEF macro. This will create a new event type with the given name and parameters + */ class EventManager { public: - using EventList = std::list>; + using EventList = std::list>; + /** + * @brief Subscribes to an event + * @tparam E Event + * @param function Function to call when the event is posted + * @return Token to unsubscribe from the event + */ template static EventList::iterator subscribe(typename E::Callback function) { return s_events.insert(s_events.end(), std::make_pair(E::id, new E(function))); } + /** + * @brief Subscribes to an event + * @tparam E Event + * @param token Unique token to register the event to. Later required to unsubscribe again + * @param function Function to call when the event is posted + */ template static void subscribe(void *token, typename E::Callback function) { s_tokenStore.insert(std::make_pair(token, subscribe(function))); } - static void unsubscribe(EventList::iterator iter) noexcept { - s_events.remove(*iter); + /** + * @brief Unsubscribes from an event + * @param token Token returned by subscribe + */ + static void unsubscribe(const EventList::iterator &token) noexcept { + s_events.remove(*token); } + /** + * @brief Unsubscribes from an event + * @tparam E Event + * @param token Token passed to subscribe + */ template static void unsubscribe(void *token) noexcept { auto iter = std::find_if(s_tokenStore.begin(), s_tokenStore.end(), [&](auto &item) { @@ -85,6 +115,11 @@ namespace hex { } + /** + * @brief Posts an event to all subscribers of it + * @tparam E Event + * @param args Arguments to pass to the event + */ template static void post(auto &&...args) noexcept { for (const auto &[id, event] : s_events) { @@ -93,6 +128,9 @@ namespace hex { } } + /** + * @brief Unsubscribe all subscribers from all events + */ static void clear() noexcept { s_events.clear(); s_tokenStore.clear(); diff --git a/lib/libimhex/include/hex/api/imhex_api.hpp b/lib/libimhex/include/hex/api/imhex_api.hpp index 0d320e4fb..1cf899566 100644 --- a/lib/libimhex/include/hex/api/imhex_api.hpp +++ b/lib/libimhex/include/hex/api/imhex_api.hpp @@ -26,13 +26,7 @@ namespace hex { namespace ImHexApi { - namespace Common { - - void closeImHex(bool noQuestions = false); - void restartImHex(); - - } - + /* Functions to query information from the Hex Editor and interact with it */ namespace HexEditor { using TooltipFunction = std::function; @@ -87,32 +81,127 @@ namespace hex { void setCurrentSelection(std::optional region); } + /** + * @brief Adds a background color highlighting to the Hex Editor + * @param region The region to highlight + * @param color The color to use for the highlighting + * @return Unique ID used to remove the highlighting again later + */ u32 addBackgroundHighlight(const Region ®ion, color_t color); + + /** + * @brief Removes a background color highlighting from the Hex Editor + * @param id The ID of the highlighting to remove + */ void removeBackgroundHighlight(u32 id); + + /** + * @brief Adds a foreground color highlighting to the Hex Editor + * @param region The region to highlight + * @param color The color to use for the highlighting + * @return Unique ID used to remove the highlighting again later + */ u32 addForegroundHighlight(const Region ®ion, color_t color); + + /** + * @brief Removes a foreground color highlighting from the Hex Editor + * @param id The ID of the highlighting to remove + */ void removeForegroundHighlight(u32 id); + /** + * @brief Adds a hover tooltip to the Hex Editor + * @param region The region to add the tooltip to + * @param value Text to display in the tooltip + * @param color The color of the tooltip + * @return Unique ID used to remove the tooltip again later + */ u32 addTooltip(Region region, std::string value, color_t color); + + /** + * @brief Removes a hover tooltip from the Hex Editor + * @param id The ID of the tooltip to remove + */ void removeTooltip(u32 id); + + /** + * @brief Adds a background color highlighting to the Hex Editor using a callback function + * @param function Function that draws the highlighting based on the hovered region + * @return Unique ID used to remove the highlighting again later + */ u32 addTooltipProvider(TooltipFunction function); + + /** + * @brief Removes a background color highlighting from the Hex Editor + * @param id The ID of the highlighting to remove + */ void removeTooltipProvider(u32 id); + + /** + * @brief Adds a background color highlighting to the Hex Editor using a callback function + * @param function Function that draws the highlighting based on the hovered region + * @return Unique ID used to remove the highlighting again later + */ u32 addBackgroundHighlightingProvider(const impl::HighlightingFunction &function); + + /** + * @brief Removes a background color highlighting from the Hex Editor + * @param id The ID of the highlighting to remove + */ void removeBackgroundHighlightingProvider(u32 id); + + /** + * @brief Adds a foreground color highlighting to the Hex Editor using a callback function + * @param function Function that draws the highlighting based on the hovered region + * @return Unique ID used to remove the highlighting again later + */ u32 addForegroundHighlightingProvider(const impl::HighlightingFunction &function); + + /** + * @brief Removes a foreground color highlighting from the Hex Editor + * @param id The ID of the highlighting to remove + */ void removeForegroundHighlightingProvider(u32 id); + /** + * @brief Checks if there's a valid selection in the Hex Editor right now + */ bool isSelectionValid(); + + /** + * @brief Gets the current selection in the Hex Editor + * @return The current selection + */ std::optional getSelection(); + + /** + * @brief Sets the current selection in the Hex Editor + * @param region The region to select + * @param provider The provider to select the region in + */ void setSelection(const Region ®ion, prv::Provider *provider = nullptr); + + /** + * @brief Sets the current selection in the Hex Editor + * @param region The region to select + */ void setSelection(const ProviderRegion ®ion); + + /** + * @brief Sets the current selection in the Hex Editor + * @param address The address to select + * @param size The size of the selection + * @param provider The provider to select the region in + */ void setSelection(u64 address, size_t size, prv::Provider *provider = nullptr); } + /* Functions to interact with Bookmarks */ namespace Bookmarks { struct Entry { @@ -124,10 +213,19 @@ namespace hex { bool locked; }; + /** + * @brief Adds a new bookmark + * @param address The address of the bookmark + * @param size The size of the bookmark + * @param name The name of the bookmark + * @param comment The comment of the bookmark + * @param color The color of the bookmark or 0x00 for the default color + */ void add(u64 address, size_t size, const std::string &name, const std::string &comment, color_t color = 0x00000000); } + /* Functions to interact with the loaded data provider */ namespace Provider { namespace impl { @@ -137,30 +235,82 @@ namespace hex { } + /** + * @brief Gets the currently selected data provider + * @return The currently selected data provider + */ prv::Provider *get(); + + /** + * @brief Gets a list of all currently loaded data providers + * @return The currently loaded data providers + */ const std::vector &getProviders(); + /** + * @brief Sets the currently selected data provider + * @param index Index of the provider to select + */ void setCurrentProvider(u32 index); + /** + * @brief Checks whether the currently selected data provider is valid + * @return Whether the currently selected data provider is valid + */ bool isValid(); + + /** + * @brief Marks the currently selected data provider as dirty + */ void markDirty(); + + /** + * @brief Marks the currently selected data provider as clean + */ void resetDirty(); + + /** + * @brief Checks whether the currently selected data provider is dirty + * @return Whether the currently selected data provider is dirty + */ bool isDirty(); + + /** + * @brief Adds a newly created provider to the list of providers + * @param provider The provider to add + * @param skipLoadInterface Whether to skip loading the provider's loading interface + */ void add(prv::Provider *provider, bool skipLoadInterface = false); + /** + * @brief Creates a new provider and adds it to the list of providers + * @tparam T The type of the provider to create + * @param args Arguments to pass to the provider's constructor + */ template T> void add(auto &&...args) { add(new T(std::forward(args)...)); } + /** + * @brief Removes a provider from the list of providers + * @param provider The provider to remove + * @param noQuestions Whether to skip asking the user for confirmation + */ void remove(prv::Provider *provider, bool noQuestions = false); + /** + * @brief Creates a new provider using its unlocalized name + * @param unlocalizedName The unlocalized name of the provider to create + * @param skipLoadInterface Whether to skip loading the provider's loading interface + */ prv::Provider* createProvider(const std::string &unlocalizedName, bool skipLoadInterface = false); } + /* Functions to interact with various ImHex system settings */ namespace System { namespace impl { @@ -204,37 +354,148 @@ namespace hex { Error }; + /** + * @brief Closes ImHex + * @param noQuestions Whether to skip asking the user for confirmation + */ + void closeImHex(bool noQuestions = false); + + /** + * @brief Restarts ImHex + */ + void restartImHex(); + + /** + * @brief Sets the progress bar in the task bar + * @param state The state of the progress bar + * @param type The type of the progress bar progress + * @param progress The progress of the progress bar + */ void setTaskBarProgress(TaskProgressState state, TaskProgressType type, u32 progress); + + /** + * @brief Gets the current program arguments + * @return The current program arguments + */ const ProgramArguments &getProgramArguments(); + + /** + * @brief Gets a program argument + * @param index The index of the argument to get + * @return The argument at the given index + */ std::optional getProgramArgument(int index); + + /** + * @brief Gets the current target FPS + * @return The current target FPS + */ float getTargetFPS(); + + /** + * @brief Sets the target FPS + * @param fps The target FPS + */ void setTargetFPS(float fps); + + /** + * @brief Gets the current global scale + * @return The current global scale + */ float getGlobalScale(); + + /** + * @brief Gets the current native scale + * @return The current native scale + */ float getNativeScale(); + + /** + * @brief Gets the current main window position + * @return Position of the main window + */ ImVec2 getMainWindowPosition(); + + /** + * @brief Gets the current main window size + * @return Size of the main window + */ ImVec2 getMainWindowSize(); + + /** + * @brief Gets the current main dock space ID + * @return ID of the main dock space + */ ImGuiID getMainDockSpaceId(); + + /** + * @brief Checks if borderless window mode is enabled currently + * @return Whether borderless window mode is enabled + */ bool isBorderlessWindowModeEnabled(); + /** + * @brief Gets the init arguments passed to ImHex from the splash screen + * @return Init arguments + */ std::map &getInitArguments(); constexpr static float DefaultFontSize = 13.0; + + /** + * @brief Gets the current custom font path + * @return The current custom font path + */ const std::filesystem::path &getCustomFontPath(); + + /** + * @brief Gets the current font size + * @return The current font size + */ float getFontSize(); + + /** + * @brief Sets if ImHex should follow the system theme + * @param enabled Whether to follow the system theme + */ void enableSystemThemeDetection(bool enabled); + + /** + * @brief Checks if ImHex follows the system theme + * @return Whether ImHex follows the system theme + */ bool usesSystemThemeDetection(); + + /** + * @brief Gets the currently set additional folder paths + * @return The currently set additional folder paths + */ const std::vector &getAdditionalFolderPaths(); + + /** + * @brief Sets the additional folder paths + * @param paths The additional folder paths + */ void setAdditionalFolderPaths(const std::vector &paths); + + /** + * @brief Gets the current GPU vendor + * @return The current GPU vendor + */ const std::string &getGPUVendor(); + /** + * @brief Checks if ImHex is running in portable mode + * @return Whether ImHex is running in portable mode + */ bool isPortableVersion(); } diff --git a/lib/libimhex/include/hex/api/keybinding.hpp b/lib/libimhex/include/hex/api/keybinding.hpp index 4193b38ad..09fbd6cbb 100644 --- a/lib/libimhex/include/hex/api/keybinding.hpp +++ b/lib/libimhex/include/hex/api/keybinding.hpp @@ -363,14 +363,54 @@ namespace hex { return result; } + /** + * @brief The ShortcutManager handles global and view-specific shortcuts. + * New shortcuts can be constructed using the + operator on Key objects. For example: CTRL + ALT + Keys::A + */ class ShortcutManager { public: + + /** + * @brief Add a global shortcut. Global shortcuts can be triggered regardless of what view is currently focused + * @param shortcut The shortcut to add. + * @param callback The callback to call when the shortcut is triggered. + */ static void addGlobalShortcut(const Shortcut &shortcut, const std::function &callback); + + /** + * @brief Add a view-specific shortcut. View-specific shortcuts can only be triggered when the specified view is focused. + * @param view The view to add the shortcut to. + * @param shortcut The shortcut to add. + * @param callback The callback to call when the shortcut is triggered. + */ static void addShortcut(View *view, const Shortcut &shortcut, const std::function &callback); + + /** + * @brief Process a key event. This should be called from the main loop. + * @param currentView Current view to process + * @param ctrl Whether the CTRL key is pressed + * @param alt Whether the ALT key is pressed + * @param shift Whether the SHIFT key is pressed + * @param super Whether the SUPER key is pressed + * @param focused Whether the current view is focused + * @param keyCode The key code of the key that was pressed + */ static void process(View *currentView, bool ctrl, bool alt, bool shift, bool super, bool focused, u32 keyCode); + + /** + * @brief Process a key event. This should be called from the main loop. + * @param ctrl Whether the CTRL key is pressed + * @param alt Whether the ALT key is pressed + * @param shift Whether the SHIFT key is pressed + * @param super Whether the SUPER key is pressed + * @param keyCode The key code of the key that was pressed + */ static void processGlobals(bool ctrl, bool alt, bool shift, bool super, u32 keyCode); + /** + * @brief Clear all shortcuts + */ static void clearShortcuts(); private: diff --git a/lib/libimhex/include/hex/api/project_file_manager.hpp b/lib/libimhex/include/hex/api/project_file_manager.hpp index 67e1eeb39..f07bf9134 100644 --- a/lib/libimhex/include/hex/api/project_file_manager.hpp +++ b/lib/libimhex/include/hex/api/project_file_manager.hpp @@ -14,42 +14,104 @@ namespace hex { + /** + * @brief Project file manager + * + * The project file manager is used to load and store project files. It is used by all features of ImHex + * that want to store any data to a Project File. + * + */ class ProjectFile { public: struct Handler { using Function = std::function; - std::fs::path basePath; - bool required; - Function load, store; + + std::fs::path basePath; //< Base path for where to store the files in the project file + bool required; //< If true, ImHex will display an error if this handler fails to load or store data + Function load, store; //< Functions to load and store data }; struct ProviderHandler { using Function = std::function; - std::fs::path basePath; - bool required; - Function load, store; + + std::fs::path basePath; //< Base path for where to store the files in the project file + bool required; //< If true, ImHex will display an error if this handler fails to load or store data + Function load, store; //< Functions to load and store data }; + /** + * @brief Load a project file + * + * @param filePath Path to the project file + * @return true if the project file was loaded successfully + * @return false if the project file was not loaded successfully + */ static bool load(const std::fs::path &filePath); + + /** + * @brief Store a project file + * + * @param filePath Path to the project file + * @return true if the project file was stored successfully + * @return false if the project file was not stored successfully + */ static bool store(std::optional filePath = std::nullopt); + /** + * @brief Check if a project file is currently loaded + * + * @return true if a project file is currently loaded + * @return false if no project file is currently loaded + */ static bool hasPath(); + + /** + * @brief Clear the currently loaded project file + */ static void clearPath(); + + /** + * @brief Get the path to the currently loaded project file + * @return Path to the currently loaded project file + */ static std::fs::path getPath(); + + /** + * @brief Set the path to the currently loaded project file + * @param path Path to the currently loaded project file + */ static void setPath(const std::fs::path &path); + /** + * @brief Register a handler for storing and loading global data from a project file + * + * @param handler The handler to register + */ static void registerHandler(const Handler &handler) { getHandlers().push_back(handler); } + /** + * @brief Register a handler for storing and loading per-provider data from a project file + * + * @param handler The handler to register + */ static void registerPerProviderHandler(const ProviderHandler &handler) { getProviderHandlers().push_back(handler); } + /** + * @brief Get the list of registered handlers + * @return List of registered handlers + */ static std::vector& getHandlers() { return s_handlers; } + /** + * @brief Get the list of registered per-provider handlers + * @return List of registered per-provider handlers + */ static std::vector& getProviderHandlers() { return s_providerHandlers; } diff --git a/lib/libimhex/include/hex/api/task.hpp b/lib/libimhex/include/hex/api/task.hpp index 5af6570ca..7ed05a6e0 100644 --- a/lib/libimhex/include/hex/api/task.hpp +++ b/lib/libimhex/include/hex/api/task.hpp @@ -17,6 +17,9 @@ namespace hex { class TaskHolder; class TaskManager; + /** + * @brief A type representing a running asynchronous task + */ class Task { public: Task() = default; @@ -26,14 +29,38 @@ namespace hex { Task(Task &&other) noexcept; ~Task(); + /** + * @brief Updates the current process value of the task + * @param value Current value + */ void update(u64 value = 0); + + /** + * @brief Sets the maximum value of the task + * @param value Maximum value of the task + */ void setMaxValue(u64 value); + + /** + * @brief Interrupts the task + * For regular Tasks, this just throws an exception to stop the task. + * If a custom interrupt callback is set, an exception is thrown and the callback is called. + */ + void interrupt(); + + /** + * @brief Sets a callback that is called when the task is interrupted + * @param callback Callback to be called + */ + void setInterruptCallback(std::function callback); + [[nodiscard]] bool isBackgroundTask() const; [[nodiscard]] bool isFinished() const; [[nodiscard]] bool hadException() const; [[nodiscard]] bool wasInterrupted() const; [[nodiscard]] bool shouldInterrupt() const; + void clearException(); [[nodiscard]] std::string getExceptionMessage() const; @@ -41,10 +68,6 @@ namespace hex { [[nodiscard]] u64 getValue() const; [[nodiscard]] u64 getMaxValue() const; - void interrupt(); - - void setInterruptCallback(std::function callback); - private: void finish(); void interruption(); @@ -72,6 +95,9 @@ namespace hex { friend class TaskManager; }; + /** + * @brief A type holding a weak reference to a Task + */ class TaskHolder { public: TaskHolder() = default; @@ -87,6 +113,9 @@ namespace hex { std::weak_ptr m_task; }; + /** + * @brief The Task Manager is responsible for running and managing asynchronous tasks + */ class TaskManager { public: TaskManager() = delete; @@ -96,19 +125,45 @@ namespace hex { constexpr static auto NoProgress = 0; + /** + * @brief Creates a new asynchronous task that gets displayed in the Task Manager in the footer + * @param name Name of the task + * @param maxValue Maximum value of the task + * @param function Function to be executed + * @return A TaskHolder holding a weak reference to the task + */ static TaskHolder createTask(std::string name, u64 maxValue, std::function function); + + /** + * @brief Creates a new asynchronous task that does not get displayed in the Task Manager + * @param name Name of the task + * @param function Function to be executed + * @return A TaskHolder holding a weak reference to the task + */ static TaskHolder createBackgroundTask(std::string name, std::function function); + + /** + * @brief Creates a new synchronous task that will execute the given function at the start of the next frame + * @param function Function to be executed + */ + static void doLater(const std::function &function); + + /** + * @brief Creates a callback that will be executed when all tasks are finished + * @param function Function to be executed + */ + static void runWhenTasksFinished(const std::function &function); + + static void collectGarbage(); static size_t getRunningTaskCount(); static size_t getRunningBackgroundTaskCount(); - static std::list> &getRunningTasks(); - static void doLater(const std::function &function); + static std::list> &getRunningTasks(); static void runDeferredCalls(); - static void runWhenTasksFinished(const std::function &function); private: static std::mutex s_deferredCallsMutex, s_tasksFinishedMutex; diff --git a/lib/libimhex/include/hex/api/theme_manager.hpp b/lib/libimhex/include/hex/api/theme_manager.hpp index 7f76fde5e..4e996f616 100644 --- a/lib/libimhex/include/hex/api/theme_manager.hpp +++ b/lib/libimhex/include/hex/api/theme_manager.hpp @@ -11,6 +11,9 @@ namespace hex::api { + /** + * @brief The Theme Manager takes care of loading and applying themes + */ class ThemeManager { public: constexpr static auto NativeTheme = "Native"; @@ -25,12 +28,35 @@ namespace hex::api { }; using StyleMap = std::map; + /** + * @brief Changes the current theme to the one with the given name + * @param name Name of the theme to change to + */ static void changeTheme(std::string name); + /** + * @brief Adds a theme from json data + * @param content JSON data of the theme + */ static void addTheme(const std::string &content); + + /** + * @brief Adds a theme handler to handle color values loaded from a theme file + * @param name Name of the handler + * @param colorMap Map of color names to their respective constants + * @param getFunction Function to get the color value of a constant + * @param setFunction Function to set the color value of a constant + */ static void addThemeHandler(const std::string &name, const ColorMap &colorMap, const std::function &getFunction, const std::function &setFunction); + + /** + * @brief Adds a style handler to handle style values loaded from a theme file + * @param name Name of the handler + * @param styleMap Map of style names to their respective constants + */ static void addStyleHandler(const std::string &name, const StyleMap &styleMap); + static std::vector getThemeNames(); static const std::string &getThemeImagePostfix(); diff --git a/lib/libimhex/source/api/content_registry.cpp b/lib/libimhex/source/api/content_registry.cpp index 8f7ccc75d..ec145a8ca 100644 --- a/lib/libimhex/source/api/content_registry.cpp +++ b/lib/libimhex/source/api/content_registry.cpp @@ -19,58 +19,89 @@ namespace hex { constexpr auto SettingsFile = "settings.json"; - void load() { - bool loaded = false; - for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Config)) { - wolv::io::File file(dir / SettingsFile, wolv::io::File::Mode::Read); + namespace impl { - if (file.isValid()) { - getSettingsData() = nlohmann::json::parse(file.readString()); - loaded = true; - break; + std::map> &getEntries() { + static std::map> entries; + + return entries; + } + + std::map &getCategoryDescriptions() { + static std::map descriptions; + + return descriptions; + } + + nlohmann::json getSetting(const std::string &unlocalizedCategory, const std::string &unlocalizedName) { + auto &settings = getSettingsData(); + + if (!settings.contains(unlocalizedCategory)) return {}; + if (!settings[unlocalizedCategory].contains(unlocalizedName)) return {}; + + return settings[unlocalizedCategory][unlocalizedName]; + } + + nlohmann::json &getSettingsData() { + static nlohmann::json settings; + + return settings; + } + + void load() { + bool loaded = false; + for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Config)) { + wolv::io::File file(dir / SettingsFile, wolv::io::File::Mode::Read); + + if (file.isValid()) { + getSettingsData() = nlohmann::json::parse(file.readString()); + loaded = true; + break; + } + } + + if (!loaded) + store(); + } + + void store() { + for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Config)) { + wolv::io::File file(dir / SettingsFile, wolv::io::File::Mode::Create); + + if (file.isValid()) { + file.write(getSettingsData().dump(4)); + break; + } } } - if (!loaded) - store(); - } - - void store() { - for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Config)) { - wolv::io::File file(dir / SettingsFile, wolv::io::File::Mode::Create); - - if (file.isValid()) { - file.write(getSettingsData().dump(4)); - break; + void clear() { + for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Config)) { + wolv::io::fs::remove(dir / SettingsFile); } } - } - void clear() { - for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Config)) { - wolv::io::fs::remove(dir / SettingsFile); - } - } + static auto getCategoryEntry(const std::string &unlocalizedCategory) { + auto &entries = getEntries(); + const size_t curSlot = entries.size(); + auto found = entries.find(Category { unlocalizedCategory }); - static auto getCategoryEntry(const std::string &unlocalizedCategory) { - auto &entries = getEntries(); - const size_t curSlot = entries.size(); - auto found = entries.find(Category { unlocalizedCategory }); + if (found == entries.end()) { + auto [iter, _] = entries.emplace(Category { unlocalizedCategory, curSlot }, std::vector {}); + return iter; + } - if (found == entries.end()) { - auto [iter, _] = entries.emplace(Category { unlocalizedCategory, curSlot }, std::vector {}); - return iter; + return found; } - return found; } - void add(const std::string &unlocalizedCategory, const std::string &unlocalizedName, i64 defaultValue, const Callback &callback, bool requiresRestart) { + void add(const std::string &unlocalizedCategory, const std::string &unlocalizedName, i64 defaultValue, const impl::Callback &callback, bool requiresRestart) { log::debug("Registered new integer setting: [{}]: {}", unlocalizedCategory, unlocalizedName); - getCategoryEntry(unlocalizedCategory)->second.emplace_back(Entry { unlocalizedName, requiresRestart, callback }); + impl::getCategoryEntry(unlocalizedCategory)->second.emplace_back(impl::Entry { unlocalizedName, requiresRestart, callback }); - auto &json = getSettingsData(); + auto &json = impl::getSettingsData(); if (!json.contains(unlocalizedCategory)) json[unlocalizedCategory] = nlohmann::json::object(); @@ -78,12 +109,12 @@ namespace hex { json[unlocalizedCategory][unlocalizedName] = int(defaultValue); } - void add(const std::string &unlocalizedCategory, const std::string &unlocalizedName, const std::string &defaultValue, const Callback &callback, bool requiresRestart) { + void add(const std::string &unlocalizedCategory, const std::string &unlocalizedName, const std::string &defaultValue, const impl::Callback &callback, bool requiresRestart) { log::debug("Registered new string setting: [{}]: {}", unlocalizedCategory, unlocalizedName); - getCategoryEntry(unlocalizedCategory)->second.emplace_back(Entry { unlocalizedName, requiresRestart, callback }); + impl::getCategoryEntry(unlocalizedCategory)->second.emplace_back(impl::Entry { unlocalizedName, requiresRestart, callback }); - auto &json = getSettingsData(); + auto &json = impl::getSettingsData(); if (!json.contains(unlocalizedCategory)) json[unlocalizedCategory] = nlohmann::json::object(); @@ -91,12 +122,12 @@ namespace hex { json[unlocalizedCategory][unlocalizedName] = std::string(defaultValue); } - void add(const std::string &unlocalizedCategory, const std::string &unlocalizedName, const std::vector &defaultValue, const Callback &callback, bool requiresRestart) { + void add(const std::string &unlocalizedCategory, const std::string &unlocalizedName, const std::vector &defaultValue, const impl::Callback &callback, bool requiresRestart) { log::debug("Registered new string array setting: [{}]: {}", unlocalizedCategory, unlocalizedName); - getCategoryEntry(unlocalizedCategory)->second.emplace_back(Entry { unlocalizedName, requiresRestart, callback }); + impl::getCategoryEntry(unlocalizedCategory)->second.emplace_back(impl::Entry { unlocalizedName, requiresRestart, callback }); - auto &json = getSettingsData(); + auto &json = impl::getSettingsData(); if (!json.contains(unlocalizedCategory)) json[unlocalizedCategory] = nlohmann::json::object(); @@ -105,11 +136,11 @@ namespace hex { } void addCategoryDescription(const std::string &unlocalizedCategory, const std::string &unlocalizedCategoryDescription) { - getCategoryDescriptions()[unlocalizedCategory] = unlocalizedCategoryDescription; + impl::getCategoryDescriptions()[unlocalizedCategory] = unlocalizedCategoryDescription; } void write(const std::string &unlocalizedCategory, const std::string &unlocalizedName, i64 value) { - auto &json = getSettingsData(); + auto &json = impl::getSettingsData(); if (!json.contains(unlocalizedCategory)) json[unlocalizedCategory] = nlohmann::json::object(); @@ -118,7 +149,7 @@ namespace hex { } void write(const std::string &unlocalizedCategory, const std::string &unlocalizedName, const std::string &value) { - auto &json = getSettingsData(); + auto &json = impl::getSettingsData(); if (!json.contains(unlocalizedCategory)) json[unlocalizedCategory] = nlohmann::json::object(); @@ -127,7 +158,7 @@ namespace hex { } void write(const std::string &unlocalizedCategory, const std::string &unlocalizedName, const std::vector &value) { - auto &json = getSettingsData(); + auto &json = impl::getSettingsData(); if (!json.contains(unlocalizedCategory)) json[unlocalizedCategory] = nlohmann::json::object(); @@ -137,7 +168,7 @@ namespace hex { i64 read(const std::string &unlocalizedCategory, const std::string &unlocalizedName, i64 defaultValue) { - auto &json = getSettingsData(); + auto &json = impl::getSettingsData(); if (!json.contains(unlocalizedCategory)) return defaultValue; @@ -151,7 +182,7 @@ namespace hex { } std::string read(const std::string &unlocalizedCategory, const std::string &unlocalizedName, const std::string &defaultValue) { - auto &json = getSettingsData(); + auto &json = impl::getSettingsData(); if (!json.contains(unlocalizedCategory)) return defaultValue; @@ -165,7 +196,7 @@ namespace hex { } std::vector read(const std::string &unlocalizedCategory, const std::string &unlocalizedName, const std::vector &defaultValue) { - auto &json = getSettingsData(); + auto &json = impl::getSettingsData(); if (!json.contains(unlocalizedCategory)) return defaultValue; @@ -181,61 +212,37 @@ namespace hex { return json[unlocalizedCategory][unlocalizedName].get>(); } - - std::map> &getEntries() { - static std::map> entries; - - return entries; - } - - std::map &getCategoryDescriptions() { - static std::map descriptions; - - return descriptions; - } - - nlohmann::json getSetting(const std::string &unlocalizedCategory, const std::string &unlocalizedName) { - auto &settings = getSettingsData(); - - if (!settings.contains(unlocalizedCategory)) return {}; - if (!settings[unlocalizedCategory].contains(unlocalizedName)) return {}; - - return settings[unlocalizedCategory][unlocalizedName]; - } - - nlohmann::json &getSettingsData() { - static nlohmann::json settings; - - return settings; - } - } namespace ContentRegistry::CommandPaletteCommands { - void add(Type type, const std::string &command, const std::string &unlocalizedDescription, const DisplayCallback &displayCallback, const ExecuteCallback &executeCallback) { + void add(Type type, const std::string &command, const std::string &unlocalizedDescription, const impl::DisplayCallback &displayCallback, const impl::ExecuteCallback &executeCallback) { log::debug("Registered new command palette command: {}", command); - getEntries().push_back(ContentRegistry::CommandPaletteCommands::Entry { type, command, unlocalizedDescription, displayCallback, executeCallback }); + impl::getEntries().push_back(ContentRegistry::CommandPaletteCommands::impl::Entry { type, command, unlocalizedDescription, displayCallback, executeCallback }); } - void addHandler(Type type, const std::string &command, const QueryCallback &queryCallback, const DisplayCallback &displayCallback) { + void addHandler(Type type, const std::string &command, const impl::QueryCallback &queryCallback, const impl::DisplayCallback &displayCallback) { log::debug("Registered new command palette command handler: {}", command); - getHandlers().push_back(ContentRegistry::CommandPaletteCommands::Handler { type, command, queryCallback, displayCallback }); + impl::getHandlers().push_back(ContentRegistry::CommandPaletteCommands::impl::Handler { type, command, queryCallback, displayCallback }); } - std::vector &getEntries() { - static std::vector commands; + namespace impl { - return commands; - } + std::vector &getEntries() { + static std::vector commands; - std::vector &getHandlers() { - static std::vector commands; + return commands; + } + + std::vector &getHandlers() { + static std::vector commands; + + return commands; + } - return commands; } } @@ -270,14 +277,14 @@ namespace hex { runtime.setIncludePaths(fs::getDefaultPaths(fs::ImHexPath::PatternsInclude) | fs::getDefaultPaths(fs::ImHexPath::Patterns)); - for (const auto &func : getFunctions()) { + for (const auto &func : impl::getFunctions()) { if (func.dangerous) runtime.addDangerousFunction(func.ns, func.name, func.parameterCount, func.callback); else runtime.addFunction(func.ns, func.name, func.parameterCount, func.callback); } - for (const auto &[name, callback] : getPragmas()) { + for (const auto &[name, callback] : impl::getPragmas()) { runtime.addPragma(name, callback); } @@ -288,13 +295,13 @@ namespace hex { void addPragma(const std::string &name, const pl::api::PragmaHandler &handler) { log::debug("Registered new pattern language pragma: {}", name); - getPragmas()[name] = handler; + impl::getPragmas()[name] = handler; } void addFunction(const pl::api::Namespace &ns, const std::string &name, pl::api::FunctionParameterCount parameterCount, const pl::api::FunctionCallback &func) { log::debug("Registered new pattern language function: {}", getFunctionName(ns, name)); - getFunctions().push_back({ + impl::getFunctions().push_back({ ns, name, parameterCount, func, false @@ -304,7 +311,7 @@ namespace hex { void addDangerousFunction(const pl::api::Namespace &ns, const std::string &name, pl::api::FunctionParameterCount parameterCount, const pl::api::FunctionCallback &func) { log::debug("Registered new dangerous pattern language function: {}", getFunctionName(ns, name)); - getFunctions().push_back({ + impl::getFunctions().push_back({ ns, name, parameterCount, func, true @@ -312,48 +319,58 @@ namespace hex { } - std::map &impl::getVisualizers() { - static std::map visualizers; - - return visualizers; - } - - void addVisualizer(const std::string &name, const VisualizerFunctionCallback &function, u32 parameterCount) { + void addVisualizer(const std::string &name, const impl::VisualizerFunctionCallback &function, u32 parameterCount) { log::debug("Registered new pattern visualizer function: {}", name); impl::getVisualizers()[name] = impl::Visualizer { parameterCount, function }; } - std::map &getPragmas() { - static std::map pragmas; - return pragmas; + namespace impl { + + std::map &getVisualizers() { + static std::map visualizers; + + return visualizers; + } + + std::map &getPragmas() { + static std::map pragmas; + + return pragmas; + } + + std::vector &getFunctions() { + static std::vector functions; + + return functions; + } + } - std::vector &getFunctions() { - static std::vector functions; - - return functions; - } } namespace ContentRegistry::Views { + namespace impl { + + std::map &getEntries() { + static std::map views; + + return views; + } + + } + void impl::add(View *view) { log::debug("Registered new view: {}", view->getUnlocalizedName()); - getEntries().insert({ view->getUnlocalizedName(), view }); - } - - std::map &getEntries() { - static std::map views; - - return views; + impl::getEntries().insert({ view->getUnlocalizedName(), view }); } View *getViewByName(const std::string &unlocalizedName) { - auto &views = getEntries(); + auto &views = impl::getEntries(); if (views.contains(unlocalizedName)) return views[unlocalizedName]; @@ -368,13 +385,17 @@ namespace hex { void add(const std::string &unlocalizedName, const impl::Callback &function) { log::debug("Registered new tool: {}", unlocalizedName); - getEntries().emplace_back(impl::Entry { unlocalizedName, function, false }); + impl::getEntries().emplace_back(impl::Entry { unlocalizedName, function, false }); } - std::vector &getEntries() { - static std::vector entries; + namespace impl { + + std::vector &getEntries() { + static std::vector tools; + + return tools; + } - return entries; } } @@ -384,21 +405,26 @@ namespace hex { void add(const std::string &unlocalizedName, size_t requiredSize, impl::GeneratorFunction displayGeneratorFunction, std::optional editingFunction) { log::debug("Registered new data inspector format: {}", unlocalizedName); - getEntries().push_back({ unlocalizedName, requiredSize, requiredSize, std::move(displayGeneratorFunction), std::move(editingFunction) }); + impl::getEntries().push_back({ unlocalizedName, requiredSize, requiredSize, std::move(displayGeneratorFunction), std::move(editingFunction) }); } void add(const std::string &unlocalizedName, size_t requiredSize, size_t maxSize, impl::GeneratorFunction displayGeneratorFunction, std::optional editingFunction) { log::debug("Registered new data inspector format: {}", unlocalizedName); - getEntries().push_back({ unlocalizedName, requiredSize, maxSize, std::move(displayGeneratorFunction), std::move(editingFunction) }); + impl::getEntries().push_back({ unlocalizedName, requiredSize, maxSize, std::move(displayGeneratorFunction), std::move(editingFunction) }); } - std::vector &getEntries() { - static std::vector entries; + namespace impl { + + std::vector &getEntries() { + static std::vector entries; + + return entries; + } - return entries; } + } namespace ContentRegistry::DataProcessorNode { @@ -410,13 +436,17 @@ namespace hex { } void addSeparator() { - getEntries().push_back({ "", "", [] { return nullptr; } }); + impl::getEntries().push_back({ "", "", [] { return nullptr; } }); } - std::vector &getEntries() { - static std::vector nodes; + namespace impl { + + std::vector &getEntries() { + static std::vector nodes; + + return nodes; + } - return nodes; } } @@ -449,7 +479,7 @@ namespace hex { LangEntry::setFallbackLanguage(code.get()); } - getLanguages().insert({ code.get(), hex::format("{} ({})", language.get(), country.get()) }); + impl::getLanguages().insert({ code.get(), hex::format("{} ({})", language.get(), country.get()) }); std::map translationDefinitions; for (auto &[key, value] : translations.items()) { @@ -461,20 +491,25 @@ namespace hex { translationDefinitions[key] = value.get(); } - getLanguageDefinitions()[code.get()].emplace_back(std::move(translationDefinitions)); + impl::getLanguageDefinitions()[code.get()].emplace_back(std::move(translationDefinitions)); } - std::map &getLanguages() { - static std::map languages; + namespace impl { + + std::map &getLanguages() { + static std::map languages; + + return languages; + } + + std::map> &getLanguageDefinitions() { + static std::map> definitions; + + return definitions; + } - return languages; } - std::map> &getLanguageDefinitions() { - static std::map> definitions; - - return definitions; - } } @@ -483,13 +518,13 @@ namespace hex { void registerMainMenuItem(const std::string &unlocalizedName, u32 priority) { log::debug("Registered new main menu item: {}", unlocalizedName); - getMainMenuItems().insert({ priority, { unlocalizedName } }); + impl::getMainMenuItems().insert({ priority, { unlocalizedName } }); } void addMenuItem(const std::vector &unlocalizedMainMenuNames, u32 priority, const Shortcut &shortcut, const impl::MenuCallback &function, const impl::EnabledCallback& enabledCallback, View *view) { log::debug("Added new menu item to menu {} with priority {}", wolv::util::combineStrings(unlocalizedMainMenuNames, " -> "), priority); - getMenuItems().insert({ + impl::getMenuItems().insert({ priority, { unlocalizedMainMenuNames, shortcut, function, enabledCallback } }); @@ -503,86 +538,89 @@ namespace hex { log::debug("Added new menu item sub menu to menu {} with priority {}", wolv::util::combineStrings(unlocalizedMainMenuNames, " -> "), priority); unlocalizedMainMenuNames.emplace_back(impl::SubMenuValue); - getMenuItems().insert({ + impl::getMenuItems().insert({ priority, { unlocalizedMainMenuNames, {}, function, enabledCallback } }); } void addMenuItemSeparator(std::vector unlocalizedMainMenuNames, u32 priority) { unlocalizedMainMenuNames.emplace_back(impl::SeparatorValue); - getMenuItems().insert({ + impl::getMenuItems().insert({ priority, { unlocalizedMainMenuNames, {}, []{}, []{ return true; } } }); } void addWelcomeScreenEntry(const impl::DrawCallback &function) { - getWelcomeScreenEntries().push_back(function); + impl::getWelcomeScreenEntries().push_back(function); } void addFooterItem(const impl::DrawCallback &function) { - getFooterItems().push_back(function); + impl::getFooterItems().push_back(function); } void addToolbarItem(const impl::DrawCallback &function) { - getToolbarItems().push_back(function); + impl::getToolbarItems().push_back(function); } void addSidebarItem(const std::string &icon, const impl::DrawCallback &function) { - getSidebarItems().push_back({ icon, function }); + impl::getSidebarItems().push_back({ icon, function }); } void addTitleBarButton(const std::string &icon, const std::string &unlocalizedTooltip, const impl::ClickCallback &function) { - getTitleBarButtons().push_back({ icon, unlocalizedTooltip, function }); + impl::getTitleBarButtons().push_back({ icon, unlocalizedTooltip, function }); } void addLayout(const std::string &unlocalizedName, const impl::LayoutFunction &function) { log::debug("Added new layout: {}", unlocalizedName); - getLayouts().push_back({ unlocalizedName, function }); + impl::getLayouts().push_back({ unlocalizedName, function }); } + namespace impl { - std::multimap &getMainMenuItems() { - static std::multimap items; + std::multimap &getMainMenuItems() { + static std::multimap items; - return items; - } - std::multimap &getMenuItems() { - static std::multimap items; + return items; + } + std::multimap &getMenuItems() { + static std::multimap items; - return items; - } + return items; + } - std::vector &getWelcomeScreenEntries() { - static std::vector entries; + std::vector &getWelcomeScreenEntries() { + static std::vector entries; - return entries; - } - std::vector &getFooterItems() { - static std::vector items; + return entries; + } + std::vector &getFooterItems() { + static std::vector items; - return items; - } - std::vector &getToolbarItems() { - static std::vector items; + return items; + } + std::vector &getToolbarItems() { + static std::vector items; - return items; - } - std::vector &getSidebarItems() { - static std::vector items; + return items; + } + std::vector &getSidebarItems() { + static std::vector items; - return items; - } - std::vector &getTitleBarButtons() { - static std::vector buttons; + return items; + } + std::vector &getTitleBarButtons() { + static std::vector buttons; - return buttons; - } + return buttons; + } - std::vector &getLayouts() { - static std::vector layouts; + std::vector &getLayouts() { + static std::vector layouts; + + return layouts; + } - return layouts; } } @@ -595,12 +633,18 @@ namespace hex { getEntries().push_back(unlocalizedName); } - std::vector &getEntries() { - static std::vector providerNames; - return providerNames; + namespace impl { + + std::vector &getEntries() { + static std::vector providerNames; + + return providerNames; + } + } + } namespace ContentRegistry::DataFormatter { @@ -608,13 +652,17 @@ namespace hex { void add(const std::string &unlocalizedName, const impl::Callback &callback) { log::debug("Registered new data formatter: {}", unlocalizedName); - getEntries().push_back({ unlocalizedName, callback }); + impl::getEntries().push_back({ unlocalizedName, callback }); } - std::vector &getEntries() { - static std::vector entries; + namespace impl { + + std::vector &getEntries() { + static std::vector entries; + + return entries; + } - return entries; } } @@ -625,13 +673,17 @@ namespace hex { for (const auto &extension : extensions) log::debug("Registered new data handler for extensions: {}", extension); - getEntries().push_back({ extensions, callback }); + impl::getEntries().push_back({ extensions, callback }); } - std::vector &getEntries() { - static std::vector entries; + namespace impl { + + std::vector &getEntries() { + static std::vector entries; + + return entries; + } - return entries; } } @@ -700,29 +752,38 @@ namespace hex { return userData.editingDone || ImGui::IsKeyPressed(ImGuiKey_Enter) || ImGui::IsKeyPressed(ImGuiKey_Escape); } - void impl::addDataVisualizer(const std::string &unlocalizedName, DataVisualizer *visualizer) { - getVisualizers().insert({ unlocalizedName, visualizer }); + namespace impl { + + void addDataVisualizer(const std::string &unlocalizedName, DataVisualizer *visualizer) { + getVisualizers().insert({ unlocalizedName, visualizer }); + + } + + std::map &getVisualizers() { + static std::map visualizers; + + return visualizers; + } } - std::map &impl::getVisualizers() { - static std::map visualizers; - - return visualizers; - } } namespace ContentRegistry::Hashes { - std::vector &impl::getHashes() { - static std::vector hashes; + namespace impl { - return hashes; - } + std::vector &getHashes() { + static std::vector hashes; + + return hashes; + } + + void add(Hash *hash) { + getHashes().push_back(hash); + } - void impl::add(Hash *hash) { - getHashes().push_back(hash); } } diff --git a/lib/libimhex/source/api/imhex_api.cpp b/lib/libimhex/source/api/imhex_api.cpp index 3bfed1cc3..54d4d76fa 100644 --- a/lib/libimhex/source/api/imhex_api.cpp +++ b/lib/libimhex/source/api/imhex_api.cpp @@ -19,19 +19,6 @@ namespace hex { - namespace ImHexApi::Common { - - void closeImHex(bool noQuestions) { - EventManager::post(noQuestions); - } - - void restartImHex() { - EventManager::post(); - EventManager::post(false); - } - - } - namespace ImHexApi::HexEditor { @@ -408,6 +395,14 @@ namespace hex { } + void closeImHex(bool noQuestions) { + EventManager::post(noQuestions); + } + + void restartImHex() { + EventManager::post(); + EventManager::post(false); + } void setTaskBarProgress(TaskProgressState state, TaskProgressType type, u32 progress) { EventManager::post(u32(state), u32(type), progress); diff --git a/lib/libimhex/source/api/localization.cpp b/lib/libimhex/source/api/localization.cpp index 3d542047e..85eaea49a 100644 --- a/lib/libimhex/source/api/localization.cpp +++ b/lib/libimhex/source/api/localization.cpp @@ -76,7 +76,7 @@ namespace hex { void LangEntry::loadLanguage(const std::string &language) { LangEntry::s_currStrings.clear(); - auto &definitions = ContentRegistry::Language::getLanguageDefinitions(); + auto &definitions = ContentRegistry::Language::impl::getLanguageDefinitions(); if (!definitions.contains(language)) return; @@ -92,7 +92,7 @@ namespace hex { } const std::map &LangEntry::getSupportedLanguages() { - return ContentRegistry::Language::getLanguages(); + return ContentRegistry::Language::impl::getLanguages(); } void LangEntry::setFallbackLanguage(const std::string &language) { diff --git a/lib/libimhex/source/api/task.cpp b/lib/libimhex/source/api/task.cpp index 2aac5dc68..5dd97f499 100644 --- a/lib/libimhex/source/api/task.cpp +++ b/lib/libimhex/source/api/task.cpp @@ -47,7 +47,7 @@ namespace hex { void Task::update(u64 value) { this->m_currValue = value; - if (this->m_shouldInterrupt) + if (this->m_shouldInterrupt) [[unlikely]] throw TaskInterruptor(); } diff --git a/main/source/init/tasks.cpp b/main/source/init/tasks.cpp index 71e80ace0..fcecbd057 100644 --- a/main/source/init/tasks.cpp +++ b/main/source/init/tasks.cpp @@ -225,7 +225,7 @@ namespace hex::init { while (ImHexApi::Provider::isValid()) ImHexApi::Provider::remove(ImHexApi::Provider::get()); - ContentRegistry::Provider::getEntries().clear(); + ContentRegistry::Provider::impl::getEntries().clear(); ImHexApi::System::getInitArguments().clear(); ImHexApi::HexEditor::impl::getBackgroundHighlights().clear(); @@ -235,48 +235,48 @@ namespace hex::init { ImHexApi::HexEditor::impl::getTooltips().clear(); ImHexApi::HexEditor::impl::getTooltipFunctions().clear(); - ContentRegistry::Settings::getEntries().clear(); - ContentRegistry::Settings::getSettingsData().clear(); + ContentRegistry::Settings::impl::getEntries().clear(); + ContentRegistry::Settings::impl::getSettingsData().clear(); - ContentRegistry::CommandPaletteCommands::getEntries().clear(); - ContentRegistry::CommandPaletteCommands::getHandlers().clear(); + ContentRegistry::CommandPaletteCommands::impl::getEntries().clear(); + ContentRegistry::CommandPaletteCommands::impl::getHandlers().clear(); - ContentRegistry::PatternLanguage::getFunctions().clear(); - ContentRegistry::PatternLanguage::getPragmas().clear(); + ContentRegistry::PatternLanguage::impl::getFunctions().clear(); + ContentRegistry::PatternLanguage::impl::getPragmas().clear(); ContentRegistry::PatternLanguage::impl::getVisualizers().clear(); { - auto &views = ContentRegistry::Views::getEntries(); + auto &views = ContentRegistry::Views::impl::getEntries(); for (auto &[name, view] : views) delete view; views.clear(); } - ContentRegistry::Tools::getEntries().clear(); - ContentRegistry::DataInspector::getEntries().clear(); + ContentRegistry::Tools::impl::getEntries().clear(); + ContentRegistry::DataInspector::impl::getEntries().clear(); - ContentRegistry::Language::getLanguages().clear(); - ContentRegistry::Language::getLanguageDefinitions().clear(); + ContentRegistry::Language::impl::getLanguages().clear(); + ContentRegistry::Language::impl::getLanguageDefinitions().clear(); LangEntry::resetLanguageStrings(); - ContentRegistry::Interface::getWelcomeScreenEntries().clear(); - ContentRegistry::Interface::getFooterItems().clear(); - ContentRegistry::Interface::getToolbarItems().clear(); - ContentRegistry::Interface::getMainMenuItems().clear(); - ContentRegistry::Interface::getMenuItems().clear(); - ContentRegistry::Interface::getSidebarItems().clear(); - ContentRegistry::Interface::getTitleBarButtons().clear(); - ContentRegistry::Interface::getLayouts().clear(); + ContentRegistry::Interface::impl::getWelcomeScreenEntries().clear(); + ContentRegistry::Interface::impl::getFooterItems().clear(); + ContentRegistry::Interface::impl::getToolbarItems().clear(); + ContentRegistry::Interface::impl::getMainMenuItems().clear(); + ContentRegistry::Interface::impl::getMenuItems().clear(); + ContentRegistry::Interface::impl::getSidebarItems().clear(); + ContentRegistry::Interface::impl::getTitleBarButtons().clear(); + ContentRegistry::Interface::impl::getLayouts().clear(); ShortcutManager::clearShortcuts(); TaskManager::getRunningTasks().clear(); - ContentRegistry::DataProcessorNode::getEntries().clear(); + ContentRegistry::DataProcessorNode::impl::getEntries().clear(); - ContentRegistry::DataFormatter::getEntries().clear(); - ContentRegistry::FileHandler::getEntries().clear(); + ContentRegistry::DataFormatter::impl::getEntries().clear(); + ContentRegistry::FileHandler::impl::getEntries().clear(); ContentRegistry::Hashes::impl::getHashes().clear(); api::ThemeManager::reset(); @@ -396,14 +396,14 @@ namespace hex::init { bool loadSettings() { try { // Try to load settings from file - ContentRegistry::Settings::load(); + ContentRegistry::Settings::impl::load(); } catch (std::exception &e) { // If that fails, create a new settings file log::error("Failed to load configuration! {}", e.what()); - ContentRegistry::Settings::clear(); - ContentRegistry::Settings::store(); + ContentRegistry::Settings::impl::clear(); + ContentRegistry::Settings::impl::store(); return false; } @@ -413,7 +413,7 @@ namespace hex::init { bool storeSettings() { try { - ContentRegistry::Settings::store(); + ContentRegistry::Settings::impl::store(); } catch (std::exception &e) { log::error("Failed to store configuration! {}", e.what()); return false; diff --git a/main/source/window/win_window.cpp b/main/source/window/win_window.cpp index 1fab32bdc..e1311ebd2 100644 --- a/main/source/window/win_window.cpp +++ b/main/source/window/win_window.cpp @@ -418,7 +418,7 @@ namespace hex { ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImGui::GetColorU32(ImGuiCol_ScrollbarGrabActive)); ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImGui::GetColorU32(ImGuiCol_ScrollbarGrabHovered)); - auto &titleBarButtons = ContentRegistry::Interface::getTitleBarButtons(); + auto &titleBarButtons = ContentRegistry::Interface::impl::getTitleBarButtons(); // Draw custom title bar buttons ImGui::SetCursorPosX(ImGui::GetWindowWidth() - buttonSize.x * (4 + titleBarButtons.size())); @@ -446,7 +446,7 @@ namespace hex { // Draw close button if (ImGui::TitleBarButton(ICON_VS_CHROME_CLOSE, buttonSize)) { - ImHexApi::Common::closeImHex(); + ImHexApi::System::closeImHex(); } ImGui::PopStyleColor(5); diff --git a/main/source/window/window.cpp b/main/source/window/window.cpp index 2dc33ba88..17d6632fe 100644 --- a/main/source/window/window.cpp +++ b/main/source/window/window.cpp @@ -106,7 +106,7 @@ namespace hex { auto logoData = romfs::get("logo.png"); this->m_logoTexture = ImGui::Texture(reinterpret_cast(logoData.data()), logoData.size()); - ContentRegistry::Settings::store(); + ContentRegistry::Settings::impl::store(); EventManager::post(); EventManager::post(); } @@ -317,7 +317,7 @@ namespace hex { auto drawList = ImGui::GetWindowDrawList(); ImGui::PopStyleVar(); auto sidebarPos = ImGui::GetCursorPos(); - auto sidebarWidth = ContentRegistry::Interface::getSidebarItems().empty() ? 0 : 30_scaled; + auto sidebarWidth = ContentRegistry::Interface::impl::getSidebarItems().empty() ? 0 : 30_scaled; ImGui::SetCursorPosX(sidebarWidth); @@ -334,7 +334,7 @@ namespace hex { ImGui::Separator(); ImGui::SetCursorPosX(8); - for (const auto &callback : ContentRegistry::Interface::getFooterItems()) { + for (const auto &callback : ContentRegistry::Interface::impl::getFooterItems()) { auto prevIdx = drawList->_VtxCurrentIdx; callback(); auto currIdx = drawList->_VtxCurrentIdx; @@ -355,7 +355,7 @@ namespace hex { static i32 openWindow = -1; u32 index = 0; ImGui::PushID("SideBarWindows"); - for (const auto &[icon, callback] : ContentRegistry::Interface::getSidebarItems()) { + for (const auto &[icon, callback] : ContentRegistry::Interface::impl::getSidebarItems()) { ImGui::SetCursorPosY(sidebarPos.y + sidebarWidth * index); ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetColorU32(ImGuiCol_MenuBarBg)); @@ -404,13 +404,13 @@ namespace hex { ImGui::Image(this->m_logoTexture, ImVec2(menuBarHeight, menuBarHeight)); } - for (const auto &[priority, menuItem] : ContentRegistry::Interface::getMainMenuItems()) { + for (const auto &[priority, menuItem] : ContentRegistry::Interface::impl::getMainMenuItems()) { if (ImGui::BeginMenu(LangEntry(menuItem.unlocalizedName))) { ImGui::EndMenu(); } } - for (auto &[priority, menuItem] : ContentRegistry::Interface::getMenuItems()) { + for (auto &[priority, menuItem] : ContentRegistry::Interface::impl::getMenuItems()) { const auto &[unlocalizedNames, shortcut, callback, enabledCallback] = menuItem; createNestedMenu(unlocalizedNames, shortcut, callback, enabledCallback); } @@ -424,7 +424,7 @@ namespace hex { // Render toolbar if (ImGui::BeginMenuBar()) { - for (const auto &callback : ContentRegistry::Interface::getToolbarItems()) { + for (const auto &callback : ContentRegistry::Interface::impl::getToolbarItems()) { callback(); ImGui::SameLine(); } @@ -524,7 +524,7 @@ namespace hex { TaskManager::runDeferredCalls(); // Draw main menu popups - for (auto &[priority, menuItem] : ContentRegistry::Interface::getMenuItems()) { + for (auto &[priority, menuItem] : ContentRegistry::Interface::impl::getMenuItems()) { const auto &[unlocalizedNames, shortcut, callback, enabledCallback] = menuItem; if (ImGui::BeginPopup(unlocalizedNames.front().c_str())) { @@ -540,7 +540,7 @@ namespace hex { auto &io = ImGui::GetIO(); // Loop through all views and draw them - for (auto &[name, view] : ContentRegistry::Views::getEntries()) { + for (auto &[name, view] : ContentRegistry::Views::impl::getEntries()) { ImGui::GetCurrentContext()->NextWindowData.ClearFlags(); // Draw always visible views @@ -766,7 +766,7 @@ namespace hex { // Check if a custom file handler can handle the file bool handled = false; - for (const auto &[extensions, handler] : ContentRegistry::FileHandler::getEntries()) { + for (const auto &[extensions, handler] : ContentRegistry::FileHandler::impl::getEntries()) { for (const auto &extension : extensions) { if (path.extension() == extension) { // Pass the file to the handler and check if it was successful @@ -857,11 +857,11 @@ namespace hex { handler.ReadOpenFn = [](ImGuiContext *ctx, ImGuiSettingsHandler *, const char *) -> void* { return ctx; }; handler.ReadLineFn = [](ImGuiContext *, ImGuiSettingsHandler *, void *, const char *line) { - for (auto &[name, view] : ContentRegistry::Views::getEntries()) { + for (auto &[name, view] : ContentRegistry::Views::impl::getEntries()) { std::string format = view->getUnlocalizedName() + "=%d"; sscanf(line, format.c_str(), &view->getWindowOpenState()); } - for (auto &[name, function, detached] : ContentRegistry::Tools::getEntries()) { + for (auto &[name, function, detached] : ContentRegistry::Tools::impl::getEntries()) { std::string format = name + "=%d"; sscanf(line, format.c_str(), &detached); } @@ -870,10 +870,10 @@ namespace hex { handler.WriteAllFn = [](ImGuiContext *, ImGuiSettingsHandler *handler, ImGuiTextBuffer *buf) { buf->appendf("[%s][General]\n", handler->TypeName); - for (auto &[name, view] : ContentRegistry::Views::getEntries()) { + for (auto &[name, view] : ContentRegistry::Views::impl::getEntries()) { buf->appendf("%s=%d\n", name.c_str(), view->getWindowOpenState()); } - for (auto &[name, function, detached] : ContentRegistry::Tools::getEntries()) { + for (auto &[name, function, detached] : ContentRegistry::Tools::impl::getEntries()) { buf->appendf("%s=%d\n", name.c_str(), detached); } diff --git a/plugins/builtin/source/content/command_palette_commands.cpp b/plugins/builtin/source/content/command_palette_commands.cpp index bec643d10..4ad1b08a5 100644 --- a/plugins/builtin/source/content/command_palette_commands.cpp +++ b/plugins/builtin/source/content/command_palette_commands.cpp @@ -57,9 +57,9 @@ namespace hex::plugin::builtin { ContentRegistry::CommandPaletteCommands::Type::SymbolCommand, ">", [](const auto &input) { - std::vector result; + std::vector result; - for (const auto &[priority, entry] : ContentRegistry::Interface::getMenuItems()) { + for (const auto &[priority, entry] : ContentRegistry::Interface::impl::getMenuItems()) { if (!entry.enabledCallback()) continue; @@ -67,7 +67,7 @@ namespace hex::plugin::builtin { std::transform(entry.unlocalizedNames.begin(), entry.unlocalizedNames.end(), std::back_inserter(names), [](auto &name) { return LangEntry(name); }); if (auto combined = wolv::util::combineStrings(names, " -> "); hex::containsIgnoreCase(combined, input) && !combined.contains(ContentRegistry::Interface::impl::SeparatorValue) && !combined.contains(ContentRegistry::Interface::impl::SubMenuValue)) { - result.emplace_back(ContentRegistry::CommandPaletteCommands::QueryResult { + result.emplace_back(ContentRegistry::CommandPaletteCommands::impl::QueryResult { std::move(combined), [entry](const auto&) { entry.callback(); } }); diff --git a/plugins/builtin/source/content/main_menu_items.cpp b/plugins/builtin/source/content/main_menu_items.cpp index 957b50e34..cfa230b0d 100644 --- a/plugins/builtin/source/content/main_menu_items.cpp +++ b/plugins/builtin/source/content/main_menu_items.cpp @@ -297,7 +297,7 @@ namespace hex::plugin::builtin { /* Open Other */ ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.file", "hex.builtin.menu.file.open_other"}, 1150, Shortcut::None, [] { - for (const auto &unlocalizedProviderName : ContentRegistry::Provider::getEntries()) { + for (const auto &unlocalizedProviderName : ContentRegistry::Provider::impl::getEntries()) { if (ImGui::MenuItem(LangEntry(unlocalizedProviderName))) ImHexApi::Provider::createProvider(unlocalizedProviderName); } @@ -390,7 +390,7 @@ namespace hex::plugin::builtin { /* Quit ImHex */ ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.file", "hex.builtin.menu.file.quit"}, 10100, ALT + Keys::F4, [] { - ImHexApi::Common::closeImHex(); + ImHexApi::System::closeImHex(); }); } @@ -415,7 +415,7 @@ namespace hex::plugin::builtin { ContentRegistry::Interface::registerMainMenuItem("hex.builtin.menu.view", 3000); ContentRegistry::Interface::addMenuItemSubMenu({ "hex.builtin.menu.view" }, 1000, [] { - for (auto &[name, view] : ContentRegistry::Views::getEntries()) { + for (auto &[name, view] : ContentRegistry::Views::impl::getEntries()) { if (view->hasViewMenuItemEntry()) ImGui::MenuItem(LangEntry(view->getUnlocalizedName()), "", &view->getWindowOpenState()); } @@ -433,11 +433,11 @@ namespace hex::plugin::builtin { ContentRegistry::Interface::registerMainMenuItem("hex.builtin.menu.layout", 4000); ContentRegistry::Interface::addMenuItemSubMenu({ "hex.builtin.menu.layout" }, 1000, [] { - for (auto &[layoutName, func] : ContentRegistry::Interface::getLayouts()) { + for (auto &[layoutName, func] : ContentRegistry::Interface::impl::getLayouts()) { if (ImGui::MenuItem(LangEntry(layoutName), "", false, ImHexApi::Provider::isValid())) { auto dock = ImHexApi::System::getMainDockSpaceId(); - for (auto &[viewName, view] : ContentRegistry::Views::getEntries()) { + for (auto &[viewName, view] : ContentRegistry::Views::impl::getEntries()) { view->getWindowOpenState() = false; } diff --git a/plugins/builtin/source/content/settings_entries.cpp b/plugins/builtin/source/content/settings_entries.cpp index e853b44ee..ba262c927 100644 --- a/plugins/builtin/source/content/settings_entries.cpp +++ b/plugins/builtin/source/content/settings_entries.cpp @@ -22,13 +22,13 @@ namespace { std::vector userFolders; - void loadUserFoldersFromSetting(nlohmann::json &setting) { + void loadUserFoldersFromSetting(const std::vector &paths) { userFolders.clear(); - std::vector paths = setting; for (const auto &path : paths) { - // JSON reads char8_t as array, char8_t is not supported as of now - std::u8string_view uString(reinterpret_cast(&path.front()), reinterpret_cast(std::next(&path.back()))); - userFolders.emplace_back(uString); + userFolders.emplace_back( + reinterpret_cast(path.data()), + reinterpret_cast(path.data() + path.size()) + ); } } @@ -158,7 +158,7 @@ namespace hex::plugin::builtin { if (ImGui::Combo(name.data(), &selection, scaling, IM_ARRAYSIZE(scaling))) { setting = selection; - ImHexApi::Common::restartImHex(); + ImHexApi::System::restartImHex(); return true; } @@ -618,9 +618,9 @@ namespace hex::plugin::builtin { } static void loadFoldersSettings() { - static const std::string dirsSetting { "hex.builtin.setting.folders" }; - auto dirs = ContentRegistry::Settings::getSetting(dirsSetting, dirsSetting); - loadUserFoldersFromSetting(dirs); + auto directories = ContentRegistry::Settings::read("hex.builtin.setting.folders", "hex.builtin.setting.folders", std::vector { }); + + loadUserFoldersFromSetting(directories); ImHexApi::System::setAdditionalFolderPaths(userFolders); } diff --git a/plugins/builtin/source/content/tools_entries.cpp b/plugins/builtin/source/content/tools_entries.cpp index dc52a5104..5830288f6 100644 --- a/plugins/builtin/source/content/tools_entries.cpp +++ b/plugins/builtin/source/content/tools_entries.cpp @@ -658,8 +658,8 @@ namespace hex::plugin::builtin { } std::string getWikipediaApiUrl() { - auto setting = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.wiki_explain_language"); - return "https://" + std::string(setting) + ".wikipedia.org/w/api.php?format=json&action=query&prop=extracts&explaintext&redirects=10&formatversion=2"; + auto setting = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.wiki_explain_language", "en"); + return "https://" + setting + ".wikipedia.org/w/api.php?format=json&action=query&prop=extracts&explaintext&redirects=10&formatversion=2"; } void drawWikiExplainer() { diff --git a/plugins/builtin/source/content/ui_items.cpp b/plugins/builtin/source/content/ui_items.cpp index 6ef5686d0..7a18a3427 100644 --- a/plugins/builtin/source/content/ui_items.cpp +++ b/plugins/builtin/source/content/ui_items.cpp @@ -32,7 +32,7 @@ namespace hex::plugin::builtin { View::confirmButtons("hex.builtin.common.yes"_lang, "hex.builtin.common.no"_lang, [] { ImHexApi::Provider::resetDirty(); - ImHexApi::Common::closeImHex(); + ImHexApi::System::closeImHex(); }, [] { ImGui::CloseCurrentPopup(); } ); @@ -73,7 +73,7 @@ namespace hex::plugin::builtin { if (TaskManager::getRunningTaskCount() == 0 && TaskManager::getRunningBackgroundTaskCount() == 0) { ImGui::CloseCurrentPopup(); - ImHexApi::Common::closeImHex(); + ImHexApi::System::closeImHex(); } ImGui::EndPopup(); @@ -114,7 +114,7 @@ namespace hex::plugin::builtin { ImGui::NewLine(); ImGui::Separator(); if (ImGui::Button("hex.builtin.common.okay"_lang) || ImGui::IsKeyDown(ImGuiKey_Escape)) { - ImHexApi::Common::closeImHex(); + ImHexApi::System::closeImHex(); ImGui::CloseCurrentPopup(); } diff --git a/plugins/builtin/source/content/views/view_command_palette.cpp b/plugins/builtin/source/content/views/view_command_palette.cpp index 0049c5954..a6885ecb9 100644 --- a/plugins/builtin/source/content/views/view_command_palette.cpp +++ b/plugins/builtin/source/content/views/view_command_palette.cpp @@ -108,7 +108,7 @@ namespace hex::plugin::builtin { std::vector results; - for (const auto &[type, command, unlocalizedDescription, displayCallback, executeCallback] : ContentRegistry::CommandPaletteCommands::getEntries()) { + for (const auto &[type, command, unlocalizedDescription, displayCallback, executeCallback] : ContentRegistry::CommandPaletteCommands::impl::getEntries()) { auto AutoComplete = [this, currCommand = command](auto) { this->focusInputTextBox(); @@ -137,7 +137,7 @@ namespace hex::plugin::builtin { } } - for (const auto &handler : ContentRegistry::CommandPaletteCommands::getHandlers()) { + for (const auto &handler : ContentRegistry::CommandPaletteCommands::impl::getHandlers()) { const auto &[type, command, queryCallback, displayCallback] = handler; auto processedInput = input; diff --git a/plugins/builtin/source/content/views/view_data_inspector.cpp b/plugins/builtin/source/content/views/view_data_inspector.cpp index a4cbbbea9..714b76d1b 100644 --- a/plugins/builtin/source/content/views/view_data_inspector.cpp +++ b/plugins/builtin/source/content/views/view_data_inspector.cpp @@ -59,7 +59,7 @@ namespace hex::plugin::builtin { return; // Decode bytes using registered inspectors - for (auto &entry : ContentRegistry::DataInspector::getEntries()) { + for (auto &entry : ContentRegistry::DataInspector::impl::getEntries()) { if (validBytes < entry.requiredSize) continue; diff --git a/plugins/builtin/source/content/views/view_data_processor.cpp b/plugins/builtin/source/content/views/view_data_processor.cpp index 378f8e6ff..820131fa7 100644 --- a/plugins/builtin/source/content/views/view_data_processor.cpp +++ b/plugins/builtin/source/content/views/view_data_processor.cpp @@ -264,7 +264,7 @@ namespace hex::plugin::builtin { } } - for (const auto &[unlocalizedCategory, unlocalizedName, function] : ContentRegistry::DataProcessorNode::getEntries()) { + for (const auto &[unlocalizedCategory, unlocalizedName, function] : ContentRegistry::DataProcessorNode::impl::getEntries()) { if (unlocalizedCategory.empty() && unlocalizedName.empty()) { ImGui::Separator(); } else if (unlocalizedCategory.empty()) { @@ -617,7 +617,7 @@ namespace hex::plugin::builtin { std::unique_ptr ViewDataProcessor::loadNode(const nlohmann::json &node) { try { - auto &nodeEntries = ContentRegistry::DataProcessorNode::getEntries(); + auto &nodeEntries = ContentRegistry::DataProcessorNode::impl::getEntries(); std::unique_ptr newNode; for (auto &entry : nodeEntries) { diff --git a/plugins/builtin/source/content/views/view_hex_editor.cpp b/plugins/builtin/source/content/views/view_hex_editor.cpp index 9a158a7dd..afe9acb68 100644 --- a/plugins/builtin/source/content/views/view_hex_editor.cpp +++ b/plugins/builtin/source/content/views/view_hex_editor.cpp @@ -1083,7 +1083,7 @@ namespace hex::plugin::builtin { auto selection = ImHexApi::HexEditor::getSelection(); auto provider = ImHexApi::Provider::get(); - for (const auto &[unlocalizedName, callback] : ContentRegistry::DataFormatter::getEntries()) { + for (const auto &[unlocalizedName, callback] : ContentRegistry::DataFormatter::impl::getEntries()) { if (ImGui::MenuItem(LangEntry(unlocalizedName))) { ImGui::SetClipboardText( callback( diff --git a/plugins/builtin/source/content/views/view_pattern_data.cpp b/plugins/builtin/source/content/views/view_pattern_data.cpp index b7747ae64..0fdcaf4ab 100644 --- a/plugins/builtin/source/content/views/view_pattern_data.cpp +++ b/plugins/builtin/source/content/views/view_pattern_data.cpp @@ -12,10 +12,8 @@ namespace hex::plugin::builtin { ViewPatternData::ViewPatternData() : View("hex.builtin.view.pattern_data.name") { EventManager::subscribe(this, [this]() { - auto patternStyle = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.pattern_tree_style"); - - if (patternStyle.is_number()) - this->m_patternDrawer.setTreeStyle(static_cast(patternStyle.get())); + auto patternStyle = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.pattern_tree_style", 0); + this->m_patternDrawer.setTreeStyle(static_cast(patternStyle)); }); EventManager::subscribe(this, [this](auto, auto) { diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index ff001ba11..078f05dad 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -753,19 +753,8 @@ namespace hex::plugin::builtin { }); EventManager::subscribe(this, [this] { - { - auto syncPatternSource = ContentRegistry::Settings::getSetting("hex.builtin.setting.general", "hex.builtin.setting.general.sync_pattern_source"); - - if (syncPatternSource.is_number()) - this->m_syncPatternSourceCode = static_cast(syncPatternSource); - } - - { - auto autoLoadPatterns = ContentRegistry::Settings::getSetting("hex.builtin.setting.general", "hex.builtin.setting.general.auto_load_patterns"); - - if (autoLoadPatterns.is_number()) - this->m_autoLoadPatterns = static_cast(autoLoadPatterns); - } + this->m_syncPatternSourceCode = ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.sync_pattern_source", 0); + this->m_autoLoadPatterns = ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.auto_load_patterns", 1); }); EventManager::subscribe(this, [this](prv::Provider *provider) { diff --git a/plugins/builtin/source/content/views/view_settings.cpp b/plugins/builtin/source/content/views/view_settings.cpp index 67b0f3fc9..9d81c1693 100644 --- a/plugins/builtin/source/content/views/view_settings.cpp +++ b/plugins/builtin/source/content/views/view_settings.cpp @@ -34,7 +34,7 @@ namespace hex::plugin::builtin { if (ImGui::BeginPopupModal(View::toWindowName("hex.builtin.view.settings.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_NoResize)) { if (ImGui::BeginTabBar("settings")) { - auto &entries = ContentRegistry::Settings::getEntries(); + auto &entries = ContentRegistry::Settings::impl::getEntries(); std::vector::const_iterator> sortedCategories; @@ -46,7 +46,7 @@ namespace hex::plugin::builtin { return item0->first.slot < item1->first.slot; }); - const auto &descriptions = ContentRegistry::Settings::getCategoryDescriptions(); + const auto &descriptions = ContentRegistry::Settings::impl::getCategoryDescriptions(); for (auto &it : sortedCategories) { auto &[category, settings] = *it; @@ -59,7 +59,7 @@ namespace hex::plugin::builtin { ImGui::Separator(); for (auto &[name, requiresRestart, callback] : settings) { - auto &setting = ContentRegistry::Settings::getSettingsData()[category.name][name]; + auto &setting = ContentRegistry::Settings::impl::getSettingsData()[category.name][name]; if (callback(LangEntry(name), setting)) { log::debug("Setting [{}]: {} was changed to {}", category.name, name, [&] -> std::string{ if (setting.is_number()) @@ -87,7 +87,7 @@ namespace hex::plugin::builtin { this->getWindowOpenState() = false; if (!this->getWindowOpenState() && this->m_restartRequested) { - View::showYesNoQuestionPopup("hex.builtin.view.settings.restart_question"_lang, ImHexApi::Common::restartImHex, [] {}); + View::showYesNoQuestionPopup("hex.builtin.view.settings.restart_question"_lang, ImHexApi::System::restartImHex, [] {}); } } diff --git a/plugins/builtin/source/content/views/view_tools.cpp b/plugins/builtin/source/content/views/view_tools.cpp index 0caa8caa5..d8c4ae180 100644 --- a/plugins/builtin/source/content/views/view_tools.cpp +++ b/plugins/builtin/source/content/views/view_tools.cpp @@ -8,7 +8,7 @@ namespace hex::plugin::builtin { ViewTools::ViewTools() : View("hex.builtin.view.tools.name") { } void ViewTools::drawContent() { - auto &tools = ContentRegistry::Tools::getEntries(); + auto &tools = ContentRegistry::Tools::impl::getEntries(); if (ImGui::Begin(View::toWindowName("hex.builtin.view.tools.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) { for (auto iter = tools.begin(); iter != tools.end(); iter++) { diff --git a/plugins/builtin/source/content/welcome_screen.cpp b/plugins/builtin/source/content/welcome_screen.cpp index e6af9f402..cc2774541 100644 --- a/plugins/builtin/source/content/welcome_screen.cpp +++ b/plugins/builtin/source/content/welcome_screen.cpp @@ -136,10 +136,10 @@ namespace hex::plugin::builtin { } static void loadDefaultLayout() { - auto layouts = ContentRegistry::Interface::getLayouts(); + auto layouts = ContentRegistry::Interface::impl::getLayouts(); if (!layouts.empty()) { - for (auto &[viewName, view] : ContentRegistry::Views::getEntries()) { + for (auto &[viewName, view] : ContentRegistry::Views::impl::getEntries()) { view->getWindowOpenState() = false; } @@ -153,7 +153,7 @@ namespace hex::plugin::builtin { } static bool isAnyViewOpen() { - const auto &views = ContentRegistry::Views::getEntries(); + const auto &views = ContentRegistry::Views::impl::getEntries(); return std::any_of(views.begin(), views.end(), [](const std::pair &entry) { return entry.second->getWindowOpenState(); @@ -252,7 +252,7 @@ namespace hex::plugin::builtin { ImGui::SetNextWindowPos(ImGui::GetWindowPos() + ImGui::GetCursorPos()); if (ImGui::BeginPopup("hex.builtin.welcome.start.popup.open_other"_lang)) { - for (const auto &unlocalizedProviderName : ContentRegistry::Provider::getEntries()) { + for (const auto &unlocalizedProviderName : ContentRegistry::Provider::impl::getEntries()) { if (ImGui::Hyperlink(LangEntry(unlocalizedProviderName))) { ImHexApi::Provider::createProvider(unlocalizedProviderName); ImGui::CloseCurrentPopup(); @@ -365,7 +365,7 @@ namespace hex::plugin::builtin { hex::openWebpage("hex.builtin.welcome.learn.plugins.link"_lang); } - auto extraWelcomeScreenEntries = ContentRegistry::Interface::getWelcomeScreenEntries(); + auto extraWelcomeScreenEntries = ContentRegistry::Interface::impl::getWelcomeScreenEntries(); if (!extraWelcomeScreenEntries.empty()) { ImGui::TableNextRow(ImGuiTableRowFlags_None, ImGui::GetTextLineHeightWithSpacing() * 5); ImGui::TableNextColumn(); @@ -442,36 +442,28 @@ namespace hex::plugin::builtin { (void)EventManager::subscribe([]() { { - auto theme = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.color"); + auto theme = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.color", api::ThemeManager::NativeTheme); - if (theme.is_string()) { - if (theme != api::ThemeManager::NativeTheme) { - static std::string lastTheme; + if (theme != api::ThemeManager::NativeTheme) { + static std::string lastTheme; - if (const auto thisTheme = theme.get(); thisTheme != lastTheme) { - EventManager::post(thisTheme); - lastTheme = thisTheme; - } + if (theme != lastTheme) { + EventManager::post(theme); + lastTheme = theme; } } } { - auto language = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.language"); + auto language = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.language", "en-US"); - if (language.is_string()) { - LangEntry::loadLanguage(static_cast(language)); - } else { - // If no language is specified, fall back to English. - LangEntry::loadLanguage("en-US"); - } + LangEntry::loadLanguage(language); } { - auto targetFps = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.fps"); + auto targetFps = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.fps", 60); - if (targetFps.is_number()) - ImHexApi::System::setTargetFPS(targetFps); + ImHexApi::System::setTargetFPS(targetFps); } }); diff --git a/plugins/builtin/source/ui/hex_editor.cpp b/plugins/builtin/source/ui/hex_editor.cpp index 55526c486..df49ed81a 100644 --- a/plugins/builtin/source/ui/hex_editor.cpp +++ b/plugins/builtin/source/ui/hex_editor.cpp @@ -91,72 +91,27 @@ namespace hex::plugin::builtin::ui { EventManager::subscribe(this, [this] { { - auto bytesPerRow = ContentRegistry::Settings::getSetting("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.bytes_per_row"); - - if (bytesPerRow.is_number()) { - this->m_bytesPerRow = static_cast(bytesPerRow); - this->m_encodingLineStartAddresses.clear(); - } - } - - { - auto ascii = ContentRegistry::Settings::getSetting("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.ascii"); - - if (ascii.is_number()) - this->m_showAscii = static_cast(ascii); - } - - { - auto greyOutZeros = ContentRegistry::Settings::getSetting("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.grey_zeros"); - - if (greyOutZeros.is_number()) - this->m_grayOutZero = static_cast(greyOutZeros); - } - - { - auto upperCaseHex = ContentRegistry::Settings::getSetting("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.uppercase_hex"); - - if (upperCaseHex.is_number()) - this->m_upperCaseHex = static_cast(upperCaseHex); - } - - { - auto selectionColor = ContentRegistry::Settings::getSetting("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.highlight_color"); - - if (selectionColor.is_number()) - this->m_selectionColor = static_cast(selectionColor); + this->m_bytesPerRow = ContentRegistry::Settings::read("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.bytes_per_row", 16); + this->m_encodingLineStartAddresses.clear(); } { auto &visualizers = ContentRegistry::HexEditor::impl::getVisualizers(); - auto selectedVisualizer = ContentRegistry::Settings::getSetting("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.visualizer"); + auto selectedVisualizer = ContentRegistry::Settings::read("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.visualizer", "hex.builtin.visualizer.hexadecimal.8bit"); - if (selectedVisualizer.is_string() && visualizers.contains(selectedVisualizer)) + if (visualizers.contains(selectedVisualizer)) this->m_currDataVisualizer = visualizers[selectedVisualizer]; else this->m_currDataVisualizer = visualizers["hex.builtin.visualizer.hexadecimal.8bit"]; } - { - auto syncScrolling = ContentRegistry::Settings::getSetting("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.sync_scrolling"); - - if (syncScrolling.is_number()) - this->m_syncScrolling = static_cast(syncScrolling); - } - - { - auto padding = ContentRegistry::Settings::getSetting("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.byte_padding"); - - if (padding.is_number()) - this->m_byteCellPadding = static_cast(padding); - } - - { - auto padding = ContentRegistry::Settings::getSetting("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.char_padding"); - - if (padding.is_number()) - this->m_characterCellPadding = static_cast(padding); - } + this->m_showAscii = ContentRegistry::Settings::read("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.ascii", 1); + this->m_grayOutZero = ContentRegistry::Settings::read("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.grey_zeros", 1); + this->m_upperCaseHex = ContentRegistry::Settings::read("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.uppercase_hex", 1); + this->m_selectionColor = ContentRegistry::Settings::read("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.highlight_color", 0x60C08080); + this->m_syncScrolling = ContentRegistry::Settings::read("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.sync_scrolling", 0); + this->m_byteCellPadding = ContentRegistry::Settings::read("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.byte_padding", 0); + this->m_characterCellPadding = ContentRegistry::Settings::read("hex.builtin.setting.hex_editor", "hex.builtin.setting.hex_editor.char_padding", 0); }); } diff --git a/plugins/windows/source/plugin_windows.cpp b/plugins/windows/source/plugin_windows.cpp index 58ef215e6..68f04c3af 100644 --- a/plugins/windows/source/plugin_windows.cpp +++ b/plugins/windows/source/plugin_windows.cpp @@ -22,7 +22,7 @@ namespace hex::plugin::windows { static void detectSystemTheme() { // Setup system theme change detector EventManager::subscribe([] { - bool themeFollowSystem = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.color").get() == api::ThemeManager::NativeTheme; + bool themeFollowSystem = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.color", api::ThemeManager::NativeTheme) == api::ThemeManager::NativeTheme; if (!themeFollowSystem) return; @@ -43,7 +43,7 @@ static void detectSystemTheme() { }); EventManager::subscribe([=] { - bool themeFollowSystem = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.color").get() == api::ThemeManager::NativeTheme; + bool themeFollowSystem = ContentRegistry::Settings::read("hex.builtin.setting.interface", "hex.builtin.setting.interface.color", api::ThemeManager::NativeTheme) == api::ThemeManager::NativeTheme; if (themeFollowSystem) EventManager::post();