2009-03-28 08:57:34 +00:00
|
|
|
// Copyright (C) 2003-2009 Dolphin Project.
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
|
|
|
#include "Common.h"
|
|
|
|
|
|
|
|
#include "State.h"
|
|
|
|
#include "Core.h"
|
|
|
|
#include "StringUtil.h"
|
|
|
|
#include "CoreTiming.h"
|
|
|
|
#include "HW/HW.h"
|
|
|
|
#include "PowerPC/PowerPC.h"
|
2008-12-18 13:21:02 +00:00
|
|
|
#include "PowerPC/Jit64/Jit.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2009-01-15 06:48:15 +00:00
|
|
|
#include "PluginManager.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "minilzo.h"
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(__LZO_STRICT_16BIT)
|
|
|
|
#define IN_LEN (8*1024u)
|
|
|
|
#elif defined(LZO_ARCH_I086) && !defined(LZO_HAVE_MM_HUGE_ARRAY)
|
|
|
|
#define IN_LEN (60*1024u)
|
|
|
|
#else
|
|
|
|
#define IN_LEN (128*1024ul)
|
|
|
|
#endif
|
|
|
|
#define OUT_LEN (IN_LEN + IN_LEN / 16 + 64 + 3)
|
|
|
|
|
|
|
|
static unsigned char __LZO_MMODEL out [ OUT_LEN ];
|
|
|
|
|
|
|
|
#define HEAP_ALLOC(var,size) \
|
|
|
|
lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ]
|
|
|
|
|
|
|
|
static HEAP_ALLOC(wrkmem,LZO1X_1_MEM_COMPRESS);
|
|
|
|
|
|
|
|
|
|
|
|
static int ev_Save;
|
|
|
|
static int ev_Load;
|
|
|
|
|
2009-06-28 01:11:35 +00:00
|
|
|
static std::string cur_filename, lastFilename;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
static bool const bCompressed = true;
|
|
|
|
|
2009-06-28 01:11:35 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
version = 1,
|
2008-12-08 05:30:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void DoState(PointerWrap &p)
|
|
|
|
{
|
|
|
|
u32 cookie = 0xBAADBABE + version;
|
|
|
|
p.Do(cookie);
|
|
|
|
if (cookie != 0xBAADBABE + version) {
|
|
|
|
PanicAlert("Can't load states from other versions.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Begin with video plugin, so that it gets a chance to clear it's caches and writeback modified things to RAM
|
2009-01-15 06:48:15 +00:00
|
|
|
CPluginManager &pm = CPluginManager::GetInstance();
|
|
|
|
pm.GetVideo()->DoState(p.GetPPtr(), p.GetMode());
|
|
|
|
pm.GetDSP()->DoState(p.GetPPtr(), p.GetMode());
|
2009-02-09 23:12:15 +00:00
|
|
|
pm.GetPad(0)->DoState(p.GetPPtr(), p.GetMode());
|
2008-12-08 05:30:24 +00:00
|
|
|
PowerPC::DoState(p);
|
|
|
|
HW::DoState(p);
|
|
|
|
CoreTiming::DoState(p);
|
|
|
|
}
|
|
|
|
|
2009-06-26 22:36:44 +00:00
|
|
|
THREAD_RETURN CompressAndDumpState(void *pArgs)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2009-06-26 22:36:44 +00:00
|
|
|
saveStruct *saveArg = (saveStruct *)pArgs;
|
|
|
|
u8 *buffer = saveArg->buffer;
|
|
|
|
size_t sz = saveArg->size;
|
2008-12-08 05:30:24 +00:00
|
|
|
lzo_uint out_len = 0;
|
2009-06-28 01:11:35 +00:00
|
|
|
state_header header;
|
|
|
|
std::string filename = cur_filename;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2009-06-26 22:36:44 +00:00
|
|
|
delete saveArg;
|
|
|
|
|
2009-06-28 01:11:35 +00:00
|
|
|
// Moving to last overwritten save-state
|
|
|
|
if(File::Exists(cur_filename.c_str())) {
|
|
|
|
if(File::Exists(FULL_STATESAVES_DIR "lastState.sav"))
|
|
|
|
File::Delete(FULL_STATESAVES_DIR "lastState.sav");
|
|
|
|
|
|
|
|
if(!File::Rename(cur_filename.c_str(), FULL_STATESAVES_DIR "lastState.sav"))
|
|
|
|
Core::DisplayMessage("Failed to move previous state to state undo backup", 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *f = fopen(filename.c_str(), "wb");
|
2008-12-08 05:30:24 +00:00
|
|
|
if(f == NULL) {
|
|
|
|
Core::DisplayMessage("Could not save state", 2000);
|
2009-06-26 22:36:44 +00:00
|
|
|
return 0;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2009-06-28 01:11:35 +00:00
|
|
|
// Setting up the header
|
|
|
|
memcpy(header.gameID, Core::GetStartupParameter().GetUniqueID().c_str(), 6);
|
|
|
|
header.sz = bCompressed ? sz : 0;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2009-06-28 01:11:35 +00:00
|
|
|
fwrite(&header, sizeof(state_header), 1, f);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2008-12-20 12:10:59 +00:00
|
|
|
if (bCompressed) {
|
2008-12-08 05:30:24 +00:00
|
|
|
if (lzo_init() != LZO_E_OK)
|
|
|
|
PanicAlert("Internal LZO Error - lzo_init() failed");
|
|
|
|
else {
|
|
|
|
lzo_uint cur_len;
|
|
|
|
lzo_uint i = 0;
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
if((i + IN_LEN) >= sz)
|
|
|
|
cur_len = sz - i;
|
|
|
|
else
|
|
|
|
cur_len = IN_LEN;
|
|
|
|
|
|
|
|
if(lzo1x_1_compress((buffer + i), cur_len, out, &out_len, wrkmem) != LZO_E_OK)
|
|
|
|
PanicAlert("Internal LZO Error - compression failed");
|
|
|
|
|
|
|
|
// The size of the data to write is 'out_len'
|
|
|
|
fwrite(&out_len, sizeof(int), 1, f);
|
|
|
|
fwrite(out, out_len, 1, f);
|
|
|
|
|
|
|
|
if(cur_len != IN_LEN)
|
|
|
|
break;
|
|
|
|
i += cur_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
fwrite(buffer, sz, 1, f);
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
delete [] buffer;
|
|
|
|
|
2009-01-15 06:48:15 +00:00
|
|
|
Core::DisplayMessage(StringFromFormat("Saved State to %s",
|
2009-06-28 01:11:35 +00:00
|
|
|
filename.c_str()).c_str(), 2000);
|
2009-06-26 22:36:44 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::Thread *saveThread = NULL;
|
|
|
|
|
|
|
|
void SaveStateCallback(u64 userdata, int cyclesLate)
|
|
|
|
{
|
|
|
|
// If already saving state, wait for it to finish
|
|
|
|
if(saveThread)
|
|
|
|
{
|
|
|
|
delete saveThread;
|
|
|
|
saveThread = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
jit.ClearCache();
|
|
|
|
u8 *ptr = 0;
|
|
|
|
PointerWrap p(&ptr, PointerWrap::MODE_MEASURE);
|
|
|
|
DoState(p);
|
|
|
|
size_t sz = (size_t)ptr;
|
|
|
|
u8 *buffer = new u8[sz];
|
|
|
|
ptr = buffer;
|
|
|
|
p.SetMode(PointerWrap::MODE_WRITE);
|
|
|
|
DoState(p);
|
|
|
|
|
|
|
|
saveStruct *saveData = new saveStruct;
|
|
|
|
saveData->buffer = buffer;
|
|
|
|
saveData->size = sz;
|
|
|
|
|
|
|
|
Core::DisplayMessage("Saving State...", 1000);
|
|
|
|
|
|
|
|
saveThread = new Common::Thread(CompressAndDumpState, saveData);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LoadStateCallback(u64 userdata, int cyclesLate)
|
|
|
|
{
|
|
|
|
lzo_uint cur_len;
|
|
|
|
lzo_uint new_len;
|
|
|
|
|
|
|
|
bool bCompressedState;
|
|
|
|
|
2009-06-28 01:11:35 +00:00
|
|
|
// If saving state, wait for it to finish
|
|
|
|
if(saveThread)
|
|
|
|
{
|
|
|
|
delete saveThread;
|
|
|
|
saveThread = NULL;
|
|
|
|
}
|
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
FILE *f = fopen(cur_filename.c_str(), "rb");
|
|
|
|
if (!f) {
|
|
|
|
Core::DisplayMessage("State not found", 2000);
|
|
|
|
return;
|
|
|
|
}
|
2009-01-15 06:48:15 +00:00
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
u8 *buffer = NULL;
|
2009-06-28 01:11:35 +00:00
|
|
|
state_header header;
|
|
|
|
size_t sz;
|
|
|
|
|
|
|
|
fread(&header, sizeof(state_header), 1, f);
|
|
|
|
|
|
|
|
if(memcmp(Core::GetStartupParameter().GetUniqueID().c_str(), header.gameID, 6))
|
|
|
|
{
|
|
|
|
char gameID[7] = {0};
|
|
|
|
memcpy(gameID, header.gameID, 6);
|
|
|
|
Core::DisplayMessage(StringFromFormat("State belongs to a different game (ID %s)",
|
|
|
|
gameID), 2000);
|
|
|
|
|
|
|
|
fclose(f);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2009-06-28 01:11:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2009-06-28 01:11:35 +00:00
|
|
|
sz = header.sz;
|
2008-12-08 05:30:24 +00:00
|
|
|
bCompressedState = (sz != 0);
|
|
|
|
|
2008-12-20 12:10:59 +00:00
|
|
|
if (bCompressedState) {
|
2009-06-28 01:11:35 +00:00
|
|
|
Core::DisplayMessage("Decompressing State...", 500);
|
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
if (lzo_init() != LZO_E_OK)
|
|
|
|
PanicAlert("Internal LZO Error - lzo_init() failed");
|
|
|
|
else {
|
|
|
|
lzo_uint i = 0;
|
|
|
|
buffer = new u8[sz];
|
|
|
|
|
2008-12-20 12:10:59 +00:00
|
|
|
for (;;) {
|
|
|
|
if (fread(&cur_len, 1, sizeof(int), f) == 0)
|
2008-12-08 05:30:24 +00:00
|
|
|
break;
|
|
|
|
fread(out, 1, cur_len, f);
|
|
|
|
|
|
|
|
int res = lzo1x_decompress(out, cur_len, (buffer + i), &new_len, NULL);
|
|
|
|
if(res != LZO_E_OK)
|
|
|
|
PanicAlert("Internal LZO Error - decompression failed (%d)", res);
|
|
|
|
|
|
|
|
// The size of the data to read to our buffer is 'new_len'
|
|
|
|
i += new_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
sz = (int)(ftell(f) - sizeof(int));
|
|
|
|
fseek(f, sizeof(int), SEEK_SET);
|
|
|
|
|
|
|
|
buffer = new u8[sz];
|
|
|
|
|
|
|
|
int x;
|
|
|
|
if ((x = (int)fread(buffer, 1, sz, f)) != sz)
|
|
|
|
PanicAlert("wtf? %d %d", x, sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
2009-06-28 01:11:35 +00:00
|
|
|
jit.ClearCache();
|
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
u8 *ptr = buffer;
|
|
|
|
PointerWrap p(&ptr, PointerWrap::MODE_READ);
|
|
|
|
DoState(p);
|
|
|
|
delete [] buffer;
|
|
|
|
|
|
|
|
Core::DisplayMessage(StringFromFormat("Loaded state from %s", cur_filename.c_str()).c_str(), 2000);
|
|
|
|
}
|
|
|
|
|
|
|
|
void State_Init()
|
|
|
|
{
|
|
|
|
ev_Load = CoreTiming::RegisterEvent("LoadState", &LoadStateCallback);
|
|
|
|
ev_Save = CoreTiming::RegisterEvent("SaveState", &SaveStateCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void State_Shutdown()
|
|
|
|
{
|
2009-06-26 22:36:44 +00:00
|
|
|
if(saveThread)
|
|
|
|
{
|
|
|
|
delete saveThread;
|
|
|
|
saveThread = NULL;
|
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2008-12-20 12:10:59 +00:00
|
|
|
std::string MakeStateFilename(int state_number)
|
|
|
|
{
|
2008-12-08 05:30:24 +00:00
|
|
|
return StringFromFormat(FULL_STATESAVES_DIR "%s.s%02i", Core::GetStartupParameter().GetUniqueID().c_str(), state_number);
|
|
|
|
}
|
|
|
|
|
2009-06-28 01:11:35 +00:00
|
|
|
void State_SaveAs(const std::string &filename)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2009-06-28 01:11:35 +00:00
|
|
|
cur_filename = filename;
|
|
|
|
lastFilename = filename;
|
2008-12-08 05:30:24 +00:00
|
|
|
CoreTiming::ScheduleEvent_Threadsafe(0, ev_Save);
|
|
|
|
}
|
|
|
|
|
2009-06-28 01:11:35 +00:00
|
|
|
void State_Save(int slot)
|
|
|
|
{
|
|
|
|
State_SaveAs(MakeStateFilename(slot));
|
|
|
|
}
|
|
|
|
|
|
|
|
void State_LoadAs(const std::string &filename)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2009-06-28 01:11:35 +00:00
|
|
|
cur_filename = filename;
|
2008-12-08 05:30:24 +00:00
|
|
|
CoreTiming::ScheduleEvent_Threadsafe(0, ev_Load);
|
|
|
|
}
|
2009-06-28 01:11:35 +00:00
|
|
|
|
|
|
|
void State_Load(int slot)
|
|
|
|
{
|
|
|
|
State_LoadAs(MakeStateFilename(slot));
|
|
|
|
}
|
|
|
|
|
|
|
|
void State_LoadLastSaved()
|
|
|
|
{
|
|
|
|
if(lastFilename.empty())
|
|
|
|
Core::DisplayMessage("There is no last saved state", 2000);
|
|
|
|
else
|
|
|
|
State_LoadAs(lastFilename);
|
|
|
|
}
|