From b44aaf8a8693fd1309cb5a6a7a8364d1d1dcb1fb Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Fri, 15 Mar 2024 22:57:00 -0500 Subject: [PATCH 1/2] 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); } } From 28ce81f09b2bf8eafad8b9fef55088fb30de923d Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Fri, 17 Jan 2025 22:21:39 -0600 Subject: [PATCH 2/2] Core/Config: Use structured bindings for cleaner std::map/pair usage. --- .../Core/ConfigLoaders/GameConfigLoader.cpp | 22 ++++++------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/Source/Core/Core/ConfigLoaders/GameConfigLoader.cpp b/Source/Core/Core/ConfigLoaders/GameConfigLoader.cpp index 5ee8216be0..a922a9ec38 100644 --- a/Source/Core/Core/ConfigLoaders/GameConfigLoader.cpp +++ b/Source/Core/Core/ConfigLoaders/GameConfigLoader.cpp @@ -250,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); } } } @@ -266,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; @@ -279,7 +274,7 @@ private: if (location.system == Config::System::Session) continue; - layer->Set(location, value.second); + layer->Set(location, value); } } @@ -296,11 +291,8 @@ 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& value = config.second; - if (!IsSettingSaveable(location) || location.system == Config::System::Session) continue;