-
Notifications
You must be signed in to change notification settings - Fork 3
/
glhook.cpp
117 lines (91 loc) · 2.4 KB
/
glhook.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
114
115
116
117
#include "glhook.h"
// Vars
int hookMethod = 0;
DWORD glTLS = 0;
// Declarations
DWORD GetTblOffset(DWORD funcAddr);
DWORD HookGLNative(DWORD funcTblAddr, void *funcHook);
void UnhookGLNative(DWORD funcTblAddr, void *funcOrig);
int GLInit()
{
__asm
{
push eax
mov eax, dword ptr fs:[0x18]
mov glTLS, eax
pop eax
}
}
DWORD GLHook(char *funcName, void *funcHook)
{
DWORD funcAddr = 0;
DWORD funcTblAddr = 0;
DWORD funcNativeOrig = 0;
if (!glTLS || !funcHook)
return 0;
if (!(funcAddr = (DWORD)GetProcAddress(GetModuleHandle("opengl32.dll"), funcName)))
return 0;
if (!(funcTblAddr = GetTblOffset(funcAddr)))
return 0;
if (!(funcNativeOrig = HookGLNative(funcTblAddr, funcHook)))
return 0;
return funcNativeOrig;
}
bool GLUnhook(char *funcName, void *funcOrig)
{
DWORD funcAddr = 0;
DWORD funcTblAddr = 0;
DWORD funcNativeOrig = 0;
if (!glTLS || !funcOrig)
return false;
if (!(funcAddr = (DWORD)GetProcAddress(GetModuleHandle("opengl32.dll"), funcName)))
return false;
if (!(funcTblAddr = GetTblOffset(funcAddr)))
return false;
UnhookGLNative(funcTblAddr, funcOrig);
return true;
}
DWORD GetTblOffset(DWORD funcAddr)
{
if (*(BYTE*)(funcAddr + 0x07) == 0x83) {
hookMethod = 0;
return ((*(DWORD*)(glTLS + *(DWORD*)(*(DWORD*)(funcAddr + 0x14)))) + (*(DWORD*)(funcAddr + 0x1C)));
}
else if (*(BYTE*)(funcAddr + 0x07) == 0xA0) {
hookMethod = 1;
return (*(DWORD*)(funcAddr + 0x08));
}
return 0;
}
DWORD HookGLNative(DWORD funcTblAddr, void *funcHook)
{
DWORD funcNativeOrig = 0;
switch(hookMethod)
{
case 0:
funcNativeOrig = *(DWORD*)funcTblAddr;
*(DWORD*)funcTblAddr = (DWORD)funcHook;
break;
case 1:
funcNativeOrig = *(DWORD*)(glTLS + funcTblAddr);
*(DWORD*)(glTLS + funcTblAddr) = (DWORD)funcHook;
break;
default:
break;
}
return funcNativeOrig;
}
void UnhookGLNative(DWORD funcTblAddr, void *funcOrig)
{
switch(hookMethod)
{
case 0:
*(DWORD*)funcTblAddr = (DWORD)funcOrig;
break;
case 1:
*(DWORD*)(glTLS + funcTblAddr) = (DWORD)funcOrig;
break;
default:
break;
}
}