diff --git a/src/core/module.cpp b/src/core/module.cpp index 775e1ef19..ad907dfc3 100644 --- a/src/core/module.cpp +++ b/src/core/module.cpp @@ -11,6 +11,7 @@ #include "core/loader/dwarf.h" #include "core/memory.h" #include "core/module.h" +#include "qt_gui/cheats_patches.h" namespace Core { @@ -90,6 +91,12 @@ void Module::LoadModuleToMemory(u32& max_tls_index) { LoadOffset += CODE_BASE_INCR * (1 + aligned_base_size / CODE_BASE_INCR); LOG_INFO(Core_Linker, "Loading module {} to {}", name, fmt::ptr(*out_addr)); + if (cheats_eboot_address == 0) { + if (name == "eboot") { + cheats_eboot_address = base_virtual_addr; + } + } + // Initialize trampoline generator. void* trampoline_addr = std::bit_cast(base_virtual_addr + aligned_base_size); Xbyak::CodeGenerator c(TrampolineSize, trampoline_addr); diff --git a/src/qt_gui/cheats_patches.cpp b/src/qt_gui/cheats_patches.cpp index e0be22b4b..b1b3e7841 100644 --- a/src/qt_gui/cheats_patches.cpp +++ b/src/qt_gui/cheats_patches.cpp @@ -22,6 +22,8 @@ #include "cheats_patches.h" #include "common/path_util.h" +uintptr_t cheats_eboot_address = 0; + CheatsPatches::CheatsPatches(const QString& gameName, const QString& gameSerial, const QString& gameVersion, const QString& gameSize, const QPixmap& gameImage, QWidget* parent) @@ -234,7 +236,23 @@ void CheatsPatches::applyCheat(const QString& modName, bool enabled) { LOG_INFO(Loader, "Cheat applied:{}, Offset:{}, Value:{}", modNameStr, offsetStr, valueStr); - // Implement // Send a request to modify the process memory. + if (cheats_eboot_address == 0) { + LOG_INFO(Loader, "Can't apply mod until a game has been started"); + return; + } + + void* cheatAddress = reinterpret_cast(cheats_eboot_address + std::stoi(offsetStr, 0, 16)); + + std::vector bytePatch; + + for (size_t i = 0; i < valueStr.length(); i += 2) { + unsigned char byte = + static_cast(std::strtol(valueStr.substr(i, 2).c_str(), nullptr, 16)); + + bytePatch.push_back(byte); + } + + std::memcpy(cheatAddress, bytePatch.data(), bytePatch.size()); } } diff --git a/src/qt_gui/cheats_patches.h b/src/qt_gui/cheats_patches.h index bcd2679c0..fe2da8d33 100644 --- a/src/qt_gui/cheats_patches.h +++ b/src/qt_gui/cheats_patches.h @@ -16,6 +16,8 @@ #include #include +extern uintptr_t cheats_eboot_address; + class CheatsPatches : public QWidget { Q_OBJECT