forked from AndroidModLoader/GTA_CLEOMod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleohelpers.h
1024 lines (964 loc) · 30.3 KB
/
cleohelpers.h
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
#define MAX_STR_LEN 0xFF
#define MAX_SCRIPT_VARS_TO_SAVE 32
#define CHEAT_STRING_SIZE 30
#include <stdint.h>
#include <string>
#include <deque>
#include <filesystem>
#include "cleo.h"
#include "cleoaddon.h"
extern eGameIdent* nGameIdent;
extern cleo_ifs_t* cleo;
extern cleo_addon_ifs_t cleo_addon_ifs;
extern uint8_t* ScriptSpace;
extern int* pScriptsStorage, *pScriptsStorageEnd;
extern void (*UpdateCompareFlag)(void*, uint8_t);
namespace fs = std::filesystem;
#define CLEO_RegisterOpcode(x, h) cleo->RegisterOpcode(x, h); cleo->RegisterOpcodeFunction(#h, h)
#define CLEO_Fn(h) void h (void *handle, uint32_t *ip, uint16_t opcode, const char *name)
// https://github.com/gta-reversed/gta-reversed-modern/blob/1b37b015fbda7957ebbc36dbe8d5e4a90ebb6891/source/game_sa/Scripts/RunningScript.h#L17
constexpr auto SHORT_STRING_SIZE = 8;
constexpr auto LONG_STRING_SIZE = 16;
enum eScriptParameterType : int8_t
{
SCRIPT_PARAM_END_OF_ARGUMENTS, //< Special type used for vararg stuff
SCRIPT_PARAM_STATIC_INT_32BITS,
SCRIPT_PARAM_GLOBAL_NUMBER_VARIABLE, //< Global int32 variable
SCRIPT_PARAM_LOCAL_NUMBER_VARIABLE, //< Local int32 variable
SCRIPT_PARAM_STATIC_INT_8BITS,
SCRIPT_PARAM_STATIC_INT_16BITS,
SCRIPT_PARAM_STATIC_FLOAT,
// Types below are only available in GTA SA
// Number arrays
SCRIPT_PARAM_GLOBAL_NUMBER_ARRAY, //< Global array of numbers (always int32)
SCRIPT_PARAM_LOCAL_NUMBER_ARRAY, //< Local array of numbers (always int32)
SCRIPT_PARAM_STATIC_SHORT_STRING, //< Static 8 byte string
SCRIPT_PARAM_GLOBAL_SHORT_STRING_VARIABLE, //< Local 8 byte string
SCRIPT_PARAM_LOCAL_SHORT_STRING_VARIABLE, //< Local 8 byte string
SCRIPT_PARAM_GLOBAL_SHORT_STRING_ARRAY, //< Global 8 byte string array
SCRIPT_PARAM_LOCAL_SHORT_STRING_ARRAY, //< Local 8 byte string array
SCRIPT_PARAM_STATIC_PASCAL_STRING, //< Pascal string is a sequence of characters with optional size specification. (So says Google)
SCRIPT_PARAM_STATIC_LONG_STRING, //< 16 byte string
SCRIPT_PARAM_GLOBAL_LONG_STRING_VARIABLE, //< Global 16 byte string
SCRIPT_PARAM_LOCAL_LONG_STRING_VARIABLE, //< Local 16 byte string
SCRIPT_PARAM_GLOBAL_LONG_STRING_ARRAY, //< Global array of 16 byte strings
SCRIPT_PARAM_LOCAL_LONG_STRING_ARRAY, //< Local array of 16 byte strings
};
#include <set>
extern std::set<void*> gAllocationsMap;
extern std::set<FILE*> gFilesMap;
// Game Structs
struct GTAVector3D
{
float x, y, z;
float SqrMagnitude() { return x*x + y*y + z*z; }
inline GTAVector3D operator-(const GTAVector3D& v) { return { x - v.x, y - v.y, z - v.z }; }
};
struct GTAMatrix
{
GTAVector3D right;
unsigned int flags;
GTAVector3D up;
unsigned int pad1;
GTAVector3D at;
unsigned int pad2;
GTAVector3D pos;
unsigned int pad3;
void* ptr;
bool bln;
};
struct GTAScript
{
GTAScript* next;
GTAScript* prev;
char name[8];
// Incomplete so dont try it ;)
};
struct tByteFlag
{
uint8_t nId : 7;
bool bEmpty : 1;
};
struct GTAEntity
{
inline uintptr_t AsInt() { return (uintptr_t)this; }
inline int& IntAt(int off) { return *(int*)(AsInt() + off); }
inline uint32_t& UIntAt(int off) { return *(uint32_t*)(AsInt() + off); }
inline uint8_t& UInt8At(int off) { return *(uint8_t*)(AsInt() + off); }
inline GTAVector3D& GetPos()
{
GTAMatrix* matrix = *(GTAMatrix**)(AsInt() + 20);
return matrix ? matrix->pos : *(GTAVector3D*)(AsInt() + 4);
}
};
struct GTAPedSA : GTAEntity
{
char structure[1996];
bool Player() { return UIntAt(1436) < 2; }
};
struct GTAVehicleSA : GTAEntity
{
char structure[2604];
};
struct GTAObjectSA : GTAEntity
{
char structure[420];
};
union GXTChar
{
struct { char s1, s2; };
uint16_t s;
};
// Custom Funcs
void AddGXTLabel(const char* gxtLabel, const char* text);
inline void* AllocMem(size_t size)
{
void* mem = malloc(size);
if(mem) gAllocationsMap.insert(mem);
return mem;
}
inline bool IsAlloced(void* mem)
{
return (gAllocationsMap.find(mem) != gAllocationsMap.end());
}
inline void FreeMem(void* mem)
{
if (gAllocationsMap.find(mem) != gAllocationsMap.end())
{
free(mem);
gAllocationsMap.erase(mem);
}
}
inline FILE* DoFile(const char* filename, const char* mode)
{
char path[256];
snprintf(path, sizeof(path), "%s/%s", cleo->GetCleoStorageDir(), filename);
FILE* file = fopen(path, mode);
if(file) gFilesMap.insert(file);
return file;
}
inline bool IsFileCreated(FILE* file)
{
return (file && gFilesMap.find(file) != gFilesMap.end());
}
inline void FreeFile(FILE* file)
{
if(file)
{
fflush(file);
fclose(file);
if (gFilesMap.find(file) != gFilesMap.end())
{
gFilesMap.erase(file);
}
}
}
inline void AsciiToGXTChar(const char* src, GXTChar* dst)
{
int i = 0;
while(src[i])
{
dst[i].s = src[i];
++i;
}
dst[i].s = 0;
}
inline void AsciiToGXTChar(const char* src, uint16_t* dst) { AsciiToGXTChar(src, (GXTChar*)dst); }
inline const char* GXTCharToAscii(const GXTChar* src, uint8_t start)
{
static char buf[256] = { 0 };
if(!src) return buf;
const char* str = (char*)src;
int i = 0;
char symbol;
while (i < (sizeof(buf) - 1) && (symbol = str[2 * i]))
{
if (symbol >= 0x80 && symbol <= 0x83)
symbol += 0x40;
else if (symbol >= 0x84 && symbol <= 0x8D)
symbol += 0x42;
else if (symbol >= 0x8E && symbol <= 0x91)
symbol += 0x44;
else if (symbol >= 0x92 && symbol <= 0x95)
symbol += 0x47;
else if (symbol >= 0x96 && symbol <= 0x9A)
symbol += 0x49;
else if (symbol >= 0x9B && symbol <= 0xA4)
symbol += 0x4B;
else if (symbol >= 0xA5 && symbol <= 0xA8)
symbol += 0x4D;
else if (symbol >= 0xA9 && symbol <= 0xCC)
symbol += 0x50;
else if (symbol == 0xCD)
symbol = 0xD1;
else if (symbol == 0xCE)
symbol = 0xF1;
else if (symbol == 0xCF)
symbol = 0xBF;
else if (symbol >= 0xD0)
symbol = 0x23; // '#'
buf[i] = symbol;
++i; // uint16_t
}
buf[i] = 0;
return buf;
}
inline const char* GXTCharToAscii(const uint16_t* src, uint8_t start) { return GXTCharToAscii((const GXTChar*)src, start); }
inline int ValueForGame(int for3, int forvc, int forsa, int forlcs = 0, int forvcs = 0)
{
switch(*nGameIdent)
{
case GTA3: return for3;
case GTAVC: return forvc;
case GTASA: return forsa;
case GTALCS: return forlcs;
case GTAVCS: return forvcs;
}
return 0;
}
inline int ValueForSA(int forsa, int forothers)
{
return *nGameIdent == GTASA ? forsa : forothers;
}
inline int GetPCOffset()
{
switch(*nGameIdent)
{
case GTASA: return 20;
case GTALCS: return 24;
default: return 16;
}
}
inline uint8_t*& GetPC(void* handle)
{
return *(uint8_t**)((uintptr_t)handle + GetPCOffset());
}
inline uint8_t* GetPC_CLEO(void* handle) // weird-ass trash from CLEO for VC *facepalm*
{
return (uint8_t*)cleo->GetRealCodePointer(*(uint32_t*)((uintptr_t)handle + GetPCOffset()));
}
inline uint8_t*& GetBasePC(void* handle)
{
return *(uint8_t**)((uintptr_t)handle + GetPCOffset() - 4);
}
inline uint8_t** GetStack(void* handle)
{
return (uint8_t**)((uintptr_t)handle + ValueForGame(20, 20, 24, 28, 20));
}
inline uint16_t& GetStackDepth(void* handle)
{
return *(uint16_t*)((uintptr_t)handle + ValueForGame(44, 44, 56, 92, 516));
}
inline bool& GetCond(void* handle)
{
return *(bool*)((uintptr_t)handle + ValueForGame(121, 121, 229, 525, 521));
}
inline bool& GetNotFlag(void* handle)
{
return *(bool*)((uintptr_t)handle + ValueForGame(130, 130, 242));
}
inline uint16_t& GetLogicalOp(void* handle)
{
return *(uint16_t*)((uintptr_t)handle + ValueForGame(128, 128, 240));
}
inline bool& IsMissionScript(void* handle)
{
return *(bool*)((uintptr_t)handle + ValueForGame(133, 133, 252));
}
inline void PushStack(void* handle)
{
uint8_t** stack = GetStack(handle);
uint8_t*& bytePtr = GetPC(handle);
uint16_t& stackDepth = GetStackDepth(handle);
stack[stackDepth++] = bytePtr;
}
inline void PopStack(void* handle)
{
GetPC(handle) = GetStack(handle)[--GetStackDepth(handle)];
}
inline int* GetLocalVars(void* handle)
{
return (int*)((uintptr_t)handle + ValueForGame(48, 48, 60, 96, 520));
}
inline void ThreadJump(void* handle, int offset)
{
uint8_t* newPCPointer = (uint8_t*)cleo->GetRealLabelPointer(handle, offset);
if(newPCPointer) // CLEO script
{
GetPC(handle) = newPCPointer;
}
else // Not CLEO script (main.scm ???)
{
uint8_t*& bytePtr = GetPC(handle);
int baseOffset = ValueForGame(0, 0, 16, 20, 20);
if(baseOffset)
{
uint8_t* basePtr = GetBasePC(handle);
bytePtr = (uint8_t*)((offset < 0) ? (basePtr - offset) : (ScriptSpace + offset));
}
else
{
bytePtr = (uint8_t*)((offset < 0) ? (ValueForGame(0x20000, 0x370E8, 0, 0) - offset) : offset);
}
}
}
inline void Skip1Byte(void* handle)
{
GetPC(handle) += 1;
}
inline void Skip2Bytes(void* handle)
{
GetPC(handle) += 2;
}
inline void Skip4Bytes(void* handle)
{
GetPC(handle) += 4;
}
inline void SkipBytes(void* handle, uint32_t bytes)
{
GetPC(handle) += bytes;
}
inline uint8_t* GetRealPC(void* handle)
{
return (cleo->GetGameIdentifier() == GTASA ? GetPC(handle) : GetPC_CLEO(handle));
}
inline uint8_t Read1Byte_NoSkip(void* handle)
{
return *GetRealPC(handle);
}
inline uint16_t Read2Bytes_NoSkip(void* handle)
{
return *(uint16_t*)GetRealPC(handle);
}
inline uint32_t Read4Bytes_NoSkip(void* handle)
{
return *(uint32_t*)GetRealPC(handle);
}
inline uint8_t Read1Byte(void* handle)
{
uint8_t thatOneByte = *GetRealPC(handle);
Skip1Byte(handle);
return thatOneByte;
}
inline uint16_t Read2Bytes(void* handle)
{
uint16_t theseBytes = *(uint16_t*)GetRealPC(handle);
Skip2Bytes(handle);
return theseBytes;
}
inline uint32_t Read4Bytes(void* handle)
{
uint32_t theseBytes = *(uint16_t*)GetRealPC(handle);
Skip4Bytes(handle);
return theseBytes;
}
inline char* CLEO_ReadStringEx(void* handle, char* buf = NULL, size_t size = 0)
{
uint8_t type = Read1Byte_NoSkip(handle);
static char newBuf[MAX_STR_LEN];
if(!buf) buf = (char*)newBuf;
if(size < 1) size = MAX_STR_LEN;
switch(type)
{
default:
return NULL; // ok, what's left?
case SCRIPT_PARAM_STATIC_INT_8BITS:
case SCRIPT_PARAM_STATIC_INT_16BITS:
buf[0] = (char)cleo->ReadParam(handle)->i;
buf[1] = 0;
return buf;
case SCRIPT_PARAM_STATIC_INT_32BITS:
case SCRIPT_PARAM_GLOBAL_NUMBER_VARIABLE:
case SCRIPT_PARAM_LOCAL_NUMBER_VARIABLE:
case SCRIPT_PARAM_STATIC_FLOAT:
case SCRIPT_PARAM_GLOBAL_NUMBER_ARRAY:
case SCRIPT_PARAM_LOCAL_NUMBER_ARRAY:
{
char *str = (char*)cleo->ReadParam(handle)->i;
if(!str) return NULL;
strncpy(buf, str, size - 1);
buf[size - 1] = 0;
return buf;
}
case SCRIPT_PARAM_STATIC_SHORT_STRING:
Skip1Byte(handle); // "type" byte
if(size < 1) size = SHORT_STRING_SIZE;
for(int i = 0; i < SHORT_STRING_SIZE; ++i)
{
if(i < size) buf[i] = (char)Read1Byte(handle);
else Skip1Byte(handle);
buf[size - 1] = 0;
}
return buf;
//return cleo->ReadString8byte(handle, buf, size) ? buf : NULL;
case SCRIPT_PARAM_GLOBAL_SHORT_STRING_VARIABLE:
case SCRIPT_PARAM_LOCAL_SHORT_STRING_VARIABLE:
case SCRIPT_PARAM_GLOBAL_SHORT_STRING_ARRAY:
case SCRIPT_PARAM_LOCAL_SHORT_STRING_ARRAY:
{
size = ((size > SHORT_STRING_SIZE) ? SHORT_STRING_SIZE : size) - 1;
strncpy(buf, (char*)cleo->GetPointerToScriptVar(handle), size);
buf[size] = 0;
return buf;
}
case SCRIPT_PARAM_STATIC_PASCAL_STRING: // not even a LONG STRING
return cleo->ReadStringLong(handle, buf, size) ? buf : NULL;
case SCRIPT_PARAM_STATIC_LONG_STRING:
Skip1Byte(handle);
size = ((size > LONG_STRING_SIZE) ? LONG_STRING_SIZE : size) - 1;
strncpy(buf, (char*)GetRealPC(handle), size);
buf[size] = 0;
SkipBytes(handle, LONG_STRING_SIZE);
return buf;
case SCRIPT_PARAM_GLOBAL_LONG_STRING_VARIABLE:
case SCRIPT_PARAM_LOCAL_LONG_STRING_VARIABLE:
case SCRIPT_PARAM_GLOBAL_LONG_STRING_ARRAY:
case SCRIPT_PARAM_LOCAL_LONG_STRING_ARRAY:
{
size = ((size > LONG_STRING_SIZE) ? LONG_STRING_SIZE : size) - 1;
strncpy(buf, (char*)cleo->GetPointerToScriptVar(handle), size);
buf[size] = 0;
return buf;
}
}
return buf;
}
inline void CLEO_WriteStringEx(void* handle, const char* buf)
{
uint8_t byte = Read1Byte_NoSkip(handle);
char* dst;
switch(byte)
{
default:
dst = (char*)cleo->ReadParam(handle)->i;
strcpy(dst, buf);
break;
case SCRIPT_PARAM_GLOBAL_SHORT_STRING_VARIABLE:
case SCRIPT_PARAM_LOCAL_SHORT_STRING_VARIABLE:
case SCRIPT_PARAM_GLOBAL_SHORT_STRING_ARRAY:
case SCRIPT_PARAM_LOCAL_SHORT_STRING_ARRAY:
{
dst = (char*)cleo->GetPointerToScriptVar(handle);
strncpy(dst, buf, SHORT_STRING_SIZE-1); dst[SHORT_STRING_SIZE-1] = 0;
break;
}
case SCRIPT_PARAM_GLOBAL_LONG_STRING_VARIABLE:
case SCRIPT_PARAM_LOCAL_LONG_STRING_VARIABLE:
case SCRIPT_PARAM_GLOBAL_LONG_STRING_ARRAY:
case SCRIPT_PARAM_LOCAL_LONG_STRING_ARRAY:
{
dst = (char*)cleo->GetPointerToScriptVar(handle);
strncpy(dst, buf, LONG_STRING_SIZE-1); dst[LONG_STRING_SIZE-1] = 0;
break;
}
}
}
inline char* CLEO_GetStringPtr(void* handle)
{
uint8_t byte = Read1Byte_NoSkip(handle);
if(byte >= SCRIPT_PARAM_STATIC_SHORT_STRING)
{
return (char*)cleo->GetPointerToScriptVar(handle);
}
else
{
return (char*)cleo->ReadParam(handle)->i;
}
}
inline uint32_t CLEO_GetStringPtrMaxSize(void* handle)
{
uint8_t byte = Read1Byte_NoSkip(handle);
switch(byte)
{
default:
return MAX_STR_LEN;
case SCRIPT_PARAM_STATIC_SHORT_STRING:
case SCRIPT_PARAM_GLOBAL_SHORT_STRING_VARIABLE:
case SCRIPT_PARAM_LOCAL_SHORT_STRING_VARIABLE:
case SCRIPT_PARAM_GLOBAL_SHORT_STRING_ARRAY:
case SCRIPT_PARAM_LOCAL_SHORT_STRING_ARRAY:
return SHORT_STRING_SIZE;
case SCRIPT_PARAM_STATIC_LONG_STRING:
case SCRIPT_PARAM_GLOBAL_LONG_STRING_VARIABLE:
case SCRIPT_PARAM_LOCAL_LONG_STRING_VARIABLE:
case SCRIPT_PARAM_GLOBAL_LONG_STRING_ARRAY:
case SCRIPT_PARAM_LOCAL_LONG_STRING_ARRAY:
return LONG_STRING_SIZE;
}
}
// https://github.com/cleolibrary/CLEO4/blob/efe00ef49945a85012cc2938c27ff82cccea5866/source/CCustomOpcodeSystem.cpp#L462
inline int CLEO_FormatString(void* handle, char *str, size_t len, const char *format)
{
unsigned int written = 0;
const char *iter = format;
char bufa[MAX_STR_LEN], fmtbufa[64], *fmta, readbuf[MAX_STR_LEN];
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
sprintf(buffiter, "%d", cleo->ReadParam(handle)->i);
while (*buffiter) *fmta++ = *buffiter++;
}
else
{
*fmta++ = *iter;
}
iter++;
}
//get immediate width value
while (isdigit(*iter)) *fmta++ = *iter++;
//get precision
if (*iter == '.')
{
*fmta++ = *iter++;
if (*iter == '*')
{
char *buffiter = bufa;
sprintf(buffiter, "%d", cleo->ReadParam(handle)->i);
while (*buffiter) *fmta++ = *buffiter++;
}
else
{
while (isdigit(*iter)) *fmta++ = *iter++;
}
}
//get size
if (*iter == 'h' || *iter == 'l')
{
*fmta++ = *iter++;
}
switch (*iter)
{
case 's':
{
*fmta++ = *iter++;
*fmta++ = 0;
static const char none[] = "(null)";
const char *astr = CLEO_ReadStringEx(handle, readbuf, sizeof(readbuf));
const char *striter = astr ? astr : none;
snprintf(bufa, sizeof(bufa), fmtbufa, striter); striter = bufa;
while (*striter)
{
if (written++ >= len) return -1;
*str++ = *striter++;
}
break;
}
case 'c':
{
*fmta++ = *iter++;
*fmta++ = 0;
snprintf(bufa, sizeof(bufa), fmtbufa, (char)cleo->ReadParam(handle)->i);
const char *striter = bufa;
while (*striter)
{
if (written++ >= len) return -1;
*str++ = *striter++;
}
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')
{
sprintf(bufaiter, "%08x", cleo->ReadParam(handle)->u);
}
else if (*iter == 'P')
{
sprintf(bufaiter, "%08X", cleo->ReadParam(handle)->u);
}
else
{
*fmta++ = *iter;
*fmta = '\0';
if (*iter == 'a' || *iter == 'A' ||
*iter == 'e' || *iter == 'E' ||
*iter == 'f' || *iter == 'F' ||
*iter == 'g' || *iter == 'G')
{
sprintf(bufaiter, fmtbufa, cleo->ReadParam(handle)->f);
}
else
{
sprintf(bufaiter, fmtbufa, cleo->ReadParam(handle)->i);
}
}
while (*bufaiter)
{
if (written++ >= len) return -1;
*str++ = *bufaiter++;
}
iter++;
break;
}
}
}
}
if (written >= len) return -1;
*str++ = 0;
return (int)written;
}
inline bool IsCLEORelatedGXTKey(char* gxtLabel)
{
if(gxtLabel[0] == 'C' && gxtLabel[1] == 'L') // Most likely a CLEO
{
if((gxtLabel[2] == 'M' && gxtLabel[3] == 'N' && gxtLabel[4] == 'U') ||
(gxtLabel[2] == 'D' && gxtLabel[3] == 'S' && gxtLabel[4] == 'C'))
return true; // nuh-uh
}
else if(gxtLabel[0] == 'C' && gxtLabel[1] == 'S' && gxtLabel[2] == 'I' && gxtLabel[3] == '_') return true; // nuh-uh
else if(gxtLabel[0] == 'S' && gxtLabel[1] == 'P' && gxtLabel[2] == 'L' &&
gxtLabel[3] == 'A' && gxtLabel[4] == 'S' && gxtLabel[5] == 'H') return true; // nuh-uh
return false; // uh-nuh
}
extern uint16_t FreeScriptAddonInfoId;
extern ScriptAddonInfo ScriptAddonInfosStorage[0x400];
inline uint16_t AssignAddonInfo(void* handle)
{
uint16_t id = FreeScriptAddonInfoId++;
*(uint16_t*)((uintptr_t)handle + ValueForGame(0x26, 0x2E, 0x3A, 0, 0)) = id;
return id;
}
inline ScriptAddonInfo& GetAddonInfo(void* handle)
{
uint16_t id = *(uint16_t*)((uintptr_t)handle + ValueForGame(0x26, 0x2E, 0x3A, 0, 0));
if(!id) id = AssignAddonInfo(handle);
return ScriptAddonInfosStorage[id];
}
inline uint16_t GetScmFunc(void* handle)
{
return GetAddonInfo(handle).scmFuncId;
}
inline void SetScmFunc(void* handle, uint16_t idx)
{
GetAddonInfo(handle).scmFuncId = idx;
}
inline void SkipOpcodeParameters(void* handle, int count)
{
int len;
for(int i = 0; i < count; ++i)
{
switch(Read1Byte(handle))
{
case SCRIPT_PARAM_STATIC_INT_8BITS:
SkipBytes(handle, 1);
break;
case SCRIPT_PARAM_STATIC_INT_16BITS:
case SCRIPT_PARAM_GLOBAL_NUMBER_VARIABLE:
case SCRIPT_PARAM_LOCAL_NUMBER_VARIABLE:
case SCRIPT_PARAM_GLOBAL_LONG_STRING_VARIABLE:
case SCRIPT_PARAM_LOCAL_LONG_STRING_VARIABLE:
case SCRIPT_PARAM_GLOBAL_SHORT_STRING_VARIABLE:
case SCRIPT_PARAM_LOCAL_SHORT_STRING_VARIABLE:
SkipBytes(handle, 2);
break;
case SCRIPT_PARAM_GLOBAL_NUMBER_ARRAY:
case SCRIPT_PARAM_LOCAL_NUMBER_ARRAY:
case SCRIPT_PARAM_GLOBAL_SHORT_STRING_ARRAY:
case SCRIPT_PARAM_LOCAL_SHORT_STRING_ARRAY:
case SCRIPT_PARAM_GLOBAL_LONG_STRING_ARRAY:
case SCRIPT_PARAM_LOCAL_LONG_STRING_ARRAY:
SkipBytes(handle, 6);
break;
case SCRIPT_PARAM_STATIC_INT_32BITS:
case SCRIPT_PARAM_STATIC_FLOAT:
SkipBytes(handle, 4);
break;
case SCRIPT_PARAM_STATIC_SHORT_STRING:
SkipBytes(handle, 8);
break;
case SCRIPT_PARAM_STATIC_LONG_STRING:
SkipBytes(handle, 16);
break;
case SCRIPT_PARAM_STATIC_PASCAL_STRING:
int len = (int)Read1Byte(handle);
SkipBytes(handle, len);
break;
}
}
}
inline void SkipUnusedParameters(void *handle)
{
while(Read1Byte_NoSkip(handle) != 0) SkipOpcodeParameters(handle, 1);
Skip1Byte(handle); // skip ending byte
}
inline int GetVarArgCount(void* handle)
{
int count = 0;
uint8_t* pcsave = GetPC(handle);
while(Read1Byte_NoSkip(handle) != SCRIPT_PARAM_END_OF_ARGUMENTS)
{
SkipOpcodeParameters(handle, 1);
++count;
}
GetPC(handle) = pcsave;
return count;
}
inline int GetScriptsStorageSize()
{
return (*pScriptsStorageEnd - *pScriptsStorage) >> 2;
}
inline void* GetScriptHandleFromStorage(int i)
{
if(i >= 0 && i < (*pScriptsStorageEnd - *pScriptsStorage) >> 2)
{
int storageItem = *(int*)(*pScriptsStorage + i * 4);
if(storageItem)
{
return *(void**)(storageItem + 28);
}
}
return NULL;
}
inline int GetScriptMenuIndexFromStorage(int i)
{
if(i >= 0 && i < (*pScriptsStorageEnd - *pScriptsStorage) >> 2)
{
int storageItem = *(int*)(*pScriptsStorage + i * 4);
if(storageItem)
{
return *(int*)(storageItem + 24);
}
}
return -1;
}
inline void* GetScriptHandleFromStorage_NoCheck(int i)
{
int storageItem = *(int*)(*pScriptsStorage + i * 4);
if(storageItem)
{
return *(void**)(storageItem + 28);
}
return NULL;
}
inline int GetScriptMenuIndexFromStorage_NoCheck(int i)
{
int storageItem = *(int*)(*pScriptsStorage + i * 4);
if(storageItem)
{
return *(int*)(storageItem + 24);
}
return -1;
}
inline void* CLEO_GetScriptFromFilename(const char* filename)
{
int len = GetScriptsStorageSize();
for(int i = 0; i < len; ++i)
{
int storageItem = *(int*)(*pScriptsStorage + i * 4);
const char* storageFile = *(const char**)(storageItem + 20);
if(!strcasecmp(storageFile, filename)) return *(void**)(storageItem + 28);
}
return NULL;
}
inline const char* CLEO_GetScriptFilename(void* handle)
{
int len = GetScriptsStorageSize();
for(int i = 0; i < len; ++i)
{
int storageItem = *(int*)(*pScriptsStorage + i * 4);
void* scriptHandle = *(void**)(storageItem + 28);
if(scriptHandle == handle) return *(const char**)(storageItem + 20);
}
return NULL;
}
inline const char* GetVarTypeName(eScriptParameterType type)
{
#define STRP(__param) case SCRIPT_PARAM_##__param: return #__param
switch(type)
{
STRP(END_OF_ARGUMENTS);
STRP(STATIC_INT_32BITS);
STRP(GLOBAL_NUMBER_VARIABLE);
STRP(LOCAL_NUMBER_VARIABLE);
STRP(STATIC_INT_8BITS);
STRP(STATIC_INT_16BITS);
STRP(STATIC_FLOAT);
STRP(GLOBAL_NUMBER_ARRAY);
STRP(LOCAL_NUMBER_ARRAY);
STRP(STATIC_SHORT_STRING);
STRP(GLOBAL_SHORT_STRING_VARIABLE);
STRP(LOCAL_SHORT_STRING_VARIABLE);
STRP(GLOBAL_SHORT_STRING_ARRAY);
STRP(LOCAL_SHORT_STRING_ARRAY);
STRP(STATIC_PASCAL_STRING);
STRP(STATIC_LONG_STRING);
STRP(GLOBAL_LONG_STRING_VARIABLE);
STRP(LOCAL_LONG_STRING_VARIABLE);
STRP(GLOBAL_LONG_STRING_ARRAY);
STRP(LOCAL_LONG_STRING_ARRAY);
}
#undef STRP
return "UNKNOWN";
}
extern GTAScript **pActiveScripts, **pIdleScripts;
inline bool IsInActiveScripts(void* handle)
{
for (GTAScript* script = *pActiveScripts; script != NULL; script = script->next)
{
if (script == handle)
{
return true;
}
}
return false;
}
inline bool IsInPausedScripts(void* handle)
{
for (GTAScript* script = *pIdleScripts; script != NULL; script = script->next)
{
if (script == handle)
{
return true;
}
}
return false;
}
inline bool IsInCLEOScripts(void* handle)
{
int len = GetScriptsStorageSize();
for(int i = 0; i < len; ++i)
{
int storageItem = *(int*)(*pScriptsStorage + i * 4);
if(*(void**)(storageItem + 28) == handle) return true;
}
return false;
}
inline bool IsValidScriptHandle(void* handle)
{
return IsInActiveScripts(handle) || IsInPausedScripts(handle) || IsInCLEOScripts(handle);
}
inline bool IsParamString(void* handle, bool checkIfPointer = false)
{
if(Read1Byte_NoSkip(handle) >= SCRIPT_PARAM_STATIC_SHORT_STRING)
{
return true;
}
else if(checkIfPointer)
{
uint8_t* backupPC = GetPC(handle);
void* probMem = (void*)cleo->ReadParam(handle)->i;
GetPC(handle) = backupPC;
if(IsAlloced(probMem)) return true;
}
return false;
}
// CLEO5
struct PausedScriptInfo
{
GTAScript* ptr;
std::string msg;
PausedScriptInfo(GTAScript* ptr, const char* msg) : ptr(ptr), msg(msg) {}
PausedScriptInfo(void* ptr, const char* msg) : ptr((GTAScript*)ptr), msg(msg) {}
};
extern std::deque<PausedScriptInfo> pausedScripts;
static const char DIR_GAME[] = "root:"; // game root directory
static const char DIR_USER[] = "userfiles:"; // game save directory
static const char DIR_SCRIPT[] = "."; // current script directory
static const char DIR_CLEO[] = "cleo:"; // game\cleo directory
static const char DIR_MODULES[] = "modules:"; // game\cleo\modules directory
inline const char* GetFilesDir()
{
return aml->GetAndroidDataPath();
}
inline std::string& GetWorkDir(void* handle)
{
return GetAddonInfo(handle).workDir;
}
inline const char* GetScriptWorkDir(void* handle)
{
std::string& workDir = GetWorkDir(handle);
if(GetAddonInfo(handle).isCustom && !workDir.empty()) return workDir.c_str();
return GetFilesDir();
}
inline const char* GetUserDirectory()
{
return aml->GetDataPath();
}
inline const char* GetCleoDir()
{
return cleo->GetCleoStorageDir();
}
inline const char* GetModulesDir()
{
return cleo->GetCleoPluginLoadDir();
}
inline std::string ResolvePath(void* handle, const char* path, const char* customWorkDir = NULL)
{
if(!path) return "";
enum class VPref{ None, Game, User, Script, Cleo, Modules } virtualPrefix = VPref::None;
fs::path fsPath = fs::path(path);
fs::path::iterator root = fsPath.begin();
if(root != fsPath.end())
{
if(*root == DIR_GAME) virtualPrefix = VPref::Game;
else if (*root == DIR_USER) virtualPrefix = VPref::User;
else if (*root == DIR_SCRIPT) virtualPrefix = VPref::Script;
else if (*root == DIR_CLEO) virtualPrefix = VPref::Cleo;
else if (*root == DIR_MODULES) virtualPrefix = VPref::Modules;
}
fs::path resolved;
switch(virtualPrefix)
{
default: //case VPref::None:
{
if(fsPath.is_relative())
{
if(customWorkDir != NULL)
fsPath = ResolvePath(handle, customWorkDir) / fsPath;
else
fsPath = GetScriptWorkDir(handle) / fsPath;
}
return fs::weakly_canonical(fsPath).string();
}
case VPref::User:
resolved = GetUserDirectory();