Skip to content

Commit 4f46fff

Browse files
committed
Remove a bunch of extraneous Providers.
1 parent 58ea21a commit 4f46fff

File tree

2 files changed

+143
-5
lines changed

2 files changed

+143
-5
lines changed

src/gui2/build.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,28 +416,30 @@ def plugin(name, id, srcs, hdrs, romfsdir, deps):
416416
srcs=sources_from(
417417
"dep/imhex/plugins/builtin/source",
418418
except_for=[
419-
"dep/imhex/plugins/builtin/source/content/views/view_achievements.cpp",
420419
"dep/imhex/plugins/builtin/source/content/achievements.cpp",
420+
"dep/imhex/plugins/builtin/source/content/data_processor_nodes.cpp",
421421
"dep/imhex/plugins/builtin/source/content/main_menu_items.cpp",
422-
"dep/imhex/plugins/builtin/source/content/welcome_screen.cpp",
423422
"dep/imhex/plugins/builtin/source/content/out_of_box_experience.cpp",
423+
"dep/imhex/plugins/builtin/source/content/providers.cpp",
424424
"dep/imhex/plugins/builtin/source/content/ui_items.cpp",
425425
"dep/imhex/plugins/builtin/source/content/views.cpp",
426+
"dep/imhex/plugins/builtin/source/content/views/view_achievements.cpp",
426427
"dep/imhex/plugins/builtin/source/content/views/view_data_processor.cpp",
427428
"dep/imhex/plugins/builtin/source/content/views/view_tutorials.cpp",
428-
"dep/imhex/plugins/builtin/source/content/data_processor_nodes.cpp",
429+
"dep/imhex/plugins/builtin/source/content/welcome_screen.cpp",
429430
]
430431
+ glob(
431432
"dep/imhex/plugins/builtin/source/content/data_processor_nodes/*"
432433
)
433434
+ glob("dep/imhex/plugins/builtin/source/content/tutorials/*"),
434435
)
435436
+ [
436-
"./imhex_overrides/welcome.cc",
437+
"./imhex_overrides/main_menu_items.cpp",
438+
"./imhex_overrides/providers.cpp",
437439
"./imhex_overrides/stubs.cc",
438440
"./imhex_overrides/ui_items.cc",
439441
"./imhex_overrides/views.cpp",
440-
"./imhex_overrides/main_menu_items.cpp",
442+
"./imhex_overrides/welcome.cc",
441443
],
442444
hdrs=headers_from("dep/imhex/plugins/builtin/include"),
443445
romfsdir="dep/imhex/plugins/builtin/romfs",
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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

Comments
 (0)