DCDecomp
A decompilation of Dark Cloud for the PlayStation 2
Loading...
Searching...
No Matches
scriptinterpreter.cpp
1#include "scriptinterpreter.hpp"
2
3#include <cstdio>
4#include <cstdlib>
5#include <cstring>
6
8 if (tag_table == NULL) {
9 return -1;
10 }
11
12 do {
13 if (!ControlCode()) {
14 return -1;
15 }
16
18 return -1;
19 }
20 } while (current_tag >= tag_count || current_tag < 0);
21
22 if (tag_table[current_tag].argument_types[0] >= 0) {
23 int status = GetArg(tag_table[current_tag].argument_types);
24
25 if (status == 0) {
26 return -1;
27 }
28
29 if (status < 0) {
30 printf("error at ");
31 printf("%s\n", tag_table[current_tag].name);
32
33 for (;;) {
34 }
35 }
36 }
37
38 return current_tag;
39}
40
41void CScriptInterpreter::SetTAG(TAG_PARAM *tags, int count) {
42 tag_table = tags;
43 tag_count = count;
44}
45
47 function_table = functions;
48 function_count = count;
49}
50
51void CScriptInterpreter::SetScript(char *script, int script_size) {
52 data = script;
53 size = script_size;
54 pos = 0;
56 current_tag = -1;
57 PreProcess__FR9input_str__2(*this);
58}
59
61 data = NULL;
62 data = NULL;
63 size = 0;
64 pos = 0;
66 current_tag = -1;
67 tag_table = NULL;
68 function_table = NULL;
69}
70
72 if (!SkipSpace__FR9input_str__3(*this)) {
73 return 0;
74 }
75
76 int control_type = 1;
77
78 if (memcmp(data + pos, "if", 2) == 0) {
79 control_type = 2;
80 printf("if\n");
81 }
82
83 if (control_type == 1) {
84 return 1;
85 }
86
87 int c;
88
89 do {
90 if (get(&c) == 0) {
91 return 0;
92 }
93 } while (c != '(');
94
95 int condition;
96
97 if (!CallFunction(&condition)) {
98 return 0;
99 }
100
101 if (condition == 0) {
102 do {
103 if (get(&c) == 0) {
104 return 0;
105 }
106 } while (c != '}');
107 }
108
109 return control_type;
110}
111
113 char call_text[512];
114 void *call_arguments[24];
115
116 if (!SkipSpace__FR9input_str__3(*this)) {
117 return 0;
118 }
119
120 int length = 0;
121 int argc = 0;
122 int c;
123
124 while (1) {
125 if (get(&c) == 0) {
126 return 0;
127 }
128
129 if (c == ')') {
130 break;
131 }
132
133 if (c == '(' || c == ',') {
134 call_text[length++] = 0;
135 argc++;
136 } else if (CheckChar__Fc__3(c)) {
137 call_text[length++] = c;
138 }
139 }
140
141 call_text[length] = 0;
142 call_text[length + 1] = 0;
143
144 if (function_table == NULL) {
145 return -1;
146 }
147
148 SPI_FUNC_PARAM *command = NULL;
149
150 for (length = 0; length < function_count; length++) {
151 if (strcmp(function_table[length].name, call_text) == 0) {
152 command = &function_table[length];
153 break;
154 }
155 }
156
157 if (command == NULL) {
158 return -1;
159 }
160
161 for (length = 0; command->argument_types[length] >= 0; length++) {
162 }
163
164 if (length != argc || argc >= 24) {
165 return -1;
166 }
167
168 int used = 0;
170 char *word = call_text;
171
172 for (length = 0; length < argc; length++) {
173 int value_size = 0;
174
175 while (*word++ != 0) {
176 }
177
178 call_arguments[length] = value_out;
179
180 switch (command->argument_types[length]) {
181 case SCRIPT_ARGUMENT_STRING:
182 strcpy((char *) value_out, word);
183 value_size = strlen(word);
184 value_size = ((value_size >> 2) + 1) << 2;
185 break;
186 case SCRIPT_ARGUMENT_INTEGER:
187 *(int *) value_out = atoi(word);
188 value_size = 4;
189 break;
190 case SCRIPT_ARGUMENT_FLOAT:
191 *(float *) value_out = atof(word);
192 value_size = 4;
193 break;
194 }
195
196 value_out += value_size;
197 used += value_size;
198 }
199
200 *result = command->function(call_arguments);
201 return 1;
202}
203
204int CScriptInterpreter::GetArg(int *argument_types) {
205 char token[256];
206
207 if (!SkipSpace__FR9input_str__3(*this)) {
208 return 0;
209 }
210
211 int argc;
212
213 for (argc = 0; argument_types[argc] >= 0; argc++) {
214 }
215
216 int c;
217 int i;
218 int used = 0;
219
220 for (i = 0; i < argc; i++) {
221 arguments[i] = &argument_text[used];
222 int length = 0;
223
224 if (!SkipSpace__FR9input_str__3(*this)) {
225 return 0;
226 }
227
228 if (get(&c) == 0) {
229 return 0;
230 }
231
232 if (c != ',') {
233 token[length++] = c;
234 } else if (!SkipSpace__FR9input_str__3(*this)) {
235 return 0;
236 }
237
238 while (1) {
239 if (get(&c) == 0) {
240 return 0;
241 }
242
243 if (c == ',' || !CheckChar__Fc__3(c)) {
244 break;
245 }
246
247 token[length++] = c;
248 }
249
250 token[length] = 0;
251
252 switch (argument_types[i]) {
253 case SCRIPT_ARGUMENT_STRING:
254 if (token[0] != '"') {
255 return -1;
256 }
257
258 for (length = 1;; length++) {
259 if (token[length] == '"') {
260 token[length] = 0;
261 break;
262 }
263
264 if (token[length] == 0) {
265 return -1;
266 }
267 }
268
269 strcpy((char *) arguments[i], token + 1);
270 used += ((((int) strlen((char *) arguments[i]) + 1) >> 2) + 1) << 2;
271 break;
272 case SCRIPT_ARGUMENT_INTEGER:
273 for (length = 0; token[length] != 0; length++) {
274 char digit = token[length];
275
276 if ((digit < '0' || digit > '9') && digit != '-') {
277 return -1;
278 }
279 }
280
281 *(int *) arguments[i] = atoi(token);
282 used += 4;
283 break;
284 case SCRIPT_ARGUMENT_FLOAT:
285 for (length = 0; token[length] != 0; length++) {
286 char digit = token[length];
287
288 if ((digit < '0' || digit > '9') && digit != '.' && digit != '-') {
289 return -1;
290 }
291 }
292
293 *(float *) arguments[i] = atof(token);
294 used += 4;
295 break;
296 default:
297 return -1;
298 }
299 }
300
301 return 1;
302}
303
305 char command[256];
306
307 if (!SkipSpace__FR9input_str__3(*this)) {
308 return 0;
309 }
310
311 int length = 0;
312 int c;
313
314 while (1) {
315 if (get(&c) == 0) {
316 return 0;
317 }
318
319 if (!CheckChar__Fc__3(c)) {
320 break;
321 }
322
323 command[length++] = c;
324 }
325
326 command[length] = 0;
327
328 if (command[0] < 'A' || command[0] > 'Z') {
329 *tag_index = -1;
330 return 1;
331 }
332
333 for (int i = 0; i < tag_count; i++) {
334 if (strcmp(tag_table[i].name, command) == 0) {
335 *tag_index = i;
336 return 1;
337 }
338 }
339
340 if (strcmp(command, "end") == 0) {
341 return 0;
342 }
343
344 *tag_index = -1;
345 return 1;
346}
int CallFunction(int *result)
SPI_FUNC_PARAM * function_table
int GetArg(int *argument_types)
void SetFunction(SPI_FUNC_PARAM *functions, int count)
void SetScript(char *script, int script_size)
void SetTAG(TAG_PARAM *tags, int count)
int SearchCommand(int *tag_index)
SPI_FUNCTION function
int pos
Definition sysmes.hpp:13
char * data
Definition sysmes.hpp:11
int size
Definition sysmes.hpp:12