annotate scn2k/scn2k_cmd.cc @ 23:f94d28dcf7bc

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