Skip to content

Commit

Permalink
Cheats: Fix detection of ungrouped cheat import
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Sep 8, 2024
1 parent 32a3311 commit 1956575
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
9 changes: 9 additions & 0 deletions src/common/string_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,15 @@ inline std::string ToChars(bool value, int base)
std::optional<std::vector<u8>> DecodeHex(const std::string_view str);
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.
ALWAYS_INLINE static bool StartsWithNoCase(const std::string_view str, const std::string_view prefix)
{
Expand Down
25 changes: 10 additions & 15 deletions src/core/cheats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,6 @@ 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)
{
return static_cast<int>(static_cast<unsigned char>(ch));
Expand Down Expand Up @@ -285,7 +280,7 @@ bool CheatList::LoadFromPCSXRString(const std::string& str)
continue;
}

while (!IsHexCharacter(*start) && start != end)
while (!StringUtil::IsHexDigit(*start) && start != end)
start++;
if (start == end)
continue;
Expand All @@ -296,7 +291,7 @@ bool CheatList::LoadFromPCSXRString(const std::string& str)
inst.second = 0;
if (end_ptr)
{
while (!IsHexCharacter(*end_ptr) && end_ptr != end)
while (!StringUtil::IsHexDigit(*end_ptr) && end_ptr != end)
end_ptr++;
if (end_ptr != end)
inst.second = static_cast<u32>(std::strtoul(end_ptr, nullptr, 16));
Expand Down Expand Up @@ -480,7 +475,7 @@ bool CheatList::LoadFromEPSXeString(const std::string& str)
continue;
}

while (!IsHexCharacter(*start) && start != end)
while (!StringUtil::IsHexDigit(*start) && start != end)
start++;
if (start == end)
continue;
Expand All @@ -491,7 +486,7 @@ bool CheatList::LoadFromEPSXeString(const std::string& str)
inst.second = 0;
if (end_ptr)
{
while (!IsHexCharacter(*end_ptr) && end_ptr != end)
while (!StringUtil::IsHexDigit(*end_ptr) && end_ptr != end)
end_ptr++;
if (end_ptr != end)
inst.second = static_cast<u32>(std::strtoul(end_ptr, nullptr, 16));
Expand Down Expand Up @@ -622,15 +617,15 @@ CheatList::Format CheatList::DetectFileFormat(const std::string& str)
if (start[0] == '#' || start[0] == ';')
continue;

if (std::strncmp(line.data(), "cheats", 6) == 0)
if (line.starts_with("cheats"))
return Format::Libretro;

// pcsxr if we see brackets
if (start[0] == '[')
return Format::PCSXR;

// otherwise if it's a code, it's probably epsxe
if (std::isdigit(start[0]))
if (StringUtil::IsHexDigit(start[0]))
return Format::EPSXe;
}

Expand Down Expand Up @@ -772,7 +767,7 @@ bool CheatList::LoadFromPackage(const std::string& serial)
continue;
}

while (!IsHexCharacter(*start) && start != end)
while (!StringUtil::IsHexDigit(*start) && start != end)
start++;
if (start == end)
continue;
Expand All @@ -783,7 +778,7 @@ bool CheatList::LoadFromPackage(const std::string& serial)
inst.second = 0;
if (end_ptr)
{
while (!IsHexCharacter(*end_ptr) && end_ptr != end)
while (!StringUtil::IsHexDigit(*end_ptr) && end_ptr != end)
end_ptr++;
if (end_ptr != end)
inst.second = static_cast<u32>(std::strtoul(end_ptr, nullptr, 16));
Expand Down Expand Up @@ -928,7 +923,7 @@ bool CheatCode::SetInstructionsFromString(const std::string& str)
if (*start == '#' || *start == ';' || *start == '/' || *start == '\"')
continue;

while (!IsHexCharacter(*start) && start != end)
while (!StringUtil::IsHexDigit(*start) && start != end)
start++;
if (start == end)
continue;
Expand All @@ -939,7 +934,7 @@ bool CheatCode::SetInstructionsFromString(const std::string& str)
inst.second = 0;
if (end_ptr)
{
while (!IsHexCharacter(*end_ptr) && end_ptr != end)
while (!StringUtil::IsHexDigit(*end_ptr) && end_ptr != end)
end_ptr++;
if (end_ptr != end)
inst.second = static_cast<u32>(std::strtoul(end_ptr, nullptr, 16));
Expand Down

0 comments on commit 1956575

Please sign in to comment.