Cheats: Fix detection of ungrouped cheat import

This commit is contained in:
Stenzek 2024-09-08 11:49:59 +10:00
parent f4e8470502
commit 9d5a84541f
No known key found for this signature in database
2 changed files with 19 additions and 15 deletions

View file

@ -206,6 +206,15 @@ inline std::string ToChars(bool value, int base)
std::optional<std::vector<u8>> DecodeHex(const std::string_view str); std::optional<std::vector<u8>> DecodeHex(const std::string_view str);
std::string EncodeHex(const u8* data, int length); std::string EncodeHex(const u8* data, int length);
/// Returns true if the character is a hexadecimal digit.
template<typename T>
ALWAYS_INLINE static bool IsHexDigit(T ch)
{
return ((ch >= static_cast<T>('a') && ch <= static_cast<T>('a')) ||
(ch >= static_cast<T>('A') && ch <= static_cast<T>('Z')) ||
(ch >= static_cast<T>('0') && ch <= static_cast<T>('9')));
}
/// StartsWith/EndsWith variants which aren't case sensitive. /// StartsWith/EndsWith variants which aren't case sensitive.
ALWAYS_INLINE static bool StartsWithNoCase(const std::string_view str, const std::string_view prefix) ALWAYS_INLINE static bool StartsWithNoCase(const std::string_view str, const std::string_view prefix)
{ {

View file

@ -171,11 +171,6 @@ CheatList::CheatList() = default;
CheatList::~CheatList() = default; CheatList::~CheatList() = default;
static bool IsHexCharacter(char c)
{
return (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f') || (c >= '0' && c <= '9');
}
static int SignedCharToInt(char ch) static int SignedCharToInt(char ch)
{ {
return static_cast<int>(static_cast<unsigned char>(ch)); return static_cast<int>(static_cast<unsigned char>(ch));
@ -285,7 +280,7 @@ bool CheatList::LoadFromPCSXRString(const std::string& str)
continue; continue;
} }
while (!IsHexCharacter(*start) && start != end) while (!StringUtil::IsHexDigit(*start) && start != end)
start++; start++;
if (start == end) if (start == end)
continue; continue;
@ -296,7 +291,7 @@ bool CheatList::LoadFromPCSXRString(const std::string& str)
inst.second = 0; inst.second = 0;
if (end_ptr) if (end_ptr)
{ {
while (!IsHexCharacter(*end_ptr) && end_ptr != end) while (!StringUtil::IsHexDigit(*end_ptr) && end_ptr != end)
end_ptr++; end_ptr++;
if (end_ptr != end) if (end_ptr != end)
inst.second = static_cast<u32>(std::strtoul(end_ptr, nullptr, 16)); inst.second = static_cast<u32>(std::strtoul(end_ptr, nullptr, 16));
@ -480,7 +475,7 @@ bool CheatList::LoadFromEPSXeString(const std::string& str)
continue; continue;
} }
while (!IsHexCharacter(*start) && start != end) while (!StringUtil::IsHexDigit(*start) && start != end)
start++; start++;
if (start == end) if (start == end)
continue; continue;
@ -491,7 +486,7 @@ bool CheatList::LoadFromEPSXeString(const std::string& str)
inst.second = 0; inst.second = 0;
if (end_ptr) if (end_ptr)
{ {
while (!IsHexCharacter(*end_ptr) && end_ptr != end) while (!StringUtil::IsHexDigit(*end_ptr) && end_ptr != end)
end_ptr++; end_ptr++;
if (end_ptr != end) if (end_ptr != end)
inst.second = static_cast<u32>(std::strtoul(end_ptr, nullptr, 16)); inst.second = static_cast<u32>(std::strtoul(end_ptr, nullptr, 16));
@ -622,7 +617,7 @@ CheatList::Format CheatList::DetectFileFormat(const std::string& str)
if (start[0] == '#' || start[0] == ';') if (start[0] == '#' || start[0] == ';')
continue; continue;
if (std::strncmp(line.data(), "cheats", 6) == 0) if (line.starts_with("cheats"))
return Format::Libretro; return Format::Libretro;
// pcsxr if we see brackets // pcsxr if we see brackets
@ -630,7 +625,7 @@ CheatList::Format CheatList::DetectFileFormat(const std::string& str)
return Format::PCSXR; return Format::PCSXR;
// otherwise if it's a code, it's probably epsxe // otherwise if it's a code, it's probably epsxe
if (std::isdigit(start[0])) if (StringUtil::IsHexDigit(start[0]))
return Format::EPSXe; return Format::EPSXe;
} }
@ -772,7 +767,7 @@ bool CheatList::LoadFromPackage(const std::string& serial)
continue; continue;
} }
while (!IsHexCharacter(*start) && start != end) while (!StringUtil::IsHexDigit(*start) && start != end)
start++; start++;
if (start == end) if (start == end)
continue; continue;
@ -783,7 +778,7 @@ bool CheatList::LoadFromPackage(const std::string& serial)
inst.second = 0; inst.second = 0;
if (end_ptr) if (end_ptr)
{ {
while (!IsHexCharacter(*end_ptr) && end_ptr != end) while (!StringUtil::IsHexDigit(*end_ptr) && end_ptr != end)
end_ptr++; end_ptr++;
if (end_ptr != end) if (end_ptr != end)
inst.second = static_cast<u32>(std::strtoul(end_ptr, nullptr, 16)); inst.second = static_cast<u32>(std::strtoul(end_ptr, nullptr, 16));
@ -928,7 +923,7 @@ bool CheatCode::SetInstructionsFromString(const std::string& str)
if (*start == '#' || *start == ';' || *start == '/' || *start == '\"') if (*start == '#' || *start == ';' || *start == '/' || *start == '\"')
continue; continue;
while (!IsHexCharacter(*start) && start != end) while (!StringUtil::IsHexDigit(*start) && start != end)
start++; start++;
if (start == end) if (start == end)
continue; continue;
@ -939,7 +934,7 @@ bool CheatCode::SetInstructionsFromString(const std::string& str)
inst.second = 0; inst.second = 0;
if (end_ptr) if (end_ptr)
{ {
while (!IsHexCharacter(*end_ptr) && end_ptr != end) while (!StringUtil::IsHexDigit(*end_ptr) && end_ptr != end)
end_ptr++; end_ptr++;
if (end_ptr != end) if (end_ptr != end)
inst.second = static_cast<u32>(std::strtoul(end_ptr, nullptr, 16)); inst.second = static_cast<u32>(std::strtoul(end_ptr, nullptr, 16));