1
0
mirror of synced 2024-11-15 11:33:23 +01:00
ImHex/plugins/libimhex/source/helpers/content_registry.cpp

113 lines
3.8 KiB
C++
Raw Normal View History

#include <helpers/content_registry.hpp>
#include <helpers/shared_data.hpp>
#include <filesystem>
#include <fstream>
namespace hex {
/* Settings */
void ContentRegistry::Settings::load() {
std::ifstream settingsFile(std::filesystem::path((SharedData::mainArgv)[0]).parent_path() / "settings.json");
if (settingsFile.good())
settingsFile >> getSettingsData();
}
void ContentRegistry::Settings::store() {
std::ofstream settingsFile(std::filesystem::path((SharedData::mainArgv)[0]).parent_path() / "settings.json", std::ios::trunc);
settingsFile << getSettingsData();
}
void ContentRegistry::Settings::add(std::string_view category, std::string_view name, s64 defaultValue, const std::function<bool(nlohmann::json&)> &callback) {
ContentRegistry::Settings::getEntries()[category.data()].emplace_back(Entry{ name.data(), callback });
2021-01-12 16:50:15 +01:00
auto &json = getSettingsData();
if (!json.contains(category.data()))
json[category.data()] = nlohmann::json::object();
if (!json[category.data()].contains(name.data()))
json[category.data()][name.data()] = defaultValue;
}
void ContentRegistry::Settings::add(std::string_view category, std::string_view name, std::string_view defaultValue, const std::function<bool(nlohmann::json&)> &callback) {
ContentRegistry::Settings::getEntries()[category.data()].emplace_back(Entry{ name.data(), callback });
2021-01-12 16:50:15 +01:00
getSettingsData()[category.data()] = nlohmann::json::object();
getSettingsData()[category.data()][name.data()] = defaultValue;
}
std::map<std::string, std::vector<ContentRegistry::Settings::Entry>>& ContentRegistry::Settings::getEntries() {
return SharedData::settingsEntries;
}
nlohmann::json& ContentRegistry::Settings::getSettingsData() {
return SharedData::settingsJson;
}
2021-01-11 21:11:03 +01:00
/* Events */
auto ContentRegistry::Events::get(std::string_view name) {
auto &customEvents = SharedData::customEvents;
auto &lastId = SharedData::customEventsLastId;
2021-01-11 21:11:03 +01:00
if (!customEvents.contains(name.data())) {
customEvents[name.data()] = static_cast<hex::Events>(lastId);
lastId++;
}
return customEvents[name.data()];
}
/* Command Palette Commands */
void ContentRegistry::CommandPaletteCommands::add(ContentRegistry::CommandPaletteCommands::Type type, std::string_view command, std::string_view description, const std::function<std::string(std::string)> &callback) {
2021-01-12 16:50:15 +01:00
getEntries().push_back(ContentRegistry::CommandPaletteCommands::Entry{ type, command.data(), description.data(), callback });
}
2021-01-12 16:50:15 +01:00
std::vector<ContentRegistry::CommandPaletteCommands::Entry>& ContentRegistry::CommandPaletteCommands::getEntries() {
return SharedData::commandPaletteCommands;
}
/* Pattern Language Functions */
void ContentRegistry::PatternLanguageFunctions::add(std::string_view name, u32 parameterCount, const std::function<hex::lang::ASTNode*(std::vector<hex::lang::ASTNode*>)> &func) {
2021-01-12 16:50:15 +01:00
getEntries()[name.data()] = Function{ parameterCount, func };
}
2021-01-12 16:50:15 +01:00
std::map<std::string, ContentRegistry::PatternLanguageFunctions::Function>& ContentRegistry::PatternLanguageFunctions::getEntries() {
return SharedData::patternLanguageFunctions;
}
2021-01-12 16:50:15 +01:00
/* Views */
View* ContentRegistry::Views::add(View *view) {
auto &views = getEntries();
views.push_back(view);
return views.back();
}
std::vector<View*>& ContentRegistry::Views::getEntries() {
return SharedData::views;
2021-01-12 16:50:15 +01:00
}
/* Tools */
void ContentRegistry::Tools::add(const std::function<void()> &function) {
getEntries().push_back(function);
}
std::vector<std::function<void()>>& ContentRegistry::Tools::getEntries() {
return SharedData::tools;
2021-01-12 16:50:15 +01:00
}
}