about summary refs log tree commit diff stats
path: root/gba/source/gamedata.c
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2017-07-11 16:37:21 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2017-07-11 16:37:21 -0400
commit57afb4058710a978bd7b07a368125d04378c62f1 (patch)
tree7a06f1679f0612c58d5a209a013261ff5ad6cb82 /gba/source/gamedata.c
parent004575f7cec14946c1936aceca6efee38b7f8a74 (diff)
downloadgen3uploader-57afb4058710a978bd7b07a368125d04378c62f1.tar.gz
gen3uploader-57afb4058710a978bd7b07a368125d04378c62f1.tar.bz2
gen3uploader-57afb4058710a978bd7b07a368125d04378c62f1.zip
started tweaking with stuff
Diffstat (limited to 'gba/source/gamedata.c')
-rw-r--r--gba/source/gamedata.c534
1 files changed, 534 insertions, 0 deletions
diff --git a/gba/source/gamedata.c b/gba/source/gamedata.c new file mode 100644 index 0000000..19d5100 --- /dev/null +++ b/gba/source/gamedata.c
@@ -0,0 +1,534 @@
1/*
2 * Pokemon Gen III Data Extractor by hatkirby 2017.
3 *
4 * This software may be modified and distributed under the terms
5 * of the MIT license. See the LICENSE file for details.
6 *
7 */
8#include "gamedata.h"
9
10void decryptSaveStructures(
11 pSaveBlock1 SaveBlock1,
12 pSaveBlock2 SaveBlock2,
13 pSaveBlock3 SaveBlock3)
14{
15 if (GAME_RS)
16 {
17 // R/S doesn't have save crypto.
18 return;
19 }
20
21 u8* sb1raw = (u8*)SaveBlock1;
22 u8* sb2raw = (u8*)SaveBlock2;
23 //u8* sb3raw = (u8*)SaveBlock3; // unused
24
25 u32* xor_key_ptr = (u32*)(&sb2raw[( GAME_EM ? 0xA8 : 0xF20 )]);
26
27 u32 xor_key = *xor_key_ptr;
28 u16 xor_key16 = (u16)xor_key;
29 if (!xor_key)
30 {
31 // xor key is zero, nothing needs to be done.
32 return;
33 }
34
35 u32* ptr_to_xor;
36 u32 save_offset;
37 int i;
38 u32* bag_pocket_offsets;
39 u32* bag_pocket_counts;
40 if (GAME_FRLG)
41 {
42 // loop over and decrypt various things
43 save_offset = 0x3D38 + 4;
44 for (i = 3; i >= 0; i--)
45 {
46 ptr_to_xor = (u32*)(&sb1raw[save_offset]);
47 *ptr_to_xor ^= xor_key;
48 save_offset += 12;
49 }
50
51 for (i = 0; i <= 0x3f; i++)
52 {
53 save_offset = 0x1200 + (i*sizeof(u32));
54 ptr_to_xor = (u32*)(&sb1raw[save_offset]);
55 *ptr_to_xor ^= xor_key;
56 }
57
58 // loop over each of the bag pockets and decrypt decrypt decrypt
59 bag_pocket_offsets = (u32[5]) { 0x310, 0x388, 0x430, 0x464, 0x54C };
60 bag_pocket_counts = (u32[5]) { 42, 30, 13, 58, 43 };
61
62 for (i = 0; i < 5; i++)
63 {
64 for (int bag_i = 0; bag_i < bag_pocket_counts[i]; bag_i++)
65 {
66 save_offset = bag_pocket_offsets[i] + (sizeof(u32) * bag_i) + 2;
67 *(u16*)(&sb1raw[save_offset]) ^= xor_key16;
68 }
69 }
70
71 // decrypt some more stuff
72 save_offset = 0xaf8;
73 ptr_to_xor = (u32*)(&sb1raw[save_offset]);
74 *ptr_to_xor ^= xor_key;
75
76 save_offset = 0x290;
77 ptr_to_xor = (u32*)(&sb1raw[save_offset]);
78 *ptr_to_xor ^= xor_key;
79
80 save_offset = 0x294;
81 *(u16*)(&sb1raw[save_offset]) ^= xor_key16;
82 } else {
83 // Emerald
84
85 // loop over and decrypt various things
86 for (i = 0; i <= 0x3f; i++)
87 {
88 save_offset = 0x159c + (i*sizeof(u32));
89 ptr_to_xor = (u32*)(&sb1raw[save_offset]);
90 *ptr_to_xor ^= xor_key;
91 }
92
93 // loop over each of the bag pockets and decrypt decrypt decrypt
94 bag_pocket_offsets = (u32[5]) { 0x560, 0x5D8, 0x650, 0x690, 0x790 };
95 bag_pocket_counts = (u32[5]) { 30, 30, 16, 64, 46 };
96
97 for (i = 0; i < 5; i++)
98 {
99 for (int bag_i = 0; bag_i < bag_pocket_counts[i]; bag_i++)
100 {
101 save_offset = bag_pocket_offsets[i] + (sizeof(u32) * bag_i) + 2;
102 *(u16*)(&sb1raw[save_offset]) ^= xor_key16;
103 }
104 }
105
106 // decrypt some more stuff
107 save_offset = 0x1F4;
108 ptr_to_xor = (u32*)(&sb1raw[save_offset]);
109 *ptr_to_xor ^= xor_key;
110
111 save_offset = 0x490;
112 ptr_to_xor = (u32*)(&sb1raw[save_offset]);
113 *ptr_to_xor ^= xor_key;
114
115 save_offset = 0x494;
116 *(u16*)(&sb1raw[save_offset]) ^= xor_key16;
117 }
118
119 *xor_key_ptr = 0;
120}
121
122bool initSaveData(
123 pSaveBlock1* SaveBlock1,
124 pSaveBlock2* SaveBlock2,
125 pSaveBlock3* SaveBlock3)
126{
127 // check the ROM code, make sure this game is supported.
128 u8* ROM = (u8*) 0x8000000;
129
130 u32 gamecode = (*(u32*)(&ROM[0xAC]));
131
132 void(*loadsave)(char a1);
133 //void(*mainloop)();
134 //void(*load_pokemon)();
135 pSaveBlock1 gSaveBlock1;
136 pSaveBlock2 gSaveBlock2;
137 pSaveBlock3 gSaveBlock3;
138 //u32 titlemid = 0;
139
140 // get the address of the save loading function.
141 switch (gamecode)
142 {
143 // --- R/S ---
144 case 'DVXA': // Ruby German
145 case 'DPXA': // Sapphire German
146 {
147 // TODO: detect debug ROM?
148 gSaveBlock1 = (pSaveBlock1) 0x2025734;
149 gSaveBlock2 = (pSaveBlock2) 0x2024EA4;
150 gSaveBlock3 = (pSaveBlock3) 0x20300A0;
151 loadsave = (void(*)(char)) 0x8126249; // same for v1.0 + v1.1
152 //mainloop = (void(*)()) 0x80003D9;
153 //load_pokemon = (void(*)()) 0x8047da9;
154
155 break;
156 }
157
158 case 'FVXA': // Ruby French
159 case 'FPXA': // Sapphire French
160 {
161 gSaveBlock1 = (pSaveBlock1) 0x2025734;
162 gSaveBlock2 = (pSaveBlock2) 0x2024EA4;
163 gSaveBlock3 = (pSaveBlock3) 0x20300A0;
164 loadsave = (void(*)(char)) 0x8126351; // same for v1.0 + v1.1
165 //mainloop = (void(*)()) 0x80003D9;
166 //load_pokemon = (void(*)()) 0x8047e95;
167
168 break;
169 }
170
171 case 'IVXA': // Ruby Italian
172 case 'IPXA': // Sapphire Italian
173 {
174 gSaveBlock1 = (pSaveBlock1) 0x2025734;
175 gSaveBlock2 = (pSaveBlock2) 0x2024EA4;
176 gSaveBlock3 = (pSaveBlock3) 0x20300A0;
177 loadsave = (void(*)(char)) 0x8126249; // same for v1.0 + v1.1
178 //mainloop = (void(*)()) 0x80003D9;
179 //load_pokemon = (void(*)()) 0x8047dbd;
180
181 break;
182 }
183
184 case 'SVXA': // Ruby Spanish
185 case 'SPXA': // Sapphire Spanish
186 {
187 gSaveBlock1 = (pSaveBlock1) 0x2025734;
188 gSaveBlock2 = (pSaveBlock2) 0x2024EA4;
189 gSaveBlock3 = (pSaveBlock3) 0x20300A0;
190 loadsave = (void(*)(char)) 0x8126349; // same for v1.0 + v1.1
191 //mainloop = (void(*)()) 0x80003D9;
192 //load_pokemon = (void(*)()) 0x8047ea5;
193
194 break;
195 }
196
197 case 'EVXA': // Ruby English
198 case 'EPXA': // Sapphire English
199 {
200 gSaveBlock1 = (pSaveBlock1) 0x2025734;
201 gSaveBlock2 = (pSaveBlock2) 0x2024EA4;
202 gSaveBlock3 = (pSaveBlock3) 0x20300A0;
203 //mainloop = (void(*)()) 0x80002A5;
204
205 // version number
206 switch (ROM[0xBC])
207 {
208 case 0:
209 {
210 loadsave = (void(*)(char)) 0x8125EC9;
211 //load_pokemon = (void(*)()) 0x8047a85;
212
213 break;
214 }
215
216 case 1:
217 case 2:
218 {
219 loadsave = (void(*)(char)) 0x8125EE9;
220 //load_pokemon = (void(*)()) 0x8047aa5;
221
222 break;
223 }
224
225 default:
226 {
227 return false; // unsupported version
228 }
229 }
230
231 break;
232 }
233
234 case 'JVXA': // Ruby Japanese
235 case 'JPXA': // Sapphire Japanese
236 {
237 gSaveBlock1 = (pSaveBlock1) 0x2025494;
238 gSaveBlock2 = (pSaveBlock2) 0x2024C04;
239 gSaveBlock3 = (pSaveBlock3) 0x202FDBC;
240 loadsave = (void(*)(char)) 0x8120d05; // same for v1.0 + v1.1
241 //mainloop = (void(*)()) 0x80002A9;
242 //load_pokemon = (void(*)()) 0x8044d55;
243
244 break;
245 }
246
247 /// --- FR/LG ---
248 // In FR/LG, the function that initialises the save-block pointers to default does not set up saveblock3.
249 // Which will need to be set up before loading the save if we want boxed Pokémon to not disappear.
250 // Oh, and loadsave() offset is different between FR and LG...
251
252 case 'DRPB': // FireRed German
253 case 'DGPB': // LeafGreen German
254 {
255 gSaveBlock1 = (pSaveBlock1) 0x202552C;
256 gSaveBlock2 = (pSaveBlock2) 0x2024588;
257 gSaveBlock3 = (pSaveBlock3) 0x2029314;
258 *(pSaveBlock3*)(0x3004f60) = gSaveBlock3;
259 loadsave = (void(*)(char)) ( GAME_FR ? 0x80da721 : 0x80da6f5 );
260 //mainloop = (void(*)()) 0x8000425;
261 //titlemid = 0x80791df;
262 //load_pokemon = (void(*)()) 0x804c251;
263
264 break;
265 }
266
267 case 'FRPB': // FireRed French
268 case 'FGPB': // LeafGreen French
269 {
270 gSaveBlock1 = (pSaveBlock1) 0x202552C;
271 gSaveBlock2 = (pSaveBlock2) 0x2024588;
272 gSaveBlock3 = (pSaveBlock3) 0x2029314;
273 *(pSaveBlock3*)(0x3004f60) = gSaveBlock3;
274 loadsave = (void(*)(char)) ( GAME_FR ? 0x80da7e1 : 0x80da7b5 );
275 //mainloop = (void(*)()) 0x8000417;
276 //titlemid = 0x807929f;
277 //load_pokemon = (void(*)()) 0x804c311;
278
279 break;
280 }
281
282 case 'IRPB': // FireRed Italian
283 case 'IGPB': // LeafGreen Italian
284 {
285 gSaveBlock1 = (pSaveBlock1) 0x202552C;
286 gSaveBlock2 = (pSaveBlock2) 0x2024588;
287 gSaveBlock3 = (pSaveBlock3) 0x2029314;
288 *(pSaveBlock3*)(0x3004f60) = gSaveBlock3;
289 loadsave = (void(*)(char)) ( GAME_FR ? 0x80da721 : 0x80da6f5 );
290 //mainloop = (void(*)()) 0x8000425;
291 //titlemid = 0x80791cb;
292 //load_pokemon = (void(*)()) 0x804c23d;
293
294 break;
295 }
296
297 case 'SRPB': // FireRed Spanish
298 case 'SGPB': // LeafGreen Spanish
299 {
300 gSaveBlock1 = (pSaveBlock1) 0x202552C;
301 gSaveBlock2 = (pSaveBlock2) 0x2024588;
302 gSaveBlock3 = (pSaveBlock3) 0x2029314;
303 *(pSaveBlock3*)(0x3004f60) = gSaveBlock3;
304 loadsave = (void(*)(char)) ( GAME_FR ? 0x80da809 : 0x80da7dd );
305 //mainloop = (void(*)()) 0x8000417;
306 //titlemid = 0x80792b3;
307 //load_pokemon = (void(*)()) 0x804c325;
308
309 break;
310 }
311
312 case 'ERPB': // FireRed English
313 case 'EGPB': // LeafGreen English
314 {
315 gSaveBlock1 = (pSaveBlock1) 0x202552C;
316 gSaveBlock2 = (pSaveBlock2) 0x2024588;
317 gSaveBlock3 = (pSaveBlock3) 0x2029314;
318 *(pSaveBlock3*)(0x3005010) = gSaveBlock3;
319
320 // version number
321 switch (ROM[0xBC])
322 {
323 case 0:
324 {
325 loadsave = (void(*)(char)) ( GAME_FR ? 0x80da4fd : 0x80da4d1 );
326 //mainloop = (void(*)()) 0x800041b;
327 //titlemid = 0x807927b;
328 //load_pokemon = (void(*)()) 0x804c231;
329
330 break;
331 }
332
333 case 1:
334 {
335 loadsave = (void(*)(char)) ( GAME_FR ? 0x80da511 : 0x80da4e5 );
336 //mainloop = (void(*)()) 0x8000429;
337 //titlemid = 0x807928f;
338 //load_pokemon = (void(*)()) 0x804c245;
339
340 break;
341 }
342
343 default:
344 {
345 return false; // unsupported version
346 }
347 }
348
349 break;
350 }
351
352 case 'JRPB': // FireRed Japanese
353 case 'JGPB': // LeafGreen Japanese
354 {
355 gSaveBlock1 = (pSaveBlock1) 0x202548C;
356 gSaveBlock2 = (pSaveBlock2) 0x20244E8;
357 gSaveBlock3 = (pSaveBlock3) 0x202924C;
358 *(pSaveBlock3*)(0x3005050) = gSaveBlock3;
359
360 // version number
361 switch (ROM[0xBC])
362 {
363 case 0:
364 {
365 loadsave = (void(*)(char)) ( GAME_FR ? 0x80db4e5 : 0x80db4b9 );
366 //mainloop = (void(*)()) 0x800041b;
367 //titlemid = 0x8078a0f;
368 //load_pokemon = (void(*)()) 0x804b9e9;
369
370 break;
371 }
372
373 case 1:
374 {
375 if ((gamecode << 8) == 'GPB\x00')
376 {
377 // LeafGreen v1.1 Japanese is undumped.
378 // Therefore, it is unsupported.
379 // I will make guesses at the offsets in the comments, but I will not actually implement them until LeafGreen v1.1 is dumped.
380
381 return false;
382 }
383
384 loadsave = (void(*)(char)) 0x80db529; // potential LG1.1 address: 0x80db4fd
385 //mainloop = (void(*)()) 0x8000417;
386 //titlemid = 0x8078987;
387 //load_pokemon = (void(*)()) 0x804b9c5;
388
389 break;
390 }
391
392 default:
393 {
394 return false; // unsupported version
395 }
396 }
397
398 break;
399 }
400
401 /// --- Emerald ---
402 // In Emerald, the saveblock pointer that isn't set up is saveblock1 (in FR/LG it was saveblock3).
403 // The initial save loading code after the copyright screen is also updated, now it sets up ASLR/crypto here before loading the save.
404
405 case 'DEPB': // Emerald German
406 {
407 gSaveBlock1 = (pSaveBlock1) 0x2025A00;
408 gSaveBlock2 = (pSaveBlock2) 0x2024A54;
409 gSaveBlock3 = (pSaveBlock3) 0x2029808;
410 *(pSaveBlock1*)(0x3005d8c) = gSaveBlock1;
411 loadsave = (void(*)(char)) 0x8153075;
412 //mainloop = (void(*)()) 0x800042b;
413 //titlemid = 0x816fdb5;
414 //load_pokemon = (void(*)()) 0x8076dd5;
415
416 break;
417 }
418
419 case 'FEPB': // Emerald French
420 {
421 gSaveBlock1 = (pSaveBlock1) 0x2025A00;
422 gSaveBlock2 = (pSaveBlock2) 0x2024A54;
423 gSaveBlock3 = (pSaveBlock3) 0x2029808;
424 *(pSaveBlock1*)(0x3005d8c) = gSaveBlock1;
425 loadsave = (void(*)(char)) 0x815319d;
426 //mainloop = (void(*)()) 0x800042b;
427 //titlemid = 0x816fedd;
428 //load_pokemon = (void(*)()) 0x8076dd1;
429
430 break;
431 }
432
433 case 'IEPB': // Emerald Italian
434 {
435 gSaveBlock1 = (pSaveBlock1) 0x2025A00;
436 gSaveBlock2 = (pSaveBlock2) 0x2024A54;
437 gSaveBlock3 = (pSaveBlock3) 0x2029808;
438 *(pSaveBlock1*)(0x3005d8c) = gSaveBlock1;
439 loadsave = (void(*)(char)) 0x8153065;
440 //mainloop = (void(*)()) 0x800042b;
441 //titlemid = 0x816fda5;
442 //load_pokemon = (void(*)()) 0x8076dd5;
443
444 break;
445 }
446
447 case 'SEPB': // Emerald Spanish
448 {
449 gSaveBlock1 = (pSaveBlock1) 0x2025A00;
450 gSaveBlock2 = (pSaveBlock2) 0x2024A54;
451 gSaveBlock3 = (pSaveBlock3) 0x2029808;
452 *(pSaveBlock1*)(0x3005d8c) = gSaveBlock1;
453 loadsave = (void(*)(char)) 0x8153175;
454 //mainloop = (void(*)()) 0x800042b;
455 //titlemid = 0x816feb5;
456 //load_pokemon = (void(*)()) 0x8076dd1;
457
458 break;
459 }
460
461 case 'EEPB': // Emerald English
462 {
463 gSaveBlock1 = (pSaveBlock1) 0x2025A00;
464 gSaveBlock2 = (pSaveBlock2) 0x2024A54;
465 gSaveBlock3 = (pSaveBlock3) 0x2029808;
466 *(pSaveBlock1*)(0x3005d8c) = gSaveBlock1;
467 loadsave = (void(*)(char)) 0x81534d1;
468 //mainloop = (void(*)()) 0x800042b;
469 //titlemid = 0x817014d;
470 //load_pokemon = (void(*)()) 0x8076dd5;
471
472 break;
473 }
474
475 case 'JEPB': // Emerald Japanese
476 {
477 gSaveBlock1 = (pSaveBlock1) 0x20256A4;
478 gSaveBlock2 = (pSaveBlock2) 0x20246F8;
479 gSaveBlock3 = (pSaveBlock3) 0x20294AC;
480 *(pSaveBlock1*)(0x3005aec) = gSaveBlock1;
481 loadsave = (void(*)(char)) 0x815340d;
482 //mainloop = (void(*)()) 0x800042b;
483 //titlemid = 0x816ff45;
484 //load_pokemon = (void(*)()) 0x80767dd;
485
486 break;
487 }
488
489 default:
490 {
491 return false; // this game isn't supported
492 }
493 }
494
495 loadsave(0);
496
497 // now the save is loaded, we can do what we want with the loaded blocks.
498 // first, we're going to want to decrypt the parts that are crypted, if applicable.
499 decryptSaveStructures(gSaveBlock1,gSaveBlock2,gSaveBlock3);
500
501 *SaveBlock1 = gSaveBlock1;
502 *SaveBlock2 = gSaveBlock2;
503 *SaveBlock3 = gSaveBlock3;
504
505 /*
506 // time to call the payload.
507 payload(gSaveBlock1,gSaveBlock2,gSaveBlock3);
508 // Now, we better call the function that sets the pokemon-related stuff from the structure elements of the loaded save again.
509 // Just in case the payload did something with that.
510 load_pokemon();
511 // In FR/LG/Emerald, just returning to the game is unwise.
512 // The game reloads the savefile.
513 // In FR/LG, this is done at the title screen after setting ASLR/saveblock-crypto up. (probably because at initial save-load, SaveBlock3 ptr isn't set up lol)
514 // So, better bypass the title screen and get the game to return directly to the Continue/New Game screen.
515 // In Emerald, the save reload happens after the Continue option was chosen, so we have no choice but to bypass everything and get the game to go straight to the overworld.
516 // Easiest way to do this is to call into the middle of the function we want, using an ASM wrapper to set up the stack.
517 // Here goes...
518 if (titlemid) {
519 // Function reserves an extra 4 bytes of stack space in FireRed/LeafGreen, and none in Emerald.
520 call_into_middle_of_titlescreen_func(titlemid,(GAME_EM ? 0 : 4));
521 }
522 // Now we've done what we want, time to return to the game.
523 // Can't just return, the game will reload the save.
524 // So let's just call the main-loop directly ;)
525 // turn the sound back on before we head back to the game
526 *(vu16 *)(REG_BASE + 0x84) = 0x8f;
527 // re-enable interrupts
528 REG_IME = 1;
529 mainloop();
530 // Anything past here will not be executed.
531 return 0;
532 */
533 return true;
534}