From ba7eb5abf4cd50b4dd1dc894bc633b678d38a1cb Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 15 Sep 2020 03:13:22 -0400 Subject: [PATCH 1/2] cheat_engine: Remove unnecessary system argument to CheatParser's Parse function This isn't used within the function at all in any implementations, so we can remove it entirely. --- src/core/file_sys/patch_manager.cpp | 14 +++++--------- src/core/memory/cheat_engine.cpp | 5 ++--- src/core/memory/cheat_engine.h | 5 ++--- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cpp index 87c354a43..b9c09b456 100644 --- a/src/core/file_sys/patch_manager.cpp +++ b/src/core/file_sys/patch_manager.cpp @@ -76,8 +76,7 @@ VirtualDir FindSubdirectoryCaseless(const VirtualDir dir, std::string_view name) } std::optional> ReadCheatFileFromFolder( - const Core::System& system, u64 title_id, const PatchManager::BuildID& build_id_, - const VirtualDir& base_path, bool upper) { + u64 title_id, const PatchManager::BuildID& build_id_, const VirtualDir& base_path, bool 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 file = base_path->GetFile(fmt::format("{}.txt", build_id)); @@ -95,9 +94,8 @@ std::optional> ReadCheatFileFromFolder( return std::nullopt; } - Core::Memory::TextCheatParser parser; - return parser.Parse(system, - std::string_view(reinterpret_cast(data.data()), data.size())); + const Core::Memory::TextCheatParser parser; + return parser.Parse(std::string_view(reinterpret_cast(data.data()), data.size())); } void AppendCommaIfNotEmpty(std::string& to, std::string_view with) { @@ -335,14 +333,12 @@ std::vector PatchManager::CreateCheatList( auto cheats_dir = FindSubdirectoryCaseless(subdir, "cheats"); if (cheats_dir != nullptr) { - auto res = ReadCheatFileFromFolder(system, title_id, build_id_, cheats_dir, true); - if (res.has_value()) { + if (const auto res = ReadCheatFileFromFolder(title_id, build_id_, cheats_dir, true)) { std::copy(res->begin(), res->end(), std::back_inserter(out)); continue; } - res = ReadCheatFileFromFolder(system, title_id, build_id_, cheats_dir, false); - if (res.has_value()) { + if (const auto res = ReadCheatFileFromFolder(title_id, build_id_, cheats_dir, false)) { std::copy(res->begin(), res->end(), std::back_inserter(out)); } } diff --git a/src/core/memory/cheat_engine.cpp b/src/core/memory/cheat_engine.cpp index e503118dd..68d09d350 100644 --- a/src/core/memory/cheat_engine.cpp +++ b/src/core/memory/cheat_engine.cpp @@ -98,10 +98,9 @@ std::string_view ExtractName(std::string_view data, std::size_t start_index) { } } // Anonymous namespace -std::vector TextCheatParser::Parse(const Core::System& system, - std::string_view data) const { +std::vector TextCheatParser::Parse(std::string_view data) const { std::vector out(1); - std::optional current_entry = std::nullopt; + std::optional current_entry; for (std::size_t i = 0; i < data.size(); ++i) { if (::isspace(data[i])) { diff --git a/src/core/memory/cheat_engine.h b/src/core/memory/cheat_engine.h index fa039a831..a31002346 100644 --- a/src/core/memory/cheat_engine.h +++ b/src/core/memory/cheat_engine.h @@ -47,8 +47,7 @@ class CheatParser { public: virtual ~CheatParser(); - virtual std::vector Parse(const Core::System& system, - std::string_view data) const = 0; + [[nodiscard]] virtual std::vector Parse(std::string_view data) const = 0; }; // CheatParser implementation that parses text files @@ -56,7 +55,7 @@ class TextCheatParser final : public CheatParser { public: ~TextCheatParser() override; - std::vector Parse(const Core::System& system, std::string_view data) const override; + [[nodiscard]] std::vector Parse(std::string_view data) const override; }; // Class that encapsulates a CheatList and manages its interaction with memory and CoreTiming From 3a8464cde28bd89146aa44af836f80ff925c293c Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 15 Sep 2020 03:24:42 -0400 Subject: [PATCH 2/2] cheat_engine: Convert ExtractName into a non-template function We don't need to create two separate instantiations of the same code, we can simply make the character template argument a regular function parameter. --- src/core/memory/cheat_engine.cpp | 36 +++++++++++++++----------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/core/memory/cheat_engine.cpp b/src/core/memory/cheat_engine.cpp index 68d09d350..29284a42d 100644 --- a/src/core/memory/cheat_engine.cpp +++ b/src/core/memory/cheat_engine.cpp @@ -19,10 +19,24 @@ #include "core/memory/cheat_engine.h" namespace Core::Memory { - +namespace { constexpr auto CHEAT_ENGINE_NS = std::chrono::nanoseconds{1000000000 / 12}; 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) : metadata(metadata), system(system) {} @@ -82,22 +96,6 @@ CheatParser::~CheatParser() = default; TextCheatParser::~TextCheatParser() = default; -namespace { -template -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 TextCheatParser::Parse(std::string_view data) const { std::vector out(1); std::optional current_entry; @@ -114,7 +112,7 @@ std::vector TextCheatParser::Parse(std::string_view data) const { return {}; } - const auto name = ExtractName<'}'>(data, i + 1); + const auto name = ExtractName(data, i + 1, '}'); if (name.empty()) { return {}; } @@ -131,7 +129,7 @@ std::vector TextCheatParser::Parse(std::string_view data) const { current_entry = out.size(); out.emplace_back(); - const auto name = ExtractName<']'>(data, i + 1); + const auto name = ExtractName(data, i + 1, ']'); if (name.empty()) { return {}; }