Lua: add savestate callbacks, apply movie.rerecordcounting to on-memory savestate

Note: "quiet" option of savestate.save is not implemented.
This commit is contained in:
gocha 2012-08-09 23:00:41 +09:00
parent be43e26824
commit e6522d25c9
2 changed files with 83 additions and 2 deletions

View File

@ -49,7 +49,6 @@
#endif
// TODO remove if not necessary
bool g_disableStatestateWarnings = false;
bool g_onlyCallSavestateCallbacks = false;
@ -2094,9 +2093,14 @@ DEFINE_LUA_FUNCTION(state_load, "location[,option]")
} return 0;
case LUA_TUSERDATA: // in-memory save slot
{
LuaContextInfo& info = GetCurrentInfo();
StateData& stateData = **((StateData**)luaL_checkudata(L, 1, "StateData*"));
if(stateData.buffer[0])
if(stateData.buffer[0]){
bool8 prevRerecordCountSkip = S9xMovieGetRerecordCountSkip();
S9xMovieSetRerecordCountSkip(info.rerecordCountingDisabled);
S9xUnfreezeGameMem(stateData.buffer, stateData.size);
S9xMovieSetRerecordCountSkip(prevRerecordCountSkip);
}
else // the first byte of a valid savestate is never 0 (snes9x: it should start with "#!s9xsnp")
luaL_error(L, "attempted to load an anonymous savestate before saving it");
} return 0;

View File

@ -190,6 +190,9 @@
#include "movie.h"
#include "display.h"
#include "language.h"
#ifdef HAVE_LUA
#include "lua-engine.h"
#endif
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
@ -1203,6 +1206,48 @@ bool8 S9xFreezeGame (const char *filename)
{
STREAM stream = NULL;
#ifdef HAVE_LUA
// parse state number
int filenameLen = strlen(filename);
bool numberedState = false;
int stateNumber;
if (filenameLen >= 4)
{
numberedState = (filename[filenameLen - 4] == '.') && isdigit(filename[filenameLen - 3]) && isdigit(filename[filenameLen - 2]) && isdigit(filename[filenameLen - 1]);
if (numberedState)
{
stateNumber = strtol(&filename[filenameLen - 3], NULL, 10);
}
}
if (numberedState) {
LuaSaveData saveData;
CallRegisteredLuaSaveFunctions(stateNumber, saveData);
char luaSaveFilename [MAX_PATH];
strncpy(luaSaveFilename, filename, MAX_PATH);
luaSaveFilename[MAX_PATH-(1+7/*strlen(".luasav")*/)] = '\0';
strcat(luaSaveFilename, ".luasav");
if(saveData.recordList)
{
FILE* luaSaveFile = fopen(luaSaveFilename, "wb");
if(luaSaveFile)
{
saveData.ExportRecords(luaSaveFile);
fclose(luaSaveFile);
}
}
else
{
unlink(luaSaveFilename);
}
}
extern bool g_onlyCallSavestateCallbacks;
if(g_onlyCallSavestateCallbacks)
return TRUE;
#endif
if (S9xOpenSnapshotFile(filename, FALSE, &stream))
{
S9xFreezeToStream(stream);
@ -1295,6 +1340,38 @@ bool8 S9xUnfreezeGame (const char *filename)
S9xMessage(S9X_INFO, S9X_FREEZE_FILE_INFO, String);
#ifdef HAVE_LUA
// parse state number
int filenameLen = strlen(filename);
bool numberedState = false;
int stateNumber;
if (filenameLen >= 4)
{
numberedState = (filename[filenameLen - 4] == '.') && isdigit(filename[filenameLen - 3]) && isdigit(filename[filenameLen - 2]) && isdigit(filename[filenameLen - 1]);
if (numberedState)
{
stateNumber = strtol(&filename[filenameLen - 3], NULL, 10);
}
}
if (numberedState) {
LuaSaveData saveData;
char luaSaveFilename [MAX_PATH];
strncpy(luaSaveFilename, filename, MAX_PATH);
luaSaveFilename[MAX_PATH-(1+7/*strlen(".luasav")*/)] = '\0';
strcat(luaSaveFilename, ".luasav");
FILE* luaSaveFile = fopen(luaSaveFilename, "rb");
if(luaSaveFile)
{
saveData.ImportRecords(luaSaveFile);
fclose(luaSaveFile);
}
CallRegisteredLuaLoadFunctions(stateNumber, saveData);
}
#endif
return (TRUE);
}