forked from AUTOMATIC1111/IntoTheBreachLua
-
Notifications
You must be signed in to change notification settings - Fork 3
/
sdl-hooks.cpp
191 lines (152 loc) · 4.69 KB
/
sdl-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
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
183
184
185
186
187
188
189
190
191
#include <windows.h>
#include "utils.h"
#include "lua5.1.h"
#include "sdl2.h"
#include "opengl32.h"
#include "SDL.h"
#include "sdl-utils.h"
#include "glew/glew.h"
#include <GL/GL.h>
#include <GL/GLU.h>
#include "glext.h"
#include "xxhash.h"
HOOK_SDL(SDL_GL_SwapWindow, void, (SDL_Window * window)) {
if(! SDL::hookListDraw.empty()) {
SDL::Screen screen;
screen.begin();
for(auto i = SDL::hookListDraw.rbegin(); i != SDL::hookListDraw.rend(); ++i) {
SDL::DrawHook *hook = *i;
hook->draw(screen);
}
screen.finishWithoutSwapping();
}
SDL::lastFrameMap.clear();
return (*dll_SDL_GL_SwapWindow)(window);
}
HOOK_SDL(SDL_PollEvent, int, (SDL_Event *evt)) {
if(SDL::hookListEvents.empty() || evt==NULL) {
return (*dll_SDL_PollEvent)(evt);
}
while(1) {
int ret = (*dll_SDL_PollEvent)(evt);
if(ret == 0) return 0;
SDL::Event eventObject;
eventObject.event = *evt;
bool handled = false;
for(SDL::EventHook *hook : SDL::hookListEvents) {
if(hook->handle(eventObject)) {
handled = true;
break;
}
}
if(!handled) {
return 1;
}
}
}
static GLuint currentBoundTexture;
static GLuint currentUsedTexture;
HOOK_OPENGL(glBindTexture, void, (GLenum target, GLuint texture)) {
if(target == GL_TEXTURE_2D) {
currentBoundTexture = texture;
}
return (*dll_glBindTexture)(target, texture);
}
struct Tl {
GLdouble x;
GLdouble y;
};
std::vector<Tl> tlStack = { Tl {0, 0} };
GLenum matrixMode;
HOOK_OPENGL(glMatrixMode, void, (GLenum mode)) {
matrixMode = mode;
return (*dll_glMatrixMode)(mode);
}
HOOK_OPENGL(glPushMatrix, void, ()) {
if(matrixMode == GL_MODELVIEW) {
tlStack.push_back(tlStack.at(tlStack.size() - 1));
}
return (*dll_glPushMatrix)();
}
HOOK_OPENGL(glPopMatrix, void, ()) {
if(matrixMode == GL_MODELVIEW && tlStack.size()>1) {
tlStack.pop_back();
}
return (*dll_glPopMatrix)();
}
HOOK_OPENGL(glLoadIdentity, void, ()) {
if(matrixMode == GL_MODELVIEW) {
Tl &tl = tlStack.at(tlStack.size() - 1);
tl.x = 0;
tl.y = 0;
}
return (*dll_glLoadIdentity)();
}
HOOK_OPENGL(glTranslatef, void, (GLfloat x, GLfloat y, GLfloat z)) {
if(matrixMode == GL_MODELVIEW) {
Tl &tl = tlStack.at(tlStack.size() - 1);
tl.x += x;
tl.y += y;
}
(*dll_glTranslatef)(x, y, z);
}
HOOK_OPENGL(glScalef, void, (GLfloat x, GLfloat y, GLfloat z)) {
return (*dll_glScalef)(x, y, z);
}
static std::map<unsigned long long, SDL::Coord *> coordsToBeFixed;
static int glVertex2fNum = 0;
HOOK_OPENGL(glVertex2f, void, (GLfloat x, GLfloat y)) {
if(glVertex2fNum++ % 4 == 0) {
if(currentUsedTexture != currentBoundTexture) {
currentUsedTexture = currentBoundTexture;
auto iter = SDL::texturesMap.find(currentBoundTexture);
if(iter != SDL::texturesMap.end()) {
unsigned long long hash = iter->second;
SDL::Coord & coord = SDL::lastFrameMap[hash];
Tl &tl = tlStack.at(tlStack.size() - 1);
coord.x = x + tl.x;
coord.y = y + tl.y;
}
}
}
return (*dll_glVertex2f)(x, y);
}
HOOK_OPENGL(glVertexPointer, void, (GLint size, GLenum type, GLsizei stride, const GLvoid *pointer)) {
if (size >= 2 && type == GL_FLOAT && pointer) {
if (currentUsedTexture != currentBoundTexture) {
currentUsedTexture = currentBoundTexture;
auto iter = SDL::texturesMap.find(currentBoundTexture);
if (iter != SDL::texturesMap.end()) {
unsigned long long hash = iter->second;
SDL::Coord & coord = SDL::lastFrameMap[hash];
Tl &tl = tlStack.at(tlStack.size() - 1);
coord.x = ((float*)pointer)[0] + tl.x;
coord.y = ((float*)pointer)[1] + tl.y;
}
}
}
return (*dll_glVertexPointer)(size, type, stride, pointer);
}
HOOK_OPENGL(glVertexAttribPointer, void, (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void * pointer)) {
if (index == 0 && size >= 2 && type == GL_FLOAT && pointer) {
if (currentUsedTexture != currentBoundTexture) {
currentUsedTexture = currentBoundTexture;
auto iter = SDL::texturesMap.find(currentBoundTexture);
if (iter != SDL::texturesMap.end()) {
unsigned long long hash = iter->second;
SDL::Coord & coord = SDL::lastFrameMap[hash];
Tl &tl = tlStack.at(tlStack.size() - 1);
coord.x = ((float*)pointer)[0] + tl.x;
coord.y = ((float*)pointer)[1] + tl.y;
}
}
}
return (*dll_glVertexAttribPointer)(index, size, type, normalized, stride, pointer);
}
HOOK_OPENGL(glTexImage2D, void, (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels)) {
if(format == 6408) {
unsigned long long const hash = XXH64(pixels, width * height * 4, 0);
SDL::texturesMap[currentBoundTexture] = hash;
}
return (*dll_glTexImage2D)(target, level, internalformat, width, height, border, format, type, pixels);
}