-
Notifications
You must be signed in to change notification settings - Fork 2
/
ScriptHandler.h
362 lines (308 loc) · 10.1 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
/* -*- C++ -*-
*
* ScriptHandler.h - Script manipulation class
*
* Copyright (c) 2001-2018 Ogapee. All rights reserved.
* (C) 2016-2018 jh10001 <[email protected]>
*
*
* 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
*/
#ifndef __SCRIPT_HANDLER_H__
#define __SCRIPT_HANDLER_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "BaseReader.h"
#define IS_TWO_BYTE(x) \
( ((unsigned char)(x) > (unsigned char)0x80) && ((unsigned char)(x) !=(unsigned char) 0xff) )
typedef unsigned char uchar3[3];
class ScriptHandler
{
public:
enum { END_NONE = 0,
END_COMMA = 1,
END_1BYTE_CHAR = 2,
END_COMMA_READ = 4 // for LUA
};
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 // pointer 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();
void setSaveDir(const char *path);
FILE *fopen( const char *path, const char *mode, bool use_save_dir=false );
void setKeyTable( const unsigned char *key_table );
// basic parser function
const char *readToken();
const char *readLabel();
const char *readStr();
int readInt();
void skipToken();
int parseInt( char **buf );
int parseIntExpression( char **buf );
void readVariable( bool reread_flag=false );
// function for string access
inline char *getStringBuffer(){ return string_buffer; };
char *saveStringBuffer();
void addStringBuffer( char ch );
// function for direct manipulation of script address
inline char *getCurrent(bool use_script=false){ return (use_script && is_internal_script)?last_script_context->current_script:current_script; };
inline char *getNext(){ return next_script; };
inline char *getWait(){ return wait_script?wait_script:next_script; };
void setCurrent(char *pos);
void pushCurrent( char *pos );
void popCurrent();
void enterExternalScript(char *pos); // LUA
void leaveExternalScript();
bool isExternalScript();
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 );
void setEndStatus(int val){ end_status |= val; };
inline int getEndStatus(){ return end_status; };
void skipLine( int no=1 );
void setLinepage( bool val );
void setEnglishMode( bool val ){ english_mode = 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 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 );
int getStringFromInteger( char *buffer, int no, int num_column, bool is_zero_inserted=false );
int openScript( char *path );
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 );
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_width;
int screen_height;
int variable_range;
int global_variable_border;
BaseReader *cBR;
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;
};
};
struct ScriptContext{
ScriptContext *prev, *next;
char *current_script;
char *next_script;
int end_status;
VariableInfo current_variable, pushed_variable;
ScriptContext(){
prev = next = NULL;
current_script = next_script = NULL;
}
};
int readScript(char *path);
int readScriptSub(FILE *fp, char **buf, int encrypt_mode);
void readConfiguration();
int labelScript();
int findLabel( const char* label );
char *checkComma( char *buf );
void parseStr( 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;
char *archive_path;
char *save_dir;
int script_buffer_length;
char *script_buffer;
unsigned 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;
char *clickstr_list;
bool english_mode;
char *current_script;
char *next_script;
char *wait_script; // address where '@' or '//' appears
char *pushed_current_script;
char *pushed_next_script;
bool is_internal_script;
ScriptContext root_script_context, *last_script_context;
unsigned char key_table[256];
bool key_table_flag;
};
#endif // __SCRIPT_HANDLER_H__