Debian Codegraph @

tools/cheatsConverter/src/convert.cpp

raw symbols SCIP
1 /* Cheats converter
2
3 Copyright 2009 DeSmuME team
4
5 This file is part of DeSmuME
6
7 DeSmuME is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 DeSmuME is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with DeSmuME; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <memory.h>
25 #include <string.h>
26 #include <algorithm>
27 #include "convert.h"
28
29 CHEATS_LIST list[MAX_CHEAT_LIST] = {0};
30 u32 num = 0;
31 char ROMserial[50] = {0};
32
33 static void convertSerial()
34 {
35 const char *regions[] = { "JPFSEDIRKH",
36 "JPN",
37 "EUR",
38 "FRA",
39 "ESP",
40 "USA",
41 "NOE",
42 "ITA",
43 "RUS",
44 "KOR",
45 "HOL",
46
47 };
48 char buf[5] = {0};
49
50 if (!ROMserial[0]) return;
51
52 memcpy(buf, &ROMserial[strlen(ROMserial)-6], 4);
53 sprintf(ROMserial, "NTR-%s-", buf);
54 u32 region = (u32)(std::max<s32>(strchr(regions[0], buf[3]) - regions[0] + 1, 0));
55 if (region != 0)
56 strcat(ROMserial, regions[region]);
57 else
58 memset(ROMserial, 0, sizeof(ROMserial));
59 }
60
61 static void cheatsClear()
62 {
63 memset(list, 0, sizeof(list));
64 for (int i = 0; i < MAX_CHEAT_LIST; i++)
65 list[i].type = 0xFF;
66 num = 0;
67 }
68
69 bool save(char *filename)
70 {
71 char *types[] = {"DS", "AR", "CB"};
72 char buf[(sizeof(list[0].hi)+sizeof(list[0].lo)) * 2 + 200] = { 0 };
73
74 FILE *flist = fopen(filename, "w");
75
76 if (flist)
77 {
78 fprintf(flist, "; DeSmuME cheat file. VERSION %i.%03i\n", CHEAT_VERSION_MAJOR, CHEAT_VERSION_MINOR);
79 fprintf(flist, "Name=\n");
80 fprintf(flist, "Serial=%s\n", ROMserial);
81 fputs("\n; cheats list\n", flist);
82 for (unsigned int i = 0; i < num; i++)
83 {
84 if (list[i].num == 0) continue;
85 memset(buf, 0, sizeof(buf));
86 sprintf(buf, "%s %c ", types[list[i].type], list[i].enabled?'1':'0');
87 for (int t = 0; t < list[i].num; t++)
88 {
89 char buf2[10] = { 0 };
90 if (list[i].type == 0)
91 {
92 list[i].hi[t] &= 0x0FFFFFFF;
93 list[i].hi[t] |= (list[i].size << 28);
94 }
95 sprintf(buf2, "%08X", list[i].hi[t]);
96 strcat(buf, buf2);
97 sprintf(buf2, "%08X", list[i].lo[t]);
98 strcat(buf, buf2);
99 if (t < (list[i].num - 1))
100 strcat(buf, ",");
101 }
102 strcat(buf, " ;");
103 strcat(buf, trim(list[i].description));
104 fprintf(flist, "%s\n", buf);
105 }
106 fputs("\n", flist);
107 fclose(flist);
108 return true;
109 }
110
111 return false;
112 }
113
114 //==================================================================================== Loads
115 bool load_1_0(char *fname)
116 {
117 #ifdef WIN32
118 char buf[200] = {0};
119 num = GetPrivateProfileInt("General", "NumberOfCheats", 0, fname);
120 if (num == 0) return false;
121 if (num > MAX_CHEAT_LIST) num = MAX_CHEAT_LIST;
122 for (unsigned int i = 0; i < num; i++)
123 {
124 wsprintf(buf, "Desc%04i", i);
125 GetPrivateProfileString("Cheats", buf, "", list[i].description, 75, fname);
126 wsprintf(buf, "Type%04i", i);
127 list[i].type = GetPrivateProfileInt("Cheats", buf, 0xFF, fname);
128 wsprintf(buf, "Num_%04i", i);
129 list[i].num = GetPrivateProfileInt("Cheats", buf, 0, fname);
130 wsprintf(buf, "Enab%04i", i);
131 list[i].enabled = GetPrivateProfileInt("Cheats", buf, 0, fname)==1?true:false;
132 wsprintf(buf, "Size%04i", i);
133 list[i].size = GetPrivateProfileInt("Cheats", buf, 0, fname);
134 for (int t = 0; t < list[i].num; t++)
135 {
136 char tmp_buf[10] = { 0 };
137 wsprintf(buf, "H%03i%04i", i, t);
138 GetPrivateProfileString("Cheats", buf, "0", tmp_buf, 10, fname);
139 sscanf_s(tmp_buf, "%x", &list[i].hi[t]);
140 wsprintf(buf, "L%03i%04i", i, t);
141 list[i].lo[t] = GetPrivateProfileInt("Cheats", buf, 0, fname);
142 }
143 }
144 #endif
145 return true;
146 }
147
148 bool load_1_3(char *fname)
149 {
150 FILE *fcheat = fopen(fname, "r");
151 char buf[4096] = {0};
152 int last=0;
153
154 if (fcheat)
155 {
156 cheatsClear();
157 memset(buf, 0, sizeof(buf));
158
159 while (!feof(fcheat))
160 {
161 fgets(buf, sizeof(buf), fcheat);
162 if (buf[0] == ';') continue;
163 strcpy(buf, trim(buf));
164 if (strlen(buf) == 0) continue;
165 if ( (buf[0] == 'D') &&
166 (buf[1] == 'e') &&
167 (buf[2] == 's') &&
168 (buf[3] == 'c') &&
169 (buf[4] == '=') ) // new cheat block
170 {
171 strncpy((char *)list[last].description, (char *)buf+5, strlen(buf)-5);
172 fgets(buf, sizeof(buf), fcheat);
173 if ( (buf[0] == 'I') &&
174 (buf[1] == 'n') &&
175 (buf[2] == 'f') &&
176 (buf[3] == 'o') &&
177 (buf[4] == '=') ) // Info cheat
178 {
179 u32 dstart = 5;
180 u32 dsize = 0;
181 char bf[4] = { 0 };
182
183 while ( (buf[dstart+dsize] != ',') && (buf[dstart+dsize]!=0)) { dsize++; };
184 if (buf[dstart+dsize]==0) continue; // error
185 strncpy(bf, (char*)buf+dstart, dsize);
186 list[last].type=atoi(bf);
187 dstart += (dsize+1); dsize=0;
188 while ( (buf[dstart+dsize] != ',') && (buf[dstart+dsize]!=0)) { dsize++; };
189 if (buf[dstart+dsize]==0) continue; // error
190 strncpy(bf, (char*)buf+dstart, dsize);
191 dstart += (dsize+1); dsize=0;
192 list[last].num=atoi(bf);
193 while ( (buf[dstart+dsize] != ',') && (buf[dstart+dsize]!=0)) { dsize++; };
194 if (buf[dstart+dsize]==0) continue; // error
195 strncpy(bf, (char*)(buf+dstart), dsize);
196 dstart += (dsize+1); dsize=0;
197 list[last].enabled=atoi(bf)==1?true:false;
198 while ( (buf[dstart+dsize] != ',') && (buf[dstart+dsize]!=0)) { dsize++; };
199 strncpy(bf, (char*)buf+dstart, dsize);
200 list[last].size=atoi(bf);
201 fgets(buf, sizeof(buf), fcheat);
202 if ( (buf[0] == 'D') &&
203 (buf[1] == 'a') &&
204 (buf[2] == 't') &&
205 (buf[3] == 'a') &&
206 (buf[4] == '=') ) // Data cheat
207 {
208 int offs = 5;
209 char tmp_buf[9];
210 memset(tmp_buf, 0, 9);
211
212 for (int j=0; j<list[last].num; j++)
213 {
214 strncpy(tmp_buf, (char *)buf+offs, 8);
215 sscanf_s(tmp_buf, "%x", &list[last].hi[j]);
216 offs+=8;
217 strncpy(tmp_buf, (char *)buf+offs, 8);
218 sscanf_s(tmp_buf, "%x", &list[last].lo[j]);
219 offs+=8;
220 if (buf[offs] != ',') continue; //error
221 offs++; // skip comma
222 }
223 }
224 else
225 continue; // error
226 }
227 else
228 continue; // error
229
230 last++;
231 } // else parser error
232 }
233 fclose(fcheat);
234
235 //INFO("Loaded %i cheats\n", last);
236 num = last;
237
238 convertSerial();
239 return true;
240 }
241 return false;
242 }
243
244 bool getVersion(char *fname, u8 &major, u8 &minor)
245 {
246 FILE *fp = fopen(fname, "r");
247 major = 0;
248 minor = 0;
249 memset(ROMserial, 0, sizeof(ROMserial));
250
251 if (fp)
252 {
253 char buf[1024] = { 0 };
254 fgets(buf, 1024, fp);
255 strcpy(buf, trim(buf));
256 if (strlen(buf) == 0)
257 {
258 fclose(fp);
259 return false;
260 }
261
262 if (strncmp(buf, "; DeSmuME cheat file. VERSION ", 29) == 0)
263 {
264 char bf[10] = {0};
265 strncpy(bf, (char*)(buf + 30), 1);
266 major = atoi(bf);
267 strncpy(bf, (char*)(buf + 32), 3);
268 minor = atoi(bf);
269
270 while (!feof(fp))
271 {
272 fgets(buf, 1024, fp);
273 strcpy(buf, trim(buf));
274 if ( (buf[0] == 'N') &&
275 (buf[1] == 'a') &&
276 (buf[2] == 'm') &&
277 (buf[3] == 'e') &&
278 (buf[4] == '=') ) // serial
279 {
280 strcpy(ROMserial, (char *)(buf+5));
281 break;
282 }
283 }
284
285 fclose(fp);
286 return true;
287 }
288
289 if (strncmp(buf, "[General]", 9) == 0)
290 {
291 major = 1; minor = 0;
292 fclose(fp);
293 return true;
294 }
295
296 fclose(fp);
297 }
298 return false;
299 }
300
301 bool convertFile(char *fname)
302 {
303 u8 majorVersion = 0;
304 u8 minorVersion = 0;
305
306 if (!getVersion(fname, majorVersion, minorVersion))
307 return false;
308 //printf(" - version %i.%03i - ", majorVersion, minorVersion);
309
310 switch(majorVersion)
311 {
312 case 1:
313 switch (minorVersion)
314 {
315 case 0:
316 {
317 if (!load_1_0(fname)) return false;
318 if (!save(fname)) return false;
319 return true;
320 }
321 case 3:
322 {
323 if (!load_1_3(fname)) return false;
324 if (!save(fname)) return false;
325 return true;
326 }
327
328 default:
329 return false;
330 }
331 break;
332 case 2:
333 return false;
334 }
335
336 return false;
337 }
338