mirror of
https://github.com/hedge-dev/UnleashedRecomp.git
synced 2025-10-30 07:11:05 +00:00
62 lines
1.2 KiB
C++
62 lines
1.2 KiB
C++
#include "config.h"
|
|
|
|
void Config::Load()
|
|
{
|
|
auto configPath = GetConfigPath();
|
|
|
|
if (!std::filesystem::exists(configPath))
|
|
{
|
|
Config::Save();
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
auto toml = toml::parse_file(configPath.string());
|
|
|
|
for (auto def : Definitions)
|
|
def->ReadValue(toml);
|
|
}
|
|
catch (toml::parse_error& err)
|
|
{
|
|
printf("Failed to parse configuration: %s\n", err.what());
|
|
}
|
|
}
|
|
|
|
void Config::Save()
|
|
{
|
|
auto userPath = GetUserPath();
|
|
|
|
if (!std::filesystem::exists(userPath))
|
|
std::filesystem::create_directory(userPath);
|
|
|
|
std::string result;
|
|
std::string section;
|
|
|
|
for (auto def : Definitions)
|
|
{
|
|
auto isFirstSection = section.empty();
|
|
auto isDefWithSection = section != def->GetSection();
|
|
auto tomlDef = def->GetDefinition(isDefWithSection);
|
|
|
|
section = def->GetSection();
|
|
|
|
// Don't output prefix space for first section.
|
|
if (!isFirstSection && isDefWithSection)
|
|
result += '\n';
|
|
|
|
result += tomlDef + '\n';
|
|
}
|
|
|
|
std::ofstream out(GetConfigPath());
|
|
|
|
if (out.is_open())
|
|
{
|
|
out << result;
|
|
out.close();
|
|
}
|
|
else
|
|
{
|
|
printf("Failed to write configuration.\n");
|
|
}
|
|
}
|