Cheats: Strip whitespace from code names

This commit is contained in:
Stenzek 2024-11-29 12:48:52 +10:00
parent c4d4a7a774
commit 208e6c4b35
No known key found for this signature in database
1 changed files with 45 additions and 13 deletions

View File

@ -1246,11 +1246,17 @@ void Cheats::ParseFile(CheatCodeList* dst_list, const std::string_view file_cont
continue; continue;
} }
const std::string_view name = StringUtil::StripWhitespace(linev.substr(1, linev.length() - 2));
if (name.empty())
{
WARNING_LOG("Empty cheat code name at line {}: {}", reader.GetCurrentLineNumber(), line);
continue;
}
if (!next_code_metadata.name.empty()) if (!next_code_metadata.name.empty())
finish_code(); finish_code();
// new code. // new code.
const std::string_view name = linev.substr(1, linev.length() - 2);
next_code_metadata.name = next_code_metadata.name =
next_code_group.empty() ? std::string(name) : fmt::format("{}\\{}", next_code_group, name); next_code_group.empty() ? std::string(name) : fmt::format("{}\\{}", next_code_group, name);
continue; continue;
@ -1514,12 +1520,26 @@ bool Cheats::ImportPCSXFile(CodeInfoList* dst, const std::string_view file_conte
continue; continue;
} }
std::string_view name_part = StringUtil::StripWhitespace(linev.substr(1, linev.length() - 2));
if (!name_part.empty() && name_part.front() == '*')
name_part = name_part.substr(1);
if (name_part.empty())
{
if (!reader.LogError(error, stop_on_error, "Empty code name at line {}: {}", reader.GetCurrentLineNumber(),
line))
{
return false;
}
continue;
}
// new code. // new code.
if (!current_code.name.empty() && !finish_code()) if (!current_code.name.empty() && !finish_code())
return false; return false;
current_code = CodeInfo(); current_code = CodeInfo();
current_code.name = (linev[1] == '*') ? linev.substr(2, linev.length() - 3) : linev.substr(1, linev.length() - 2); current_code.name = name_part;
current_code.file_offset_start = static_cast<u32>(reader.GetCurrentLineOffset()); current_code.file_offset_start = static_cast<u32>(reader.GetCurrentLineOffset());
current_code.file_offset_end = current_code.file_offset_start; current_code.file_offset_end = current_code.file_offset_start;
current_code.file_offset_body_start = current_code.file_offset_start; current_code.file_offset_body_start = current_code.file_offset_start;
@ -1683,12 +1703,24 @@ bool Cheats::ImportEPSXeFile(CodeInfoList* dst, const std::string_view file_cont
continue; continue;
} }
const std::string_view name_part = StringUtil::StripWhitespace(linev.substr(1));
if (name_part.empty())
{
if (!reader.LogError(error, stop_on_error, "Empty code name at line {}: {}", reader.GetCurrentLineNumber(),
line))
{
return false;
}
continue;
}
if (!current_code.name.empty() && !finish_code()) if (!current_code.name.empty() && !finish_code())
return false; return false;
// new code. // new code.
current_code = CodeInfo(); current_code = CodeInfo();
current_code.name = linev.substr(1); current_code.name = name_part;
current_code.file_offset_start = static_cast<u32>(reader.GetCurrentOffset()); current_code.file_offset_start = static_cast<u32>(reader.GetCurrentOffset());
current_code.file_offset_end = current_code.file_offset_start; current_code.file_offset_end = current_code.file_offset_start;
current_code.file_offset_body_start = current_code.file_offset_start; current_code.file_offset_body_start = current_code.file_offset_start;
@ -2360,7 +2392,7 @@ void Cheats::GamesharkCheatCode::Apply() const
index++; index++;
} }
break; break;
case InstructionCode::ExtConstantWriteIfMatchWithRestore8: case InstructionCode::ExtConstantWriteIfMatchWithRestore8:
{ {
const u8 value = DoMemoryRead<u8>(inst.address); const u8 value = DoMemoryRead<u8>(inst.address);
@ -2372,7 +2404,7 @@ void Cheats::GamesharkCheatCode::Apply() const
index++; index++;
} }
break; break;
case InstructionCode::ExtConstantForceRange8: case InstructionCode::ExtConstantForceRange8:
{ {
const u8 value = DoMemoryRead<u8>(inst.address); const u8 value = DoMemoryRead<u8>(inst.address);
@ -3950,14 +3982,14 @@ void Cheats::GamesharkCheatCode::ApplyOnDisable() const
index++; index++;
} }
break; break;
[[unlikely]] default: [[unlikely]] default:
{ {
ERROR_LOG("Unhandled instruction code 0x{:02X} ({:08X} {:08X})", static_cast<u8>(inst.code.GetValue()), ERROR_LOG("Unhandled instruction code 0x{:02X} ({:08X} {:08X})", static_cast<u8>(inst.code.GetValue()),
inst.first, inst.second); inst.first, inst.second);
index++; index++;
} }
break; break;
} }
} }
} }