diff --git a/plugins/builtin/include/content/providers/disk_provider.hpp b/plugins/builtin/include/content/providers/disk_provider.hpp index c5113c651..66f4f9eb9 100644 --- a/plugins/builtin/include/content/providers/disk_provider.hpp +++ b/plugins/builtin/include/content/providers/disk_provider.hpp @@ -2,8 +2,6 @@ #include -#include "content/providers/file_provider.hpp" - #include #include #include diff --git a/plugins/builtin/include/content/views/view_data_inspector.hpp b/plugins/builtin/include/content/views/view_data_inspector.hpp index 921b9ce8d..8d77d390d 100644 --- a/plugins/builtin/include/content/views/view_data_inspector.hpp +++ b/plugins/builtin/include/content/views/view_data_inspector.hpp @@ -23,7 +23,7 @@ namespace hex::plugin::builtin { private: struct InspectorCacheEntry { std::string unlocalizedName; - ContentRegistry::DataInspector::DisplayFunction displayFunction; + ContentRegistry::DataInspector::impl::DisplayFunction displayFunction; }; bool m_shouldInvalidate = true; diff --git a/plugins/builtin/source/content/providers.cpp b/plugins/builtin/source/content/providers.cpp index eb70d6aca..973e25b61 100644 --- a/plugins/builtin/source/content/providers.cpp +++ b/plugins/builtin/source/content/providers.cpp @@ -1,5 +1,4 @@ #include -#include #include "content/providers/gdb_provider.hpp" #include "content/providers/file_provider.hpp" @@ -8,41 +7,11 @@ namespace hex::plugin::builtin { void registerProviders() { - ContentRegistry::Provider::add("hex.builtin.provider.gdb"); - ContentRegistry::Provider::add("hex.builtin.provider.disk"); - (void) EventManager::subscribe([](const std::string &unlocalizedName, hex::prv::Provider **provider){ - if (unlocalizedName != "hex.builtin.provider.file") return; + ContentRegistry::Provider::add("hex.builtin.provider.file", false); + ContentRegistry::Provider::add("hex.builtin.provider.gdb"); + ContentRegistry::Provider::add("hex.builtin.provider.disk"); - auto newProvider = new prv::FileProvider(); - - hex::ImHexApi::Provider::add(newProvider); - - if (provider != nullptr) - *provider = newProvider; - }); - - (void) EventManager::subscribe([](const std::string &unlocalizedName, hex::prv::Provider **provider){ - if (unlocalizedName != "hex.builtin.provider.gdb") return; - - auto newProvider = new prv::GDBProvider(); - - hex::ImHexApi::Provider::add(newProvider); - - if (provider != nullptr) - *provider = newProvider; - }); - - (void) EventManager::subscribe([](const std::string &unlocalizedName, hex::prv::Provider **provider){ - if (unlocalizedName != "hex.builtin.provider.disk") return; - - auto newProvider = new prv::DiskProvider(); - - hex::ImHexApi::Provider::add(newProvider); - - if (provider != nullptr) - *provider = newProvider; - }); } } \ No newline at end of file diff --git a/plugins/libimhex/include/hex/api/content_registry.hpp b/plugins/libimhex/include/hex/api/content_registry.hpp index 308ab054f..96f3b5bb5 100644 --- a/plugins/libimhex/include/hex/api/content_registry.hpp +++ b/plugins/libimhex/include/hex/api/content_registry.hpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include #include @@ -100,11 +102,17 @@ namespace hex { /* View Registry. Allows adding of new windows */ namespace Views { - void add(View *view); + + namespace impl { + + void add(View *view); + + } + template T, typename ... Args> void add(Args&& ... args) { - return add(new T(std::forward(args)...)); + return impl::add(new T(std::forward(args)...)); } std::vector& getEntries(); @@ -113,16 +121,21 @@ namespace hex { /* Tools Registry. Allows adding new entries to the tools window */ namespace Tools { - using Callback = std::function; - struct Entry { - std::string name; - Callback function; - }; + namespace impl { - void add(const std::string &unlocalizedName, const Callback &function); + using Callback = std::function; - std::vector& getEntries(); + struct Entry { + std::string name; + Callback function; + }; + + } + + 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 */ @@ -134,35 +147,45 @@ namespace hex { Octal }; - using DisplayFunction = std::function; - using GeneratorFunction = std::function&, std::endian, NumberDisplayStyle)>; + namespace impl { - struct Entry { - std::string unlocalizedName; - size_t requiredSize; - GeneratorFunction generatorFunction; - }; + using DisplayFunction = std::function; + using GeneratorFunction = std::function&, std::endian, NumberDisplayStyle)>; - void add(const std::string &unlocalizedName, size_t requiredSize, GeneratorFunction function); + struct Entry { + std::string unlocalizedName; + size_t requiredSize; + impl::GeneratorFunction generatorFunction; + }; - std::vector& getEntries(); + } + + void add(const std::string &unlocalizedName, size_t requiredSize, impl::GeneratorFunction function); + + std::vector& getEntries(); } /* Data Processor Node Registry. Allows adding new processor nodes to be used in the data processor */ namespace DataProcessorNode { - using CreatorFunction = std::function; - struct Entry { - std::string category; - std::string name; - CreatorFunction creatorFunction; - }; + namespace impl { + + using CreatorFunction = std::function; + + struct Entry { + std::string category; + std::string name; + CreatorFunction creatorFunction; + }; + + void add(const Entry &entry); + + } - void add(const Entry &entry); template T, typename ... Args> void add(const std::string &unlocalizedCategory, const std::string &unlocalizedName, Args&& ... args) { - add(Entry{ unlocalizedCategory.c_str(), unlocalizedName.c_str(), + add(impl::Entry{ unlocalizedCategory.c_str(), unlocalizedName.c_str(), [=]{ auto node = new T(std::forward(args)...); node->setUnlocalizedName(unlocalizedName); @@ -173,7 +196,7 @@ namespace hex { void addSeparator(); - std::vector& getEntries(); + std::vector& getEntries(); } @@ -202,7 +225,28 @@ namespace hex { /* Provider Registry. Allows adding new data providers to be created from the UI */ namespace Provider { - void add(const std::string &unlocalizedName); + namespace impl { + + void addProviderName(const std::string &unlocalizedName); + + } + + template T> + void add(const std::string &unlocalizedName, bool addToList = true) { + (void) EventManager::subscribe([expectedName = unlocalizedName](const std::string &name, hex::prv::Provider **provider){ + if (name != expectedName) return; + + auto newProvider = new T(); + + hex::ImHexApi::Provider::add(newProvider); + + if (provider != nullptr) + *provider = newProvider; + }); + + if (addToList) + impl::addProviderName(unlocalizedName); + } const std::vector& getEntries(); diff --git a/plugins/libimhex/include/hex/helpers/shared_data.hpp b/plugins/libimhex/include/hex/helpers/shared_data.hpp index ccf7a6012..a81a27586 100644 --- a/plugins/libimhex/include/hex/helpers/shared_data.hpp +++ b/plugins/libimhex/include/hex/helpers/shared_data.hpp @@ -62,8 +62,8 @@ namespace hex { static std::vector commandPaletteCommands; static std::map patternLanguageFunctions; static std::vector views; - static std::vector toolsEntries; - static std::vector dataInspectorEntries; + static std::vector toolsEntries; + static std::vector dataInspectorEntries; static u32 patternPaletteOffset; static std::string popupMessage; static std::list bookmarkEntries; @@ -79,7 +79,7 @@ namespace hex { static std::vector providerNames; - static std::vector dataProcessorNodes; + static std::vector dataProcessorNodes; static u32 dataProcessorNodeIdCounter; static u32 dataProcessorLinkIdCounter; static u32 dataProcessorAttrIdCounter; diff --git a/plugins/libimhex/source/api/content_registry.cpp b/plugins/libimhex/source/api/content_registry.cpp index 53a11ab47..ce6c045ff 100644 --- a/plugins/libimhex/source/api/content_registry.cpp +++ b/plugins/libimhex/source/api/content_registry.cpp @@ -184,7 +184,7 @@ namespace hex { /* Views */ - void ContentRegistry::Views::add(View *view) { + void ContentRegistry::Views::impl::add(View *view) { getEntries().emplace_back(view); } @@ -196,27 +196,27 @@ namespace hex { /* Tools */ void ContentRegistry::Tools:: add(const std::string &unlocalizedName, const std::function &function) { - getEntries().emplace_back(Entry{ unlocalizedName, function }); + getEntries().emplace_back(impl::Entry{ unlocalizedName, function }); } - std::vector& ContentRegistry::Tools::getEntries() { + std::vector& ContentRegistry::Tools::getEntries() { return SharedData::toolsEntries; } /* Data Inspector */ - void ContentRegistry::DataInspector::add(const std::string &unlocalizedName, size_t requiredSize, ContentRegistry::DataInspector::GeneratorFunction function) { + void ContentRegistry::DataInspector::add(const std::string &unlocalizedName, size_t requiredSize, ContentRegistry::DataInspector::impl::GeneratorFunction function) { getEntries().push_back({ unlocalizedName, requiredSize, std::move(function) }); } - std::vector& ContentRegistry::DataInspector::getEntries() { + std::vector& ContentRegistry::DataInspector::getEntries() { return SharedData::dataInspectorEntries; } /* Data Processor Nodes */ - void ContentRegistry::DataProcessorNode::add(const Entry &entry) { + void ContentRegistry::DataProcessorNode::impl::add(const impl::Entry &entry) { getEntries().push_back(entry); } @@ -224,7 +224,7 @@ namespace hex { getEntries().push_back({ "", "", []{ return nullptr; } }); } - std::vector& ContentRegistry::DataProcessorNode::getEntries() { + std::vector& ContentRegistry::DataProcessorNode::getEntries() { return SharedData::dataProcessorNodes; } @@ -273,7 +273,7 @@ namespace hex { /* Providers */ - void ContentRegistry::Provider::add(const std::string &unlocalizedName) { + void ContentRegistry::Provider::impl::addProviderName(const std::string &unlocalizedName) { SharedData::providerNames.push_back(unlocalizedName); } diff --git a/plugins/libimhex/source/helpers/shared_data.cpp b/plugins/libimhex/source/helpers/shared_data.cpp index 2610c60c3..7ba384235 100644 --- a/plugins/libimhex/source/helpers/shared_data.cpp +++ b/plugins/libimhex/source/helpers/shared_data.cpp @@ -14,8 +14,8 @@ namespace hex { std::vector SharedData::commandPaletteCommands; std::map SharedData::patternLanguageFunctions; std::vector SharedData::views; - std::vector SharedData::toolsEntries; - std::vector SharedData::dataInspectorEntries; + std::vector SharedData::toolsEntries; + std::vector SharedData::dataInspectorEntries; u32 SharedData::patternPaletteOffset; std::string SharedData::popupMessage; std::list SharedData::bookmarkEntries; @@ -31,7 +31,7 @@ namespace hex { std::vector SharedData::providerNames; - std::vector SharedData::dataProcessorNodes; + std::vector SharedData::dataProcessorNodes; u32 SharedData::dataProcessorNodeIdCounter = 1; u32 SharedData::dataProcessorLinkIdCounter = 1; u32 SharedData::dataProcessorAttrIdCounter = 1;