-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.cpp
182 lines (141 loc) · 5.11 KB
/
main.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <windows.h>
#define GLEW_STATIC
#if defined _M_X64
#include "GLx64/glew.h"
#pragma comment(lib, "GLx64/glew32s.lib")
#elif defined _M_IX86
#include "GLx86/glew.h"
#pragma comment(lib, "GLx86/glew32s.lib")
#endif
#include <gl/gl.h>
#pragma comment(lib,"opengl32.lib")
#include "MinHook/include/MinHook.h" //detour x86&x64
//add all minhook files to your project
//==========================================================================================================================
bool FirstInit = false; //init once
#include <fstream>
using namespace std;
char dlldir[320];
char* GetDirectoryFile(char *filename)
{
static char path[320];
strcpy_s(path, dlldir);
strcat_s(path, filename);
return path;
}
void Log(const char *fmt, ...)
{
if (!fmt) return;
char text[4096];
va_list ap;
va_start(ap, fmt);
vsprintf_s(text, fmt, ap);
va_end(ap);
ofstream logfile(GetDirectoryFile("log.txt"), ios::app);
if (logfile.is_open() && text) logfile << text << endl;
logfile.close();
}
//==========================================================================================================================
//Typedef the function prototype straight from MSDN
typedef BOOL(__stdcall * twglSwapBuffers) (_In_ HDC hDc);
//Create instance of function
twglSwapBuffers owglSwapBuffers;
//Execution will get detoured to this
BOOL __stdcall hwglSwapBuffers(_In_ HDC hDc)
{
//draw text here
//Log("hwglSwapBuffers");
if (FirstInit == FALSE)
{
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
//Log("Failed to initialize GLEW");
}
FirstInit = TRUE;
}
//return execution to original function
return owglSwapBuffers(hDc);
}
//==========================================================================================================================
typedef void(*func_glDrawElementsBaseVertex_t) (GLenum mode, GLsizei count, GLenum type, GLvoid *indices, GLint basevertex);
func_glDrawElementsBaseVertex_t oglDrawElementsBaseVertex;
void hglDrawElementsBaseVertex(GLenum mode, GLsizei count, GLenum type, GLvoid *indices, GLint basevertex)
{
//wallhack here
oglDrawElementsBaseVertex(mode, count, type, indices, basevertex);
}
//==========================================================================================================================
typedef void(*func_glBindMultiTextureEXT_t) (GLenum texunit, GLenum target, GLuint texture);
func_glBindMultiTextureEXT_t oglBindMultiTextureEXT;
void hglBindMultiTextureEXT(GLenum texunit, GLenum target, GLuint texture)
{
//model rec here
glColorMask(0, 1, 0, 1); //test
oglBindMultiTextureEXT(texunit, target, texture);
}
//==========================================================================================================================
typedef PROC(*func_wglGetProcAddress_t) (LPCSTR lpszProc);
static func_wglGetProcAddress_t _wglGetProcAddress;
func_wglGetProcAddress_t owglGetProcAddress;
PROC hwglGetProcAddress(LPCSTR ProcName)
{
//Log("hwglGetProcAddress");
//wglGetProcAddress is for opengl extensions
if (!strcmp(ProcName, "glDrawElementsBaseVertex"))
{
oglDrawElementsBaseVertex = (func_glDrawElementsBaseVertex_t)owglGetProcAddress(ProcName);
return (FARPROC)hglDrawElementsBaseVertex;
}
else if (!strcmp(ProcName, "glBindMultiTextureEXT"))
{
oglBindMultiTextureEXT = (func_glBindMultiTextureEXT_t)owglGetProcAddress(ProcName);
return (FARPROC)hglBindMultiTextureEXT;
}
//glBufferSubData
//glVertexAttribPointer
//glViewportIndexedf
//glGetUniformLocation
//glBufferData
//glCompressedTexSubImage2DARB
return owglGetProcAddress(ProcName);
}
//==========================================================================================================================
DWORD WINAPI OpenglInit(__in LPVOID lpParameter)
{
while (GetModuleHandle("opengl32.dll") == 0)
{
Sleep(100);
}
//HMODULE dll = LoadLibrary(TEXT("opengl32"));
HMODULE hMod = GetModuleHandle("opengl32.dll");
if (hMod)
{
//use GetProcAddress to find address of wglSwapBuffers in opengl32.dll
void* ptr = GetProcAddress(hMod, "wglSwapBuffers");
MH_Initialize();
MH_CreateHook(ptr, hwglSwapBuffers, reinterpret_cast<void**>(&owglSwapBuffers));
MH_EnableHook(ptr);
_wglGetProcAddress = (func_wglGetProcAddress_t)GetProcAddress(hMod, "wglGetProcAddress");
//MH_Initialize();
MH_CreateHook(_wglGetProcAddress, hwglGetProcAddress, (void**)&owglGetProcAddress);
MH_EnableHook(_wglGetProcAddress);
}
return 1;
}
//==========================================================================================================================
BOOL __stdcall DllMain (HINSTANCE hinstDll, DWORD fdwReason, LPVOID lpvReserved)
{
switch(fdwReason)
{
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls (hinstDll);
GetModuleFileName(hinstDll, dlldir, 512);
for (int i = strlen(dlldir); i > 0; i--) { if (dlldir[i] == '\\') { dlldir[i + 1] = 0; break; } }
CreateThread(0, 0, OpenglInit, 0, 0, 0); //init
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}