/
script.c
24932 lines (21834 loc) · 675 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 Hercules.
* http://herc.ws - http://github.com/HerculesWS/Hercules
*
* Copyright (C) 2012-2016 Hercules Dev Team
* Copyright (C) Athena Dev Teams
*
* Hercules 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define HERCULES_CORE
#include "config/core.h" // RENEWAL, RENEWAL_ASPD, RENEWAL_CAST, RENEWAL_DROP, RENEWAL_EDP, RENEWAL_EXP, RENEWAL_LVDMG, SCRIPT_CALLFUNC_CHECK, SECURE_NPCTIMEOUT, SECURE_NPCTIMEOUT_INTERVAL
#include "script.h"
#include "map/atcommand.h"
#include "map/battle.h"
#include "map/battleground.h"
#include "map/channel.h"
#include "map/chat.h"
#include "map/chrif.h"
#include "map/clif.h"
#include "map/date.h"
#include "map/elemental.h"
#include "map/guild.h"
#include "map/homunculus.h"
#include "map/instance.h"
#include "map/intif.h"
#include "map/itemdb.h"
#include "map/log.h"
#include "map/mail.h"
#include "map/map.h"
#include "map/mapreg.h"
#include "map/mercenary.h"
#include "map/mob.h"
#include "map/npc.h"
#include "map/party.h"
#include "map/path.h"
#include "map/pc.h"
#include "map/pet.h"
#include "map/pet.h"
#include "map/quest.h"
#include "map/skill.h"
#include "map/status.h"
#include "map/status.h"
#include "map/storage.h"
#include "map/unit.h"
#include "common/cbasetypes.h"
#include "common/conf.h"
#include "common/db.h"
#include "common/memmgr.h"
#include "common/md5calc.h"
#include "common/mmo.h" // NEW_CARTS
#include "common/nullpo.h"
#include "common/random.h"
#include "common/showmsg.h"
#include "common/socket.h" // usage: getcharip
#include "common/sql.h"
#include "common/strlib.h"
#include "common/sysinfo.h"
#include "common/timer.h"
#include "common/utils.h"
#include "common/HPM.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifndef WIN32
#include <sys/time.h>
#endif
struct script_interface script_s;
struct script_interface *script;
static inline int GETVALUE(const struct script_buf *buf, int i) __attribute__((nonnull (1)));
static inline int GETVALUE(const struct script_buf *buf, int i)
{
Assert_ret(VECTOR_LENGTH(*buf) > i + 2);
return (int)MakeDWord(MakeWord(VECTOR_INDEX(*buf, i), VECTOR_INDEX(*buf, i+1)),
MakeWord(VECTOR_INDEX(*buf, i+2), 0));
}
static inline void SETVALUE(struct script_buf *buf, int i, int n) __attribute__((nonnull (1)));
static inline void SETVALUE(struct script_buf *buf, int i, int n)
{
Assert_retv(VECTOR_LENGTH(*buf) > i + 2);
VECTOR_INDEX(*buf, i) = GetByte(n, 0);
VECTOR_INDEX(*buf, i+1) = GetByte(n, 1);
VECTOR_INDEX(*buf, i+2) = GetByte(n, 2);
}
const char* script_op2name(int op) {
#define RETURN_OP_NAME(type) case type: return #type
switch( op ) {
RETURN_OP_NAME(C_NOP);
RETURN_OP_NAME(C_POS);
RETURN_OP_NAME(C_INT);
RETURN_OP_NAME(C_PARAM);
RETURN_OP_NAME(C_FUNC);
RETURN_OP_NAME(C_STR);
RETURN_OP_NAME(C_CONSTSTR);
RETURN_OP_NAME(C_ARG);
RETURN_OP_NAME(C_NAME);
RETURN_OP_NAME(C_EOL);
RETURN_OP_NAME(C_RETINFO);
RETURN_OP_NAME(C_USERFUNC);
RETURN_OP_NAME(C_USERFUNC_POS);
RETURN_OP_NAME(C_REF);
RETURN_OP_NAME(C_LSTR);
// operators
RETURN_OP_NAME(C_OP3);
RETURN_OP_NAME(C_LOR);
RETURN_OP_NAME(C_LAND);
RETURN_OP_NAME(C_LE);
RETURN_OP_NAME(C_LT);
RETURN_OP_NAME(C_GE);
RETURN_OP_NAME(C_GT);
RETURN_OP_NAME(C_EQ);
RETURN_OP_NAME(C_NE);
RETURN_OP_NAME(C_XOR);
RETURN_OP_NAME(C_OR);
RETURN_OP_NAME(C_AND);
RETURN_OP_NAME(C_ADD);
RETURN_OP_NAME(C_SUB);
RETURN_OP_NAME(C_MUL);
RETURN_OP_NAME(C_POW);
RETURN_OP_NAME(C_DIV);
RETURN_OP_NAME(C_MOD);
RETURN_OP_NAME(C_NEG);
RETURN_OP_NAME(C_LNOT);
RETURN_OP_NAME(C_NOT);
RETURN_OP_NAME(C_R_SHIFT);
RETURN_OP_NAME(C_L_SHIFT);
RETURN_OP_NAME(C_ADD_POST);
RETURN_OP_NAME(C_SUB_POST);
RETURN_OP_NAME(C_ADD_PRE);
RETURN_OP_NAME(C_SUB_PRE);
RETURN_OP_NAME(C_RE_EQ);
RETURN_OP_NAME(C_RE_NE);
default:
ShowDebug("script_op2name: unexpected op=%d\n", op);
return "???";
}
#undef RETURN_OP_NAME
}
#ifdef SCRIPT_DEBUG_DUMP_STACK
static void script_dump_stack(struct script_state* st)
{
int i;
nullpo_retv(st);
ShowMessage("\tstart = %d\n", st->start);
ShowMessage("\tend = %d\n", st->end);
ShowMessage("\tdefsp = %d\n", st->stack->defsp);
ShowMessage("\tsp = %d\n", st->stack->sp);
for( i = 0; i < st->stack->sp; ++i )
{
struct script_data* data = &st->stack->stack_data[i];
ShowMessage("\t[%d] %s", i, script->op2name(data->type));
switch( data->type )
{
case C_INT:
case C_POS:
ShowMessage(" %d\n", data->u.num);
break;
case C_STR:
case C_CONSTSTR:
ShowMessage(" \"%s\"\n", data->u.str);
break;
case C_NAME:
ShowMessage(" \"%s\" (id=%d ref=%p subtype=%s)\n", reference_getname(data), data->u.num, data->ref, script->op2name(script->str_data[data->u.num].type));
break;
case C_RETINFO:
{
struct script_retinfo* ri = data->u.ri;
ShowMessage(" %p {scope.vars=%p, scope.arrays=%p, script=%p, pos=%d, nargs=%d, defsp=%d}\n", ri, ri->scope.vars, ri->scope.arrays, ri->script, ri->pos, ri->nargs, ri->defsp);
}
break;
default:
ShowMessage("\n");
break;
}
}
}
#endif
/// Reports on the console the src of a script error.
void script_reportsrc(struct script_state *st) {
struct block_list* bl;
nullpo_retv(st);
if( st->oid == 0 )
return; //Can't report source.
bl = map->id2bl(st->oid);
if( bl == NULL )
return;
switch( bl->type ) {
case BL_NPC:
{
const struct npc_data *nd = BL_UCCAST(BL_NPC, bl);
if (bl->m >= 0)
ShowDebug("Source (NPC): %s at %s (%d,%d)\n", nd->name, map->list[bl->m].name, bl->x, bl->y);
else
ShowDebug("Source (NPC): %s (invisible/not on a map)\n", nd->name);
}
break;
default:
if( bl->m >= 0 )
ShowDebug("Source (Non-NPC type %u): name %s at %s (%d,%d)\n", bl->type, clif->get_bl_name(bl), map->list[bl->m].name, bl->x, bl->y);
else
ShowDebug("Source (Non-NPC type %u): name %s (invisible/not on a map)\n", bl->type, clif->get_bl_name(bl));
break;
}
}
/// Reports on the console information about the script data.
void script_reportdata(struct script_data* data)
{
if( data == NULL )
return;
switch( data->type ) {
case C_NOP:// no value
ShowDebug("Data: nothing (nil)\n");
break;
case C_INT:// number
ShowDebug("Data: number value=%"PRId64"\n", data->u.num);
break;
case C_STR:
case C_CONSTSTR:// string
if( data->u.str ) {
ShowDebug("Data: string value=\"%s\"\n", data->u.str);
} else {
ShowDebug("Data: string value=NULL\n");
}
break;
case C_NAME:// reference
if( reference_tovariable(data) ) {// variable
const char* name = reference_getname(data);
ShowDebug("Data: variable name='%s' index=%u\n", name, reference_getindex(data));
} else if( reference_toconstant(data) ) {// constant
ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
} else if( reference_toparam(data) ) {// param
ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
} else {// ???
ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script->op2name(data->type));
ShowDebug("Please report this!!! - script->str_data.type=%s\n", script->op2name(script->str_data[reference_getid(data)].type));
}
break;
case C_POS:// label
ShowDebug("Data: label pos=%"PRId64"\n", data->u.num);
break;
default:
ShowDebug("Data: %s\n", script->op2name(data->type));
break;
}
}
/// Reports on the console information about the current built-in function.
void script_reportfunc(struct script_state* st)
{
int params, id;
struct script_data* data;
if (!script_hasdata(st,0)) {
// no stack
return;
}
data = script_getdata(st,0);
if (!data_isreference(data) || script->str_data[reference_getid(data)].type != C_FUNC) {
// script currently not executing a built-in function or corrupt stack
return;
}
id = reference_getid(data);
params = script_lastdata(st)-1;
if (params > 0) {
int i;
ShowDebug("Function: %s (%d parameter%s):\n", script->get_str(id), params, ( params == 1 ) ? "" : "s");
for (i = 2; i <= script_lastdata(st); i++) {
script->reportdata(script_getdata(st,i));
}
} else {
ShowDebug("Function: %s (no parameters)\n", script->get_str(id));
}
}
/*==========================================
* Output error message
*------------------------------------------*/
static void disp_error_message2(const char *mes,const char *pos,int report) __attribute__((nonnull (1))) analyzer_noreturn;
static void disp_error_message2(const char *mes,const char *pos,int report) {
script->error_msg = aStrdup(mes);
script->error_pos = pos;
script->error_report = report;
longjmp( script->error_jump, 1 );
}
#define disp_error_message(mes,pos) (disp_error_message2((mes),(pos),1))
void disp_warning_message(const char *mes, const char *pos) {
script->warning(script->parser_current_src,script->parser_current_file,script->parser_current_line,mes,pos);
}
/// Checks event parameter validity
void check_event(struct script_state *st, const char *evt)
{
if( evt && evt[0] && !stristr(evt, "::On") )
{
ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
script->reportsrc(st);
}
}
/*==========================================
* Hashes the input string
*------------------------------------------*/
unsigned int calc_hash(const char* p) {
unsigned int h;
nullpo_ret(p);
#if defined(SCRIPT_HASH_DJB2)
h = 5381;
while( *p ) // hash*33 + c
h = ( h << 5 ) + h + ((unsigned char)(*p++));
#elif defined(SCRIPT_HASH_SDBM)
h = 0;
while( *p ) // hash*65599 + c
h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)(*p++));
#elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
h = 0;
while( *p ) {
unsigned int g;
h = ( h << 4 ) + ((unsigned char)(*p++));
g = h & 0xF0000000;
if( g ) {
h ^= g >> 24;
h &= ~g;
}
}
#else // athena hash
h = 0;
while( *p )
h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)(*p++);
#endif
return h % SCRIPT_HASH_SIZE;
}
/*==========================================
* Hashes the input string in a case insensitive way
*------------------------------------------*/
unsigned int calc_hash_ci(const char* p) {
unsigned int h = 0;
#ifdef ENABLE_CASE_CHECK
nullpo_ret(p);
#if defined(SCRIPT_HASH_DJB2)
h = 5381;
while( *p ) // hash*33 + c
h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
#elif defined(SCRIPT_HASH_SDBM)
h = 0;
while( *p ) // hash*65599 + c
h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
#elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
h = 0;
while( *p ) {
unsigned int g;
h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
g = h & 0xF0000000;
if( g ) {
h ^= g >> 24;
h &= ~g;
}
}
#else // athena hash
h = 0;
while( *p )
h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
#endif
#endif // ENABLE_CASE_CHECK
return h % SCRIPT_HASH_SIZE;
}
/*==========================================
* script->str_data manipulation functions
*------------------------------------------*/
/// Looks up string using the provided id.
const char* script_get_str(int id)
{
Assert_retr(NULL, id >= LABEL_START && id < script->str_size);
return script->str_buf+script->str_data[id].str;
}
/// Returns the uid of the string, or -1.
int script_search_str(const char* p)
{
int i;
for( i = script->str_hash[script->calc_hash(p)]; i != 0; i = script->str_data[i].next ) {
if( strcmp(script->get_str(i),p) == 0 ) {
return i;
}
}
return -1;
}
void script_casecheck_clear_sub(struct casecheck_data *ccd)
{
#ifdef ENABLE_CASE_CHECK
nullpo_retv(ccd);
if (ccd->str_data) {
aFree(ccd->str_data);
ccd->str_data = NULL;
}
ccd->str_data_size = 0;
ccd->str_num = 1;
if (ccd->str_buf) {
aFree(ccd->str_buf);
ccd->str_buf = NULL;
}
ccd->str_pos = 0;
ccd->str_size = 0;
memset(ccd->str_hash, 0, sizeof(ccd->str_hash));
#endif // ENABLE_CASE_CHECK
}
void script_global_casecheck_clear(void) {
script_casecheck_clear_sub(&script->global_casecheck);
}
void script_local_casecheck_clear(void) {
script_casecheck_clear_sub(&script->local_casecheck);
}
const char *script_casecheck_add_str_sub(struct casecheck_data *ccd, const char *p)
{
#ifdef ENABLE_CASE_CHECK
int len;
int h = script->calc_hash_ci(p);
nullpo_retr(NULL, ccd);
if (ccd->str_hash[h] == 0) {
//empty bucket, add new node here
ccd->str_hash[h] = ccd->str_num;
} else {
int i;
for (i = ccd->str_hash[h]; ; i = ccd->str_data[i].next) {
const char *s = NULL;
Assert_retb(i >= 0 && i < ccd->str_size);
s = ccd->str_buf+ccd->str_data[i].str;
if (strcasecmp(s,p) == 0) {
return s; // string already in list
}
if (ccd->str_data[i].next == 0)
break; // reached the end
}
// append node to end of list
ccd->str_data[i].next = ccd->str_num;
}
// grow list if neccessary
if( ccd->str_num >= ccd->str_data_size ) {
ccd->str_data_size += 1280;
RECREATE(ccd->str_data,struct str_data_struct,ccd->str_data_size);
memset(ccd->str_data + (ccd->str_data_size - 1280), '\0', 1280);
}
len=(int)strlen(p);
// grow string buffer if neccessary
while( ccd->str_pos+len+1 >= ccd->str_size ) {
ccd->str_size += 10240;
RECREATE(ccd->str_buf,char,ccd->str_size);
memset(ccd->str_buf + (ccd->str_size - 10240), '\0', 10240);
}
safestrncpy(ccd->str_buf+ccd->str_pos, p, len+1);
ccd->str_data[ccd->str_num].type = C_NOP;
ccd->str_data[ccd->str_num].str = ccd->str_pos;
ccd->str_data[ccd->str_num].val = 0;
ccd->str_data[ccd->str_num].next = 0;
ccd->str_data[ccd->str_num].func = NULL;
ccd->str_data[ccd->str_num].backpatch = -1;
ccd->str_data[ccd->str_num].label = -1;
ccd->str_pos += len+1;
ccd->str_num++;
#endif // ENABLE_CASE_CHECK
return NULL;
}
const char *script_global_casecheck_add_str(const char *p) {
return script_casecheck_add_str_sub(&script->global_casecheck, p);
}
const char *script_local_casecheck_add_str(const char *p) {
return script_casecheck_add_str_sub(&script->local_casecheck, p);
}
/// Stores a copy of the string and returns its id.
/// If an identical string is already present, returns its id instead.
int script_add_str(const char* p)
{
int len, h = script->calc_hash(p);
#ifdef ENABLE_CASE_CHECK
const char *existingentry = NULL;
#endif // ENABLE_CASE_CHECK
if (script->str_hash[h] == 0) {
// empty bucket, add new node here
script->str_hash[h] = script->str_num;
} else {
// scan for end of list, or occurence of identical string
int i;
for (i = script->str_hash[h]; ; i = script->str_data[i].next) {
if( strcmp(script->get_str(i),p) == 0 ) {
return i; // string already in list
}
if( script->str_data[i].next == 0 )
break; // reached the end
}
// append node to end of list
script->str_data[i].next = script->str_num;
}
#ifdef ENABLE_CASE_CHECK
if( (strncmp(p, ".@", 2) == 0) ) {
// Local scope vars are checked separately to decrease false positives
existingentry = script->local_casecheck.add_str(p);
} else {
existingentry = script->global_casecheck.add_str(p);
if( existingentry ) {
if( strcasecmp(p, "disguise") == 0 || strcasecmp(p, "Poison_Spore") == 0
|| strcasecmp(p, "PecoPeco_Egg") == 0 || strcasecmp(p, "Soccer_Ball") == 0
|| strcasecmp(p, "Horn") == 0 || strcasecmp(p, "Treasure_Box_") == 0
|| strcasecmp(p, "Lord_of_Death") == 0
) // Known duplicates, don't bother warning the user
existingentry = NULL;
}
}
if( existingentry ) {
DeprecationCaseWarning("script_add_str", p, existingentry, script->parser_current_file); // TODO
}
#endif // ENABLE_CASE_CHECK
// grow list if neccessary
if( script->str_num >= script->str_data_size ) {
script->str_data_size += 1280;
RECREATE(script->str_data,struct str_data_struct,script->str_data_size);
memset(script->str_data + (script->str_data_size - 1280), '\0', 1280);
}
len=(int)strlen(p);
// grow string buffer if neccessary
while( script->str_pos+len+1 >= script->str_size ) {
script->str_size += 10240;
RECREATE(script->str_buf,char,script->str_size);
memset(script->str_buf + (script->str_size - 10240), '\0', 10240);
}
safestrncpy(script->str_buf+script->str_pos, p, len+1);
script->str_data[script->str_num].type = C_NOP;
script->str_data[script->str_num].str = script->str_pos;
script->str_data[script->str_num].val = 0;
script->str_data[script->str_num].next = 0;
script->str_data[script->str_num].func = NULL;
script->str_data[script->str_num].backpatch = -1;
script->str_data[script->str_num].label = -1;
script->str_pos += len+1;
return script->str_num++;
}
/**
* Appends 1 byte to the script buffer.
*
* @param a The byte to append.
*/
void add_scriptb(int a)
{
VECTOR_ENSURE(script->buf, 1, SCRIPT_BLOCK_SIZE);
VECTOR_PUSH(script->buf, (uint8)a);
}
/**
* Appends a c_op value to the script buffer.
*
* The value is variable-length encoded into 8-bit blocks.
* The encoding scheme is ( 01?????? )* 00??????, LSB first.
* All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
*
* @param a The value to append.
*/
void add_scriptc(int a)
{
while( a >= 0x40 )
{
script->addb((a&0x3f)|0x40);
a = (a - 0x40) >> 6;
}
script->addb(a);
}
/**
* Appends an integer value to the script buffer.
*
* The value is variable-length encoded into 8-bit blocks.
* The encoding scheme is ( 11?????? )* 10??????, LSB first.
* All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
*
* @param a The value to append.
*/
void add_scripti(int a)
{
while( a >= 0x40 )
{
script->addb((a&0x3f)|0xc0);
a = (a - 0x40) >> 6;
}
script->addb(a|0x80);
}
/**
* Appends a script->str_data object (label/function/variable/integer) to the script buffer.
*
* @param l The id of the script->str_data entry (Maximum up to 16M)
*/
void add_scriptl(int l)
{
int backpatch = script->str_data[l].backpatch;
switch(script->str_data[l].type) {
case C_POS:
case C_USERFUNC_POS:
script->addc(C_POS);
script->addb(script->str_data[l].label);
script->addb(script->str_data[l].label>>8);
script->addb(script->str_data[l].label>>16);
break;
case C_NOP:
case C_USERFUNC:
// Embedded data backpatch there is a possibility of label
script->addc(C_NAME);
script->str_data[l].backpatch = VECTOR_LENGTH(script->buf);
script->addb(backpatch);
script->addb(backpatch>>8);
script->addb(backpatch>>16);
break;
case C_INT:
script->addi(abs(script->str_data[l].val));
if( script->str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
script->addc(C_NEG);
break;
default: // assume C_NAME
script->addc(C_NAME);
script->addb(l);
script->addb(l>>8);
script->addb(l>>16);
break;
}
}
/*==========================================
* Resolve the label
*------------------------------------------*/
void set_label(int l,int pos, const char* script_pos)
{
int i;
if(script->str_data[l].type==C_INT || script->str_data[l].type==C_PARAM || script->str_data[l].type==C_FUNC) {
//Prevent overwriting constants values, parameters and built-in functions [Skotlex]
disp_error_message("set_label: invalid label name",script_pos);
return;
}
if(script->str_data[l].label!=-1) {
disp_error_message("set_label: dup label ",script_pos);
return;
}
script->str_data[l].type=(script->str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
script->str_data[l].label=pos;
for (i = script->str_data[l].backpatch; i >= 0 && i != 0x00ffffff; ) {
int next = GETVALUE(&script->buf, i);
VECTOR_INDEX(script->buf, i-1) = (script->str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
SETVALUE(&script->buf, i, pos);
i = next;
}
}
/// Skips spaces and/or comments.
const char* script_skip_space(const char* p)
{
if( p == NULL )
return NULL;
for(;;)
{
while( ISSPACE(*p) )
++p;
if( *p == '/' && p[1] == '/' )
{// line comment
while(*p && *p!='\n')
++p;
}
else if( *p == '/' && p[1] == '*' )
{// block comment
p += 2;
for(;;)
{
if( *p == '\0' ) {
script->disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
return p;
}
if( *p == '*' && p[1] == '/' )
{// end of block comment
p += 2;
break;
}
++p;
}
}
else
break;
}
return p;
}
/// Skips a word.
/// A word consists of undercores and/or alphanumeric characters,
/// and valid variable prefixes/postfixes.
const char* skip_word(const char* p)
{
nullpo_retr(NULL, p);
// prefix
switch( *p ) {
case '@':// temporary char variable
++p; break;
case '#':// account variable
p += ( p[1] == '#' ? 2 : 1 ); break;
case '\'':// instance variable
++p; break;
case '.':// npc variable
p += ( p[1] == '@' ? 2 : 1 ); break;
case '$':// global variable
p += ( p[1] == '@' ? 2 : 1 ); break;
}
while (ISALNUM(*p) || *p == '_')
++p;
// postfix
if( *p == '$' )// string
p++;
return p;
}
/// Adds a word to script->str_data.
/// @see skip_word
/// @see script->add_str
int add_word(const char* p) {
size_t len;
int i;
nullpo_retr(0, p);
// Check for a word
len = script->skip_word(p) - p;
if( len == 0 )
disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
// Duplicate the word
if( len+1 > script->word_size )
RECREATE(script->word_buf, char, (script->word_size = (len+1)));
memcpy(script->word_buf, p, len);
script->word_buf[len] = 0;
// add the word
i = script->add_str(script->word_buf);
return i;
}
/// Parses a function call.
/// The argument list can have parenthesis or not.
/// The number of arguments is checked.
static
const char* parse_callfunc(const char* p, int require_paren, int is_custom)
{
const char *p2;
char *arg = NULL;
char null_arg = '\0';
int func;
bool macro = false;
nullpo_retr(NULL, p);
// is need add check for arg null pointer below?
func = script->add_word(p);
if (script->str_data[func].type == C_FUNC) {
script->syntax.nested_call++;
if (script->syntax.last_func != -1) {
if (script->str_data[func].val == script->buildin_lang_macro_offset) {
script->syntax.lang_macro_active = true;
macro = true;
} else if (script->str_data[func].val == script->buildin_lang_macro_fmtstring_offset) {
script->syntax.lang_macro_fmtstring_active = true;
macro = true;
}
}
if( !macro ) {
// buildin function
script->syntax.last_func = script->str_data[func].val;
script->addl(func);
script->addc(C_ARG);
}
arg = script->buildin[script->str_data[func].val];
if (script->str_data[func].deprecated)
DeprecationWarning(p);
if( !arg ) arg = &null_arg; // Use a dummy, null string
} else if( script->str_data[func].type == C_USERFUNC || script->str_data[func].type == C_USERFUNC_POS ) {
// script defined function
script->addl(script->buildin_callsub_ref);
script->addc(C_ARG);
script->addl(func);
arg = script->buildin[script->str_data[script->buildin_callsub_ref].val];
if( *arg == 0 )
disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
if( *arg != '*' )
++arg; // count func as argument
} else {
#ifdef SCRIPT_CALLFUNC_CHECK
const char* name = script->get_str(func);
if( !is_custom && strdb_get(script->userfunc_db, name) == NULL ) {
#endif
disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
#ifdef SCRIPT_CALLFUNC_CHECK
} else {;
script->addl(script->buildin_callfunc_ref);
script->addc(C_ARG);
script->addc(C_STR);
while( *name ) script->addb(*name ++);
script->addb(0);
arg = script->buildin[script->str_data[script->buildin_callfunc_ref].val];
if( *arg != '*' ) ++ arg;
}
#endif
}
p = script->skip_word(p);
p = script->skip_space(p);
script->syntax.curly[script->syntax.curly_count].type = TYPE_ARGLIST;
script->syntax.curly[script->syntax.curly_count].count = 0;
if( *p == ';' )
{// <func name> ';'
script->syntax.curly[script->syntax.curly_count].flag = ARGLIST_NO_PAREN;
} else if( *p == '(' && *(p2=script->skip_space(p+1)) == ')' )
{// <func name> '(' ')'
script->syntax.curly[script->syntax.curly_count].flag = ARGLIST_PAREN;
p = p2;
/*
} else if( 0 && require_paren && *p != '(' )
{// <func name>
script->syntax.curly[script->syntax.curly_count].flag = ARGLIST_NO_PAREN;
*/
} else {// <func name> <arg list>
if( require_paren ) {
if( *p != '(' )
disp_error_message("need '('",p);
++p; // skip '('
script->syntax.curly[script->syntax.curly_count].flag = ARGLIST_PAREN;
} else if( *p == '(' ) {
script->syntax.curly[script->syntax.curly_count].flag = ARGLIST_UNDEFINED;
} else {
script->syntax.curly[script->syntax.curly_count].flag = ARGLIST_NO_PAREN;
}
++script->syntax.curly_count;
while( *arg ) {
p2=script->parse_subexpr(p,-1);
if( p == p2 )
break; // not an argument
if( *arg != '*' )
++arg; // next argument
p=script->skip_space(p2);
if( *arg == 0 || *p != ',' )
break; // no more arguments
++p; // skip comma
}
--script->syntax.curly_count;
}
if( arg && *arg && *arg != '?' && *arg != '*' )
disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script->config.warn_func_mismatch_paramnum);
if( script->syntax.curly[script->syntax.curly_count].type != TYPE_ARGLIST )
disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
if( script->syntax.curly[script->syntax.curly_count].flag == ARGLIST_PAREN ) {
if( *p != ')' )
disp_error_message("parse_callfunc: expected ')' to close argument list",p);
++p;
if (script->str_data[func].val == script->buildin_lang_macro_offset)
script->syntax.lang_macro_active = false;
else if (script->str_data[func].val == script->buildin_lang_macro_fmtstring_offset)
script->syntax.lang_macro_fmtstring_active = false;
}
if (!macro) {
if (0 == --script->syntax.nested_call)
script->syntax.last_func = -1;
script->addc(C_FUNC);
}
return p;
}
/// Processes end of logical script line.
/// @param first When true, only fix up scheduling data is initialized
/// @param p Script position for error reporting in set_label
void parse_nextline(bool first, const char* p)
{
if( !first )
{
script->addc(C_EOL); // mark end of line for stack cleanup
script->set_label(LABEL_NEXTLINE, VECTOR_LENGTH(script->buf), p); // fix up '-' labels
}
// initialize data for new '-' label fix up scheduling
script->str_data[LABEL_NEXTLINE].type = C_NOP;
script->str_data[LABEL_NEXTLINE].backpatch = -1;
script->str_data[LABEL_NEXTLINE].label = -1;
}
/**
* Pushes a variable into stack, processing its array index if needed.
* @see parse_variable
*/
void parse_variable_sub_push(int word, const char *p2)
{
if( p2 ) {
const char* p3 = NULL;
// process the variable index
// push the getelementofarray method into the stack
script->addl(script->buildin_getelementofarray_ref);
script->addc(C_ARG);
script->addl(word);
// process the sub-expression for this assignment
p3 = script->parse_subexpr(p2 + 1, 1);
p3 = script->skip_space(p3);
if( *p3 != ']' ) {// closing parenthesis is required for this script
disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
}
// push the closing function stack operator onto the stack
script->addc(C_FUNC);
p3++;
} else {
// No array index, simply push the variable or value onto the stack
script->addl(word);
}
}
/// Parse a variable assignment using the direct equals operator
/// @param p script position where the function should run from
/// @return NULL if not a variable assignment, the new position otherwise
const char* parse_variable(const char* p)
{
int word;
c_op type = C_NOP;
const char *p2 = NULL;
const char *var = p;