From e7cfc699753ef4d7ced59e7b51583ea5e2d6f1de Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Fri, 25 Sep 2020 22:00:34 +1000 Subject: [PATCH] Cheats: Fix parsing of Retroarch cheats --- src/core/cheats.cpp | 19 ++++++++++--------- src/frontend-common/common_host_interface.cpp | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/core/cheats.cpp b/src/core/cheats.cpp index bc9d9c17b..680473b47 100644 --- a/src/core/cheats.cpp +++ b/src/core/cheats.cpp @@ -149,7 +149,7 @@ bool CheatList::LoadFromLibretroFile(const char* filename) while (*value_start != '\0' && std::isspace(*value_start)) value_start++; - if (value_start == end) + if (*value_start == '\0') continue; char* value_end = value_start + std::strlen(value_start) - 1; @@ -159,9 +159,6 @@ bool CheatList::LoadFromLibretroFile(const char* filename) value_end--; } - if (value_start == value_end) - continue; - if (*value_start == '\"') { if (*value_end != '\"') @@ -178,7 +175,7 @@ bool CheatList::LoadFromLibretroFile(const char* filename) return false; const std::string* num_cheats_value = FindKey(kvp, "cheats"); - const u32 num_cheats = StringUtil::FromChars(*num_cheats_value).value_or(0); + const u32 num_cheats = num_cheats_value ? StringUtil::FromChars(*num_cheats_value).value_or(0) : 0; if (num_cheats == 0) return false; @@ -204,6 +201,11 @@ bool CheatList::LoadFromLibretroFile(const char* filename) return !m_codes.empty(); } +static bool IsLibretroSeparator(char ch) +{ + return (ch == ' ' || ch == '-' || ch == ':' || ch == '+'); +} + bool CheatList::ParseLibretroCheat(CheatCode* cc, const char* line) { const char* current_ptr = line; @@ -215,7 +217,7 @@ bool CheatList::ParseLibretroCheat(CheatCode* cc, const char* line) current_ptr = end_ptr; if (end_ptr) { - if (*end_ptr != ' ') + if (!IsLibretroSeparator(*end_ptr)) { Log_WarningPrintf("Malformed code '%s'", line); break; @@ -226,9 +228,9 @@ bool CheatList::ParseLibretroCheat(CheatCode* cc, const char* line) if (end_ptr && *end_ptr == '\0') end_ptr = nullptr; - if (end_ptr) + if (end_ptr && *end_ptr != '\0') { - if (*end_ptr != '+') + if (!IsLibretroSeparator(*end_ptr)) { Log_WarningPrintf("Malformed code '%s'", line); break; @@ -285,7 +287,6 @@ std::optional CheatList::DetectFileFormat(const char* filenam return Format::Count; char line[1024]; - KeyValuePairVector kvp; while (std::fgets(line, sizeof(line), fp.get())) { char* start = line; diff --git a/src/frontend-common/common_host_interface.cpp b/src/frontend-common/common_host_interface.cpp index cbea0e300..defaaab22 100644 --- a/src/frontend-common/common_host_interface.cpp +++ b/src/frontend-common/common_host_interface.cpp @@ -2203,7 +2203,7 @@ bool CommonHostInterface::LoadCheatList(const char* filename) return false; std::unique_ptr cl = std::make_unique(); - if (!cl->LoadFromPCSXRFile(filename)) + if (!cl->LoadFromFile(filename, CheatList::Format::Autodetect)) { AddFormattedOSDMessage(15.0f, TranslateString("OSDMessage", "Failed to load cheats from '%s'."), filename); return false;