-
Notifications
You must be signed in to change notification settings - Fork 30
/
gsc.cpp
2161 lines (1821 loc) · 66.1 KB
/
gsc.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 "gsc.hpp"
/*
CoD2 search for "parameter count exceeds 256" and go upwards
CoD1 search for "parameter count exceeds 256" or "unknown (builtin) function '%s'" and go upwards
*/
#if COD_VERSION == COD2_1_0
Scr_GetFunction_t Scr_GetFunction = (Scr_GetFunction_t)0x08115824;
Scr_GetMethod_t Scr_GetMethod = (Scr_GetMethod_t)0x0811595C;
#elif COD_VERSION == COD2_1_2
Scr_GetFunction_t Scr_GetFunction = (Scr_GetFunction_t)0x08117B56;
Scr_GetMethod_t Scr_GetMethod = (Scr_GetMethod_t)0x08117C8E;
#elif COD_VERSION == COD2_1_3
Scr_GetFunction_t Scr_GetFunction = (Scr_GetFunction_t)0x08117CB2;
Scr_GetMethod_t Scr_GetMethod = (Scr_GetMethod_t)0x08117DEA;
#elif COD_VERSION == COD4_1_7 || COD_VERSION == COD4_1_7_L
//Scr_GetFunction_t Scr_GetFunction = (Scr_GetFunction_t)0x080BD238;
//Scr_GetMethod_t Scr_GetMethod = (Scr_GetMethod_t)0x080BFEF4;
cHook *hook_Scr_GetFunction;
cHook *hook_Scr_GetMethod;
Scr_FunctionCall Scr_GetFunction(const char **fname, int *fdev) {
//printf("CoD4 Scr_GetFunction: fdev=%d fname=%s\n", *fdev, *fname);
hook_Scr_GetFunction->unhook();
int (*sig)(const char **fname, int *fdev);
*(int *)&sig = hook_Scr_GetFunction->from;
int m = sig(fname, fdev);
hook_Scr_GetFunction->hook();
/*
when I use the real return-type instead of int (*sig), this errors occurs:
##### LINK libcod4_1_7.so #####
objects_cod4_1_7/libcod.opp: In function `Scr_GetFunction_CoD4(char const**, int*)':
libcod.cpp:(.text+0x132b): undefined reference to `sig(char const**, int*)'
/usr/bin/ld: objects_cod4_1_7/libcod.opp: relocation R_386_GOTOFF against undefined hidden symbol `_Z3sigPPKcPi' can not be used when making a shared object
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
k_cod4_nodlzom@Debian-70-wheezy-64-LAMP:~/libcod$ rm objects_cod4_1_7/libcod.opp
*/
return (Scr_FunctionCall)m;
}
Scr_MethodCall Scr_GetMethod(const char **fname, int *fdev) {
//printf("CoD4 Scr_GetMethod: fdev=%d fname=%s\n", *fdev, *fname);
hook_Scr_GetMethod->unhook();
int (*sig)(const char **fname, int *fdev);
*(int *)&sig = hook_Scr_GetMethod->from;
int m = sig(fname, fdev);
hook_Scr_GetMethod->hook();
return (Scr_MethodCall)m;
}
#else
#warning Scr_GetFunction_t Scr_GetFunction = (Scr_GetFunction_t)NULL;
#warning Scr_GetMethod_t Scr_GetMethod = (Scr_GetMethod_t)NULL;
Scr_GetFunction_t Scr_GetFunction = (Scr_GetFunction_t)NULL;
Scr_GetMethod_t Scr_GetMethod = (Scr_GetMethod_t)NULL;
#endif
#if COD_VERSION == COD2_1_0 // search "animation '%s' not defined in anim tree '%s'"
unsigned short (*GetVariableName)(unsigned short) = (unsigned short(*)(unsigned short))0x0807CA72;
unsigned short (*GetNextVariable)(unsigned short) = (unsigned short(*)(unsigned short))0x0807C9CE; //idk original funcname
#elif COD_VERSION == COD2_1_2
unsigned short (*GetVariableName)(unsigned short) = (unsigned short(*)(unsigned short))0x0807CFF6;
unsigned short (*GetNextVariable)(unsigned short) = (unsigned short(*)(unsigned short))0x0807CF52; //idk original funcname
#elif COD_VERSION == COD2_1_3
unsigned short (*GetVariableName)(unsigned short) = (unsigned short(*)(unsigned short))0x0807D0C2;
unsigned short (*GetNextVariable)(unsigned short) = (unsigned short(*)(unsigned short))0x0807D01E; //idk original funcname
#else
unsigned short (*GetVariableName)(unsigned short) = (unsigned short(*)(unsigned short))NULL;
unsigned short (*GetNextVariable)(unsigned short) = (unsigned short(*)(unsigned short))NULL;
#endif
#if COD_VERSION == COD2_1_0
char *(*SL_ConvertToString)(unsigned short) = (char*(*)(unsigned short))0x08078896;
#elif COD_VERSION == COD2_1_2
char *(*SL_ConvertToString)(unsigned short) = (char*(*)(unsigned short))0x08078E1A;
#elif COD_VERSION == COD2_1_3
char *(*SL_ConvertToString)(unsigned short) = (char*(*)(unsigned short))0x08078EE6;
#else
char *SL_ConvertToString(unsigned short) { return NULL; }//error_wrong_patch
#endif
char *stackGetParamTypeAsString(int param) {
aStackElement *scriptStack = *(aStackElement**)getStack();
aStackElement *arg = scriptStack - param;
char *type = "UNKNOWN TYPE!";
switch (arg->type) {
case 0: type = "UNDEFINED"; break;
case 1: type = "OBJECT"; break;
case 2: type = "STRING"; break;
case 3: type = "LOCALIZED_STRING"; break;
case 4: type = "VECTOR"; break;
case 5: type = "FLOAT"; break;
case 6: type = "INT"; break;
case 7: type = "CODEPOS"; break;
case 8: type = "PRECODEPOS"; break;
case 9: type = "FUNCTION"; break;
case 10: type = "STACK"; break;
case 11: type = "ANIMATION"; break;
case 12: type = "DEVELOPER_CODEPOS"; break;
case 13: type = "INCLUDE_CODEPOS"; break;
case 14: type = "THREAD_LIST"; break;
case 15: type = "THREAD_1"; break;
case 16: type = "THREAD_2"; break;
case 17: type = "THREAD_3"; break;
case 18: type = "THREAD_4"; break;
case 19: type = "STRUCT"; break;
case 20: type = "REMOVED_ENTITY"; break;
case 21: type = "ENTITY"; break;
case 22: type = "ARRAY"; break;
case 23: type = "REMOVED_THREAD"; break;
}
return type;
}
int stackPrintParam(int param) {
if (param >= stackGetNumberOfParams())
return 0;
switch (stackGetParamType(param)) {
case STACK_STRING:
char *str;
stackGetParamString(param, &str); // no error checking, since we know it's a string
printf("%s", str);
return 1;
case STACK_VECTOR:
float vec[3];
stackGetParamVector(param, vec);
printf("(%.2f, %.2f, %.2f)", vec[0], vec[1], vec[2]);
return 1;
case STACK_FLOAT:
float tmp_float;
stackGetParamFloat(param, &tmp_float);
printf("%.3f", tmp_float); // need a way to define precision
return 1;
case STACK_INT:
int tmp_int;
stackGetParamInt(param, &tmp_int);
printf("%d", tmp_int);
return 1;
}
printf("(%s)", stackGetParamTypeAsString(param));
return 0;
}
void gsc_utils_printf() {
char *str;
if ( ! stackGetParams("s", &str)) {
printf("scriptengine> WARNING: printf undefined argument!\n");
stackPushUndefined();
return;
}
int param = 1; // maps to first %
int len = strlen(str);
for (int i=0; i<len; i++) {
if (str[i] == '%')
{
if(str[i + 1] == '%') {
putchar('%');
i++;
} else
stackPrintParam(param++);
}
else
putchar(str[i]);
}
stackPushInt(1);
}
void gsc_utils_printfline() {
gsc_utils_printf();
printf("\n");
}
void gsc_utils_com_printf() {
char *str;
if ( ! stackGetParams("s", &str)) {
printf("scriptengine> WARNING: com_printf undefined argument!\n");
stackPushUndefined();
return;
}
Com_Printf("%s", str);
stackPushInt(1);
}
void gsc_utils_redirectprintf() {
char *str;
if ( ! stackGetParams("s", &str)) {
printf("scriptengine> WARNING: gsc_utils_redirectprintf undefined argument!\n");
stackPushUndefined();
return;
}
SV_FlushRedirect(str);
stackPushInt(1);
}
Scr_Function scriptFunctions[] = {
{"printf", gsc_utils_printf, 0},
{"printfline", gsc_utils_printfline, 0}, // adds \n at end
{"com_printf", gsc_utils_com_printf, 0},
{"redirectprintf", gsc_utils_redirectprintf, 0},
#if COD_VERSION < COD4_1_7
{"getArrayKeys", Scr_GetArrayKeys, 0},
#endif
#if COMPILE_MYSQL == 1
{"mysql_init" , gsc_mysql_init , 0},
{"mysql_real_connect" , gsc_mysql_real_connect , 0},
{"mysql_close" , gsc_mysql_close , 0},
{"mysql_query" , gsc_mysql_query , 0},
{"mysql_errno" , gsc_mysql_errno , 0},
{"mysql_error" , gsc_mysql_error , 0},
{"mysql_affected_rows" , gsc_mysql_affected_rows , 0},
{"mysql_store_result" , gsc_mysql_store_result , 0},
{"mysql_num_rows" , gsc_mysql_num_rows , 0},
{"mysql_num_fields" , gsc_mysql_num_fields , 0},
{"mysql_field_seek" , gsc_mysql_field_seek , 0},
{"mysql_fetch_field" , gsc_mysql_fetch_field , 0},
{"mysql_fetch_row" , gsc_mysql_fetch_row , 0},
{"mysql_free_result" , gsc_mysql_free_result , 0},
{"mysql_real_escape_string", gsc_mysql_real_escape_string, 0},
{"mysql_async_create_query", gsc_mysql_async_create_query, 0},
{"mysql_async_create_query_nosave", gsc_mysql_async_create_query_nosave, 0},
{"mysql_async_getdone_list", gsc_mysql_async_getdone_list, 0},
{"mysql_async_getresult_and_free", gsc_mysql_async_getresult_and_free, 0},
{"mysql_async_initializer" , gsc_mysql_async_initializer , 0},
{"mysql_reuse_connection" , gsc_mysql_reuse_connection , 0},
#endif
#if COMPILE_PLAYER == 1
{"free_slot" , gsc_free_slot , 0},
{"kick2" , gsc_kick_slot , 0},
#endif
#if COMPILE_MEMORY == 1
{"memory_malloc" , gsc_memory_malloc , 0},
{"memory_free" , gsc_memory_free , 0},
{"memory_int_get", gsc_memory_int_get, 0},
{"memory_int_set", gsc_memory_int_set, 0},
{"memory_memset" , gsc_memory_memset , 0},
{"binarybuffer_new" , gsc_binarybuffer_new , 0},
{"binarybuffer_free" , gsc_binarybuffer_free , 0},
{"binarybuffer_seek" , gsc_binarybuffer_seek , 0},
{"binarybuffer_write", gsc_binarybuffer_write, 0},
{"binarybuffer_read" , gsc_binarybuffer_read , 0},
#endif
#if COMPILE_UTILS == 1
{"disableGlobalPlayerCollision" , gsc_utils_disableGlobalPlayerCollision, 0},
{"getAscii" , gsc_utils_getAscii , 0},
{"toUpper" , gsc_utils_toupper , 0},
{"system" , gsc_utils_system , 0},
{"file_link" , gsc_utils_file_link , 0},
{"file_unlink" , gsc_utils_file_unlink , 0},
{"file_exists" , gsc_utils_file_exists , 0},
{"FS_LoadDir" , gsc_utils_FS_LoadDir , 0},
{"getType" , gsc_utils_getType , 0},
{"stringToFloat" , gsc_utils_stringToFloat , 0},
{"rundll" , gsc_utils_rundll , 0},
{"Cmd_ExecuteString" , gsc_utils_ExecuteString , 0},
{"sendGameServerCommand" , gsc_utils_sendgameservercommand , 0},
{"scandir" , gsc_utils_scandir , 0},
{"fopen" , gsc_utils_fopen , 0},
{"fread" , gsc_utils_fread , 0},
{"fwrite" , gsc_utils_fwrite , 0},
{"fclose" , gsc_utils_fclose , 0},
{"sprintf" , gsc_utils_sprintf , 0},
{"add_language" , gsc_add_language , 0},
{"load_languages" , gsc_load_languages , 0},
{"get_language_item" , gsc_get_language_item , 0},
{"themetext" , gsc_themetext , 0},
{"G_FindConfigstringIndex" , gsc_G_FindConfigstringIndex , 0},
{"G_FindConfigstringIndexOriginal", gsc_G_FindConfigstringIndexOriginal , 0},
{"call_function_raw" , gsc_call_function_raw , 0},
{"dlopen" , gsc_dlopen , 0},
{"dlsym" , gsc_dlsym , 0},
{"dlclose" , gsc_dlclose , 0},
{"resetignoredweapons" , gsc_utils_resetignoredweapons , 0},
{"ignoreweapon" , gsc_utils_ignoreweapon , 0},
{"setdefaultweapon" , gsc_utils_setdefaultweapon , 0},
{"getweaponmaxammo" , gsc_utils_getweaponmaxammo , 0},
{"getweapondamage" , gsc_utils_getweapondamage , 0},
{"setweapondamage" , gsc_utils_setweapondamage , 0},
{"getweaponmeleedamage" , gsc_utils_getweaponmeleedamage , 0},
{"setweaponmeleedamage" , gsc_utils_setweaponmeleedamage , 0},
{"getweaponfiretime" , gsc_utils_getweaponfiretime , 0},
{"setweaponfiretime" , gsc_utils_setweaponfiretime , 0},
{"getweaponmeleetime" , gsc_utils_getweaponmeleetime , 0},
{"setweaponmeleetime" , gsc_utils_setweaponmeleetime , 0},
{"getweaponreloadtime" , gsc_utils_getweaponreloadtime , 0},
{"setweaponreloadtime" , gsc_utils_setweaponreloadtime , 0},
{"getweaponreloademptytime" , gsc_utils_getweaponreloademptytime , 0},
{"setweaponreloademptytime" , gsc_utils_setweaponreloademptytime , 0},
{"getweaponhitlocmultiplier" , gsc_utils_getweaponhitlocmultiplier , 0},
{"setweaponhitlocmultiplier" , gsc_utils_setweaponhitlocmultiplier , 0},
{"getloadedweapons" , gsc_utils_getloadedweapons , 0},
#endif
#if COMPILE_TCC == 1
{"tcc_new" , gsc_tcc_new , 0},
{"tcc_add_include_path", gsc_tcc_add_include_path, 0},
{"tcc_add_file" , gsc_tcc_add_file , 0},
{"tcc_run" , gsc_tcc_run , 0},
{"tcc_delete" , gsc_tcc_delete , 0},
#endif
{"sqrt" , gsc_math_sqrt, 0},
{"sqrtInv", gsc_math_sqrtInv, 0},
{NULL, NULL, 0}
};
Scr_FunctionCall Scr_GetCustomFunction(const char **fname, int *fdev) {
//printf("Scr_GetCustomFunction: fdev=%d fname=%s\n", *fdev, *fname);
Scr_FunctionCall m = Scr_GetFunction(fname, fdev);
if (m)
return m;
for (int i=0; scriptFunctions[i].name; i++) {
if (strcasecmp(*fname, scriptFunctions[i].name))
continue;
Scr_Function func = scriptFunctions[i];
*fname = func.name;
*fdev = func.developer;
return func.call;
}
return NULL;
}
void gsc_player_printf(int id) {
printf("id: %.8x\n", id);
}
Scr_Method scriptMethods[] = {
{"printf", gsc_player_printf, 0}, // rather use sprintf() to re-use iprintlnbold() etc.?
#if COMPILE_PLAYER == 1
{"getStance" , gsc_player_stance_get , 0},
{"setVelocity" , gsc_player_velocity_set , 0},
{"addVelocity" , gsc_player_velocity_add , 0},
{"getVelocity" , gsc_player_velocity_get , 0},
{ "aimButtonPressed", gsc_player_button_ads , 0},
{ "leftButtonPressed", gsc_player_button_left , 0},
{ "rightButtonPressed", gsc_player_button_right , 0},
{ "forwardButtonPressed", gsc_player_button_forward , 0},
{ "backButtonPressed", gsc_player_button_back , 0},
{ "leanleftButtonPressed", gsc_player_button_leanleft , 0},
{"leanrightButtonPressed", gsc_player_button_leanright , 0},
{ "jumpButtonPressed", gsc_player_button_jump , 0},
{"getIP" , gsc_player_getip , 0},
{"getPing" , gsc_player_getping , 0},
{"getSpectatorClient" , gsc_player_spectatorclient_get, 0},
{"ClientCommand" , gsc_player_ClientCommand , 0},
{"getLastConnectTime" , gsc_player_getLastConnectTime , 0},
{"getLastMSG" , gsc_player_getLastMSG , 0},
{"getAddressType" , gsc_player_addresstype , 0},
{"getClientState" , gsc_player_getclientstate , 0},
{"renameClient" , gsc_player_renameclient , 0},
{"setAlive" , gsc_entity_setalive , 0},
{"setBounds" , gsc_entity_setbounds , 0},
{"get_userinfo" , gsc_get_userinfo , 0},
{"set_userinfo" , gsc_set_userinfo , 0},
{"printOutOfBand" , gsc_player_outofbandprint , 0},
{"connectionlessPacket" , gsc_player_connectionlesspacket, 0},
{"clientuserinfochanged" , gsc_player_clientuserinfochanged, 0},
{"resetNextReliableTime" , gsc_player_resetNextReliableTime, 0},
{"setg_speed" , gsc_player_setg_speed , 0},
{"setg_gravity" , gsc_player_setg_gravity , 0},
{"setweaponfiremeleedelay", gsc_player_setweaponfiremeleedelay, 0},
#if COD_VERSION < COD4_1_7
{"setmovespeedscale" , gsc_player_setmovespeedscale , 0},
{"ismantling" , gsc_player_ismantling , 0},
{"isonladder" , gsc_player_isonladder , 0},
#endif
#endif
{NULL, NULL, 0}
};
Scr_MethodCall Scr_GetCustomMethod(const char **fname, int *fdev) {
//printf("Scr_GetCustomMethod: fdev=%d fname=%s\n", *fdev, *fname);
Scr_MethodCall m = Scr_GetMethod(fname, fdev);
if (m)
return m;
for (int i=0; scriptMethods[i].name; i++) {
if (strcasecmp(*fname, scriptMethods[i].name))
continue;
Scr_Method func = scriptMethods[i];
*fname = func.name;
*fdev = func.developer;
return func.call;
}
return NULL;
}
/*
In CoD2 this address can be found in every get-param-function
In CoD1 its a bit harder, search for: "cannot cast %s to int" and go the function upwards:
the stack-address is in a context like: dword_830AE88 - 8 * a1
*/
int getStack()
{
# if COD_VERSION == COD2_1_0
return 0x083D7610;
#elif COD_VERSION == COD2_1_2
return 0x083D7A10; // diff to 1.3: 1080
#elif COD_VERSION == COD2_1_3
return 0x083D8A90;
#elif COD_VERSION == COD1_1_5
return 0x0830AE88;
#elif COD_VERSION == COD4_1_7
return 0x08c055b0;
#elif COD_VERSION == COD4_1_7_L
return 0x08C06330;
#else
#warning int getStack() return NULL;
return (int)NULL;
#endif
}
int stackGetParamType(int param)
{
aStackElement *scriptStack = *(aStackElement**)getStack();
aStackElement *arg = scriptStack - param;
return arg->type;
}
int stackGetParams(char *params, ...)
{
va_list args;
va_start(args, params);
int errors = 0;
int len = strlen(params);
int i;
for (i=0; i<len; i++)
{
switch (params[i])
{
case ' ': // ignore param (e.g. to ignore the function-id from closer()-wrapper)
break;
case 'i': {
int *tmp = va_arg(args, int *);
if ( ! stackGetParamInt(i, tmp)) {
printf("Param %d needs to be an int, %s=%d given! NumParams=%d\n", i, ">make function for this<", stackGetParamType(i), stackGetNumberOfParams());
errors++;
}
break;
}
case 'v': {
float *tmp = va_arg(args, float *);
if ( ! stackGetParamVector(i, tmp)) {
printf("Param %d needs to be a vector, %s=%d given! NumParams=%d\n", i, ">make function for this<", stackGetParamType(i), stackGetNumberOfParams());
errors++;
}
break;
}
case 'f': {
float *tmp = va_arg(args, float *);
if ( ! stackGetParamFloat(i, tmp)) {
printf("Param %d needs to be a float, %s=%d given! NumParams=%d\n", i, ">make function for this<", stackGetParamType(i), stackGetNumberOfParams());
errors++;
}
break;
}
case 's': {
char **tmp = va_arg(args, char **);
if ( ! stackGetParamString(i, tmp)) {
printf("Param %d needs to be a string, %s=%d given! NumParams=%d\n", i, ">make function for this<", stackGetParamType(i), stackGetNumberOfParams());
errors++;
}
break;
}
default:
errors++;
printf("[WARNING] stackGetParams: errors=%d Identifier '%c' is not implemented!\n", errors, params[i]);
}
}
va_end(args);
return errors == 0; // success if no errors
}
// function can be found in same context as getStack()
int getNumberOfParams() // as in stackNew()
{
# if COD_VERSION == COD2_1_0
return 0x083D761C;
#elif COD_VERSION == COD2_1_2
return 0x083D7A1C; // diff to 1.3: 1080
#elif COD_VERSION == COD2_1_3
return 0x083D8A9C;
#elif COD_VERSION == COD1_1_5
return 0x0830AE84;
#elif COD_VERSION == COD4_1_7
return 0x08c055bc;
#elif COD_VERSION == COD4_1_7_L
return 0x08C0633C;
#else
#warning int getNumberOfParams() return NULL;
return (int)NULL;
#endif
}
// todo: check if the parameter really exists (number_of_params)
int stackGetParamInt(int param, int *value)
{
//printf("stackGetParamInt() start...");
aStackElement *scriptStack = *(aStackElement**)getStack();
aStackElement *arg = scriptStack - param;
if (arg->type != STACK_INT)
return 0;
*value = (int)arg->offsetData;
//printf("... end\n");
return 1;
}
/*
CoD2: just look in getent() e.g.
int __cdecl sub_8078DFC(int a1)
{
return stringtable_8205E80 + 8 * a1;
}
kinda hard to find this for CoD1, because i cant trace the get-param-functions...
but search for: "parameter %d does not exist"
- then make xrefs-to to it
- all those functions shall have ONE parent-function
- go into that parent-function and now just click through till it looks like str-function
*/
int stackGetParamString(int param, char **value) // as in the sub-functions in getentarray (hard one, use the graph to find it)
{
aStackElement *scriptStack = *(aStackElement**)getStack();
aStackElement *arg = scriptStack - param;
if (arg->type != STACK_STRING)
return 0;
//*value = "bla";
//printf("offsetData=%.8x ", arg->offsetData);
// lnxded 1.3: get_string_stack_element_by_id_sub_8078ec8(int id)
//printf("datatable + 8*%d = %.8x \"%s\"\n", arg->offsetData, *(int *)0x08206F00 + 8*(int)arg->offsetData, *(int *)0x08206F00 + 8*(int)arg->offsetData + 4);
# if COD_VERSION == COD2_1_0
*value = (char *)(*(int *)0x08283E80 + 8*(int)arg->offsetData + 4);
#elif COD_VERSION == COD2_1_2
*value = (char *)(*(int *)0x08205E80 + 8*(int)arg->offsetData + 4);
#elif COD_VERSION == COD2_1_3
*value = (char *)(*(int *)0x08206F00 + 8*(int)arg->offsetData + 4);
#elif COD_VERSION == COD1_1_5
*value = (char *)(*(int *)0x081F6940 + 8*(int)arg->offsetData + 4);
#elif COD_VERSION == COD4_1_7
*value = (char *)(*(int *)0x0897ca00 + 12 * (int)arg->offsetData + 4);
#elif COD_VERSION == COD4_1_7_L
*value = (char *)(*(int *)0x0897D780 + 12 * (int)arg->offsetData + 4);
#else
#warning stackGetParamString(int param, char **value) *value = (char *)(*(int *)NULL + 8*(int)arg->offsetData + 4);
*value = (char *)(*(int *)NULL + 8*(int)arg->offsetData + 4);
#endif
return 1;
}
int stackGetParamVector(int param, float value[3])
{
aStackElement *scriptStack = *(aStackElement**)getStack();
aStackElement *arg = scriptStack - param;
if (arg->type != STACK_VECTOR)
return 0;
value[0] = *(float *)((int)(arg->offsetData) + 0);
value[1] = *(float *)((int)(arg->offsetData) + 4);
value[2] = *(float *)((int)(arg->offsetData) + 8);
return 1;
}
int stackGetParamFloat(int param, float *value)
{
aStackElement *scriptStack = *(aStackElement**)getStack();
aStackElement *arg = scriptStack - param;
if (arg->type == STACK_INT)
{
int asInteger;
int ret = stackGetParamInt(param, &asInteger);
if (!ret)
return 0;
*value = (float) asInteger;
return 1;
}
// *value = (float)arg->offsetData;
// gcc: error: pointer value used where a floating point value was expected
// so i use the tmp for casting
float tmp;
if (arg->type != STACK_FLOAT)
return 0;
//swapEndian(&arg->offsetData); // DOESEN WORK EVEN WITH SWAP
//*value = (float)(int)arg->offsetData; // DOESNT WORK
// jeah gcc, you fucked me off!
memcpy(&tmp, &arg->offsetData, 4); // cast to float xD
*value = tmp;
return 1;
}
int stackGetNumberOfParams()
{
int numberOfParams = *(int *)getNumberOfParams();
return numberOfParams;
}
int cdecl_injected_closer_stack_debug()
{
int i;
int numberOfParams = *(int *)0x83D8A9C;
aStackElement *scriptStack = *(aStackElement**)0x83D8A90;
printf("numberOfParams=%d\n", numberOfParams);
for (i=0; i<numberOfParams; i++)
{
aStackElement *stackPtr = scriptStack - i;
printf("i=%d offsetData=%p type=%d\n", i, stackPtr->offsetData, stackPtr->type);
switch (stackPtr->type)
{
case STACK_OBJECT:
{
printf("type=object with: ");
int object_table = 0x8297500; // int16
int offsetData = *(int *)(object_table + 2*((int)stackPtr->offsetData * 8) + 0);
int type = *(int *)(object_table + 2*((int)stackPtr->offsetData * 8) + 4) % 31;
printf("offsetData:%.8x type:%.8x\n", offsetData, type);
break;
}
}
}
return 1;
}
unsigned short get_var_by_idx(unsigned short index) {
#if COD_VERSION == COD2_1_0
unsigned short *words = (unsigned short*)0x0815AB82;
#elif COD_VERSION == COD2_1_2
unsigned short *words = (unsigned short*)0x0817C902;
#elif COD_VERSION == COD2_1_3
unsigned short *words = (unsigned short*)0x0817D922;
#else
unsigned short *words = (unsigned short*)0xdeadbeef;
#endif
return words[6 * index];
}
//thanks to riicchhaarrd/php
unsigned short Scr_GetArray(int index) {
if(index >= stackGetNumberOfParams()) {
printf("scriptengine> Scr_GetArray: one parameter is required\n");
return 0;
}
int stack = getStack();
int base = *(int*)(stack - 8 * index); //in cod1 its without dereferencing the ptr
int vartype = *(int*)(base + 4);
if(vartype == STACK_OBJECT) //VT_OBJECT
return *(unsigned short*)base;
printf("scriptengine> Scr_GetArray: the parameter must be an array\n");
return 0;
}
void Scr_GetArrayKeys() {
unsigned short arrIndex = Scr_GetArray(0);
stackPushArray();
if(arrIndex == 0)
return; // we didn't find a valid array
unsigned short i, var;
for(i = GetNextVariable(arrIndex); i != 0;) {
//printf("%d: %s = %s\n", i, SL_ConvertToString(GetVariableName(i)), SL_ConvertToString(var));
stackPushString(SL_ConvertToString(GetVariableName(i)));
stackPushArrayLast();
i = GetNextVariable(i);
var = get_var_by_idx(i);
}
}
/* THE BEGINNING of generalizing the push-value-functions! */
// pushing a new stack-element on stack
// available through getStack()
// 11.03.2013, Sido|Meine Jordans Instrumental, Generalisation is unstable!
/* search for "Internal script stack overflow", thats stackNew() */
/* can also be found in the next stackPush-functions */
int stackNew()
{
int (*signature)();
#if COD_VERSION == COD2_1_0
*((int *)(&signature)) = 0x080837B0;
#elif COD_VERSION == COD2_1_2
*((int *)(&signature)) = 0x08083D2C;
#elif COD_VERSION == COD2_1_3
*((int *)(&signature)) = 0x08083DF8;
#elif COD_VERSION == COD1_1_5
*((int *)(&signature)) = 0x080AE084;
#elif COD_VERSION == COD4_1_7
*((int *)(&signature)) = 0x0815EC48;
#elif COD_VERSION == COD4_1_7_L
*((int *)(&signature)) = 0x0815EC68;
#else
#warning int stackNew() *((int *)(&signature)) = NULL;
*((int *)(&signature)) = (int)NULL;
#endif
return signature();
}
int stackPushUndefined()
{
aStackElement *scriptStack;
scriptStack = *(aStackElement**)getStack();
#if DEBUG_GSC
printf("stackPushUndefined(): type=%d value=%.8x\n", scriptStack->type, scriptStack->offsetData);
#endif
int ret = stackNew();
scriptStack = *(aStackElement**)getStack();
#if DEBUG_GSC
printf("stackPushUndefined(): type=%d value=%.8x\n", scriptStack->type, scriptStack->offsetData);
#endif
//aStackElement *scriptStack = *(aStackElement**)getStack();
//aStackElement *scriptStack = *(aStackElement**)ret;
scriptStack->type = STACK_UNDEFINED;
scriptStack->offsetData = NULL; // never tested anything else for UNDEFINED
//return (int) *(aStackElement**) getStack(); // dunno...
return 123; // dunno... works also lol. so no need to return some specific stackelement
}
/*
HOWTO: how to find the addresses of a binary?
1) find the function-string in winhex
2) go to file offset in IDA -> then copy the real address
3) then search for the SWAPPED value in winhex again
4) go for it in ida
5) convert the crazy numbers to str/func-pair
6) go into function and get the offset of the internal function
*/
int stackReturnInt(int ret) /*obsolete - not guaranteed to be available later*/
{
return stackPushInt(ret);
/*
int (*signature)(int);
*((int *)(&signature)) = 0x08085164;
return signature(ret);
*/
}
int stackPushInt(int ret) // as in isalive
{
#if 1
int (*signature)(int);
# if COD_VERSION == COD2_1_0
*((int *)(&signature)) = 0x08084B1C;
#elif COD_VERSION == COD2_1_2
*((int *)(&signature)) = 0x08085098; // difference to 1.3: CC
#elif COD_VERSION == COD2_1_3
*((int *)(&signature)) = 0x08085164;
#elif COD_VERSION == COD4_1_7
*((int *)(&signature)) = 0x0815EFFA;
#elif COD_VERSION == COD4_1_7_L
*((int *)(&signature)) = 0x0815F01A;
#else
#warning int stackPushInt(int ret)
*((int *)(&signature)) = (int)NULL;
#endif
return signature(ret);
#endif
// this shit dont work! seg fault
#if 0
aStackElement *scriptStack;
stackNew();
scriptStack = *(aStackElement**)getStack();
scriptStack->type = STACK_INT;
scriptStack->offsetData = (void *)ret;
printf("still working int??\n");
return ret;
return -1234; // crap, just testin if i can return void
#endif
}
int stackReturnVector(float *ret) // obsolete
{
return stackPushVector(ret);
}
/*
for CoD1:
search MT_AllocIndex
go 2 functions up or so and compare structure with CoD2
for other CoD's:
just go into getOrigin() or so and look
*/
int stackPushVector(float *ret) // as in vectornormalize
{
int (*signature)(float *);
# if COD_VERSION == COD2_1_0
*((int *)(&signature)) = 0x08084CBE;
#elif COD_VERSION == COD2_1_2
*((int *)(&signature)) = 0x0808523A; // difference to 1.3: CC
#elif COD_VERSION == COD2_1_3
*((int *)(&signature)) = 0x08085306;
#elif COD_VERSION == COD1_1_5
printf("*((int *)(&signature)) = 0x080AF464;\n");
*((int *)(&signature)) = 0x080AF464;
#elif COD_VERSION == COD4_1_7
*((int *)(&signature)) = 0x0815EDF2;
#elif COD_VERSION == COD4_1_7_L
*((int *)(&signature)) = 0x0815EE12;
#else
#warning int stackPushVector(float *ret) *((int *)(&signature)) = NULL;
*((int *)(&signature)) = (int)NULL;
#endif
return signature(ret);
#if 0
int (*alloc_vector)(float *);
*((int *)(&alloc_vector)) = 0x080A5D14;
aStackElement *scriptStack;
stackNew();
scriptStack = *(aStackElement**)getStack();
scriptStack->type = STACK_VECTOR;
printf("1\n");
scriptStack->offsetData = (void *)alloc_vector(ret);
printf("2\n");
#endif
}
int stackPushFloat(float ret) // as in distance
{
int (*signature)(float);
# if COD_VERSION == COD2_1_0
*((int *)(&signature)) = 0x08084B40;
#elif COD_VERSION == COD2_1_2
*((int *)(&signature)) = 0x080850BC; // difference to 1.3: CC
#elif COD_VERSION == COD2_1_3
*((int *)(&signature)) = 0x08085188;
#elif COD_VERSION == COD4_1_7
*((int *)(&signature)) = 0x0815EF7A;
#elif COD_VERSION == COD4_1_7_L
*((int *)(&signature)) = 0x0815EF9A;
#else
#warning int stackPushFloat(float ret) *((int *)(&signature)) = NULL;
*((int *)(&signature)) = (int)NULL;
#endif
return signature(ret);
}
int stackPushString(char *toPush) // as in getcvar()
{
int (*signature)(char *);
# if COD_VERSION == COD2_1_0
*((int *)(&signature)) = 0x08084C1A;
#elif COD_VERSION == COD2_1_2
*((int *)(&signature)) = 0x08085196; // difference to 1.3: CC
#elif COD_VERSION == COD2_1_3
*((int *)(&signature)) = 0x08085262;
#elif COD_VERSION == COD4_1_7
*((int *)(&signature)) = 0x0815EC48;
#elif COD_VERSION == COD4_1_7_L
*((int *)(&signature)) = 0x0815EC68;
#else
#warning int stackPushString(char *toPush) *((int *)(&signature)) = NULL;
*((int *)(&signature)) = (int)NULL;
#endif
return signature(toPush);
}
int stackPushEntity(int arg) // as in getent() // todo: find out how to represent an entity
{
int (*signature)(int);
# if COD_VERSION == COD2_1_0
*((int *)(&signature)) = 0x08118CC0;
#elif COD_VERSION == COD2_1_2
*((int *)(&signature)) = 0x0811AFF4; // difference OTHER then CC
#elif COD_VERSION == COD2_1_3
*((int *)(&signature)) = 0x08117F50;
#elif COD_VERSION == COD4_1_7 || COD_VERSION == COD4_1_7_L
*((int *)(&signature)) = 0x080C7770;
#else
#warning int stackPushEntity(int arg) *((int *)(&signature)) = NULL;
*((int *)(&signature)) = (int)NULL;
#endif
return signature(arg);
}
int stackCallScriptFunction(int self, int scriptFunction, int numberOfArgs)
{
int (*signature)(int, int, int);
*((int *)(&signature)) = 0x0811B284;
return signature(self, scriptFunction, numberOfArgs);
}
int stackCallbackPlayerDamage = 0x0884D718;
// as in bullettrace
int stackPushArray() {
int (*signature)();
# if COD_VERSION == COD2_1_0
*((int *)(&signature)) = 0x08084CF0;
#elif COD_VERSION == COD2_1_2
*((int *)(&signature)) = 0x0808526C;
#elif COD_VERSION == COD2_1_3
*((int *)(&signature)) = 0x08085338;
#elif COD_VERSION == COD4_1_7
*((int *)(&signature)) = 0x0815ED6A;
#elif COD_VERSION == COD4_1_7_L
*((int *)(&signature)) = 0x0815ED8A;
#else
#warning int stackPushArray() *((int *)(&signature)) = NULL;
*((int *)(&signature)) = (int)NULL;
#endif
return signature();
}
int stackSetKeyInArray(int precachedStringOffset) // TODOOOOOO
{
int (*signature)(int);
*((int *)(&signature)) = 0x080853B6;
return signature(precachedStringOffset);
}
int stackPushArrayLast() { // as in getentarray
int (*signature)();
#if COD_VERSION == COD2_1_0
*((int *)(&signature)) = 0x08084D1C;
#elif COD_VERSION == COD2_1_2
*((int *)(&signature)) = 0x08085298;
#elif COD_VERSION == COD2_1_3
*((int *)(&signature)) = 0x08085364;
#elif COD_VERSION == COD4_1_7
*((int *)(&signature)) = 0x0815D5A0;
#elif COD_VERSION == COD4_1_7_L
*((int *)(&signature)) = 0x0815D5C0;
#else
#warning int stackPushArrayLast() *((int *)(&signature)) = NULL;
*((int *)(&signature)) = (int)NULL;
#endif
return signature();
}
int sub_807BCA8()
{
int16_t (*sub_807B9F8)();
*(int *)(&sub_807B9F8) = 0x0807B9F8;
int *startStack = (int*)0x08297500;
int16_t var;
int *ptr;
var = sub_807B9F8(); // make new variable
printf("var=%d\n", var);
ptr = &startStack[4*var];
startStack[4 * var + 2] = 96;