/
PatchTorsoIkCrash.cpp
80 lines (63 loc) · 1.76 KB
/
PatchTorsoIkCrash.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <StdInc.h>
#include <jitasm.h>
#include <Hooking.h>
static hook::cdecl_stub<int(void*, uint16_t)> getBoneIndexFromTag([]()
{
return hook::get_call(hook::get_pattern("E8 ? ? ? ? 66 89 46 02"));
});
static constexpr uint16_t kRequiredBoneTags[5] {
0xE0FD, // SKEL_Spine_Root
0x5C01, // SKEL_Spine0
0x60F0, // SKEL_Spine1
0x60F1, // SKEL_Spine2
0x60F2, // SKEL_Spine3
};
static bool IsPedSkeletonApplicable(char* ped, char* skeleton)
{
if (skeleton == nullptr)
{
return false;
}
for (auto boneTag : kRequiredBoneTags)
{
if (getBoneIndexFromTag(ped, boneTag) == -1)
{
return false;
}
}
return true;
}
static HookFunction hookFunction([]()
{
static struct : jitasm::Frontend
{
intptr_t retSuccess;
intptr_t retFail;
void Init(intptr_t success, intptr_t fail)
{
this->retSuccess = success;
this->retFail = fail;
}
virtual void InternalMain() override
{
mov(rcx, qword_ptr[rdi + 0x30]); // CPed*
mov(rdx, r15); // rage::crSkeleton*
mov(rax, (uintptr_t)IsPedSkeletonApplicable);
call(rax);
test(al, al);
jz("fail");
mov(rax, retSuccess);
jmp(rax);
L("fail");
mov(rax, retFail);
jmp(rax);
}
} patchStub;
{
auto start = hook::get_pattern("4D 85 FF 0F 84 83 09 00 00", 0);
auto finish = hook::get_pattern("8B 95 A0 01 00 00 41 B0 01 49 8B CF", 17);
patchStub.Init(reinterpret_cast<intptr_t>(start) + 9, reinterpret_cast<intptr_t>(finish));
hook::nop(start, 9);
hook::jump(start, patchStub.GetCode());
}
});