diff --git a/Source/Core/Core/CheatCodes.h b/Source/Core/Core/CheatCodes.h index fc73b10f56..68de210dbd 100644 --- a/Source/Core/Core/CheatCodes.h +++ b/Source/Core/Core/CheatCodes.h @@ -3,12 +3,14 @@ #pragma once +#include #include #include #include "Common/IniFile.h" +#include "Common/StringUtil.h" -template +template void ReadEnabledOrDisabled(const Common::IniFile& ini, const std::string& section, bool enabled, std::vector* codes) { @@ -20,19 +22,48 @@ void ReadEnabledOrDisabled(const Common::IniFile& ini, const std::string& sectio if (line.empty() || line[0] != '$') continue; + // Exclude the initial '$' from the comparison. + const auto name = StripWhitespace(std::string_view{line}.substr(1)); + + bool matched{false}; + for (T& code : *codes) { - // Exclude the initial '$' from the comparison. - if (line.compare(1, std::string::npos, code.name) == 0) + if (name == code.name) + { code.enabled = enabled; + matched = true; + break; + } + } + + if (!matched && disregard_creator_in_name) + { + // For backwards compatibility, where certain cheat code types (namedly Gecko cheat codes) + // would be stored in the _Enabled/_Disabled sections without including the creator name, + // there will be a second attempt to match the parsed line with any of the cheat codes in the + // list after disregarding the potential creator name. + static const std::regex s_regex("(.*)(\\[.+\\])"); + for (T& code : *codes) + { + const std::string codeNameWithoutCreator{ + StripWhitespace(std::regex_replace(code.name, s_regex, "$1"))}; + + if (name == codeNameWithoutCreator) + { + code.enabled = enabled; + matched = true; + break; + } + } } } } -template +template void ReadEnabledAndDisabled(const Common::IniFile& ini, const std::string& section, std::vector* codes) { - ReadEnabledOrDisabled(ini, section + "_Enabled", true, codes); - ReadEnabledOrDisabled(ini, section + "_Disabled", false, codes); + ReadEnabledOrDisabled(ini, section + "_Enabled", true, codes); + ReadEnabledOrDisabled(ini, section + "_Disabled", false, codes); } diff --git a/Source/Core/Core/GeckoCodeConfig.cpp b/Source/Core/Core/GeckoCodeConfig.cpp index 9729ad731c..bddd859d19 100644 --- a/Source/Core/Core/GeckoCodeConfig.cpp +++ b/Source/Core/Core/GeckoCodeConfig.cpp @@ -189,7 +189,7 @@ std::vector LoadCodes(const Common::IniFile& globalIni, const Common: gcodes.push_back(gcode); } - ReadEnabledAndDisabled(*ini, "Gecko", &gcodes); + ReadEnabledAndDisabled(*ini, "Gecko", &gcodes); if (ini == &globalIni) {