Merge pull request #13282 from jordan-woyak/onion-tweaks

Core/Config: Replace some std::pair with struct to make code more readable.
This commit is contained in:
JosJuice 2025-01-19 12:20:48 +01:00 committed by GitHub
commit dabb4350bd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 38 additions and 34 deletions

View File

@ -60,9 +60,22 @@ std::vector<std::string> GetGameIniFilenames(const std::string& id, std::optiona
return filenames;
}
struct SectionKey
{
std::string section;
std::string key;
friend auto operator<=>(const SectionKey&, const SectionKey&) = default;
};
struct SystemSection
{
Config::System system;
std::string section;
};
using Location = Config::Location;
using INIToLocationMap = std::map<std::pair<std::string, std::string>, Location>;
using INIToSectionMap = std::map<std::string, std::pair<Config::System, std::string>>;
using INIToLocationMap = std::map<SectionKey, Location>;
using INIToSectionMap = std::map<std::string, SystemSection>;
// This is a mapping from the legacy section-key pairs to Locations.
// New settings do not need to be added to this mapping.
@ -123,7 +136,7 @@ static Location MapINIToRealLocation(const std::string& section, const std::stri
static const INIToSectionMap& ini_to_section = GetINIToSectionMap();
const auto it2 = ini_to_section.find(section);
if (it2 != ini_to_section.end())
return {it2->second.first, it2->second.second, key};
return {it2->second.system, it2->second.section, key};
// Attempt to load it as a configuration option
// It will be in the format of '<System>.<Section>'
@ -144,20 +157,19 @@ static Location MapINIToRealLocation(const std::string& section, const std::stri
return {Config::System::Main, "", ""};
}
static std::pair<std::string, std::string> GetINILocationFromConfig(const Location& location)
static SectionKey GetINILocationFromConfig(const Location& location)
{
static const INIToLocationMap& ini_to_location = GetINIToLocationMap();
const auto it = std::find_if(ini_to_location.begin(), ini_to_location.end(),
[&location](const auto& entry) { return entry.second == location; });
if (it != ini_to_location.end())
return it->first;
for (auto& [ini_location, config_location] : GetINIToLocationMap())
{
if (config_location == location)
return ini_location;
}
static const INIToSectionMap& ini_to_section = GetINIToSectionMap();
const auto it2 = std::ranges::find_if(ini_to_section, [&location](const auto& entry) {
return entry.second.first == location.system && entry.second.second == location.section;
});
if (it2 != ini_to_section.end())
return {it2->first, location.key};
for (auto& [section_name, sys_sec] : GetINIToSectionMap())
{
if (sys_sec.system == location.system && sys_sec.section == location.section)
return {section_name, location.key};
}
return {Config::GetSystemName(location.system) + "." + location.section, location.key};
}
@ -238,12 +250,10 @@ private:
profile_ini.Load(ini_path);
const auto* ini_section = profile_ini.GetOrCreateSection("Profile");
const auto& section_map = ini_section->GetValues();
for (const auto& value : section_map)
for (const auto& [key, value] : ini_section->GetValues())
{
Config::Location location{std::get<2>(use_data), std::get<1>(use_data) + num,
value.first};
layer->Set(location, value.second);
Config::Location location{std::get<2>(use_data), std::get<1>(use_data) + num, key};
layer->Set(location, value);
}
}
}
@ -254,12 +264,9 @@ private:
{
const std::string section_name = section.GetName();
// Regular key,value pairs
const auto& section_map = section.GetValues();
for (const auto& value : section_map)
for (const auto& [key, value] : section.GetValues())
{
const auto location = MapINIToRealLocation(section_name, value.first);
const auto location = MapINIToRealLocation(section_name, key);
if (location.section.empty() && location.key.empty())
continue;
@ -267,7 +274,7 @@ private:
if (location.system == Config::System::Session)
continue;
layer->Set(location, value.second);
layer->Set(location, value);
}
}
@ -284,26 +291,23 @@ void INIGameConfigLayerLoader::Save(Config::Layer* layer)
for (const std::string& file_name : GetGameIniFilenames(m_id, m_revision))
ini.Load(File::GetUserPath(D_GAMESETTINGS_IDX) + file_name, true);
for (const auto& config : layer->GetLayerMap())
for (const auto& [location, value] : layer->GetLayerMap())
{
const Config::Location& location = config.first;
const std::optional<std::string>& value = config.second;
if (!IsSettingSaveable(location) || location.system == Config::System::Session)
continue;
const auto ini_location = GetINILocationFromConfig(location);
if (ini_location.first.empty() && ini_location.second.empty())
if (ini_location.section.empty() && ini_location.key.empty())
continue;
if (value)
{
auto* ini_section = ini.GetOrCreateSection(ini_location.first);
ini_section->Set(ini_location.second, *value);
auto* ini_section = ini.GetOrCreateSection(ini_location.section);
ini_section->Set(ini_location.key, *value);
}
else
{
ini.DeleteKey(ini_location.first, ini_location.second);
ini.DeleteKey(ini_location.section, ini_location.key);
}
}