/
script.c
1966 lines (1593 loc) · 51 KB
/
script.c
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
/*
This file is part of CoDExtended.
CoDExtended is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CoDExtended is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CoDExtended. If not, see <http://www.gnu.org/licenses/>.
*/
#include "script.h"
#include "steamwrapper.h"
/*
:: Scr_GetFunction/GetMethod ::
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
*/
void scr_field_awesome(int a1) {
int argc = Scr_GetNumParam();
if(argc == 0) {
Scr_AddString("Laughing With");
//works - Richard
} else {
char *s = Scr_GetString(0);
printf("char *s = %s\n", s);
/*
throw error()
crash here yet to figure out
*/
}
}
static scr_memberfield scr_entity_memberfields[] = {
//easy to add your own new fields which do not exist yet on entities
//fieldname, unused, unused, set func ptr, get func ptr
{"php", 0, 0, scr_field_awesome, scr_field_awesome},
{"guid", 0, 0, scr_field_awesome, scr_field_awesome},
{"number", 0, 0, scr_field_awesome, scr_field_awesome},
{"index", 0, 0, scr_field_awesome, scr_field_awesome},
{"add_your_own_field", 0, 0, scr_field_awesome, scr_field_awesome},
{NULL,NULL,NULL,NULL}
};
/*
static scr_memberfield scr_client_memberfields[] = {
{"name", 0, 0, 0, 0},
{"sessionteam", 0x0, 0x3, 0x316b0, 0x315ec},
{"sessionstate", 0x0, 0x3, 0x31580, 0x314dc},
{"maxhealth", 0x2150, 0x0, 0x31470, 0xd6a41ee8},
{"handicap", 0x214c, 0x0, 0x316d8, 0xd6a41ee8},
{"score", 0x20e0, 0x0, 0x313fc, 0xd6a41ee8},
{"deaths", 0x20e4, 0x0, 0xd6a41ee8, 0xd6a41ee8},
{"statusicon", 0x0, 0x3, 0x31398, 0x3136c},
{"headicon", 0x0, 0x3, 0x31314, 0x312d4},
{"headiconteam", 0x0, 0x3, 0x31264, 0x311ac},
{"spectatorclient", 0x20d4, 0x0, 0x313d4, 0xd6a41ee8},
{"archivetime", 0x20dc, 0x1, 0x31130, 0x310ec},
{"pers", 0x20e8, 0x7, 0x316d8, 0xd6a41ee8},
{"php", 0, 1, scr_field_awesome, scr_field_awesome},
{NULL,NULL,NULL,NULL}
};
*/
void GScr_SendServerCommand(int a1);
void PlayerCmd_SendConnectionlessPacket(int a1);
void GScr_killserver(int);
void GScr_typeof(int);
void Scr_fuckPrecacheString(int a1);
void GScr_getSite(int a1) {
#if 0
char* url = Scr_GetString(0);
std::string result = get_site_result(url);
Scr_AddString((char*)result.c_str());
#endif
}
long long current_timestamp();
void GScr_seconds(int a1) {
Scr_AddInt(time(NULL));
}
#ifdef xDEBUG
void MakeTrail(int self) {
vec3_t start, end;
Scr_GetVector(0, start);
Scr_GetVector(1, end);
gentity_t *tent = G_TempEntity(start, EV_RAILTRAIL);
VectorCopy(end, tent->s.origin2);
tent->s.otherEntityNum2 = 0;
}
#endif
#if 0
gentity_t *FindEntity( const char *key, const char *value ) {
gentity_t *ent = NULL;
for(int i = 0; i < level.num_entities; i++) {
ent = &g_entities[i];
if(!ent->inuse) {
//dostuff
}
}
}
void ___test_debug_dont_really_blah(int a) {
short classname = Scr_GetOffset(*(short *)&g_scr_data[420], "classname");
if(classname >= 0) {
}
}
#endif
void GScr_xtnded_anim(int a1) {
/*int index = 0;
int b = *(int*)0x82F5948;
int v1 = *(int *)(b - 8 * index);*/
//Scr_GetAnim((int)&v1, 0, 0);
const char* str_anim = Scr_GetString(0);
int anim_index = oBG_AnimationIndexForString(str_anim);
//printf("anim = %d\n", anim_index);
if(anim_index==-1)
Scr_AddInt(0);
else
Scr_AddInt(anim_index);
}
#ifdef xDEBUG
void scr_newzombie(int);
void scr_spawnbrushmodel( int a1 ) ;
#endif
void Math_Scr_sqrt(int a) {
float f = Scr_GetFloat(0);
Scr_AddFloat( sqrt(f) );
}
#ifdef xDEBUG
void trace_For_me(int);
#endif
void gscr_malloc(int a) {
int size = Scr_GetInt(0);
void *p = malloc(size);
if(!p) {
Scr_AddUndefined();
return;
}
Scr_AddInt((int)p);
}
void gscr_free(int a) {
void *p = (void*)Scr_GetInt(0);
if(!p)
return;
free(p);
}
void gscr_memset(int a) {
void *s = (void*)Scr_GetInt(0);
int c = Scr_GetInt(1);
int n = Scr_GetInt(2);
memset(s, c, n);
}
void gscr_memcpy(int a) {
void *s1 = (void*)Scr_GetInt(0);
void *s2 = (void*)Scr_GetInt(1);
int n = Scr_GetInt(2);
memcpy(s1, s2, n);
}
void GScr_system(int a) {
#ifdef uFEATUREUNSAFE
char* command = Scr_GetString(0);
Scr_AddBool(system(command));
#else
Scr_AddBool(0); // Scr_AddString("Unsafe features are disabled"); ?
#endif
}
void GScr_getUnixTime() {
Scr_AddInt(time(NULL));
}
void Scr_GetArrayKeys(int a);
void Scr_PassArray(int);
void GScr_SetTempVec(unsigned n);
SCRIPTFUNCTION scriptFunctions[] = {
// name, function, developer
{"settempvec", GScr_SetTempVec, 0},
// MySQL
#ifdef uMYSQL
{"mysql_init", GScr_mysql_init, 0},
{"mysql_close", GScr_mysql_close, 0},
{"mysql_affected_rows", GScr_mysql_affected_rows, 0},
{"mysql_errno", GScr_mysql_errno, 0},
{"mysql_error", GScr_mysql_error, 0},
{"mysql_fetch_field", GScr_mysql_fetch_field, 0},
{"mysql_fetch_row", GScr_mysql_fetch_row, 0},
{"mysql_field_seek", GScr_mysql_field_seek, 0},
{"mysql_free_result", GScr_mysql_free_result, 0},
{"mysql_num_fields", GScr_mysql_num_fields, 0},
{"mysql_num_rows", GScr_mysql_num_rows, 0},
{"mysql_query", GScr_mysql_query, 0},
{"mysql_real_connect", GScr_mysql_real_connect, 0},
{"mysql_real_escape_string", GScr_mysql_real_escape_string, 0},
{"mysql_store_result", GScr_mysql_store_result, 0},
{"mysql_get_connection", GScr_mysql_get_connection, 0},
#endif
// Math
{"sqrt", Math_Scr_sqrt, 0},
// String
{"strtok", Scr_StrTok, 0},
{"issubstr", Scr_IsSubStr, 0},
{"tolower", Scr_ToLower, 0},
{"toupper", Scr_ToUpper, 0},
{"ucfirst", Scr_ucfirst, 0},
{"replace", Scr_replace, 0},
{"trim", Scr_trim, 0},
// Util
#ifdef xDEBUG
{"maketrail", MakeTrail, 0},
#endif
{"convert_string", Scr_convertToIString, 0},
{"xprecachestring", Scr_fuckPrecacheString, 0},
#ifdef xDEBUG
{"spawn_zombie", scr_newzombie, 0},
{"spawn_brushmodel", scr_spawnbrushmodel, 0},
#endif
{"printconsole", GScr_printconsole, 0},
{"getsite", GScr_getSite, 0},
{"seconds", GScr_seconds, 0},
{"get_animation_index", GScr_xtnded_anim, 0},
{"typeof", GScr_typeof, 0},
{"sendservercommand", GScr_SendServerCommand, 0},
{"trace", GScr_Trace, 0},
#ifdef xDEBUG
{"debugtrace", trace_For_me, 0},
#endif
{"md5", GScr_md5, 0},
{"getarraykeys", Scr_GetArrayKeys, 0},
{"passarray", Scr_PassArray, 0},
{"creturn", GScr_return, 0},
{"cmd_argc", GScr_Cmd_Argc, 0},
{"cmd_argv", GScr_Cmd_Argv, 0},
{"cmd_argvbuffer", GScr_trap_Argv, 0},
{"concatargs", GScr_ConcatArgs, 0},
{"getchat", GScr_getChat, 0},
{"fopen", GScr_fopen, 0},
{"fread", GScr_fread, 0},
{"fclose", GScr_fclose, 0},
{"fwrite", GScr_fwrite, 0},
{"fexists", GScr_fexists, 0},
{"fsize", GScr_fsize, 0},
{"strpos", GScr_strpos, 0},
{"salt_password", GScr_salt_password, 0},
{"getconfigstring", GScr_getconfigstring, 0},
{"configstringindex", GScr_configstringindex, 0},
{"system", GScr_system, 0},
{"getunixtime", GScr_getUnixTime, 0},
{NULL, NULL, 0}
};
void PlayerCmd_IsUsingClient(int a1) {
//Scr_AddInt(xtnded_clients[a1].clientusage);
client_t *cl = getclient(a1);
int clientbuild = atoi( Info_ValueForKey(cl->userinfo, "xtndedbuild") );
if(clientbuild != clientversion) {
Scr_AddBool(false);
return;
}
Scr_AddBool(true);
}
void ScriptEnt_GetPosition(int);
SCRIPTFUNCTION scriptMethods[] = {
//name, function, developer
/*
======
ENTITY
======
*/
{"getposition", ScriptEnt_GetPosition, 0},
{"setbounds", ScriptEnt_SetBounds, 0},
{"setsize", ScriptEnt_SetBounds, 0},
{"settakedamage", ScriptEnt_SetTakeDamage, 0},
{"setmaxs", ScriptEnt_SetMaxs, 0},
{"setmins", ScriptEnt_SetMins, 0},
{"setabsmins", ScriptEnt_SetAbsMin, 0},
{"setabsmaxs", ScriptEnt_SetAbsMax, 0},
{"setlight", ScriptEnt_SetLight, 0},
{"showtoplayer", Ent_ShowToPlayer, 0},
/*
======
PLAYER
======
*/
{"getmuid", PlayerCmd_GetMUID, 0},
{"setvelocity", PlayerCmd_SetVelocity, 0},
{"getvelocity", PlayerCmd_GetVelocity, 0},
{"getplayerangles", PlayerCmd_getPlayerAngles, 0},
{"getspectatorclient", PlayerCmd_getSpectatorClient, 0},
{"getip", PlayerCmd_getip, 0},
{"ispure", PlayerCmd_ispure, 0},
{"sendconnectionlesspacket", PlayerCmd_SendConnectionlessPacket, 0},
{"sendservercommand", PlayerCmd_SendServerCommand, 0},
{"sendgamestate", PlayerCmd_SendGamestate, 0},
{"hasclient", PlayerCmd_IsUsingClient, 0},
{"get_ip", PlayerCmd_getip, 0},
{"getint", PlayerCmd_GetInt, 0},
{"setint", PlayerCmd_SetInt, 0},
{"getbyte", PlayerCmd_GetByte, 0},
{"getfloat", PlayerCmd_GetFloat, 0},
{"setfloat", PlayerCmd_SetFloat, 0},
{"getstance", PlayerCmd_GetStance, 0},
{"setbyte", PlayerCmd_SetByte, 0},
{"getuserinfokey", PlayerCmd_GetUserInfoKey, 0},
{"getuserinfo", PlayerCmd_GetUserInfo, 0},
{"dropclient", PlayerCmd_DropClient, 0},
{"kickbot", PlayerCmd_kickbot, 0},
{"renamebot", PlayerCmd_renamebot, 0},
{"isbot", PlayerCmd_isbot, 0},
{"backbuttonpressed", PlayerCmd_backButtonPressed, 0},
{"forwardbuttonpressed", PlayerCmd_forwardButtonPressed, 0},
{"leftbuttonpressed", PlayerCmd_leftButtonPressed, 0},
{"rightbuttonpressed", PlayerCmd_rightButtonPressed, 0},
{"moveupbuttonpressed", PlayerCmd_moveupButtonPressed, 0},
{"movedownbuttonpressed", PlayerCmd_movedownButtonPressed, 0},
{"aimbuttonpressed", PlayerCmd_aimButtonPressed, 0},
{"reloadbuttonpressed", PlayerCmd_reloadButtonPressed, 0},
{"leanleftbuttonpressed", PlayerCmd_leanLeftButtonPressed, 0},
{"leanrightbuttonpressed", PlayerCmd_leanRightButtonPressed, 0},
{"hasperk", PlayerCmd_HasPerk, 0},
{"setperk", PlayerCmd_SetPerk, 0},
{"unsetperk", PlayerCmd_UnsetPerk, 0},
{"getping", PlayerCmd_GetPing, 0},
{"setmaxspeed", PlayerCmd_SetMaxSpeed, 0},
{"setmovespeedscale", PlayerCmd_SetMoveSpeedScale, 0},
{"freeze_controls", PlayerCmd_FreezeControls, 0},
{NULL, NULL, 0}
};
game_script_data* g_scr_data;
scr_const_t* scr_const;
xscr_const_t xscr_const;
unsigned char* g_clients;
unsigned char* hudelems;
int callbackTest;
int callbackPlayerCommand;
int callbackRemoteCommand;
int callbackFireGrenade;
bool scr_return = 0;
Scr_LoadScr_t Scr_LoadScript;
Scr_GetFunctionHandle_t Scr_GetFunctionHandle;
#ifdef _WIN32
Scr_ExecThread_t Scr_ExecThread = (Scr_ExecThread_t)0x481E10;
Scr_ExecEntThread_t Scr_ExecEntThread = (Scr_ExecEntThread_t)0x481EC0;
Scr_FreeThread_t Scr_FreeThread = (Scr_FreeThread_t)0x482070;
SL_ConvertToString_t SL_ConvertToString = (SL_ConvertToString_t)0x474EE0;
#else
Scr_ExecThread_t Scr_ExecThread = (Scr_ExecThread_t)0x80A95EC;
Scr_ExecEntThread_t Scr_ExecEntThread = (Scr_ExecEntThread_t)0x80a9674; //actually is Scr_ExecEntThreadNum
Scr_FreeThread_t Scr_FreeThread = (Scr_FreeThread_t)0x80A97D4; //RemoveRefToObject__FUs
SL_ConvertToString_t SL_ConvertToString;
#endif
SL_GetString_t SL_GetString;
/*
Can't use Scr_GetMethod/Scr_GetFunction because those names already exists and aren't mangled.
*/
Scr_GetFunction_t Scr_GetFunction;
Scr_GetMethod_t Scr_GetMethod;
#ifdef _WIN32
Scr_MakeArray_t Scr_MakeArray = (Scr_MakeArray_t)0x483330;
Scr_AddArray_t Scr_AddArray = (Scr_AddArray_t)0x483380;
Scr_Error_t Scr_Error = (Scr_Error_t)0x483470;
#else
Scr_MakeArray_t Scr_MakeArray;
Scr_AddArray_t Scr_AddArray;
Scr_Error_t Scr_Error;
#endif
Scr_AddArrayStringIndexed_t Scr_AddArrayStringIndexed;
#ifdef _WIN32
Scr_GetNumParam_t Scr_GetNumParam = (Scr_GetNumParam_t)0x483060;
Scr_GetPointerType_t Scr_GetPointerType = (Scr_GetPointerType_t)0x482FC0;
Scr_GetType_t Scr_GetType = (Scr_GetType_t)0x482F50;
Scr_AddInt_t Scr_AddInt = (Scr_AddInt_t)0x483090;
Scr_AddBool_t Scr_AddBool = (Scr_AddBool_t)0x483070;
Scr_AddString_t Scr_AddString = (Scr_AddString_t)0x483210;
Scr_AddVector_t Scr_AddVector = (Scr_AddVector_t)0x4832E0;
Scr_AddUndefined_t Scr_AddUndefined = (Scr_AddUndefined_t)0x4830F0;
Scr_AddEntity_t Scr_AddEntity = (Scr_AddEntity_t)0x483140;
#else
Scr_GetNumParam_t Scr_GetNumParam;
Scr_GetPointerType_t Scr_GetPointerType;
Scr_GetEntity_t Scr_GetEntity;
Scr_GetType_t Scr_GetType;
Scr_AddFloat_t Scr_AddFloat;
Scr_AddInt_t Scr_AddInt;
Scr_AddBool_t Scr_AddBool;
Scr_AddString_t Scr_AddString;
Scr_AddIString_t Scr_AddIString;
Scr_AddVector_t Scr_AddVector;
Scr_AddUndefined_t Scr_AddUndefined;
Scr_AddEntity_t Scr_AddEntity;
#endif
Scr_AllocString_t Scr_AllocString;
Scr_SetString_t Scr_SetString;
Scr_GetConstString_t Scr_GetConstString;
oBG_AnimationIndexForString_t oBG_AnimationIndexForString;
CallSpawnEntity_t CallSpawnEntity;
Scr_GetBool_t Scr_GetBool;
Scr_GetInt_t Scr_GetInt;
Scr_GetAnim_t Scr_GetAnim = (Scr_GetAnim_t)0x80A7FAC;
Scr_GetAnimsIndex_t Scr_GetAnimsIndex;
Scr_GetFloat_t Scr_GetFloat;
Scr_GetVector_t Scr_GetVector;
Scr_GetString_t Scr_GetString;
Scr_GetFunc_t Scr_GetFunc;
Scr_GetOffset_t Scr_GetOffset;
SCRIPTFUNCTIONCALL Scr_GetCustomFunction(const char** fname, int* fdev) {
SCRIPTFUNCTIONCALL m = Scr_GetFunction(fname, fdev);
void (*fc)(int);
*(int*)&fc = (int)m;
if(!m) {
for(unsigned int i = 0; scriptFunctions[i].name; i++) {
if(!strcmp(*fname, scriptFunctions[i].name)) {
SCRIPTFUNCTION func = scriptFunctions[i];
*fname = func.name;
*fdev = func.developer;
return func.call;
}
}
}
return fc;
}
SCRIPTFUNCTIONCALL Scr_GetCustomMethod(const char** fname, int* fdev) {
SCRIPTFUNCTIONCALL m = Scr_GetMethod(fname, fdev);
void (*fc)(int);
*(int*)&fc = (int)m;
if(!m) {
for(unsigned int i = 0; scriptMethods[i].name; i++) {
if(!strcmp(*fname, scriptMethods[i].name)) {
SCRIPTFUNCTION func = scriptMethods[i];
*fname = func.name;
*fdev = func.developer;
return func.call;
}
}
}
return fc;
}
static int load_callback(const char* file, const char* functionname, bool flag) {
if(!Scr_LoadScript(file) && !flag)
Com_Error(ERR_DROP, "Could not find script '%s'.", file);
int v4 = Scr_GetFunctionHandle(file, functionname);
if(!v4 && !flag)
Com_Error(ERR_DROP, "Could not find label '%s'.", functionname, file);
if(!flag || v4)
cprintf(PRINT_GOOD|PRINT_UNDERLINE,"callback[%s] = %x\n", functionname, v4);
return v4;
}
void GScr_typeof(int a1) {
int type = Scr_GetType(0);
Scr_AddString(Scr_GetVariableType(type));
}
char* Scr_GetVariableType(int type) {
switch(type) {
default:
case VT_UNDEFINED: {
return "undefined";
}
break;
case VT_INT: {
return "integer";
}
break;
case VT_FLOAT: {
return "float";
}
break;
case VT_STRING: {
return "string";
}
break;
case VT_VECTOR: {
return "vector";
}
break;
case VT_LOCALIZED_STRING: {
return "localized-string";
}
break;
case VT_OBJECT: {
return "object";
}
break;
case VT_ARRAY: {
return "array";
}
break;
}
}
/*
=============
FUNCTIONS
=============
*/
void GScr_printconsole(int entityIndex) { //if this was a method the index would be the entity's number
const char* txt = Scr_GetString(0);
printf(txt);
}
void GScr_salt_password(int a1) {
char* password = Scr_GetString(0);
char* salt = Scr_GetString(1);
Scr_AddString(get_pass_hash(password, salt));
}
void GScr_getconfigstring(int a1) {
int i = Scr_GetInt(0);
char cs[MAX_INFO_STRING];
SV_GetConfigstring(i, cs, sizeof(cs));
Scr_AddString(cs);
}
void GScr_configstringindex(int a1) {
char* str = Scr_GetString(0);
int min = Scr_GetInt(1);
int max = Scr_GetInt(2);
int i;
char cs[MAX_INFO_STRING];
for(i = 1; i < max; i++) {
SV_GetConfigstring(i, cs, sizeof(cs));
if(!strcasecmp(str, cs)) {
Scr_AddInt(i);
return;
}
}
Scr_AddInt(0);
}
void GScr_strpos(int a1) {
char* haystack = Scr_GetString(0);
char* needle = Scr_GetString(1);
char* p = strstr(haystack, needle);
if(p)
Scr_AddInt(((int)p-(int)haystack));
else
Scr_AddInt(-1);
}
void GScr_SendServerCommand(int a1) {
char* cmd = Scr_GetString(0);
SV_SendServerCommand(NULL, 1, cmd);
}
void PlayerCmd_SendConnectionlessPacket(int a1) {
char *msg = Scr_GetString(0);
for(char *i = msg; *i != '\0'; *i++)
if(*i == '\'')
*i = '"';
client_t *cl = getclient(a1);
if(cl)
NET_OutOfBandPrint(NS_SERVER, cl->netchan.remoteAddress, "%s", msg);
}
void GScr_Trace(int a1) {
trace_t tr;
vec3_t start, end, mins, maxs;
Scr_GetVector(0, start);
Scr_GetVector(1, mins);
Scr_GetVector(2, maxs);
Scr_GetVector(3, end);
int ignore = Scr_GetInt(4);
int mask = Scr_GetInt(5);
int locational = 0;
int staticmodels = 0;
if(Scr_GetNumParam() > 6)
locational = Scr_GetInt(6);
if(Scr_GetNumParam() > 7)
staticmodels = Scr_GetInt(7);
void (*trace)(void*,float*,float*,float*,float*,int ignore,int contentmask,int locational,char *priorityMap,int staticmodels);
*(int*)&trace = 0x80916F4;
trace(&tr,start,mins,maxs,end,-1,mask,locational,NULL,staticmodels);
Scr_MakeArray();
Scr_AddVector(tr.endpos); Scr_AddArrayStringIndexed(scr_const->position);
if((tr.entityNum - 1022) > 1)
Scr_AddInt(tr.entityNum);//Scr_AddEntity(&g_entities[tr.entityNum]); //scr_addentity crashed sometime cba
else
Scr_AddUndefined();
Scr_AddArrayStringIndexed(scr_const->entity);
Scr_AddFloat(tr.fraction); Scr_AddArrayStringIndexed(scr_const->fraction);
Scr_AddVector(tr.normal); Scr_AddArrayStringIndexed(scr_const->normal);
Scr_AddInt(tr.contents); Scr_AddArrayStringIndexed(xscr_const.contents);
Scr_AddInt(tr.surfaceFlags); Scr_AddArrayStringIndexed(scr_const->surfacetype);
Scr_AddInt(tr.textureName); Scr_AddArrayStringIndexed(xscr_const.texturename);
}
void GScr_Cmd_Argc(int a1) {
Scr_AddInt(Cmd_Argc());
}
void GScr_Cmd_Argv(int a1) {
unsigned int idx = Scr_GetInt(0);
if(idx > Cmd_Argc()) {
Scr_AddString("");
return;
}
Scr_AddString(Cmd_Argv(idx));
}
void GScr_trap_Argv(int a1) {
unsigned int idx = Scr_GetInt(0);
if(idx > Cmd_Argc()) {
Scr_AddString("");
return;
}
char buf[MAX_STRING_CHARS];
Cmd_ArgvBuffer(idx, buf, sizeof(buf));
Scr_AddString(buf);
}
void GScr_ConcatArgs(int a1) {
unsigned int idx = Scr_GetInt(0);
if(idx > Cmd_Argc()) {
Scr_AddString("");
return;
}
Scr_AddString(ConcatArgs(idx));
}
void GScr_md5(int a1) {
char* str = Scr_GetString(0);
Scr_AddString(get_md5(str));
}
void GScr_getChat(int a1) {
unsigned int idx = Scr_GetInt(0);
if(idx > Cmd_Argc()) {
Scr_AddString("");
return;
}
char* chat = ConcatArgs(idx);
if(strlen(chat) == 0 || (chat[0] == 0x15 && strlen(chat)==1)) {
Scr_AddString("");
return;
}
if(chat[0] == 0x15)
Scr_AddString(&chat[1]);
else
Scr_AddString(chat);
}
void GScr_return(int a1) {
scr_return = 1;
}
bool Scr_Continue() {
bool ret = scr_return;
scr_return = 0;
return(!ret);
}
/*
=============
FILE FUNCTIONS
=============
*/
void GScr_fopen(int entityIndex) {
char* name = Scr_GetString(0);
char* mode = Scr_GetString(1);
FILE* f = fopen(name, mode);
if(f)
Scr_AddInt((int)(f));
else
Scr_AddInt(-1);
}
void GScr_fexists(int entityIndex) {
char* name = Scr_GetString(0);
FILE* f;
if((f = fopen(name, "r"))) {
fclose(f);
Scr_AddBool(true);
} else {
Scr_AddBool(false);
}
}
void GScr_fread(int entityIndex) {
char *buf = NULL;
size_t size = 0;
int len = Scr_GetInt(0);
FILE *f = (FILE*)(Scr_GetInt(1));
if(f == NULL) {
Scr_AddUndefined();
} else {
fseek(f, 0, SEEK_END);
size = ftell(f);
rewind(f);
buf = (char *) malloc(size);
fread(buf, size, 1, f);
Scr_AddString(buf);
free(buf);
}
}
void GScr_fwrite(int entityIndex) {
char* text = Scr_GetString(0);
FILE *f = (FILE*)(Scr_GetInt(1));
if(f == NULL) {
Scr_AddBool(false);
} else {
Scr_AddBool(true);
fprintf(f, text);
}
}
void GScr_fsize(int entityIndex) {
char* name = Scr_GetString(0);
FILE *f = fopen(name, "r");
size_t len;
if(f) {
fseek(f, 0, SEEK_END);
len = ftell(f);
rewind(f);
Scr_AddInt((int)len);
} else {
Scr_AddInt(-1);
}
}
void GScr_fclose(int entityIndex) {
FILE *f = (FILE*)(Scr_GetInt(0));
if(f) {
Scr_AddBool(true);
fclose(f);
} else {
Scr_AddBool(false);
}
}
void Scr_LoadConsts() {
xscr_const.texturename = Scr_AllocString("texturename", 1);
xscr_const.contents = Scr_AllocString("contents", 1);
xscr_const.perks[PERK_QUICK_RELOAD] = Scr_AllocString("sleight_of_hand", 1);
}
void _Scr_StartupGameType() {
unsigned short ret = Scr_ExecThread(g_scr_data->startgametype, 0);
Scr_FreeThread(ret);
}
void _Scr_LoadGameType() {
unsigned short ret = Scr_ExecThread(g_scr_data->gametype_main, 0);
Scr_FreeThread(ret);
}
void _Scr_PlayerConnect(gentity_t *self) {
unsigned short ret = Scr_ExecEntThread(self->s.number, 0, g_scr_data->playerconnect, 0);
Scr_FreeThread(ret);
}
void _Scr_PlayerDisconnect(self)
gentity_t *self;
{
unsigned short ret = Scr_ExecEntThread(self->s.number, 0, g_scr_data->playerdisconnect, 0);
Scr_FreeThread(ret);
}
void _Scr_PlayerDamage(gentity_t *self, gentity_t *inflictor, gentity_t *attacker, int damage, int dmgflags, unsigned mod, int weapon, vec3_t point, vec3_t dir, int hitloc) {
short (*_G_GetHitLocationString)(int) = (short(*)(int))GAME("G_GetHitLocationString");
Scr_AddConstString(G_GetHitLocationString(hitloc));
int (*getinfo)(int);
*(int*)&getinfo = GAME("BG_GetInfoForWeapon");
unsigned info = (unsigned)getinfo(weapon);
char **_modNames = (char**)GAME("modNames");
if(dir)
Scr_AddVector(dir);
else
Scr_AddUndefined();
if(point)
Scr_AddVector(point);
else
Scr_AddUndefined();
Scr_AddString((char*)*(int*)(info + 4));
if(mod < 0x18)
Scr_AddString(_modNames[mod]);
else
Scr_AddString("badMOD");
Scr_AddInt(dmgflags);
Scr_AddInt(damage);
if(attacker) Scr_AddEntity(attacker); else Scr_AddUndefined();
if(inflictor) Scr_AddEntity(inflictor); else Scr_AddUndefined();
unsigned short ret = Scr_ExecEntThread(self->s.number, 0, g_scr_data->playerdamage, 9);
Scr_FreeThread(ret);
}
void _Cmd_MenuResponse_f(gentity_t *self) {
int svId = atoi(Cmd_Argv(1));
cvar_t *svid = Cvar_Get("sv_serverId", "0", 72);
if(svid->integer != svId)
return;
char cs[MAX_STRING_CHARS] = {0};
unsigned menuIndex = atoi(Cmd_Argv(2));
SV_GetConfigstring( 1180 + menuIndex, cs, sizeof( cs ) );
void (*o)(gentity_t*) = (void(*)(gentity_t*))GAME("Cmd_MenuResponse_f");
o(self);
}
void GScr_LoadGametypeScript( void ) {
char v1[64];
snprintf(v1, 64, "maps/mp/gametypes/%s", g_gametype->string);
g_scr_data->gametype_main = load_callback(v1, "main", 0);
g_scr_data->startgametype = load_callback("maps/mp/gametypes/_callbacksetup", "CodeCallback_StartGameType", 0);
g_scr_data->playerconnect = load_callback("maps/mp/gametypes/_callbacksetup", "CodeCallback_PlayerConnect", 0);
g_scr_data->playerdisconnect = load_callback("maps/mp/gametypes/_callbacksetup", "CodeCallback_PlayerDisconnect", 0);
g_scr_data->playerdamage = load_callback("maps/mp/gametypes/_callbacksetup", "CodeCallback_PlayerDamage", 0);
g_scr_data->playerkilled = load_callback("maps/mp/gametypes/_callbacksetup", "CodeCallback_PlayerKilled", 0);
callbackPlayerCommand = load_callback("callback", "CodeCallback_PlayerCommand", 1);
callbackRemoteCommand = load_callback("callback", "CodeCallback_RemoteCommand", 1);
callbackFireGrenade = load_callback("callback", "CodeCallback_FireGrenade", 1);
extern int callbackEntityDamage, callbackEntityKilled;
callbackEntityDamage = load_callback("callback", "EntityDamage", 1);
callbackEntityKilled = load_callback("callback", "EntityDeath", 1);
Scr_LoadConsts();
}
static int x_localized_string_index = 128;
int X_SetLocalizedStringIndex(int index, const char* str) {
int i = index;
if(!str || !*str)
return 0;
if(i >= 256)
i = x_localized_string_index;
if(i >= 256)
i = x_localized_string_index = 0;
SV_SetConfigstring(i + 1244, str);
return i;
}
static int x_model_index = 1;
int X_ModelIndex(int index, const char* name) {
int i = index;
if(!name || !*name)
return 0;
if(i >= 256)
i = x_model_index;
if(i >= 256)
i = x_model_index = 0;
SV_SetConfigstring(i + 268, name);
return i;
}
int X_LocalizedStringIndex(const char* str) {
int i;
char s[MAX_STRING_CHARS];
if(x_localized_string_index >= 256)
x_localized_string_index = 128;
if(!str || !*str)
return 0;
int start = 1244;
for(i = 1; i < 256; i++) {
SV_GetConfigstring( start + i, s, sizeof( s ) );
if(!*s)
break;
if ( !strcmp( s, str ) )
return i;
}
if(i == 256) { //idea to add ignoring strings, maybe first bit. or use half/half 128 for tmp storage
//overflow
//i = 2047;
i = x_localized_string_index;
}
SV_SetConfigstring(i + 1244, str); //would require to call the precacheString(..) each time you use that string so let's just make a new function
++x_localized_string_index;
return i;
}
/*
//TODO
* reserve player's name up to <sv_maxclients> configstrings which can only be overwritten by connecting players.
* reserve "real precached" strings up to e.g 128~?
* reserve some more
- Richard
*/
void Scr_fuckPrecacheString(int a1) {
int argc = Scr_GetNumParam();
char* str;
int index = 256;
if(argc == 1) {
str = Scr_GetString(0);
} else {
index = Scr_GetInt(0);
str = Scr_GetString(1);
}
int idx = X_SetLocalizedStringIndex(index, str);
Scr_AddInt(idx);
}