From a2a862b07a94429c5007109281a6b2a97e85a6d1 Mon Sep 17 00:00:00 2001 From: TheIndra55 Date: Sat, 27 Apr 2024 04:12:33 +0200 Subject: [PATCH] Add heap and shadow map patches Co-authored-by: Half-asian --- src/modules/Patches.cpp | 29 +++++++++++++++++++++++------ src/modules/Patches.h | 40 ++++++++++++++++++++++++++++++++++++++-- src/util/Helpers.cpp | 26 ++++++++++++++++++++++++++ src/util/Helpers.h | 10 ++++++++++ 4 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 src/util/Helpers.cpp create mode 100644 src/util/Helpers.h diff --git a/src/modules/Patches.cpp b/src/modules/Patches.cpp index 776430c..08da7aa 100644 --- a/src/modules/Patches.cpp +++ b/src/modules/Patches.cpp @@ -7,7 +7,7 @@ #include "MainMenu.h" #include "patches/Reloc.h" -// Instance of patches so we can get it in our hooks without calling GetModule +// Instance of patches so we can get it in our hooks without calling GetModule each call static Patches* s_patches; static MainMenu* s_menu; @@ -94,11 +94,6 @@ Patches::Patches() MH_CreateHook((void*)GET_ADDRESS(0x4642F0, 0x467E60, 0x000000), MakePeHandle, nullptr); #endif - if (m_heapSize.GetValue() > 0) - { - PatchHeapSize(); - } - // Insert DeathState hooks MH_CreateHook((void*)GET_ADDRESS(0x55DEC0, 0x5581D0, 0x75AA50), DeathState_Entry, (void**)&s_DeathState_Entry); MH_CreateHook((void*)GET_ADDRESS(0x56EC70, 0x5699C0, 0x75AF90), DeathState_Process, (void**)&s_DeathState_Process); @@ -106,6 +101,19 @@ Patches::Patches() // NOP the original death wipe code in DeathState::Entry Hooking::Nop((void*)GET_ADDRESS(0x55E188, 0x5584DC, 0x75AEDE), 5); + // Patches + if (m_heapSize.GetValue() > 0) + { + PatchHeapSize(); + } + +#ifdef TR7 + if (m_shadowMapSize.GetValue() > 0) + { + PatchShadowMap(); + } +#endif + MH_EnableHook(MH_ALL_HOOKS); } @@ -131,6 +139,15 @@ void Patches::PatchHeapSize() const noexcept Hooking::Patch(match.get_first(19), size); } +#ifdef TR7 +void Patches::PatchShadowMap() const noexcept +{ + auto match = hook::pattern("BF 00 04 00 00 3B C7 8B F1 BA 80 00 00 00").count(1); + + Hooking::Patch(match.get_first(1), m_shadowMapSize.GetValue()); +} +#endif + void Patches::OnInput(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { // Remove the quit message diff --git a/src/modules/Patches.h b/src/modules/Patches.h index a4c561a..55322e2 100644 --- a/src/modules/Patches.h +++ b/src/modules/Patches.h @@ -1,7 +1,39 @@ #pragma once - + +#include + #include "Module.h" #include "Options.h" +#include "util/Helpers.h" + +template +class Patch +{ +private: + T m_value; + + static int GetPatchValue(const char* name, int defaultValue) noexcept + { + auto path = std::filesystem::current_path() / "patches.ini"; + + // Read the value from the ini file + char value[20]; + GetPrivateProfileStringA("Patches", name, NULL, value, sizeof(value), path.string().c_str()); + + return Helpers::StringToInt(std::string(value), defaultValue); + } + +public: + Patch(const char* name, T defaultValue) + { + m_value = GetPatchValue(name, defaultValue); + } + + T GetValue() const noexcept + { + return m_value; + } +}; class Patches : public Module { @@ -10,10 +42,14 @@ class Patches : public Module Option m_noCinematicBars{ "NoCinematicBars", true }; Option m_noMotionBlur{ "NoMotionBlur", false }; - Option m_heapSize{ "HeapSize", 0 }; + Patch m_heapSize{ "HeapSize", 0 }; +#ifdef TR7 + Patch m_shadowMapSize{ "MaxShadowMap", 0 }; +#endif void RemoveIntro() const noexcept; void PatchHeapSize() const noexcept; + void PatchShadowMap() const noexcept; public: Patches(); diff --git a/src/util/Helpers.cpp b/src/util/Helpers.cpp new file mode 100644 index 0000000..fa20b27 --- /dev/null +++ b/src/util/Helpers.cpp @@ -0,0 +1,26 @@ +#include "Helpers.h" + +#include + +int Helpers::StringToInt(const std::string& str, int defaultValue) +{ + int value; + auto [p, ec] = std::from_chars(str.data(), str.data() + str.size(), value); + + if (ec != std::errc()) + { + return defaultValue; + } + + if (p[0] == 'M') + { + value <<= 20; + } + + if (p[0] == 'K') + { + value <<= 10; + } + + return value; +} \ No newline at end of file diff --git a/src/util/Helpers.h b/src/util/Helpers.h new file mode 100644 index 0000000..1d76388 --- /dev/null +++ b/src/util/Helpers.h @@ -0,0 +1,10 @@ +#pragma once + +#include + +class Helpers +{ +public: + // Converts a value to integer with support for units such as megabytes + static int StringToInt(const std::string& value, int defaultValue); +}; \ No newline at end of file