UnleashedRecomp/UnleashedRecomp/config.cpp
2024-10-21 22:30:11 +01:00

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");
}
}