|
|
|
@ -10,7 +10,6 @@
|
|
|
|
|
#include "core/cheats/gateway_cheat.h"
|
|
|
|
|
#include "core/core.h"
|
|
|
|
|
#include "core/core_timing.h"
|
|
|
|
|
#include "core/hle/kernel/process.h"
|
|
|
|
|
|
|
|
|
|
namespace Cheats {
|
|
|
|
|
|
|
|
|
@ -18,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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -33,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<std::shared_ptr<CheatBase>> CheatEngine::GetCheats() const {
|
|
|
|
|
std::shared_lock<std::shared_mutex> lock(cheats_list_mutex);
|
|
|
|
|
std::span<const std::shared_ptr<CheatBase>> CheatEngine::GetCheats() const {
|
|
|
|
|
std::shared_lock lock{cheats_list_mutex};
|
|
|
|
|
return cheats_list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CheatEngine::AddCheat(const std::shared_ptr<CheatBase>& cheat) {
|
|
|
|
|
std::unique_lock<std::shared_mutex> lock(cheats_list_mutex);
|
|
|
|
|
cheats_list.push_back(cheat);
|
|
|
|
|
void CheatEngine::AddCheat(std::shared_ptr<CheatBase>&& 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<std::shared_mutex> 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;
|
|
|
|
@ -58,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<CheatBase>& new_cheat) {
|
|
|
|
|
std::unique_lock<std::shared_mutex> lock(cheats_list_mutex);
|
|
|
|
|
void CheatEngine::UpdateCheat(std::size_t index, std::shared_ptr<CheatBase>&& 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);
|
|
|
|
|
|
|
|
|
@ -82,7 +75,14 @@ void CheatEngine::SaveCheatFile() const {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CheatEngine::LoadCheatFile() {
|
|
|
|
|
void CheatEngine::LoadCheatFile(u64 title_id) {
|
|
|
|
|
{
|
|
|
|
|
std::unique_lock lock{cheats_list_mutex};
|
|
|
|
|
if (loaded_title_id.has_value() && loaded_title_id == title_id) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::string cheat_dir = FileUtil::GetUserPath(FileUtil::UserPath::CheatsDir);
|
|
|
|
|
const std::string filepath = fmt::format("{}{:016X}.txt", cheat_dir, title_id);
|
|
|
|
|
|
|
|
|
@ -90,20 +90,22 @@ void CheatEngine::LoadCheatFile() {
|
|
|
|
|
FileUtil::CreateDir(cheat_dir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!FileUtil::Exists(filepath))
|
|
|
|
|
if (!FileUtil::Exists(filepath)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto gateway_cheats = GatewayCheat::LoadFile(filepath);
|
|
|
|
|
{
|
|
|
|
|
std::unique_lock<std::shared_mutex> lock(cheats_list_mutex);
|
|
|
|
|
std::move(gateway_cheats.begin(), gateway_cheats.end(), std::back_inserter(cheats_list));
|
|
|
|
|
std::unique_lock lock{cheats_list_mutex};
|
|
|
|
|
loaded_title_id = title_id;
|
|
|
|
|
cheats_list = std::move(gateway_cheats);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CheatEngine::RunCallback([[maybe_unused]] std::uintptr_t user_data, s64 cycles_late) {
|
|
|
|
|
{
|
|
|
|
|
std::shared_lock<std::shared_mutex> 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);
|
|
|
|
|
}
|
|
|
|
|