Skip to content

Commit

Permalink
Add heap and shadow map patches
Browse files Browse the repository at this point in the history
Co-authored-by: Half-asian <[email protected]>
  • Loading branch information
TheIndra55 and Half-asian committed Apr 27, 2024
1 parent e01f25b commit a2a862b
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 8 deletions.
29 changes: 23 additions & 6 deletions src/modules/Patches.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>
// Instance of patches so we can get it in our hooks without calling GetModule<T> each call
static Patches* s_patches;
static MainMenu* s_menu;

Expand Down Expand Up @@ -94,18 +94,26 @@ 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);

// 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);
}

Expand All @@ -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
Expand Down
40 changes: 38 additions & 2 deletions src/modules/Patches.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
#pragma once


#include <filesystem>

#include "Module.h"
#include "Options.h"
#include "util/Helpers.h"

template <typename T>
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
{
Expand All @@ -10,10 +42,14 @@ class Patches : public Module
Option<bool> m_noCinematicBars{ "NoCinematicBars", true };
Option<bool> m_noMotionBlur{ "NoMotionBlur", false };

Option<unsigned int> m_heapSize{ "HeapSize", 0 };
Patch<int> m_heapSize{ "HeapSize", 0 };
#ifdef TR7
Patch<int> m_shadowMapSize{ "MaxShadowMap", 0 };
#endif

void RemoveIntro() const noexcept;
void PatchHeapSize() const noexcept;
void PatchShadowMap() const noexcept;

public:
Patches();
Expand Down
26 changes: 26 additions & 0 deletions src/util/Helpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "Helpers.h"

#include <charconv>

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;
}
10 changes: 10 additions & 0 deletions src/util/Helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <string>

class Helpers
{
public:
// Converts a value to integer with support for units such as megabytes
static int StringToInt(const std::string& value, int defaultValue);
};

0 comments on commit a2a862b

Please sign in to comment.