Skip to content

Commit

Permalink
Ability to select a skylander when more than one are on the portal. C…
Browse files Browse the repository at this point in the history
…an also list skylanders on portal.
  • Loading branch information
silicontrip committed Jan 25, 2013
1 parent db7ffcf commit cedf90b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 47 deletions.
112 changes: 68 additions & 44 deletions fileio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ void SkylanderIO::fprinthex(FILE *f, unsigned char *c, unsigned int n) {
unsigned int h,i;
unsigned char j;


for (h=0; h<n; h+=16) {

fprintf (f,"%04x: ",h);
Expand Down Expand Up @@ -59,38 +58,31 @@ void SkylanderIO::initWithEncryptedFile(char * name) throw (int)
}
}

void SkylanderIO::initWithPortal(void) throw (int) {
void SkylanderIO::initWithPortal(int number) throw (int) {


if (!sky) {
printf("Reading Skylander from portal.\n");
ReadPortal(buffer);
//printf("Reading Skylander from portal.\n");
ReadPortal(buffer,number);
DecryptBuffer(buffer);
sky = new Skylander(buffer);
printf("\nSkylander read from portal.\n");

// printf("\nSkylander read from portal.\n");
}
}

void SkylanderIO::ReadPortal(unsigned char *s) throw (int)
void SkylanderIO::ReadPortal(unsigned char *s, int number) throw (int)
{

bool bNewSkylander = false;

unsigned char data[0x10];
unsigned char *ptr;

PortalIO *port;

port = new PortalIO();


try {
// must start with a read of block zero
port->ReadBlock(0, data, bNewSkylander);
} catch (int e) {
bNewSkylander = !bNewSkylander;
port->ReadBlock(0, data, bNewSkylander);
}
// must start with a read of block zero
port->ReadBlock(0, data, number);

// I don't know that we need this, but the web driver sets the color when reading the data
port->SetPortalColor(0xC8, 0xC8, 0xC8);
Expand All @@ -100,19 +92,19 @@ void SkylanderIO::ReadPortal(unsigned char *s) throw (int)

for(int block=1; block < 0x40; ++block) {
ptr += 0x10;
port->ReadBlock(block, data, bNewSkylander);
port->ReadBlock(block, data, number);
memcpy(ptr, data, sizeof(data));
}

delete port;
}
bool SkylanderIO::writeSkylanderToPortal() throw (int)

bool SkylanderIO::writeSkylanderToPortal(int number) throw (int)
{
bool bResult;
bool bNewSkylander = false;
unsigned char data[0x10];

unsigned char old[1024];
unsigned char skydata[1024];

Expand All @@ -121,17 +113,17 @@ bool SkylanderIO::writeSkylanderToPortal() throw (int)
if (sky) {

PortalIO *port;

ReadPortal(old);
ReadPortal(old,number);

memcpy (skydata,sky->getData(),1024);

EncryptBuffer(skydata);

printf("\nWriting Skylander to portal.\n");

port = new PortalIO();


for(int i=0; i<2; i++) {
// two pass write
Expand All @@ -142,7 +134,7 @@ bool SkylanderIO::writeSkylanderToPortal() throw (int)
} else {
selectAccessControlBlock = 0;
}

for(int block=0; block < 0x40; ++block) {
bool changed, OK;
int offset = block * BLOCK_SIZE;
Expand All @@ -155,7 +147,7 @@ bool SkylanderIO::writeSkylanderToPortal() throw (int)
}
}

return true;
return true;
}
return false;
}
Expand All @@ -172,12 +164,12 @@ bool SkylanderIO::writeSkylanderToEncryptedFile(char *name) throw (int)
{
if (sky) {
unsigned char skydata[1024];

memcpy (skydata,sky->getData(),1024);
EncryptBuffer(skydata);
WriteSkylanderFile(name,skydata);
}

}

// Encrypt entire buffer of character data
Expand Down Expand Up @@ -215,52 +207,84 @@ void SkylanderIO::ReadSkylanderFile(char *name) throw (int)
{
FILE *file;
unsigned long fileLen;

//Open file
file = fopen(name, "rb");
if (!file)
{
throw 1;
// fprintf(stderr, "Unable to open file %s\n", name);
// return NULL;
// fprintf(stderr, "Unable to open file %s\n", name);
// return NULL;
}

//Get file length
fseek(file, 0, SEEK_END);
fileLen=ftell(file);
fseek(file, 0, SEEK_SET);

if(fileLen != 1024){
throw 2;

//Read file contents into buffer
fread(buffer, fileLen, 1, file);
fclose(file);

}
//Read file contents into buffer
fread(buffer, fileLen, 1, file);
fclose(file);


}

bool SkylanderIO::WriteSkylanderFile(char *name, unsigned char * filedata) throw (int)
{
FILE *file;
bool OK = true;
int count;

file = fopen(name, "wb");
if (!file)
{
throw 1;
// fprintf(stderr, "Unable to open file %s for writing.\n", name);
// return false;
// fprintf(stderr, "Unable to open file %s for writing.\n", name);
// return false;
}

count = fwrite(filedata, 1024, 1, file);
if(count < 1) {
fclose(file);
throw 3;
OK = false;
}

fclose(file);
return OK;
}

void SkylanderIO::listSkylanders() {

PortalIO *port;
Skylander *sky;
unsigned char data[0x10];


port = new PortalIO();
sky = new Skylander(buffer);

try {
for (int s=0; s<3; s++)
{
memset(data,0,0x10);
// must start with a read of block zero
port->ReadBlock(1, data, s);

printf("%0d: %s\n",s,sky->toyName(data[0] + data[1] * 0x100));
}
delete sky;
delete port;

} catch (int e) {
delete sky;
delete port;
if (e != 8)
throw e;
}

}

8 changes: 5 additions & 3 deletions fileio.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ class SkylanderIO {
void fprinthex(FILE *, unsigned char *, unsigned int);
void initWithUnencryptedFile(char *) throw (int);
void initWithEncryptedFile(char *) throw (int);
void initWithPortal() throw (int);
void initWithPortal(int) throw (int);

void listSkylanders();

Skylander * getSkylander() { return sky; }

// void setSkylander(Skylander *);

bool writeSkylanderToPortal() throw (int);
bool writeSkylanderToPortal(int) throw (int);
bool writeSkylanderToUnencryptedFile(char *) throw (int);
bool writeSkylanderToEncryptedFile(char *) throw (int);

Expand All @@ -50,7 +52,7 @@ class SkylanderIO {

void ReadSkylanderFile(char *name) throw (int);
bool WriteSkylanderFile(char *name, unsigned char *) throw (int);
void ReadPortal(unsigned char *s) throw (int);
void ReadPortal(unsigned char *, int) throw (int);

void EncryptBuffer(unsigned char* );
void DecryptBuffer(unsigned char* );
Expand Down

0 comments on commit cedf90b

Please sign in to comment.