diff options
Diffstat (limited to 'source/deserialize.c')
-rw-r--r-- | source/deserialize.c | 215 |
1 files changed, 215 insertions, 0 deletions
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 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2017 hatkirby | ||
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 | #include "deserialize.h" | ||
8 | #include <stdio.h> | ||
9 | #include "encoding.h" | ||
10 | |||
11 | cJSON* pokemonToJson(const struct PokemonIntermediate* pki) | ||
12 | { | ||
13 | char d_pokename[31]; | ||
14 | decodePokemonCharset(pki->nickname, 10, d_pokename, pki->language); | ||
15 | |||
16 | char d_otName[22]; | ||
17 | decodePokemonCharset(pki->otName, 7, d_otName, pki->language); | ||
18 | |||
19 | char d_key[57]; | ||
20 | sprintf( | ||
21 | d_key, | ||
22 | "%08lx%08lx%08lx%08lx%08lx%08lx%08lx", | ||
23 | pki->key[0], | ||
24 | pki->key[1], | ||
25 | pki->key[2], | ||
26 | pki->key[3], | ||
27 | pki->key[4], | ||
28 | pki->key[5], | ||
29 | pki->key[6]); | ||
30 | |||
31 | cJSON* jPoke = cJSON_CreateObject(); | ||
32 | |||
33 | cJSON_AddNumberToObject( | ||
34 | jPoke, | ||
35 | "species", | ||
36 | __builtin_bswap16(pki->species)); | ||
37 | |||
38 | cJSON_AddItemToObject( | ||
39 | jPoke, | ||
40 | "nickname", | ||
41 | cJSON_CreateString(d_pokename)); | ||
42 | |||
43 | cJSON_AddItemToObject( | ||
44 | jPoke, | ||
45 | "otName", | ||
46 | cJSON_CreateString(d_otName)); | ||
47 | |||
48 | cJSON_AddNumberToObject( | ||
49 | jPoke, | ||
50 | "otId", | ||
51 | __builtin_bswap16(pki->otId)); | ||
52 | |||
53 | cJSON_AddNumberToObject( | ||
54 | jPoke, | ||
55 | "level", | ||
56 | pki->level); | ||
57 | |||
58 | cJSON_AddNumberToObject( | ||
59 | jPoke, | ||
60 | "hp", | ||
61 | __builtin_bswap32(pki->hp)); | ||
62 | |||
63 | cJSON_AddNumberToObject( | ||
64 | jPoke, | ||
65 | "attack", | ||
66 | __builtin_bswap32(pki->attack)); | ||
67 | |||
68 | cJSON_AddNumberToObject( | ||
69 | jPoke, | ||
70 | "defense", | ||
71 | __builtin_bswap32(pki->defense)); | ||
72 | |||
73 | cJSON_AddNumberToObject( | ||
74 | jPoke, | ||
75 | "speed", | ||
76 | __builtin_bswap32(pki->speed)); | ||
77 | |||
78 | cJSON_AddNumberToObject( | ||
79 | jPoke, | ||
80 | "spAttack", | ||
81 | __builtin_bswap32(pki->spAttack)); | ||
82 | |||
83 | cJSON_AddNumberToObject( | ||
84 | jPoke, | ||
85 | "spDefense", | ||
86 | __builtin_bswap32(pki->spDefense)); | ||
87 | |||
88 | cJSON_AddNumberToObject( | ||
89 | jPoke, | ||
90 | "coolness", | ||
91 | pki->cool); | ||
92 | |||
93 | cJSON_AddNumberToObject( | ||
94 | jPoke, | ||
95 | "beauty", | ||
96 | pki->beauty); | ||
97 | |||
98 | cJSON_AddNumberToObject( | ||
99 | jPoke, | ||
100 | "cuteness", | ||
101 | pki->cute); | ||
102 | |||
103 | cJSON_AddNumberToObject( | ||
104 | jPoke, | ||
105 | "smartness", | ||
106 | pki->smart); | ||
107 | |||
108 | cJSON_AddNumberToObject( | ||
109 | jPoke, | ||
110 | "toughness", | ||
111 | pki->tough); | ||
112 | |||
113 | cJSON_AddNumberToObject( | ||
114 | jPoke, | ||
115 | "sheen", | ||
116 | pki->sheen); | ||
117 | |||
118 | cJSON_AddItemToObject( | ||
119 | jPoke, | ||
120 | "key", | ||
121 | cJSON_CreateString(d_key)); | ||
122 | |||
123 | cJSON_AddNumberToObject( | ||
124 | jPoke, | ||
125 | "experience", | ||
126 | __builtin_bswap32(pki->experience)); | ||
127 | |||
128 | cJSON_AddNumberToObject( | ||
129 | jPoke, | ||
130 | "heldItem", | ||
131 | __builtin_bswap16(pki->heldItem)); | ||
132 | |||
133 | cJSON* jMoves = cJSON_CreateArray(); | ||
134 | |||
135 | for (int j=0; j<4; j++) | ||
136 | { | ||
137 | if (pki->moves[j] != 0) | ||
138 | { | ||
139 | cJSON* jMove = cJSON_CreateObject(); | ||
140 | |||
141 | cJSON_AddNumberToObject( | ||
142 | jMove, | ||
143 | "id", | ||
144 | __builtin_bswap16(pki->moves[j])); | ||
145 | |||
146 | cJSON_AddNumberToObject( | ||
147 | jMove, | ||
148 | "ppBonuses", | ||
149 | (pki->ppBonuses >> (2*j)) & 3); | ||
150 | |||
151 | cJSON_AddItemToArray(jMoves, jMove); | ||
152 | } else { | ||
153 | break; | ||
154 | } | ||
155 | } | ||
156 | |||
157 | cJSON_AddItemToObject( | ||
158 | jPoke, | ||
159 | "moves", | ||
160 | jMoves); | ||
161 | |||
162 | if (pki->otGender) | ||
163 | { | ||
164 | cJSON_AddStringToObject(jPoke, "otGender", "female"); | ||
165 | } else { | ||
166 | cJSON_AddStringToObject(jPoke, "otGender", "male"); | ||
167 | } | ||
168 | |||
169 | cJSON_AddNumberToObject( | ||
170 | jPoke, | ||
171 | "metLevel", | ||
172 | pki->metLevel); | ||
173 | |||
174 | cJSON_AddNumberToObject( | ||
175 | jPoke, | ||
176 | "metLocation", | ||
177 | pki->metLocation); | ||
178 | |||
179 | cJSON_AddBoolToObject( | ||
180 | jPoke, | ||
181 | "shiny", | ||
182 | pki->shiny); | ||
183 | |||
184 | cJSON_AddNumberToObject( | ||
185 | jPoke, | ||
186 | "nature", | ||
187 | pki->nature); | ||
188 | |||
189 | if (pki->gender == 0) | ||
190 | { | ||
191 | cJSON_AddStringToObject(jPoke, "gender", "male"); | ||
192 | } else if (pki->gender == 1) | ||
193 | { | ||
194 | cJSON_AddStringToObject(jPoke, "gender", "female"); | ||
195 | } else if (pki->gender == 2) | ||
196 | { | ||
197 | cJSON_AddStringToObject(jPoke, "gender", "genderless"); | ||
198 | } | ||
199 | |||
200 | cJSON_AddBoolToObject( | ||
201 | jPoke, | ||
202 | "secondAbility", | ||
203 | pki->altAbility); | ||
204 | |||
205 | // Handle Unown form. | ||
206 | if (pki->species == 201) | ||
207 | { | ||
208 | cJSON_AddNumberToObject( | ||
209 | jPoke, | ||
210 | "unownLetter", | ||
211 | pki->unownLetter); | ||
212 | } | ||
213 | |||
214 | return jPoke; | ||
215 | } | ||