forked from capull0/SkyDumper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skylander.cpp
210 lines (179 loc) · 4.79 KB
/
skylander.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
//
// Created by thn on 27.12.16.
//
#include "skylander.h"
#include "portalio.h"
SkylanderIO::SkylanderIO() {
buffer = new unsigned char[1025];
}
SkylanderIO::~SkylanderIO() {
delete buffer;
}
void SkylanderIO::ReadSkylander() {
unsigned char data[0x10];
unsigned char *ptr;
int status;
PortalIO *port;
port = new PortalIO();
port->SetPortalColor(0x00, 0xff, 0x00);
port->SetPortalColor(0x00, 0xff, 0x00);
port->SetPortalColor(0x00, 0xff, 0x00);
bool found = false;
bool first = true;
int count = 0;
while (!found) {
status = port->PortalStatus();
if (status > 0) {
break;
} else {
if (first) {
printf("waiting for skylander ");
first = false;
} else {
count++;
if (count == 30) {
count = 0;
printf(".");
}
}
fflush(stdout);
}
}
printf("\n");
port->SetPortalColor(0xff, 0x00, 0x00);
port->SetPortalColor(0xff, 0x00, 0x00);
ptr = buffer;
for (unsigned int block = 0; block < 0x40; ++block) {
port->ReadBlock(block, data, 0);
memcpy(ptr, data, sizeof(data));
ptr += 0x10;
}
port->SetPortalColor(0x00, 0x00, 0xff);
port->SetPortalColor(0x00, 0x00, 0xff);
delete port;
}
void SkylanderIO::WriteSkylander() {
PortalIO *port;
bool selectAccessControlBlock;
int status;
port = new PortalIO();
port->SetPortalColor(0x00, 0xff, 0x00);
port->SetPortalColor(0x00, 0xff, 0x00);
port->SetPortalColor(0x00, 0xff, 0x00);
bool found = false;
bool first = true;
int count = 0;
while (!found) {
status = port->PortalStatus();
if (status > 0) {
break;
} else {
if (first) {
printf("waiting for skylander ");
first = false;
} else {
count++;
if (count == 30) {
count = 0;
printf(".");
}
}
fflush(stdout);
}
}
printf("\n");
port->SetPortalColor(0xff, 0x00, 0x00);
port->SetPortalColor(0xff, 0x00, 0x00);
for (int i = 0; i < 2; i++) {
selectAccessControlBlock = i == 0;
for (unsigned int block = 0; block < 0x40; ++block) {
int offset = block * 0x10;
if (IsAccessControlBlock(block) == selectAccessControlBlock) {
port->WriteBlock(block, buffer + offset, 0);
}
}
}
}
void SkylanderIO::ResetSkylander() {
for (unsigned int block = 0; block < 0x40; ++block) {
int offset = block * 0x10;
if (!IsAccessControlBlock(block) && block > 0x4 && block != 0x22 && block != 0x3e) {
for (int i = 0; i < 0x10; ++i) {
int offset2 = offset + i;
buffer[offset2] = 0;
}
}
}
}
void SkylanderIO::ReadSkylanderFile(char *name) {
FILE *file;
int count;
unsigned long fileLen;
//Open file
file = fopen(name, "rb");
if (!file) {
printf("Cannot open File.\n");
exit(1);
}
//Get file length
fseek(file, 0, SEEK_END);
fileLen = (unsigned long)ftell(file);
fseek(file, 0, SEEK_SET);
if (fileLen != 1024) {
printf("Invalid Skylander File.\n");
exit(1);
}
//Read file contents into buffer
count = (int) fread(buffer, fileLen, 1, file);
if (count < 1) {
fclose(file);
printf("Cannot read/write to File.\n");
exit(1);
}
fclose(file);
}
void SkylanderIO::FileExists(char *name) {
if (realpath(name, NULL)) {
printf("Skylander File already exists.\n");
exit(1);
}
}
void SkylanderIO::WriteSkylanderFile(char *name, unsigned char *filedata) {
FILE *file;
int count;
file = fopen(name, "wb");
if (!file) {
printf("Cannot open File.\n");
exit(1);
}
count = (int) fwrite(filedata, 1024, 1, file);
if (count < 1) {
fclose(file);
printf("Cannot read/write to File.\n");
exit(1);
}
fclose(file);
}
void SkylanderIO::dump(unsigned char *c, unsigned int n) {
unsigned int h, i;
unsigned char j;
for (h = 0; h < n; h += 16) {
printf("%04x: ", h);
for (i = 0; i < 16; i++) {
if (i + h < n)
printf("%02x ", *(c + i + h) & 0xff);
else
printf(" ");
}
for (i = 0; i < 16; i++) {
if (i + h < n) {
j = *(c + i + h);
if (j < 32) j = '.';
if (j >= 127) j = '.';
printf("%c", j);
} else
printf(" ");
}
printf("\n");
}
}