Mercurial > otakunoraifu
annotate scn2k/scn2k_cmd.cc @ 52:15a18fbe6f21
* Known bugs added to the README
* Code cleaning (0 -> NULL when needed, indentation, spaces, ...)
author | thib |
---|---|
date | Sat, 18 Apr 2009 18:35:39 +0000 |
parents | 5f548e5957a8 |
children | d7cde171a1de |
rev | line source |
---|---|
0 | 1 /* |
2 * Copyright (c) 2004-2006 Kazunori "jagarl" Ueno | |
3 * All rights reserved. | |
4 * | |
5 * Redistribution and use in source and binary forms, with or without | |
6 * modification, are permitted provided that the following conditions | |
7 * are met: | |
8 * 1. Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * 2. Redistributions in binary form must reproduce the above copyright | |
11 * notice, this list of conditions and the following disclaimer in the | |
12 * documentation and/or other materials provided with the distribution. | |
13 * 3. The name of the author may not be used to endorse or promote products | |
14 * derived from this software without specific prior written permission. | |
15 * | |
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
26 */ | |
27 | |
28 | |
52 | 29 #include "scn2k.h" |
0 | 30 |
52 | 31 #include <stdlib.h> |
32 #include <stdarg.h> | |
33 #include <stdio.h> | |
34 #include <string.h> | |
35 #include <string> | |
36 #include "system/file.h" | |
0 | 37 |
38 using namespace std; | |
39 | |
40 | |
41 // #define SCN_DUMP | |
42 /* Ãí°ÕÅÀ¡§ @@@ ¤Çɽµ */ | |
43 | |
44 | |
45 | |
46 //bool debug_flag = true; | |
47 bool debug_flag = false; | |
48 void dprintf(const char* fmt, ...) { | |
49 if (debug_flag) { | |
52 | 50 va_list ap; |
51 va_start(ap, fmt); | |
0 | 52 vprintf(fmt, ap); |
53 va_end(ap); | |
54 } | |
55 } | |
56 | |
57 | |
58 void eprintf(const char* fmt, ...) { | |
59 va_list ap; va_start(ap, fmt); | |
60 // vprintf(fmt, ap); | |
61 va_end(ap); | |
62 } | |
63 | |
64 /************************************************************** | |
65 ** Flag | |
66 */ | |
67 | |
68 Flags::Flags(void) { | |
69 int i,j; | |
70 for (i=0; i<=TYPE_VARMAX; i++) { | |
71 for (j=0; j<2000; j++) { | |
72 var[i][j] = 0; | |
73 } | |
74 } | |
75 sys = 0; | |
76 } | |
77 | |
78 bool Flags::IsInt(int type) const { | |
79 int v = type % 26; | |
43
01aa5ddf7dc8
A lot of very minor improvements (deleted some unused variables, and other things like that...)
thib
parents:
29
diff
changeset
|
80 return (v >= 0 && v < 7) || v == 25; |
0 | 81 } |
82 | |
83 int Flags::MaxIndex(int type) const { | |
84 switch (type / 26) { | |
85 case 1: | |
86 return 63999; | |
87 case 2: | |
88 return 31999; | |
89 case 3: | |
90 return 15999; | |
91 case 4: | |
92 return 7999; | |
93 default: | |
94 return 1999; | |
95 } | |
96 } | |
97 | |
98 int Flags::operator()() const { | |
99 return sys; // rand() % 10000; | |
100 } | |
101 | |
102 int Flags::operator() (VarInfo info) const { | |
103 return Get(info.type, info.number); | |
104 } | |
105 | |
106 int Flags::Get(int type, int number) const { | |
107 int index = type % 26; | |
108 type /= 26; | |
109 if (index == 25) { | |
110 if (var[7][number] != 0) return var[7][number]; | |
111 if (cgm_data.find(number) == cgm_data.end()) return 0; | |
112 else return 1; | |
113 } | |
114 if (index == 10) index = 8; | |
115 if (index == 11) index = 9; | |
116 if (index > TYPE_VARMAX || uint(type) > 4) return 0; | |
117 if (type == 0) { | |
118 // A[]..G[], Z[] ¤òľ¤ËÆɤà | |
119 if (uint(number) >= 2000) return 0; | |
120 return var[index][number]; | |
121 } else { | |
122 // Ab[]..G4b[], Z8b[] ¤Ê¤É¤òÆɤà | |
123 int factor = 1 << (type - 1); | |
124 int eltsize = 32 / factor; | |
125 if (uint(number) >= (64000 / factor)) return 0; | |
126 return (var[index][number / eltsize] >> ((number % eltsize) * factor)) & ((1 << factor) - 1); | |
127 } | |
128 } | |
129 | |
130 void Flags::Set(VarInfo info, int value) { | |
131 int type = info.type / 26; | |
132 int index = info.type % 26; | |
133 if (index == 25) { | |
134 if (uint(info.number) >= 2000) return; | |
135 if (value == 0) | |
136 cgm_data.erase(info.number); | |
137 else | |
138 cgm_data.insert(info.number); | |
139 index = 7; | |
140 } | |
141 if (index == 10) index = 8; | |
142 if (index == 11) index = 9; | |
143 if (index < 0 || index > TYPE_VARMAX) { | |
144 fprintf(stderr,"Error: invalid access to Var<%d>[%d]\n",info.type,info.number); | |
145 } | |
146 if (type == 0) { | |
147 // A[]..G[], Z[] ¤òľ¤Ë½ñ¤¯ | |
148 if (uint(info.number) >= 2000) return; | |
149 var[index][info.number] = value; | |
150 } else { | |
151 // Ab[]..G4b[], Z8b[] ¤Ê¤É¤ò½ñ¤¯ | |
152 int factor = 1 << (type - 1); | |
153 int eltsize = 32 / factor; | |
154 int eltmask = (1 << factor) - 1; | |
155 int shift = (info.number % eltsize) * factor; | |
156 if (uint(info.number) >= (64000 / factor)) return; | |
157 var[index][info.number / eltsize] = | |
158 (var[index][info.number / eltsize] & ~(eltmask << shift)) | |
159 | (value & eltmask) << shift; | |
160 } | |
161 } | |
162 | |
163 void Flags::SetSys(int value) { | |
164 sys = value; | |
165 } | |
52 | 166 |
0 | 167 void Flags::SetStr(VarInfo info, string val) { |
168 switch(info.type) { | |
52 | 169 case TYPE_VARLOCSTR: |
170 if (info.number >= 3) return; | |
171 loc_str[info.number] = val; | |
172 break; | |
173 case TYPE_VARSYSSTR: | |
174 if (info.number >= 2000) return; | |
175 sys_str[info.number] = val; | |
176 break; | |
177 case TYPE_VARSTR: | |
178 if (info.number >= 2000) return; | |
179 str[info.number] = val; | |
180 break; | |
0 | 181 } |
182 return; | |
183 } | |
52 | 184 |
0 | 185 void Flags::Str(int type, unsigned int number, char* buf, int sz) const { |
186 if (sz <= 0) return; | |
187 buf[0] = 0; | |
188 const string* sptr; | |
189 switch(type) { | |
52 | 190 case TYPE_VARLOCSTR: |
191 if (number >= 3) return; | |
192 sptr = &loc_str[number]; | |
193 break; | |
194 case TYPE_VARSYSSTR: | |
195 if (number >= 2000) return; | |
196 sptr = &sys_str[number]; | |
197 break; | |
198 case TYPE_VARSTR: | |
199 if (number >= 2000) return; | |
200 sptr = &str[number]; | |
201 break; | |
0 | 202 } |
203 | |
204 int len = sptr->length(); | |
205 if (sz-1 > len) sz = len; | |
206 sptr->copy(buf, sz, 0); | |
207 buf[sz] = 0; | |
208 return; | |
209 } | |
52 | 210 |
0 | 211 string Flags::Str(int type, unsigned int number) const { |
212 switch(type) { | |
213 case TYPE_VARLOCSTR: | |
214 if (number >= 3) return ""; | |
215 return loc_str[number]; | |
216 case TYPE_VARSYSSTR: | |
217 if (number >= 2000) return ""; | |
218 return sys_str[number]; | |
219 case TYPE_VARSTR: | |
220 if (number >= 2000) return ""; | |
221 return str[number]; | |
222 } | |
223 return ""; | |
224 } | |
225 | |
226 void Flags::Save(string& save) { | |
227 char buf[1024]; | |
228 save = "\n[Flags]\n"; | |
229 int i,j; | |
230 for (i=0; i<=TYPE_NONSYSVARMAX; i++) { | |
231 for (j=0; j<2000; j++) { | |
232 if (var[i][j] != 0) { | |
233 sprintf(buf, "V<%d>[%04d]=%d\n",i,j,var[i][j]); | |
234 save += buf; | |
235 } | |
236 } | |
237 } | |
238 for (j=0; j<2000; j++) { | |
239 if (str[j].length() != 0) { | |
240 sprintf(buf, "V<C>[%04d]=%s\n", j, str[j].c_str()); | |
241 save += buf; | |
242 } | |
243 } | |
244 } | |
52 | 245 |
0 | 246 void Flags::Load(const char* save) { |
247 int i,j; | |
248 for (i=0; i<=TYPE_NONSYSVARMAX; i++) { | |
249 for (j=0; j<2000; j++) { | |
250 var[i][j] = 0; | |
251 } | |
252 } | |
253 sys = 0; | |
254 for (j=0; j<2000; j++) { | |
255 str[j] = ""; | |
256 } | |
257 | |
258 save = strstr(save, "\n[Flags]\n"); | |
259 | |
260 if (save) { | |
261 save += strlen("\n[Flags]\n"); | |
262 do { | |
263 if (save[0] == '[') break; // next section | |
264 if (strncmp(save, "V<",2) == 0) { | |
265 if (strncmp(save, "V<C>[",5) == 0) { // string | |
266 char buf[1024]; | |
267 int n; | |
268 if (sscanf(save, "V<C>[%04d]=",&n) == 1) { | |
269 char* s = strchr(save, '='); | |
270 s++; | |
271 char* send = strchr(s, '\n'); | |
272 int slen = send - s; | |
273 strncpy(buf, s, slen); | |
274 buf[slen] = 0; | |
275 if (n >= 0 && n < 2000) str[n] = buf; | |
276 } | |
277 } else if (save[2] >= '0' && save[2] <= '9') { | |
278 int c,n,v; | |
279 if (sscanf(save, "V<%d>[%04d]=%d\n",&c,&n,&v) == 3) { | |
280 if (c >= 0 && c <= TYPE_NONSYSVARMAX && n >= 0 && n < 2000) | |
281 var[c][n] = v; | |
282 } | |
283 } | |
284 } | |
285 save = strchr(save, '\n'); | |
286 if (save) save++; | |
287 } while (save); | |
288 } | |
289 } | |
290 | |
52 | 291 void Flags::SaveSys(string& save) { //FIXME: see how to factorize with Save |
0 | 292 char buf[1024]; |
293 int j; | |
294 save = "\n[Flags]\n"; | |
295 for (j=0; j<2000; j++) { | |
296 if (var[6][j] != 0) { | |
297 sprintf(buf, "V<6>[%04d]=%d\n",j,var[6][j]); | |
298 save += buf; | |
299 } | |
300 } | |
301 for (j=0; j<2000; j++) { | |
302 if (var[7][j] != 0) { | |
303 sprintf(buf, "V<25>[%04d]=%d\n",j,var[7][j]); | |
304 save += buf; | |
305 } | |
306 } | |
307 for (j=0; j<2000; j++) { | |
308 if (sys_str[j].length() != 0) { | |
309 sprintf(buf, "V<M>[%04d]=%s\n", j, sys_str[j].c_str()); | |
310 save += buf; | |
311 } | |
312 } | |
313 } | |
52 | 314 |
315 void Flags::LoadSys(const char* save) { //FIXME: Same as Save and SaveSys | |
0 | 316 int i,j; |
317 for (i=6; i<=7; i++) { | |
318 for (j=0; j<2000; j++) { | |
319 var[i][j] = 0; | |
320 } | |
321 } | |
322 for (j=0; j<2000; j++) { | |
323 sys_str[j] = ""; | |
324 } | |
325 sys = 0; | |
326 | |
327 save = strstr(save, "\n[Flags]\n"); | |
328 | |
329 if (save) { | |
330 save += strlen("\n[Flags]\n"); | |
331 do { | |
332 if (save[0] == '[') break; // next section | |
333 if (strncmp(save, "V<",2) == 0) { | |
334 if (strncmp(save, "V<M>[",5) == 0) { // string | |
335 char buf[1024]; | |
336 int n; | |
337 if (sscanf(save, "V<M>[%04d]=",&n) == 1) { | |
338 char* s = strchr(save, '='); | |
339 s++; | |
340 char* send = strchr(s, '\n'); | |
341 int slen = send - s; | |
342 strncpy(buf, s, slen); | |
343 buf[slen] = 0; | |
344 if (n >= 0 && n < 2000) sys_str[n] = buf; | |
345 } | |
346 } else if (save[2] >= '0' && save[2] <= '9') { | |
347 int c,n,v; | |
348 if (sscanf(save, "V<%d>[%04d]=%d\n",&c,&n,&v) == 3) { | |
349 if (c == 6 && n >= 0 && n < 2000) | |
350 var[6][n] = v; | |
351 else if (c == 25 && n >= 0 && n < 2000) | |
352 var[7][n] = v; | |
353 } | |
354 } | |
355 } | |
356 save = strchr(save, '\n'); | |
357 if (save) save++; | |
358 } while (save); | |
359 } | |
360 } | |
361 | |
362 bool Flags::Exec(Cmd& cmd) { | |
363 if (cmd.cmd_type == CMD_FLAGS) { // ÂåÆþ±é»» | |
364 if (cmd.args.size() != 2) return false; | |
365 Set(cmd.args[0], cmd.args[1].value); | |
366 cmd.clear(); | |
367 return true; | |
368 } | |
369 if (cmd.cmd1 == 1 && cmd.cmd2 == 0x0a) { // ʸ»úÎó±é»» | |
370 VarInfo arg1 = cmd.args[0]; | |
371 switch(cmd.cmd3) { | |
52 | 372 case 0: |
373 if (cmd.cmd4 == 0) { | |
374 SetStr(arg1, cmd.Str(cmd.args[1])); | |
375 } else if (cmd.cmd4 == 1) { | |
376 string s = cmd.Str(cmd.args[1]); | |
377 const char* sc = s.c_str(); | |
378 int len = cmd.args[2].value; | |
379 int i; | |
380 for (i=0; i < sc[i] && len != 0; i++, len--) { | |
381 if (sc[i]<0 && sc[i+1]!=0) i++; | |
382 } | |
383 s.erase(i); // Á´³Ñ¤Ç len ʸ»ú¤Þ¤ÇÀÚ¤êµÍ¤á¤ë | |
384 SetStr(arg1, s); | |
385 // fprintf(stderr,"Set[%d,%d]<-%s\n",arg1.type,arg1.number,s.c_str()); | |
386 } else break; | |
387 cmd.clear(); | |
388 break; | |
389 case 1: | |
390 if (cmd.cmd4 == 0) { | |
391 SetStr(arg1, ""); | |
392 cmd.clear(); | |
393 } else if (cmd.cmd4 == 1) { | |
394 // Îΰè»ØÄê¤Çʸ»úÎ󥯥ꥢ | |
395 VarInfo v1 = cmd.args[0]; | |
396 VarInfo v2 = cmd.args[1]; | |
397 eprintf("memclear(str). Var[%d]<%d> - Var[%d]<%d>\n",v1.type, v1.number, v2.type, v2.number); | |
398 if (v1.type != v2.type || (v1.type != TYPE_VARSTR && v1.type != TYPE_VARSYSSTR && v1.type != TYPE_VARLOCSTR)) { | |
399 eprintf(" error: bad args\n"); | |
400 } else { | |
401 if (v1.number < 0) v1.number = 0; | |
402 if (v2.number > 2000) v2.number = 2000; | |
403 for (; v1.number <= v2.number; v1.number++) { | |
404 SetStr(v1, ""); | |
405 } | |
406 } | |
407 cmd.clear(); | |
0 | 408 } |
52 | 409 case 2: |
410 SetStr(arg1, Str(arg1.type,arg1.number) + cmd.Str(cmd.args[1])); | |
411 // fprintf(stderr,"Append[%d,%d]<-%s(%d:%d)\n",arg1.type,arg1.number,Str(arg1.type,arg1.number).c_str(),cmd.args[1].type,cmd.args[1].number); | |
412 cmd.clear(); | |
413 break; | |
414 case 3: | |
415 SetSys(strlen(cmd.Str(cmd.args[0]))); | |
416 cmd.clear(); | |
417 break; | |
418 case 4: | |
419 { int v = strcmp(cmd.Str(cmd.args[0]), cmd.Str(cmd.args[1])); | |
420 // string s1=cmd.Str(cmd.args[0]); | |
421 // string s2=cmd.Str(cmd.args[1]); | |
422 // fprintf(stderr,"Cmp %s(%d:%d):%s(%d:%d):%d\n",s1.c_str(),cmd.args[0].type,cmd.args[0].number,s2.c_str(),cmd.args[1].type,cmd.args[1].number,v); | |
423 if (v < 0) SetSys(-1); | |
424 else if (v > 0) SetSys(1); | |
425 else SetSys(0); | |
0 | 426 cmd.clear(); |
52 | 427 break; } |
428 case 5: // substring, index from left | |
429 case 6: // substring, index from right | |
430 // Á´³ÑÂбþ¤é¤·¤¤ | |
431 //FIXME: Make sure it works properly | |
432 { int offset = cmd.args[2].value; | |
433 int len = strlen(cmd.Str(cmd.args[1])); | |
434 string str = cmd.Str(cmd.args[1]); | |
435 const char* s = str.c_str(); | |
436 if (cmd.cmd3 == 6) offset = len - offset; | |
437 if (offset < 0) offset = 0; | |
438 // ÀèƬ N ʸ»ú¤òÆɤßÈô¤Ð¤¹ | |
439 int i; | |
440 int offset_top = 0; | |
441 for (i=0; i<offset && s[offset_top] != 0; i++) { | |
442 if (s[offset_top] < 0 && s[offset_top+1] != 0) offset_top += 2; | |
443 else offset_top += 1; | |
444 } | |
445 if (s[offset_top] == 0) { | |
446 SetStr(arg1, ""); | |
447 } else if (cmd.cmd4 == 0) { // ŤµÀ©¸Â¤Ê¤· | |
448 SetStr(arg1, string(s, offset_top, len-offset_top)); | |
449 } else { // cmd.cmd4 == 1 | |
450 int slen = cmd.args[3].value; | |
451 int offset_end = offset_top; | |
452 for (i=0; i<slen && s[offset_end] != 0; i++) { | |
453 if (s[offset_end] < 0 && s[offset_end]+1 != 0) offset_end += 2; | |
454 else offset_end += 1; | |
0 | 455 } |
52 | 456 string result(s, offset_top, offset_end-offset_top); |
457 SetStr(arg1, result); | |
0 | 458 } |
459 cmd.clear(); | |
52 | 460 break; } |
461 case 7: {// strlen w/ kanji | |
462 const char* s = cmd.Str(cmd.args[0]); int i; | |
463 for (i=0; *s != 0; i++) { | |
464 if (*s < 0 && s[1] != 0) s += 2; | |
465 else s++; | |
466 } | |
467 SetSys(i); | |
468 cmd.clear(); | |
469 break; } | |
470 case 8: // ʸ»úÎó¤òÀڤäÆû¤¯¤¹¤ë | |
471 if (cmd.args[1].value <= 0) { | |
472 SetStr(arg1, ""); | |
473 } else if (cmd.args[1].value < strlen(cmd.Str(cmd.args[1]))) { | |
474 Str(arg1.type,arg1.number).erase(cmd.args[1].value); | |
475 } | |
476 cmd.clear(); | |
477 break; | |
478 case 0x0e: // ´Á»ú¥â¡¼¥É¤Çitoa | |
479 { | |
480 int arg1 = cmd.args[0].value; | |
481 string result; | |
482 char wc[3]; wc[2]=0; | |
483 char buf[20]; | |
484 if (cmd.cmd4 == 0) { | |
485 sprintf(buf, "%d", arg1); | |
486 } else { // cmd.cmd4 == 1 | |
487 char fmt[20]; | |
488 sprintf(fmt, "%%%dd", cmd.args[2].value); | |
489 sprintf(buf, fmt, arg1); | |
0 | 490 } |
52 | 491 int i; |
492 for (i=0; buf[i] != 0; i++) { | |
493 if (buf[i] == ' ') { | |
494 wc[0] = 0x81; // ' ' in SHIFT_JIS | |
495 wc[1] = 0x40; | |
496 } else if (buf[i] == '-') { | |
497 wc[0] = 0x81; // '-' in SHIFT_JIS | |
498 wc[1] = 0x7c; | |
499 } else if (isdigit(buf[i])) { | |
500 wc[0] = 0x82; // number in SHIFT_JIS | |
501 wc[1] = buf[i] - '0' + 0x4f; | |
502 } else { | |
503 continue; | |
504 } | |
505 result += wc; | |
0 | 506 } |
52 | 507 SetStr(cmd.args[1], result); |
0 | 508 cmd.clear(); |
509 } | |
52 | 510 break; |
511 case 0x0f: case 0x11: // itoa (0x11 ¤ÎÊý¤Ï zero padding ¤¹¤ë¤Ã¤Ý¤¤) | |
512 if (cmd.cmd4 == 0) { | |
513 int arg1 = cmd.args[0].value; | |
514 char buf[1024]; sprintf(buf, "%d", arg1); | |
515 SetStr(cmd.args[1], buf); | |
516 cmd.clear(); | |
517 } else if (cmd.cmd4 == 1) { | |
518 // ´Á»ú(SJIS) : 82 [4f+N] | |
519 // ¤ä¤Ï¤ê´Á»ú¤¸¤ã¤Ê¤¤¡© | |
520 int arg1 = cmd.args[0].value; | |
521 char buf[1024]; char fmt[1024]; | |
522 if (cmd.cmd3 == 0x0f) { | |
523 sprintf(fmt, "%%%dd",cmd.args[2].value); /* ¶õÇò¤Ç¥Ñ¥Ç¥£¥ó¥° */ | |
524 } else { | |
525 sprintf(fmt, "%%0%dd",cmd.args[2].value); | |
526 } | |
527 sprintf(buf, fmt, arg1); | |
528 SetStr(cmd.args[1], buf); | |
529 cmd.clear(); | |
530 } | |
531 break; | |
532 case 0x64: // ʸ»úÎó¤Îɽ¼¨ : °ú¿ô¤ò¥Æ¥¥¹¥È¥¦¥£¥ó¥É¥¦¤Ëɽ¼¨ | |
533 if (cmd.cmd4 == 1) { | |
534 char buf[256]; | |
535 snprintf(buf, 255, "%d", Get(cmd.args[0].type, cmd.args[0].number)); | |
536 cmd.args[0].type = TYPE_STR; | |
537 cmd.args[0].value = cmd.AddStr(buf); | |
538 cmd.cmd4 = 0; | |
539 } | |
540 cmd.cmd_type = CMD_TEXT; | |
541 break; | |
0 | 542 } |
543 } | |
544 if (cmd.cmd1 == 1 && cmd.cmd2 == 0x0b) { // ¿ôÃÍÊÑ¿ô±é»» | |
545 if (cmd.cmd3 == 0 && cmd.cmd4 == 0) { | |
546 /* Ê£¿ô¤ÎÊÑ¿ô¤ò¥»¥Ã¥È */ | |
547 VarInfo v1 = cmd.args[0]; | |
548 eprintf("set multiple-var Var[%d]<%d> <- ",v1.type, v1.number); | |
549 int i; | |
550 if (cmd.args.size() < cmd.argc) { | |
551 eprintf(" error: argsize changed %d -> %d\n",cmd.argc, cmd.args.size()); | |
552 cmd.argc = cmd.args.size(); | |
553 } | |
554 for (i=0; i<cmd.argc; i++) { | |
555 eprintf("%d, ",cmd.args[i+1].value); | |
556 Set(v1, cmd.args[i+1].value); | |
557 v1.number++; | |
558 } | |
559 eprintf("\n"); | |
560 cmd.clear(); | |
561 } else if (cmd.cmd3 == 1 && cmd.cmd4 == 0) { | |
562 /* Îΰè»ØÄê¤ÇÊÑ¿ô¤ò¥¯¥ê¥¢ */ | |
563 VarInfo v1 = cmd.args[0]; | |
564 VarInfo v2 = cmd.args[1]; | |
565 eprintf("memclear. Var[%d]<%d> - Var[%d]<%d>\n",v1.type, v1.number, v2.type, v2.number); | |
566 if (v1.type != v2.type || !IsInt(v1.type)) eprintf(" error: bad args\n"); | |
567 else { | |
568 if (v1.number < 0) v1.number = 0; | |
569 if (v2.number > MaxIndex(v2.type)) v2.number = MaxIndex(v2.type); | |
570 for (; v1.number <= v2.number; v1.number++) | |
571 Set(v1, 0); | |
572 } | |
573 cmd.clear(); | |
574 } else if (cmd.cmd3 == 1 && cmd.cmd4 == 1) { | |
575 /* Îΰè»ØÄê¤ÇÊÑ¿ô¤ò¥»¥Ã¥È */ | |
576 VarInfo v1 = cmd.args[0]; | |
577 VarInfo v2 = cmd.args[1]; | |
578 int value = cmd.args[2].value; | |
579 eprintf("memset. Var[%d]<%d> - Var[%d]<%d> <- %d\n",v1.type, v1.number, v2.type, v2.number, value); | |
580 if (v1.type != v2.type || !IsInt(v1.type)) eprintf(" error: bad args\n"); | |
581 else { | |
582 if (v1.number < 0) v1.number = 0; | |
583 if (v2.number > MaxIndex(v2.type)) v2.number = MaxIndex(v2.type); | |
584 for (; v1.number <= v2.number; v1.number++) | |
585 Set(v1, value); | |
586 } | |
587 cmd.clear(); | |
588 } else if (cmd.cmd3 == 4 && cmd.cmd4 == 1) { // Îΰ襯¥ê¥¢(sysfunc.txt) | |
589 VarInfo v1 = cmd.args[0]; | |
590 int step = cmd.args[1].value; | |
591 int deal = cmd.args[2].value; | |
592 int val = cmd.args[3].value; | |
593 eprintf("memclear. Var[%d]<%d> step %d deal %d <- val %d\n",v1.type, v1.number, step, deal, val); | |
594 int i; for (i=0; i<deal; i++) { | |
595 Set(v1, val); | |
596 v1.number += step; | |
597 } | |
598 cmd.clear(); | |
599 } else if (cmd.cmd3 == 0x64 && cmd.cmd4 == 0) { //Îΰè¤Ç¿ôÃͤò¹ç·×¤¹¤ë | |
600 VarInfo v1 = cmd.args[0]; | |
601 VarInfo v2 = cmd.args[1]; | |
602 eprintf("sum var. Var[%d]<%d> - Var[%d]<%d>\n",v1.type, v1.number, v2.type, v2.number); | |
603 int sum = 0; | |
604 if (v1.type != v2.type || !IsInt(v1.type)) eprintf(" error: bad args\n"); | |
605 else { | |
606 if (v1.number < 0) v1.number = 0; | |
607 if (v2.number > MaxIndex(v2.type)) v2.number = MaxIndex(v2.type); | |
608 for (; v1.number <= v2.number; v1.number++) | |
609 sum += (*this)(v1); | |
610 } | |
611 eprintf(" ret %d\n",sum); | |
612 cmd.SetSysvar(sum); | |
613 } | |
614 } | |
615 return false; | |
616 } | |
617 | |
618 /********************************************************************* | |
619 ** Cmd | |
620 */ | |
621 | |
622 /* ¿ôÃÍ num := 0x24 0xff <int num> */ | |
623 /* ÊÑ¿ô var := 0x24 <uchar type> 0x5b <exp> 0x5d */ | |
624 /* ¹à token := num | var | 0x28 <exp> 0x29 | <plus|minus> token */ | |
625 | |
626 int Cmd::GetLeftToken(const char*& d, VarInfo& info) { | |
627 bool var_flag = true; | |
628 int minus_flag = 0; | |
629 int value = 0; | |
630 if (d[0] == 0x5c && (d[1] == 1 || d[1] == 0) ) { | |
631 if (d[1] == 1) {dprintf("minus-"); minus_flag ^= 1;} | |
632 else dprintf("plus-"); | |
633 d += 2; | |
634 var_flag = false; | |
635 } | |
636 if (d[0] == 0x24 && ((unsigned const char*)d)[1] == 0xff) { | |
637 // if ( (d[0] == 0x30 || d[0] == 0x31) && d[1] == 0x24 && ((unsigned const char*)d)[2] == 0xff) /* @@@ not supported; selection Æâ¤Ç¡¢0x30|0x31 ¤¬Éտ魯¤ë¤³¤È¤¬¤¢¤ë */ | |
638 // numerical atom | |
639 d += 6; | |
640 value = read_little_endian_int(d-4); | |
641 dprintf("%d",value); | |
642 var_flag = false; | |
643 } else if (d[0] == 0x24 && *(unsigned char*)(d+1) == 0xc8) { | |
644 dprintf("V<sys>"); | |
645 d += 2; | |
646 info.type = TYPE_SYS; info.number = 0; | |
647 value = info.value = flags(); | |
648 } else if (d[0] == 0x24 && d[2] == 0x5b) { | |
649 // 0x24,<type>,0x5b,<expr>,0x5d-terminated term | |
650 info.type = *(unsigned char*)(d+1); | |
651 d += 3; | |
652 dprintf("V<%d>[",info.type); | |
653 info.number = GetExpression(d); | |
654 dprintf("]"); | |
655 if (*d == 0x5d) d++; | |
656 else SetError(); | |
657 if (info.type == TYPE_VARSTR || info.type == TYPE_VARSYSSTR || info.type == TYPE_VARLOCSTR) { | |
658 value = 0; | |
659 info.value = StrVar(info.type, info.number); | |
660 } else { | |
661 value = info.value = flags(info); | |
662 } | |
663 dprintf("(=%d)",value); | |
664 } else SetError(); | |
665 | |
666 if (minus_flag) value = -value; | |
667 if (!var_flag) { | |
668 info.type = TYPE_VAL; | |
669 info.value = value; | |
670 } | |
671 return value; | |
672 } | |
673 | |
47
5f548e5957a8
* get rid of the "deprecated conversion from string constant to ‘char*’" warnings
thib
parents:
44
diff
changeset
|
674 static const char* op_str[70] = { |
0 | 675 // 0 1 2 3 4 5 6 7 8 9 |
676 "+", "-", "*", "/", "%", "&", "|", "^", "<<", ">>", // +00 | |
677 "err.","err.","err.","err.","err.","err.","err.","err.","err.","err.", // +10 | |
678 "+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "<<=", ">>=", // +20 | |
679 "=", "err.","err.","err.","err.","err.","err.","err.","err.","err.", // +30 | |
680 "==", "!=", "<=", "<", ">=", ">", "err.","err.","err.","err.", // +40 | |
681 "err.","err.","err.","err.","err.","err.","err.","err.","err.","err.", // +50 | |
682 "&&", "||", "err.","err.","err.","err.","err.","err.","err.","err.", // +60 | |
683 }; | |
684 | |
685 static int op_pri_tbl[12] = { | |
686 // + - * / % & | ^ << >> | |
687 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 10, 10}; | |
688 | |
689 inline int op_pri(int op) { | |
690 if (op > 11) return 10; | |
691 return op_pri_tbl[op]; | |
692 } | |
693 inline int op_pri_cond(int op) { | |
694 if (op <= 11) return op_pri_tbl[op]; | |
695 else if (op < 50) return 7; | |
696 else if (op == 60) return 8; | |
697 else if (op == 61) return 8; | |
698 else return 10; | |
699 } | |
700 | |
701 | |
702 inline int eval(int v1, int op, int v2) { | |
703 switch(op) { | |
704 case 0: return v1+v2; | |
705 case 1: return v1-v2; | |
706 case 2: return v1*v2; | |
707 case 3: return v2!=0 ? v1/v2 : v1; | |
708 case 4: return v2!=0 ? v1%v2 : v1; | |
709 case 5: return v1&v2; | |
710 case 6: return v1|v2; | |
711 case 7: return v1^v2; | |
712 case 8: return v1<<v2; | |
713 case 9: return v1>>v2; | |
714 case 40: return v1 == v2; | |
715 case 41: return v1 != v2; | |
716 case 42: return v1 <= v2; | |
717 case 43: return v1 < v2; | |
718 case 44: return v1 >= v2; | |
719 case 45: return v1 > v2; | |
720 case 60: return v1 && v2; | |
721 case 61: return v1 || v2; | |
722 } | |
723 return v2; | |
724 } | |
725 | |
52 | 726 Cmd::Cmd(const Flags& f, int _sys_ver) : flags(f), system_version(_sys_ver) { |
727 cmd_type = CMD_NOP; | |
728 argc = 0; | |
729 errorflag = false; | |
730 cmdstr[0] = 0; | |
731 strend = 0; | |
732 pos = -1; | |
733 } | |
734 | |
0 | 735 /* ±é»»»Ò op := 0x5c <uchar op> */ |
736 /* ¿ô¼° exp: [op] <token> [op <token> [...]] */ | |
737 int Cmd::GetExpression(const char*& d, VarInfo* info_ptr) { | |
738 #define STACK_DEPTH 1024 | |
739 #define OP_LB 11 | |
740 char op_stack[STACK_DEPTH]; | |
741 int val_stack[STACK_DEPTH]; | |
742 int stack_count = 0; | |
743 | |
744 // Âè°ì¹à¤ÎÆɤ߹þ¤ß | |
745 while(*d == 0x28) { | |
746 d++; | |
747 dprintf("("); | |
748 op_stack[stack_count++] = OP_LB; | |
749 } | |
750 VarInfo info; | |
751 int value = GetLeftToken(d, info); | |
752 | |
753 while(*d == 0x29 && stack_count > 0 && op_stack[stack_count-1] == OP_LB) { | |
754 d++; | |
755 dprintf(")"); | |
756 stack_count--; | |
757 } | |
758 | |
759 if (*d != 0x5c && stack_count == 0) { | |
760 if (info_ptr) *info_ptr = info; | |
761 return value; // ñ½ã¤Êleft-term¤Ï¤³¤³¤Ç½ªÎ»¡£Í¸ú¤Êinfo_ptr¤òµ¢¤¹¡Ê²ÄǽÀ¤¬¤¢¤ë¡Ë | |
762 } | |
763 | |
764 while(*d == 0x5c) { | |
765 int op_type = *(unsigned char*)(d+1); | |
766 d += 2; | |
767 if (op_type < 70) dprintf("%s",op_str[op_type]); | |
768 else dprintf("err."); | |
769 if (op_type >= 10) SetError(); | |
770 int cur_pri = op_pri(op_type); | |
771 while(stack_count != 0 && op_pri(op_stack[stack_count-1]) <= cur_pri) { | |
772 // Í¥Àè½ç°Ì¤Î¹â¤¤¡¢Àè¹Ô¤¹¤ë±é»»¤ò¹Ô¤¦ | |
773 value = eval(val_stack[stack_count-1], op_stack[stack_count-1], value); | |
774 stack_count--; | |
775 } | |
776 val_stack[stack_count] = value; | |
777 op_stack[stack_count++] = op_type; | |
778 while(*d == 0x28) { | |
779 d++; | |
780 dprintf("("); | |
781 op_stack[stack_count++] = OP_LB; | |
782 } | |
783 if (stack_count >= STACK_DEPTH) SetError(); | |
784 value = GetLeftToken(d, info); | |
785 | |
786 while (*d != 0x5c && stack_count > 0) { | |
787 // ̤¼Â¹Ô¤Î±é»»¤ò½ª¤ï¤é¤»¤ë | |
788 if (op_stack[stack_count-1] != OP_LB) { | |
789 value = eval(val_stack[stack_count-1], op_stack[stack_count-1], value); | |
790 stack_count--; | |
791 } else if (*d == 0x29) { /* op_stack == OP_LB */ | |
792 // bracket ½ªÃ¼¤¬¤¢¤ì¤Ð¡¢ÊĤ¸¤Æ¤ª¤¯ | |
793 d++; | |
794 dprintf(")"); | |
795 stack_count--; | |
796 } else break; // error | |
797 } | |
798 } | |
799 if (stack_count) SetError(); // unbalanced bracket | |
800 dprintf("(=%d)",value); | |
801 if (info_ptr) { | |
802 info_ptr->type = TYPE_VAL; | |
803 info_ptr->value = value; | |
804 } | |
805 return value; | |
806 } | |
807 | |
808 // ¾ò·ïʬ´ôÀìÍѤˡ¢¾ò·ï±é»»¤È»»½Ñ±é»»¤Îº®¹ç¤ò¸¡ÃΤǤ¤ëÀìÍѥ롼¥Á¥ó¡ÊËÜÍè¤ÏGetExpression¤Çº¹¤·»Ù¤¨¤Ê¤¤) | |
809 int Cmd::GetExpressionCond(const char*& d) { | |
810 char op_stack[STACK_DEPTH]; | |
811 int val_stack[STACK_DEPTH]; | |
812 int valattr_stack[STACK_DEPTH]; | |
813 #define ATTR_VAL 0 | |
814 #define ATTR_FLAG 1 | |
815 int stack_count = 0; | |
816 | |
817 // Âè°ì¹à¤ÎÆɤ߹þ¤ß | |
818 while(*d == 0x28) { | |
819 d++; | |
820 dprintf("("); | |
821 op_stack[stack_count++] = OP_LB; | |
822 } | |
823 VarInfo info; | |
824 int value = GetLeftToken(d, info); | |
825 while(*d == 0x29 && stack_count > 0 && op_stack[stack_count-1] == OP_LB) { | |
826 d++; | |
827 dprintf(")"); | |
828 stack_count--; | |
829 } | |
830 bool valattr = ATTR_VAL; | |
831 | |
832 while(*d == 0x5c) { | |
833 int op_type = *(unsigned char*)(d+1); | |
834 d += 2; | |
835 if (op_type < 70) dprintf("%s",op_str[op_type]); | |
836 else dprintf("err."); | |
837 int cur_pri = op_pri_cond(op_type); | |
838 while(stack_count != 0 && op_pri_cond(op_stack[stack_count-1]) <= cur_pri) { | |
839 // Í¥Àè½ç°Ì¤Î¹â¤¤¡¢Àè¹Ô¤¹¤ë±é»»¤ò¹Ô¤¦ | |
840 if (op_stack[stack_count-1] >= 60) { | |
841 if (valattr_stack[stack_count-1] != ATTR_FLAG || valattr != ATTR_FLAG) SetError(); | |
842 } else { | |
843 if (valattr_stack[stack_count-1] != ATTR_VAL || valattr != ATTR_VAL) SetError(); | |
844 } | |
845 value = eval(val_stack[stack_count-1], op_stack[stack_count-1], value); | |
846 if (op_stack[stack_count-1] >= 40) valattr = ATTR_FLAG; | |
847 stack_count--; | |
848 } | |
849 val_stack[stack_count] = value; | |
850 valattr_stack[stack_count] = valattr; | |
851 op_stack[stack_count++] = op_type; | |
852 while(*d == 0x28) { | |
853 d++; | |
854 dprintf("("); | |
855 op_stack[stack_count++] = OP_LB; | |
856 } | |
857 if (stack_count >= STACK_DEPTH) SetError(); | |
858 value = GetLeftToken(d, info); | |
859 valattr = ATTR_VAL; | |
860 | |
861 while (*d != 0x5c && stack_count > 0) { | |
862 // ̤¼Â¹Ô¤Î±é»»¤ò½ª¤ï¤é¤»¤ë | |
863 if (op_stack[stack_count-1] != OP_LB) { | |
864 if (op_stack[stack_count-1] >= 60) { | |
865 if (valattr_stack[stack_count-1] != ATTR_FLAG || valattr != ATTR_FLAG) SetError(); | |
866 } else { | |
867 if (valattr_stack[stack_count-1] != ATTR_VAL || valattr != ATTR_VAL) SetError(); | |
868 } | |
869 value = eval(val_stack[stack_count-1], op_stack[stack_count-1], value); | |
870 if (op_stack[stack_count-1] >= 40) valattr = ATTR_FLAG; | |
871 stack_count--; | |
872 // bracket ½ªÃ¼¤¬¤¢¤ì¤Ð¡¢ÊĤ¸¤Æ¤ª¤¯ | |
873 } else if (*d == 0x29) { /* op_stack == OP_LB */ | |
874 d++; | |
875 dprintf(")"); | |
876 stack_count--; | |
877 } else break; // error | |
878 } | |
879 } | |
880 if (stack_count) SetError(); // unbalanced bracket | |
881 if (value) dprintf("(=true)"); | |
882 else dprintf("(=false)"); | |
883 return value; | |
884 } | |
885 | |
886 | |
887 /* | |
888 str = | |
889 arg = | |
890 args = 0x28 <exp> [[0x2c] <exp> [[0x2c] <exp> [...] ]] | |
891 */ | |
892 | |
893 int Cmd::GetArgs(const char*& d) { | |
894 if (*d != 0x28) return 0; /* °ú¿ô¤Ê¤· */ | |
895 d++; | |
896 dprintf("args:"); | |
897 VarInfo var; | |
898 int i; for (i=0; i<100 ; i++) { | |
899 /* number, variable, string ¤Î¼ïÊ̤ʤ¯ÃͤòÆÀ¤ë */ | |
900 if (*d == 0x61) { // ¤è¤¯¤ï¤«¤é¤Ê¤¤(ÃÒÂ奢¥Õ¥¿¡¼) | |
29
d229cce98f50
* no more (or, at least, less) duplicate code between scn2kdump and the rest
thib
parents:
21
diff
changeset
|
901 dprintf("@%d",d[1]); |
0 | 902 d += 2; |
903 if (*d == 0x28) { | |
904 dprintf("{"); | |
905 GetArgs(d); // (A,B,C)À᤬´Þ¤Þ¤ì¤ë¤³¤È¤¬¤¢¤ë | |
906 dprintf("}"); | |
907 } else { | |
908 dprintf("{}"); | |
909 } | |
910 } else if (d[0] == 0x0a || d[0] == 0x40) { // ¤è¤¯¤ï¤«¤é¤Ê¤¤ (Little Busters!) | |
911 int var; | |
912 if (system_version == 0) { var = read_little_endian_int(d+1); d += 5;} | |
913 else { var = read_little_endian_short(d+1); d += 3;} | |
914 dprintf("line %d; ",var); | |
915 } else if (*d == 0x24 || (*d == 0x5c && (d[1] == 1 || d[1] == 0)) || *d == 0x28) { | |
916 GetExpression(d, &var); | |
917 args.push_back(var); | |
918 } else if (StrType(d)) { | |
919 var.type = TYPE_STR; | |
920 var.value = GetString(d); | |
921 args.push_back(var); | |
922 } else SetError(); | |
923 if (*d == 0x29) break; | |
924 if (*d == 0x2c) {d++;} // ¼¡¤Î arg ¤¬±é»»»Ò¤Ç»Ï¤Þ¤ë¡¢¤Ê¤É¤¬¤Ê¤±¤ì¤Ð¸ºß¤·¤Ê¤¤ | |
925 dprintf(","); | |
926 } | |
927 if (*d == 0x29) d++; | |
928 else SetError(); | |
929 return i; | |
930 } | |
931 | |
932 int Cmd::GetArgsSpecial(int normal_args,const char*& d) { | |
933 if (*d != 0x28) return 0; /* °ú¿ô¤Ê¤· */ | |
934 d++; | |
935 dprintf("args:"); | |
936 int i; for (i=0; i<normal_args; i++) { | |
937 /* number, variable, string ¤Î¼ïÊ̤ʤ¯ÃͤòÆÀ¤ë */ | |
938 if (*d == 0x24 || (*d == 0x5c && (d[1] == 1 || d[1] == 0)) || *d == 0x28) { | |
939 GetExpression(d); | |
940 } else if (StrType(d)) { | |
941 GetString(d); | |
942 } else SetError(); | |
943 if (*d == 0x29) break; | |
944 if (*d == 0x2c) {d++;} // ¼¡¤Î arg ¤¬±é»»»Ò¤Ç»Ï¤Þ¤ë¡¢¤Ê¤É¤¬¤Ê¤±¤ì¤Ð¸ºß¤·¤Ê¤¤ | |
945 dprintf(","); | |
946 } | |
947 for (i=0; i<argc ; i++) { | |
948 if (*d == 0x28) { | |
949 /* | |
950 ** cmd 01-22:0c1c, 01-22:0835 | |
951 ** Princess Bride ¤Î¥«¡¼¥É¤¬Íî¤Á¤ë¥¢¥Ë¥á¤Î¾ìÌÌ | |
952 ** ¤Ê¤ª¡¢_PBCARDANM* ¤Î²èÁü¤Ï¤³¤Î¥³¥Þ¥ó¥É¤Ç¤Î¤ß»È¤ï¤ì¤Æ¤¤¤ë¤Î¤Ç¡¢Æüì½èÍý¤È¤·¤Æ̵»ë¤¹¤ë¤³¤È¤â²Äǽ | |
953 ** | |
954 ** cmd 01-04:0276, 026c, 0270 | |
955 ** Ê£¿ô¤Î enum ¤¬ args ¤Î¿ô¤À¤±Â³¤¯½èÍý¡£Æüì½èÍý¤È¤·¤ÆʬΥ¤¹¤ë | |
956 */ | |
957 dprintf("enum.<"); | |
958 /* (...) ¤ÏÎóµó·¿ or ¹½Â¤ÂΤβÄǽÀ¤¬¤¢¤ë */ | |
959 const char* d_orig = d; | |
960 int pt = args.size(); args.push_back(VarInfo(0)); | |
961 int count = GetArgs(d); | |
962 args[pt] = VarInfo(count); | |
963 dprintf(">"); | |
964 } else if (*d == 0x61 && (d[1] >= 0x00 && d[1] <= 0x04) && d[2] == 0x28 ) { | |
965 /* »È¤ï¤ì¤ë¥³¥Þ¥ó¥É¤Ï 01-21:004b, 01-28:0064 ¤Î¤¤¤º¤ì¤«¡ÊR,C,PB,LO) | |
966 ** ¤½¤ì¤é¤Î¥³¥Þ¥ó¥É¤Ï | |
967 ** arg1: ²èÁü¥Õ¥¡¥¤¥ë̾ | |
968 ** arg2 : Sel ÈÖ¹æ | |
969 ** ¤é¤·¤¯¡¢arg3 °Ê¹ß¤¬ 0x61 <00-04> (a,b,c,...) ¤È¤Ê¤ë¡Ê¥À¥ó¥×¾å¤Ï enum ¤Èɽµ¤µ¤ì¤ë) | |
970 ** () Æâ¤Î°ú¿ô¤Ï¤µ¤Þ¤¶¤Þ¤Ç¡¢a ¤Î¤ß¡Ê²èÁü¥Õ¥¡¥¤¥ë̾¡Ë¡¢ | |
971 ** a,b b=SEL? | |
972 ** a,b,c (b,c)=ºÂɸ¡© | |
973 ** a,(b,c,d,e,f,g) b-g = src / dest? | |
974 ** ¤é¤·¤¤ | |
975 */ | |
976 dprintf("kasane. #%d <",d[1]); | |
977 d += 2; | |
978 int pt = args.size(); args.push_back(VarInfo(0)); | |
979 int count = GetArgs(d); | |
980 args[pt] = VarInfo(count); | |
981 dprintf(">"); | |
982 } else if (*d == 0x24 || (*d == 0x5c && (d[1] == 1 || d[1] == 0))) { | |
983 /* cmd 01-15:0028 ; »Ï¤á¤Ë 0x24 À᤬¤¢¤ê¡¢Â³¤¤¤Æ 0x28 Àá¤Ë¤Ê¤ë */ | |
984 VarInfo var; | |
985 GetExpression(d, &var); | |
986 args.push_back(var); | |
987 i--; // ¤³¤Î°ú¿ô¤Ïargc ¤Î¿ô¤Ë¤ÏÆþ¤é¤Ê¤¤ | |
988 } else SetError(); | |
989 if (d[0] == 0x0a || d[0] == 0x40) { | |
990 /* cmd 01-15:0028 ; 0x28 Àá¤Î¸å¤ËËè²ó 0x0a À᤬Íè¤ë */ | |
991 int var; | |
992 if (system_version == 0) { var = read_little_endian_int(d+1); d += 5;} | |
993 else { var = read_little_endian_short(d+1); d += 3;} | |
994 dprintf("line %d; ",var); | |
995 } | |
996 if (*d == 0x29) break; | |
997 if (*d == 0x2c) {d++;} // ¼¡¤Î arg ¤¬±é»»»Ò¤Ç»Ï¤Þ¤ë¡¢¤Ê¤É¤¬¤Ê¤±¤ì¤Ð¸ºß¤·¤Ê¤¤ | |
998 dprintf(","); | |
999 } | |
1000 if (*d == 0x29) d++; | |
1001 else SetError(); | |
1002 return 0; | |
1003 } | |
1004 | |
1005 /* switch | |
1006 <exp> | |
1007 0x7b | |
1008 <exp> <int> | |
1009 ... | |
1010 0x7d | |
1011 */ | |
1012 | |
1013 int Cmd::GetSwitch(const char*& d) { | |
1014 if (*d != 0x28) {SetError(); return -1;} | |
1015 d++; | |
1016 dprintf("switch. "); | |
1017 int var = GetExpression(d); | |
1018 if (*d != 0x29) {SetError(); return -1;} | |
1019 d++; | |
1020 dprintf("->\n"); | |
1021 if (*d == 0x7b) { | |
1022 d++; | |
1023 } else SetError(); | |
1024 | |
1025 int default_jmp = -1; int jmpto = -1; | |
1026 int i; for (i=0; i<argc; i++) { | |
1027 dprintf("\t"); | |
1028 if (*d++ != 0x28) {SetError(); return -1;} | |
1029 if (*d != 0x29) { | |
1030 int item = GetExpression(d); | |
1031 if (*d++ != 0x29) {SetError(); return -1;} | |
1032 int jmp = read_little_endian_int(d); | |
1033 if (var == item) { | |
1034 dprintf("(selected)"); | |
1035 jmpto = jmp; | |
1036 } | |
1037 dprintf(" -> %d\n", jmp); | |
1038 } else { | |
1039 d++; | |
1040 default_jmp = read_little_endian_int(d); | |
1041 } | |
1042 d += 4; | |
1043 } | |
1044 if (default_jmp != -1) { | |
1045 dprintf("default -> %d\n",default_jmp); | |
1046 if (jmpto == -1) jmpto = default_jmp; | |
1047 } | |
1048 if (*d == 0x7d) { | |
1049 d++; | |
1050 } else SetError(); | |
1051 return jmpto; | |
1052 } | |
1053 /* simple switch | |
1054 <exp> | |
1055 0x7b | |
1056 <int> | |
1057 ... | |
1058 0x7d | |
1059 */ | |
1060 int Cmd::GetSimpleSwitch(const char*& d) { | |
1061 if (*d != 0x28) {SetError(); return -1;} | |
1062 d++; | |
1063 dprintf("simple switch. "); | |
1064 int var = GetExpression(d); | |
1065 if (*d != 0x29) {SetError(); return -1;} | |
1066 d++; | |
1067 dprintf(" ->\n"); | |
1068 int jumpto = -1; | |
1069 if (*d == 0x7b) { | |
1070 d++; | |
1071 } else SetError(); | |
1072 int i; for (i=0; i<argc; i++) { | |
1073 int j = read_little_endian_int(d); | |
1074 d += 4; | |
1075 dprintf("\t%d -> %d\n", i+1, j); | |
1076 if (var == i) jumpto = j; | |
1077 } | |
1078 if (*d == 0x7d) { | |
1079 d++; | |
1080 } else SetError(); | |
1081 return jumpto; | |
1082 } | |
1083 | |
1084 /* | |
1085 selection | |
1086 ? <exp> | |
1087 0x7b | |
1088 <0x0a|0x40> <ushort | uint> | |
1089 */ | |
1090 void Cmd::GetSelection(const char*& d) { | |
1091 dprintf("selection. "); | |
1092 if (*d == 0x28) { | |
1093 d++; | |
1094 GetExpression(d); | |
1095 if (*d != 0x29) { SetError(); return;} | |
1096 d++; | |
1097 } | |
1098 if (*d == 0x7b) { | |
1099 d++; | |
1100 dprintf("{\n\t"); | |
1101 } else SetError(); | |
1102 int arg_count = 0; | |
1103 string text = ""; | |
1104 int cond_result = false; | |
1105 int sel_no = 0; | |
1106 while(*d != 0x7d) { | |
1107 if (d[0] == 0x0a || d[0] == 0x40) { | |
1108 int var; | |
1109 if (system_version == 0) { var = read_little_endian_int(d+1); d += 5;} | |
1110 else { var = read_little_endian_short(d+1); d += 3;} | |
1111 dprintf("Line %d; ",var); | |
1112 if (text.length() != 0) { | |
1113 if (cond_result) ; // ¾ò·ïÀ᤬ true ¤Ê¤éɽ¼¨¤·¤Ê¤¤ | |
1114 else { | |
1115 const char* str = text.c_str(); | |
1116 VarInfo var; | |
1117 var.type = TYPE_STR; | |
1118 var.value = CopyString(str); | |
1119 args.push_back(var); | |
1120 var.type = TYPE_VAL; | |
1121 var.value = sel_no; | |
1122 args.push_back(var); | |
1123 } | |
1124 sel_no++; | |
1125 } | |
1126 text = ""; | |
1127 cond_result = false; | |
1128 } else if (d[0] == 0x2c) { | |
1129 dprintf(":comma:"); | |
1130 } else if (d[0] == 0x28) { | |
1131 dprintf(":cond:"); | |
1132 d++; | |
1133 while(d[0] != 0x29) { | |
1134 int result = GetExpressionCond(d); // PRINT- Àá¤Ç¤Ê¤¤¤Ð¤¢¤¤¡¢¾ò·ïɽ¼¨¡£¼¡¤Ïʸ»úÀá¡¢¤Þ¤¿¤ÏPRINTÀá¤Î¤Ï¤º | |
1135 if (*d == 0x32) { // 0x32 ¤Ê¤é¡¢¸½ºß¤Î¾ò·ïÀá¤òɽ¼¨¤·¤Ê¤¤ | |
1136 d++; dprintf("##"); | |
1137 cond_result = result; | |
1138 } else if (*d == 0x31) { // 0x31 ¤Ê¤é¡¢¸½ºß¤Î¾ò·ïÀá¤òɽ¼¨¤¹¤ë | |
1139 // Little Busters! : ¤³¤Î¾ò·ï¤ÇÀµ¤·¤¤¤«¤Ï̤¸¡¾Ú | |
1140 d++; dprintf("***"); | |
1141 cond_result = !result; | |
1142 } | |
1143 dprintf(":"); | |
1144 } | |
1145 d++; | |
1146 } else if (StrType(d)) { | |
1147 int strpt = GetString(d); | |
1148 text += strheap + strpt; | |
1149 arg_count++; | |
1150 dprintf("\n\t"); | |
1151 } else if (*d == 0x23 && strncmp(d,"###PRINT",8) == 0) { | |
1152 d += 8; | |
1153 if (d[0] != 0x28) SetError(); | |
1154 else { // ʸ»úÊÑ¿ô¤ÎÆâÍƤÎɽ¼¨ | |
1155 d++; | |
1156 dprintf("Print."); | |
1157 VarInfo info; | |
1158 GetLeftToken(d, info); | |
1159 if (d[0] != 0x29 || info.type == -1) SetError(); | |
1160 d++; | |
21
d1bb7b365816
Fixed dynamic strings in selections (Fuko Pranks for instance)
thib
parents:
13
diff
changeset
|
1161 dprintf(";");/* |
0 | 1162 // ¿ôÃͤòÁ´³Ñʸ»ú¤ËÊÑ´¹¤·¤ÆÅÐÏ¿ |
1163 char str[10], str2[20]; // itoa | |
1164 sprintf(str, "%d", info.value); | |
1165 int i; for (i=0; str[i] != 0; i++) { | |
1166 str2[i*2] = 0xa3; | |
1167 str2[i*2+1] = 0xb0 + str[i]-'0'; | |
1168 } | |
21
d1bb7b365816
Fixed dynamic strings in selections (Fuko Pranks for instance)
thib
parents:
13
diff
changeset
|
1169 str2[i*2] = 0;*/ |
d1bb7b365816
Fixed dynamic strings in selections (Fuko Pranks for instance)
thib
parents:
13
diff
changeset
|
1170 text += strheap + info.value; |
0 | 1171 } |
1172 } else { SetError(); break;} | |
1173 } | |
1174 d++; | |
1175 /* @@@ */ | |
1176 /* °ìÃפ·¤Ê¤¤¾ì¹ç¤¬¤¢¤ë¤Î¤Ç¥³¥á¥ó¥È¥¢¥¦¥È */ | |
1177 // if (arg_count != argc) SetError(); | |
1178 dprintf("\n}\n"); | |
1179 } | |
1180 | |
1181 void Cmd::GetCmd(Flags& flags_orig, const char*& d ) { | |
52 | 1182 if (d == NULL) { SetError(); return;} |
0 | 1183 if (cmd_type != CMD_NOP) return; |
1184 | |
1185 cmdstr[0] = 0; | |
1186 rawdata = d; | |
1187 if (*d == 0x23) { /* ¥³¥Þ¥ó¥É */ | |
1188 cmd_type = CMD_OTHER; | |
1189 cmd1 = *(unsigned const char*)(d+1); | |
1190 cmd2 = *(unsigned const char*)(d+2); | |
1191 cmd3 = read_little_endian_short(d+3); | |
1192 argc = read_little_endian_short(d+5); | |
1193 cmd4 = *(unsigned const char*)(d+7); | |
1194 d += 8; | |
1195 /* verbose */ | |
1196 // dprintf(" 0x23 - cmd %02x-%02x:%04x:%02x[%2d] \n",cmd1,cmd2,cmd3,cmd4,argc); | |
29
d229cce98f50
* no more (or, at least, less) duplicate code between scn2kdump and the rest
thib
parents:
21
diff
changeset
|
1197 sprintf(cmdstr, "%02x-%02x:%04x:%02x : %s",cmd1,cmd2,cmd3,cmd4, CmdDescr(cmd1,cmd2,cmd3,cmd4)); |
0 | 1198 /* °ú¿ô¤òÆÀ¤ë */ |
1199 /* Æüì°ú¿ô¤Î¤â¤Î */ | |
1200 int is_special = 0; | |
1201 if (cmd1 == 0) { | |
1202 if (cmd2 == 1) { | |
1203 int jump_arg = -1; | |
1204 if (cmd3 == 0 || cmd3 == 5) { | |
1205 /* gosub / goto */ | |
1206 jump_arg =read_little_endian_int(d); | |
1207 d += 4; | |
1208 if (cmd3 == 0) | |
1209 dprintf("\tjmp -> %d\n", jump_arg); | |
1210 else /* cmd3 == 5 */ | |
1211 dprintf("\tcall -> %d\n", jump_arg); | |
1212 is_special = 1; | |
1213 } else if (cmd3 == 1 || cmd3 == 2) { | |
1214 /* conditional jump (if / unless) */ | |
1215 if (*d++ != 0x28) { SetError(); return;} | |
1216 dprintf("\t"); | |
1217 int cond = GetExpressionCond(d); | |
1218 if (cmd3 == 1) cond = !cond; // µÕ¤Ë¤Ê¤ë | |
1219 if (*d++ != 0x29) { SetError(); return; } | |
1220 int jumpto = read_little_endian_int(d); | |
1221 d += 4; | |
1222 dprintf("-> %d\n", jumpto); | |
1223 if (! cond) jump_arg = jumpto; /* condition ¤¬Ëþ¤¿¤µ¤ì¤Ê¤¤¾ì¹ç¡¢¥¸¥ã¥ó¥× */ | |
1224 is_special = 1; | |
1225 } else if (cmd3 == 4) { | |
1226 /* switch to */ | |
1227 jump_arg = GetSwitch(d); | |
1228 is_special = 1; | |
1229 } else if (cmd3 == 8 || cmd3 == 3) { | |
1230 /* switch to */ | |
1231 jump_arg = GetSimpleSwitch(d); | |
1232 is_special = 1; | |
1233 } else if (cmd3 == 16) { // call with parameters | |
1234 GetArgs(d); | |
1235 jump_arg = read_little_endian_int(d); | |
1236 d += 4; | |
1237 is_special = 1; | |
1238 } else goto retry; | |
1239 if (jump_arg == -1) { | |
1240 cmd_type = CMD_NOP; | |
1241 } | |
1242 else { | |
1243 cmd_type = CMD_JMP; | |
1244 args.push_back(VarInfo(jump_arg)); | |
1245 } | |
1246 } else if (cmd2 == 2 && (cmd3 == 0 || cmd3 == 1 || cmd3 == 2 || cmd3 == 3 || cmd3 == 0x0d) ) { | |
1247 /* selection */ | |
1248 GetSelection(d); | |
1249 is_special = 1; | |
1250 } | |
1251 } | |
1252 retry: | |
1253 /* °ìÈÌ°ú¿ô¤Î¤â¤Î */ | |
1254 if (!is_special) { | |
29
d229cce98f50
* no more (or, at least, less) duplicate code between scn2kdump and the rest
thib
parents:
21
diff
changeset
|
1255 dprintf(" 0x23 - cmd %02x-%02x:%04x:%02x[%2d] : %s\n",cmd1,cmd2,cmd3,cmd4,argc,CmdDescr(cmd1,cmd2,cmd3,cmd4)); |
0 | 1256 dprintf("\t"); |
1257 if (cmd1 == 1 && cmd2 == 0x22 && (cmd3 == 0xc1c || cmd3 == 0x835)) GetArgsSpecial(3, d); | |
1258 else if (cmd1 == 1 && cmd2 == 0x0b && cmd3 == 0x65) GetArgsSpecial(0, d); | |
1259 else if (cmd1 == 1 && cmd2 == 0x15 && cmd3 == 0x28) GetArgsSpecial(0, d); | |
1260 else if (cmd1 == 1 && cmd2 == 4 && (cmd3 == 0x26c || cmd3 == 0x26d || cmd3 == 0x270 || cmd3 == 0x276)) GetArgsSpecial(0, d); | |
1261 else if (cmd1 == 1 && cmd2 == 4 && cmd3 == 0x586) GetArgsSpecial(1, d); | |
43
01aa5ddf7dc8
A lot of very minor improvements (deleted some unused variables, and other things like that...)
thib
parents:
29
diff
changeset
|
1262 else if (cmd1 == 1 && ((cmd2 == 0x21 && cmd3 == 0x4b) || (cmd2 == 0x28 && cmd3 == 0x64))) GetArgsSpecial(2,d); |
0 | 1263 else GetArgs(d); |
1264 dprintf("\n"); | |
1265 | |
1266 } | |
1267 } else if (*d == 0x24) { /* ÂåÆþ±é»» */ | |
1268 if (d[1] == 0x12 || d[2] != 0x5b) SetError(); | |
1269 dprintf("expr: "); | |
1270 sprintf(cmdstr, "expr"); | |
1271 | |
1272 VarInfo info; | |
1273 int value = GetLeftToken(d, info); | |
1274 if (d[0] != 0x5c) SetError(); | |
1275 int type = d[1]; | |
1276 if (type < 20 || type > 30) SetError(); | |
1277 else dprintf("%s",op_str[type]); | |
1278 d += 2; | |
1279 int value2 = GetExpression(d); | |
1280 // ÂåÆþ¾ðÊó¤òËä¤á¹þ¤à | |
1281 if (type != 30) value2 = eval(value, type-20, value2); | |
1282 cmd_type = CMD_FLAGS; | |
1283 args.push_back(info); | |
1284 args.push_back(value2); | |
1285 dprintf("\n"); | |
1286 } else if (StrType(d)) { /* ʸ»ú½ÐÎÏ */ | |
1287 VarInfo info; | |
1288 info.type = TYPE_STR; | |
1289 info.value = GetString(d); | |
1290 args.push_back(info); | |
1291 cmd_type = CMD_TEXT; | |
1292 dprintf("\n"); | |
1293 } else if (*d == 0x0a || *d == 0x40 || *d == 0x21) { /* ¥Ç¥Ð¥Ã¥°Íѥǡ¼¥¿¤È´ûÆɥե饰 */ | |
1294 cmd_type = CMD_NOP; | |
1295 if (*d == 0x0a) { | |
1296 dprintf("line "); | |
1297 d++; | |
1298 int l; | |
1299 if (system_version == 0) { | |
1300 l = read_little_endian_int(d); | |
1301 d += 4; | |
1302 } else { | |
1303 l = read_little_endian_short(d); | |
1304 d += 2; | |
1305 } | |
1306 dprintf("%d\n", l); | |
1307 } else { /* 0x40, 0x21 */ | |
1308 // ´ûÆÉ¥Þ¡¼¥«¡¼¤é¤·¤¤¡£¥¨¥ó¥È¥ê¡¼¥Ý¥¤¥ó¥È¤È¥»¡¼¥Ö¥Ý¥¤¥ó¥È¤â»È¤ï¤ì¤ë¡£ | |
1309 // RealLive 1.2.5¤«¤é¡¢0x40¤Ï¥»¡¼¥Ö¥Ý¥¤¥ó¥È¡¢0x21¤Ï¥¨¥ó¥È¥ê¡¼¥Ý¥¤¥ó¥È¡£ | |
1310 // 1.2.5°ÊÁ°¡¢¤É¤Á¤é¤â0x40¤¬»È¤ï¤ì¤ë¡£ | |
1311 int kidoku_index; | |
1312 d++; | |
1313 if (system_version == 0) { | |
1314 kidoku_index = read_little_endian_int(d); | |
1315 d += 4; | |
1316 } else { | |
1317 kidoku_index = read_little_endian_short(d); | |
1318 d += 2; | |
1319 } | |
1320 dprintf("kidoku marker %d\n", kidoku_index); | |
1321 // text_readflag¤Ï¡¢¤³¤Îkidoku_index¤ò»È¤Ã¤¿¤éÎɤ¤¤«¤Ê¡£ | |
1322 } | |
1323 } else if (*d == 0x2c) { /* ??? */ | |
1324 dprintf("commd;0x2c\n"); // conditional jump ¤Î¹Ô¤Àè¤Ë¤è¤¯¤¢¤ë¤é¤·¤¤¡Ê¾ï¤Ë¡¢¤«¤Ï¤ï¤«¤é¤Ê¤¤¡Ë | |
1325 d++; | |
1326 } else { | |
1327 SetError(); | |
1328 } | |
1329 } | |
52 | 1330 |
0 | 1331 void Cmd::clear(void) { |
1332 cmd_type = CMD_NOP; | |
1333 ResetString(); | |
1334 args.clear(); | |
1335 errorflag = false; | |
1336 pos = -1; | |
1337 } | |
1338 | |
1339 char Cmd::strtype[256] = { | |
1340 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, /* +00 */ | |
1341 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, /* +10 */ // 0123456789ABCDEF | |
1342 1,0,3,0, 0,0,0,1, 0,0,0,0, 0,1,1,0, /* +20 */ // !"#$%&'()*+,-./ | |
1343 1,1,1,1, 1,1,1,1, 1,1,1,0, 0,0,0,1, /* +30 */ // 0123456789:;<=>? | |
1344 0,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, /* +40 */ // @ABCDEFGHIJKLMNO | |
1345 1,1,1,1, 1,1,1,1, 1,1,1,0, 0,0,0,1, /* +50 */ // PQRSTUVWXYZ[\]^_ | |
1346 0,0,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, /* +60 */ // `abcdefghijklmno | |
1347 1,1,1,1, 1,1,1,1, 1,1,1,1, 0,0,0,0, /* +70 */ // pqrstuvwxyz{|}~ | |
1348 2,2,2,2, 2,2,2,2, 2,2,2,2, 2,2,2,2, /* +80 */ | |
1349 2,2,2,2, 2,2,2,2, 2,2,2,2, 2,2,2,2, /* +90 */ | |
1350 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, /* +A0 */ | |
1351 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, /* +B0 */ | |
1352 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, /* +C0 */ | |
1353 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, /* +D0 */ | |
1354 2,2,2,2, 2,2,2,2, 2,2,2,2, 2,2,2,2, /* +E0 */ | |
1355 2,2,2,2, 2,2,2,2, 2,2,2,2, 2,2,0,0 /* +F0 */ | |
1356 }; | |
1357 | |
1358 int Cmd::GetString(const char*& d) { | |
1359 int retnum = -1; | |
1360 bool quote_flag = false; | |
1361 int stype; | |
1362 retnum = strend; | |
1363 while(1) { | |
1364 if (*d == '\\') { | |
1365 d++; | |
1366 strheap[strend++] = *d++; | |
1367 } else if (*d == '"') { | |
1368 if (quote_flag) quote_flag = false; | |
1369 else quote_flag = true; | |
1370 d++; | |
1371 } else if (quote_flag) { | |
1372 strheap[strend++] = *d++; | |
43
01aa5ddf7dc8
A lot of very minor improvements (deleted some unused variables, and other things like that...)
thib
parents:
29
diff
changeset
|
1373 } else if ((stype = StrType(d))) { |
0 | 1374 strheap[strend++] = *d++; |
1375 if (stype == 2) strheap[strend++] = *d++; | |
1376 } else break; | |
1377 } | |
1378 strheap[strend++] = 0; | |
1379 dprintf("\"%s\"", strheap + retnum); | |
1380 if (strend >= STRHEAP_SIZE) { | |
1381 dprintf("Error: string heap overflow\n"); | |
1382 } | |
1383 return retnum; | |
1384 } | |
1385 | |
1386 int Cmd::CopyString(const char* d) { | |
1387 int retnum = strend; | |
1388 int len = strlen(d); | |
1389 memcpy(strheap+strend, d, len+1); | |
1390 strend += len+1; | |
1391 d += len+1; | |
1392 return retnum; | |
1393 } | |
1394 | |
1395 int Cmd::StrVar(int type, int var_num) { | |
1396 int retnum = strend; | |
1397 flags.Str(type, var_num, strheap+strend, STRHEAP_SIZE-strend); | |
1398 strend += strlen(strheap+strend)+1; | |
1399 return retnum; | |
1400 } | |
1401 | |
1402 void Cmd::SetSysvar(int n, int val) { | |
1403 VarInfo info; | |
1404 if (cmd_type != CMD_SYSVAR) { | |
1405 args.clear(); | |
1406 } | |
1407 cmd_type = CMD_SYSVAR; | |
1408 | |
1409 info.type = TYPE_SYS; | |
1410 info.number = n; | |
1411 info.value = val; | |
1412 args.push_back(info); | |
1413 } | |
1414 void Cmd::SetFlagvar(VarInfo info, int val) { | |
1415 if (cmd_type != CMD_SYSVAR) { | |
1416 args.clear(); | |
1417 } | |
1418 cmd_type = CMD_SYSVAR; | |
1419 | |
1420 info.value = val; | |
1421 args.push_back(info); | |
1422 } | |
1423 | |
1424 void Cmd::SetStrvar(VarInfo info, const string& s) { | |
1425 if (cmd_type != CMD_SYSVAR) { | |
1426 args.clear(); | |
1427 } | |
1428 | |
1429 cmd_type = CMD_SYSVAR; | |
1430 const char* ss = s.c_str(); | |
1431 info.value = CopyString(ss); | |
1432 args.push_back(info); | |
1433 } | |
1434 | |
52 | 1435 |
1436 const char* Cmd::Str(const VarInfo& info) const { | |
1437 if (info.type != TYPE_STR && info.type != TYPE_VARSTR && info.type != TYPE_VARLOCSTR && info.type != TYPE_VARSYSSTR) | |
1438 return ""; | |
1439 int pt = info.value; | |
1440 if (pt < 0 || pt >= STRHEAP_SIZE) return ""; | |
1441 return strheap + pt; | |
1442 } | |
1443 | |
1444 int Cmd::AddStr(char* s) { | |
1445 // 1-0a-0064 ¤Ï¤³¤¦¤¤¤¦¤â¤Î¤¬É¬Íפ餷¤¤ | |
1446 int start = strend; | |
1447 while (*s) strheap[strend++] = *s++; | |
1448 strheap[strend++] = 0; | |
1449 return start; | |
1450 } | |
1451 | |
1452 | |
0 | 1453 void Cmd::read(const CmdSimplified& from) { |
1454 errorflag = false; | |
1455 ResetString(); | |
1456 | |
1457 cmd_type = Cmdtype(from.type); | |
1458 cmd1 = from.cmd1; | |
1459 cmd2 = from.cmd2; | |
1460 cmd3 = from.cmd3; | |
1461 cmd4 = from.cmd4; | |
1462 argc = from.argc; | |
1463 /* args ¤ÎÆɤ߹þ¤ß */ | |
1464 args.clear(); | |
1465 char* d = from.args; | |
52 | 1466 if (d == NULL) return; |
0 | 1467 while(*d != TYPE_END) { |
1468 VarInfo info; | |
1469 switch(*d) { | |
1470 case TYPE_VAL: | |
1471 info.type = TYPE_VAL; | |
1472 info.number = 0; | |
1473 info.value = read_little_endian_int(d+1); | |
1474 d += 5; | |
1475 args.push_back(info); | |
1476 break; | |
1477 case TYPE_STR: | |
1478 info.type = TYPE_STR; | |
1479 info.number = 0; | |
1480 d++; | |
1481 info.value = CopyString( d); | |
1482 d += strlen(d)+1; | |
1483 args.push_back(info); | |
1484 break; | |
1485 default: | |
1486 fprintf(stderr,"Cmd::read: Invalid Load Data\n"); | |
1487 *d = TYPE_END; | |
1488 } | |
1489 } | |
1490 } | |
52 | 1491 |
0 | 1492 void Cmd::write(CmdSimplified& to, char*& buffer) const { |
1493 /* | |
1494 if (cmd_type != CMD_OTHER) { | |
1495 fprintf(stderr,"Cmd::write: Invalid Cmd during Saving Data\n"); | |
1496 to.cmd1 = 0; to.cmd2 = 0; to.cmd3 = 0; to.cmd4 = 0; to.argc = 0; to.args = 0; | |
1497 return; | |
1498 } | |
1499 */ | |
1500 to.type = cmd_type; | |
1501 to.cmd1 = cmd1; | |
1502 to.cmd2 = cmd2; | |
1503 to.cmd3 = cmd3; | |
1504 to.cmd4 = cmd4; | |
1505 to.argc = argc; | |
1506 /* args ¤Î½ñ¤¹þ¤ß */ | |
1507 if (args.empty()) { | |
52 | 1508 to.args = NULL; |
0 | 1509 } else { |
1510 to.args = buffer; | |
1511 char* d = to.args; | |
1512 vector<VarInfo>::const_iterator it; | |
1513 for (it = args.begin(); it != args.end(); it++) { | |
1514 int type = it->type; | |
1515 if ( (type >= 0 && type < 7) || type == TYPE_VAL || type == char(TYPE_SYS)) { // digits | |
1516 *d++ = TYPE_VAL; | |
1517 write_little_endian_int(d, it->value); | |
1518 d += 4; | |
1519 } else if (type == TYPE_VARSTR || type == TYPE_VARSYSSTR || type == TYPE_VARLOCSTR || type == TYPE_STR) { // string | |
1520 *d++ = TYPE_STR; | |
1521 const char* s = Str(*it); | |
1522 int len = strlen(s); | |
1523 memcpy(d, s, len+1); | |
1524 d += len+1; | |
1525 } else { | |
1526 fprintf(stderr,"Cmd::write: Invalid Cmd args during Saving Data\n"); | |
1527 } | |
1528 } | |
1529 *d++ = TYPE_END; | |
1530 buffer = d; | |
1531 } | |
1532 } | |
52 | 1533 |
0 | 1534 void CmdSimplified::copy(const CmdSimplified& from, char*& args_buffer) { |
1535 *this = from; | |
52 | 1536 if (args == NULL) return; |
0 | 1537 char* args_old = from.args; |
1538 /* args ¤Î¥³¥Ô¡¼ */ | |
1539 while(*args_old != TYPE_END) { | |
1540 if (*args_old == TYPE_VAL) { | |
1541 args_old += 5; | |
1542 } else { /* TYPE_STR */ | |
1543 args_old += strlen(args_old)+1; | |
1544 } | |
1545 } | |
1546 args_old++; | |
1547 int args_len = args_old - from.args; | |
1548 memmove(args_buffer, from.args, args_len); | |
1549 args = args_buffer; | |
1550 args_buffer += args_len; | |
1551 } | |
52 | 1552 |
0 | 1553 void CmdSimplified::Save(string& saveret) { |
1554 char buf[1024]; | |
1555 sprintf(buf, "%02x-%02x:%04x:%02x(%02d),", cmd1, cmd2, cmd3, cmd4, argc); | |
1556 saveret += buf; | |
1557 | |
1558 /* args ¤Î¥³¥Ô¡¼ */ | |
1559 char* d = args; | |
1560 while(d && *d != TYPE_END) { | |
1561 if (*d == TYPE_VAL) { | |
1562 d++; | |
1563 sprintf(buf, "%d,", read_little_endian_int(d)); | |
1564 d += 4; | |
1565 } else { /* TYPE_STR ¤È²¾Äê */ | |
1566 d++; | |
1567 if (strlen(d) > 1000) d[1000] = 0; // ¤¢¤ê¤¨¤Ê¤¤¡¦¡¦¡¦ | |
1568 int i; int cnt = 0; | |
1569 buf[cnt++] = '"'; | |
1570 for (i=0; d[i] != 0; i++) { | |
1571 if (d[i] == '"') buf[cnt++] = '"'; | |
1572 buf[cnt++] = d[i]; | |
1573 } | |
1574 buf[cnt++]='"'; | |
1575 buf[cnt++] = ','; | |
1576 buf[cnt++] = 0; | |
1577 d += strlen(d)+1; | |
1578 } | |
1579 saveret += buf; | |
1580 } | |
1581 saveret += 'E'; | |
1582 } | |
1583 | |
1584 void CmdSimplified::Load(const char* save, char*& args_buffer) { | |
1585 args = args_buffer; | |
1586 | |
1587 type = CMD_OTHER; | |
1588 sscanf(save, "%02x-%02x:%04x:%02x(%02d),", &cmd1, &cmd2, &cmd3, &cmd4, &argc); | |
1589 save = strchr(save, ','); | |
52 | 1590 if (save == NULL) { |
0 | 1591 *args_buffer++ = TYPE_END; |
1592 return; | |
1593 } | |
1594 save++; | |
1595 while(*save != 'E' && *save != '\n' && *save != '\0') { | |
1596 if (isdigit(*save)) { | |
1597 int v; | |
1598 sscanf(save,"%d,",&v); | |
1599 *args_buffer++ = TYPE_VAL; | |
1600 write_little_endian_int(args_buffer, v); | |
1601 args_buffer+= 4; | |
1602 save = strchr(save, ','); | |
1603 if (save) save++; | |
1604 } else { // *save == '"' | |
1605 save++; | |
1606 *args_buffer++ = TYPE_STR; | |
1607 while(1) { | |
1608 if (*save == 0) break; | |
1609 if (*save == '"') { | |
1610 if (save[1] != '"') break; | |
1611 save++; | |
1612 } | |
1613 *args_buffer++ = *save++; | |
1614 } | |
1615 save += 2; | |
1616 *args_buffer++ = 0; | |
1617 } | |
1618 } | |
1619 *args_buffer++ = TYPE_END; | |
1620 } | |
1621 | |
1622 #ifdef SCN_DUMP | |
1623 void usage(void) { | |
1624 fprintf(stderr,"usage : scn2kdump [inputfile] [outputfile]\n"); | |
1625 fprintf(stderr," inputfile: seen.txt(default)\n"); | |
1626 fprintf(stderr," outputfile: seen.txt_out(default)\n"); | |
1627 exit(-1); | |
1628 } | |
1629 int main(int argc, char** argv) { | |
1630 /* determine file names */ | |
1631 bool verbose = false; | |
1632 char* inname = "seen.txt"; | |
52 | 1633 char* outname = NULL; |
0 | 1634 if (argc > 2 && strcmp(argv[1],"-v") == 0) { |
1635 int i; for (i=1; i<argc; i++) argv[i] = argv[i+1]; | |
1636 argc--; | |
1637 verbose = true; | |
1638 } | |
1639 switch(argc) { | |
52 | 1640 case 1: break; |
1641 case 2: | |
1642 inname = argv[1]; | |
1643 break; | |
1644 case 3: | |
1645 inname = argv[1]; | |
1646 outname = argv[2]; | |
1647 break; | |
1648 default: usage(); | |
0 | 1649 } |
1650 /* open output file */ | |
1651 FILE* outstream = stdout; | |
1652 /* create archive instance */ | |
1653 SCN2kFILE archive(inname); | |
1654 archive.Init(); | |
1655 if (archive.Deal() == 0) { | |
1656 fprintf(stderr,"Cannot open / Invalid archive file %s\n",inname); | |
1657 usage(); | |
1658 } | |
1659 /* dump files */ | |
1660 archive.InitList(); | |
1661 char* fname; | |
1662 fprintf(stderr,"Dump start\n"); | |
1663 int system_version = 0; | |
1664 while( (fname = archive.ListItem()) != 0) { | |
1665 ARCINFO* info = archive.Find(fname,""); | |
52 | 1666 if (info == NULL) continue; |
0 | 1667 char* data = info->CopyRead(); |
1668 char* d = data; | |
1669 char* dend = d + info->Size(); | |
1670 /* version ³Îǧ */ | |
1671 if (read_little_endian_int(d) == 0x1cc) { | |
1672 system_version = 0; | |
1673 } else if (read_little_endian_int(d) == 0x1d0) { | |
1674 system_version = 1; | |
1675 } else { | |
1676 continue; | |
1677 } | |
1678 if (read_little_endian_int(d+4) == 0x1adb2) ; // little busters! | |
1679 else if (read_little_endian_int(d+4) != 0x2712) continue; | |
1680 int header_size; | |
1681 if (system_version == 0) { | |
1682 header_size = 0x1cc + read_little_endian_int(d+0x20) * 4; | |
1683 } else { | |
1684 header_size = read_little_endian_int(d+0x20); | |
1685 } | |
1686 d += header_size; | |
1687 | |
1688 const char* dcur = d; | |
1689 const char* dstart = d; | |
1690 fprintf(stderr,"Dumping %s\n",fname); | |
1691 Flags flags; | |
1692 /* ºÇ½é¤«¤éºÇ¸å¤Þ¤Ç¥³¥Þ¥ó¥É¼èÆÀ -> ½ÐÎϤò·«¤êÊÖ¤¹ */ | |
1693 while(dcur<dend) { | |
1694 const char* dprev = dcur; | |
1695 Cmd cmd(flags, system_version); cmd.ClearError(); | |
1696 | |
1697 /* end? */ | |
1698 if (*dcur == -1) { | |
1699 /* 0xff x 32byte + 0x00 : end sign */ | |
1700 int i; for (i=0; i<0x20; i++) | |
1701 if (dcur[i] != -1) break; | |
1702 if (i == 0x20 && dcur[i] == 0) break; | |
1703 } | |
1704 dprintf("%d : ",dcur-dstart); | |
1705 cmd.GetCmd(flags, dcur); | |
1706 if (cmd.IsError()) { | |
1707 fprintf(outstream, "Error at %6d\n",dprev-dstart); | |
1708 while(dcur < dend) { | |
1709 if (*dcur == 0x29 && dcur[1] == 0x0a) {dcur++;break;} | |
1710 dcur++; | |
1711 } | |
1712 dprev -= 2*16; | |
1713 int ilen = (dcur-dprev+15)/16; | |
1714 int i; for (i=0; i<ilen; i++) { | |
1715 fprintf(outstream, "%6d: ",dprev-dstart); | |
1716 int j; for (j=0; j<16; j++) { | |
1717 if (dprev >= dend) break; | |
1718 if (dprev < data) continue; | |
1719 fprintf(outstream, "%02x ",*(unsigned char*)(dprev)); | |
1720 dprev++; | |
1721 } | |
1722 fprintf(outstream, "\n"); | |
1723 } | |
1724 } | |
1725 } | |
1726 delete info; | |
1727 } | |
1728 return 0; | |
1729 } | |
1730 #endif | |
1731 |