diff --git a/plugins/libimhex/include/hex/api/content_registry.hpp b/plugins/libimhex/include/hex/api/content_registry.hpp index f0feda328..d7ba8c7e8 100644 --- a/plugins/libimhex/include/hex/api/content_registry.hpp +++ b/plugins/libimhex/include/hex/api/content_registry.hpp @@ -42,6 +42,14 @@ namespace hex { static void add(std::string_view category, std::string_view name, s64 defaultValue, const std::function &callback); static void add(std::string_view category, std::string_view name, std::string_view defaultValue, const std::function &callback); + static void write(std::string_view category, std::string_view name, s64 value); + static void write(std::string_view category, std::string_view name, std::string_view value); + static void write(std::string_view category, std::string_view name, const std::vector& value); + + static s64 read(std::string_view category, std::string_view name, s64 defaultValue); + static std::string read(std::string_view category, std::string_view name, std::string_view defaultValue); + static std::vector read(std::string_view category, std::string_view name, const std::vector& defaultValue = { }); + static std::map>& getEntries(); static nlohmann::json& getSettingsData(); }; diff --git a/plugins/libimhex/source/api/content_registry.cpp b/plugins/libimhex/source/api/content_registry.cpp index d0f30c475..6ad99c2db 100644 --- a/plugins/libimhex/source/api/content_registry.cpp +++ b/plugins/libimhex/source/api/content_registry.cpp @@ -39,6 +39,68 @@ namespace hex { getSettingsData()[category.data()][name.data()] = defaultValue; } + void ContentRegistry::Settings::write(std::string_view category, std::string_view name, s64 value) { + auto &json = getSettingsData(); + + if (!json.contains(category.data())) + json[category.data()] = nlohmann::json::object(); + + json[category.data()][name.data()] = value; + } + + void ContentRegistry::Settings::write(std::string_view category, std::string_view name, std::string_view value) { + auto &json = getSettingsData(); + + if (!json.contains(category.data())) + json[category.data()] = nlohmann::json::object(); + + json[category.data()][name.data()] = value; + } + + void ContentRegistry::Settings::write(std::string_view category, std::string_view name, const std::vector& value) { + auto &json = getSettingsData(); + + if (!json.contains(category.data())) + json[category.data()] = nlohmann::json::object(); + + json[category.data()][name.data()] = value; + } + + + s64 ContentRegistry::Settings::read(std::string_view category, std::string_view name, s64 defaultValue) { + auto &json = getSettingsData(); + + if (!json.contains(category.data())) + return defaultValue; + if (!json[category.data()].contains(name.data())) + return defaultValue; + + return json[category.data()][name.data()].get(); + } + + std::string ContentRegistry::Settings::read(std::string_view category, std::string_view name, std::string_view defaultValue) { + auto &json = getSettingsData(); + + if (!json.contains(category.data())) + return defaultValue.data(); + if (!json[category.data()].contains(name.data())) + return defaultValue.data(); + + return json[category.data()][name.data()].get(); + } + + std::vector ContentRegistry::Settings::read(std::string_view category, std::string_view name, const std::vector& defaultValue) { + auto &json = getSettingsData(); + + if (!json.contains(category.data())) + return defaultValue; + if (!json[category.data()].contains(name.data())) + return defaultValue; + + return json[category.data()][name.data()].get>(); + } + + std::map>& ContentRegistry::Settings::getEntries() { return SharedData::settingsEntries; }