mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-05-03 10:31:36 -05:00
Allow reading and writing settings through code
This commit is contained in:
@@ -42,6 +42,14 @@ namespace hex {
|
||||
static void add(std::string_view category, std::string_view name, s64 defaultValue, const std::function<bool(nlohmann::json&)> &callback);
|
||||
static void add(std::string_view category, std::string_view name, std::string_view defaultValue, const std::function<bool(nlohmann::json&)> &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<std::string>& 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<std::string> read(std::string_view category, std::string_view name, const std::vector<std::string>& defaultValue = { });
|
||||
|
||||
static std::map<std::string, std::vector<Entry>>& getEntries();
|
||||
static nlohmann::json& getSettingsData();
|
||||
};
|
||||
|
||||
@@ -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<std::string>& 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<s64>();
|
||||
}
|
||||
|
||||
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::string>();
|
||||
}
|
||||
|
||||
std::vector<std::string> ContentRegistry::Settings::read(std::string_view category, std::string_view name, const std::vector<std::string>& 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::vector<std::string>>();
|
||||
}
|
||||
|
||||
|
||||
std::map<std::string, std::vector<ContentRegistry::Settings::Entry>>& ContentRegistry::Settings::getEntries() {
|
||||
return SharedData::settingsEntries;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user