From e27993aff2a6d9ee0cf2e907a02872539675299b Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 11 Jan 2021 21:11:03 +0100 Subject: [PATCH] Added custom event registry --- .../include/helpers/content_registry.hpp | 4 ++ plugins/libimhex/include/helpers/event.hpp | 10 ++++- .../libimhex/include/helpers/shared_data.hpp | 45 +++++++++++-------- .../source/helpers/content_registry.cpp | 15 +++++++ 4 files changed, 54 insertions(+), 20 deletions(-) diff --git a/plugins/libimhex/include/helpers/content_registry.hpp b/plugins/libimhex/include/helpers/content_registry.hpp index f8c443354..585e3e48e 100644 --- a/plugins/libimhex/include/helpers/content_registry.hpp +++ b/plugins/libimhex/include/helpers/content_registry.hpp @@ -33,6 +33,10 @@ namespace hex { static std::map>& getEntries(); static nlohmann::json& getSettingsData(); }; + + struct Events { + static auto get(std::string_view name); + }; }; } \ No newline at end of file diff --git a/plugins/libimhex/include/helpers/event.hpp b/plugins/libimhex/include/helpers/event.hpp index 528f7a07d..0d6d92d9b 100644 --- a/plugins/libimhex/include/helpers/event.hpp +++ b/plugins/libimhex/include/helpers/event.hpp @@ -1,11 +1,13 @@ #pragma once +#include + #include #include namespace hex { - enum class Events { + enum class Events : u32 { FileLoaded, DataChanged, PatternChanged, @@ -21,7 +23,11 @@ namespace hex { ProjectFileStore, ProjectFileLoad, - SettingsChanged + SettingsChanged, + + + /* This is not a real event but a flag to show all events after this one are plugin ones */ + Events_BuiltinEnd }; struct EventHandler { diff --git a/plugins/libimhex/include/helpers/shared_data.hpp b/plugins/libimhex/include/helpers/shared_data.hpp index a7fcd2061..cae4b1289 100644 --- a/plugins/libimhex/include/helpers/shared_data.hpp +++ b/plugins/libimhex/include/helpers/shared_data.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -55,30 +56,36 @@ namespace hex { static std::map sharedVariablesStorage; static ImVec2 windowPosStorage, windowSizeStorage; static nlohmann::json settingsJsonStorage; + static std::map customEventsStorage; + static u32 customEventsLastIdStorage = u32(Events::Events_BuiltinEnd) + 1; - this->imguiContext = ImGui::GetCurrentContext(); - this->eventHandlers = &eventHandlersStorage; - this->deferredCalls = &deferredCallsStorage; - this->currentProvider = ¤tProviderStorage; - this->settingsEntries = &settingsEntriesStorage; - this->sharedVariables = &sharedVariablesStorage; - this->settingsJson = &settingsJsonStorage; + this->imguiContext = ImGui::GetCurrentContext(); + this->eventHandlers = &eventHandlersStorage; + this->deferredCalls = &deferredCallsStorage; + this->currentProvider = ¤tProviderStorage; + this->settingsEntries = &settingsEntriesStorage; + this->sharedVariables = &sharedVariablesStorage; + this->settingsJson = &settingsJsonStorage; + this->customEvents = &customEventsStorage; + this->customEventsLastId = &customEventsLastIdStorage; - this->windowPos = &windowPosStorage; - this->windowSize = &windowSizeStorage; + this->windowPos = &windowPosStorage; + this->windowSize = &windowSizeStorage; } void initializeData(const SharedData &other) { - this->imguiContext = other.imguiContext; - this->eventHandlers = other.eventHandlers; - this->deferredCalls = other.deferredCalls; - this->currentProvider = other.currentProvider; - this->settingsEntries = other.settingsEntries; - this->sharedVariables = other.sharedVariables; - this->settingsJson = other.settingsJson; + this->imguiContext = other.imguiContext; + this->eventHandlers = other.eventHandlers; + this->deferredCalls = other.deferredCalls; + this->currentProvider = other.currentProvider; + this->settingsEntries = other.settingsEntries; + this->sharedVariables = other.sharedVariables; + this->settingsJson = other.settingsJson; + this->customEvents = other.customEvents; + this->customEventsLastId = other.customEventsLastId; - this->windowPos = other.windowPos; - this->windowSize = other.windowSize; + this->windowPos = other.windowPos; + this->windowSize = other.windowSize; } public: @@ -88,6 +95,8 @@ namespace hex { prv::Provider **currentProvider; std::map> *settingsEntries; nlohmann::json *settingsJson; + std::map *customEvents; + u32 *customEventsLastId; ImVec2 *windowPos; ImVec2 *windowSize; diff --git a/plugins/libimhex/source/helpers/content_registry.cpp b/plugins/libimhex/source/helpers/content_registry.cpp index a0a23ada0..868034e57 100644 --- a/plugins/libimhex/source/helpers/content_registry.cpp +++ b/plugins/libimhex/source/helpers/content_registry.cpp @@ -47,4 +47,19 @@ namespace hex { return *SharedData::get().settingsJson; } + + /* Events */ + + auto ContentRegistry::Events::get(std::string_view name) { + auto &customEvents = *SharedData::get().customEvents; + auto &lastId = *SharedData::get().customEventsLastId; + + if (!customEvents.contains(name.data())) { + customEvents[name.data()] = static_cast(lastId); + lastId++; + } + + return customEvents[name.data()]; + } + } \ No newline at end of file