From 34e22f57f04180d71772f192dad32df6e82fbe6d Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 24 Sep 2017 12:40:12 -0400 Subject: GBA now sends over contents of boxes --- source/deserialize.c | 215 +++++++++++++++++++++++++++++++++++++++++++++++++ source/deserialize.h | 16 ++++ source/http.c | 4 +- source/main.c | 220 +++++++-------------------------------------------- 4 files changed, 261 insertions(+), 194 deletions(-) create mode 100644 source/deserialize.c create mode 100644 source/deserialize.h (limited to 'source') diff --git a/source/deserialize.c b/source/deserialize.c new file mode 100644 index 0000000..e6fdf3b --- /dev/null +++ b/source/deserialize.c @@ -0,0 +1,215 @@ +/* + * Copyright (C) 2017 hatkirby + * + * This software may be modified and distributed under the terms + * of the MIT license. See the LICENSE file for details. + */ +#include "deserialize.h" +#include +#include "encoding.h" + +cJSON* pokemonToJson(const struct PokemonIntermediate* pki) +{ + char d_pokename[31]; + decodePokemonCharset(pki->nickname, 10, d_pokename, pki->language); + + char d_otName[22]; + decodePokemonCharset(pki->otName, 7, d_otName, pki->language); + + char d_key[57]; + sprintf( + d_key, + "%08lx%08lx%08lx%08lx%08lx%08lx%08lx", + pki->key[0], + pki->key[1], + pki->key[2], + pki->key[3], + pki->key[4], + pki->key[5], + pki->key[6]); + + cJSON* jPoke = cJSON_CreateObject(); + + cJSON_AddNumberToObject( + jPoke, + "species", + __builtin_bswap16(pki->species)); + + cJSON_AddItemToObject( + jPoke, + "nickname", + cJSON_CreateString(d_pokename)); + + cJSON_AddItemToObject( + jPoke, + "otName", + cJSON_CreateString(d_otName)); + + cJSON_AddNumberToObject( + jPoke, + "otId", + __builtin_bswap16(pki->otId)); + + cJSON_AddNumberToObject( + jPoke, + "level", + pki->level); + + cJSON_AddNumberToObject( + jPoke, + "hp", + __builtin_bswap32(pki->hp)); + + cJSON_AddNumberToObject( + jPoke, + "attack", + __builtin_bswap32(pki->attack)); + + cJSON_AddNumberToObject( + jPoke, + "defense", + __builtin_bswap32(pki->defense)); + + cJSON_AddNumberToObject( + jPoke, + "speed", + __builtin_bswap32(pki->speed)); + + cJSON_AddNumberToObject( + jPoke, + "spAttack", + __builtin_bswap32(pki->spAttack)); + + cJSON_AddNumberToObject( + jPoke, + "spDefense", + __builtin_bswap32(pki->spDefense)); + + cJSON_AddNumberToObject( + jPoke, + "coolness", + pki->cool); + + cJSON_AddNumberToObject( + jPoke, + "beauty", + pki->beauty); + + cJSON_AddNumberToObject( + jPoke, + "cuteness", + pki->cute); + + cJSON_AddNumberToObject( + jPoke, + "smartness", + pki->smart); + + cJSON_AddNumberToObject( + jPoke, + "toughness", + pki->tough); + + cJSON_AddNumberToObject( + jPoke, + "sheen", + pki->sheen); + + cJSON_AddItemToObject( + jPoke, + "key", + cJSON_CreateString(d_key)); + + cJSON_AddNumberToObject( + jPoke, + "experience", + __builtin_bswap32(pki->experience)); + + cJSON_AddNumberToObject( + jPoke, + "heldItem", + __builtin_bswap16(pki->heldItem)); + + cJSON* jMoves = cJSON_CreateArray(); + + for (int j=0; j<4; j++) + { + if (pki->moves[j] != 0) + { + cJSON* jMove = cJSON_CreateObject(); + + cJSON_AddNumberToObject( + jMove, + "id", + __builtin_bswap16(pki->moves[j])); + + cJSON_AddNumberToObject( + jMove, + "ppBonuses", + (pki->ppBonuses >> (2*j)) & 3); + + cJSON_AddItemToArray(jMoves, jMove); + } else { + break; + } + } + + cJSON_AddItemToObject( + jPoke, + "moves", + jMoves); + + if (pki->otGender) + { + cJSON_AddStringToObject(jPoke, "otGender", "female"); + } else { + cJSON_AddStringToObject(jPoke, "otGender", "male"); + } + + cJSON_AddNumberToObject( + jPoke, + "metLevel", + pki->metLevel); + + cJSON_AddNumberToObject( + jPoke, + "metLocation", + pki->metLocation); + + cJSON_AddBoolToObject( + jPoke, + "shiny", + pki->shiny); + + cJSON_AddNumberToObject( + jPoke, + "nature", + pki->nature); + + if (pki->gender == 0) + { + cJSON_AddStringToObject(jPoke, "gender", "male"); + } else if (pki->gender == 1) + { + cJSON_AddStringToObject(jPoke, "gender", "female"); + } else if (pki->gender == 2) + { + cJSON_AddStringToObject(jPoke, "gender", "genderless"); + } + + cJSON_AddBoolToObject( + jPoke, + "secondAbility", + pki->altAbility); + + // Handle Unown form. + if (pki->species == 201) + { + cJSON_AddNumberToObject( + jPoke, + "unownLetter", + pki->unownLetter); + } + + return jPoke; +} diff --git a/source/deserialize.h b/source/deserialize.h new file mode 100644 index 0000000..4ed8754 --- /dev/null +++ b/source/deserialize.h @@ -0,0 +1,16 @@ +/* + * Copyright (C) 2017 hatkirby + * + * This software may be modified and distributed under the terms + * of the MIT license. See the LICENSE file for details. + */ +#ifndef DESERIALIZE_H_3E597847 +#define DESERIALIZE_H_3E597847 + +#include +#include "pokemon.h" +#include "cJSON.h" + +cJSON* pokemonToJson(const struct PokemonIntermediate* pki); + +#endif /* end of include guard: DESERIALIZE_H_3E597847 */ diff --git a/source/http.c b/source/http.c index 1979e22..be62dce 100644 --- a/source/http.c +++ b/source/http.c @@ -58,7 +58,7 @@ static int tcpWrite( result = net_write(*(s32*)firstParam, buffer, block); } - if ((result == 0) || (result == -56)) + if ((result == 0) || (result == -56) || (result == -11)) { usleep(20 * 1000); @@ -73,7 +73,7 @@ static int tcpWrite( sent += result; left -= result; buffer += result; - usleep(100); + usleep(1000); if ((sent / TCP_BLOCK_SIZE) > step) { diff --git a/source/main.c b/source/main.c index f066a7d..de36755 100644 --- a/source/main.c +++ b/source/main.c @@ -19,6 +19,7 @@ #include "pokemon.h" #include "http.h" #include "netinf.h" +#include "deserialize.h" void printmain() { @@ -285,7 +286,7 @@ void* extractor(void* userdata) sendMsg(1); - // Start receiving party pokémon. + // Start receiving pokémon. printf("Getting party...\n"); u32 partyCount = getMsg(); @@ -301,214 +302,49 @@ void* extractor(void* userdata) struct PokemonIntermediate* pki = (struct PokemonIntermediate*)(&rawdata); - char d_pokename[31]; - decodePokemonCharset(pki->nickname, 10, d_pokename, pki->language); - - char d_otName[22]; - decodePokemonCharset(pki->otName, 7, d_otName, pki->language); - - char d_key[57]; - sprintf( - d_key, - "%08lx%08lx%08lx%08lx%08lx%08lx%08lx", - pki->key[0], - pki->key[1], - pki->key[2], - pki->key[3], - pki->key[4], - pki->key[5], - pki->key[6]); - - cJSON* jPoke = cJSON_CreateObject(); - - cJSON_AddNumberToObject( - jPoke, - "species", - __builtin_bswap16(pki->species)); - - cJSON_AddItemToObject( - jPoke, - "nickname", - cJSON_CreateString(d_pokename)); - - cJSON_AddItemToObject( - jPoke, - "otName", - cJSON_CreateString(d_otName)); - - cJSON_AddNumberToObject( - jPoke, - "otId", - __builtin_bswap16(pki->otId)); - - cJSON_AddNumberToObject( - jPoke, - "level", - pki->level); - - cJSON_AddNumberToObject( - jPoke, - "hp", - __builtin_bswap32(pki->hp)); - - cJSON_AddNumberToObject( - jPoke, - "attack", - __builtin_bswap32(pki->attack)); - - cJSON_AddNumberToObject( - jPoke, - "defense", - __builtin_bswap32(pki->defense)); - - cJSON_AddNumberToObject( - jPoke, - "speed", - __builtin_bswap32(pki->speed)); - - cJSON_AddNumberToObject( - jPoke, - "spAttack", - __builtin_bswap32(pki->spAttack)); - - cJSON_AddNumberToObject( - jPoke, - "spDefense", - __builtin_bswap32(pki->spDefense)); - - cJSON_AddNumberToObject( - jPoke, - "coolness", - pki->cool); - - cJSON_AddNumberToObject( - jPoke, - "beauty", - pki->beauty); - - cJSON_AddNumberToObject( - jPoke, - "cuteness", - pki->cute); - - cJSON_AddNumberToObject( - jPoke, - "smartness", - pki->smart); - - cJSON_AddNumberToObject( - jPoke, - "toughness", - pki->tough); - - cJSON_AddNumberToObject( - jPoke, - "sheen", - pki->sheen); - - cJSON_AddItemToObject( - jPoke, - "key", - cJSON_CreateString(d_key)); - - cJSON_AddNumberToObject( - jPoke, - "experience", - __builtin_bswap32(pki->experience)); - - cJSON_AddNumberToObject( - jPoke, - "heldItem", - __builtin_bswap16(pki->heldItem)); - - cJSON* jMoves = cJSON_CreateArray(); - - for (int j=0; j<4; j++) - { - if (pki->moves[j] != 0) - { - cJSON* jMove = cJSON_CreateObject(); - - cJSON_AddNumberToObject( - jMove, - "id", - __builtin_bswap16(pki->moves[j])); + cJSON* jPoke = pokemonToJson(pki); - cJSON_AddNumberToObject( - jMove, - "ppBonuses", - (pki->ppBonuses >> (2*j)) & 3); + cJSON_AddStringToObject(jPoke, "storage", "party"); + cJSON_AddNumberToObject(jPoke, "slot", i); - cJSON_AddItemToArray(jMoves, jMove); - } else { - break; - } - } + cJSON_AddItemToArray(jParty, jPoke); + } - cJSON_AddItemToObject( - jPoke, - "moves", - jMoves); + for (int i=0; i<14; i++) + { + printf("Getting box %d...\n", i+1); - if (pki->otGender) + for (int j=0; j<30; j++) { - cJSON_AddStringToObject(jPoke, "otGender", "female"); - } else { - cJSON_AddStringToObject(jPoke, "otGender", "male"); - } - - cJSON_AddNumberToObject( - jPoke, - "metLevel", - pki->metLevel); + int isPoke = getMsg(); - cJSON_AddNumberToObject( - jPoke, - "metLocation", - pki->metLocation); + if (isPoke == 1) + { + usleep(5000); - cJSON_AddBoolToObject( - jPoke, - "shiny", - pki->shiny); + u32 rawdata[sizeof(struct PokemonIntermediate) / 4]; + getMsgArr(rawdata, sizeof(struct PokemonIntermediate) / 4); - cJSON_AddNumberToObject( - jPoke, - "nature", - pki->nature); + struct PokemonIntermediate* pki = + (struct PokemonIntermediate*)(&rawdata); - if (pki->gender == 0) - { - cJSON_AddStringToObject(jPoke, "gender", "male"); - } else if (pki->gender == 1) - { - cJSON_AddStringToObject(jPoke, "gender", "female"); - } else if (pki->gender == 2) - { - cJSON_AddStringToObject(jPoke, "gender", "genderless"); - } + cJSON* jPoke = pokemonToJson(pki); - cJSON_AddBoolToObject( - jPoke, - "secondAbility", - pki->altAbility); + cJSON_AddStringToObject(jPoke, "storage", "box"); + cJSON_AddNumberToObject(jPoke, "box", i); + cJSON_AddNumberToObject(jPoke, "slot", j); - // Handle Unown form. - if (pki->species == 201) - { - cJSON_AddNumberToObject( - jPoke, - "unownLetter", - pki->unownLetter); + cJSON_AddItemToArray(jParty, jPoke); + } } - - cJSON_AddItemToArray(jParty, jPoke); } cJSON_AddItemToObject(root, "pokemon", jParty); char *rendered = cJSON_Print(root); - printf("%s\n", rendered); + //printf("%s\n", rendered); + + printf("Press A to send.\n"); waitForButtons(PAD_BUTTON_A); -- cgit 1.4.1