This repository has been archived by the owner on May 9, 2022. It is now read-only.
forked from Evengard/UniMod
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibVer.cpp
422 lines (380 loc) · 8.87 KB
/
libVer.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
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
#include "stdafx.h"
#include "unit.h"
#include <windows.h>
#include <conio.h>
#include <psapi.h>
#include <stdio.h>
#include <objbase.h>
extern void initStepTwo();
extern "C" void __cdecl onNetPacketClient(BYTE *&BufStart,BYTE *E);
extern "C" void __cdecl onNetPacketServer(BYTE *&BufStart,BYTE *E, BYTE *MyPlayer, BYTE *MyUc);
extern "C" int __cdecl playerOnTrySpell(bigUnitStruct *Unit,byte *Uc,spellPacket *Pckt);
extern "C" void conSendToServer(const char *Cmd);
extern void luaAddonsLoad();
void* conAddr=NULL;
bool justDoNoxCmd=false;
bool conDelayed=true;
char requirepatch_prevmode[7];
namespace
{
void __declspec(naked) netOnCli()
{
__asm
{
jb b
a:
push 0x4942B5
retn
b:
push ebp
push ebx
lea eax, [esp+8-4]
push eax
call onNetPacketClient
add esp, 8
pop ebp
cmp ebp, ebx
jnb a
push 0x48EAA8
retn
}
}
void __declspec(naked) netOnServ()
{
__asm
{
jb b
a:
push 0x51CE4E
retn
b:
mov ecx, [esp+2Ch]
push esi
push edi
push ebp
lea eax, [esp+0Ch-4]
push ecx
push eax
call onNetPacketServer
add esp, 10h
pop esi
cmp esi, [esp+2Ch]
jnb a
push 0x51BBA1
retn
}
}
void __declspec(naked) MyOnTryCastSpell()
{
__asm
{
push esi
push edi
push ebp
call playerOnTrySpell
add esp, 0Ch
xor edi, edi
cmp eax, 0
jnz a
push 0x51C121
retn
a:
js b
inc edi
b:
push 0x51C19C
retn
}
}
int delayedConL(lua_State *L)
{
int top;
const char *errorstr;
char* Dest;
lua_rawgeti(L, 2, 1);
if (lua_pcall(L, 0, 0, 0))
{
top = lua_gettop(L);
errorstr = (const char *)lua_tolstring(L, top, 0);
Dest = new char[strlen(errorstr)+10];
strcpy(Dest, "Error: ");
strncat(Dest, errorstr, strlen(errorstr));
conPrintI(Dest);
}
return 0;
}
void __declspec(naked) FixMeBack()
{
int data[4];
int bt[1];
data[0]=0x40FF43;
data[1]=0x40FEF6;
data[2]=0x40FEEC;
data[3]=0x40FF00;
bt[0]=0x40FF48;
DWORD OldProtect;
VirtualProtect((byte*)bt[0],0x10,PAGE_EXECUTE_READWRITE,&OldProtect);
memcpy((byte*)bt[0],data,0x10);
VirtualProtect((byte*)bt[0],0x10,OldProtect,&OldProtect);
__asm
{
retn;
}
}
void __declspec(naked) InitMe()
{
FixMeBack();
luaL_openlibs(L);
// Loading custom lua addons here
luaAddonsLoad();
lua_pushcclosure(L, delayedConL, 0);
lua_setfield(L, LUA_REGISTRYINDEX, "delayedCon");
initStepTwo();
__asm
{
push 0x401C70;
retn;
}
}
}
extern void InjectOffs(DWORD Addr,void *Fn);
extern void InjectJumpTo(DWORD Addr,void *Fn);
extern void InjectAddr(DWORD Addr,void *Fn);
extern void InjectData(DWORD offset, byte* buff, size_t size);
HRESULT __stdcall CoCreateInstanceNox(REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, REFIID riid, LPVOID *ppv)
{
HRESULT res = CoCreateInstance(rclsid, pUnkOuter, dwClsContext, riid, ppv);
static bool isWolapiPatched = false;
//static byte wolapiclsid[16] = { 0xF5, 0xBA, 0xD3, 0x4D, 0x79, 0x75, 0xD1, 0x11, 0xB1, 0xC6, 0x00, 0x60, 0x97, 0x17, 0x65, 0x56 };
if (/*memcmp(wolapiclsid, &rclsid, 16) == 0*/isWolapiPatched == false)
{
HMODULE hProcessModule[100];
DWORD dwReturn;
DWORD dwModuleNb;
char BaseName[MAX_PATH];
EnumProcessModules(GetCurrentProcess(), hProcessModule, sizeof(hProcessModule), &dwReturn);
dwModuleNb = dwReturn / sizeof(HMODULE);
for (DWORD i = 0; i < dwModuleNb; i++)
{
GetModuleBaseName(GetCurrentProcess(), hProcessModule[i], BaseName, sizeof(BaseName));
for (int i = 0; i < strlen(BaseName); i++)
{
char BaseNameC = BaseName[i];
BaseName[i] = tolower(BaseNameC);
}
if (strcmp(BaseName, "wolapi.dll") == 0)
{
DWORD patchaddr = (DWORD)hProcessModule[i] + 0x116AC;
byte nops[2] = { 0x90, 0x90 };
InjectData(patchaddr, nops, 2);
isWolapiPatched = true;
break;
}
}
}
return res;
}
void initModLib1(HMODULE hModule)
{
void* MyModule = (void*)hModule;
void* addr = (char*)InitMe + (int)MyModule - (0x36D0FF58 - 0x36900000 + (int)MyModule);
byte *bt=(byte*)(0x40FF54);
DWORD OldProtect;
VirtualProtect(bt,0x4,PAGE_EXECUTE_READWRITE,&OldProtect);
memcpy((byte*)bt,&addr,0x4);
VirtualProtect(bt,0x4,OldProtect,&OldProtect);
InjectJumpTo(0x48EAA2, netOnCli);
InjectJumpTo(0x494296, netOnCli);
InjectJumpTo(0x51BB9B, netOnServ);
InjectJumpTo(0x51CE48, netOnServ);
InjectAddr(0x51CF08, MyOnTryCastSpell);
void* cciaddr = (void*)CoCreateInstanceNox;
InjectData(0x581448, (byte*)&cciaddr, sizeof(&cciaddr));
}
void __cdecl initModLib2()
{
conAddr = (void*)0x443C86;
}
extern "C" int conDoCmd(char *Cmd,bool &PrintNil);
int conDoCmd(char *Cmd,bool &PrintNil)
{
int gf = ~*GameFlags & 1;
int top = lua_gettop(L);
const char* mode;
if(!gf)
mode="server";
else
mode="client";
int topLoad = lua_gettop(L);
bool repatch_needed = false;
if (strncmp(requirepatch_prevmode, mode, 7) != 0)
{
repatch_needed = true;
strncpy(requirepatch_prevmode, mode, 7);
}
if (repatch_needed)
{
lua_getfield(L, LUA_REGISTRYINDEX, "require.lua");
if (lua_isfunction(L, -1))
{
lua_getfield(L, LUA_REGISTRYINDEX, mode);
lua_setfenv(L, -2);
int err = lua_pcall(L, 0, 0, 0);
if (err != 0)
{
const char* errorMsg = lua_tostring(L, -1);
MessageBoxA(NULL, errorMsg, "LUA load error! The file won't be loaded!", MB_OK);
return 0;
}
//lua_pushstring(L, "require.lua");
//lua_pushstring(L, mode);
//lua_settable(L, LUA_REGISTRYINDEX);
}
}
lua_settop(L, topLoad);
lua_getfield(L, LUA_REGISTRYINDEX, mode);
lua_getfield(L, LUA_GLOBALSINDEX, "conInput");
if ( lua_type(L, -1) == LUA_TFUNCTION )
{
lua_pushstring(L, Cmd);
int errcode = lua_pcall(L, 1, 1, 0);
if (!errcode)
errcode = lua_tointeger(L, -1);
lua_settop(L, top);
return errcode;
}
int statuscode = luaL_loadstring(L, Cmd);
int errcode = statuscode;
if(statuscode==3)
{
int cmdLength = strlen(Cmd);
bool allMatched = false;
char* cmdPointer;
for(cmdPointer = Cmd; strchr("_ABCDEFGHIJKLMNOPQRSTUVWXUZabcdefghijklmnopqrstuvwxyz1234567890", *cmdPointer); cmdPointer++)
{
if(cmdPointer==(Cmd+cmdLength))
break;
}
if(cmdPointer==(Cmd+cmdLength))
{
lua_getfield(L, LUA_GLOBALSINDEX, "tostring");
lua_getfield(L, top + 1, Cmd);
if(lua_type(L, -1))
{
if (lua_pcall(L, 1, 1, 0))
{
errcode = 1;
}
else
{
const char* returnString = lua_tolstring(L, -1, 0);
char Dest[200];
strcpy(Dest, " = ");
strncat(&Dest[strlen(Dest)], returnString, (199-strlen(Dest)));
Dest[199] = 0;
conPrintI(Dest);
errcode = 0;
}
}
else
PrintNil = true;
}
}
else
{
if(!statuscode)
{
lua_pushvalue(L, top + 1);
lua_setfenv(L, -2);
if(conDelayed)
{
const char* timeoutFunc;
if(!gf)
timeoutFunc="setTimeout";
else
timeoutFunc="cliSetTimeout";
lua_getfield(L, LUA_REGISTRYINDEX, timeoutFunc);
lua_getfield(L, LUA_REGISTRYINDEX, "delayedCon");
lua_pushinteger(L, 0);
lua_createtable(L, 0, 0);
lua_pushvalue(L, -5);
lua_rawseti(L, -2, 1);
errcode = lua_pcall(L, 3, 0, 0);
if(errcode)
{
int newTop = lua_gettop(L);
const char* errorStr = lua_tolstring(L, newTop, 0);
char Dest[200];
strcpy(Dest, "Adv error: ");
strncat(&Dest[strlen(Dest)], errorStr, (199-strlen(Dest)));
conPrintI(Dest);
}
}
else
{
errcode=lua_pcall(L, 0, 0, 0);
if(errcode)
{
int newTop = lua_gettop(L);
const char* errorStr = lua_tolstring(L, newTop, 0);
char Dest[200];
strcpy(Dest, "error: ");
strncat(&Dest[strlen(Dest)], errorStr, (199-strlen(Dest)));
conPrintI(Dest);
}
}
}
lua_settop(L, top);
}
return errcode;
}
void onComCmd2();
DWORD __cdecl onConCmd(wchar_t *A,DWORD B) // TODO: TO REIMPLEMENT FROM ASM!!!
{
int cmdResult = 0;
int result;
char Dest[0xC8];
bool printNil=false;
if (justDoNoxCmd)
{
cmdResult = 1;
}
else
{
int length = wcslen(A);
if ( length+1 >= 0xC8 )
return 0;
wcstombs(Dest, A, 0xC7);
Dest[0xC7] = 0;
if (*GameFlags) // Çàêëàäêà âûïèëåíà
{
cmdResult = conDoCmd(Dest, printNil); // TODO: Implement conDoCmd
}
else
{
cmdResult = 1;
}
}
if (cmdResult)
{
__asm
{
push B
push A
push onConCmd_GOBACK
sub esp,0x88
jmp conAddr
onConCmd_GOBACK:
add esp,8
mov result,eax
}
if(printNil && !result && !justDoNoxCmd)
{
conPrintI(" = nil");
}
//result = ((int (__cdecl *)(unsigned __int32, unsigned __int32))conAddr)((unsigned __int32)A, B);
}
else
result = 1;
return result;
}