-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathext_sector.s
562 lines (430 loc) · 9.92 KB
/
ext_sector.s
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
;.intel_syntax noprefix
;.arch i686
;.text
bits 32
;extern ?CastState@LuaState@LuaPlus@@SAPAV12@PAUlua_State@@@Z
;extern ??0LuaObject@LuaPlus@@QAE@ABVLuaStackObject@1@@Z
;extern ??1LuaObject@LuaPlus@@QAE@XZ
;extern ?lua_tostring@@YAPBDPAUlua_State@@H@Z
;extern ?lua_tonumber@@YAMPAUlua_State@@H@Z
;extern ?AssignClientIndex@CLobby@Moho@@AAEXHVStrArg@gpg@@AAIAAH@Z
;extern ?AssignCommandSource@CLobby@Moho@@AAEIHHAAV?$vector@USSTICommandSource@Moho@@V?$allocator@USSTICommandSource@Moho@@@std@@@std@@AAI@
;extern lua_cast_CLobby?
; MohoEngine.dll addresses
;?CastState@LuaState@LuaPlus@@SAPAV12@PAUlua_State@@@Z equ 0x105CE4B0
;??0LuaObject@LuaPlus@@QAE@ABVLuaStackObject@1@@Z equ 0x105CE280
;??1LuaObject@LuaPlus@@QAE@XZ equ 0x105CE480
;?lua_tostring@@YAPBDPAUlua_State@@H@Z equ 0x105CE290
;?lua_tonumber@@YAMPAUlua_State@@H@Z
;__imp_atoi equ 0x105CE7C4
;?AssignClientIndex@CLobby@Moho@@AAEXHVStrArg@gpg@@AAIAAH@Z equ 0x1038D150
;?AssignCommandSource@CLobby@Moho@@AAEIHHAAV?$vector@USSTICommandSource@Moho@@V?$allocator@USSTICommandSource@Moho@@@std@@@std@@AAI@ equ 0x1038D240
;lua_cast_CLobby? equ 0x103937A0
; ForgedAlliance.exe addresses
; LuaPlus
LuaState__CastState equ 0x90A510 ; LuaState* (lua_state*)
LuaState__Error equ 0x90C1D0 ; (LuaState*, fmt, args...)
?CastState@LuaState@LuaPlus@@SAPAV12@PAUlua_State@@@Z equ 0x90A510
??0LuaObject@LuaPlus@@QAE@ABVLuaStackObject@1@@Z equ 0x908A70
??1LuaObject@LuaPlus@@QAE@XZ equ 0x9075D0
LuaObject_GetString equ 0x907A90
; struct LuaStackObject
; {
; // 0x8 bytes
; LuaState* state;
; int stack_index;
; };
LuaStackObject__GetBoolean equ 0x415560
lua_pushnumber equ 0x90CD40 ; (lua_state*, float)
_lua_pushbool equ 0x90CF80; (lua_state*, bool)
?AssignClientIndex@CLobby@Moho@@AAEXHVStrArg@gpg@@AAIAAH@Z equ 0x7C4E80
?AssignCommandSource@CLobby@Moho@@AAEIHHAAV?$vector@USSTICommandSource@Moho@@V?$allocator@USSTICommandSource@Moho@@@std@@@std@@AAI@ equ 0x7C4F60
lua_cast_CLobby? equ 0x7CB7E0
Moho__NET_MakeNATTraversal? equ 0x7BC5F0 ; (LuaObject* rvo, LuaState*)
LuaObject_PushStack equ 0x907D80 ; (LuaStackObject* rvo, LuaState*)
; Returned object is probably an iterator of sorts?
; It is 0x10 bytes.
Moho__BVSet_Add equ 0x6E5660 ; void* __stdcall (void* this<ecx>, void* rvo<ebx>, int setItem)
Moho__Set__Add equ 0x4036A0 ; void *__stdcall (void *this<edi>, void *rvo, int setItem<esi>)
; Utils
?lua_tostring@@YAPBDPAUlua_State@@H@Z equ 0x90CA90
_lua_tostring equ 0x90CA90
_lua_tonumber equ 0x90C9F0
_lua_toboolean equ 0x90CA40
__imp_atoi equ 0xA835CE
__ftol2_sse equ 0xA89CC0
lua_gettop equ 0x90C590 ; (lua_state*)
_stricmp equ 0xAA549E ; int (const char*, const char*)
Logf equ 0x937CB0
_Logf equ 0x937CB0
; Globals
g_STIDriver equ 0x10C4F50
g_SWldSessionInfo equ 0x10C4F58
g_CWldSession equ 0x10A6470
_g_CWldSession equ 0x10A6470
_g_Sim equ 0x10A63F0
; c Symbols
; Imports
extern _print_hello_world
extern _ext_ValidateFocusArmyRequest
extern _cxx_AddCommandSourceId
extern _cxx_SetCommandSourceId
global _stricmp
global _lua_pushbool
global _Logf
global _g_CWldSession
global _g_Sim
; Begin the Code
%ifndef CXX_BUILD
org 0x128B000
%endif
section .ext
; <Area for hooks>
_HOOK_lua_LoadSavedGame: ; (lua_state*<eax>)
jmp _ext_do_shit_tm
align 0x8
_HOOK_CWldSession__ValidateFocusArmyRequest: ; (int)
jmp _ext_ValidateFocusArmyRequest
align 0x8
_HOOK_ArmyGetHandicap: ; (lua_state*)
jmp _ArmyGetHandicap_addValidCmdSource
; </ Area for hooks>
align 0x4
_ArmyGetHandicap_addValidCmdSource:
push ebx
mov ebx, [esp+0x8] ; ebx = lua_state*
push ebx
call lua_gettop
add esp, 0x4
cmp eax, 0x3
jnz .bad_argument_list
; Get set_or_unset
push 3
push ebx
call _lua_toboolean
add esp, 0x8
push eax ; push set_or_unset
; Get sourceId
push 2
push ebx
call _lua_toint
add esp, 0x8
push eax ; push sourceId
; Get ArmyId
push 1
push ebx
call _lua_toint
add esp, 0x8
push eax ; push armyId
; Add source id to SimArmy
push ebx ; lua_state*
call _cxx_SetCommandSourceId
add esp, 0x10
pop ebx
ret
.bad_argument_list:
; Return the same error as usual.
push eax
push 1
mov eax, [0x10B8AF4]
push eax
push 0xE0A220 ; "%s\n expected %d args, but got %d"
push esi
call LuaState__Error
ret
align 0x4
_ext_do_shit_tm: ; (lua_state*<eax>)
push ebp
mov ebp, esp
; Function body
push ebx
push ecx
push edx
push esi
mov edx, eax ; edx = lua_state*
mov eax, esp
call _print_int
; Get int function id (first param)
push 1
push edx
call _lua_toint
add esp, 0x8
cmp eax, 0x1
jnb .unknown_func
push edx
; Call the func
mov ebx, _function_table
mov ebx, [ebx+eax*4]
call ebx
add esp, 0x4
jmp .cleanup
.unknown_func:
; Function end
.cleanup:
mov ecx, eax
mov eax, esp
call _print_int
mov eax, ecx
pop esi
pop edx
pop ecx
pop ebx
pop ebp
; Balance stack in lua_LoadSavedGame
mov ecx, [ebp-0xC] ; Clear SEH
mov [fs:0], ecx
pop edi
pop esi
pop ebx
mov esp, ebp
pop ebp
ret
align 0x4
aExtD: db 'EXT: %08x',0
align 0x4
aExtS: db 'EXT: %s',0
align 0x4
aEXTError: db 'EXTError',0
align 0x4
_print_int: ; (int<eax>)
push ecx
push edx
push eax
push aExtD
call Logf
add esp, 0x8
pop edx
pop ecx
ret
align 0x4
_print_str: ; (const char*<eax>)
push ecx
push edx
push eax
push aExtS
call Logf
add esp, 0x8
pop edx
pop ecx
ret
align 0x4
_lua_toint: ; (lus_state*, int)
; LuaPlus lua_tostring does not work for integers/numbers
; found it out the really hard way
; So here's a new new new one
push ebp
mov ebp, esp
push ecx
push edx
mov eax, [ebp+0xc]
push eax
mov eax, [ebp+0x8]
push eax
call _lua_tonumber
call __ftol2_sse
add esp, 0x8
pop edx
pop ecx
pop ebp
ret
align 0x4
_lua_exit:; (lua_state*<eax>)
push eax
call LuaState__CastState
push aEXTError
push eax
call LuaState__Error
ret
align 0x4
_moho_set_hackadd: ; (void* this<eax>, int setItem<ecx>)
; Adds the item to the set without checking for any size boundaries
mov eax, [eax+0x8]
mov edx, ecx
shr edx, 5
and ecx, 0x1f
lea eax, [eax+edx*4]
mov edx, 1
shl edx, cl
or [eax], edx
ret
align 0x4
_string_data: ; const void* (string* this<ecx>)
cmp dword [ecx+0x18], 0x10
jb .sso
mov eax, [ecx+0x4]
ret
.sso:
lea eax, [ecx+0x4]
ret
align 0x4
_function_table:
dd _info
dd _add_validCmdSource
_ext_version: dd 0.01
align 0x4
_info: ; (lua_state*)
; Returns version for this extension
call _print_hello_world
mov eax, [_ext_version]
push eax
mov eax, [esp+0x8]
push eax
call lua_pushnumber
add esp, 0x8
mov eax, 1 ; Returns 1 result to lua
ret
align 0x4
_add_validCmdSource: ; (lua_state*)
; Adds valid command source for a player
; Lua args: (playerName, sourceId)
push ebp
mov ebp, esp
push ebx
push ecx
push edx
push esi
push edi
mov edx, [ebp+0x8] ; edx = lua_state*
; Get ClientManager*
mov ebx, [g_STIDriver]
mov ebx, [ebx+0x8] ; ebx = IClientManager*
; Get Client*
push 2
push edx
call _lua_tostring
add esp, 0x8
mov esi, eax
mov edi, [ebx+0x420]
mov ebx, [ebx+0x424]
.it_loop:
cmp edi, ebx
je .it_end
mov ecx, [edi]
add ecx, 0x4
call _string_data
push esi
push eax
call _stricmp
add esp, 0x8
test eax, eax
jz .found
add edi, 0x4
jmp .it_loop
.it_end: ; User not found
; Handle invalid user.
jmp .locret
.found:
mov ecx, [edi] ; ecx = Client*
mov edx, [ebp+0x8] ; edx = lua_state*
; Get sourceId
push 3
push edx
call _lua_toint
add esp, 0x8
; Set Client* to have the valid source
mov ebx, eax
lea eax, [ecx+0x30]
mov ecx, ebx
call _moho_set_hackadd
; Set UserArmy* to have the valid source
mov eax, [g_CWldSession]
mov eax, [eax+0x3F0]
mov eax, [eax+ebx*4]
add eax, 0x128
; Tiny fix to seemingly bugged set
;mov edx, [eax+0x8]
;add edx, 4
;mov [eax+0xc], edx
;mov ecx, ebx
;call _moho_set_hackadd
; Add source id to SimArmy
push ebx ; Source id
push esi ; PlayerName
mov eax, [ebp+0x8]
push eax ; lua_state*
call _cxx_AddCommandSourceId
add esp, 0xC
;sub esp, 0x10
;add ecx, 0x30
;mov ebx, esp
;push eax
;call _print_int
;call Moho__BVSet_Add
;mov esi, ecx
;mov edi, eax
;push esp
;call Moho__Set__Add
;add esp, 0x10
;mov eax, 0 ; Returns 0 results to lua
.locret:
pop edi
pop esi
pop edx
pop ecx
pop ebx
pop ebp
ret
align 0x4
_make_nat: ; (lua_state*)
; Makes a NatTraversalProvider and returns it to lua
; Does not work.... yet...??..
push ecx
mov ecx, [esp+0xc] ; ecx = lua_state*
push ecx
call ?CastState@LuaState@LuaPlus@@SAPAV12@PAUlua_State@@@Z ; eax = casted LuaState
add esp, 0x4
push eax
lea eax, [esp-0x100]
push eax
call Moho__NET_MakeNATTraversal?
add esp, 0x8
push ecx
lea ecx, [esp-0x200]
push ecx ; rvo for LuaStackObject
lea ecx, [esp-0x100] ; ecx = LuaObject*
call LuaObject_PushStack
pop ecx
ret
_ext_Clobby_addCOmmandSOurce: ; Broken code, do not use, here for reference
push edx
call ?CastState@LuaState@LuaPlus@@SAPAV12@PAUlua_State@@@Z ; eax = casted LuaState
add esp, 0x4
; Get CLobby instance
; Create LuaStackObject
mov [ebp - 0x8], eax
mov [ebp - 0x4], dword 1
; push LuaStackObject Address
lea ecx, [ebp - 0x8]
push ecx
; LuaObject(const LuaStackObject&)
lea ecx, [ebp - 0x1c]
call ??0LuaObject@LuaPlus@@QAE@ABVLuaStackObject@1@@Z
lea eax, [ebp - 0x4]
call lua_cast_CLobby?
lea ecx, [ebp - 0x4]
mov ecx, eax ; ecx = CLobby*
call ??1LuaObject@LuaPlus@@QAE@XZ
; Get PlayerName
push -1
push edx
call ?lua_tostring@@YAPBDPAUlua_State@@H@Z ; eax = playerName
add esp, 0x8
mov ebx, eax ; ebx = playerName
; Get OwnerID
push -2
push edx
call _lua_toint
add esp, 0x8
mov esi, eax ; esi = OwnerID
; Do the thing
mov edx, [ebp - 0x100]
push edx
push edx
push ebx
push eax
call ?AssignClientIndex@CLobby@Moho@@AAEXHVStrArg@gpg@@AAIAAH@Z
push edx
push edx
push 0
push esi
call ?AssignCommandSource@CLobby@Moho@@AAEIHHAAV?$vector@USSTICommandSource@Moho@@V?$allocator@USSTICommandSource@Moho@@@std@@@std@@AAI@