Skip to content

Commit 324bab5

Browse files
committed
Fix crash with next generation graphics
1 parent 608b15b commit 324bab5

7 files changed

Lines changed: 48 additions & 0 deletions

File tree

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@
1010
[submodule "vendor/catch2"]
1111
path = vendor/catch2
1212
url = https://github.com/catchorg/Catch2
13+
[submodule "vendor/jitasm"]
14+
path = vendor/jitasm
15+
url = https://github.com/TombRaiderModding/jitasm.git

src/modules/Patches.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <Hooking.Patterns.h>
22
#include <MinHook.h>
3+
#include <jitasm.h>
34
#include <thread>
45

56
#include "Patches.h"
@@ -145,6 +146,9 @@ Patches::Patches()
145146

146147
// Fix issues with dynamic drawing when using next generation graphics
147148
MH_CreateHook((void*)0x402EF0, TransToDrawVertexV4f, nullptr);
149+
150+
// Fix issue with some broken models in next generation graphics reading out of bounds
151+
PatchDrawableHelper();
148152
#endif
149153

150154
// Patches
@@ -204,6 +208,35 @@ void Patches::PatchShadowMap() const noexcept
204208

205209
Hooking::Patch(match.get_first(1), m_shadowMapSize.GetValue());
206210
}
211+
212+
void Patches::PatchDrawableHelper() const noexcept
213+
{
214+
auto match = hook::pattern("05 ? ? ? ? 0F 28 08 0F 58 C1").count_hint(1);
215+
216+
if (match.empty())
217+
{
218+
return;
219+
}
220+
221+
static struct : jitasm::Frontend
222+
{
223+
void InternalMain()
224+
{
225+
cmp(eax, 256 * 16); // Check for overflow on pivots
226+
jg("fail");
227+
add(eax, 0x113F880); // Original code
228+
229+
mov(edx, 0x5C136B); // Jump to normal code
230+
jmp(edx);
231+
232+
L("fail");
233+
mov(edx, 0x5C1505); // Jump to end of function
234+
jmp(edx);
235+
}
236+
} stub;
237+
238+
Hooking::Jump(match.get_first(), stub.GetCode());
239+
}
207240
#endif
208241

209242
#ifdef TR8

src/modules/Patches.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class Patches : public Module
5757
void RemoveIntro() const noexcept;
5858
void PatchHeapSize() const noexcept;
5959
void PatchShadowMap() const noexcept;
60+
void PatchDrawableHelper() const noexcept;
6061

6162
public:
6263
Patches();

src/util/Hooking.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,10 @@ void Hooking::Patch(void* address, unsigned int data) noexcept
3030
memcpy(address, &data, sizeof(data));
3131

3232
VirtualProtect(address, sizeof(data), oldProtect, &oldProtect);
33+
}
34+
35+
void Hooking::Jump(void* address, void* location)
36+
{
37+
Patch(address, { 0xE9 });
38+
Patch((void*)((char*)address + 1), (uintptr_t)location - (uintptr_t)address - 5);
3339
}

src/util/Hooking.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ class Hooking
4242

4343
// Patches data at a memory region
4444
static void Patch(void* address, unsigned int data) noexcept;
45+
46+
// Inserts a jump at a memory region
47+
static void Jump(void* address, void* location);
4548
};
4649

4750
#if TR7

vendor.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ files {
99
includedirs {
1010
"vendor/minhook/include",
1111
"vendor/patterns",
12+
"vendor/jitasm",
1213
"vendor/imgui",
1314
"vendor/imgui/backends"
1415
}

vendor/jitasm

Submodule jitasm added at b3c8f9e

0 commit comments

Comments
 (0)