|
| 1 | +#include <hex/api/content_registry/provider.hpp> |
| 2 | + |
| 3 | +#include "content/providers/gdb_provider.hpp" |
| 4 | +#include "content/providers/file_provider.hpp" |
| 5 | +#include "content/providers/null_provider.hpp" |
| 6 | +#include "content/providers/disk_provider.hpp" |
| 7 | +#include "content/providers/intel_hex_provider.hpp" |
| 8 | +#include "content/providers/motorola_srec_provider.hpp" |
| 9 | +#include "content/providers/memory_file_provider.hpp" |
| 10 | +#include "content/providers/view_provider.hpp" |
| 11 | +#include <content/providers/process_memory_provider.hpp> |
| 12 | +#include <content/providers/base64_provider.hpp> |
| 13 | +#include <content/providers/udp_provider.hpp> |
| 14 | +#include <popups/popup_notification.hpp> |
| 15 | + |
| 16 | +#include <hex/api/project_file_manager.hpp> |
| 17 | +#include <hex/api/task_manager.hpp> |
| 18 | +#include <hex/helpers/fmt.hpp> |
| 19 | + |
| 20 | +#include <nlohmann/json.hpp> |
| 21 | +#include <toasts/toast_notification.hpp> |
| 22 | + |
| 23 | +#include <wolv/utils/guards.hpp> |
| 24 | + |
| 25 | +namespace hex::plugin::builtin { |
| 26 | + |
| 27 | + void registerProviders() { |
| 28 | + |
| 29 | + ContentRegistry::Provider::add<FileProvider>(false); |
| 30 | + ContentRegistry::Provider::add<NullProvider>(false); |
| 31 | + ContentRegistry::Provider::add<MemoryFileProvider>(false); |
| 32 | + ContentRegistry::Provider::add<ViewProvider>(false); |
| 33 | + |
| 34 | + ProjectFile::registerHandler({ |
| 35 | + .basePath = "providers", |
| 36 | + .required = true, |
| 37 | + .load = [](const std::fs::path &basePath, const Tar &tar) { |
| 38 | + auto json = nlohmann::json::parse(tar.readString(basePath / "providers.json")); |
| 39 | + auto providerIds = json.at("providers").get<std::vector<int>>(); |
| 40 | + |
| 41 | + bool success = true; |
| 42 | + std::map<hex::prv::Provider*, std::string> providerWarnings; |
| 43 | + for (const auto &id : providerIds) { |
| 44 | + auto providerSettings = nlohmann::json::parse(tar.readString(basePath / fmt::format("{}.json", id))); |
| 45 | + |
| 46 | + auto providerType = providerSettings.at("type").get<std::string>(); |
| 47 | + auto newProvider = ImHexApi::Provider::createProvider(providerType, true, false); |
| 48 | + ON_SCOPE_EXIT { |
| 49 | + if (!success) { |
| 50 | + for (auto &task : TaskManager::getRunningTasks()) |
| 51 | + task->interrupt(); |
| 52 | + |
| 53 | + TaskManager::runWhenTasksFinished([]{ |
| 54 | + for (const auto &provider : ImHexApi::Provider::getProviders()) |
| 55 | + ImHexApi::Provider::remove(provider, true); |
| 56 | + }); |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + if (newProvider == nullptr) { |
| 61 | + // If a provider is not created, it will be overwritten when saving the project, |
| 62 | + // so we should prevent the project from loading at all |
| 63 | + ui::ToastError::open( |
| 64 | + fmt::format("hex.builtin.popup.error.project.load"_lang, |
| 65 | + fmt::format("hex.builtin.popup.error.project.load.create_provider"_lang, providerType) |
| 66 | + ) |
| 67 | + ); |
| 68 | + success = false; |
| 69 | + break; |
| 70 | + } |
| 71 | + |
| 72 | + newProvider->setID(id); |
| 73 | + bool loaded = false; |
| 74 | + try { |
| 75 | + newProvider->loadSettings(providerSettings.at("settings")); |
| 76 | + loaded = true; |
| 77 | + } catch (const std::exception &e){ |
| 78 | + providerWarnings[newProvider] = e.what(); |
| 79 | + } |
| 80 | + if (loaded) { |
| 81 | + if (!newProvider->open() || !newProvider->isAvailable() || !newProvider->isReadable()) { |
| 82 | + providerWarnings[newProvider] = newProvider->getErrorMessage(); |
| 83 | + } else { |
| 84 | + EventProviderOpened::post(newProvider); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + std::string warningMessage; |
| 90 | + for (const auto &warning : providerWarnings){ |
| 91 | + ImHexApi::Provider::remove(warning.first); |
| 92 | + warningMessage.append( |
| 93 | + fmt::format("\n - {} : {}", warning.first->getName(), warning.second)); |
| 94 | + } |
| 95 | + |
| 96 | + // If no providers were opened, display an error with |
| 97 | + // the warnings that happened when opening them |
| 98 | + if (ImHexApi::Provider::getProviders().empty()) { |
| 99 | + ui::ToastError::open(fmt::format("{}{}", "hex.builtin.popup.error.project.load"_lang, "hex.builtin.popup.error.project.load.no_providers"_lang, warningMessage)); |
| 100 | + |
| 101 | + return false; |
| 102 | + } else { |
| 103 | + // Else, if there are warnings, still display them |
| 104 | + if (warningMessage.empty()) { |
| 105 | + return true; |
| 106 | + } else { |
| 107 | + ui::ToastWarning::open(fmt::format("hex.builtin.popup.error.project.load.some_providers_failed"_lang, warningMessage)); |
| 108 | + } |
| 109 | + |
| 110 | + return success; |
| 111 | + } |
| 112 | + }, |
| 113 | + .store = [](const std::fs::path &basePath, const Tar &tar) { |
| 114 | + std::vector<int> providerIds; |
| 115 | + for (const auto &provider : ImHexApi::Provider::getProviders()) { |
| 116 | + auto id = provider->getID(); |
| 117 | + providerIds.push_back(id); |
| 118 | + |
| 119 | + nlohmann::json json; |
| 120 | + json["type"] = provider->getTypeName(); |
| 121 | + json["settings"] = provider->storeSettings({}); |
| 122 | + |
| 123 | + tar.writeString(basePath / fmt::format("{}.json", id), json.dump(4)); |
| 124 | + } |
| 125 | + |
| 126 | + tar.writeString(basePath / "providers.json", |
| 127 | + nlohmann::json({ { "providers", providerIds } }).dump(4) |
| 128 | + ); |
| 129 | + |
| 130 | + return true; |
| 131 | + } |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | +} |
| 136 | + |
0 commit comments