From 4f46fff3be65c609527413f0f646b3d128a1afed Mon Sep 17 00:00:00 2001 From: David Given Date: Sat, 18 Oct 2025 01:03:39 +0200 Subject: [PATCH] Remove a bunch of extraneous Providers. --- src/gui2/build.py | 12 ++- src/gui2/imhex_overrides/providers.cpp | 136 +++++++++++++++++++++++++ 2 files changed, 143 insertions(+), 5 deletions(-) create mode 100644 src/gui2/imhex_overrides/providers.cpp diff --git a/src/gui2/build.py b/src/gui2/build.py index 10e91cef..97d98157 100644 --- a/src/gui2/build.py +++ b/src/gui2/build.py @@ -416,16 +416,17 @@ plugin( srcs=sources_from( "dep/imhex/plugins/builtin/source", except_for=[ - "dep/imhex/plugins/builtin/source/content/views/view_achievements.cpp", "dep/imhex/plugins/builtin/source/content/achievements.cpp", + "dep/imhex/plugins/builtin/source/content/data_processor_nodes.cpp", "dep/imhex/plugins/builtin/source/content/main_menu_items.cpp", - "dep/imhex/plugins/builtin/source/content/welcome_screen.cpp", "dep/imhex/plugins/builtin/source/content/out_of_box_experience.cpp", + "dep/imhex/plugins/builtin/source/content/providers.cpp", "dep/imhex/plugins/builtin/source/content/ui_items.cpp", "dep/imhex/plugins/builtin/source/content/views.cpp", + "dep/imhex/plugins/builtin/source/content/views/view_achievements.cpp", "dep/imhex/plugins/builtin/source/content/views/view_data_processor.cpp", "dep/imhex/plugins/builtin/source/content/views/view_tutorials.cpp", - "dep/imhex/plugins/builtin/source/content/data_processor_nodes.cpp", + "dep/imhex/plugins/builtin/source/content/welcome_screen.cpp", ] + glob( "dep/imhex/plugins/builtin/source/content/data_processor_nodes/*" @@ -433,11 +434,12 @@ plugin( + glob("dep/imhex/plugins/builtin/source/content/tutorials/*"), ) + [ - "./imhex_overrides/welcome.cc", + "./imhex_overrides/main_menu_items.cpp", + "./imhex_overrides/providers.cpp", "./imhex_overrides/stubs.cc", "./imhex_overrides/ui_items.cc", "./imhex_overrides/views.cpp", - "./imhex_overrides/main_menu_items.cpp", + "./imhex_overrides/welcome.cc", ], hdrs=headers_from("dep/imhex/plugins/builtin/include"), romfsdir="dep/imhex/plugins/builtin/romfs", diff --git a/src/gui2/imhex_overrides/providers.cpp b/src/gui2/imhex_overrides/providers.cpp new file mode 100644 index 00000000..6b0db871 --- /dev/null +++ b/src/gui2/imhex_overrides/providers.cpp @@ -0,0 +1,136 @@ +#include + +#include "content/providers/gdb_provider.hpp" +#include "content/providers/file_provider.hpp" +#include "content/providers/null_provider.hpp" +#include "content/providers/disk_provider.hpp" +#include "content/providers/intel_hex_provider.hpp" +#include "content/providers/motorola_srec_provider.hpp" +#include "content/providers/memory_file_provider.hpp" +#include "content/providers/view_provider.hpp" +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +#include + +namespace hex::plugin::builtin { + + void registerProviders() { + + ContentRegistry::Provider::add(false); + ContentRegistry::Provider::add(false); + ContentRegistry::Provider::add(false); + ContentRegistry::Provider::add(false); + + ProjectFile::registerHandler({ + .basePath = "providers", + .required = true, + .load = [](const std::fs::path &basePath, const Tar &tar) { + auto json = nlohmann::json::parse(tar.readString(basePath / "providers.json")); + auto providerIds = json.at("providers").get>(); + + bool success = true; + std::map providerWarnings; + for (const auto &id : providerIds) { + auto providerSettings = nlohmann::json::parse(tar.readString(basePath / fmt::format("{}.json", id))); + + auto providerType = providerSettings.at("type").get(); + auto newProvider = ImHexApi::Provider::createProvider(providerType, true, false); + ON_SCOPE_EXIT { + if (!success) { + for (auto &task : TaskManager::getRunningTasks()) + task->interrupt(); + + TaskManager::runWhenTasksFinished([]{ + for (const auto &provider : ImHexApi::Provider::getProviders()) + ImHexApi::Provider::remove(provider, true); + }); + } + }; + + if (newProvider == nullptr) { + // If a provider is not created, it will be overwritten when saving the project, + // so we should prevent the project from loading at all + ui::ToastError::open( + fmt::format("hex.builtin.popup.error.project.load"_lang, + fmt::format("hex.builtin.popup.error.project.load.create_provider"_lang, providerType) + ) + ); + success = false; + break; + } + + newProvider->setID(id); + bool loaded = false; + try { + newProvider->loadSettings(providerSettings.at("settings")); + loaded = true; + } catch (const std::exception &e){ + providerWarnings[newProvider] = e.what(); + } + if (loaded) { + if (!newProvider->open() || !newProvider->isAvailable() || !newProvider->isReadable()) { + providerWarnings[newProvider] = newProvider->getErrorMessage(); + } else { + EventProviderOpened::post(newProvider); + } + } + } + + std::string warningMessage; + for (const auto &warning : providerWarnings){ + ImHexApi::Provider::remove(warning.first); + warningMessage.append( + fmt::format("\n - {} : {}", warning.first->getName(), warning.second)); + } + + // If no providers were opened, display an error with + // the warnings that happened when opening them + if (ImHexApi::Provider::getProviders().empty()) { + ui::ToastError::open(fmt::format("{}{}", "hex.builtin.popup.error.project.load"_lang, "hex.builtin.popup.error.project.load.no_providers"_lang, warningMessage)); + + return false; + } else { + // Else, if there are warnings, still display them + if (warningMessage.empty()) { + return true; + } else { + ui::ToastWarning::open(fmt::format("hex.builtin.popup.error.project.load.some_providers_failed"_lang, warningMessage)); + } + + return success; + } + }, + .store = [](const std::fs::path &basePath, const Tar &tar) { + std::vector providerIds; + for (const auto &provider : ImHexApi::Provider::getProviders()) { + auto id = provider->getID(); + providerIds.push_back(id); + + nlohmann::json json; + json["type"] = provider->getTypeName(); + json["settings"] = provider->storeSettings({}); + + tar.writeString(basePath / fmt::format("{}.json", id), json.dump(4)); + } + + tar.writeString(basePath / "providers.json", + nlohmann::json({ { "providers", providerIds } }).dump(4) + ); + + return true; + } + }); + } + +} +