-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBWUtil.cpp
More file actions
438 lines (390 loc) · 10.4 KB
/
BWUtil.cpp
File metadata and controls
438 lines (390 loc) · 10.4 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// BWUtil.cpp: implementation of the BWUtil class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "APMAlert.h"
#include "BWUtil.h"
#include "Logger.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
BWUtil* BWUtil::g_util;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
BWUtil::BWUtil()
{
patchflag = false;
}
BWUtil::~BWUtil()
{
for (std::vector<PatchedJmp*>::iterator it = m_patchedVec.begin();
it != m_patchedVec.end(); it++)
{
delete[] (*it)->origMemory;
delete[] (*it)->newMemory;
delete *it;
}
for (std::vector<PatchedMem*>::iterator it2 = m_patchedMemVec.begin();
it2 != m_patchedMemVec.end(); it2++)
{
delete[] (*it2)->origMemory;
delete[] (*it2)->newMemory;
delete *it2;
}
}
BWUtil* BWUtil::getInst()
{
if (!g_util)
g_util = new BWUtil();
return g_util;
}
void BWUtil::releaseInst()
{
if (g_util)
{
delete g_util;
g_util = NULL;
}
}
void BWUtil::WriteMem(DWORD memOffset, DWORD dataptr, DWORD datalen)
{
DWORD oldprot;
//DWORD oldprot2;
VirtualProtect((LPVOID)memOffset, datalen, PAGE_EXECUTE_READWRITE, &oldprot);
//VirtualProtect((LPVOID)dataptr, datalen, PAGE_EXECUTE_READWRITE, &oldprot2);
CopyMemory((LPVOID)memOffset, (LPVOID)dataptr, datalen);
VirtualProtect((LPVOID)memOffset, datalen, oldprot, &oldprot);
//VirtualProtect((LPVOID)dataptr, datalen, oldprot2, &oldprot2);
}
void BWUtil::CallOrJumpPatch(DWORD location, DWORD dst, byte flag)
{
unsigned char * ddd = new unsigned char[5];
ddd[0] = flag; // call
*((PDWORD)&ddd[1]) = (dst - location) - 5;
WriteMem(location, (DWORD)ddd, 5);
delete[] ddd;
ddd = NULL;
}
void BWUtil::AddPatch2(DWORD dAddr, DWORD dJmpTo, byte exxarg, int whenrun)
{
int needmem = 2 + 5 + 5 + 5; // pushad popad, call myfunction, jmp orgin dst, jmp after origin function
if (exxarg > 0)
{
needmem += 1;// push ebx
}
PatchedJmp* p = new PatchedJmp();
p->addr = dAddr;
p->jmpTo = dJmpTo;
p->origMemory = new byte[5];
p->newMemory = new byte[needmem];
p->numNops = 0;
memset(p->newMemory, 0x90, needmem); // fill with nop
DWORD oldprot;
VirtualProtect((LPVOID)p->newMemory, needmem, PAGE_EXECUTE_READWRITE, &oldprot);
for (int i = 0; i < 5; i++)
{
// ±£´æÇ°5¸öbyte
p->origMemory[i] = *(byte*)(dAddr + i);
p->newMemory[i] = p->origMemory[i];
}
CLogger::getinstance()->log("AddPatch2 %#x -> %#x originext%#x", p->addr, p->jmpTo, p->addr + 5);
DWORD arrpos = 0;
//handle redirecting jmps/calls
if (whenrun == 1)
{
if (p->origMemory[0] == 0xE8 || p->origMemory[0] == 0xE9)
{
p->newMemory[arrpos] = p->origMemory[0];
DWORD oldtarget = *(DWORD*)(&p->origMemory[1]);// old offset
oldtarget += p->addr + 5; // old target
*(DWORD*)(&p->newMemory[arrpos + 1]) = (oldtarget - (DWORD)(&p->newMemory[arrpos])) - 5;
arrpos += 5;
}
}
CLogger::getinstance()->log("AddPatch2 1 %#x -> %#x, %d", p->addr, p->jmpTo, arrpos);
DWORD pushad = 0x60;
p->newMemory[arrpos++] = pushad;
// ebx
if (exxarg > 0)
p->newMemory[arrpos++] = exxarg;
CallOrJumpPatch((DWORD)&p->newMemory[arrpos], p->jmpTo, 0xE8); // Call our hooking function
arrpos += 5;
CLogger::getinstance()->log("AddPatch2 2 %#x -> %#x, %d", p->addr, p->jmpTo, arrpos);
DWORD popad = 0x61;
p->newMemory[arrpos++] = popad;
if (whenrun == 2)
{
if (p->origMemory[0] == 0xE8 || p->origMemory[0] == 0xE9)
{
p->newMemory[arrpos] = p->origMemory[0];
DWORD oldtarget = *(DWORD*)(&p->origMemory[1]);// old offset
oldtarget += p->addr + 5; // old target
*(DWORD*)(&p->newMemory[arrpos + 1]) = (oldtarget - (DWORD)(&p->newMemory[arrpos])) - 5;
arrpos += 5;
}
}
CallOrJumpPatch((DWORD)&p->newMemory[arrpos], p->addr + 5, 0xE9); // Jump back right past where we overwrite
arrpos += 5;
CLogger::getinstance()->log("AddPatch2 2 %#x -> %#x, %d", p->addr, p->jmpTo, arrpos);
m_patchedVec.push_back(p);
}
void BWUtil::AddPatchMem(DWORD dAddr, byte* newMem, int memLength)
{
PatchedMem* p = new PatchedMem();
p->addr = dAddr;
p->memLength = memLength;
p->newMemory = new byte[memLength];
int i;
for (i = 0; i < memLength; i++)
p->newMemory[i] = newMem[i];
p->origMemory = new byte[memLength];
for (i = 0; i < memLength; i++)
p->origMemory[i] = *(byte*)(p->addr + i);
m_patchedMemVec.push_back(p);
}
void BWUtil::Patch()
{
const byte Nop = 0x90;
int i=0;
std::vector<PatchedJmp*>::iterator it;
for (it = m_patchedVec.begin(); it != m_patchedVec.end(); it++)
{
CallOrJumpPatch((*it)->addr, (DWORD)(*it)->newMemory, 0xE9);
for (i = 0; i < (*it)->numNops; i++)
{
WriteMem((*it)->addr + 5 + i, (DWORD)&Nop, 1);
}
}
std::vector<PatchedMem*>::iterator it2;
for (it2 = m_patchedMemVec.begin(); it2 != m_patchedMemVec.end(); it2++)
{
DWORD t = (DWORD)&(*it2)->newMemory;
DWORD t2 = (DWORD)&(*it2)->newMemory[0];
WriteMem((*it2)->addr, (DWORD)&(*it2)->newMemory[0], (*it2)->memLength);
}
patchflag = true;
CLogger::getinstance()->log("Patched game");
}
void BWUtil::UnPatch()
{
int i=0;
std::vector<PatchedJmp*>::iterator it;
for (it = m_patchedVec.begin(); it != m_patchedVec.end(); it++)
{
WriteMem((*it)->addr, (DWORD)(*it)->origMemory, 5 + (*it)->numNops);
}
std::vector<PatchedMem*>::iterator it2;
for (it2 = m_patchedMemVec.begin(); it2 != m_patchedMemVec.end(); it2++)
{
WriteMem((*it2)->addr, (DWORD)(*it2)->origMemory, (*it2)->memLength);
}
patchflag = false;
CLogger::getinstance()->log("UnPatched game");
}
void BWCenteredTextOut(char * text)
{
__asm {
pushad
mov esi, [text]
mov eax, -1
push 0h
push esi
mov esi, BWFXN_CTEXTOUT
call esi
popad
}
}
void BWTextOut(char * text)
{
__asm {
pushad
xor eax, eax
mov edi, text
mov esi, BWFXN_TEXTOUT
call esi
popad
}
}
void BWDrawBox(DWORD x, DWORD y, DWORD w, DWORD h, byte clr)
{
__asm {
pushad
mov cl, clr
mov eax, BOX_COLOR
mov byte ptr ds:[eax], cl
push h
push w
push y
push x
mov esi, BWFXN_DRAWBOX
call esi
popad
}
}
void BWDrawTransparentBox(unsigned int x, unsigned int y, unsigned int w, unsigned int h, byte midclr)
{
bool bDraw = true;
for(unsigned int i = y; i < y+h; i++) {
for(unsigned int z = x; z < x+w; z++) {
if(bDraw)
BWDrawBox(z,i,1,1,midclr);
bDraw = !bDraw;
}
if(w % 2 == 0)
bDraw = !bDraw;
}
}
void BWDrawPixel(DWORD x, DWORD y, byte clr)
{
__asm {
pushad
mov cl, clr
mov eax, BOX_COLOR
mov byte ptr ds:[eax], cl
push 1
push 1
push y
push x
mov esi, BWFXN_DRAWBOX
call esi
popad
}
}
void BWDrawText(DWORD x, DWORD y, char * text)
{
__asm {
pushad
mov eax, [text]
mov esi, x
push y
mov ecx, BWFXN_DRAWTEXT
call ecx
popad
}
}
void BWPlaySound(DWORD sound)
{
__asm {
pushad
push 0
push 0
push 20
push sound
mov eax, BWFXN_PLAYSOUND
call eax
popad
}
}
void BWRestoreTextFormat(DWORD storedFormat)
{
__asm {
pushad
mov ecx, storedFormat
mov eax, BWFXN_FORMATTEXT
call eax
popad
}
}
void BWFormatText(DWORD format)
{
__asm {
pushad
cmp format, 0
jnz PtrLoad
xor ecx, ecx
jmp FuncCall
PtrLoad:
mov eax, dword ptr ss: [format]
mov ecx, dword ptr ds: [eax]
FuncCall:
mov edx, BWFXN_FORMATTEXT
call edx
popad
}
}
void BWFormatTextR(DWORD format)
{
BWFormatText(BWTF_RESET);
BWFormatText(format);
}
void BWDrawFormattedText(DWORD x, DWORD y, char * text, DWORD format)
{
BWFormatTextR(format);
BWDrawText(x, y, text);
}
void BWRefreshText(DWORD x, DWORD y, DWORD x2, DWORD y2)
{
__asm {
pushad
push x2
mov eax, x
mov ecx, y
mov edx, y2
mov esi, BWFXN_REFRESHTEXT
call esi
popad
}
}
/*
// NOTE: This will oddly actually draw the string, which is not what we want
function BWGetTextRect(x, y: DWORD; str: PChar): TRect; stdcall;
begin
asm
pushad
push ecx
lea ecx,[esp]
push ecx
mov eax, [Offsets.TextRectX]
mov edi, x
mov dword ptr [eax],edi
mov eax, [Offsets.TextRectY]
mov edi, y
mov dword ptr [eax],edi
mov eax,[str]
call dword ptr [Offsets.BWFXN_GetTextRect]
pop ecx
popad
end;
Result.Left := (Integer(Word(Pointer(Offsets.TextRectLeft)^)));
Result.Top := (Integer(Word(Pointer(Offsets.TextRectTop)^)));
Result.Right := (Integer(Word(Pointer(Offsets.TextRectRight)^)));
Result.Bottom := (Integer(Word(Pointer(Offsets.TextRectBottom)^)));
end;
*/
// not gonna implement this, not used ever
/*
function BWGetTextWidth(str: PChar): Integer; stdcall;
begin
asm
pushad
mov ecx, [Offsets.TextWidth]
mov dword ptr ds:[ecx],0
mov eax, [str]
call dword ptr [Offsets.BWFXN_GetTextWidth]
popad
end;
Result := (PInteger(Offsets.TextWidth))^;
end;
*/
int BWGetTextWidth(char * text)
{
__asm {
pushad
mov ecx, TEXT_WIDTH
mov dword ptr ds:[ecx], 0
mov eax, [text]
mov edx, BWFXN_GETTEXTWIDTH
call edx
popad
}
return *(int *)(TEXT_WIDTH);
}
void __stdcall BWSetFontFunc(DWORD font)
{
}
void __stdcall DrawTextFunc(DWORD y)
{
}