add check to stop patches applying to wrong game

previously if you added a patch to a game, but closed the window and opened a different game it would still try to apply the patch, this is now fixed
This commit is contained in:
CrazyBloo 2024-08-24 08:48:57 -04:00
parent 5377ae9c37
commit 50a359d0eb
5 changed files with 24 additions and 14 deletions

View File

@ -10,6 +10,7 @@ namespace MemoryPatcher {
uintptr_t g_eboot_address; uintptr_t g_eboot_address;
u64 g_eboot_image_size; u64 g_eboot_image_size;
std::string g_game_serial;
std::vector<patchInfo> pending_patches; std::vector<patchInfo> pending_patches;
@ -19,11 +20,12 @@ void AddPatchToQueue(patchInfo patchToAdd) {
void ApplyPendingPatches() { void ApplyPendingPatches() {
//TODO: need to verify that the patch is actually for the game we open,
//if we enable patches but open a different game they will still attempt to load
for (size_t i = 0; i < pending_patches.size(); ++i) { for (size_t i = 0; i < pending_patches.size(); ++i) {
patchInfo currentPatch = pending_patches[i]; patchInfo currentPatch = pending_patches[i];
if (currentPatch.gameSerial != g_game_serial)
continue;
PatchMemory(currentPatch.modNameStr, currentPatch.offsetStr, currentPatch.valueStr, PatchMemory(currentPatch.modNameStr, currentPatch.offsetStr, currentPatch.valueStr,
currentPatch.isOffset, currentPatch.littleEndian, currentPatch.patchMask, currentPatch.isOffset, currentPatch.littleEndian, currentPatch.patchMask,
currentPatch.maskOffset); currentPatch.maskOffset);
@ -50,7 +52,7 @@ void PatchMemory(std::string modNameStr, std::string offsetStr, std::string valu
cheatAddress = reinterpret_cast<void*>(PatternScan(offsetStr) + maskOffset); cheatAddress = reinterpret_cast<void*>(PatternScan(offsetStr) + maskOffset);
} }
//TODO: implement mask_jump32 // TODO: implement mask_jump32
if (cheatAddress == nullptr) { if (cheatAddress == nullptr) {
LOG_ERROR(Loader, "Failed to get address for patch {}", modNameStr); LOG_ERROR(Loader, "Failed to get address for patch {}", modNameStr);

View File

@ -10,6 +10,7 @@ namespace MemoryPatcher {
extern uintptr_t g_eboot_address; extern uintptr_t g_eboot_address;
extern u64 g_eboot_image_size; extern u64 g_eboot_image_size;
extern std::string g_game_serial;
enum PatchMask : uint8_t { enum PatchMask : uint8_t {
None, None,
@ -18,6 +19,7 @@ enum PatchMask : uint8_t {
}; };
struct patchInfo { struct patchInfo {
std::string gameSerial;
std::string modNameStr; std::string modNameStr;
std::string offsetStr; std::string offsetStr;
std::string valueStr; std::string valueStr;

View File

@ -13,6 +13,7 @@
#include "common/scm_rev.h" #include "common/scm_rev.h"
#include "common/singleton.h" #include "common/singleton.h"
#include "common/version.h" #include "common/version.h"
#include "common/memory_patcher.h"
#include "core/file_format/playgo_chunk.h" #include "core/file_format/playgo_chunk.h"
#include "core/file_format/psf.h" #include "core/file_format/psf.h"
#include "core/file_format/splash.h" #include "core/file_format/splash.h"
@ -93,6 +94,7 @@ void Emulator::Run(const std::filesystem::path& file) {
auto* param_sfo = Common::Singleton<PSF>::Instance(); auto* param_sfo = Common::Singleton<PSF>::Instance();
param_sfo->open(sce_sys_folder.string() + "/param.sfo", {}); param_sfo->open(sce_sys_folder.string() + "/param.sfo", {});
id = std::string(param_sfo->GetString("CONTENT_ID"), 7, 9); id = std::string(param_sfo->GetString("CONTENT_ID"), 7, 9);
MemoryPatcher::g_game_serial = id;
title = param_sfo->GetString("TITLE"); title = param_sfo->GetString("TITLE");
LOG_INFO(Loader, "Game id: {} Title: {}", id, title); LOG_INFO(Loader, "Game id: {} Title: {}", id, title);
u32 fw_version = param_sfo->GetInteger("SYSTEM_VER"); u32 fw_version = param_sfo->GetInteger("SYSTEM_VER");

View File

@ -748,14 +748,14 @@ void CheatsPatches::loadPatches(const QString& serial) {
QXmlStreamAttributes attributes = xmlReader.attributes(); QXmlStreamAttributes attributes = xmlReader.attributes();
QString appVer = attributes.value("AppVer").toString(); QString appVer = attributes.value("AppVer").toString();
if (appVer == m_gameVersion) { if (appVer == m_gameVersion) {
patchName = attributes.value("Name").toString(); patchName = attributes.value("Name").toString();
patchAuthor = attributes.value("Author").toString(); patchAuthor = attributes.value("Author").toString();
patchNote = attributes.value("Note").toString(); patchNote = attributes.value("Note").toString();
} }
if (appVer == "mask") { if (appVer == "mask") {
patchName = attributes.value("Name").toString(); patchName = attributes.value("Name").toString();
patchAuthor = attributes.value("Author").toString(); patchAuthor = attributes.value("Author").toString();
patchNote = attributes.value("Note").toString(); patchNote = attributes.value("Note").toString();
} }
} else if (xmlReader.name() == QStringLiteral("PatchList")) { } else if (xmlReader.name() == QStringLiteral("PatchList")) {
QJsonArray linesArray; QJsonArray linesArray;
@ -778,7 +778,7 @@ void CheatsPatches::loadPatches(const QString& serial) {
} }
if (!patchName.isEmpty() && !patchLines.isEmpty()) { if (!patchName.isEmpty() && !patchLines.isEmpty()) {
addPatchToLayout(patchName, patchAuthor, patchNote, patchLines); addPatchToLayout(patchName, patchAuthor, patchNote, patchLines, serial);
patchName.clear(); patchName.clear();
patchAuthor.clear(); patchAuthor.clear();
patchNote.clear(); patchNote.clear();
@ -791,7 +791,8 @@ void CheatsPatches::loadPatches(const QString& serial) {
} }
void CheatsPatches::addPatchToLayout(const QString& name, const QString& author, void CheatsPatches::addPatchToLayout(const QString& name, const QString& author,
const QString& note, const QJsonArray& linesArray) { const QString& note, const QJsonArray& linesArray,
const QString& serial) {
QCheckBox* patchCheckBox = new QCheckBox(name); QCheckBox* patchCheckBox = new QCheckBox(name);
patchesGroupBoxLayout->addWidget(patchCheckBox); patchesGroupBoxLayout->addWidget(patchCheckBox);
@ -800,6 +801,7 @@ void CheatsPatches::addPatchToLayout(const QString& name, const QString& author,
patchInfo.author = author; patchInfo.author = author;
patchInfo.note = note; patchInfo.note = note;
patchInfo.linesArray = linesArray; patchInfo.linesArray = linesArray;
patchInfo.serial = serial;
m_patchInfos[name] = patchInfo; m_patchInfos[name] = patchInfo;
// Hook checkbox hover events // Hook checkbox hover events
@ -898,7 +900,7 @@ void CheatsPatches::applyPatch(const QString& patchName, bool enabled) {
if (type == "mask") { if (type == "mask") {
patchMask = MemoryPatcher::PatchMask::Mask; patchMask = MemoryPatcher::PatchMask::Mask;
//im not sure if this works, there is no games to test the mask offset on yet // im not sure if this works, there is no games to test the mask offset on yet
if (!maskOffsetStr.toStdString().empty()) if (!maskOffsetStr.toStdString().empty())
maskOffsetValue = std::stoi(maskOffsetStr.toStdString(), 0, 10); maskOffsetValue = std::stoi(maskOffsetStr.toStdString(), 0, 10);
} }
@ -908,6 +910,7 @@ void CheatsPatches::applyPatch(const QString& patchName, bool enabled) {
if (MemoryPatcher::g_eboot_address == 0) { if (MemoryPatcher::g_eboot_address == 0) {
MemoryPatcher::patchInfo addingPatch; MemoryPatcher::patchInfo addingPatch;
addingPatch.gameSerial = patchInfo.serial.toStdString();
addingPatch.modNameStr = patchName.toStdString(); addingPatch.modNameStr = patchName.toStdString();
addingPatch.offsetStr = address.toStdString(); addingPatch.offsetStr = address.toStdString();
addingPatch.valueStr = patchValue.toStdString(); addingPatch.valueStr = patchValue.toStdString();

View File

@ -52,7 +52,7 @@ private:
void addCheatsToLayout(const QJsonArray& modsArray); void addCheatsToLayout(const QJsonArray& modsArray);
void addPatchToLayout(const QString& name, const QString& author, const QString& note, void addPatchToLayout(const QString& name, const QString& author, const QString& note,
const QJsonArray& linesArray); const QJsonArray& linesArray, const QString& serial);
void createFilesJson(); void createFilesJson();
void uncheckAllCheatCheckBoxes(); void uncheckAllCheatCheckBoxes();
@ -85,6 +85,7 @@ private:
QString author; QString author;
QString note; QString note;
QJsonArray linesArray; QJsonArray linesArray;
QString serial;
}; };
// Members // Members