forked from silicontrip/SkyReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
315 lines (259 loc) · 8.01 KB
/
main.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include "checksum.h"
#include "crypt.h"
#include "fileio.h"
#include "portalio.h"
#include "skylander.h"
using namespace std;
// Version 2.0
void usage()
{
printf("\n"
"Usage:\n"
"editor [-i <file>|-p] [-s <skylander>] [-d] [-e] [-o <file>|-P] [-M <money>] [-X experience] ... \n"
"\n"
"Reading/Writing:\n"
"-i <file>\tread skylander data from file, with option to decrypt the data.\n"
"-p\t\tread skylander data from portal and decrypt the data.\n"
"-s <skylander> select which skylander.\n"
"-d\t\tdecrypt the data read from the file.\n"
"-o <file>\twrite skylander data to <filename>.\n"
"-P\t\tencrypt and write skylander data to the portal.\n"
"-e\t\tencrypt data when writing file.\n"
"-D\t\tdump the data of a skylander to the display.\n"
"-l\t\tList skylanders on portal.\n"
"\nUpgrade:\n"
"-M <money>\tupgrade skylander money (max 65,000).\n"
"-X <xp>\t\tupgrade skylander Experience (level 10 = 33,000).\n"
"-H <hp>\t\tupgrade skylander Hero Points (max 100).\n"
"-C <challenges>\tupgrade skylander challenges.\n"
"-L <points>\tupgrade the skylander skillpoints on the left path.\n"
"-R <points>\tupgrade the skylander skillpoints on the right path.\n"
"-c\t\tupdate checksums.\n"
"\n"
"Examples: \n"
"editor -p -o spyro.bin\n"
"This would save a copy of the figurine to the file dspyro.bak\n"
"editor -i spyro.bin -o spyro_upgrade.bin -L 65535 -M 65000 -X 33000 -H 100\n"
"upgrade spyro.bin using skills on the LEFT path seen in the character menu\n"
"and write it to file spyro_upgrade.bin\n"
"\n"
"editor -i spyro.bin -P -M 65000 -X 33000\n"
"Upgrade skylander, leave skills as is, and write to the portal.\n"
"\n"
"editor -i spyro.bin -P\n"
"Read file from spyro.bin and write it to the portal.\n");
}
void printquad (unsigned int i)
{
printf("%02X ",i & 0xff);
printf("%02X ",(i & 0xff00 )/ 0x100);
printf("%02X ",(i & 0xff0000) / 0x10000);
printf("%02X ",(i & 0xff000000) / 0x1000000);
printf("\n");
}
int main(int argc, char* argv[])
{
unsigned char *buffer, *original_data;
bool OK, OK2;
bool encrypt,decrypt,portalIn,portalOut,dump,upgrade,flash,list;
char * inFile, *outFile;
const static char *legal_flags = "lFePpcDo:i:dM:X:H:C:L:R:s:";
encrypt = false;
decrypt = false;
portalIn = false;
portalOut = false;
upgrade = false;
dump = false;
inFile = NULL;
outFile = NULL;
flash = false;
list = false;
unsigned int money, xp, hp, challenges, skillleft, skillright,skylander_number;
money = 0;
xp = 0;
hp = 0;
challenges = 0;
skillleft = 0;
skillright = 0;
skylander_number = 0;
SkylanderIO *skio;
Checksum crc;
int k;
while ((k = getopt (argc, argv, legal_flags)) != -1) {
switch (k) {
case 'e': encrypt = true; break;
case 'd': decrypt = true; break;
case 'P': portalOut = true; break;
case 'p': portalIn = true; break;
case 'D': dump = true; break;
case 'F': flash = true; break;
case 'i':
inFile = new char[strlen(optarg)+1];
strcpy(inFile,optarg);
break;
case 'o':
outFile = new char[strlen(optarg)+1];
strcpy(outFile,optarg);
break;
case 'M':
money = atoi(optarg);
upgrade = true;
break;
case 'X':
xp = atoi(optarg);
upgrade = true;
break;
case 'H':
hp = atoi(optarg);
upgrade = true;
break;
case 'C':
challenges = atoi(optarg);
upgrade = true;
break;
case 'L':
skillleft = atoi(optarg);
upgrade = true;
break;
case 'R':
skillright = atoi(optarg);
upgrade = true;
break;
case 's':
skylander_number = atoi(optarg);
break;
case 'c':
upgrade = true;
break;
case 'l':
list = true;
break;
default:
usage () ;
exit (0);
}
}
try {
// some entertainment.
if (flash)
{
PortalIO *pio ;
pio = new PortalIO();
pio->flash();
exit (0);
}
if (list)
{
printf ("Listing Skylanders.\n\n");
skio = new SkylanderIO();
skio->listSkylanders();
exit (0);
}
// validate command line options
if ( (!inFile && !portalIn) || (inFile && portalIn)) {
printf ("Must Choose One of: read from file -i <file> or read from portal -p\n");
usage();
exit(0);
}
if (!outFile && !portalOut && !dump) {
printf ("Nothing to write. Choose file -o, portal -P or dump -D\n");
usage();
exit(0);
}
if (portalIn && upgrade) {
printf ("It is not recommended to upgrade directly from the portal (write to a file first)\n");
usage();
exit(0);
}
if (portalIn && portalOut) {
printf ("It is not recommended to read and write directly from the portal (write to a file first)\n");
usage();
exit(0);
}
skio = new SkylanderIO();
printf ("Reading Skylander\n\n");
if (portalIn) {
skio->initWithPortal(skylander_number);
}
if (inFile) {
if (decrypt) {
skio->initWithEncryptedFile(inFile);
} else {
skio->initWithUnencryptedFile(inFile);
}
}
//skio->getSkylander()->dump();
if(! skio->getSkylander()->validateChecksum()) {
fprintf(stderr, "Warning. Skylander data read from portal, but checksums are incorrect. File may be corrupt.\n");
}
/*
else {
printf ("Skylander Checksum OK.\n");
}
*/
if (dump) {
Skylander * sky ;
sky = skio->getSkylander() ;
printf("Serial Number: %X\n",sky->getSerial());
printf("Toy Type: %s (%d)\n",sky->getToyTypeName(),sky->getToyType());
printf ("trading ID: ");
skio->fprinthex(stdout,sky->getTradingID(),8);
printf("Area 0 sequence: %d\n",sky->getArea0Sequence());
printf("Area 1 sequence: %d\n",sky->getArea1Sequence());
printf("Area %d selected.\n",sky->getArea());
printf("Experience: %d\n",sky->getXP());
printf("Money: %d\n",sky->getMoney());
printf("Skills: %X\n",sky->getSkill());
printf("Platforms: %s\n",sky->getPlatformName());
printf("Name: %s\n",sky->getName());
printf("Hat: %d\n",sky->getHat());
printf("Hero Points: %d\n",sky->getHeroPoints());
printf("Heroic Challenges: %x\n",sky->getHeroicChallenges());
printf("\n");
}
if (upgrade) {
if (money) { skio->getSkylander()->setMoney(money); }
if (xp) { skio->getSkylander()->setXP(xp); }
if (hp) { skio->getSkylander()->setHeroPoints(hp); }
if (challenges) { skio->getSkylander()->setHeroicChallenges(challenges); }
if (skillleft) { skio->getSkylander() ->setSkillLeft(skillleft); }
if (skillright) { skio->getSkylander() ->setSkillLeft(skillright); }
skio->getSkylander()->computeChecksum();
}
if (outFile || portalOut) {
printf ("Writing Skylander.\n\n");
}
if (outFile) {
if (encrypt) {
skio->writeSkylanderToEncryptedFile(outFile);
} else {
skio->writeSkylanderToUnencryptedFile(outFile);
}
}
if (portalOut)
{
skio->writeSkylanderToPortal(skylander_number);
}
delete skio;
printf("Success!\n");
return 0;
} catch (int e) {
switch (e) {
case 1: printf ("Cannot open File.\n"); break;
case 2: printf ("Invalid Skylander File.\n"); break;
case 3: printf ("Cannot write to File.\n"); break;
case 4: printf ("Unable to get USB Device List.\n"); break;
case 5: printf ("Cannot Find Portal USB.\n"); break;
case 6: printf ("Unable to write to Portal.\n"); break;
case 7: printf ("Invalid Skylander Block.\n"); break;
case 8: printf ("Unable to read Skylander from Portal.\n"); break;
case 9: printf ("Wireless portal not connected.\n"); break;
case 10: printf ("Skylander Write Verify Error.\n"); break;
case 11: printf ("No Skylander detected on portal.\n"); break;
default: printf ("Unknown exception: %d.\n",e); break;
}
}
}