From 90f1d2c08f7e8bf06214d76a042c223d319463bf Mon Sep 17 00:00:00 2001 From: CrazyBloo Date: Fri, 23 Aug 2024 02:37:39 -0400 Subject: [PATCH] initial patch implementation --- src/common/memory_patcher.cpp | 15 +++++++++++---- src/common/memory_patcher.h | 3 ++- src/qt_gui/cheats_patches.cpp | 32 +++++++++++++++++++++++++++++++- src/qt_gui/cheats_patches.h | 1 + 4 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/common/memory_patcher.cpp b/src/common/memory_patcher.cpp index 336bf3ad..ca9d5bf6 100644 --- a/src/common/memory_patcher.cpp +++ b/src/common/memory_patcher.cpp @@ -15,16 +15,23 @@ void ApplyPendingPatches() { for (size_t i = 0; i < pending_patches.size(); ++i) { patchInfo currentPatch = pending_patches[i]; - LOG_INFO(Loader, "loading patch {}", i); - PatchMemory(currentPatch.modNameStr, currentPatch.offsetStr, currentPatch.valueStr); + PatchMemory(currentPatch.modNameStr, currentPatch.offsetStr, currentPatch.valueStr, + currentPatch.isOffset); } pending_patches.clear(); } -void PatchMemory(std::string modNameStr, std::string offsetStr, std::string valueStr) { +void PatchMemory(std::string modNameStr, std::string offsetStr, std::string valueStr, bool isOffset) { // Send a request to modify the process memory. - void* cheatAddress = reinterpret_cast(g_eboot_address + std::stoi(offsetStr, 0, 16)); + void* cheatAddress = nullptr; + + if (isOffset) { + cheatAddress = reinterpret_cast(g_eboot_address + std::stoi(offsetStr, 0, 16)); + } else { + cheatAddress = + reinterpret_cast(g_eboot_address + (std::stoi(offsetStr, 0, 16) - 0x400000)); + } std::vector bytePatch; diff --git a/src/common/memory_patcher.h b/src/common/memory_patcher.h index 43039fdb..80f8e44a 100644 --- a/src/common/memory_patcher.h +++ b/src/common/memory_patcher.h @@ -11,6 +11,7 @@ struct patchInfo { std::string modNameStr; std::string offsetStr; std::string valueStr; + bool isOffset; }; extern std::vector pending_patches; @@ -18,6 +19,6 @@ extern std::vector pending_patches; void AddPatchToQueue(patchInfo patchToAdd); void ApplyPendingPatches(); -void PatchMemory(std::string modNameStr, std::string offsetStr, std::string valueStr); +void PatchMemory(std::string modNameStr, std::string offsetStr, std::string valueStr, bool isOffset); } // namespace MemoryPatcher \ No newline at end of file diff --git a/src/qt_gui/cheats_patches.cpp b/src/qt_gui/cheats_patches.cpp index a58a07e7..5205a68c 100644 --- a/src/qt_gui/cheats_patches.cpp +++ b/src/qt_gui/cheats_patches.cpp @@ -580,6 +580,8 @@ void CheatsPatches::addPatchToLayout(const QString& name, const QString& author, // Hook checkbox hover events patchCheckBox->installEventFilter(this); + + connect(patchCheckBox, &QCheckBox::toggled, [=](bool checked) { applyPatch(name, checked); }); } void CheatsPatches::updateNoteTextEdit(const QString& patchName) { @@ -641,12 +643,40 @@ void CheatsPatches::applyCheat(const QString& modName, bool enabled) { addingPatch.modNameStr = modNameStr; addingPatch.offsetStr = offsetStr; addingPatch.valueStr = valueStr; + addingPatch.isOffset = true; MemoryPatcher::AddPatchToQueue(addingPatch); continue; } - MemoryPatcher::PatchMemory(modNameStr, offsetStr, valueStr); + MemoryPatcher::PatchMemory(modNameStr, offsetStr, valueStr, true); + } +} + +void CheatsPatches::applyPatch(const QString& patchName, bool enabled) { + if (m_patchInfos.contains(patchName)) { + const PatchInfo& patchInfo = m_patchInfos[patchName]; + + foreach (const QJsonValue& value, patchInfo.linesArray) { + QJsonObject lineObject = value.toObject(); + QString type = lineObject["Type"].toString(); + QString address = lineObject["Address"].toString(); + QString patchValue = lineObject["Value"].toString(); + + if (MemoryPatcher::g_eboot_address == 0) { + MemoryPatcher::patchInfo addingPatch; + addingPatch.modNameStr = patchName.toStdString(); + addingPatch.offsetStr = address.toStdString(); + addingPatch.valueStr = patchValue.toStdString(); + addingPatch.isOffset = false; + + MemoryPatcher::AddPatchToQueue(addingPatch); + continue; + } + + MemoryPatcher::PatchMemory(patchName.toStdString(), address.toStdString(), + patchValue.toStdString(), false); + } } } diff --git a/src/qt_gui/cheats_patches.h b/src/qt_gui/cheats_patches.h index d9cb7227..ad0c0e91 100644 --- a/src/qt_gui/cheats_patches.h +++ b/src/qt_gui/cheats_patches.h @@ -50,6 +50,7 @@ private: void createFilesJson(); void uncheckAllCheatCheckBoxes(); void applyCheat(const QString& modName, bool enabled); + void applyPatch(const QString& patchName, bool enabled); // Event Filtering bool eventFilter(QObject* obj, QEvent* event);