-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScriptHandler.cpp
2330 lines (2062 loc) · 70.6 KB
/
ScriptHandler.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
/* -*- C++ -*-
*
* ScriptHandler.cpp - Script manipulation class of ONScripter-EN
*
* Copyright (c) 2001-2009 Ogapee. All rights reserved.
* (original ONScripter, of which this is a fork).
*
* ogapee@aqua.dti2.ne.jp
*
* Copyright (c) 2007-2011 "Uncle" Mion Sonozaki
*
* UncleMion@gmail.com
*
* This program 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 2 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/>
* or write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// Modified by Haeleth, Autumn 2006, to better support OS X/Linux packaging.
// Modified by Mion, April 2009, to update from
// Ogapee's 20090331 release source code.
// Modified by Mion, November 2009, to update from
// Ogapee's 20091115 release source code.
#include "ScriptHandler.h"
#include "ONScripterLabel.h" //so this can call doErrorBox
#include <sys/stat.h>
#include <sys/types.h>
#ifdef WIN32
#include <direct.h>
#include <windows.h>
#endif
#define TMP_SCRIPT_BUF_LEN 4096
#define STRING_BUFFER_LENGTH 2048
#define SKIP_SPACE(p) while ( *(p) == ' ' || *(p) == '\t' ) (p)++
ScriptHandler::ScriptHandler()
{
num_of_labels = 0;
script_buffer = NULL;
kidoku_buffer = NULL;
log_info[LABEL_LOG].filename = "NScrllog.dat";
log_info[FILE_LOG].filename = "NScrflog.dat";
clickstr_list = NULL;
string_buffer = new char[STRING_BUFFER_LENGTH];
str_string_buffer = new char[STRING_BUFFER_LENGTH];
saved_string_buffer = new char[STRING_BUFFER_LENGTH];
gosub_string_buffer = new char[STRING_BUFFER_LENGTH];
variable_data = new VariableData[VARIABLE_RANGE];
extended_variable_data = NULL;
num_extended_variable_data = 0;
max_extended_variable_data = 1;
root_array_variable = NULL;
screen_size = SCREEN_SIZE_640x480;
global_variable_border = 200;
ons = NULL;
archive_path = NULL;
save_path = NULL;
savedir = NULL;
script_path = NULL;
game_identifier = NULL;
game_hash = 0;
current_cmd[0] = '\0';
current_cmd_type = gosub_cmd_type = CMD_NONE;
zenkakko_flag = false;
strict_warnings = false;
default_script = NO_SCRIPT_PREF;
preferred_script = NO_SCRIPT_PREF;
system_menu_script = NO_SCRIPT_PREF;
rgosub_wait_pos = NULL;
rgosub_wait_1byte = NULL;
total_rgosub_wait_size = num_rgosub_waits = cur_rgosub_wait = 0;
is_rgosub_click = rgosub_click_newpage = rgosub_1byte_mode = false;
rgosub_flag = false; // roto 20100205
ignore_textgosub_newline = false;
}
ScriptHandler::~ScriptHandler()
{
reset();
if ( script_buffer ) delete[] script_buffer;
if ( kidoku_buffer ) delete[] kidoku_buffer;
delete[] string_buffer;
delete[] str_string_buffer;
delete[] saved_string_buffer;
delete[] gosub_string_buffer;
delete[] variable_data;
if (script_path) delete[] script_path;
if (save_path) delete[] save_path;
if (savedir) delete[] savedir;
if (game_identifier) delete[] game_identifier;
}
void ScriptHandler::reset()
{
for (int i=0 ; i<VARIABLE_RANGE ; i++)
variable_data[i].reset(true);
if (extended_variable_data) delete[] extended_variable_data;
extended_variable_data = NULL;
num_extended_variable_data = 0;
max_extended_variable_data = 1;
ArrayVariable *av = root_array_variable;
while(av){
ArrayVariable *tmp = av;
av = av->next;
delete tmp;
}
root_array_variable = current_array_variable = NULL;
// reset log info
resetLog( log_info[LABEL_LOG] );
resetLog( log_info[FILE_LOG] );
// reset number alias
Alias *alias;
alias = root_num_alias.next;
while (alias){
Alias *tmp = alias;
alias = alias->next;
delete tmp;
};
last_num_alias = &root_num_alias;
last_num_alias->next = NULL;
// reset string alias
alias = root_str_alias.next;
while (alias){
Alias *tmp = alias;
alias = alias->next;
delete tmp;
};
last_str_alias = &root_str_alias;
last_str_alias->next = NULL;
// reset misc. variables
end_status = END_NONE;
kidokuskip_flag = false;
current_cmd[0] = '\0';
current_cmd_type = gosub_cmd_type = CMD_NONE;
zenkakko_flag = false;
linepage_flag = false;
english_mode = false;
textgosub_flag = false;
skip_enabled = false;
if (clickstr_list){
delete[] clickstr_list;
clickstr_list = NULL;
}
internal_current_script = NULL;
preferred_script = default_script;
if (rgosub_wait_pos) delete[] rgosub_wait_pos;
rgosub_wait_pos = NULL;
if (rgosub_wait_1byte) delete[] rgosub_wait_1byte;
rgosub_wait_1byte = NULL;
total_rgosub_wait_size = num_rgosub_waits = cur_rgosub_wait = 0;
is_rgosub_click = rgosub_click_newpage = rgosub_1byte_mode = false;
}
FILE *ScriptHandler::fopen( const char *path, const char *mode, const bool save, const bool usesavedir )
{
const char* root;
char *file_name;
FILE *fp = NULL;
if (usesavedir && savedir) {
root = savedir;
file_name = new char[strlen(root)+strlen(path)+1];
sprintf( file_name, "%s%s", root, path );
//printf("handler:fopen(\"%s\")\n", file_name);
fp = ::fopen( file_name, mode );
} else if (save) {
root = save_path;
file_name = new char[strlen(root)+strlen(path)+1];
sprintf( file_name, "%s%s", root, path );
//printf("handler:fopen(\"%s\")\n", file_name);
fp = ::fopen( file_name, mode );
} else {
// search within archive_path(s)
file_name = new char[archive_path->max_path_len()+strlen(path)+1];
for (int n=0; n<archive_path->get_num_paths(); n++) {
root = archive_path->get_path(n);
//printf("root: %s\n", root);
sprintf( file_name, "%s%s", root, path );
//printf("handler:fopen(\"%s\")\n", file_name);
fp = ::fopen( file_name, mode );
if (fp != NULL) break;
}
}
delete[] file_name;
return fp;
}
FILE *ScriptHandler::fopen( const char *root, const char *path, const char *mode )
{
char *file_name;
FILE *fp = NULL;
file_name = new char[strlen(root)+strlen(path)+1];
sprintf( file_name, "%s%s", root, path );
//printf("handler:fopen(\"%s\")\n", file_name);
fp = ::fopen( file_name, mode );
delete[] file_name;
return fp;
}
void ScriptHandler::setKeyTable( const unsigned char *key_table )
{
int i;
if (key_table){
key_table_flag = true;
for (i=0 ; i<256 ; i++) this->key_table[i] = key_table[i];
}
else{
key_table_flag = false;
for (i=0 ; i<256 ; i++) this->key_table[i] = i;
}
}
void ScriptHandler::setSavedir( const char *dir )
{
savedir = new char[ strlen(dir) + strlen(save_path) + 2];
sprintf( savedir, "%s%s%c", save_path, dir, DELIMITER );
mkdir(savedir
#ifndef WIN32
, 0755
#endif
);
}
// basic parser function
const char *ScriptHandler::readToken(bool check_pretext)
{
current_script = next_script;
char *buf = current_script;
end_status = END_NONE;
current_variable.type = VAR_NONE;
num_rgosub_waits = cur_rgosub_wait = 0;
current_cmd_type = CMD_NONE;
if (rgosub_flag && is_rgosub_click){
string_counter = 0;
char ch = rgosub_click_newpage ? '\\' : '@';
addStringBuffer( ch );
if (rgosub_1byte_mode)
addStringBuffer( '`' );
rgosub_wait_1byte[num_rgosub_waits] = rgosub_1byte_mode;
rgosub_wait_pos[num_rgosub_waits++] = buf;
current_cmd_type = CMD_TEXT;
if (!rgosub_1byte_mode){
SKIP_SPACE( buf );
}
} else {
SKIP_SPACE( buf );
}
markAsKidoku( buf );
readTokenTop:
if (!is_rgosub_click)
string_counter = 0;
char ch = *buf;
if ((ch == ';') && !is_rgosub_click){ // comment
while ( ch != 0x0a && ch != '\0' ){
addStringBuffer( ch );
ch = *++buf;
}
}
else if (is_rgosub_click || ch & 0x80 ||
(ch >= '0' && ch <= '9') ||
ch == '@' || ch == '\\' || ch == '/' ||
ch == '%' || ch == '?' || ch == '$' ||
ch == '[' || ch == '(' || ch == '`' ||
ch == '!' || ch == '#' || ch == ',' ||
ch == '{' || ch == '<' || ch == '>' ||
ch == '"'){ // text or pretext
//Mion: parsing pretext tags as separate tokens from text,
// which is what NScr seems to be doing
if (check_pretext && !is_rgosub_click &&
( (ch == '[') ||
(zenkakko_flag && (ch == (char)0x81) && (buf[1] == (char)0x79)) )) {
//pretext tag
current_cmd_type = CMD_PRETEXT;
if (ch != '[') ++buf; //fullwidth bracket is 2 bytes
ch = *++buf;
bool in_1byte_mode = false;
while (1){
if ((ch == 0x0a) || (ch == 0x00)) {
//Mion: NScr will actually parse until a terminating ']',
// even including newlines... probably not used by anything
// (or a good idea), so we'll stop parsing at end of line
errorAndCont( "readToken: unterminated pretext tag" );
break;
}
else if (ch == '`')
in_1byte_mode = !in_1byte_mode;
else if (!in_1byte_mode && (ch == ']')) {
++buf;
break;
}
else if (!in_1byte_mode && zenkakko_flag &&
(ch == (char)0x81) &&
(buf[1] == (char)0x7A)) {
buf += 2;
break;
}
else if ( IS_TWO_BYTE(ch) ) {
addStringBuffer( ch );
ch = *++buf;
}
addStringBuffer( ch );
ch = *++buf;
}
next_script = buf;
return string_buffer;
}
current_cmd_type = CMD_TEXT;
bool ignore_click_flag = false;
bool clickstr_flag = false;
bool is_nscr_english = (english_mode && (ch == '>'));
if (is_nscr_english)
ch = *++buf;
bool in_1byte_mode = is_nscr_english;
if (is_rgosub_click)
in_1byte_mode = rgosub_1byte_mode;
while (1){
if (rgosub_flag && (num_rgosub_waits == total_rgosub_wait_size)){
//double the rgosub wait buffer size
char **tmp = rgosub_wait_pos;
bool *tmp2 = rgosub_wait_1byte;
total_rgosub_wait_size *= 2;
rgosub_wait_pos = new char*[total_rgosub_wait_size];
rgosub_wait_1byte = new bool[total_rgosub_wait_size];
for (int i=0; i<num_rgosub_waits; i++){
rgosub_wait_pos[i] = tmp[i];
rgosub_wait_1byte[i] = tmp2[i];
}
delete[] tmp;
delete[] tmp2;
}
char *tmp = buf;
SKIP_SPACE(tmp);
if (!(is_nscr_english || in_1byte_mode) ||
(*tmp == 0x0a) || (*tmp == 0x00)) {
// always skip trailing spaces
buf = tmp;
ch = *buf;
}
if ((ch == 0x0a) || (ch == 0x00)) break;
if ( IS_TWO_BYTE(ch) ){
if (!ignore_click_flag &&
(checkClickstr(buf) > 0))
clickstr_flag = true;
else
clickstr_flag = false;
addStringBuffer( ch );
ch = *++buf;
if (ch == 0x0a || ch == '\0') break; //invalid 2-byte char
addStringBuffer( ch );
ch = *++buf;
//Mion: ons-en processes clickstr chars here in readToken,
// not in ONScripterLabel_text - adds a special
// sequence '\@' after the clickstr char
if (clickstr_flag) {
// insert a clickwait-or-newpage
addStringBuffer('\\');
addStringBuffer('@');
if (textgosub_flag) {
char *tmp = buf;
SKIP_SPACE(tmp);
// if "ignore-textgosub-newline" cmd-line option,
// ignore newline after clickwait if textgosub used
// (fixes some older onscripter-en games)
if (ignore_textgosub_newline && (*tmp == 0x0a))
buf = tmp+1;
break;
}
if (rgosub_flag){
rgosub_wait_1byte[num_rgosub_waits] = in_1byte_mode;
rgosub_wait_pos[num_rgosub_waits++] = buf+1;
}
}
ignore_click_flag = clickstr_flag = false;
}
else {
if (ch == '`'){
addStringBuffer( ch );
ch = *++buf;
if (!is_nscr_english)
in_1byte_mode = !in_1byte_mode;
}
else if (in_1byte_mode) {
if (ch == '$'){
if (buf[1] == '$') ++buf; else{
addStrVariable(&buf);
while (*--buf == ' ');
ch = *++buf;
ignore_click_flag = false;
continue;
}
}
if ((ch == '_') && (checkClickstr(buf+1) > 0)) {
ignore_click_flag = true;
ch = *++buf;
continue;
}
if ((ch == '@') || (ch == '\\')) {
if (!ignore_click_flag){
addStringBuffer( ch );
if (rgosub_flag){
rgosub_wait_1byte[num_rgosub_waits] = in_1byte_mode;
rgosub_wait_pos[num_rgosub_waits++] = buf+1;
}
}
if (textgosub_flag){
++buf;
// if "ignore-textgosub-newline", ignore
// newline after clickwait if textgosub
// (fixes older onscripter-en games)
char *tmp = buf;
SKIP_SPACE(tmp);
if (ignore_textgosub_newline &&(*tmp == 0x0a))
buf = tmp+1;
break;
}
ch = *++buf;
continue;
}
if (!ignore_click_flag && (checkClickstr(buf) > 0))
clickstr_flag = true;
else
clickstr_flag = false;
// no ruby in 1byte mode; escape parens
if (ch == '(') {
addStringBuffer( LEFT_PAREN );
} else if (ch == ')') {
addStringBuffer( RIGHT_PAREN );
} else if (ch == 0x0a || ch == '\0') break;
else
addStringBuffer( ch );
ch = *++buf;
//Mion: ons-en processes clickstr chars here in readToken,
// not in ONScripterLabel_text - adds a special
// sequence '\@' after the clickstr char
if (clickstr_flag) {
// insert a clickwait-or-newpage
addStringBuffer('\\');
addStringBuffer('@');
if (textgosub_flag) break;
if (rgosub_flag){
rgosub_wait_1byte[num_rgosub_waits] = in_1byte_mode;
rgosub_wait_pos[num_rgosub_waits++] = buf;
}
}
ignore_click_flag = clickstr_flag = false;
}
else{ //!in_1byte_mode
if ((ch == '@') || (ch == '\\')) {
if (!ignore_click_flag){
addStringBuffer( ch );
if (rgosub_flag){
rgosub_wait_1byte[num_rgosub_waits] = in_1byte_mode;
rgosub_wait_pos[num_rgosub_waits++] = buf+1;
}
}
if (textgosub_flag){
++buf;
// if "ignore-textgosub-newline", ignore
// newline after clickwait if textgosub
// (fixes older onscripter-en games)
char *tmp = buf;
SKIP_SPACE(tmp);
if (ignore_textgosub_newline &&(*tmp == 0x0a))
buf = tmp+1;
break;
}
ch = *++buf;
continue;
}
if (ch == '%' || ch == '?'){
addIntVariable(&buf);
}
else if (ch == '$'){
addStrVariable(&buf);
}
else if (ch == '<'){
addStringBuffer(TXTBTN_START);
ch = *++buf;
}
else if (ch == '>'){
addStringBuffer(TXTBTN_END);
ch = *++buf;
}
else if (ch == '{') {
// comma list of var/val pairs
buf++;
pushCurrent(buf);
next_script = buf;
TmpVariableDataLink *tmp = &tmp_variable_data_link;
while (tmp->next != NULL)
tmp = tmp->next;
while( *buf != '}' ) {
readVariable();
pushVariable();
tmp->next = new TmpVariableDataLink;
tmp = tmp->next;
tmp->vi.var_no = pushed_variable.var_no;
tmp->vi.type = pushed_variable.type;
VariableData &vd = getVariableData(tmp->vi.var_no);
tmp->num = vd.num;
//printf("variable: $%d\n", pushed_variable.var_no);
buf = next_script;
if ( tmp->vi.type & VAR_INT ) {
tmp->num = parseIntExpression(&buf);
//printf("int: %d\n", x);
} else if ( tmp->vi.type & VAR_STR ) {
bool invar_1byte_mode = false;
int tmp_count = 0;
strcpy(saved_string_buffer, "");
while (*buf != 0x0a && *buf != '\0' &&
(invar_1byte_mode || ((*buf != ',') && (*buf != '}')))) {
if (*buf == '`')
invar_1byte_mode = !invar_1byte_mode;
if ((tmp_count+1 >= STRING_BUFFER_LENGTH) ||
(IS_TWO_BYTE(*buf) && (tmp_count+2 >= STRING_BUFFER_LENGTH)))
errorAndExit("readToken: var string length exceeds 2048 bytes.");
else if (IS_TWO_BYTE(*buf)) {
saved_string_buffer[tmp_count++] = *buf++;
saved_string_buffer[tmp_count++] = *buf++;
} else if ((*buf == '\\') || (*buf == BACKSLASH)) {
//Mion: I really shouldn't be modifying
// the script buffer FIXME
saved_string_buffer[tmp_count++] = '\\';
*buf++ = BACKSLASH;
} else
saved_string_buffer[tmp_count++] = *buf++;
saved_string_buffer[tmp_count] = '\0';
}
setStr( &tmp->str, saved_string_buffer );
//printf("string: %s\n", saved_string_buffer);
}
next_script = checkComma(buf);
buf = next_script;
if (!(getEndStatus() & END_COMMA)) break;
}
end_status = END_NONE;
current_variable.type = VAR_NONE;
popCurrent();
if (*buf == '}')
buf++;
}
else{
if (ch == '_')
ignore_click_flag = true;
else
ignore_click_flag = false;
addStringBuffer( ch );
buf++;
if (*buf == ' ') {
addStringBuffer( *buf );
buf++;
}
}
ch = *buf;
if (ch == 0x0a || ch == '\0') break;
}
}
}
//now process any {} tmp variables
TmpVariableDataLink *tmp = tmp_variable_data_link.next;
while (tmp != NULL) {
if ( tmp->vi.type & VAR_INT )
setInt( &tmp->vi, tmp->num);
else if ( tmp->vi.type & VAR_STR )
setStr( &variable_data[ tmp->vi.var_no ].str, tmp->str );
TmpVariableDataLink *last = tmp;
tmp = tmp->next;
delete last;
}
tmp_variable_data_link.next = NULL;
current_cmd_type = CMD_TEXT;
}
else if ((ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ||
ch == '_'){ // command
do{
if (ch >= 'A' && ch <= 'Z') ch += 'a' - 'A';
addStringBuffer( ch );
ch = *++buf;
}
while((ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9') ||
ch == '_');
}
else if (ch == '*'){ // label
return readLabel();
}
else if ((ch == 0x0a) && !is_rgosub_click){
addStringBuffer( ch );
markAsKidoku( buf++ );
}
else if (ch == '~' || ch == ':'){
addStringBuffer( ch );
markAsKidoku( buf++ );
}
else if (ch != '\0'){
snprintf(errbuf, MAX_ERRBUF_LEN,
"readToken: skipping unknown heading character %c (%x)", ch, ch);
errorAndCont( errbuf );
buf++;
goto readTokenTop;
}
is_rgosub_click = false;
next_script = checkComma(buf);
//printf("readToken [%s] len=%d [%c(%x)] %p\n", string_buffer, strlen(string_buffer), ch, ch, next_script);
return string_buffer;
}
const char *ScriptHandler::readName()
{
// bare word - not a string variable
end_status = END_NONE;
current_variable.type = VAR_NONE;
current_script = next_script;
SKIP_SPACE( current_script );
char *buf = current_script;
string_counter = 0;
char ch = *buf;
if ( ((ch >= 'a') && (ch <= 'z')) ||
((ch >= 'A') && (ch <= 'Z')) ||
(ch == '_') ){
if ( (ch >= 'A') && (ch <= 'Z') )
ch += 'a' - 'A';
addStringBuffer( ch );
ch = *(++buf);
while( ((ch >= 'a') && (ch <= 'z')) ||
((ch >= 'A') && (ch <= 'Z')) ||
((ch >= '0') && (ch <= '9')) ||
(ch == '_') ){
if ( (ch >= 'A') && (ch <= 'Z') )
ch += 'a' - 'A';
addStringBuffer( ch );
ch = *(++buf);
}
}
addStringBuffer( '\0' );
next_script = checkComma(buf);
return string_buffer;
}
const char *ScriptHandler::readColor(bool *is_color)
{
// bare color type - not a string variable
end_status = END_NONE;
current_variable.type = VAR_NONE;
current_script = next_script;
SKIP_SPACE( current_script );
char *buf = current_script;
string_counter = 0;
addStringBuffer( '#' );
char ch = *(++buf);
int i;
for (i=0; i<7; i++) {
if ( ((ch >= '0') && (ch <= '9')) ||
((ch >= 'a') && (ch <= 'f')) ||
((ch >= 'A') && (ch <= 'F')) ) {
addStringBuffer( ch );
ch = *(++buf);
} else
break;
}
if (i!=6) {
if (is_color != NULL) {
*is_color = false;
string_counter = 0;
addStringBuffer( '\0' );
return string_buffer;
} else {
strncpy(string_buffer, current_script, 16);
string_buffer[16] = '\0';
errorAndExit( "readColor: not a valid color type." );
}
}
addStringBuffer( '\0' );
next_script = checkComma(buf);
if (is_color != NULL)
*is_color = true;
return string_buffer;
}
const char *ScriptHandler::readLabel()
{
// *NAME, "*NAME", or $VAR that equals "*NAME"
end_status = END_NONE;
current_variable.type = VAR_NONE;
current_script = next_script;
SKIP_SPACE( current_script );
char *buf = current_script;
char *tmp = NULL;
string_counter = 0;
char ch = *buf;
if ((ch == '$') || (ch == '"') || (ch == '`')){
parseStr(&buf);
tmp = buf;
string_counter = 0;
buf = str_string_buffer;
SKIP_SPACE(buf);
ch = *buf;
}
if (ch == '*') {
while (ch == '*'){
addStringBuffer( ch );
ch = *(++buf);
}
SKIP_SPACE(buf);
ch = *buf;
while( ((ch >= 'a') && (ch <= 'z')) ||
((ch >= 'A') && (ch <= 'Z')) ||
((ch >= '0') && (ch <= '9')) ||
(ch == '_') ){
if ( (ch >= 'A') && (ch <= 'Z') )
ch += 'a' - 'A';
addStringBuffer( ch );
ch = *(++buf);
}
}
addStringBuffer( '\0' );
if ( (string_buffer[0] == '\0') || (string_buffer[1] == '\0') ){
buf = current_script;
while (*buf && (*buf != 0x0a))
++buf;
ch = *buf;
*buf = '\0';
if (tmp != NULL) {
snprintf(errbuf, MAX_ERRBUF_LEN,
"Invalid label specification '%s' ('%s')",
current_script, str_string_buffer);
*buf = ch;
errorAndExit(errbuf);
} else {
snprintf(errbuf, MAX_ERRBUF_LEN,
"Invalid label specification '%s'", current_script);
*buf = ch;
errorAndExit(errbuf);
}
}
if (tmp != NULL)
buf = tmp;
next_script = checkComma(buf);
return string_buffer;
}
const char *ScriptHandler::readStr()
{
end_status = END_NONE;
current_variable.type = VAR_NONE;
current_script = next_script;
SKIP_SPACE( current_script );
char *buf = current_script;
string_buffer[0] = '\0';
string_counter = 0;
while(1){
parseStr(&buf);
buf = checkComma(buf);
string_counter += strlen(str_string_buffer);
if (string_counter+1 >= STRING_BUFFER_LENGTH)
errorAndExit("readStr: string length exceeds 2048 bytes.");
strcat(string_buffer, str_string_buffer);
if (buf[0] != '+') break;
buf++;
}
next_script = buf;
return string_buffer;
}
int ScriptHandler::readInt()
{
string_counter = 0;
string_buffer[string_counter] = '\0';
end_status = END_NONE;
current_variable.type = VAR_NONE;
current_script = next_script;
SKIP_SPACE( current_script );
char *buf = current_script;
int ret = parseIntExpression(&buf);
next_script = checkComma(buf);
return ret;
}
void ScriptHandler::skipToken()
{
SKIP_SPACE( current_script );
char *buf = current_script;
bool quote_flag = false;
bool text_flag = false;
while(1){
if ( *buf == 0x0a ||
(!quote_flag && !text_flag && (*buf == ':' || *buf == ';') ) ) break;
if ( *buf == '"' ) quote_flag = !quote_flag;
if ( IS_TWO_BYTE(*buf) ){
buf += 2;
if ( !quote_flag ) text_flag = true;
}
else
buf++;
}
if (text_flag && *buf == 0x0a) buf++;
next_script = buf;
}
// string access function
char *ScriptHandler::saveStringBuffer()
{
strcpy( saved_string_buffer, string_buffer );
return saved_string_buffer;
}
// script address direct manipulation function
void ScriptHandler::setCurrent(char *pos, bool nowarn)
{
// warn if directly setting current_script outside the script_buffer
if (!nowarn &&
( (pos < script_buffer) || (pos >= script_buffer + script_buffer_length) ))
errorAndCont( "setCurrent: outside script bounds", NULL, "Address Issue" );
current_script = next_script = pos;
}
void ScriptHandler::pushCurrent( char *pos )
{
// push to use a temporary address for quick buffer parsing
pushed_current_script = current_script;
pushed_next_script = next_script;
setCurrent(pos, true);
}
void ScriptHandler::popCurrent()
{
current_script = pushed_current_script;
next_script = pushed_next_script;
}
void ScriptHandler::enterExternalScript(char *pos)
{
internal_current_script = current_script;
internal_next_script = next_script;
setCurrent(pos);
internal_end_status = end_status;
internal_current_variable = current_variable;
internal_pushed_variable = pushed_variable;
}
void ScriptHandler::leaveExternalScript()
{
setCurrent(internal_current_script);
internal_current_script = NULL;
next_script = internal_next_script;
end_status = internal_end_status;
current_variable = internal_current_variable;
pushed_variable = internal_pushed_variable;
}
bool ScriptHandler::isExternalScript()
{
return (internal_current_script != NULL);
}
int ScriptHandler::getOffset( char *pos )
{
return pos - script_buffer;
}
char *ScriptHandler::getAddress( int offset )
{
return script_buffer + offset;
}
int ScriptHandler::getLineByAddress( char *address )
{
if ( (address < script_buffer) || (address >= script_buffer + script_buffer_length) ) {
errorAndExit( "getLineByAddress: outside script bounds", NULL, "Address Error" );
return -1; //dummy
}
LabelInfo label = getLabelByAddress( address );
char *addr = label.label_header;
int line = 0;
while ( address > addr && line < label.num_of_lines){
if ( *addr == 0x0a ) line++;
addr++;
}
return line;
}
char *ScriptHandler::getAddressByLine( int line )
{
LabelInfo label = getLabelByLine( line );
int l = line - label.start_line;
char *addr = label.label_header;
while ( l > 0 ){
while( *addr != 0x0a ) addr++;
addr++;
l--;
}
return addr;
}
ScriptHandler::LabelInfo ScriptHandler::getLabelByAddress( char *address )
{
int i;
for ( i=0 ; i<num_of_labels-1 ; i++ ){
if ( label_info[i+1].start_address > address )
return label_info[i];
}
return label_info[i];
}
ScriptHandler::LabelInfo ScriptHandler::getLabelByLine( int line )
{
int i;
for ( i=0 ; i<num_of_labels-1 ; i++ ){
if ( label_info[i+1].start_line > line )
return label_info[i];
}
if (i == num_of_labels-1) {
int num_lines = label_info[i].start_line + label_info[i].num_of_lines;
if (line >= num_lines) {
snprintf(errbuf, MAX_ERRBUF_LEN,
"getLabelByLine: line %d outside script bounds (%d lines)",
line, num_lines);
errorAndExit( errbuf, NULL, "Address Error" );
}
}
return label_info[i];