diff --git a/CMakeLists.txt b/CMakeLists.txt index 0023f72b3..4207f47fe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -182,11 +182,16 @@ set_target_properties(imhex PROPERTIES CXX_VISIBILITY_PRESET hidden) target_link_directories(imhex PRIVATE ${CRYPTO_LIBRARY_DIRS} ${CAPSTONE_LIBRARY_DIRS} ${MAGIC_LIBRARY_DIRS}) if (WIN32) - target_link_libraries(imhex libdl.a libmagic.a libgnurx.a libtre.a libintl.a libiconv.a libshlwapi.a libcrypto.a libwinpthread.a libcapstone.a LLVMDemangle imgui libimhex ${Python_LIBRARIES}) + target_link_libraries(imhex libdl.a libmagic.a libgnurx.a libtre.a libintl.a libiconv.a libshlwapi.a libcrypto.a libwinpthread.a libcapstone.a LLVMDemangle libimhex ${Python_LIBRARIES}) elseif (UNIX) - target_link_libraries(imhex magic crypto ${CMAKE_DL_LIBS} capstone LLVMDemangle imgui libimhex ${Python_LIBRARIES} dl) + target_link_libraries(imhex magic crypto ${CMAKE_DL_LIBS} capstone LLVMDemangle libimhex ${Python_LIBRARIES} dl) endif() +add_custom_command(TARGET imhex POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + $ + $) + if (WIN32) # Install binaries directly in the prefix, usually C:\Program Files\ImHex. set(CMAKE_INSTALL_BINDIR ".") diff --git a/external/ImGui/CMakeLists.txt b/external/ImGui/CMakeLists.txt index 63e71c8cb..372da3631 100644 --- a/external/ImGui/CMakeLists.txt +++ b/external/ImGui/CMakeLists.txt @@ -8,6 +8,8 @@ find_package(Freetype REQUIRED) pkg_search_module(GLM REQUIRED glm) pkg_search_module(GLFW REQUIRED glfw3) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") + add_library(imgui source/imgui.cpp source/imgui_demo.cpp diff --git a/external/glad/CMakeLists.txt b/external/glad/CMakeLists.txt index 49c58033e..ab0a347b9 100644 --- a/external/glad/CMakeLists.txt +++ b/external/glad/CMakeLists.txt @@ -4,9 +4,11 @@ project(glad) set(CMAKE_CXX_STANDARD 17) if (WIN32) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++ -static-libgcc -static") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++ -static-libgcc") endif() +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") + add_library(glad source/glad.c ) diff --git a/external/llvm/CMakeLists.txt b/external/llvm/CMakeLists.txt index c536d79d0..84406b925 100644 --- a/external/llvm/CMakeLists.txt +++ b/external/llvm/CMakeLists.txt @@ -4,9 +4,11 @@ project(LLVMDemangle) set(CMAKE_CXX_STANDARD 17) if (WIN32) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++ -static-libgcc -static") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++ -static-libgcc") endif() +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") + add_library(LLVMDemangle Demangle/Demangle.cpp Demangle/ItaniumDemangle.cpp diff --git a/include/helpers/plugin_handler.hpp b/include/helpers/plugin_handler.hpp index d56d0451b..0242b7f09 100644 --- a/include/helpers/plugin_handler.hpp +++ b/include/helpers/plugin_handler.hpp @@ -14,10 +14,10 @@ namespace hex { Plugin(std::string_view path); ~Plugin(); - void initializePlugin(SharedData &sharedData) const; + void initializePlugin() const; private: - using InitializePluginFunc = void(*)(SharedData &sharedData); + using InitializePluginFunc = void(*)(); void *m_handle = nullptr; diff --git a/plugins/libimhex/CMakeLists.txt b/plugins/libimhex/CMakeLists.txt index a95725cac..994f37256 100644 --- a/plugins/libimhex/CMakeLists.txt +++ b/plugins/libimhex/CMakeLists.txt @@ -8,13 +8,16 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../external/nlohmann_json ${CMAK if (WIN32) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++ -static-libgcc -static") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++ -static-libgcc") endif() -add_library(libimhex STATIC +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") + +add_library(libimhex SHARED source/helpers/event.cpp source/helpers/utils.cpp source/helpers/content_registry.cpp + source/helpers/shared_data.cpp source/lang/preprocessor.cpp source/lang/lexer.cpp diff --git a/plugins/libimhex/include/helpers/shared_data.hpp b/plugins/libimhex/include/helpers/shared_data.hpp index 52cc77490..fb645b480 100644 --- a/plugins/libimhex/include/helpers/shared_data.hpp +++ b/plugins/libimhex/include/helpers/shared_data.hpp @@ -28,106 +28,39 @@ namespace hex { 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; template - T& getVariable(std::string variableName) { - return std::any_cast((*this->sharedVariables)[variableName]); + static T& getVariable(std::string variableName) { + return std::any_cast(SharedData::sharedVariables[variableName]); } template - void setVariable(std::string variableName, T value) { - (*this->sharedVariables)[variableName] = value; - } - - private: - - void initializeData() { - static int mainArgcStorage; - static char **mainArgvStorage; - static ImGuiContext *imGuiContextStorage; - static std::vector eventHandlersStorage; - static std::vector> deferredCallsStorage; - static prv::Provider *currentProviderStorage; - static std::map> settingsEntriesStorage; - static std::map sharedVariablesStorage; - static ImVec2 windowPosStorage, windowSizeStorage; - static nlohmann::json settingsJsonStorage; - static std::map customEventsStorage; - static u32 customEventsLastIdStorage = u32(Events::Events_BuiltinEnd) + 1; - static std::vector commandPaletteCommandsStorage; - static std::map patternLanguageFunctionsStorage; - static std::vector viewsStorage; - static std::vector> toolsStorage; - - this->imguiContext = &imGuiContextStorage; - this->eventHandlers = &eventHandlersStorage; - this->deferredCalls = &deferredCallsStorage; - this->currentProvider = ¤tProviderStorage; - this->settingsEntries = &settingsEntriesStorage; - this->sharedVariables = &sharedVariablesStorage; - this->windowPos = &windowPosStorage; - this->windowSize = &windowSizeStorage; - this->settingsJson = &settingsJsonStorage; - this->customEvents = &customEventsStorage; - this->customEventsLastId = &customEventsLastIdStorage; - this->commandPaletteCommands = &commandPaletteCommandsStorage; - this->patternLanguageFunctions = &patternLanguageFunctionsStorage; - this->views = &viewsStorage; - this->tools = &toolsStorage; - this->mainArgc = &mainArgcStorage; - this->mainArgv = &mainArgvStorage; - } - - 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->windowPos = other.windowPos; - this->windowSize = other.windowSize; - this->settingsJson = other.settingsJson; - this->customEvents = other.customEvents; - this->customEventsLastId = other.customEventsLastId; - this->commandPaletteCommands = other.commandPaletteCommands; - this->patternLanguageFunctions = other.patternLanguageFunctions; - this->views = other.views; - this->tools = other.tools; - this->mainArgc = other.mainArgc; - this->mainArgv = other.mainArgv; + static void setVariable(std::string variableName, T value) { + SharedData::sharedVariables[variableName] = value; } public: - ImGuiContext **imguiContext; - std::vector *eventHandlers; - std::vector> *deferredCalls; - prv::Provider **currentProvider; - std::map> *settingsEntries; - nlohmann::json *settingsJson; - std::map *customEvents; - u32 *customEventsLastId; - std::vector *commandPaletteCommands; - std::map *patternLanguageFunctions; - std::vector *views; - std::vector> *tools; + static std::vector eventHandlers; + static std::vector> deferredCalls; + static prv::Provider *currentProvider; + static std::map> settingsEntries; + static nlohmann::json settingsJson; + static std::map customEvents; + static u32 customEventsLastId; + static std::vector commandPaletteCommands; + static std::map patternLanguageFunctions; + static std::vector views; + static std::vector> tools; - int *mainArgc; - char ***mainArgv; + static int mainArgc; + static char **mainArgv; - ImVec2 *windowPos; - ImVec2 *windowSize; + static ImVec2 windowPos; + static ImVec2 windowSize; private: - std::map *sharedVariables; + static std::map sharedVariables; }; } \ No newline at end of file diff --git a/plugins/libimhex/include/plugin.hpp b/plugins/libimhex/include/plugin.hpp index dcc08eaa2..37a5acc69 100644 --- a/plugins/libimhex/include/plugin.hpp +++ b/plugins/libimhex/include/plugin.hpp @@ -11,13 +11,8 @@ #define IMHEX_PLUGIN_SETUP namespace hex::plugin { void setup(); } \ namespace hex::plugin::internal { \ - void initializePlugin(SharedData &sharedData) { \ - if (glGetString == NULL) \ - gladLoadGL(); \ - SharedData::get().initializeData(sharedData); \ - ImGui::SetCurrentContext(*sharedData.imguiContext); \ + void initializePlugin() { \ hex::plugin::setup(); \ - \ } \ } \ void hex::plugin::setup() diff --git a/plugins/libimhex/source/helpers/content_registry.cpp b/plugins/libimhex/source/helpers/content_registry.cpp index d595dda95..ab69069fc 100644 --- a/plugins/libimhex/source/helpers/content_registry.cpp +++ b/plugins/libimhex/source/helpers/content_registry.cpp @@ -10,14 +10,14 @@ namespace hex { /* Settings */ void ContentRegistry::Settings::load() { - std::ifstream settingsFile(std::filesystem::path((*SharedData::get().mainArgv)[0]).parent_path() / "settings.json"); + std::ifstream settingsFile(std::filesystem::path((SharedData::mainArgv)[0]).parent_path() / "settings.json"); if (settingsFile.good()) settingsFile >> getSettingsData(); } void ContentRegistry::Settings::store() { - std::ofstream settingsFile(std::filesystem::path((*SharedData::get().mainArgv)[0]).parent_path() / "settings.json", std::ios::trunc); + std::ofstream settingsFile(std::filesystem::path((SharedData::mainArgv)[0]).parent_path() / "settings.json", std::ios::trunc); settingsFile << getSettingsData(); } @@ -40,19 +40,19 @@ namespace hex { } std::map>& ContentRegistry::Settings::getEntries() { - return *SharedData::get().settingsEntries; + return SharedData::settingsEntries; } nlohmann::json& ContentRegistry::Settings::getSettingsData() { - return *SharedData::get().settingsJson; + return SharedData::settingsJson; } /* Events */ auto ContentRegistry::Events::get(std::string_view name) { - auto &customEvents = *SharedData::get().customEvents; - auto &lastId = *SharedData::get().customEventsLastId; + auto &customEvents = SharedData::customEvents; + auto &lastId = SharedData::customEventsLastId; if (!customEvents.contains(name.data())) { customEvents[name.data()] = static_cast(lastId); @@ -70,7 +70,7 @@ namespace hex { } std::vector& ContentRegistry::CommandPaletteCommands::getEntries() { - return *SharedData::get().commandPaletteCommands; + return SharedData::commandPaletteCommands; } @@ -81,7 +81,7 @@ namespace hex { } std::map& ContentRegistry::PatternLanguageFunctions::getEntries() { - return *SharedData::get().patternLanguageFunctions; + return SharedData::patternLanguageFunctions; } @@ -96,7 +96,7 @@ namespace hex { } std::vector& ContentRegistry::Views::getEntries() { - return *SharedData::get().views; + return SharedData::views; } @@ -107,7 +107,7 @@ namespace hex { } std::vector>& ContentRegistry::Tools::getEntries() { - return *SharedData::get().tools; + return SharedData::tools; } } \ No newline at end of file diff --git a/plugins/libimhex/source/helpers/event.cpp b/plugins/libimhex/source/helpers/event.cpp index 01d9e9e72..dcac31631 100644 --- a/plugins/libimhex/source/helpers/event.cpp +++ b/plugins/libimhex/source/helpers/event.cpp @@ -5,21 +5,21 @@ namespace hex { void EventManager::post(Events eventType, const void *userData) { - for (auto &handler : *SharedData::get().eventHandlers) + for (auto &handler : SharedData::eventHandlers) if (eventType == handler.eventType) handler.callback(userData); } void EventManager::subscribe(Events eventType, void *owner, std::function callback) { - for (auto &handler : *SharedData::get().eventHandlers) + for (auto &handler : SharedData::eventHandlers) if (eventType == handler.eventType && owner == handler.owner) return; - SharedData::get().eventHandlers->push_back(EventHandler { owner, eventType, callback }); + SharedData::eventHandlers.push_back(EventHandler { owner, eventType, callback }); } void EventManager::unsubscribe(Events eventType, void *sender) { - std::erase_if(*SharedData::get().eventHandlers, [&eventType, &sender](EventHandler handler) { + std::erase_if(SharedData::eventHandlers, [&eventType, &sender](EventHandler handler) { return eventType == handler.eventType && sender == handler.owner; }); } diff --git a/plugins/libimhex/source/helpers/shared_data.cpp b/plugins/libimhex/source/helpers/shared_data.cpp new file mode 100644 index 000000000..49c915337 --- /dev/null +++ b/plugins/libimhex/source/helpers/shared_data.cpp @@ -0,0 +1,24 @@ +#include + +namespace hex { + + std::vector SharedData::eventHandlers; + std::vector> SharedData::deferredCalls; + prv::Provider *SharedData::currentProvider; + std::map> SharedData::settingsEntries; + nlohmann::json SharedData::settingsJson; + std::map SharedData::customEvents; + u32 SharedData::customEventsLastId; + std::vector SharedData::commandPaletteCommands; + std::map SharedData::patternLanguageFunctions; + std::vector SharedData::views; + std::vector> SharedData::tools; + + int SharedData::mainArgc; + char **SharedData::mainArgv; + + ImVec2 SharedData::windowPos; + ImVec2 SharedData::windowSize; + + std::map SharedData::sharedVariables; +} \ No newline at end of file diff --git a/plugins/libimhex/source/views/view.cpp b/plugins/libimhex/source/views/view.cpp index f414090ed..3dea249fb 100644 --- a/plugins/libimhex/source/views/view.cpp +++ b/plugins/libimhex/source/views/view.cpp @@ -17,7 +17,7 @@ namespace hex { bool View::handleShortcut(int key, int mods) { return false; } std::vector>& View::getDeferedCalls() { - return *SharedData::get().deferredCalls; + return SharedData::deferredCalls; } void View::postEvent(Events eventType, const void *userData) { @@ -76,7 +76,7 @@ namespace hex { } void View::doLater(std::function &&function) { - SharedData::get().deferredCalls->push_back(function); + SharedData::deferredCalls.push_back(function); } void View::confirmButtons(const char *textLeft, const char *textRight, std::function leftButtonFn, std::function rightButtonFn) { diff --git a/source/helpers/loader_script_handler.cpp b/source/helpers/loader_script_handler.cpp index ec14dcf51..a717dcf64 100644 --- a/source/helpers/loader_script_handler.cpp +++ b/source/helpers/loader_script_handler.cpp @@ -179,10 +179,10 @@ namespace hex { } bool LoaderScript::processFile(std::string_view scriptPath) { - Py_SetProgramName(Py_DecodeLocale((*SharedData::get().mainArgv)[0], nullptr)); + Py_SetProgramName(Py_DecodeLocale((SharedData::mainArgv)[0], nullptr)); - if (std::filesystem::exists(std::filesystem::path((*SharedData::get().mainArgv)[0]).parent_path().string() + "/lib/python" PYTHON_VERSION_MAJOR_MINOR)) - Py_SetPythonHome(Py_DecodeLocale(std::filesystem::path((*SharedData::get().mainArgv)[0]).parent_path().string().c_str(), nullptr)); + if (std::filesystem::exists(std::filesystem::path((SharedData::mainArgv)[0]).parent_path().string() + "/lib/python" PYTHON_VERSION_MAJOR_MINOR)) + Py_SetPythonHome(Py_DecodeLocale(std::filesystem::path((SharedData::mainArgv)[0]).parent_path().string().c_str(), nullptr)); PyImport_AppendInittab("_imhex", []() -> PyObject* { diff --git a/source/helpers/plugin_handler.cpp b/source/helpers/plugin_handler.cpp index 2533e2b9e..fd088ec3d 100644 --- a/source/helpers/plugin_handler.cpp +++ b/source/helpers/plugin_handler.cpp @@ -5,8 +5,8 @@ namespace hex { - // hex::plugin::internal::initializePlugin(SharedData&) - constexpr auto InitializePluginSymbol = "_ZN3hex6plugin8internal16initializePluginERNS_10SharedDataE"; + // hex::plugin::internal::initializePlugin() + constexpr auto InitializePluginSymbol = "_ZN3hex6plugin8internal16initializePluginEv"; Plugin::Plugin(std::string_view path) { this->m_handle = dlopen(path.data(), RTLD_LAZY); @@ -22,9 +22,9 @@ namespace hex { dlclose(this->m_handle); } - void Plugin::initializePlugin(SharedData &sharedData) const { + void Plugin::initializePlugin() const { if (this->m_initializePluginFunction != nullptr) - this->m_initializePluginFunction(sharedData); + this->m_initializePluginFunction(); } void PluginHandler::load(std::string_view pluginFolder) { diff --git a/source/views/view_bookmarks.cpp b/source/views/view_bookmarks.cpp index 81754d248..0cdd130e8 100644 --- a/source/views/view_bookmarks.cpp +++ b/source/views/view_bookmarks.cpp @@ -61,7 +61,7 @@ namespace hex { { u8 bytes[10] = { 0 }; - (*SharedData::get().currentProvider)->read(region.address, bytes, std::min(region.size, size_t(10))); + (SharedData::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 01222eb24..c138dfdd4 100644 --- a/source/views/view_command_palette.cpp +++ b/source/views/view_command_palette.cpp @@ -40,8 +40,8 @@ namespace hex { void ViewCommandPalette::drawContent() { - auto windowPos = *SharedData::get().windowPos; - auto windowSize = *SharedData::get().windowSize; + auto windowPos = SharedData::windowPos; + auto windowSize = SharedData::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 0537ef9f3..ca78e9156 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(userData); - auto provider = *SharedData::get().currentProvider; + auto provider = SharedData::currentProvider; if (provider == nullptr) { this->m_validBytes = 0; @@ -141,7 +141,7 @@ namespace hex { if (ImGui::Begin("Data Inspector", &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) { - auto provider = *SharedData::get().currentProvider; + auto provider = SharedData::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 e09da58dc..e0e8e66a6 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 = *SharedData::get().currentProvider; + auto provider = SharedData::currentProvider; std::vector 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 = *SharedData::get().currentProvider; + auto provider = SharedData::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 4da25f490..b52590686 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 = *SharedData::get().currentProvider; + auto provider = SharedData::currentProvider; if (provider != nullptr && provider->isAvailable()) { ImGui::TextUnformatted("Region"); diff --git a/source/views/view_hexeditor.cpp b/source/views/view_hexeditor.cpp index 5c5f145a4..676993e0a 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 = *SharedData::get().currentProvider; + auto provider = SharedData::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 = *SharedData::get().currentProvider; + auto provider = SharedData::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(userData); - auto provider = *SharedData::get().currentProvider; + auto provider = SharedData::currentProvider; auto page = provider->getPageOfAddress(region.address); if (!page.has_value()) return; @@ -110,7 +110,7 @@ namespace hex { } void ViewHexEditor::drawContent() { - auto provider = *SharedData::get().currentProvider; + auto provider = SharedData::currentProvider; size_t dataSize = (provider == nullptr || !provider->isReadable()) ? 0x00 : provider->getSize(); @@ -300,7 +300,7 @@ namespace hex { } void ViewHexEditor::drawMenu() { - auto provider = *SharedData::get().currentProvider; + auto provider = SharedData::currentProvider; if (ImGui::BeginMenu("File")) { if (ImGui::MenuItem("Open File...", "CTRL + O")) { @@ -415,7 +415,7 @@ namespace hex { bool ViewHexEditor::handleShortcut(int key, int mods) { if (mods == GLFW_MOD_CONTROL && key == GLFW_KEY_S) { - auto provider = *SharedData::get().currentProvider; + auto provider = SharedData::currentProvider; for (const auto &[address, value] : provider->getPatches()) provider->writeRaw(address, &value, sizeof(u8)); return true; @@ -444,7 +444,7 @@ namespace hex { void ViewHexEditor::openFile(std::string path) { - auto& provider = *SharedData::get().currentProvider; + auto& provider = SharedData::currentProvider; if (provider != nullptr) delete provider; @@ -493,7 +493,7 @@ namespace hex { } void ViewHexEditor::copyBytes() { - auto provider = *SharedData::get().currentProvider; + auto provider = SharedData::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); @@ -512,7 +512,7 @@ namespace hex { } void ViewHexEditor::copyString() { - auto provider = *SharedData::get().currentProvider; + auto provider = SharedData::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); @@ -527,7 +527,7 @@ namespace hex { } void ViewHexEditor::copyLanguageArray(Language language) { - auto provider = *SharedData::get().currentProvider; + auto provider = SharedData::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); @@ -629,7 +629,7 @@ namespace hex { } void ViewHexEditor::copyHexView() { - auto provider = *SharedData::get().currentProvider; + auto provider = SharedData::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); @@ -676,7 +676,7 @@ namespace hex { } void ViewHexEditor::copyHexViewHTML() { - auto provider = *SharedData::get().currentProvider; + auto provider = SharedData::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); @@ -809,7 +809,7 @@ R"( void ViewHexEditor::drawSearchPopup() { static auto InputCallback = [](ImGuiInputTextCallbackData* data) -> int { auto _this = static_cast(data->UserData); - auto provider = *SharedData::get().currentProvider; + auto provider = SharedData::currentProvider; *_this->m_lastSearchBuffer = _this->m_searchFunction(provider, data->Buf); _this->m_lastSearchIndex = 0; @@ -821,7 +821,7 @@ R"( }; static auto Find = [this](char *buffer) { - auto provider = *SharedData::get().currentProvider; + auto provider = SharedData::currentProvider; *this->m_lastSearchBuffer = this->m_searchFunction(provider, buffer); this->m_lastSearchIndex = 0; @@ -899,7 +899,7 @@ R"( } void ViewHexEditor::drawGotoPopup() { - auto provider = *SharedData::get().currentProvider; + auto provider = SharedData::currentProvider; if (ImGui::BeginPopup("Goto")) { ImGui::TextUnformatted("Goto"); @@ -1003,7 +1003,7 @@ R"( View::postEvent(Events::AddBookmark, &bookmark); } - auto provider = *SharedData::get().currentProvider; + auto provider = SharedData::currentProvider; if (ImGui::MenuItem("Set base address", nullptr, false, provider != nullptr && provider->isReadable())) { std::memset(this->m_baseAddressBuffer, sizeof(this->m_baseAddressBuffer), 0x00); View::doLater([]{ ImGui::OpenPopup("Set base address"); }); diff --git a/source/views/view_information.cpp b/source/views/view_information.cpp index bc91230d5..df4b95ab9 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 = *SharedData::get().currentProvider; + auto provider = SharedData::currentProvider; if (provider != nullptr && provider->isReadable()) { if (this->m_shouldInvalidate) { @@ -135,7 +135,7 @@ namespace hex { if (this->m_dataValid) { - for (auto &[name, value] : (*SharedData::get().currentProvider)->getDataInformation()) { + for (auto &[name, value] : (SharedData::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 47f177c12..bd9f1222e 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 = *SharedData::get().currentProvider; + auto provider = SharedData::currentProvider; if (provider != nullptr) ProjectFile::setPatches(provider->getPatches()); }); View::subscribeEvent(Events::ProjectFileLoad, [this](const void*) { - auto provider = *SharedData::get().currentProvider; + auto provider = SharedData::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 = *SharedData::get().currentProvider; + auto provider = SharedData::currentProvider; if (provider != nullptr && provider->isReadable()) { diff --git a/source/views/view_pattern.cpp b/source/views/view_pattern.cpp index bc0882cf1..05a677008 100644 --- a/source/views/view_pattern.cpp +++ b/source/views/view_pattern.cpp @@ -112,7 +112,7 @@ namespace hex { if (error) return; - auto provider = *SharedData::get().currentProvider; + auto provider = SharedData::currentProvider; if (provider == nullptr) return; @@ -224,7 +224,7 @@ namespace hex { void ViewPattern::drawContent() { if (ImGui::Begin("Pattern", &this->getWindowOpenState(), ImGuiWindowFlags_None | ImGuiWindowFlags_NoCollapse)) { - auto provider = *SharedData::get().currentProvider; + auto provider = SharedData::currentProvider; if (provider != nullptr && provider->isAvailable()) { auto textEditorSize = ImGui::GetContentRegionAvail(); @@ -378,7 +378,7 @@ namespace hex { return; } - auto provider = *SharedData::get().currentProvider; + auto provider = SharedData::currentProvider; hex::lang::Evaluator evaluator(provider, defaultDataEndianess); auto patternData = evaluator.evaluate(ast.value()); diff --git a/source/views/view_pattern_data.cpp b/source/views/view_pattern_data.cpp index 8c6c38e16..014e73042 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 = *SharedData::get().currentProvider; + auto provider = SharedData::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 548c4a5bc..62e6b1f7f 100644 --- a/source/views/view_strings.cpp +++ b/source/views/view_strings.cpp @@ -47,7 +47,7 @@ namespace hex { void ViewStrings::drawContent() { - auto provider = *SharedData::get().currentProvider; + auto provider = SharedData::currentProvider; if (this->m_shouldInvalidate) { this->m_shouldInvalidate = false; diff --git a/source/views/view_tools.cpp b/source/views/view_tools.cpp index 858c9a67b..d9bbca39f 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 { u8 value = 0; - auto provider = *SharedData::get().currentProvider; + auto provider = SharedData::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 { - auto provider = *SharedData::get().currentProvider; + auto provider = SharedData::currentProvider; if (provider == nullptr || !provider->isWritable() || args[0] >= provider->getActualSize()) return { }; diff --git a/source/window.cpp b/source/window.cpp index 25d3a277a..433f3a1bc 100644 --- a/source/window.cpp +++ b/source/window.cpp @@ -45,10 +45,8 @@ namespace hex { } Window::Window(int &argc, char **&argv) { - SharedData::get().initializeData(); - - hex::SharedData::get().mainArgc = &argc; - hex::SharedData::get().mainArgv = &argv; + hex::SharedData::mainArgc = argc; + hex::SharedData::mainArgv = argv; ContentRegistry::Settings::load(); View::postEvent(Events::SettingsChanged, nullptr); @@ -254,21 +252,21 @@ namespace hex { { int x = 0, y = 0; glfwGetWindowPos(this->m_window, &x, &y); - *SharedData::get().windowPos = ImVec2(x, y); + SharedData::windowPos = ImVec2(x, y); } { int width = 0, height = 0; glfwGetWindowSize(this->m_window, &width, &height); - *SharedData::get().windowSize = ImVec2(width, height); + SharedData::windowSize = ImVec2(width, height); } glfwSetWindowPosCallback(this->m_window, [](GLFWwindow *window, int x, int y) { - *SharedData::get().windowPos = ImVec2(x, y); + SharedData::windowPos = ImVec2(x, y); }); glfwSetWindowSizeCallback(this->m_window, [](GLFWwindow *window, int width, int height) { - *SharedData::get().windowSize = ImVec2(width, height); + SharedData::windowSize = ImVec2(width, height); }); glfwSetKeyCallback(this->m_window, [](GLFWwindow *window, int key, int scancode, int action, int mods) { @@ -297,6 +295,8 @@ namespace hex { void Window::initImGui() { IMGUI_CHECKVERSION(); auto *ctx = ImGui::CreateContext(); + GImGui = ctx; + ImGuiIO& io = ImGui::GetIO(); ImGuiStyle& style = ImGui::GetStyle(); @@ -307,7 +307,7 @@ namespace hex { style.ScaleAllSizes(this->m_globalScale); #ifdef __MINGW32__ - std::filesystem::path resourcePath = std::filesystem::path((*SharedData::get().mainArgv)[0]).parent_path(); + std::filesystem::path resourcePath = std::filesystem::path((SharedData::mainArgv)[0]).parent_path(); #elif defined(__linux__) std::filesystem::path resourcePath = "/usr/share/ImHex"; #else @@ -346,15 +346,13 @@ namespace hex { } void Window::initPlugins() { - (*SharedData::get().imguiContext) = ImGui::GetCurrentContext(); - try { - auto pluginFolderPath = std::filesystem::path((*SharedData::get().mainArgv)[0]).parent_path() / "plugins"; + auto pluginFolderPath = std::filesystem::path((SharedData::mainArgv)[0]).parent_path() / "plugins"; PluginHandler::load(pluginFolderPath.string()); } catch (std::runtime_error &e) { return; } for (const auto &plugin : PluginHandler::getPlugins()) { - plugin.initializePlugin(SharedData::get()); + plugin.initializePlugin(); } }