diff --git a/src/citra_qt/configuration/configure_cheats.cpp b/src/citra_qt/configuration/configure_cheats.cpp index 4569f7ed8..aa67f7440 100644 --- a/src/citra_qt/configuration/configure_cheats.cpp +++ b/src/citra_qt/configuration/configure_cheats.cpp @@ -9,12 +9,12 @@ #include "core/cheats/cheat_base.h" #include "core/cheats/cheats.h" #include "core/cheats/gateway_cheat.h" -#include "core/core.h" -#include "core/hle/kernel/process.h" #include "ui_configure_cheats.h" -ConfigureCheats::ConfigureCheats(u64 title_id_, QWidget* parent) - : QWidget(parent), ui(std::make_unique()), title_id{title_id_} { +ConfigureCheats::ConfigureCheats(Cheats::CheatEngine& cheat_engine_, u64 title_id_, QWidget* parent) + : QWidget(parent), + ui(std::make_unique()), cheat_engine{cheat_engine_}, title_id{ + title_id_} { // Setup gui control settings ui->setupUi(this); ui->tableCheats->setColumnWidth(0, 30); @@ -36,15 +36,14 @@ ConfigureCheats::ConfigureCheats(u64 title_id_, QWidget* parent) [this] { SaveCheat(ui->tableCheats->currentRow()); }); connect(ui->buttonDelete, &QPushButton::clicked, this, &ConfigureCheats::OnDeleteCheat); - cheat_engine = std::make_unique(title_id, Core::System::GetInstance()); - + cheat_engine.LoadCheatFile(title_id); LoadCheats(); } ConfigureCheats::~ConfigureCheats() = default; void ConfigureCheats::LoadCheats() { - cheats = cheat_engine->GetCheats(); + cheats = cheat_engine.GetCheats(); const int cheats_count = static_cast(cheats.size()); ui->tableCheats->setRowCount(cheats_count); @@ -108,12 +107,12 @@ bool ConfigureCheats::SaveCheat(int row) { ui->textNotes->toPlainText().toStdString()); if (newly_created) { - cheat_engine->AddCheat(cheat); + cheat_engine.AddCheat(std::move(cheat)); newly_created = false; } else { - cheat_engine->UpdateCheat(row, cheat); + cheat_engine.UpdateCheat(row, std::move(cheat)); } - cheat_engine->SaveCheatFile(); + cheat_engine.SaveCheatFile(title_id); int previous_row = ui->tableCheats->currentRow(); int previous_col = ui->tableCheats->currentColumn(); @@ -163,7 +162,7 @@ void ConfigureCheats::OnCheckChanged(int state) { const QCheckBox* checkbox = qobject_cast(sender()); int row = static_cast(checkbox->property("row").toInt()); cheats[row]->SetEnabled(state); - cheat_engine->SaveCheatFile(); + cheat_engine.SaveCheatFile(title_id); } void ConfigureCheats::OnTextEdited() { @@ -175,8 +174,8 @@ void ConfigureCheats::OnDeleteCheat() { if (newly_created) { newly_created = false; } else { - cheat_engine->RemoveCheat(ui->tableCheats->currentRow()); - cheat_engine->SaveCheatFile(); + cheat_engine.RemoveCheat(ui->tableCheats->currentRow()); + cheat_engine.SaveCheatFile(title_id); } LoadCheats(); diff --git a/src/citra_qt/configuration/configure_cheats.h b/src/citra_qt/configuration/configure_cheats.h index 5242c82d8..2b38e84e3 100644 --- a/src/citra_qt/configuration/configure_cheats.h +++ b/src/citra_qt/configuration/configure_cheats.h @@ -5,6 +5,8 @@ #pragma once #include +#include +#include #include "common/common_types.h" namespace Cheats { @@ -20,7 +22,8 @@ class ConfigureCheats : public QWidget { Q_OBJECT public: - explicit ConfigureCheats(u64 title_id_, QWidget* parent = nullptr); + explicit ConfigureCheats(Cheats::CheatEngine& cheat_engine, u64 title_id_, + QWidget* parent = nullptr); ~ConfigureCheats(); bool ApplyConfiguration(); @@ -53,9 +56,9 @@ private slots: private: std::unique_ptr ui; - std::vector> cheats; + Cheats::CheatEngine& cheat_engine; + std::span> cheats; bool edited = false, newly_created = false; int last_row = -1, last_col = -1; u64 title_id; - std::unique_ptr cheat_engine; }; diff --git a/src/citra_qt/configuration/configure_per_game.cpp b/src/citra_qt/configuration/configure_per_game.cpp index 4b9f73822..8742b1840 100644 --- a/src/citra_qt/configuration/configure_per_game.cpp +++ b/src/citra_qt/configuration/configure_per_game.cpp @@ -36,7 +36,7 @@ ConfigurePerGame::ConfigurePerGame(QWidget* parent, u64 title_id_, const QString graphics_tab = std::make_unique(this); system_tab = std::make_unique(this); debug_tab = std::make_unique(this); - cheat_tab = std::make_unique(title_id, this); + cheat_tab = std::make_unique(system.CheatEngine(), title_id, this); ui->setupUi(this); diff --git a/src/core/cheats/cheats.cpp b/src/core/cheats/cheats.cpp index 26a77edee..741b1c472 100644 --- a/src/core/cheats/cheats.cpp +++ b/src/core/cheats/cheats.cpp @@ -10,8 +10,6 @@ #include "core/cheats/gateway_cheat.h" #include "core/core.h" #include "core/core_timing.h" -#include "core/hle/kernel/process.h" -#include "core/hw/gpu.h" namespace Cheats { @@ -19,11 +17,11 @@ namespace Cheats { // we use the same value constexpr u64 run_interval_ticks = 50'000'000; -CheatEngine::CheatEngine(u64 title_id_, Core::System& system_) - : system(system_), title_id{title_id_} { - LoadCheatFile(); +CheatEngine::CheatEngine(Core::System& system_) : system{system_} {} + +CheatEngine::~CheatEngine() { if (system.IsPoweredOn()) { - Connect(); + system.CoreTiming().UnscheduleEvent(event, 0); } } @@ -34,24 +32,18 @@ void CheatEngine::Connect() { system.CoreTiming().ScheduleEvent(run_interval_ticks, event); } -CheatEngine::~CheatEngine() { - if (system.IsPoweredOn()) { - system.CoreTiming().UnscheduleEvent(event, 0); - } -} - -std::vector> CheatEngine::GetCheats() const { - std::shared_lock lock(cheats_list_mutex); +std::span> CheatEngine::GetCheats() const { + std::shared_lock lock{cheats_list_mutex}; return cheats_list; } -void CheatEngine::AddCheat(const std::shared_ptr& cheat) { - std::unique_lock lock(cheats_list_mutex); - cheats_list.push_back(cheat); +void CheatEngine::AddCheat(std::shared_ptr&& cheat) { + std::unique_lock lock{cheats_list_mutex}; + cheats_list.push_back(std::move(cheat)); } void CheatEngine::RemoveCheat(std::size_t index) { - std::unique_lock lock(cheats_list_mutex); + std::unique_lock lock{cheats_list_mutex}; if (index < 0 || index >= cheats_list.size()) { LOG_ERROR(Core_Cheats, "Invalid index {}", index); return; @@ -59,16 +51,16 @@ void CheatEngine::RemoveCheat(std::size_t index) { cheats_list.erase(cheats_list.begin() + index); } -void CheatEngine::UpdateCheat(std::size_t index, const std::shared_ptr& new_cheat) { - std::unique_lock lock(cheats_list_mutex); +void CheatEngine::UpdateCheat(std::size_t index, std::shared_ptr&& new_cheat) { + std::unique_lock lock{cheats_list_mutex}; if (index < 0 || index >= cheats_list.size()) { LOG_ERROR(Core_Cheats, "Invalid index {}", index); return; } - cheats_list[index] = new_cheat; + cheats_list[index] = std::move(new_cheat); } -void CheatEngine::SaveCheatFile() const { +void CheatEngine::SaveCheatFile(u64 title_id) const { const std::string cheat_dir = FileUtil::GetUserPath(FileUtil::UserPath::CheatsDir); const std::string filepath = fmt::format("{}{:016X}.txt", cheat_dir, title_id); @@ -83,7 +75,7 @@ void CheatEngine::SaveCheatFile() const { } } -void CheatEngine::LoadCheatFile() { +void CheatEngine::LoadCheatFile(u64 title_id) { const std::string cheat_dir = FileUtil::GetUserPath(FileUtil::UserPath::CheatsDir); const std::string filepath = fmt::format("{}{:016X}.txt", cheat_dir, title_id); @@ -96,15 +88,15 @@ void CheatEngine::LoadCheatFile() { auto gateway_cheats = GatewayCheat::LoadFile(filepath); { - std::unique_lock lock(cheats_list_mutex); - std::move(gateway_cheats.begin(), gateway_cheats.end(), std::back_inserter(cheats_list)); + std::unique_lock lock{cheats_list_mutex}; + cheats_list = std::move(gateway_cheats); } } void CheatEngine::RunCallback([[maybe_unused]] std::uintptr_t user_data, s64 cycles_late) { { - std::shared_lock lock(cheats_list_mutex); - for (auto& cheat : cheats_list) { + std::shared_lock lock{cheats_list_mutex}; + for (const auto& cheat : cheats_list) { if (cheat->IsEnabled()) { cheat->Execute(system); } diff --git a/src/core/cheats/cheats.h b/src/core/cheats/cheats.h index f19212e60..879359b32 100644 --- a/src/core/cheats/cheats.h +++ b/src/core/cheats/cheats.h @@ -6,6 +6,7 @@ #include #include +#include #include #include "common/common_types.h" @@ -24,22 +25,38 @@ class CheatBase; class CheatEngine { public: - explicit CheatEngine(u64 title_id_, Core::System& system); + explicit CheatEngine(Core::System& system); ~CheatEngine(); + + /// Registers the cheat execution callback. void Connect(); - std::vector> GetCheats() const; - void AddCheat(const std::shared_ptr& cheat); + + /// Returns a span of the currently active cheats. + std::span> GetCheats() const; + + /// Adds a cheat to the cheat engine. + void AddCheat(std::shared_ptr&& cheat); + + /// Removes a cheat at the specified index in the cheats list. void RemoveCheat(std::size_t index); - void UpdateCheat(std::size_t index, const std::shared_ptr& new_cheat); - void SaveCheatFile() const; + + /// Updates a cheat at the specified index in the cheats list. + void UpdateCheat(std::size_t index, std::shared_ptr&& new_cheat); + + /// Loads the cheat file from disk for the specified title id. + void LoadCheatFile(u64 title_id); + + /// Saves currently active cheats to file for the specified title id. + void SaveCheatFile(u64 title_id) const; private: - void LoadCheatFile(); + /// The cheat execution callback. void RunCallback(std::uintptr_t user_data, s64 cycles_late); + +private: + Core::System& system; + Core::TimingEventType* event; std::vector> cheats_list; mutable std::shared_mutex cheats_list_mutex; - Core::TimingEventType* event; - Core::System& system; - u64 title_id; }; } // namespace Cheats diff --git a/src/core/cheats/gateway_cheat.cpp b/src/core/cheats/gateway_cheat.cpp index b44d60477..9792a188b 100644 --- a/src/core/cheats/gateway_cheat.cpp +++ b/src/core/cheats/gateway_cheat.cpp @@ -472,8 +472,8 @@ std::string GatewayCheat::ToString() const { return result; } -std::vector> GatewayCheat::LoadFile(const std::string& filepath) { - std::vector> cheats; +std::vector> GatewayCheat::LoadFile(const std::string& filepath) { + std::vector> cheats; boost::iostreams::stream file; FileUtil::OpenFStream(file, filepath); @@ -493,7 +493,7 @@ std::vector> GatewayCheat::LoadFile(const std::string line = Common::StripSpaces(line); // remove spaces at front and end if (line.length() >= 2 && line.front() == '[') { if (!cheat_lines.empty()) { - cheats.push_back(std::make_unique(name, cheat_lines, comments)); + cheats.push_back(std::make_shared(name, cheat_lines, comments)); cheats.back()->SetEnabled(enabled); enabled = false; } @@ -511,7 +511,7 @@ std::vector> GatewayCheat::LoadFile(const std::string } } if (!cheat_lines.empty()) { - cheats.push_back(std::make_unique(name, cheat_lines, comments)); + cheats.push_back(std::make_shared(name, cheat_lines, comments)); cheats.back()->SetEnabled(enabled); } return cheats; diff --git a/src/core/cheats/gateway_cheat.h b/src/core/cheats/gateway_cheat.h index 14c81786b..ff200af59 100644 --- a/src/core/cheats/gateway_cheat.h +++ b/src/core/cheats/gateway_cheat.h @@ -77,7 +77,7 @@ public: /// (there might be multiple lines of those hex numbers) /// Comment lines start with a '*' /// This function will pares the file for such structures - static std::vector> LoadFile(const std::string& filepath); + static std::vector> LoadFile(const std::string& filepath); private: std::atomic enabled = false; diff --git a/src/core/core.cpp b/src/core/core.cpp index 4ac4d0fd0..a8b0db0e3 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -72,6 +72,8 @@ Core::Timing& Global() { return System::GetInstance().CoreTiming(); } +System::System() : cheat_engine{*this} {} + System::~System() = default; System::ResultStatus System::RunLoop(bool tight_loop) { @@ -318,7 +320,10 @@ System::ResultStatus System::Load(Frontend::EmuWindow& emu_window, const std::st LOG_ERROR(Core, "Failed to find title id for ROM (Error {})", static_cast(load_result)); } - cheat_engine = std::make_unique(title_id, *this); + + cheat_engine.LoadCheatFile(title_id); + cheat_engine.Connect(); + perf_stats = std::make_unique(title_id); if (Settings::values.custom_textures) { @@ -489,11 +494,11 @@ const Memory::MemorySystem& System::Memory() const { } Cheats::CheatEngine& System::CheatEngine() { - return *cheat_engine; + return cheat_engine; } const Cheats::CheatEngine& System::CheatEngine() const { - return *cheat_engine; + return cheat_engine; } void System::RegisterVideoDumper(std::shared_ptr dumper) { @@ -540,7 +545,6 @@ void System::Shutdown(bool is_deserializing) { if (!is_deserializing) { GDBStub::Shutdown(); perf_stats.reset(); - cheat_engine.reset(); app_loader.reset(); } custom_tex_manager.reset(); @@ -712,7 +716,7 @@ void System::serialize(Archive& ar, const unsigned int file_version) { timing->UnlockEventQueue(); Service::GSP::SetGlobalModule(*this); memory->SetDSP(*dsp_core); - cheat_engine->Connect(); + cheat_engine.Connect(); VideoCore::g_renderer->Sync(); } } diff --git a/src/core/core.h b/src/core/core.h index 3b48912d1..07fccfa92 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -10,6 +10,7 @@ #include #include #include "common/common_types.h" +#include "core/cheats/cheats.h" #include "core/frontend/applets/mii_selector.h" #include "core/frontend/applets/swkbd.h" #include "core/loader/loader.h" @@ -49,10 +50,6 @@ namespace Kernel { class KernelSystem; } -namespace Cheats { -class CheatEngine; -} - namespace VideoDumper { class Backend; } @@ -95,6 +92,7 @@ public: ErrorUnknown ///< Any other error }; + System(); ~System(); /** @@ -372,7 +370,7 @@ private: std::shared_ptr registered_swkbd; /// Cheats manager - std::unique_ptr cheat_engine; + Cheats::CheatEngine cheat_engine; /// Video dumper backend std::shared_ptr video_dumper;