-
Notifications
You must be signed in to change notification settings - Fork 1
/
ScriptHandler.h
385 lines (326 loc) · 10.7 KB
/
ScriptHandler.h
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
/* -*- C++ -*-
*
* ScriptHandler.h - Script manipulation class
*
* Copyright (c) 2001-2009 Ogapee. All rights reserved.
*
*
* 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, 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 of Sonozaki Futago-tachi, April 2009, to update from
// Ogapee's 20090331 release source code.
#ifndef __SCRIPT_HANDLER_H__
#define __SCRIPT_HANDLER_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "BaseReader.h"
#include "DirPaths.h"
#define VARIABLE_RANGE 4096
#define IS_TWO_BYTE(x) \
( ((x) & 0xe0) == 0xe0 || ((x) & 0xe0) == 0x80 )
// Mion: for escaping parentheses in handled text
#define LPAREN 0x02
#define RPAREN 0x03
//Mion: for escaping '\' in {}-varlist strings _within script_ (indicates str newline)
#define STR_BACKSLASH 0x1F
//Mion: for escaping '<' & '>' to textbtn delimiters
#define STR_TXTBTN_START 0x01
#define STR_TXTBTN_END 0x04
//Mion: for lookback text relocating
#define TEXT_UP 0x11
#define TEXT_RIGHT 0x12
#define TEXT_DOWN 0x13
#define TEXT_LEFT 0x14
typedef unsigned char uchar3[3];
class ScriptHandler
{
public:
enum { END_NONE = 0,
END_COMMA = 1,
END_1BYTE_CHAR = 2
};
struct LabelInfo{
char *name;
char *label_header;
char *start_address;
int start_line;
int num_of_lines;
};
struct ArrayVariable{
struct ArrayVariable* next;
int no;
int num_dim;
int dim[20];
int *data;
ArrayVariable(){
next = NULL;
data = NULL;
};
~ArrayVariable(){
if (data) delete[] data;
};
ArrayVariable& operator=(const ArrayVariable& av){
no = av.no;
num_dim = av.num_dim;
int total_dim = 1;
for (int i=0 ; i<20 ; i++){
dim[i] = av.dim[i];
total_dim *= dim[i];
}
if (data) delete[] data;
data = NULL;
if (av.data){
data = new int[total_dim];
memcpy(data, av.data, sizeof(int)*total_dim);
}
return *this;
};
};
enum { VAR_NONE = 0,
VAR_INT = 1, // integer
VAR_ARRAY = 2, // array
VAR_STR = 4, // string
VAR_CONST = 8, // direct value or alias, not variable
VAR_PTR = 16 // poiter to a variable, e.g. i%0, s%0
};
struct VariableInfo{
int type;
int var_no; // for integer(%), array(?), string($) variable
ArrayVariable array; // for array(?)
};
ScriptHandler();
~ScriptHandler();
void reset();
FILE *fopen( const char *path, const char *mode, const bool save = false, const bool usesavedir = false );
FILE *fopen( const char *root, const char *path, const char *mode);
void setKeyTable( const unsigned char *key_table );
// basic parser function
const char *readToken();
const char *readName();
const char *readLabel();
void readVariable( bool reread_flag=false );
const char *readStr();
int readInt();
int parseInt( char **buf );
void skipToken();
// function for string access
inline char *getStringBuffer(){ return string_buffer; };
inline char *getSavedStringBuffer(){ return saved_string_buffer; };
char *saveStringBuffer();
void addStringBuffer( char ch );
// function for direct manipulation of script address
inline char *getCurrent(){ return current_script; };
inline char *getNext(){ return next_script; };
void setCurrent(char *pos);
void pushCurrent( char *pos );
void popCurrent();
int getOffset( char *pos );
char *getAddress( int offset );
int getLineByAddress( char *address );
char *getAddressByLine( int line );
LabelInfo getLabelByAddress( char *address );
LabelInfo getLabelByLine( int line );
bool isName( const char *name );
bool isText();
bool compareString( const char *buf );
inline int getEndStatus(){ return end_status; };
inline void toggle1ByteEndStatus() {
if (end_status && END_1BYTE_CHAR)
end_status &= ~END_1BYTE_CHAR;
else
end_status |= END_1BYTE_CHAR;
}
void skipLine( int no=1 );
void setLinepage( bool val );
// function for kidoku history
bool isKidoku();
void markAsKidoku( char *address=NULL );
void setKidokuskip( bool kidokuskip_flag );
void saveKidokuData();
void loadKidokuData();
void addStrVariable(char **buf);
void addIntVariable(char **buf);
void declareDim();
void enableTextgosub(bool val);
void setClickstr( const char *list );
int checkClickstr(const char *buf, bool recursive_flag=false);
void setInt( VariableInfo *var_info, int val, int offset=0 );
void setNumVariable( int no, int val );
void pushVariable();
int getIntVariable( VariableInfo *var_info=NULL );
void setStr( char **dst, const char *src, int num=-1 );
int getStringFromInteger( char *buffer, int no, int num_column, bool is_zero_inserted=false, bool force_zenkaku=false );
int readScriptSub( FILE *fp, char **buf, int encrypt_mode );
int readScript( DirPaths *path );
int labelScript();
LabelInfo lookupLabel( const char* label );
LabelInfo lookupLabelNext( const char* label );
void errorAndExit( const char *str );
ArrayVariable *getRootArrayVariable();
void loadArrayVariable( FILE *fp );
void addNumAlias( const char *str, int no );
void addStrAlias( const char *str1, const char *str2 );
bool findNumAlias( const char *str, int *value );
bool findStrAlias( const char *str, char* buffer );
enum { LABEL_LOG = 0,
FILE_LOG = 1
};
struct LogLink{
LogLink *next;
char *name;
LogLink(){
next = NULL;
name = NULL;
};
~LogLink(){
if ( name ) delete[] name;
};
};
struct LogInfo{
LogLink root_log;
LogLink *current_log;
int num_logs;
const char *filename;
} log_info[2];
LogLink *findAndAddLog( LogInfo &info, const char *name, bool add_flag );
void resetLog( LogInfo &info );
/* ---------------------------------------- */
/* Variable */
struct VariableData{
int num;
bool num_limit_flag;
int num_limit_upper;
int num_limit_lower;
char *str;
VariableData(){
str = NULL;
reset(true);
};
void reset(bool limit_reset_flag){
num = 0;
if (limit_reset_flag)
num_limit_flag = false;
if (str){
delete[] str;
str = NULL;
}
};
};
VariableData &getVariableData(int no);
VariableInfo current_variable, pushed_variable;
int screen_size;
enum { SCREEN_SIZE_640x480 = 0,
SCREEN_SIZE_800x600 = 1,
SCREEN_SIZE_400x300 = 2,
SCREEN_SIZE_320x240 = 3
};
int global_variable_border;
char *game_identifier;
char *save_path;
//Mion: savedir is set by savedirCommand, stores save files
// and main stored gamedata files except envdata
char *savedir;
int game_hash;
BaseReader *cBR;
enum LanguageScript {
JAPANESE_SCRIPT = 0,
LATIN_SCRIPT = 1
} preferred_script, default_script;
private:
enum { OP_INVALID = 0, // 000
OP_PLUS = 2, // 010
OP_MINUS = 3, // 011
OP_MULT = 4, // 100
OP_DIV = 5, // 101
OP_MOD = 6 // 110
};
struct Alias{
struct Alias *next;
char *alias;
int num;
char *str;
Alias(){
next = NULL;
alias = NULL;
str = NULL;
};
Alias( const char *name, int num ){
next = NULL;
alias = new char[ strlen(name) + 1];
strcpy( alias, name );
str = NULL;
this->num = num;
};
Alias( const char *name, const char *str ){
next = NULL;
alias = new char[ strlen(name) + 1];
strcpy( alias, name );
this->str = new char[ strlen(str) + 1];
strcpy( this->str, str );
};
~Alias(){
if (alias) delete[] alias;
if (str) delete[] str;
};
};
int findLabel( const char* label );
char *checkComma( char *buf );
void parseStr( char **buf );
int parseIntExpression( char **buf );
void readNextOp( char **buf, int *op, int *num );
int calcArithmetic( int num1, int op, int num2 );
int parseArray( char **buf, ArrayVariable &array );
int *getArrayPtr( int no, ArrayVariable &array, int offset );
/* ---------------------------------------- */
/* Variable */
struct VariableData *variable_data;
struct ExtendedVariableData{
int no;
VariableData vd;
} *extended_variable_data;
int num_extended_variable_data;
int max_extended_variable_data;
Alias root_num_alias, *last_num_alias;
Alias root_str_alias, *last_str_alias;
ArrayVariable *root_array_variable, *current_array_variable;
DirPaths *archive_path;
int script_buffer_length;
char *script_buffer;
char *tmp_script_buf;
char *string_buffer; // update only be readToken
int string_counter;
char *saved_string_buffer; // updated only by saveStringBuffer
char *str_string_buffer; // updated only by readStr
LabelInfo *label_info;
int num_of_labels;
bool skip_enabled;
bool kidokuskip_flag;
char *kidoku_buffer;
bool text_flag; // true if the current token is text
int end_status;
bool linepage_flag;
bool textgosub_flag;
char *clickstr_list;
char *current_script;
char *next_script;
char *pushed_current_script;
char *pushed_next_script;
unsigned char key_table[256];
bool key_table_flag;
};
#endif // __SCRIPT_HANDLER_H__