tidy up memcard code, freeze/pref/sram code

This commit is contained in:
dborth 2009-03-20 21:08:19 +00:00
parent 31691ff94f
commit 5356cf636c
5 changed files with 87 additions and 178 deletions

View File

@ -31,7 +31,6 @@
#include "srtc.h"
#include "snes9xGX.h"
#include "images/saveicon.h"
#include "freeze.h"
#include "fileop.h"
#include "filebrowser.h"
@ -139,7 +138,8 @@ NGCFreezeGame (char * filepath, int method, bool silent)
S9xPrepareSoundForSnapshotSave (FALSE);
AllocSaveBuffer ();
NGCFreezeMemBuffer (); // copy freeze mem into savebuffer
// copy freeze mem into savebuffer - bufoffset contains # bytes written
NGCFreezeMemBuffer ();
woffset = bufoffset;
S9xPrepareSoundForSnapshotSave (TRUE);
@ -147,22 +147,16 @@ NGCFreezeGame (char * filepath, int method, bool silent)
if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB) // MC Slot A or B
{
// Copy in save icon
woffset = sizeof (saveicon);
memcpy (savebuffer, saveicon, woffset);
// And the freezecomment
// set freezecomment
char freezecomment[2][32];
memset(freezecomment, 0, 64);
sprintf (freezecomment[0], "%s Snapshot", APPNAME);
sprintf (freezecomment[1], Memory.ROMName);
memcpy (savebuffer + woffset, freezecomment, 64);
woffset += 64;
SetMCSaveComment(freezecomment);
// Zip and copy in the freeze
uLongf DestBuffSize = (uLongf) SAVEBUFFERSIZE;
int err= compress2((Bytef*)(savebuffer+woffset+8), (uLongf*)&DestBuffSize, (const Bytef*)savebuffer, (uLongf)bufoffset, Z_BEST_COMPRESSION);
int err= compress2((Bytef*)(savebuffer+8), (uLongf*)&DestBuffSize, (const Bytef*)savebuffer, (uLongf)bufoffset, Z_BEST_COMPRESSION);
if(err!=Z_OK)
{
@ -172,14 +166,11 @@ NGCFreezeGame (char * filepath, int method, bool silent)
}
int zippedsize = (int)DestBuffSize;
memcpy (savebuffer + woffset, &zippedsize, 4);
woffset += 4;
int decompressedsize = (int)bufoffset;
memcpy (savebuffer + woffset, &decompressedsize, 4);
woffset += 4;
memcpy (savebuffer, &zippedsize, 4);
memcpy (savebuffer+4, &decompressedsize, 4);
woffset += zippedsize;
woffset = zippedsize + 8;
}
offset = SaveFile(filepath, woffset, method, silent);
@ -304,20 +295,13 @@ NGCUnfreezeGame (char * filepath, int method, bool silent)
{
char * zipbuffer = (char *)memalign(32, SAVEBUFFERSIZE);
memset (zipbuffer, 0, SAVEBUFFERSIZE);
// skip the saveicon and comment
offset = (sizeof(saveicon) + 64);
uLongf zipsize = 0;
uLongf decompressedsize = 0;
memcpy (&zipsize, savebuffer+offset, 4);
offset += 4;
memcpy (&decompressedsize, savebuffer+offset, 4);
offset += 4;
uLongf DestBuffSize = SAVEBUFFERSIZE;
int err= uncompress((Bytef*)zipbuffer, (uLongf*)&DestBuffSize, (const Bytef*)(savebuffer + offset), zipsize);
memcpy (&zipsize, savebuffer, 4);
memcpy (&decompressedsize, savebuffer+4, 4);
int err= uncompress((Bytef*)zipbuffer, (uLongf*)&DestBuffSize, (const Bytef*)(savebuffer+8), zipsize);
if ( err!=Z_OK )
{

View File

@ -24,8 +24,10 @@
#include "filebrowser.h"
#include "fileop.h"
#include "dvd.h"
#include "images/saveicon.h"
static u8 * SysArea = NULL;
static char savecomments[2][32];
/****************************************************************************
* MountMC
@ -268,6 +270,10 @@ LoadMCFile (char *buf, int slot, char *filename, bool silent)
CARD_Unmount(slot);
}
// discard save icon and comments
memmove(buf, buf+sizeof(saveicon)+64, bytesread);
bytesread -= (sizeof(saveicon)+64);
free(SysArea);
return bytesread;
}
@ -290,6 +296,12 @@ SaveMCFile (char *buf, int slot, char *filename, int datasize, bool silent)
if(datasize <= 0)
return 0;
// add save icon and comments
memmove(buf+sizeof(saveicon)+64, buf, datasize);
memcpy(buf, saveicon, sizeof(saveicon));
memcpy(buf+sizeof(saveicon), savecomments, 64);
datasize += (sizeof(saveicon)+64);
// Try to mount the card
CardError = MountMC(slot, NOTSILENT);
@ -373,3 +385,8 @@ done:
free(SysArea);
return byteswritten;
}
void SetMCSaveComments(char * comments)
{
memcpy(savecomments, comments, 64);
}

View File

@ -17,5 +17,6 @@ int ParseMCDirectory (int slot);
int LoadMCFile (char *buf, int slot, char *filename, bool silent);
int SaveMCFile (char *buf, int slot, char *filename, int datasize, bool silent);
bool TestMC(int slot, bool silent);
void SetMCSaveComments(char * comments);
#endif

View File

@ -16,7 +16,6 @@
#include "snes9xGX.h"
#include "s9xconfig.h"
#include "images/saveicon.h"
#include "menu.h"
#include "memcardop.h"
#include "fileop.h"
@ -109,31 +108,12 @@ static const char * XMLSaveCallback(mxml_node_t *node, int where)
static int
preparePrefsData (int method)
{
int offset = 0;
// add save icon and comments for Memory Card saves
if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB)
{
offset = sizeof (saveicon);
// Copy in save icon
memcpy (savebuffer, saveicon, offset);
// And the comments
char prefscomment[2][32];
memset(prefscomment, 0, 64);
sprintf (prefscomment[0], "%s Prefs", APPNAME);
sprintf (prefscomment[1], "Preferences");
memcpy (savebuffer + offset, prefscomment, 64);
offset += 64;
}
xml = mxmlNewXML("1.0");
mxmlSetWrapMargin(0); // disable line wrapping
data = mxmlNewElement(xml, "file");
mxmlElementSetAttr(data, "app",APPNAME);
mxmlElementSetAttr(data, "version",APPVERSION);
mxmlElementSetAttr(data, "app", APPNAME);
mxmlElementSetAttr(data, "version", APPVERSION);
createXMLSection("File", "File Settings");
@ -184,11 +164,11 @@ preparePrefsData (int method)
createXMLController(btnmap[CTRL_JUST][CTRLR_GCPAD], "btnmap_just_gcpad", "Justifier - GameCube Controller");
createXMLController(btnmap[CTRL_JUST][CTRLR_WIIMOTE], "btnmap_just_wiimote", "Justifier - Wiimote");
int datasize = mxmlSaveString(xml, (char *)savebuffer+offset, (SAVEBUFFERSIZE-offset), XMLSaveCallback);
int datasize = mxmlSaveString(xml, (char *)savebuffer, SAVEBUFFERSIZE, XMLSaveCallback);
mxmlDelete(xml);
return datasize+offset;
return datasize;
}
/****************************************************************************
@ -264,16 +244,8 @@ static bool
decodePrefsData (int method)
{
bool result = false;
int offset = 0;
// skip save icon and comments for Memory Card saves
if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB)
{
offset = sizeof (saveicon);
offset += 64; // sizeof comment
}
xml = mxmlLoadString(NULL, (char *)savebuffer+offset, MXML_TEXT_CALLBACK);
xml = mxmlLoadString(NULL, (char *)savebuffer, MXML_TEXT_CALLBACK);
if(xml)
{
@ -374,6 +346,16 @@ SavePrefs (bool silent)
AllocSaveBuffer ();
datasize = preparePrefsData (method);
if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB)
{
// Set the comments
char prefscomment[2][32];
memset(prefscomment, 0, 64);
sprintf (prefscomment[0], "%s Prefs", APPNAME);
sprintf (prefscomment[1], "Preferences");
SetMCSaveComments(prefscomment);
}
offset = SaveFile(filepath, datasize, method, silent);
FreeSaveBuffer ();

View File

@ -20,120 +20,11 @@
#include "srtc.h"
#include "snes9xGX.h"
#include "images/saveicon.h"
#include "menu.h"
#include "fileop.h"
#include "filebrowser.h"
#include "input.h"
/****************************************************************************
* Prepare SRAM Save Data
*
* This sets up the savebuffer for saving in a format compatible with
* snes9x on other platforms.
***************************************************************************/
static int
preparesavedata (int method)
{
int offset = 0;
int size;
if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB)
{
// Copy in save icon
memcpy (savebuffer, saveicon, sizeof(saveicon));
offset += sizeof (saveicon);
}
// Copy in the sramcomments
char sramcomment[2][32];
memset(sramcomment, 0, 64);
sprintf (sramcomment[0], "%s SRAM", APPNAME);
sprintf (sramcomment[1], Memory.ROMName);
memcpy (savebuffer + offset, sramcomment, 64);
offset += 64;
if(method != METHOD_MC_SLOTA && method != METHOD_MC_SLOTB)
{
// make it a 512 byte header so it is compatible with other platforms
if (offset <= 512)
offset = 512;
// header was longer than 512 bytes - hopefully this never happens!
else
return 0;
}
// Copy in the SRAM
size = Memory.SRAMSize ? (1 << (Memory.SRAMSize + 3)) * 128 : 0;
if (size > 0x20000)
size = 0x20000;
if (size != 0)
{
memcpy (savebuffer + offset, Memory.SRAM, size);
offset += size;
}
else
{
offset = 0;
}
return offset;
}
/****************************************************************************
* Decode Save Data
***************************************************************************/
static void
decodesavedata (int method, int readsize)
{
int offset = 0;
char sramsavecomment[32];
int size = Memory.SRAMSize ? (1 << (Memory.SRAMSize + 3)) * 128 : 0;
if (size > 0x20000)
size = 0x20000;
// memory card save
if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB)
offset = sizeof (saveicon); // skip save icon
// Check for sram comment
memcpy (sramsavecomment, savebuffer+offset, 32);
// Snes9x GX save found!
if ( (strncmp (sramsavecomment, "Snes9x GX", 9) == 0) )
{
// adjust offset
if(method != METHOD_MC_SLOTA && method != METHOD_MC_SLOTB)
offset = 512; // skip entire 512 byte header
else
offset += 64; // skip savecomments
// import the SRAM
memcpy (Memory.SRAM, savebuffer + offset, size);
}
// check for SRAM from other version/platform of snes9x
else if (readsize == size || readsize == size + SRTC_SRAM_PAD)
{
// SRAM data should be at the start of the file, just import it and
// ignore anything after the SRAM
memcpy (Memory.SRAM, savebuffer, size);
}
else if (readsize == size + 512)
{
// SRAM has a 512 byte header - remove it, then import the SRAM,
// ignoring anything after the SRAM
memcpy(Memory.SRAM, savebuffer+512, size);
}
else
{
ErrorPrompt("Incompatible SRAM save!");
}
}
/****************************************************************************
* Load SRAM
***************************************************************************/
@ -154,7 +45,28 @@ LoadSRAM (char * filepath, int method, bool silent)
if (offset > 0)
{
decodesavedata (method, offset);
int size = Memory.SRAMSize ? (1 << (Memory.SRAMSize + 3)) * 128 : 0;
if (size > 0x20000)
size = 0x20000;
if (readsize == size + 512 || readsize == size + 512 + SRTC_SRAM_PAD)
{
// SRAM has a 512 byte header - remove it, then import the SRAM,
// ignoring anything after the SRAM
memcpy(Memory.SRAM, savebuffer+512, size);
}
else if (readsize == size || readsize == size + SRTC_SRAM_PAD)
{
// SRAM data should be at the start of the file, just import it and
// ignore anything after the SRAM
memcpy (Memory.SRAM, savebuffer, size);
}
else
{
ErrorPrompt("Incompatible SRAM save!");
}
S9xSoftReset();
FreeSaveBuffer ();
return true;
@ -195,7 +107,6 @@ bool
SaveSRAM (char * filepath, int method, bool silent)
{
bool retval = false;
int datasize;
int offset = 0;
if(method == METHOD_AUTO)
@ -204,13 +115,29 @@ SaveSRAM (char * filepath, int method, bool silent)
if(method == METHOD_AUTO)
return false;
AllocSaveBuffer ();
// determine SRAM size
int size = Memory.SRAMSize ? (1 << (Memory.SRAMSize + 3)) * 128 : 0;
datasize = preparesavedata (method);
if (size > 0x20000)
size = 0x20000;
if (datasize)
if (size > 0)
{
offset = SaveFile(filepath, datasize, method, silent);
if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB)
{
// Set the sramcomments
char sramcomment[2][32];
memset(sramcomment, 0, 64);
sprintf (sramcomment[0], "%s SRAM", APPNAME);
sprintf (sramcomment[1], Memory.ROMName);
SetMCSaveComments(sramcomment);
}
AllocSaveBuffer ();
// copy in the SRAM, leaving a 512 byte header (for compatibility with other platforms)
memcpy(savebuffer+512, Memory.SRAM, size);
offset = SaveFile(filepath, size+512, method, silent);
FreeSaveBuffer ();
if (offset > 0)
{
@ -224,8 +151,6 @@ SaveSRAM (char * filepath, int method, bool silent)
if(!silent)
ErrorPrompt("No SRAM data to save!");
}
FreeSaveBuffer ();
return retval;
}