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"
|
2021-12-07 22:47:57 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
#include <hex/api/project_file_manager.hpp>
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <hex/helpers/fmt.hpp>
|
|
|
|
|
2021-12-07 22:47:57 +01:00
|
|
|
namespace hex::plugin::builtin {
|
|
|
|
|
|
|
|
void registerProviders() {
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
ContentRegistry::Provider::add<prv::FileProvider>(false);
|
2022-08-10 09:26:48 +02:00
|
|
|
ContentRegistry::Provider::add<prv::NullProvider>(false);
|
2022-08-08 21:23:52 +02:00
|
|
|
ContentRegistry::Provider::add<prv::DiskProvider>();
|
2022-08-10 09:26:48 +02:00
|
|
|
ContentRegistry::Provider::add<prv::GDBProvider>();
|
2022-08-12 15:11:27 +02:00
|
|
|
ContentRegistry::Provider::add<prv::IntelHexProvider>();
|
|
|
|
ContentRegistry::Provider::add<prv::MotorolaSRECProvider>();
|
2022-08-08 21:23:52 +02:00
|
|
|
|
|
|
|
ProjectFile::registerHandler({
|
|
|
|
.basePath = "providers",
|
2022-09-20 15:33:36 +02:00
|
|
|
.required = true,
|
2022-08-08 21:23:52 +02:00
|
|
|
.load = [](const std::fs::path &basePath, Tar &tar) {
|
|
|
|
auto json = nlohmann::json::parse(tar.readString(basePath / "providers.json"));
|
|
|
|
auto providerIds = json["providers"].get<std::vector<int>>();
|
|
|
|
|
|
|
|
bool success = true;
|
|
|
|
for (const auto &id : providerIds) {
|
|
|
|
auto providerSettings = nlohmann::json::parse(tar.readString(basePath / hex::format("{}.json", id)));
|
|
|
|
|
2022-08-14 19:13:13 +02:00
|
|
|
auto provider = ImHexApi::Provider::createProvider(providerSettings["type"].get<std::string>(), true);
|
2022-09-20 15:33:36 +02:00
|
|
|
ON_SCOPE_EXIT { if (!success) ImHexApi::Provider::remove(provider, true); };
|
2022-08-08 21:23:52 +02:00
|
|
|
if (provider == nullptr) {
|
|
|
|
success = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-09-22 09:05:09 +02:00
|
|
|
provider->setID(id);
|
2022-08-08 21:23:52 +02:00
|
|
|
provider->loadSettings(providerSettings["settings"]);
|
2022-09-20 15:33:36 +02:00
|
|
|
if (!provider->open() || !provider->isAvailable() || !provider->isReadable())
|
2022-08-08 21:23:52 +02:00
|
|
|
success = false;
|
2022-09-04 11:16:20 +02:00
|
|
|
else
|
|
|
|
EventManager::post<EventProviderOpened>(provider);
|
2022-08-08 21:23:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
},
|
|
|
|
.store = [](const std::fs::path &basePath, Tar &tar) {
|
|
|
|
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();
|
|
|
|
json["settings"] = provider->storeSettings();
|
|
|
|
|
|
|
|
tar.write(basePath / hex::format("{}.json", id), json.dump(4));
|
|
|
|
}
|
|
|
|
|
|
|
|
tar.write(basePath / "providers.json",
|
2022-08-10 09:26:48 +02:00
|
|
|
nlohmann::json({ {"providers", providerIds } }).dump(4)
|
2022-08-08 21:23:52 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2021-12-07 22:47:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|