forked from M-itch/libcod
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gsc_utils.cpp
949 lines (770 loc) · 17 KB
/
gsc_utils.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
#include "gsc_utils.hpp"
#if COMPILE_UTILS == 1
//thanks to riicchhaarrd/php
void gsc_utils_getarraykeys()
{
unsigned int arrIndex;
if (!stackGetParamObject(0, &arrIndex))
{
stackError("gsc_utils_getarraykeys() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
int arraysize = GetArraySize(arrIndex);
if (!arraysize)
{
stackError("gsc_utils_getarraykeys() got an empty or invalid array");
stackPushUndefined();
return;
}
unsigned int index = arrIndex;
unsigned int name;
stackPushArray();
for (int i = 0; i < arraysize; i++)
{
index = GetNextVariable(index);
name = GetVariableName(index);
if (name < 0x10000)
{
stackPushString(SL_ConvertToString(name));
stackPushArrayLast();
}
}
}
/*
=================
Sys_AnsiColorPrint
Transform Q3 colour codes to ANSI escape sequences
=================
*/
#define MAXPRINTMSG 1024
#define ColorIndex(c) (((c) - '0') & 0x07)
#define Q_COLOR_ESCAPE '^'
#define Q_IsColorString(p) ((p) && *(p) == Q_COLOR_ESCAPE && *((p)+1) && isdigit(*((p)+1))) // ^[0-9]
void Sys_AnsiColorPrint( const char *msg )
{
static char buffer[ MAXPRINTMSG ];
int length = 0;
static int q3ToAnsi[ 8 ] =
{
30, // COLOR_BLACK
31, // COLOR_RED
32, // COLOR_GREEN
33, // COLOR_YELLOW
34, // COLOR_BLUE
36, // COLOR_CYAN
35, // COLOR_MAGENTA
0 // COLOR_WHITE
};
while( *msg )
{
if( Q_IsColorString( msg ) || *msg == '\n' )
{
// First empty the buffer
if( length > 0 )
{
buffer[ length ] = '\0';
fputs( buffer, stdout );
length = 0;
}
if( *msg == '\n' )
{
// Issue a reset and then the newline
fputs( "\033[0m\n", stdout );
msg++;
}
else
{
// Print the color code
snprintf( buffer, sizeof( buffer ), "\033[1;%dm", q3ToAnsi[ ColorIndex( *( msg + 1 ) ) ] );
fputs( buffer, stdout );
msg += 2;
}
}
else
{
if( length >= MAXPRINTMSG - 1 )
break;
buffer[ length ] = *msg;
length++;
msg++;
}
}
// Empty anything still left in the buffer
if( length > 0 )
{
buffer[ length ] = '\0';
fputs( buffer, stdout );
// Issue a reset at the end
fputs( "\033[0m", stdout );
}
}
extern cvar_t *con_coloredPrints;
int stackPrintParam(int param)
{
if (param >= Scr_GetNumParam())
return 0;
switch (stackGetParamType(param))
{
case STACK_STRING:
char *str;
stackGetParamString(param, &str); // no error checking, since we know it's a string
if (con_coloredPrints->boolean)
Sys_AnsiColorPrint(str);
else
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))
{
stackError("gsc_utils_printf() argument is undefined or has a wrong type");
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]);
}
stackPushBool(qtrue);
}
void gsc_utils_outofbandprint()
{
char * address;
char * msg;
if (!stackGetParams("ss", &address, &msg))
{
stackError("gsc_utils_outofbandprint() one or more arguments is undefined or has a wrong type");
stackPushUndefined();
return;
}
netadr_t from;
NET_StringToAdr(address, &from);
NET_OutOfBandPrint(NS_SERVER, from, msg);
}
void gsc_utils_sprintf()
{
char result[COD2_MAX_STRINGLENGTH];
char *str;
if (!stackGetParams("s", &str))
{
stackError("gsc_utils_sprintf() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
int param = 1; // maps to first %
int len = strlen(str);
int num = 0;
for (int i = 0; i < len; i++)
{
if (str[i] == '%')
{
if (str[i + 1] == '%')
{
result[num++] = '%';
i++;
}
else
{
if(param >= Scr_GetNumParam())
continue;
switch (stackGetParamType(param))
{
case STACK_STRING:
char *tmp_str;
stackGetParamString(param, &tmp_str); // no error checking, since we know it's a string
num += sprintf(&(result[num]), "%s", tmp_str);
break;
case STACK_VECTOR:
float vec[3];
stackGetParamVector(param, vec);
num += sprintf(&(result[num]), "(%.2f, %.2f, %.2f)", vec[0], vec[1], vec[2]);
break;
case STACK_FLOAT:
float tmp_float;
stackGetParamFloat(param, &tmp_float);
num += sprintf(&(result[num]), "%.3f", tmp_float); // need a way to define precision
break;
case STACK_INT:
int tmp_int;
stackGetParamInt(param, &tmp_int);
num += sprintf(&(result[num]), "%d", tmp_int);
break;
}
param++;
}
}
else
result[num++] = str[i];
}
result[num] = '\0';
stackPushString(result);
}
void gsc_utils_getAscii()
{
char *str;
if ( ! stackGetParams("s", &str))
{
stackError("gsc_utils_getAscii() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
if (!strlen(str))
{
stackError("gsc_utils_getAscii() string length is 0");
stackPushUndefined();
return;
}
stackPushInt(str[0]);
}
void gsc_utils_putchar()
{
int val;
if ( ! stackGetParams("i", &val))
{
stackError("gsc_utils_putchar() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
if (val < -127 || val > 127)
{
stackError("gsc_utils_putchar() character index is out of range");
stackPushUndefined();
return;
}
char s[2];
s[0] = val;
s[1] = '\0';
stackPushString( s );
}
void gsc_utils_toupper()
{
char *str;
if ( ! stackGetParams("s", &str))
{
stackError("gsc_utils_toupper() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
if (!strlen(str))
{
stackError("gsc_utils_toupper() string length is 0");
stackPushUndefined();
return;
}
stackPushString( I_strupr(str) );
}
void gsc_utils_system()
{
char *cmd;
if ( ! stackGetParams("s", &cmd))
{
stackError("gsc_utils_system() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
stackPushInt( system(cmd) );
}
static int starttime = time(NULL);
void gsc_utils_getserverstarttime()
{
stackPushInt( starttime );
}
void gsc_utils_getsystemtime()
{
time_t timer;
stackPushInt( time(&timer) );
}
void gsc_utils_getlocaltime()
{
time_t timer;
struct tm *timeinfo;
time(&timer);
timeinfo = localtime(&timer);
const char *timestring = asctime(timeinfo);
char stripped_time[128];
strncpy(stripped_time, timestring, sizeof(stripped_time));
stripped_time[strlen(timestring) - 1] = '\0';
stackPushString( stripped_time );
}
void gsc_utils_exponent()
{
float basis;
float exponent;
if ( ! stackGetParams("ff", &basis, &exponent))
{
stackError("gsc_utils_exponent() one or more arguments is undefined or has a wrong type");
stackPushUndefined();
return;
}
stackPushFloat( pow(basis, exponent) );
}
void gsc_utils_round()
{
float val;
if ( ! stackGetParams("f", &val))
{
stackError("gsc_utils_round() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
stackPushFloat( roundf(val * 100) / 100 );
}
void gsc_utils_file_link()
{
char *source, *dest;
if ( ! stackGetParams("ss", &source, &dest))
{
stackError("gsc_utils_file_link() one or more arguments is undefined or has a wrong type");
stackPushUndefined();
return;
}
int link_success = symlink(source, dest) == 0;
stackPushInt( link_success );
}
void gsc_utils_file_unlink()
{
char *file;
if ( ! stackGetParams("s", &file))
{
stackError("gsc_utils_file_unlink() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
int unlink_success = unlink(file) == 0;
stackPushInt( unlink_success );
}
void gsc_utils_file_exists()
{
char *filename;
if ( ! stackGetParams("s", &filename))
{
stackError("gsc_utils_file_exists() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
int file_exists = access(filename, F_OK) != -1;
stackPushInt( file_exists );
}
void gsc_utils_FS_LoadDir()
{
char *path, *dir;
if ( ! stackGetParams("ss", &path, &dir))
{
stackError("gsc_utils_FS_LoadDir() one or more arguments is undefined or has a wrong type");
stackPushUndefined();
return;
}
FS_LoadDir(path, dir);
stackPushBool(qtrue);
}
void gsc_utils_getType()
{
if (Scr_GetNumParam() == 0)
{
stackError("gsc_utils_getType() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
stackPushString( stackGetParamTypeAsString(0) );
}
void gsc_utils_float()
{
if (Scr_GetNumParam() == 0)
{
stackError("gsc_utils_float() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
switch (stackGetParamType(0))
{
case STACK_STRING:
char *asstring;
stackGetParamString(0, &asstring);
stackPushFloat( atof(asstring) );
return;
case STACK_FLOAT:
float asfloat;
stackGetParamFloat(0, &asfloat);
stackPushFloat( asfloat );
return;
case STACK_INT:
int asinteger;
stackGetParamInt(0, &asinteger);
stackPushFloat( float(asinteger) );
return;
default:
stackError("gsc_utils_float() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
}
void gsc_utils_ExecuteString()
{
char *str;
if ( ! stackGetParams("s", &str))
{
stackError("gsc_utils_ExecuteString() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
Cmd_ExecuteString(str);
stackPushBool(qtrue);
}
void gsc_utils_sendgameservercommand()
{
int clientNum;
char *message;
if ( ! stackGetParams("is", &clientNum, &message))
{
stackError("gsc_utils_sendgameservercommand() one or more arguments is undefined or has a wrong type");
stackPushUndefined();
return;
}
SV_GameSendServerCommand(clientNum, 0, message);
stackPushBool(qtrue);
}
void gsc_utils_scandir()
{
char *dirname;
if ( ! stackGetParams("s", &dirname))
{
stackError("gsc_utils_scandir() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
DIR *dir;
struct dirent *dir_ent;
dir = opendir(dirname);
if ( ! dir)
{
stackPushUndefined();
return;
}
stackPushArray();
while ( (dir_ent = readdir(dir)) != NULL)
{
stackPushString(dir_ent->d_name);
stackPushArrayLast();
}
closedir(dir);
}
void gsc_utils_fopen()
{
FILE *file;
char *filename, *mode;
if ( ! stackGetParams("ss", &filename, &mode))
{
stackError("gsc_utils_fopen() one or more arguments is undefined or has a wrong type");
stackPushUndefined();
return;
}
file = fopen(filename, mode);
if (!file)
{
stackError("gsc_utils_fopen() returned a error");
stackPushUndefined();
return;
}
stackPushInt((int)file);
}
void gsc_utils_fread()
{
FILE *file;
if ( ! stackGetParams("i", &file))
{
stackError("gsc_utils_fread() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
if (!file)
{
stackError("gsc_utils_fread() returned a error");
stackPushUndefined();
return;
}
char buffer[256];
int ret = fread(buffer, 1, 255, file);
if ( ! ret)
{
stackPushUndefined();
return;
}
buffer[ret] = '\0';
stackPushString(buffer);
}
void gsc_utils_fwrite()
{
FILE *file;
char *buffer;
if ( ! stackGetParams("is", &file, &buffer))
{
stackError("gsc_utils_fwrite() one or more arguments is undefined or has a wrong type");
stackPushUndefined();
return;
}
if (!file)
{
stackError("gsc_utils_fwrite() returned a error");
stackPushUndefined();
return;
}
stackPushInt(fwrite(buffer, 1, strlen(buffer), file));
}
void gsc_utils_fclose()
{
FILE *file;
if ( ! stackGetParams("i", &file))
{
stackError("gsc_utils_fclose() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
if (!file)
{
stackError("gsc_utils_fclose() returned a error");
stackPushUndefined();
return;
}
stackPushInt( fclose(file) );
}
void gsc_utils_fsize()
{
FILE *file;
if ( ! stackGetParams("i", &file))
{
stackError("gsc_utils_fsize() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
if (!file)
{
stackError("gsc_utils_fsize() returned a error");
stackPushUndefined();
return;
}
struct stat buf;
fstat(fileno(file), &buf);
stackPushInt( buf.st_size );
}
// http://code.metager.de/source/xref/RavenSoftware/jediacademy/code/game/g_utils.cpp#36
void gsc_G_FindConfigstringIndexOriginal()
{
char *name;
int min, max, create;
if ( ! stackGetParams("siii", &name, &min, &max, &create))
{
stackError("gsc_G_FindConfigstringIndexOriginal() one or more arguments is undefined or has a wrong type");
stackPushUndefined();
return;
}
stackPushInt( G_FindConfigstringIndex(name, min, max, create, "G_FindConfigstringIndex() from GSC") );
}
// simple version, without crash
void gsc_G_FindConfigstringIndex()
{
char *name;
int min, max;
if ( ! stackGetParams("sii", &name, &min, &max))
{
stackError("gsc_G_FindConfigstringIndex() one or more arguments is undefined or has a wrong type");
return;
}
for (int i = 1; i < max; i++)
{
const char *curitem = SV_GetConfigstringConst(min + i);
if ( ! *curitem)
break;
if ( ! strcasecmp(name, curitem))
{
stackPushInt(i + min);
return;
}
}
stackPushBool(qtrue);
}
void gsc_get_configstring()
{
int index;
if ( ! stackGetParams("i", &index))
{
stackError("gsc_get_configstring() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
const char *string = SV_GetConfigstringConst(index);
if ( ! *string )
stackPushUndefined();
else
stackPushString(string);
}
void gsc_set_configstring()
{
int index;
char *string;
if ( ! stackGetParams("is", &index, &string))
{
stackError("gsc_set_configstring() one or more arguments is undefined or has a wrong type");
stackPushUndefined();
return;
}
SV_SetConfigstring(index, string);
stackPushBool(qtrue);
}
void gsc_utils_sqrt()
{
float x;
if ( ! stackGetParams("f", &x))
{
stackError("gsc_utils_sqrt() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
stackPushFloat(sqrt(x));
}
void gsc_utils_sqrtInv()
{
float x;
if ( ! stackGetParams("f", &x))
{
stackError("gsc_utils_sqrtInv() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
// http://www.beyond3d.com/content/articles/8/
float xhalf = 0.5f*x;
int i = *(int*)&x;
i = 0x5f3759df - (i>>1);
x = *(float*)&i;
x = x*(1.5f - xhalf*x*x);
stackPushFloat(x);
}
void gsc_make_localized_string()
{
char *str;
if ( ! stackGetParams("s", &str))
{
stackError("gsc_make_localized_string() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
stackPushString(str);
VariableValue *var;
int param = 0;
var = &scrVmPub.top[-param];
var->type = STACK_LOCALIZED_STRING;
}
void gsc_utils_getlasttestclientnumber()
{
#if COD_VERSION == COD2_1_0
int offset = 0x083DF9EC;
#elif COD_VERSION == COD2_1_2
int offset = 0x083E1E8C;
#elif COD_VERSION == COD2_1_3
int offset = 0x083E2F0C;
#endif
stackPushInt(*(int *)offset);
}
void gsc_utils_bullethiteffect()
{
vec3_t origin;
vec3_t normal;
if ( ! stackGetParams("vv", &origin, &normal))
{
stackError("gsc_utils_bullethiteffect() one or more arguments is undefined or has a wrong type");
stackPushUndefined();
return;
}
gentity_t *entity = G_TempEntity(origin, EV_SHOTGUN_HIT);
entity->s.eventParm = DirToByte(normal);
trace_t trace;
vec3_t end_origin = { origin[0] - (normal[0] * 10), origin[1] - (normal[1] * 10), origin[2] - (normal[2] * 10) };
G_LocationalTrace(&trace, origin, end_origin, 1023, 1);
entity->s.surfaceFlags = (trace.surfaceFlags >> 20) & 0x1F;
stackPushBool(qtrue);
}
void gsc_utils_vectorscale()
{
vec3_t vector;
float scale;
if ( ! stackGetParams("vf", &vector, &scale))
{
stackError("gsc_utils_vectorscale() one or more arguments is undefined or has a wrong type");
stackPushUndefined();
return;
}
vec3_t out;
VectorScale(vector, scale, out);
stackPushVector(out);
}
void gsc_utils_remove_file()
{
char *filename;
if (!stackGetParams("s", &filename))
{
stackError("gsc_utils_remove_file() argument is undefined or has a wrong type");
stackPushUndefined();
return;
}
stackPushInt(remove( filename ));
}
void gsc_utils_remotecommand()
{
char * sFrom;
int pointerMsg;
if (!stackGetParams("si", &sFrom, &pointerMsg))
{
stackError("gsc_utils_remotecommand() one or more arguments is undefined or has a wrong type");
return;
}
netadr_t from;
msg_t * msg = (msg_t *)pointerMsg;
NET_StringToAdr(sFrom, &from);
RemoteCommand(from, msg);
}
void RemoteCommand(netadr_t from, msg_t *msg)
{
#if COD_VERSION == COD2_1_0
int lasttime_offset = 0x0848B674;
#elif COD_VERSION == COD2_1_2
int lasttime_offset = 0x0849EB74;
#elif COD_VERSION == COD2_1_3
int lasttime_offset = 0x0849FBF4;
#endif
*(int *)lasttime_offset = 0;
SVC_RemoteCommand(from, msg);
}
#endif