-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHooks.cpp
113 lines (95 loc) · 3.03 KB
/
Hooks.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "Hooks.h"
#include "Main.h"
#include "FB SDK/Frostbite.h"
#include "Util/Log.h"
#include "MinHook.h"
#pragma comment(lib, "libMinHook.x86.lib")
typedef HRESULT(WINAPI * tD3D11Present)(IDXGISwapChain*, UINT, UINT);
typedef void(__thiscall* tVisualEnvUpdate)(void*, int);
typedef void(__stdcall* tCameraUpdate)(int,int,int,int);
tD3D11Present oD3D11Present = nullptr;
tCameraUpdate oCameraUpdate = nullptr;
tVisualEnvUpdate oVisualEnvUpdate = nullptr;
HRESULT WINAPI hD3D11Present(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags)
{
g_mainHandle->Capture();
g_mainHandle->GetUI()->Draw();
return oD3D11Present(pSwapChain, SyncInterval, Flags);
}
__declspec(naked) void hCameraUpdateAsm(int a1, int a2)
{
__asm
{
push ecx // save ecx
push ebp // save ebp
mov ebp, esp // save esp
sub esp, __LOCAL_SIZE // allocate space for local variable
}
int pMatrix;
pMatrix = a2;
g_mainHandle->GetCameraManager()->CameraHook(pMatrix);
__asm
{
mov esp, ebp // restore esp
pop ebp // restore ebp
pop ecx // restore ecx
jmp oCameraUpdate // jump to original
}
}
void __fastcall hVisualEnvUpdate(void* This, void* _EDX, int a2)
{
oVisualEnvUpdate(This,a2);
if(g_mainHandle->GetVisualOverrides().disableFog)
{
DWORD* fogBools = (DWORD*)((int)This + 0x2B4);
*fogBools = 0;
}
}
//
// Creates a normal hook with MinHook and enables it
//
void Hooks::CreateHook(const char* name, int target, PVOID hook, LPVOID* original)
{
if (MH_CreateHook((LPVOID)target, hook, original) != MH_OK)
{
//Log::Error("Could not create %s hook", name.c_str());
return;
}
if (MH_EnableHook((LPVOID)target) != MH_OK)
{
//Log::Error("Could not enable %s hook", name.c_str());
return;
}
//m_hooks.insert(std::pair<string, LPVOID>(name, (LPVOID)target));
}
//
// Hooks a function by changing the address at given index
// in the virtual function table
//
PBYTE WINAPI Hooks::HookVTableFunction(PDWORD* ppVTable, PBYTE pHook, SIZE_T iIndex)
{
DWORD dwOld = 0;
VirtualProtect((void*)((*ppVTable) + iIndex), sizeof(PDWORD), PAGE_EXECUTE_READWRITE, &dwOld);
PBYTE pOrig = ((PBYTE)(*ppVTable)[iIndex]);
(*ppVTable)[iIndex] = (DWORD)pHook;
VirtualProtect((void*)((*ppVTable) + iIndex), sizeof(PDWORD), dwOld, &dwOld);
return pOrig;
}
void Hooks::Init()
{
MH_STATUS status = MH_Initialize();
if (status != MH_OK)
{
Log::Error("Could not initialize MinHook, error code: 0x%X", status);
return;
}
CreateHook("Camera Update", 0x11A4560, hCameraUpdateAsm, (LPVOID*)&oCameraUpdate);
CreateHook("VisualEnv Update", 0x1791F30, hVisualEnvUpdate, (LPVOID*)&oVisualEnvUpdate);
oD3D11Present = (tD3D11Present)HookVTableFunction((PDWORD*)fb::DxRenderer::Singleton()->pSwapChain, (PBYTE)hD3D11Present, 8);
}
void Hooks::RemoveHooks()
{
MH_RemoveHook(MH_ALL_HOOKS);
MH_Uninitialize();
HookVTableFunction((PDWORD*)fb::DxRenderer::Singleton()->pSwapChain, (PBYTE)oD3D11Present, 8);
}