config: make definitions global

This commit is contained in:
Hyper 2024-11-15 19:57:44 +00:00
parent 784d3973f4
commit b354c6123d
3 changed files with 8 additions and 8 deletions

View file

@ -14,7 +14,7 @@ void Config::Load()
{
auto toml = toml::parse_file(configPath.string());
for (auto def : Definitions)
for (auto def : g_configDefs)
{
def->ReadValue(toml);
#if _DEBUG
@ -38,7 +38,7 @@ void Config::Save()
std::string result;
std::string section;
for (auto def : Definitions)
for (auto def : g_configDefs)
{
auto isFirstSection = section.empty();
auto isDefWithSection = section != def->GetSection();

View file

@ -5,8 +5,6 @@
class Config
{
public:
inline static std::vector<ConfigDefBase*> Definitions{};
CONFIG_DEFINE_ENUM("System", ELanguage, Language, ELanguage::English);
CONFIG_DEFINE("System", bool, Hints, true);
CONFIG_DEFINE("System", bool, ControlTutorial, true);

View file

@ -42,6 +42,8 @@ public:
virtual std::string ToString() const = 0;
};
inline static std::vector<ConfigDefBase*> g_configDefs{};
template<typename T, bool isMenuOption = true>
class ConfigDef : public ConfigDefBase
{
@ -61,7 +63,7 @@ public:
ConfigDef(std::string section, std::string name, T defaultValue)
: Section(section), Name(name), DefaultValue(defaultValue)
{
Config::Definitions.emplace_back(this);
g_configDefs.emplace_back(this);
}
ConfigDef(std::string section, std::string name, T defaultValue, std::unordered_map<std::string, T> enumTemplate)
@ -70,19 +72,19 @@ public:
for (const auto& pair : EnumTemplate)
EnumTemplateReverse[pair.second] = pair.first;
Config::Definitions.emplace_back(this);
g_configDefs.emplace_back(this);
}
ConfigDef(std::string section, std::string name, T defaultValue, std::function<void(ConfigDef<T, isMenuOption>*)> readCallback)
: Section(section), Name(name), DefaultValue(defaultValue), ReadCallback(readCallback)
{
Config::Definitions.emplace_back(this);
g_configDefs.emplace_back(this);
}
ConfigDef(std::string section, std::string name, T defaultValue, std::function<void(ConfigDef<T, isMenuOption>*, const toml::v3::table&)> readImpl)
: Section(section), Name(name), DefaultValue(defaultValue), ReadImpl(readImpl)
{
Config::Definitions.emplace_back(this);
g_configDefs.emplace_back(this);
}
void ReadValue(toml::v3::ex::parse_result& toml) override