2021-12-07 22:47:57 +01:00
|
|
|
#include <hex/api/content_registry.hpp>
|
|
|
|
|
|
|
|
#include "content/providers/gdb_provider.hpp"
|
2021-12-07 23:36:28 +01:00
|
|
|
#include "content/providers/file_provider.hpp"
|
2022-08-10 09:26:48 +02:00
|
|
|
#include "content/providers/null_provider.hpp"
|
2021-12-12 00:42:12 +01:00
|
|
|
#include "content/providers/disk_provider.hpp"
|
2022-08-12 15:11:27 +02:00
|
|
|
#include "content/providers/intel_hex_provider.hpp"
|
|
|
|
#include "content/providers/motorola_srec_provider.hpp"
|
2022-10-21 12:01:28 +02:00
|
|
|
#include "content/providers/memory_file_provider.hpp"
|
2022-11-14 10:02:46 +01:00
|
|
|
#include "content/providers/view_provider.hpp"
|
2023-12-07 23:33:15 +01:00
|
|
|
#include <content/providers/process_memory_provider.hpp>
|
2023-12-26 23:42:22 +01:00
|
|
|
#include <content/providers/base64_provider.hpp>
|
2023-12-23 21:09:41 +01:00
|
|
|
#include <popups/popup_notification.hpp>
|
2021-12-07 22:47:57 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
#include <hex/api/project_file_manager.hpp>
|
2023-11-18 14:50:43 +01:00
|
|
|
#include <hex/api/task_manager.hpp>
|
2022-08-08 21:23:52 +02:00
|
|
|
#include <hex/helpers/fmt.hpp>
|
|
|
|
|
2023-03-12 18:27:29 +01:00
|
|
|
#include <nlohmann/json.hpp>
|
2024-02-26 20:51:08 +01:00
|
|
|
#include <toasts/toast_notification.hpp>
|
2023-03-12 18:27:29 +01:00
|
|
|
|
|
|
|
#include <wolv/utils/guards.hpp>
|
|
|
|
|
2021-12-07 22:47:57 +01:00
|
|
|
namespace hex::plugin::builtin {
|
|
|
|
|
|
|
|
void registerProviders() {
|
|
|
|
|
2022-11-07 00:04:47 +01:00
|
|
|
ContentRegistry::Provider::add<FileProvider>(false);
|
|
|
|
ContentRegistry::Provider::add<NullProvider>(false);
|
2023-10-04 12:00:32 +02:00
|
|
|
#if !defined(OS_WEB)
|
2023-11-10 20:47:08 +01:00
|
|
|
ContentRegistry::Provider::add<DiskProvider>();
|
2023-10-04 12:00:32 +02:00
|
|
|
#endif
|
2022-11-07 00:04:47 +01:00
|
|
|
ContentRegistry::Provider::add<GDBProvider>();
|
|
|
|
ContentRegistry::Provider::add<IntelHexProvider>();
|
|
|
|
ContentRegistry::Provider::add<MotorolaSRECProvider>();
|
2023-12-26 23:42:22 +01:00
|
|
|
ContentRegistry::Provider::add<Base64Provider>();
|
2022-11-07 00:04:47 +01:00
|
|
|
ContentRegistry::Provider::add<MemoryFileProvider>(false);
|
2022-11-14 10:02:46 +01:00
|
|
|
ContentRegistry::Provider::add<ViewProvider>(false);
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2024-06-07 20:32:04 +02:00
|
|
|
#if defined(OS_WINDOWS) || defined(OS_MACOS) || (defined(OS_LINUX) && !defined(OS_FREEBSD))
|
2023-12-07 23:33:15 +01:00
|
|
|
ContentRegistry::Provider::add<ProcessMemoryProvider>();
|
|
|
|
#endif
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
ProjectFile::registerHandler({
|
2023-06-21 20:07:36 +02:00
|
|
|
.basePath = "providers",
|
|
|
|
.required = true,
|
2023-12-27 16:33:49 +01:00
|
|
|
.load = [](const std::fs::path &basePath, const Tar &tar) {
|
2023-06-21 20:07:36 +02:00
|
|
|
auto json = nlohmann::json::parse(tar.readString(basePath / "providers.json"));
|
|
|
|
auto providerIds = json.at("providers").get<std::vector<int>>();
|
|
|
|
|
|
|
|
bool success = true;
|
|
|
|
std::map<hex::prv::Provider*, std::string> providerWarnings;
|
|
|
|
for (const auto &id : providerIds) {
|
|
|
|
auto providerSettings = nlohmann::json::parse(tar.readString(basePath / hex::format("{}.json", id)));
|
|
|
|
|
|
|
|
auto providerType = providerSettings.at("type").get<std::string>();
|
2023-12-06 11:05:13 +01:00
|
|
|
auto newProvider = ImHexApi::Provider::createProvider(providerType, true, false);
|
2023-06-21 20:07:36 +02:00
|
|
|
ON_SCOPE_EXIT {
|
|
|
|
if (!success) {
|
|
|
|
for (auto &task : TaskManager::getRunningTasks())
|
|
|
|
task->interrupt();
|
|
|
|
|
|
|
|
TaskManager::runWhenTasksFinished([]{
|
2024-02-10 23:31:05 +01:00
|
|
|
for (const auto &provider : ImHexApi::Provider::getProviders())
|
2023-04-06 12:44:25 +02:00
|
|
|
ImHexApi::Provider::remove(provider, true);
|
2023-06-21 20:07:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-12-06 11:05:13 +01:00
|
|
|
if (newProvider == nullptr) {
|
2023-10-04 12:00:32 +02:00
|
|
|
// If a provider is not created, it will be overwritten when saving the project,
|
2023-06-21 20:07:36 +02:00
|
|
|
// so we should prevent the project from loading at all
|
2024-02-26 20:51:08 +01:00
|
|
|
ui::ToastError::open(
|
|
|
|
hex::format("hex.builtin.popup.error.project.load"_lang,
|
|
|
|
hex::format("hex.builtin.popup.error.project.load.create_provider"_lang, providerType)
|
|
|
|
)
|
|
|
|
);
|
2023-06-21 20:07:36 +02:00
|
|
|
success = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-12-06 11:05:13 +01:00
|
|
|
newProvider->setID(id);
|
2023-09-12 12:00:00 +02:00
|
|
|
bool loaded = false;
|
|
|
|
try {
|
2023-12-06 11:05:13 +01:00
|
|
|
newProvider->loadSettings(providerSettings.at("settings"));
|
2023-09-12 12:00:00 +02:00
|
|
|
loaded = true;
|
|
|
|
} catch (const std::exception &e){
|
2023-12-06 11:05:13 +01:00
|
|
|
providerWarnings[newProvider] = e.what();
|
2023-09-12 12:00:00 +02:00
|
|
|
}
|
|
|
|
if (loaded) {
|
2023-12-06 11:05:13 +01:00
|
|
|
if (!newProvider->open() || !newProvider->isAvailable() || !newProvider->isReadable()) {
|
|
|
|
providerWarnings[newProvider] = newProvider->getErrorMessage();
|
2023-12-27 16:33:49 +01:00
|
|
|
} else {
|
2023-12-08 10:29:44 +01:00
|
|
|
EventProviderOpened::post(newProvider);
|
2023-12-27 16:33:49 +01:00
|
|
|
}
|
2023-09-12 12:00:00 +02:00
|
|
|
}
|
2023-06-21 20:07:36 +02:00
|
|
|
}
|
|
|
|
|
2024-02-26 20:51:08 +01:00
|
|
|
std::string warningMessage;
|
2023-11-10 20:47:08 +01:00
|
|
|
for (const auto &warning : providerWarnings){
|
2023-06-21 20:07:36 +02:00
|
|
|
ImHexApi::Provider::remove(warning.first);
|
2024-02-26 20:51:08 +01:00
|
|
|
warningMessage.append(
|
2023-06-21 20:07:36 +02:00
|
|
|
hex::format("\n - {} : {}", warning.first->getName(), warning.second));
|
|
|
|
}
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
// If no providers were opened, display an error with
|
2023-06-21 20:07:36 +02:00
|
|
|
// the warnings that happened when opening them
|
2024-02-26 20:51:08 +01:00
|
|
|
if (ImHexApi::Provider::getProviders().empty()) {
|
|
|
|
ui::ToastError::open(hex::format("{}{}", "hex.builtin.popup.error.project.load"_lang, "hex.builtin.popup.error.project.load.no_providers"_lang, warningMessage));
|
2023-06-21 20:07:36 +02:00
|
|
|
|
|
|
|
return false;
|
2024-02-26 20:51:08 +01:00
|
|
|
} else {
|
|
|
|
// Else, if there are warnings, still display them
|
|
|
|
if (warningMessage.empty()) {
|
2023-12-27 16:33:49 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
2024-02-26 20:51:08 +01:00
|
|
|
ui::ToastWarning::open(hex::format("hex.builtin.popup.error.project.load.some_providers_failed"_lang, warningMessage));
|
2023-06-21 20:07:36 +02:00
|
|
|
}
|
2024-02-26 20:51:08 +01:00
|
|
|
|
2023-06-21 20:07:36 +02:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
},
|
2023-12-27 16:33:49 +01:00
|
|
|
.store = [](const std::fs::path &basePath, const Tar &tar) {
|
2023-06-21 20:07:36 +02:00
|
|
|
std::vector<int> providerIds;
|
|
|
|
for (const auto &provider : ImHexApi::Provider::getProviders()) {
|
|
|
|
auto id = provider->getID();
|
|
|
|
providerIds.push_back(id);
|
|
|
|
|
|
|
|
nlohmann::json json;
|
|
|
|
json["type"] = provider->getTypeName();
|
2023-11-30 14:40:07 +01:00
|
|
|
json["settings"] = provider->storeSettings({});
|
2023-06-21 20:07:36 +02:00
|
|
|
|
|
|
|
tar.writeString(basePath / hex::format("{}.json", id), json.dump(4));
|
|
|
|
}
|
|
|
|
|
|
|
|
tar.writeString(basePath / "providers.json",
|
2023-03-23 11:23:07 +01:00
|
|
|
nlohmann::json({ { "providers", providerIds } }).dump(4)
|
2023-06-21 20:07:36 +02:00
|
|
|
);
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2023-06-21 20:07:36 +02:00
|
|
|
return true;
|
|
|
|
}
|
2022-08-08 21:23:52 +02:00
|
|
|
});
|
2021-12-07 22:47:57 +01:00
|
|
|
}
|
|
|
|
|
2023-12-07 23:33:15 +01:00
|
|
|
}
|