snes9xgx/source/ngc/freeze.cpp

312 lines
7.0 KiB
C++
Raw Normal View History

/****************************************************************************
* Snes9x 1.51 Nintendo Wii/Gamecube Port
2008-08-14 00:44:59 +02:00
*
* softdev July 2006
* crunchy2 May 2007-July 2007
2008-11-12 08:50:39 +01:00
* Michniewski 2008
2008-08-14 00:44:59 +02:00
* Tantric August 2008
*
* freeze.cpp
*
* Snapshots Memory File System
*
* This is a single global memory file controller.
* Don't even think of opening two at the same time!
***************************************************************************/
#include <gccore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fat.h>
#include <zlib.h>
#include "snes9x.h"
#include "memmap.h"
#include "soundux.h"
#include "snapshot.h"
#include "srtc.h"
2008-10-03 07:26:01 +02:00
#include "snes9xGX.h"
2008-08-12 05:25:16 +02:00
#include "images/saveicon.h"
#include "freeze.h"
2008-12-18 19:36:30 +01:00
#include "fileop.h"
2008-08-07 07:19:17 +02:00
#include "menudraw.h"
extern void S9xSRTCPreSaveState ();
extern void NGCFreezeStruct ();
extern bool8 S9xUnfreezeGame (const char *filename);
static int bufoffset;
char freezecomment[2][32];
/****************************************************************************
* GetMem
*
* Return x bytes from memory buffer
***************************************************************************/
int
GetMem (char *buffer, int len)
{
memcpy (buffer, savebuffer + bufoffset, len);
2008-08-12 05:25:16 +02:00
bufoffset += len;
2008-08-12 05:25:16 +02:00
return len;
}
/****************************************************************************
* PutMem
*
* Put some values in memory buffer
***************************************************************************/
static void
PutMem (char *buffer, int len)
{
memcpy (savebuffer + bufoffset, buffer, len);
2008-08-12 05:25:16 +02:00
bufoffset += len;
}
void
NGCFreezeBlock (char *name, uint8 * block, int size)
{
2008-08-12 05:25:16 +02:00
char buffer[512];
sprintf (buffer, "%s:%06d:", name, size);
PutMem (buffer, strlen (buffer));
PutMem ((char *) block, size);
}
/****************************************************************************
* NGCFreezeMembuffer
*
* Copies a snapshot of Snes9x state into memory
***************************************************************************/
static int
NGCFreezeMemBuffer ()
{
int i;
char buffer[1024];
bufoffset = 0;
S9xUpdateRTC ();
S9xSRTCPreSaveState ();
for (i = 0; i < 8; i++)
{
SoundData.channels[i].previous16[0] =
(int16) SoundData.channels[i].previous[0];
SoundData.channels[i].previous16[1] =
(int16) SoundData.channels[i].previous[1];
}
sprintf (buffer, "%s:%04d\n", SNAPSHOT_MAGIC, SNAPSHOT_VERSION);
PutMem (buffer, strlen (buffer));
sprintf (buffer, "NAM:%06d:%s%c", (int) strlen (Memory.ROMFilename) + 1,
Memory.ROMFilename, 0);
PutMem (buffer, strlen (buffer) + 1);
NGCFreezeStruct ();
return 0;
}
/****************************************************************************
* NGCFreezeGame
*
* Do freeze game for Nintendo Gamecube
***************************************************************************/
int
NGCFreezeGame (int method, bool8 silent)
{
2008-11-12 08:50:39 +01:00
char filepath[1024];
int offset = 0; // bytes written (actual)
int woffset = 0; // bytes written (expected)
char msg[100];
2008-12-18 19:36:30 +01:00
ShowAction ("Saving...");
2008-08-12 05:25:16 +02:00
if(method == METHOD_AUTO)
method = autoSaveMethod();
2008-11-12 08:50:39 +01:00
if(!MakeFilePath(filepath, FILE_SNAPSHOT, method))
return 0;
S9xSetSoundMute (TRUE);
S9xPrepareSoundForSnapshotSave (FALSE);
2008-09-27 09:13:52 +02:00
AllocSaveBuffer ();
NGCFreezeMemBuffer (); // copy freeze mem into savebuffer
2008-11-12 08:50:39 +01:00
woffset = bufoffset;
S9xPrepareSoundForSnapshotSave (TRUE);
S9xSetSoundMute (FALSE);
2008-11-12 08:50:39 +01:00
if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB) // MC Slot A or B
{
// Copy in save icon
2008-11-12 08:50:39 +01:00
woffset = sizeof (saveicon);
2008-08-13 07:47:04 +02:00
memcpy (savebuffer, saveicon, woffset);
// And the freezecomment
sprintf (freezecomment[0], "%s Freeze", VERSIONSTR);
sprintf (freezecomment[1], Memory.ROMName);
2008-08-13 07:47:04 +02:00
memcpy (savebuffer + woffset, freezecomment, 64);
woffset += 64;
// 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);
if(err!=Z_OK)
{
sprintf (msg, "zip error %s ",zError(err));
WaitPrompt (msg);
return 0;
}
int zippedsize = (int)DestBuffSize;
2008-08-13 07:47:04 +02:00
memcpy (savebuffer + woffset, &zippedsize, 4);
woffset += 4;
int decompressedsize = (int)bufoffset;
2008-08-13 07:47:04 +02:00
memcpy (savebuffer + woffset, &decompressedsize, 4);
woffset += 4;
2008-08-13 07:47:04 +02:00
woffset += zippedsize;
}
2008-11-12 08:50:39 +01:00
offset = SaveFile(filepath, woffset, method, silent);
2008-09-27 09:13:52 +02:00
FreeSaveBuffer ();
2008-08-12 05:25:16 +02:00
if(offset > 0) // save successful!
{
if(!silent)
2008-12-18 19:36:30 +01:00
WaitPrompt("Save successful");
2008-08-12 05:25:16 +02:00
return 1;
}
return 0;
}
/****************************************************************************
* NGCUnFreezeBlock
***************************************************************************/
int
NGCUnFreezeBlock (char *name, uint8 * block, int size)
{
2008-08-12 05:25:16 +02:00
char buffer[20], *e;
int len = 0;
int rem = 0;
2008-08-12 05:25:16 +02:00
GetMem (buffer, 11);
2008-08-12 05:25:16 +02:00
if (strncmp (buffer, name, 3) != 0 || buffer[3] != ':' ||
buffer[10] != ':' || (len = strtol (&buffer[4], &e, 10)) == 0 ||
e != buffer + 10)
{
bufoffset -= 11; // go back to where we started
2008-08-12 05:25:16 +02:00
return WRONG_FORMAT;
}
2008-08-12 05:25:16 +02:00
if (len > size)
{
rem = len - size;
len = size;
}
2008-08-12 05:25:16 +02:00
ZeroMemory (block, size);
2008-08-12 05:25:16 +02:00
GetMem ((char *) block, len);
2008-08-12 05:25:16 +02:00
if (rem)
{
bufoffset += rem;
}
2008-08-12 05:25:16 +02:00
return SUCCESS;
}
/****************************************************************************
* NGCUnfreezeGame
***************************************************************************/
int
NGCUnfreezeGame (int method, bool8 silent)
{
2008-11-12 08:50:39 +01:00
char filepath[1024];
2008-08-12 05:25:16 +02:00
int offset = 0;
2008-11-12 08:50:39 +01:00
int result = 0;
2008-08-12 05:25:16 +02:00
char msg[80];
2008-08-12 05:25:16 +02:00
bufoffset = 0;
2008-12-18 19:36:30 +01:00
ShowAction ("Loading...");
2008-11-12 08:50:39 +01:00
if(method == METHOD_AUTO)
2008-08-17 07:25:33 +02:00
method = autoSaveMethod(); // we use 'Save' because snapshot needs R/W
2008-11-12 08:50:39 +01:00
if(!MakeFilePath(filepath, FILE_SNAPSHOT, method))
return 0;
2008-11-12 08:50:39 +01:00
AllocSaveBuffer ();
2008-11-12 08:50:39 +01:00
offset = LoadFile(filepath, method, silent);
2008-11-12 08:50:39 +01:00
if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB) // MC in slot A or slot B
{
if (offset)
{
char * zipbuffer = (char *)malloc(SAVEBUFFERSIZE);
memset (zipbuffer, 0, SAVEBUFFERSIZE);
2008-08-13 07:47:04 +02:00
// 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;
2008-08-13 07:47:04 +02:00
int err= uncompress((Bytef*)zipbuffer, (uLongf*)&DestBuffSize, (const Bytef*)(savebuffer + offset), zipsize);
if ( err!=Z_OK )
{
2008-08-12 05:25:16 +02:00
sprintf (msg, "Unzip error %s ",zError(err));
WaitPrompt (msg);
}
2008-08-13 07:47:04 +02:00
else if ( DestBuffSize != decompressedsize )
{
2008-12-18 19:36:30 +01:00
WaitPrompt("Unzipped size doesn't match expected size!");
}
2008-08-13 07:47:04 +02:00
else
{
offset = SAVEBUFFERSIZE;
memcpy (savebuffer, zipbuffer, SAVEBUFFERSIZE);
2008-08-13 07:47:04 +02:00
}
free(zipbuffer);
}
}
2008-08-12 05:25:16 +02:00
if(offset > 0)
{
if (S9xUnfreezeGame ("AGAME") == SUCCESS)
2008-09-27 09:13:52 +02:00
result = 1;
2008-08-12 05:25:16 +02:00
else
2008-12-18 19:36:30 +01:00
WaitPrompt("Error thawing");
2008-08-12 05:25:16 +02:00
}
else
{
if(!silent)
2008-12-18 19:36:30 +01:00
WaitPrompt("Freeze file not found");
2008-08-12 05:25:16 +02:00
}
2008-09-27 09:13:52 +02:00
FreeSaveBuffer ();
return result;
}