Merge pull request #4657 from lioncash/cheatparser

cheat_engine: Remove unnecessary system argument to CheatParser's Parse function
This commit is contained in:
Rodrigo Locatti 2020-09-16 00:24:14 +00:00 committed by GitHub
commit 9cd1ea338b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 34 deletions

View file

@ -76,8 +76,7 @@ VirtualDir FindSubdirectoryCaseless(const VirtualDir dir, std::string_view name)
} }
std::optional<std::vector<Core::Memory::CheatEntry>> ReadCheatFileFromFolder( std::optional<std::vector<Core::Memory::CheatEntry>> ReadCheatFileFromFolder(
const Core::System& system, u64 title_id, const PatchManager::BuildID& build_id_, u64 title_id, const PatchManager::BuildID& build_id_, const VirtualDir& base_path, bool upper) {
const VirtualDir& base_path, bool upper) {
const auto build_id_raw = Common::HexToString(build_id_, upper); const auto build_id_raw = Common::HexToString(build_id_, upper);
const auto build_id = build_id_raw.substr(0, sizeof(u64) * 2); const auto build_id = build_id_raw.substr(0, sizeof(u64) * 2);
const auto file = base_path->GetFile(fmt::format("{}.txt", build_id)); const auto file = base_path->GetFile(fmt::format("{}.txt", build_id));
@ -95,9 +94,8 @@ std::optional<std::vector<Core::Memory::CheatEntry>> ReadCheatFileFromFolder(
return std::nullopt; return std::nullopt;
} }
Core::Memory::TextCheatParser parser; const Core::Memory::TextCheatParser parser;
return parser.Parse(system, return parser.Parse(std::string_view(reinterpret_cast<const char*>(data.data()), data.size()));
std::string_view(reinterpret_cast<const char*>(data.data()), data.size()));
} }
void AppendCommaIfNotEmpty(std::string& to, std::string_view with) { void AppendCommaIfNotEmpty(std::string& to, std::string_view with) {
@ -335,14 +333,12 @@ std::vector<Core::Memory::CheatEntry> PatchManager::CreateCheatList(
auto cheats_dir = FindSubdirectoryCaseless(subdir, "cheats"); auto cheats_dir = FindSubdirectoryCaseless(subdir, "cheats");
if (cheats_dir != nullptr) { if (cheats_dir != nullptr) {
auto res = ReadCheatFileFromFolder(system, title_id, build_id_, cheats_dir, true); if (const auto res = ReadCheatFileFromFolder(title_id, build_id_, cheats_dir, true)) {
if (res.has_value()) {
std::copy(res->begin(), res->end(), std::back_inserter(out)); std::copy(res->begin(), res->end(), std::back_inserter(out));
continue; continue;
} }
res = ReadCheatFileFromFolder(system, title_id, build_id_, cheats_dir, false); if (const auto res = ReadCheatFileFromFolder(title_id, build_id_, cheats_dir, false)) {
if (res.has_value()) {
std::copy(res->begin(), res->end(), std::back_inserter(out)); std::copy(res->begin(), res->end(), std::back_inserter(out));
} }
} }

View file

@ -19,10 +19,24 @@
#include "core/memory/cheat_engine.h" #include "core/memory/cheat_engine.h"
namespace Core::Memory { namespace Core::Memory {
namespace {
constexpr auto CHEAT_ENGINE_NS = std::chrono::nanoseconds{1000000000 / 12}; constexpr auto CHEAT_ENGINE_NS = std::chrono::nanoseconds{1000000000 / 12};
constexpr u32 KEYPAD_BITMASK = 0x3FFFFFF; constexpr u32 KEYPAD_BITMASK = 0x3FFFFFF;
std::string_view ExtractName(std::string_view data, std::size_t start_index, char match) {
auto end_index = start_index;
while (data[end_index] != match) {
++end_index;
if (end_index > data.size() ||
(end_index - start_index - 1) > sizeof(CheatDefinition::readable_name)) {
return {};
}
}
return data.substr(start_index, end_index - start_index);
}
} // Anonymous namespace
StandardVmCallbacks::StandardVmCallbacks(Core::System& system, const CheatProcessMetadata& metadata) StandardVmCallbacks::StandardVmCallbacks(Core::System& system, const CheatProcessMetadata& metadata)
: metadata(metadata), system(system) {} : metadata(metadata), system(system) {}
@ -82,26 +96,9 @@ CheatParser::~CheatParser() = default;
TextCheatParser::~TextCheatParser() = default; TextCheatParser::~TextCheatParser() = default;
namespace { std::vector<CheatEntry> TextCheatParser::Parse(std::string_view data) const {
template <char match>
std::string_view ExtractName(std::string_view data, std::size_t start_index) {
auto end_index = start_index;
while (data[end_index] != match) {
++end_index;
if (end_index > data.size() ||
(end_index - start_index - 1) > sizeof(CheatDefinition::readable_name)) {
return {};
}
}
return data.substr(start_index, end_index - start_index);
}
} // Anonymous namespace
std::vector<CheatEntry> TextCheatParser::Parse(const Core::System& system,
std::string_view data) const {
std::vector<CheatEntry> out(1); std::vector<CheatEntry> out(1);
std::optional<u64> current_entry = std::nullopt; std::optional<u64> current_entry;
for (std::size_t i = 0; i < data.size(); ++i) { for (std::size_t i = 0; i < data.size(); ++i) {
if (::isspace(data[i])) { if (::isspace(data[i])) {
@ -115,7 +112,7 @@ std::vector<CheatEntry> TextCheatParser::Parse(const Core::System& system,
return {}; return {};
} }
const auto name = ExtractName<'}'>(data, i + 1); const auto name = ExtractName(data, i + 1, '}');
if (name.empty()) { if (name.empty()) {
return {}; return {};
} }
@ -132,7 +129,7 @@ std::vector<CheatEntry> TextCheatParser::Parse(const Core::System& system,
current_entry = out.size(); current_entry = out.size();
out.emplace_back(); out.emplace_back();
const auto name = ExtractName<']'>(data, i + 1); const auto name = ExtractName(data, i + 1, ']');
if (name.empty()) { if (name.empty()) {
return {}; return {};
} }

View file

@ -47,8 +47,7 @@ class CheatParser {
public: public:
virtual ~CheatParser(); virtual ~CheatParser();
virtual std::vector<CheatEntry> Parse(const Core::System& system, [[nodiscard]] virtual std::vector<CheatEntry> Parse(std::string_view data) const = 0;
std::string_view data) const = 0;
}; };
// CheatParser implementation that parses text files // CheatParser implementation that parses text files
@ -56,7 +55,7 @@ class TextCheatParser final : public CheatParser {
public: public:
~TextCheatParser() override; ~TextCheatParser() override;
std::vector<CheatEntry> Parse(const Core::System& system, std::string_view data) const override; [[nodiscard]] std::vector<CheatEntry> Parse(std::string_view data) const override;
}; };
// Class that encapsulates a CheatList and manages its interaction with memory and CoreTiming // Class that encapsulates a CheatList and manages its interaction with memory and CoreTiming