diff --git a/include/helpers/plugin_handler.hpp b/include/helpers/plugin_handler.hpp index 730e14bef..ac24aba95 100644 --- a/include/helpers/plugin_handler.hpp +++ b/include/helpers/plugin_handler.hpp @@ -14,12 +14,12 @@ namespace hex { Plugin(std::string_view path); ~Plugin(); - void initializePlugin(ImGuiContext *ctx, prv::Provider **provider) const; + void initializePlugin(SharedData &sharedData) const; View* createView() const; void drawToolsEntry() const; private: - using InitializePluginFunc = void(*)(ImGuiContext*, hex::prv::Provider**); + using InitializePluginFunc = void(*)(SharedData &sharedData); using CreateViewFunc = View*(*)(); using DrawToolsEntryFunc = void(*)(); diff --git a/plugins/libimhex/include/helpers/event.hpp b/plugins/libimhex/include/helpers/event.hpp index 5997a7137..791748723 100644 --- a/plugins/libimhex/include/helpers/event.hpp +++ b/plugins/libimhex/include/helpers/event.hpp @@ -30,12 +30,9 @@ namespace hex { class EventManager { public: - void post(Events eventType, const void *userData); - void subscribe(Events eventType, void *owner, std::function<void(const void*)> callback); - void unsubscribe(Events eventType, void *sender); - - private: - std::vector<EventHandler> m_eventHandlers; + static void post(Events eventType, const void *userData); + static void subscribe(Events eventType, void *owner, std::function<void(const void*)> callback); + static void unsubscribe(Events eventType, void *sender); }; } \ No newline at end of file diff --git a/plugins/libimhex/include/helpers/shared_data.hpp b/plugins/libimhex/include/helpers/shared_data.hpp new file mode 100644 index 000000000..38f049356 --- /dev/null +++ b/plugins/libimhex/include/helpers/shared_data.hpp @@ -0,0 +1,71 @@ +#pragma once + +#include <functional> +#include <vector> + +#include <helpers/event.hpp> +#include <imgui.h> + +namespace hex { class SharedData; } + +namespace hex::plugin::internal { + void initializePlugin(SharedData &sharedData); +} + +namespace hex { + + namespace prv { class Provider; } + + class SharedData { + SharedData() = default; + public: + SharedData(const SharedData&) = delete; + SharedData(SharedData&&) = delete; + + static auto& get() { + static SharedData instance; + + return instance; + } + + friend void hex::plugin::internal::initializePlugin(SharedData &sharedData); + friend class Window; + + private: + + void initializeData() { + static std::vector<EventHandler> eventHandlersStorage; + static std::vector<std::function<void()>> deferredCallsStorage; + static prv::Provider *currentProviderStorage; + static ImVec2 windowPosStorage, windowSizeStorage; + + this->imguiContext = ImGui::GetCurrentContext(); + this->eventHandlers = &eventHandlersStorage; + this->deferredCalls = &deferredCallsStorage; + this->currentProvider = ¤tProviderStorage; + + 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->windowPos = other.windowPos; + this->windowSize = other.windowSize; + } + + public: + ImGuiContext *imguiContext; + std::vector<EventHandler> *eventHandlers; + std::vector<std::function<void()>> *deferredCalls; + prv::Provider **currentProvider; + + ImVec2 *windowPos; + ImVec2 *windowSize; + }; + +} \ No newline at end of file diff --git a/plugins/libimhex/include/plugin.hpp b/plugins/libimhex/include/plugin.hpp index 8d7429409..641934933 100644 --- a/plugins/libimhex/include/plugin.hpp +++ b/plugins/libimhex/include/plugin.hpp @@ -6,13 +6,13 @@ #include <hex.hpp> #include <views/view.hpp> #include <providers/provider.hpp> +#include <helpers/shared_data.hpp> -#define IMHEX_PLUGIN namespace hex::plugin::internal { \ - void initializePlugin(ImGuiContext *ctx, hex::prv::Provider **provider) { \ - if (glGetString == NULL) \ - gladLoadGL(); \ - ImGui::SetCurrentContext(ctx); \ - hex::prv::Provider::setProviderStorage(*provider); \ - } \ - } \ +#define IMHEX_PLUGIN namespace hex::plugin::internal { \ + void initializePlugin(SharedData &sharedData) { \ + if (glGetString == NULL) \ + gladLoadGL(); \ + SharedData::get().initializeData(sharedData); \ + } \ + } \ namespace hex::plugin diff --git a/plugins/libimhex/include/providers/provider.hpp b/plugins/libimhex/include/providers/provider.hpp index 721b793cb..108702930 100644 --- a/plugins/libimhex/include/providers/provider.hpp +++ b/plugins/libimhex/include/providers/provider.hpp @@ -7,6 +7,8 @@ #include <string> #include <vector> +#include <helpers/shared_data.hpp> + namespace hex::prv { class Provider { @@ -40,20 +42,10 @@ namespace hex::prv { virtual std::vector<std::pair<std::string, std::string>> getDataInformation() = 0; - static void setProviderStorage(Provider* &provider) { - Provider::s_currProvider = &provider; - } - - static Provider*& getCurrentProvider() { - return *Provider::s_currProvider; - } - protected: u32 m_currPage = 0; std::vector<std::map<u64, u8>> m_patches; - - static inline Provider **s_currProvider = nullptr; }; } \ No newline at end of file diff --git a/plugins/libimhex/include/views/view.hpp b/plugins/libimhex/include/views/view.hpp index f1602cfce..21f54464d 100644 --- a/plugins/libimhex/include/views/view.hpp +++ b/plugins/libimhex/include/views/view.hpp @@ -30,14 +30,6 @@ namespace hex { static void showErrorPopup(std::string_view errorMessage); - static void setWindowPosition(s32 x, s32 y); - - static const ImVec2& getWindowPosition(); - - static void setWindowSize(s32 width, s32 height); - - static const ImVec2& getWindowSize(); - virtual bool hasViewMenuItemEntry(); virtual ImVec2 getMinSize(); virtual ImVec2 getMaxSize(); @@ -55,17 +47,12 @@ namespace hex { protected: void confirmButtons(const char *textLeft, const char *textRight, std::function<void()> leftButtonFn, std::function<void()> rightButtonFn); + private: std::string m_viewName; bool m_windowOpen = false; - static inline EventManager s_eventManager; - static inline std::vector<std::function<void()>> s_deferedCalls; - static inline std::string s_errorMessage; - - static inline ImVec2 s_windowPos; - static inline ImVec2 s_windowSize; }; } \ No newline at end of file diff --git a/plugins/libimhex/source/helpers/event.cpp b/plugins/libimhex/source/helpers/event.cpp index 5fa81d632..01d9e9e72 100644 --- a/plugins/libimhex/source/helpers/event.cpp +++ b/plugins/libimhex/source/helpers/event.cpp @@ -1,23 +1,25 @@ #include "helpers/event.hpp" +#include <helpers/shared_data.hpp> + namespace hex { void EventManager::post(Events eventType, const void *userData) { - for (auto &handler : this->m_eventHandlers) + for (auto &handler : *SharedData::get().eventHandlers) if (eventType == handler.eventType) handler.callback(userData); } void EventManager::subscribe(Events eventType, void *owner, std::function<void(const void*)> callback) { - for (auto &handler : this->m_eventHandlers) + for (auto &handler : *SharedData::get().eventHandlers) if (eventType == handler.eventType && owner == handler.owner) return; - this->m_eventHandlers.push_back(EventHandler { owner, eventType, callback }); + SharedData::get().eventHandlers->push_back(EventHandler { owner, eventType, callback }); } void EventManager::unsubscribe(Events eventType, void *sender) { - std::erase_if(this->m_eventHandlers, [&eventType, &sender](EventHandler handler) { + std::erase_if(*SharedData::get().eventHandlers, [&eventType, &sender](EventHandler handler) { return eventType == handler.eventType && sender == handler.owner; }); } diff --git a/plugins/libimhex/source/views/view.cpp b/plugins/libimhex/source/views/view.cpp index 28bff617a..f414090ed 100644 --- a/plugins/libimhex/source/views/view.cpp +++ b/plugins/libimhex/source/views/view.cpp @@ -6,6 +6,8 @@ #include <string> #include <vector> +#include <helpers/shared_data.hpp> + namespace hex { @@ -15,11 +17,11 @@ namespace hex { bool View::handleShortcut(int key, int mods) { return false; } std::vector<std::function<void()>>& View::getDeferedCalls() { - return View::s_deferedCalls; + return *SharedData::get().deferredCalls; } void View::postEvent(Events eventType, const void *userData) { - View::s_eventManager.post(eventType, userData); + EventManager::post(eventType, userData); } void View::drawCommonInterfaces() { @@ -44,22 +46,6 @@ namespace hex { ImGui::OpenPopup("Error"); } - void View::setWindowPosition(s32 x, s32 y) { - View::s_windowPos = ImVec2(x, y); - } - - const ImVec2& View::getWindowPosition() { - return View::s_windowPos; - } - - void View::setWindowSize(s32 width, s32 height) { - View::s_windowSize = ImVec2(width, height); - } - - const ImVec2& View::getWindowSize() { - return View::s_windowSize; - } - bool View::hasViewMenuItemEntry() { return true; } @@ -82,15 +68,15 @@ namespace hex { } void View::subscribeEvent(Events eventType, std::function<void(const void*)> callback) { - View::s_eventManager.subscribe(eventType, this, callback); + EventManager::subscribe(eventType, this, callback); } void View::unsubscribeEvent(Events eventType) { - View::s_eventManager.unsubscribe(eventType, this); + EventManager::unsubscribe(eventType, this); } void View::doLater(std::function<void()> &&function) { - View::s_deferedCalls.push_back(function); + SharedData::get().deferredCalls->push_back(function); } void View::confirmButtons(const char *textLeft, const char *textRight, std::function<void()> leftButtonFn, std::function<void()> rightButtonFn) { diff --git a/source/helpers/loader_script_handler.cpp b/source/helpers/loader_script_handler.cpp index 127af8d4e..ec4c32ec0 100644 --- a/source/helpers/loader_script_handler.cpp +++ b/source/helpers/loader_script_handler.cpp @@ -81,7 +81,7 @@ namespace hex { return nullptr; } - hex::ScopeExit instanceCleanup([&]{ Py_DECREF(instance); }); + SCOPE_EXIT( Py_DECREF(instance); ); if (instance->ob_type->tp_base == nullptr || instance->ob_type->tp_base->tp_name != "ImHexType"s) { PyErr_SetString(PyExc_TypeError, "class type must extend from ImHexType"); @@ -106,7 +106,7 @@ namespace hex { return nullptr; } - hex::ScopeExit listCleanup([&]{ Py_DECREF(list); }); + SCOPE_EXIT( Py_DECREF(list); ); std::string code = keyword + " " + instance->ob_type->tp_name + " {\n"; diff --git a/source/helpers/plugin_handler.cpp b/source/helpers/plugin_handler.cpp index ff9c8700f..bf6d0e44c 100644 --- a/source/helpers/plugin_handler.cpp +++ b/source/helpers/plugin_handler.cpp @@ -9,8 +9,8 @@ namespace hex { constexpr auto CreateViewSymbol = "_ZN3hex6plugin10createViewEv"; // hex::plugin::drawToolsEntry(void) constexpr auto DrawToolsEntrySymbol = "_ZN3hex6plugin14drawToolsEntryEv"; - // hex::plugin::internal::initializePlugin(ImGuiContext*, hex::prv::Provider**) - constexpr auto InitializePluginSymbol = "_ZN3hex6plugin8internal16initializePluginEP12ImGuiContextPPNS_3prv8ProviderE"; + // hex::plugin::internal::initializePlugin(SharedData&) + constexpr auto InitializePluginSymbol = "_ZN3hex6plugin8internal16initializePluginER10SharedData"; Plugin::Plugin(std::string_view path) { this->m_handle = dlopen(path.data(), RTLD_LAZY); @@ -28,9 +28,9 @@ namespace hex { dlclose(this->m_handle); } - void Plugin::initializePlugin(ImGuiContext *ctx, prv::Provider **provider) const { + void Plugin::initializePlugin(SharedData &sharedData) const { if (this->m_initializePluginFunction != nullptr) - this->m_initializePluginFunction(ctx, provider); + this->m_initializePluginFunction(sharedData); } View* Plugin::createView() const { diff --git a/source/main.cpp b/source/main.cpp index af0fdd2c9..4dfe584eb 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -31,8 +31,6 @@ int main(int argc, char **argv) { // Shared Data std::vector<hex::lang::PatternData*> patternData; - hex::prv::Provider *dataProvider = nullptr; - hex::prv::Provider::setProviderStorage(dataProvider); // Create views window.addView<hex::ViewHexEditor>(patternData); diff --git a/source/views/view_bookmarks.cpp b/source/views/view_bookmarks.cpp index 3e55bdda0..29d5c815a 100644 --- a/source/views/view_bookmarks.cpp +++ b/source/views/view_bookmarks.cpp @@ -55,7 +55,7 @@ namespace hex { { u8 bytes[10] = { 0 }; - prv::Provider::getCurrentProvider()->read(region.address, bytes, std::min(region.size, size_t(10))); + (*SharedData::get().currentProvider)->read(region.address, bytes, std::min(region.size, size_t(10))); std::string bytesString; for (u8 i = 0; i < std::min(region.size, size_t(10)); i++) { diff --git a/source/views/view_command_palette.cpp b/source/views/view_command_palette.cpp index 954529985..67569f6b9 100644 --- a/source/views/view_command_palette.cpp +++ b/source/views/view_command_palette.cpp @@ -19,8 +19,8 @@ namespace hex { void ViewCommandPalette::drawContent() { - auto windowPos = View::getWindowPosition(); - auto windowSize = View::getWindowSize(); + auto windowPos = *SharedData::get().windowPos; + auto windowSize = *SharedData::get().windowSize; auto paletteSize = this->getMinSize(); ImGui::SetNextWindowPos(ImVec2(windowPos.x + (windowSize.x - paletteSize.x) / 2.0F, windowPos.y), ImGuiCond_Always); if (ImGui::BeginPopup("Command Palette")) { diff --git a/source/views/view_data_inspector.cpp b/source/views/view_data_inspector.cpp index 0548eee21..db2edf347 100644 --- a/source/views/view_data_inspector.cpp +++ b/source/views/view_data_inspector.cpp @@ -13,7 +13,7 @@ namespace hex { View::subscribeEvent(Events::RegionSelected, [this](const void* userData){ Region region = *static_cast<const Region*>(userData); - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; if (provider == nullptr) { this->m_validBytes = 0; @@ -137,7 +137,7 @@ namespace hex { if (ImGui::Begin("Data Inspector", &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) { - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; if (provider != nullptr && provider->isReadable()) { if (ImGui::BeginTable("##datainspector", 2, diff --git a/source/views/view_disassembler.cpp b/source/views/view_disassembler.cpp index 508a4af31..e09da58dc 100644 --- a/source/views/view_disassembler.cpp +++ b/source/views/view_disassembler.cpp @@ -51,7 +51,7 @@ namespace hex { if (cs_open(Disassembler::toCapstoneArchictecture(this->m_architecture), mode, &capstoneHandle) == CS_ERR_OK) { - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; std::vector<u8> buffer(2048, 0x00); for (u64 address = 0; address < (this->m_codeRegion[1] - this->m_codeRegion[0] + 1); address += 2048) { size_t bufferSize = std::min(u64(2048), (this->m_codeRegion[1] - this->m_codeRegion[0] + 1) - address); @@ -95,7 +95,7 @@ namespace hex { if (ImGui::Begin("Disassembler", &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) { - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; if (provider != nullptr && provider->isReadable()) { ImGui::TextUnformatted("Position"); ImGui::Separator(); diff --git a/source/views/view_hashes.cpp b/source/views/view_hashes.cpp index 1f67de79e..4da25f490 100644 --- a/source/views/view_hashes.cpp +++ b/source/views/view_hashes.cpp @@ -41,7 +41,7 @@ namespace hex { if (ImGui::Begin("Hashing", &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) { ImGui::BeginChild("##scrolling", ImVec2(0, 0), false, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoNav); - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; if (provider != nullptr && provider->isAvailable()) { ImGui::TextUnformatted("Region"); diff --git a/source/views/view_hexeditor.cpp b/source/views/view_hexeditor.cpp index 75ecf05e6..d10ffcc53 100644 --- a/source/views/view_hexeditor.cpp +++ b/source/views/view_hexeditor.cpp @@ -19,7 +19,7 @@ namespace hex { : View("Hex Editor"), m_patternData(patternData) { this->m_memoryEditor.ReadFn = [](const ImU8 *data, size_t off) -> ImU8 { - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; if (!provider->isAvailable() || !provider->isReadable()) return 0x00; @@ -30,7 +30,7 @@ namespace hex { }; this->m_memoryEditor.WriteFn = [](ImU8 *data, size_t off, ImU8 d) -> void { - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; if (!provider->isAvailable() || !provider->isWritable()) return; @@ -71,7 +71,7 @@ namespace hex { View::subscribeEvent(Events::SelectionChangeRequest, [this](const void *userData) { const Region ®ion = *reinterpret_cast<const Region*>(userData); - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; auto page = provider->getPageOfAddress(region.address); if (!page.has_value()) return; @@ -110,7 +110,7 @@ namespace hex { } void ViewHexEditor::drawContent() { - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; size_t dataSize = (provider == nullptr || !provider->isReadable()) ? 0x00 : provider->getSize(); @@ -277,7 +277,7 @@ namespace hex { } void ViewHexEditor::drawMenu() { - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; if (ImGui::BeginMenu("File")) { if (ImGui::MenuItem("Open File...", "CTRL + O")) { @@ -432,7 +432,7 @@ namespace hex { bool ViewHexEditor::handleShortcut(int key, int mods) { if (mods == GLFW_MOD_CONTROL && key == GLFW_KEY_S) { - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; for (const auto &[address, value] : provider->getPatches()) provider->writeRaw(address, &value, sizeof(u8)); return true; @@ -461,7 +461,7 @@ namespace hex { void ViewHexEditor::openFile(std::string path) { - auto& provider = prv::Provider::getCurrentProvider(); + auto& provider = *SharedData::get().currentProvider; if (provider != nullptr) delete provider; @@ -509,7 +509,7 @@ namespace hex { } void ViewHexEditor::copyBytes() { - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; size_t start = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd); size_t end = std::max(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd); @@ -528,7 +528,7 @@ namespace hex { } void ViewHexEditor::copyString() { - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; size_t start = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd); size_t end = std::max(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd); @@ -543,7 +543,7 @@ namespace hex { } void ViewHexEditor::copyLanguageArray(Language language) { - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; size_t start = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd); size_t end = std::max(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd); @@ -645,7 +645,7 @@ namespace hex { } void ViewHexEditor::copyHexView() { - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; size_t start = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd); size_t end = std::max(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd); @@ -692,7 +692,7 @@ namespace hex { } void ViewHexEditor::copyHexViewHTML() { - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; size_t start = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd); size_t end = std::max(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd); @@ -825,7 +825,7 @@ R"( void ViewHexEditor::drawSearchPopup() { static auto InputCallback = [](ImGuiInputTextCallbackData* data) -> int { auto _this = static_cast<ViewHexEditor*>(data->UserData); - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; *_this->m_lastSearchBuffer = _this->m_searchFunction(provider, data->Buf); _this->m_lastSearchIndex = 0; @@ -837,7 +837,7 @@ R"( }; static auto Find = [this](char *buffer) { - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; *this->m_lastSearchBuffer = this->m_searchFunction(provider, buffer); this->m_lastSearchIndex = 0; @@ -915,7 +915,7 @@ R"( } void ViewHexEditor::drawGotoPopup() { - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; if (ImGui::BeginPopup("Goto")) { ImGui::TextUnformatted("Goto"); diff --git a/source/views/view_information.cpp b/source/views/view_information.cpp index 700088c36..bc91230d5 100644 --- a/source/views/view_information.cpp +++ b/source/views/view_information.cpp @@ -49,7 +49,7 @@ namespace hex { if (ImGui::Begin("Data Information", &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) { ImGui::BeginChild("##scrolling", ImVec2(0, 0), false, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoNav); - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; if (provider != nullptr && provider->isReadable()) { if (this->m_shouldInvalidate) { @@ -135,7 +135,7 @@ namespace hex { if (this->m_dataValid) { - for (auto &[name, value] : prv::Provider::getCurrentProvider()->getDataInformation()) { + for (auto &[name, value] : (*SharedData::get().currentProvider)->getDataInformation()) { ImGui::LabelText(name.c_str(), "%s", value.c_str()); } diff --git a/source/views/view_patches.cpp b/source/views/view_patches.cpp index 4137a0592..47f177c12 100644 --- a/source/views/view_patches.cpp +++ b/source/views/view_patches.cpp @@ -13,13 +13,13 @@ namespace hex { ViewPatches::ViewPatches() : View("Patches") { View::subscribeEvent(Events::ProjectFileStore, [this](const void*) { - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; if (provider != nullptr) ProjectFile::setPatches(provider->getPatches()); }); View::subscribeEvent(Events::ProjectFileLoad, [this](const void*) { - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; if (provider != nullptr) provider->getPatches() = ProjectFile::getPatches(); }); @@ -32,7 +32,7 @@ namespace hex { void ViewPatches::drawContent() { if (ImGui::Begin("Patches", &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) { - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; if (provider != nullptr && provider->isReadable()) { diff --git a/source/views/view_pattern.cpp b/source/views/view_pattern.cpp index 68605e544..7b948fe5a 100644 --- a/source/views/view_pattern.cpp +++ b/source/views/view_pattern.cpp @@ -109,7 +109,7 @@ namespace hex { if (error) return; - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; if (provider == nullptr) return; @@ -182,7 +182,7 @@ namespace hex { void ViewPattern::drawContent() { if (ImGui::Begin("Pattern", &this->getWindowOpenState(), ImGuiWindowFlags_None | ImGuiWindowFlags_NoCollapse)) { - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; if (provider != nullptr && provider->isAvailable()) { this->m_textEditor.Render("Pattern"); @@ -303,7 +303,7 @@ namespace hex { return; } - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; hex::lang::Evaluator evaluator(provider, defaultDataEndianess); auto patternData = evaluator.evaluate(ast.value()); if (!patternData.has_value()) { diff --git a/source/views/view_pattern_data.cpp b/source/views/view_pattern_data.cpp index 0794036c4..fdb64088f 100644 --- a/source/views/view_pattern_data.cpp +++ b/source/views/view_pattern_data.cpp @@ -50,7 +50,7 @@ namespace hex { void ViewPatternData::drawContent() { if (ImGui::Begin("Pattern Data", &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) { - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; if (provider != nullptr && provider->isReadable()) { if (beginPatternDataTable(provider, this->m_patternData, this->m_sortedPatternData)) { diff --git a/source/views/view_strings.cpp b/source/views/view_strings.cpp index ea4e5e89f..548c4a5bc 100644 --- a/source/views/view_strings.cpp +++ b/source/views/view_strings.cpp @@ -47,7 +47,7 @@ namespace hex { void ViewStrings::drawContent() { - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; if (this->m_shouldInvalidate) { this->m_shouldInvalidate = false; diff --git a/source/views/view_tools.cpp b/source/views/view_tools.cpp index 617296a25..2c1d52a8b 100644 --- a/source/views/view_tools.cpp +++ b/source/views/view_tools.cpp @@ -42,7 +42,7 @@ namespace hex { this->m_mathEvaluator.setFunction("read", [this](auto args) -> std::optional<long double> { u8 value = 0; - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; if (provider == nullptr || !provider->isReadable() || args[0] >= provider->getActualSize()) return { }; @@ -52,7 +52,7 @@ namespace hex { }, 1, 1); this->m_mathEvaluator.setFunction("write", [this](auto args) -> std::optional<long double> { - auto provider = prv::Provider::getCurrentProvider(); + auto provider = *SharedData::get().currentProvider; if (provider == nullptr || !provider->isWritable() || args[0] >= provider->getActualSize()) return { }; diff --git a/source/window.cpp b/source/window.cpp index 5a64301e2..a123b64c8 100644 --- a/source/window.cpp +++ b/source/window.cpp @@ -48,6 +48,8 @@ namespace hex { } Window::Window() { + SharedData::get().initializeData(); + this->initGLFW(); this->initImGui(); this->initPlugins(); @@ -282,21 +284,21 @@ namespace hex { { int x = 0, y = 0; glfwGetWindowPos(this->m_window, &x, &y); - View::setWindowPosition(x, y); + *SharedData::get().windowPos = ImVec2(x, y); } { int width = 0, height = 0; glfwGetWindowSize(this->m_window, &width, &height); - View::setWindowSize(width, height); + *SharedData::get().windowSize = ImVec2(width, height); } glfwSetWindowPosCallback(this->m_window, [](GLFWwindow *window, int x, int y) { - View::setWindowPosition(x, y); + *SharedData::get().windowPos = ImVec2(x, y); }); glfwSetWindowSizeCallback(this->m_window, [](GLFWwindow *window, int width, int height) { - View::setWindowSize(width, height); + *SharedData::get().windowSize = ImVec2(width, height); }); glfwSetKeyCallback(this->m_window, [](GLFWwindow *window, int key, int scancode, int action, int mods) { @@ -377,7 +379,7 @@ namespace hex { PluginHandler::load((std::filesystem::path(mainArgv[0]).parent_path() / "plugins").string()); for (const auto &plugin : PluginHandler::getPlugins()) { - plugin.initializePlugin(ImGui::GetCurrentContext(), &prv::Provider::getCurrentProvider()); + plugin.initializePlugin(SharedData::get()); if (auto view = plugin.createView(); view != nullptr) this->m_pluginViews.push_back(view); }