about summary refs log tree commit diff stats
path: root/source/main.c
blob: 03379d598b19ef8c63ddc6b79d19acbabc4d2551 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
 * Copyright (C) 2017 hatkirby
 * Copyright (C) 2017 slipstream/RoL
 * Copyright (C) 2016 FIX94
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */
#include <gccore.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fat.h>
#include "cJSON.h"
#include "link.h"
#include "encoding.h"
#include "multiboot.h"
#include "pokemon.h"
#include "http.h"
#include "netinf.h"
#include "deserialize.h"

void printmain()
{
  printf("\x1b[2J");
  printf("\x1b[37m");
  printf("Pokemon Gen III Data Extractor by hatkirby\n");
  printf("Based on gba-gen3multiboot by slipstream/RoL\n");
  printf("Based on GBA Link Cable Dumper v1.6 by FIX94\n");
}

void endproc()
{
  printf("Start pressed, exit\n");
  VIDEO_WaitVSync();
  VIDEO_WaitVSync();
  exit(0);
}

u32 waitForButtons(u32 mask)
{
  for (;;)
  {
    PAD_ScanPads();
    VIDEO_WaitVSync();

    u32 btns = PAD_ButtonsDown(0);
    if (btns & mask)
    {
      return btns;
    }
  }
}

void warnError(char *msg)
{
  puts(msg);
  VIDEO_WaitVSync();
  VIDEO_WaitVSync();
  sleep(2);
}

void fatalError(char *msg)
{
  puts(msg);
  VIDEO_WaitVSync();
  VIDEO_WaitVSync();
  sleep(5);
  exit(0);
}

cJSON* getConfig()
{
  FILE* fp = fopen("/gen3uploader.cfg", "r");
  if (!fp)
  {
    fatalError("ERROR: Could not find config file!\n");
  }

  fseek(fp, 0L, SEEK_END);
  long lSize = ftell(fp);
  rewind(fp);

  char* buffer = calloc(1, lSize+1);
  if (!buffer)
  {
    fclose(fp);
    fatalError("ERROR: Could not allocate memory.\n");
  }

  if (fread(buffer, lSize, 1, fp) != 1)
  {
    fclose(fp);
    free(buffer);
    fatalError("ERROR: Could not read config file.\n");
  }

  cJSON* config = cJSON_Parse(buffer);

  fclose(fp);
  free(buffer);

  return config;
}

void* extractor(void* userdata)
{
  cJSON* config = (cJSON*)userdata;

  WOLFSSL_CTX* sslContext = 0;
  if (cJSON_HasObjectItem(config, "certificate"))
  {
    wolfSSL_Init();

    sslContext = wolfSSL_CTX_new(wolfTLSv1_client_method());
    if (sslContext == NULL)
    {
      fatalError("wolfSSL_CTX_new error.\n");
    }

    if (wolfSSL_CTX_load_verify_locations(
      sslContext,
      cJSON_GetObjectItem(config, "certificate")->valuestring,
      0) != SSL_SUCCESS)
    {
      fatalError("Error loading certificate file.\n");
    }
  }

  while (!initializeNetwork())
  {
    printf("Could not initialize network.\n");
    printf("Press A to try again, press B to exit.\n");

    if (waitForButtons(PAD_BUTTON_A | PAD_BUTTON_B) == PAD_BUTTON_B)
    {
      endproc();
    }
  }

  for (;;)
  {
    printmain();

    printf("Press A when GBA is plugged in and turned off.\n");
    waitForButtons(PAD_BUTTON_A);

    printf("Turn on your GBA.\n");
    printf("Waiting for a GBA in port 2...\n");
    waitForGBA();

    printf("GBA Found! Sending multiboot image...\n");
    if (!sendMultibootImage())
    {
      warnError("Failed sending multiboot image.\n");

      continue;
    }

    printf("Waiting for GBA...\n");
    waitForAck();

    VIDEO_WaitVSync();

    // Get game
    // -1 - unsupported game
    //  1 - Ruby
    //  2 - Sapphire
    //  3 - FireRed
    //  4 - LeafGreen
    //  5 - Emerald
    u32 gameId = getMsg();
    if (gameId == -1)
    {
      warnError("ERROR: Unsupported GBA game inserted!\n");

      continue;
    }

    printf("\nPokemon ");
    switch (gameId)
    {
      case 1: printf("Ruby"); break;
      case 2: printf("Sapphire"); break;
      case 3: printf("FireRed"); break;
      case 4: printf("LeafGreen"); break;
      case 5: printf("Emerald"); break;
    }

    printf("\n");
    VIDEO_WaitVSync();
    sleep(1);

    u32 isValid = getMsg();
    if (isValid == -1)
    {
      warnError("ERROR: Unsupported game version inserted!\n");

      continue;
    }

    // Get trainer name
    u8 trainerName[8];

    u32 tnd = getMsg();
    trainerName[0] = (tnd & 0xFF000000) >> 24;
    trainerName[1] = (tnd & 0x00FF0000) >> 16;
    trainerName[2] = (tnd & 0x0000FF00) >> 8;
    trainerName[3] = (tnd & 0x000000FF);

    tnd = getMsg();
    trainerName[4] = (tnd & 0xFF000000) >> 24;
    trainerName[5] = (tnd & 0x00FF0000) >> 16;
    trainerName[6] = (tnd & 0x0000FF00) >> 8;
    trainerName[7] = (tnd & 0x000000FF);

    // Get trainer ID
    u32 trainerId = getMsg();

    // Get game language.
    enum PokemonLanguage gameLanguage = getMsg();

    char d_trainerName[25];
    decodePokemonCharset(trainerName, 8, d_trainerName, gameLanguage);

    printf("Trainer: %s (%ld)\n", d_trainerName, trainerId);

    // Wait for confirmation.
    printf("Press A to import the data from this game.\n");
    printf("Press B to cancel.\n");
    VIDEO_WaitVSync();

    if (waitForButtons(PAD_BUTTON_A | PAD_BUTTON_B) & PAD_BUTTON_B)
    {
      printf("Cancelling...\n");
      VIDEO_WaitVSync();

      sendMsg(0);

      continue;
    }

    printf("Importing...\n");
    VIDEO_WaitVSync();

    sendMsg(1);

    cJSON* root = cJSON_CreateObject();

    cJSON_AddItemToObject(
      root,
      "playerName",
      cJSON_CreateString(d_trainerName));

    cJSON_AddNumberToObject(root, "playerId", trainerId);
    cJSON_AddNumberToObject(root, "gameId", gameId);
    cJSON_AddNumberToObject(root, "language", gameLanguage);

    // Get Pokédex data
    u32 pokedexSeen[13];
    u32 pokedexCaught[13];

    getMsgArr(pokedexSeen, 13);
    getMsgArr(pokedexCaught, 13);

    printf("Saw: ");
    for (int i=0; i<(13*32); i++)
    {
      if (pokedexSeen[i >> 5] >> (i & 31) & 1)
      {
        printf("#%d, ", i+1);
      }
    }

    printf("\nCaught: ");
    for (int i=0; i<(13*32); i++)
    {
      if (pokedexCaught[i >> 5] >> (i & 31) & 1)
      {
        printf("#%d, ", i+1);
      }
    }

    printf("\n");

    sendMsg(1);

    // Start receiving pokémon.
    printf("Getting party...\n");

    u32 partyCount = getMsg();

    cJSON* jParty = cJSON_CreateArray();
    cJSON* jBoxes = cJSON_CreateArray();

    for (u32 i = 0; i < partyCount; i++)
    {
      usleep(6000);

      u32 rawdata[sizeof(struct PokemonIntermediate) / 4];
      getMsgArr(rawdata, sizeof(struct PokemonIntermediate) / 4);

      struct PokemonIntermediate* pki = (struct PokemonIntermediate*)(&rawdata);

      cJSON* jPoke = pokemonToJson(pki);

      cJSON_AddStringToObject(jPoke, "storage", "party");
      cJSON_AddNumberToObject(jPoke, "slot", i);

      cJSON_AddItemToArray(jParty, jPoke);
    }

    for (int i=0; i<14; i++)
    {
      printf("Getting box %d...\n", i+1);

      u8 boxName[8];

      u32 bnd = getMsg();
      boxName[0] = (bnd & 0xFF000000) >> 24;
      boxName[1] = (bnd & 0x00FF0000) >> 16;
      boxName[2] = (bnd & 0x0000FF00) >> 8;
      boxName[3] = (bnd & 0x000000FF);

      bnd = getMsg();
      boxName[4] = (bnd & 0xFF000000) >> 24;
      boxName[5] = (bnd & 0x00FF0000) >> 16;
      boxName[6] = (bnd & 0x0000FF00) >> 8;
      boxName[7] = (bnd & 0x000000FF);

      char d_boxName[25];
      decodePokemonCharset(boxName, 9, d_boxName, gameLanguage);

      cJSON_AddItemToArray(jBoxes, cJSON_CreateString(d_boxName));

      for (int j=0; j<30; j++)
      {
        usleep(6000);

        int isPoke = getMsg();

        if (isPoke == 1)
        {
          usleep(6000);

          u32 rawdata[sizeof(struct PokemonIntermediate) / 4];
          getMsgArr(rawdata, sizeof(struct PokemonIntermediate) / 4);

          struct PokemonIntermediate* pki =
            (struct PokemonIntermediate*)(&rawdata);

          cJSON* jPoke = pokemonToJson(pki);

          cJSON_AddStringToObject(jPoke, "storage", "box");
          cJSON_AddNumberToObject(jPoke, "box", i);
          cJSON_AddNumberToObject(jPoke, "slot", j);

          cJSON_AddItemToArray(jParty, jPoke);
        }
      }
    }

    cJSON_AddItemToObject(root, "boxes", jBoxes);
    cJSON_AddItemToObject(root, "pokemon", jParty);

    char *rendered = cJSON_Print(root);
    //printf("%s\n", rendered);

    printf("Press A to send.\n");

    waitForButtons(PAD_BUTTON_A);

    int result = submitToApi(
      cJSON_GetObjectItem(config, "url")->valuestring,
      sslContext,
      root,
      cJSON_GetObjectItem(config, "username")->valuestring,
      cJSON_GetObjectItem(config, "password")->valuestring);

    printf("Result: %d\n", result);

    waitForButtons(PAD_BUTTON_A);
  }

  return NULL;
}

int main(int argc, char *argv[])
{
  void *xfb = NULL;
  GXRModeObj *rmode = NULL;
  VIDEO_Init();
  rmode = VIDEO_GetPreferredMode(NULL);
  xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  VIDEO_Configure(rmode);
  VIDEO_SetNextFramebuffer(xfb);
  VIDEO_SetBlack(FALSE);
  VIDEO_Flush();
  VIDEO_WaitVSync();
  if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
  int x = 24, y = 32, w, h;
  w = rmode->fbWidth - (32);
  h = rmode->xfbHeight - (48);
  CON_InitEx(rmode, x, y, w, h);
  VIDEO_ClearFrameBuffer(rmode, xfb, COLOR_BLACK);
  PAD_Init();

  if (!fatInitDefault())
  {
    printmain();
    fatalError("ERROR: Cannot find config file!\n");
  }

  cJSON* config = getConfig();

  initLink();

  lwp_t extractorHandle = (lwp_t)NULL;

  LWP_CreateThread(
    &extractorHandle,   // thread handle
    extractor,          // code
    config,             // userdata
    NULL,               // stack base
    16*1024,            // stack size
    LWP_PRIO_HIGHEST);  // thread priority

  for (;;)
  {
    VIDEO_WaitVSync();
    PAD_ScanPads();

    if (PAD_ButtonsDown(0) & PAD_BUTTON_START)
    {
      endproc();
    }
  }

  return 0;
}