-
Notifications
You must be signed in to change notification settings - Fork 34
/
CCustomOpcodeSystem.cpp
2795 lines (2523 loc) · 81.5 KB
/
CCustomOpcodeSystem.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
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "stdafx.h"
#include "cleo.h"
#include "CLegacy.h"
#include "CGameVersionManager.h"
#include "CCustomOpcodeSystem.h"
#include "CTextManager.h"
#include "CModelInfo.h"
namespace CLEO {
DWORD FUNC_fopen;
DWORD FUNC_fclose;
DWORD FUNC_fwrite;
DWORD FUNC_fread;
DWORD FUNC_fgetc;
DWORD FUNC_fgets;
DWORD FUNC_fputs;
DWORD FUNC_fseek;
DWORD FUNC_fprintf;
DWORD FUNC_ftell;
DWORD FUNC_fflush;
DWORD FUNC_feof;
DWORD FUNC_ferror;
OpcodeResult __stdcall opcode_0A8C(CRunningScript *thread);
OpcodeResult __stdcall opcode_0A8D(CRunningScript *thread);
OpcodeResult __stdcall opcode_0A8E(CRunningScript *thread);
OpcodeResult __stdcall opcode_0A8F(CRunningScript *thread);
OpcodeResult __stdcall opcode_0A90(CRunningScript *thread);
OpcodeResult __stdcall opcode_0A91(CRunningScript *thread);
OpcodeResult __stdcall opcode_0A92(CRunningScript *thread);
OpcodeResult __stdcall opcode_0A93(CRunningScript *thread);
OpcodeResult __stdcall opcode_0A94(CRunningScript *thread);
OpcodeResult __stdcall opcode_0A95(CRunningScript *thread);
OpcodeResult __stdcall opcode_0A96(CRunningScript *thread);
OpcodeResult __stdcall opcode_0A97(CRunningScript *thread);
OpcodeResult __stdcall opcode_0A98(CRunningScript *thread);
OpcodeResult __stdcall opcode_0A99(CRunningScript *thread);
OpcodeResult __stdcall opcode_0A9A(CRunningScript *thread);
OpcodeResult __stdcall opcode_0A9B(CRunningScript *thread);
OpcodeResult __stdcall opcode_0A9C(CRunningScript *thread);
OpcodeResult __stdcall opcode_0A9D(CRunningScript *thread);
OpcodeResult __stdcall opcode_0A9E(CRunningScript *thread);
OpcodeResult __stdcall opcode_0A9F(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AA0(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AA1(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AA2(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AA3(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AA4(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AA5(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AA6(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AA7(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AA8(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AA9(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AAA(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AAB(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AAC(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AAD(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AAE(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AAF(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AB0(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AB1(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AB2(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AB3(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AB4(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AB5(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AB6(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AB7(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AB8(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AB9(CRunningScript *thread);
OpcodeResult __stdcall opcode_0ABA(CRunningScript *thread);
OpcodeResult __stdcall opcode_0ABB(CRunningScript *thread);
OpcodeResult __stdcall opcode_0ABC(CRunningScript *thread);
OpcodeResult __stdcall opcode_0ABD(CRunningScript *thread);
OpcodeResult __stdcall opcode_0ABE(CRunningScript *thread);
OpcodeResult __stdcall opcode_0ABF(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AC0(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AC1(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AC2(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AC3(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AC4(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AC5(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AC6(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AC7(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AC8(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AC9(CRunningScript *thread);
OpcodeResult __stdcall opcode_0ACA(CRunningScript *thread);
OpcodeResult __stdcall opcode_0ACB(CRunningScript *thread);
OpcodeResult __stdcall opcode_0ACC(CRunningScript *thread);
OpcodeResult __stdcall opcode_0ACD(CRunningScript *thread);
OpcodeResult __stdcall opcode_0ACE(CRunningScript *thread);
OpcodeResult __stdcall opcode_0ACF(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AD0(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AD1(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AD2(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AD3(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AD4(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AD5(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AD6(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AD7(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AD8(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AD9(CRunningScript *thread);
OpcodeResult __stdcall opcode_0ADA(CRunningScript *thread);
OpcodeResult __stdcall opcode_0ADB(CRunningScript *thread);
OpcodeResult __stdcall opcode_0ADC(CRunningScript *thread);
OpcodeResult __stdcall opcode_0ADD(CRunningScript *thread);
OpcodeResult __stdcall opcode_0ADE(CRunningScript *thread);
OpcodeResult __stdcall opcode_0ADF(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AE0(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AE1(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AE2(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AE3(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AE4(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AE5(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AE6(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AE7(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AE8(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AE9(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AEA(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AEB(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AEC(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AED(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AEE(CRunningScript *thread);
OpcodeResult __stdcall opcode_0AEF(CRunningScript *thread);
CustomOpcodeHandler customOpcodeHandlers[100] =
{
opcode_0A8C, opcode_0A8D, opcode_0A8E, opcode_0A8F, opcode_0A90,
opcode_0A91, opcode_0A92, opcode_0A93, opcode_0A94, opcode_0A95,
opcode_0A96, opcode_0A97, opcode_0A98, opcode_0A99, opcode_0A9A,
opcode_0A9B, opcode_0A9C, opcode_0A9D, opcode_0A9E, opcode_0A9F,
opcode_0AA0, opcode_0AA1, opcode_0AA2, opcode_0AA3, opcode_0AA4,
opcode_0AA5, opcode_0AA6, opcode_0AA7, opcode_0AA8, opcode_0AA9,
opcode_0AAA, opcode_0AAB, opcode_0AAC, opcode_0AAD, opcode_0AAE,
opcode_0AAF, opcode_0AB0, opcode_0AB1, opcode_0AB2, opcode_0AB3,
opcode_0AB4, opcode_0AB5, opcode_0AB6, opcode_0AB7, opcode_0AB8,
opcode_0AB9, opcode_0ABA, opcode_0ABB, opcode_0ABC, opcode_0ABD,
opcode_0ABE, opcode_0ABF, opcode_0AC0, opcode_0AC1, opcode_0AC2,
opcode_0AC3, opcode_0AC4, opcode_0AC5, opcode_0AC6, opcode_0AC7,
opcode_0AC8, opcode_0AC9, opcode_0ACA, opcode_0ACB, opcode_0ACC,
opcode_0ACD, opcode_0ACE, opcode_0ACF, opcode_0AD0, opcode_0AD1,
opcode_0AD2, opcode_0AD3, opcode_0AD4, opcode_0AD5, opcode_0AD6,
opcode_0AD7, opcode_0AD8, opcode_0AD9, opcode_0ADA, opcode_0ADB,
opcode_0ADC, opcode_0ADD, opcode_0ADE, opcode_0ADF, opcode_0AE0,
opcode_0AE1, opcode_0AE2, opcode_0AE3, opcode_0AE4, opcode_0AE5,
opcode_0AE6, opcode_0AE7, opcode_0AE8, opcode_0AE9, opcode_0AEA,
opcode_0AEB, opcode_0AEC, opcode_0AED, opcode_0AEE, opcode_0AEF,
};
typedef OpcodeResult(__thiscall *_OpcodeHandler)(CRunningScript *thread, unsigned short opcode);
typedef void(*FuncScriptDeleteDelegateT) (CRunningScript *script);
struct ScriptDeleteDelegate {
std::vector<FuncScriptDeleteDelegateT> funcs;
template<class FuncScriptDeleteDelegateT> void operator+=(FuncScriptDeleteDelegateT mFunc) { funcs.push_back(mFunc); }
template<class FuncScriptDeleteDelegateT> void operator-=(FuncScriptDeleteDelegateT mFunc) { funcs.erase(std::remove(funcs.begin(), funcs.end(), mFunc), funcs.end()); }
void operator()(CRunningScript *script) { for (auto& f : funcs) f(script); }
};
ScriptDeleteDelegate scriptDeleteDelegate;
void RunScriptDeleteDelegate(CRunningScript *script) { scriptDeleteDelegate(script); }
_OpcodeHandler *oldOpcodeHandlerTable;
_OpcodeHandler newOpcodeHandlerTable[329];
CustomOpcodeHandler extraOpcodeHandlers[100][300];
CBuildingPool **buildingPool = nullptr; // add for future CLEO releases
CVehiclePool **vehiclePool = nullptr;
CObjectPool **objectPool = nullptr;
CPedPool **pedPool = nullptr;
inline CPedPool& GetPedPool() { return **pedPool; }
inline CVehiclePool& GetVehiclePool() { return **vehiclePool; }
inline CObjectPool& GetObjectPool() { return **objectPool; }
void(__thiscall * ProcessScript)(CRunningScript*);
const char * (__cdecl * GetUserDirectory)();
void(__cdecl * ChangeToUserDir)();
void(__cdecl * ChangeToProgramDir)(const char *);
float(__cdecl * FindGroundZ)(float x, float y);
CMarker * RadarBlips;
CHandling * Handling;
CPlayerPed * (__cdecl * GetPlayerPed)(DWORD);
CBaseModelInfo **Models;
void(__cdecl * SpawnCar)(DWORD);
WORD last_opcode = 0;
WORD last_custom_opcode = 0;
char last_thread[8] = "none";
CRunningScript * last_script;
ptrdiff_t last_off = -1;
// opcode handler for opcodes, defined by user with cleo api
OpcodeResult __fastcall extraOpcodeHandler(CRunningScript *thread, int dummy, unsigned short opcode)
{
last_custom_opcode = opcode;
last_script = thread;
return extraOpcodeHandlers[opcode % 100][opcode / 100 - 28](thread);
}
// opcode handler for custom opcodes
OpcodeResult __fastcall customOpcodeHandler(CRunningScript *thread, int dummy, unsigned short opcode)
{
last_custom_opcode = opcode;
last_script = thread;
return customOpcodeHandlers[opcode - 0x0A8C](thread);
}
char ScriptExecutionLoop()
{
CCustomScript *thread;
OpcodeResult res;
_asm mov thread, esi
last_script = thread;
try
{
do
{
ptrdiff_t off = thread->IsCustom() ? thread->GetBytePointer() - thread->GetBasePointer() : thread->GetBytePointer() - scmBlock;
WORD opcode = thread->ReadDataWord();
last_opcode = opcode;
last_off = off;
memcpy(last_thread, thread->GetName(), 8);
thread->SetNotFlag((opcode & 0x8000) != 0);
opcode &= 0x7FFF;
res = newOpcodeHandlerTable[opcode / 100](thread, opcode);
} while (res == OR_CONTINUE);
}
catch (const char * e)
{
char str[128];
sprintf(str, "%s encountered while parsing opcode '%04X' in script '%s'", e, last_opcode, last_thread);
Error(str);
}
return 0;
}
void CCustomOpcodeSystem::Inject(CCodeInjector& inj)
{
TRACE("Injecting CustomOpcodeSystem...");
CGameVersionManager& gvm = GetInstance().VersionManager;
oldOpcodeHandlerTable = gvm.TranslateMemoryAddress(MA_OPCODE_HANDLER);
// add handler for custom opcodes
oldOpcodeHandlerTable[27] = reinterpret_cast<_OpcodeHandler>(customOpcodeHandler);
// replace old OpcodeHandlerTable with the new one
//inj.MemoryWrite(gvm.TranslateMemoryAddress(MA_OPCODE_HANDLER_REF), reinterpret_cast<_OpcodeHandler>(&newOpcodeHandlerTable[0]));
MemWrite(gvm.TranslateMemoryAddress(MA_OPCODE_HANDLER_REF), reinterpret_cast<_OpcodeHandler>(&newOpcodeHandlerTable[0]));
// copy old table to the new
//std::copy(oldOpcodeHandlerTable, oldOpcodeHandlerTable + 28, newOpcodeHandlerTable);
MemCopy<_OpcodeHandler>(newOpcodeHandlerTable, oldOpcodeHandlerTable, (&oldOpcodeHandlerTable[28] - oldOpcodeHandlerTable) * 4);
//MemCopy(newOpcodeHandlerTable, oldOpcodeHandlerTable, oldOpcodeHandlerTable - (oldOpcodeHandlerTable + 28));
// fill the rest with default handler
std::fill(newOpcodeHandlerTable + 28, newOpcodeHandlerTable + 329, reinterpret_cast<_OpcodeHandler>(extraOpcodeHandler));
FUNC_fopen = gvm.TranslateMemoryAddress(MA_FOPEN_FUNCTION);
FUNC_fclose = gvm.TranslateMemoryAddress(MA_FCLOSE_FUNCTION);
FUNC_fread = gvm.TranslateMemoryAddress(MA_FREAD_FUNCTION);
FUNC_fwrite = gvm.TranslateMemoryAddress(MA_FWRITE_FUNCTION);
FUNC_fgetc = gvm.TranslateMemoryAddress(MA_FGETC_FUNCTION);
FUNC_fgets = gvm.TranslateMemoryAddress(MA_FGETS_FUNCTION);
FUNC_fputs = gvm.TranslateMemoryAddress(MA_FPUTS_FUNCTION);
FUNC_fseek = gvm.TranslateMemoryAddress(MA_FSEEK_FUNCTION);
FUNC_fprintf = gvm.TranslateMemoryAddress(MA_FPRINTF_FUNCTION);
FUNC_ftell = gvm.TranslateMemoryAddress(MA_FTELL_FUNCTION);
FUNC_fflush = gvm.TranslateMemoryAddress(MA_FFLUSH_FUNCTION);
FUNC_feof = gvm.TranslateMemoryAddress(MA_FEOF_FUNCTION);
FUNC_ferror = gvm.TranslateMemoryAddress(MA_FERROR_FUNCTION);
pedPool = gvm.TranslateMemoryAddress(MA_PED_POOL);
vehiclePool = gvm.TranslateMemoryAddress(MA_VEHICLE_POOL);
objectPool = gvm.TranslateMemoryAddress(MA_OBJECT_POOL);
GetUserDirectory = gvm.TranslateMemoryAddress(MA_GET_USER_DIR_FUNCTION);
ChangeToUserDir = gvm.TranslateMemoryAddress(MA_CHANGE_TO_USER_DIR_FUNCTION);
ChangeToProgramDir = gvm.TranslateMemoryAddress(MA_CHANGE_TO_PROGRAM_DIR_FUNCTION);
FindGroundZ = gvm.TranslateMemoryAddress(MA_FIND_GROUND_Z_FUNCTION);
GetPlayerPed = gvm.TranslateMemoryAddress(MA_GET_PLAYER_PED_FUNCTION);
Handling = gvm.TranslateMemoryAddress(MA_HANDLING);
Models = gvm.TranslateMemoryAddress(MA_MODELS);
SpawnCar = gvm.TranslateMemoryAddress(MA_SPAWN_CAR_FUNCTION);
// TODO: consider version-agnostic code
if (gvm.GetGameVersion() == GV_US10) {
// make it compatible with fastman92's limit adjuster (only required for 1.0 US)
RadarBlips = injector::ReadMemory<CMarker*>(0x583A05 + 2, true);
}
else {
RadarBlips = gvm.TranslateMemoryAddress(MA_RADAR_BLIPS);
}
/*if(gvm.GetGameVersion() == GV_US10)
{
inj.Nop(0x469FB0, 0x469FFB - 0x469FB0);
inj.ReplaceFunction(ScriptExecutionLoop, 0x469FF6);
inj.Nop(0x469FF2, 0x469FFB - 0x469FF2);
inj.ReplaceFunction(ScriptExecutionLoop, 0x469FF6);
}*/
}
inline CRunningScript& operator>>(CRunningScript& thread, DWORD& uval)
{
GetScriptParams(&thread, 1);
uval = opcodeParams[0].dwParam;
return thread;
}
inline CRunningScript& operator<<(CRunningScript& thread, DWORD uval)
{
opcodeParams[0].dwParam = uval;
SetScriptParams(&thread, 1);
return thread;
}
inline CRunningScript& operator>>(CRunningScript& thread, int& nval)
{
GetScriptParams(&thread, 1);
nval = opcodeParams[0].nParam;
return thread;
}
inline CRunningScript& operator<<(CRunningScript& thread, int nval)
{
opcodeParams[0].nParam = nval;
SetScriptParams(&thread, 1);
return thread;
}
inline CRunningScript& operator>>(CRunningScript& thread, float& fval)
{
GetScriptParams(&thread, 1);
fval = opcodeParams[0].fParam;
return thread;
}
inline CRunningScript& operator<<(CRunningScript& thread, float fval)
{
opcodeParams[0].fParam = fval;
SetScriptParams(&thread, 1);
return thread;
}
inline CRunningScript& operator>>(CRunningScript& thread, CVector& vec)
{
GetScriptParams(&thread, 3);
vec.x = opcodeParams[0].fParam;
vec.y = opcodeParams[1].fParam;
vec.z = opcodeParams[2].fParam;
return thread;
}
inline CRunningScript& operator<<(CRunningScript& thread, const CVector& vec)
{
opcodeParams[0].fParam = vec.x;
opcodeParams[1].fParam = vec.y;
opcodeParams[2].fParam = vec.z;
SetScriptParams(&thread, 3);
return thread;
}
template<typename T>
inline CRunningScript& operator>>(CRunningScript& thread, T *& pval)
{
GetScriptParams(&thread, 1);
pval = reinterpret_cast<T *>(opcodeParams[0].pParam);
return thread;
}
template<typename T>
inline CRunningScript& operator<<(CRunningScript& thread, T *pval)
{
opcodeParams[0].pParam = (void *)(pval);
SetScriptParams(&thread, 1);
return thread;
}
inline CRunningScript& operator>>(CRunningScript& thread, memory_pointer& pval)
{
GetScriptParams(&thread, 1);
pval = opcodeParams[0].pParam;
return thread;
}
template<typename T>
inline CRunningScript& operator<<(CRunningScript& thread, memory_pointer pval)
{
opcodeParams[0].pParam = pval;
SetScriptParams(&thread, 1);
return thread;
}
// read string parameter according to convention on strings
char *readString(CRunningScript *thread, char* buf = nullptr, BYTE size = 0)
{
auto paramType = *thread->GetBytePointer();
if (!paramType) return nullptr;
if (paramType >= 1 && paramType <= 8)
{
// process parameter as a pointer to string
GetScriptParams(thread, 1);
if (buf != nullptr)
{
size = size > 128 || !size ? 128 : size;
strncpy(buf, opcodeParams[0].pcParam, size - 1);
buf[size - 1] = 0;
}
return opcodeParams[0].pcParam;
}
else
{
// process as scm string
if (!buf)
{
static char result[128];
std::fill(result, result + 128, '\0');
GetScriptStringParam(thread, result, 128);
return result;
}
else
{
size = size > 128 || !size ? 128 : size;
std::fill(buf, buf + size, '\0');
GetScriptStringParam(thread, buf, size);
return buf;
}
}
}
// perform 'sprintf'-operation for parameters, passed through SCM
int format(CRunningScript *thread, char *str, size_t len, const char *format)
{
unsigned int written = 0;
const char *iter = format;
char bufa[256], fmtbufa[64], *fmta;
while (*iter)
{
while (*iter && *iter != '%')
{
if (written++ >= len)
return -1;
*str++ = *iter++;
}
if (*iter == '%')
{
if (iter[1] == '%')
{
if (written++ >= len)
return -1;
*str++ = '%'; /* "%%"->'%' */
iter += 2;
continue;
}
//get flags and width specifier
fmta = fmtbufa;
*fmta++ = *iter++;
while (*iter == '0' ||
*iter == '+' ||
*iter == '-' ||
*iter == ' ' ||
*iter == '*' ||
*iter == '#')
{
if (*iter == '*')
{
char *buffiter = bufa;
//get width
GetScriptParams(thread, 1);
_itoa(opcodeParams[0].dwParam, buffiter, 10);
while (*buffiter)
*fmta++ = *buffiter++;
}
else
*fmta++ = *iter;
iter++;
}
//get immidiate width value
while (isdigit(*iter))
*fmta++ = *iter++;
//get precision
if (*iter == '.')
{
*fmta++ = *iter++;
if (*iter == '*')
{
char *buffiter = bufa;
GetScriptParams(thread, 1);
_itoa(opcodeParams[0].dwParam, buffiter, 10);
while (*buffiter)
*fmta++ = *buffiter++;
}
else
while (isdigit(*iter))
*fmta++ = *iter++;
}
//get size
if (*iter == 'h' || *iter == 'l')
*fmta++ = *iter++;
switch (*iter)
{
case 's':
{
static const char none[] = "(null)";
const char *astr = readString(thread);
const char *striter = astr ? astr : none;
while (*striter)
{
if (written++ >= len)
return -1;
*str++ = *striter++;
}
iter++;
break;
}
case 'c':
if (written++ >= len)
return -1;
GetScriptParams(thread, 1);
*str++ = (char)opcodeParams[0].nParam;
iter++;
break;
default:
{
/* For non wc types, use system sprintf and append to wide char output */
/* FIXME: for unrecognised types, should ignore % when printing */
char *bufaiter = bufa;
if (*iter == 'p' || *iter == 'P')
{
GetScriptParams(thread, 1);
sprintf(bufaiter, "%08X", opcodeParams[0].dwParam);
}
else
{
*fmta++ = *iter;
*fmta = '\0';
if (*iter == 'a' || *iter == 'A' ||
*iter == 'e' || *iter == 'E' ||
*iter == 'f' || *iter == 'F' ||
*iter == 'g' || *iter == 'G')
{
GetScriptParams(thread, 1);
sprintf(bufaiter, fmtbufa, opcodeParams[0].fParam);
}
else
{
GetScriptParams(thread, 1);
sprintf(bufaiter, fmtbufa, opcodeParams[0].pParam);
}
}
while (*bufaiter)
{
if (written++ >= len)
return -1;
*str++ = *bufaiter++;
}
iter++;
break;
}
}
}
}
if (written >= len)
return -1;
*str++ = 0;
return (int)written;
}
// Legacy modes for CLEO 3
FILE * legacy_fopen(const char * szPath, const char * szMode)
{
FILE * hFile;
_asm
{
push szMode
push szPath
call FUNC_fopen
add esp, 8
mov hFile, eax
}
return hFile;
}
void legacy_fclose(FILE * hFile)
{
_asm
{
push hFile
call FUNC_fclose
add esp, 4
}
}
size_t legacy_fread(void * buf, size_t len, size_t count, FILE * stream)
{
_asm
{
push stream
push count
push len
push buf
call FUNC_fread
add esp, 0x10
}
}
size_t legacy_fwrite(const void * buf, size_t len, size_t count, FILE * stream)
{
_asm
{
push stream
push count
push len
push buf
call FUNC_fwrite
add esp, 0x10
}
}
char legacy_fgetc(FILE * stream)
{
_asm
{
push stream
call FUNC_fgetc
add esp, 0x4
}
}
char * legacy_fgets(char *pStr, int num, FILE * stream)
{
_asm
{
push stream
push num
push pStr
call FUNC_fgets
add esp, 0xC
}
}
int legacy_fputs(const char *pStr, FILE * stream)
{
_asm
{
push stream
push pStr
call FUNC_fputs
add esp, 0x8
}
}
int legacy_fseek(FILE * stream, long int offs, int original)
{
_asm
{
push stream
push offs
push original
call FUNC_fseek
add esp, 0xC
}
}
int legacy_ftell(FILE * stream)
{
_asm
{
push stream
call FUNC_ftell
add esp, 0x4
}
}
int __declspec(naked) fprintf(FILE * stream, const char * format, ...)
{
_asm jmp FUNC_fprintf
}
int legacy_fflush(FILE * stream)
{
_asm
{
push stream
call FUNC_fflush
add esp, 0x4
}
}
int legacy_feof(FILE * stream)
{
_asm
{
push stream
call FUNC_feof
add esp, 0x4
}
}
int legacy_ferror(FILE * stream)
{
_asm
{
push stream
call FUNC_ferror
add esp, 0x4
}
}
bool is_legacy_handle(DWORD dwHandle) { return (dwHandle & 0x1) == 0; }
FILE * convert_handle_to_file(DWORD dwHandle) { return dwHandle ? reinterpret_cast<FILE*>(is_legacy_handle(dwHandle) ? dwHandle : dwHandle & ~(0x1)) : nullptr; }
inline DWORD open_file(const char * szPath, const char * szMode, bool bLegacy)
{
FILE * hFile = bLegacy ? legacy_fopen(szPath, szMode) : fopen(szPath, szMode);
if (hFile) return bLegacy ? (DWORD)hFile : (DWORD)hFile | 0x1;
return NULL;
}
inline void close_file(DWORD dwHandle)
{
if (is_legacy_handle(dwHandle)) legacy_fclose(convert_handle_to_file(dwHandle));
else fclose(convert_handle_to_file(dwHandle));
}
inline DWORD file_get_size(DWORD file_handle)
{
FILE * hFile = convert_handle_to_file(file_handle);
if (hFile)
{
auto savedPos = ftell(hFile);
fseek(hFile, 0, SEEK_END);
DWORD dwSize = static_cast<DWORD>(ftell(hFile));
fseek(hFile, savedPos, SEEK_SET);
return dwSize;
}
return 0;
}
inline DWORD read_file(void *buf, DWORD size, DWORD count, DWORD hFile)
{
return is_legacy_handle(hFile) ? legacy_fread(buf, size, 1, convert_handle_to_file(hFile)) : fread(buf, size, 1, convert_handle_to_file(hFile));
}
inline DWORD write_file(const void *buf, DWORD size, DWORD count, DWORD hFile)
{
return is_legacy_handle(hFile) ? legacy_fwrite(buf, size, 1, convert_handle_to_file(hFile)) : fwrite(buf, size, 1, convert_handle_to_file(hFile));
}
inline void flush_file(DWORD dwHandle)
{
if (is_legacy_handle(dwHandle)) legacy_fflush(convert_handle_to_file(dwHandle));
else fflush(convert_handle_to_file(dwHandle));
}
/*inline void __impl_RetrieveScriptParam(SCRIPT_VAR*) { }
template<typename ..Params> inline void __impl_RetrieveScriptParam(SCRIPT_VAR *, unsigned&, Params&...);
template<typename Params> inline void __impl_RetrieveScriptParam(SCRIPT_VAR *, int&, Params&...);
template<typename Params> inline void __impl_RetrieveScriptParam(SCRIPT_VAR *, float&, Params&...);
template<typename ThisParam, typename Params> inline void __impl_RetrieveScriptParam(SCRIPT_VAR *, ThisParam *&, Params&...);
template<typename Params>
inline void __impl_RetrieveScriptParam(SCRIPT_VAR *var, unsigned& thisParam, Params&... restParams)
{
thisParam = var->dwParam;
__impl_RetrieveScriptParam(var + 1, restParams...);
}
template<typename... Params>
inline void __impl_RetrieveScriptParam(SCRIPT_VAR *var, int& thisParam, Params&... restParams)
{
thisParam = var->nParam;
__impl_RetrieveScriptParam(var + 1, restParams...);
}
template<typename... Params>
inline void __impl_RetrieveScriptParam(SCRIPT_VAR *var, float& thisParam, Params&... restParams)
{
thisParam = var->fParam;
__impl_RetrieveScriptParam(var + 1, restParams...);
}
template<typename ThisParam, typename... Params>
inline void __impl_RetrieveScriptParam(SCRIPT_VAR *var, ThisParam *& thisParam, Params&... restParams)
{
thisParam = reinterpret_cast<ThisParam *>(var->pParam);
__impl_RetrieveScriptParam(var + 1, restParams...);
}
template<typename... Params>
inline void RetrieveScriptParams(CCustomScript *thread, Params&... params)
{
GetScriptParams(thread, sizeof...(params));
__impl_RetrieveScriptParam(opcodeParams, params...);
}*/
inline void ThreadJump(CRunningScript *thread, int off)
{
thread->SetIp(off < 0 ? thread->GetBasePointer() - off : scmBlock + off);
}
inline void SkipUnusedParameters(CRunningScript *thread)
{
while (*thread->GetBytePointer()) GetScriptParams(thread, 1); // skip parameters
thread->ReadDataByte();
}
struct ScmFunction
{
unsigned short prevScmFunctionId, thisScmFunctionId;
BYTE *retnAddress;
SCRIPT_VAR savedTls[32];
static const size_t store_size = 0x400;
static ScmFunction *Store[store_size];
static size_t allocationPlace; // contains an index of last allocated object
void *operator new(size_t size)
{
size_t start_search = allocationPlace;
while (Store[allocationPlace]) // find first unused position in store
{
if (++allocationPlace >= store_size) allocationPlace = 0; // end of store reached
if (allocationPlace == start_search) throw std::bad_alloc(); // the store is filled up
}
ScmFunction *obj = reinterpret_cast<ScmFunction *>(::operator new(size));
Store[allocationPlace] = obj;
return obj;
}
void operator delete(void *mem)
{
Store[reinterpret_cast<ScmFunction *>(mem)->thisScmFunctionId] = nullptr;
::operator delete(mem);
}
ScmFunction(CRunningScript *thread) :
prevScmFunctionId(reinterpret_cast<CCustomScript*>(thread)->GetScmFunction()), retnAddress(thread->GetBytePointer())
{
auto cs = reinterpret_cast<CCustomScript*>(thread);
auto scope = cs->IsMission() ? missionLocals : cs->LocalVar;
std::copy(scope, scope + 32, savedTls);
SCRIPT_VAR fill_val; fill_val.dwParam = 0;
// CLEO 3 didnt initialise local storage, so dont do it if we're processing a CLEO 3 script in case the storage is used
if (cs->IsCustom() && cs->GetCompatibility() >= CLEO_VER_4_MIN)
std::fill(scope, scope + 32, fill_val); // fill with zeros
cs->SetScmFunction(thisScmFunctionId = allocationPlace);
}
void Return(CRunningScript *thread)
{
auto cs = reinterpret_cast<CCustomScript*>(thread);
std::copy(savedTls, savedTls + 32, cs->IsMission() ? missionLocals : cs->LocalVar);
cs->SetIp(retnAddress);
cs->SetScmFunction(prevScmFunctionId);
}
};
ScmFunction *ScmFunction::Store[store_size] = { /* default initializer - nullptr */ };
size_t ScmFunction::allocationPlace = 0;
void ResetScmFunctionStore()
{
for each(ScmFunction *scmFunc in ScmFunction::Store)
{
if (scmFunc) delete scmFunc;
}
ScmFunction::allocationPlace = 0;
}
/************************************************************************/
/* Opcode definitions */
/************************************************************************/
//0A8C=4,write_memory %1d% size %2d% value %3d% virtual_protect %4d%
OpcodeResult __stdcall opcode_0A8C(CRunningScript *thread)
{
GetScriptParams(thread, 4);
void *Address = opcodeParams[0].pParam;
DWORD size = opcodeParams[1].dwParam;
DWORD value = opcodeParams[2].dwParam;
bool vp = opcodeParams[3].bParam;
switch (size)
{
default:
GetInstance().CodeInjector.MemoryWrite<BYTE>(Address, value, vp, size);
break;
case 2:
GetInstance().CodeInjector.MemoryWrite<WORD>(Address, value, vp);
break;
case 4:
GetInstance().CodeInjector.MemoryWrite<DWORD>(Address, value, vp);
break;
}
return OR_CONTINUE;
}
//0A8D=4,%4d% = read_memory %1d% size %2d% virtual_protect %3d%
OpcodeResult __stdcall opcode_0A8D(CRunningScript *thread)
{
GetScriptParams(thread, 3);
//DWORD value;
void *Address = opcodeParams[0].pParam;
DWORD size = opcodeParams[1].dwParam;
bool vp = opcodeParams[2].bParam;
opcodeParams[0].dwParam = 0;
switch (size)
{
case 1:
GetInstance().CodeInjector.MemoryRead(Address, (BYTE)opcodeParams[0].ucParam, vp);
break;
case 2:
GetInstance().CodeInjector.MemoryRead(Address, (WORD)opcodeParams[0].usParam, vp);
break;
case 4:
GetInstance().CodeInjector.MemoryRead(Address, (DWORD)opcodeParams[0].dwParam, vp);
break;
default:
TRACE("[0A8D] Unallowed size %u", size);
}
SetScriptParams(thread, 1);
return OR_CONTINUE;
}
//0A8E=3,%3d% = %1d% + %2d% ; int
OpcodeResult __stdcall opcode_0A8E(CRunningScript *thread)
{
GetScriptParams(thread, 2);
opcodeParams[0].nParam += opcodeParams[1].nParam;
SetScriptParams(thread, 1);
return OR_CONTINUE;
}
//0A8F=3,%3d% = %1d% - %2d% ; int
OpcodeResult __stdcall opcode_0A8F(CRunningScript *thread)
{
GetScriptParams(thread, 2);
opcodeParams[0].nParam -= opcodeParams[1].nParam;
SetScriptParams(thread, 1);
return OR_CONTINUE;
}
//0A90=3,%3d% = %1d% * %2d% ; int
OpcodeResult __stdcall opcode_0A90(CRunningScript *thread)
{
GetScriptParams(thread, 2);
opcodeParams[0].nParam *= opcodeParams[1].nParam;
SetScriptParams(thread, 1);
return OR_CONTINUE;
}
//0A91=3,%3d% = %1d% / %2d% ; int
OpcodeResult __stdcall opcode_0A91(CRunningScript *thread)
{
GetScriptParams(thread, 2);
opcodeParams[0].nParam /= opcodeParams[1].nParam;
SetScriptParams(thread, 1);
return OR_CONTINUE;
}
//0A92=-1,create_custom_thread %1d%
OpcodeResult __stdcall opcode_0A92(CRunningScript *thread)
{
const char *script_name = readString(thread);
TRACE("[0A92] Starting new custom script %s from thread named %s", script_name, thread->GetName());
char cwd[MAX_PATH];
_getcwd(cwd, sizeof(cwd));
_chdir(cleo_dir);
auto cs = new CCustomScript(script_name);
SetScriptCondResult(thread, cs && cs->IsOK());
if (cs && cs->IsOK())
{
GetInstance().ScriptEngine.AddCustomScript(cs);
TransmitScriptParams(thread, cs);
}
else
{
if (cs) delete cs;
SkipUnusedParameters(thread);
TRACE("[0A92] Failed to load script '%s' from script '%s'.", script_name, thread->GetName());
}
_chdir(cwd);
return OR_CONTINUE;
}
//0A93=0,end_custom_thread
OpcodeResult __stdcall opcode_0A93(CRunningScript *thread)
{
CCustomScript *cs = reinterpret_cast<CCustomScript *>(thread);
if (thread->IsMission() || !cs->IsCustom())
{
TRACE("[0A93] Incorrect usage of opcode in script '%s'", thread->GetName());
return OR_CONTINUE;
}