From b44aaf8a8693fd1309cb5a6a7a8364d1d1dcb1fb Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Fri, 15 Mar 2024 22:57:00 -0500 Subject: [PATCH] Core/Config: Replace some std::pair with struct to make code more readable. --- .../Core/ConfigLoaders/GameConfigLoader.cpp | 50 ++++++++++++------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/Source/Core/Core/ConfigLoaders/GameConfigLoader.cpp b/Source/Core/Core/ConfigLoaders/GameConfigLoader.cpp index 020e8d6da3..5ee8216be0 100644 --- a/Source/Core/Core/ConfigLoaders/GameConfigLoader.cpp +++ b/Source/Core/Core/ConfigLoaders/GameConfigLoader.cpp @@ -60,9 +60,22 @@ std::vector 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, Location>; -using INIToSectionMap = std::map>; +using INIToLocationMap = std::map; +using INIToSectionMap = std::map; // 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 '.
' @@ -144,20 +157,19 @@ static Location MapINIToRealLocation(const std::string& section, const std::stri return {Config::System::Main, "", ""}; } -static std::pair 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}; } @@ -293,17 +305,17 @@ void INIGameConfigLayerLoader::Save(Config::Layer* layer) 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); } }