From c03e1c78e720e673d3d46947dbc1ce8e687ec217 Mon Sep 17 00:00:00 2001 From: DanielSvoboda Date: Fri, 23 Aug 2024 12:12:25 -0300 Subject: [PATCH] convertValueToHex --- src/common/memory_patcher.cpp | 3 ++ src/common/memory_patcher.h | 3 ++ src/qt_gui/cheats_patches.cpp | 87 +++++++++++++++++++++++++++++------ src/qt_gui/cheats_patches.h | 1 + 4 files changed, 81 insertions(+), 13 deletions(-) diff --git a/src/common/memory_patcher.cpp b/src/common/memory_patcher.cpp index af8b425c6..27cb65670 100644 --- a/src/common/memory_patcher.cpp +++ b/src/common/memory_patcher.cpp @@ -1,3 +1,6 @@ +// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + #include "common/logging/log.h" #include "memory_patcher.h" diff --git a/src/common/memory_patcher.h b/src/common/memory_patcher.h index f9f76a324..9e2da65f7 100644 --- a/src/common/memory_patcher.h +++ b/src/common/memory_patcher.h @@ -1,3 +1,6 @@ +// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + #pragma once #include #include diff --git a/src/qt_gui/cheats_patches.cpp b/src/qt_gui/cheats_patches.cpp index 32eea09fe..bb338109e 100644 --- a/src/qt_gui/cheats_patches.cpp +++ b/src/qt_gui/cheats_patches.cpp @@ -216,12 +216,14 @@ void CheatsPatches::downloadCheats(const QString& url) { cheatFile.close(); loadCheats(m_cheatFilePath); } - QMessageBox::information(this, "Cheats downloaded successfully", + QMessageBox::information(this, "Cheats Downloaded Successfully", "You have successfully downloaded the cheats for this version " - "of the game. (serial+version)"); + "of the gamegame from this repository."); } else { - QMessageBox::warning(this, "Cheats not found", - "No Cheats found for this game in this version. (serial+version)"); + QMessageBox::warning( + this, "Cheats Not Found", + "No Cheats found in this repository for this game in this version. Try downloading " + "from another repository or updating your game."); } reply->deleteLater(); }); @@ -603,15 +605,7 @@ void CheatsPatches::updateNoteTextEdit(const QString& patchName) { // .arg(type) // .arg(address) // .arg(patchValue)); - - // implement - // modify the memory before starting using /\ "type,address,patchValue" - // before doing a value conversion depending on the 'type', as per the table - // https://github.com/GoldHEN/GoldHEN_Patch_Repository/tree/main?tab=readme-ov-file#patch-types - // Creates the applyPatches function ? - // start game button } - text.replace("\\n", "\n"); instructionsTextEdit->setText(text); } @@ -663,6 +657,8 @@ void CheatsPatches::applyPatch(const QString& patchName, bool enabled) { QString address = lineObject["Address"].toString(); QString patchValue = lineObject["Value"].toString(); + patchValue = convertValueToHex(type, patchValue); + if (MemoryPatcher::g_eboot_address == 0) { MemoryPatcher::patchInfo addingPatch; addingPatch.modNameStr = patchName.toStdString(); @@ -679,6 +675,71 @@ void CheatsPatches::applyPatch(const QString& patchName, bool enabled) { } } } +QString toHex(unsigned long long value, size_t byteSize) { + std::stringstream ss; + ss << std::hex << std::setfill('0') << std::setw(byteSize * 2) << value; + return QString::fromStdString(ss.str()); +} + +QString CheatsPatches::convertValueToHex(const QString& type, const QString& valueStr) { + QString result; + std::string typeStr = type.toStdString(); + std::string valueStrStd = valueStr.toStdString(); + + if (typeStr == "byte") { + unsigned int value = std::stoul(valueStrStd, nullptr, 16); + result = toHex(value, 1); + } else if (typeStr == "bytes16") { + unsigned int value = std::stoul(valueStrStd, nullptr, 16); + result = toHex(value, 2); + } else if (typeStr == "bytes32") { + unsigned long value = std::stoul(valueStrStd, nullptr, 16); + result = toHex(value, 4); + } else if (typeStr == "bytes64") { + unsigned long long value = std::stoull(valueStrStd, nullptr, 16); + result = toHex(value, 8); + } else if (typeStr == "float32") { + union { + float f; + uint32_t i; + } floatUnion; + floatUnion.f = std::stof(valueStrStd); + result = toHex(floatUnion.i, sizeof(floatUnion.i)); + } else if (typeStr == "float64") { + union { + double d; + uint64_t i; + } doubleUnion; + doubleUnion.d = std::stod(valueStrStd); + result = toHex(doubleUnion.i, sizeof(doubleUnion.i)); + } else if (typeStr == "utf8") { + QByteArray byteArray = QString::fromStdString(valueStrStd).toUtf8(); + byteArray.append('\0'); + std::stringstream ss; + for (unsigned char c : byteArray) { + ss << std::hex << std::setfill('0') << std::setw(2) << static_cast(c); + } + result = QString::fromStdString(ss.str()); + } else if (typeStr == "utf16") { + QByteArray byteArray( + reinterpret_cast(QString::fromStdString(valueStrStd).utf16()), + QString::fromStdString(valueStrStd).size() * 2); + byteArray.append('\0'); + byteArray.append('\0'); + std::stringstream ss; + for (unsigned char c : byteArray) { + ss << std::hex << std::setfill('0') << std::setw(2) << static_cast(c); + } + result = QString::fromStdString(ss.str()); + } else if (typeStr == "bytes") { + result = valueStr; + } else if (typeStr == "mask" || typeStr == "mask_jump32") { + result = valueStr; + } else { + LOG_INFO(Loader, "Error applying Patch, unknown type: {}", typeStr); + } + return result; +} bool CheatsPatches::eventFilter(QObject* obj, QEvent* event) { if (event->type() == QEvent::HoverEnter || event->type() == QEvent::HoverLeave) { @@ -700,4 +761,4 @@ void CheatsPatches::onPatchCheckBoxHovered(QCheckBox* checkBox, bool hovered) { } else { instructionsTextEdit->setText(defaultTextEdit); } -} \ No newline at end of file +} diff --git a/src/qt_gui/cheats_patches.h b/src/qt_gui/cheats_patches.h index ad0c0e914..8e9226824 100644 --- a/src/qt_gui/cheats_patches.h +++ b/src/qt_gui/cheats_patches.h @@ -51,6 +51,7 @@ private: void uncheckAllCheatCheckBoxes(); void applyCheat(const QString& modName, bool enabled); void applyPatch(const QString& patchName, bool enabled); + QString convertValueToHex(const QString& type, const QString& valueStr); // Event Filtering bool eventFilter(QObject* obj, QEvent* event);