Fixed several LUA savestate issues, including crashes and silent failures, and added savestate.object(), which is savestate.create() with intuitive save state ordering. Also documented it.

This commit is contained in:
ugetab 2010-05-09 02:02:20 +00:00
parent 81e4c919bf
commit 40423f46a0
8 changed files with 87 additions and 29 deletions

View File

@ -1,3 +1,4 @@
08-may-2010 - ugetab - Added savestate.object() which is savestate.create() with intuitive numbering under windows
08-may-2010 - ugetab - Added emu.addgamegenie() and emu.delgamegenie() LUA functions.
07-may-2010 - ugetab - Win32 - Added context menu to Cheat Dialog Cheat Listbox, populated list with Toggle Cheat, Poke Cheat Value, and Goto In Hex Editor
07-may-2010 - ugetab - Win32 - Made enabling/disabling cheats no longer deselect the selected cheat.

View File

@ -95,6 +95,30 @@ FCEUGI::~FCEUGI()
if(archiveFilename) delete archiveFilename;
}
bool CheckFileExists(const char* filename)
{
//This function simply checks to see if the given filename exists
string checkFilename;
if (filename)
checkFilename = filename;
//Check if this filename exists
fstream test;
test.open(checkFilename.c_str(),fstream::in);
if (test.fail())
{
test.close();
return false;
}
else
{
test.close();
return true;
}
}
static void FCEU_CloseGame(void)
{
if(GameInfo)

View File

@ -108,6 +108,8 @@ int FCEU_TextScanlineOffsetFromBottom(int y);
extern FCEUS FSettings;
bool CheckFileExists(const char* filename); //Receives a filename (fullpath) and checks to see if that file exists
void FCEU_PrintError(char *format, ...);
void FCEU_printf(char *format, ...);
void FCEU_DispMessage(char *format, ...);

View File

@ -2328,12 +2328,14 @@ static int savestate_gc(lua_State *L) {
return 0;
}
// object savestate.create(int which = nil)
// Referenced by:
// savestate.create(int which = nil)
// savestate.object(int which = nil)
//
// Creates an object used for savestates.
// The object can be associated with a player-accessible savestate
// ("which" between 1 and 10) or not (which == nil).
static int savestate_create(lua_State *L) {
static int savestate_create_aliased(lua_State *L, bool newnumbering) {
int which = -1;
if (lua_gettop(L) >= 1) {
which = luaL_checkinteger(L, 1);
@ -2348,8 +2350,17 @@ static int savestate_create(lua_State *L) {
if (which > 0) {
// Find an appropriate filename. This is OS specific, unfortunately.
// So I turned the filename selection code into my bitch. :)
// Numbers are 0 through 9 though.
// Numbers are 0 through 9.
if (newnumbering) //1-9, 10 = 0. QWERTY style.
ss->filename = FCEU_MakeFName(FCEUMKF_STATE, (which % 10), 0);
else // Note: Windows Slots 1-10 = Which 2-10, 1
ss->filename = FCEU_MakeFName(FCEUMKF_STATE, which - 1, 0);
// Only ensure load if the file exists
// Also makes it persistent, but files are like that
if (CheckFileExists(ss->filename.c_str()))
ss->ensureLoad();
}
else {
//char tempbuf[100] = "snluaXXXXXX";
@ -2387,6 +2398,30 @@ static int savestate_create(lua_State *L) {
return 1;
}
// object savestate.object(int which = nil)
//
// Creates an object used for savestates.
// The object can be associated with a player-accessible savestate
// ("which" between 1 and 10) or not (which == nil).
// Uses more windows-friendly slot numbering
static int savestate_object(lua_State *L) {
// New Save Slot Numbers:
// 1-9 refer to 1-9, 10 refers to 0. QWERTY style.
return savestate_create_aliased(L,true);
}
// object savestate.create(int which = nil)
//
// Creates an object used for savestates.
// The object can be associated with a player-accessible savestate
// ("which" between 1 and 10) or not (which == nil).
// Uses original slot numbering
static int savestate_create(lua_State *L) {
// Original Save Slot Numbers:
// 1-10, 1 refers to slot 0, 2-10 refer to 1-9
return savestate_create_aliased(L,false);
}
// savestate.save(object state)
//
@ -2396,6 +2431,11 @@ static int savestate_save(lua_State *L) {
//char *filename = savestateobj2filename(L,1);
LuaSaveState *ss = (LuaSaveState *)lua_touserdata(L, 1);
if (!ss) {
luaL_error(L, "Invalid savestate.save object");
return 0;
}
if(ss->data) delete ss->data;
ss->data = new memorystream();
@ -2425,10 +2465,20 @@ static int savestate_load(lua_State *L) {
LuaSaveState *ss = (LuaSaveState *)lua_touserdata(L, 1);
if (!ss) {
luaL_error(L, "Invalid savestate.load object");
return 0;
}
numTries--;
FCEUSS_LoadFP(ss->data,SSLOADPARAM_NOBACKUP);
ss->data->seekg(0);
/*if (!ss->data) {
luaL_error(L, "Invalid savestate.load data");
return 0;
} */
if (FCEUSS_LoadFP(ss->data,SSLOADPARAM_NOBACKUP))
ss->data->seekg(0);
return 0;
}
@ -4513,6 +4563,7 @@ static const struct luaL_reg inputlib[] = {
static const struct luaL_reg savestatelib[] = {
{"create", savestate_create},
{"object", savestate_object},
{"save", savestate_save},
{"persist", savestate_persist},
{"load", savestate_load},

View File

@ -1487,26 +1487,3 @@ void FCEUI_MakeBackupMovie(bool dispMessage)
}
}
bool CheckFileExists(const char* filename)
{
//This function simply checks to see if the given filename exists
string checkFilename;
if (filename)
checkFilename = filename;
//Check if this filename exists
fstream test;
test.open(checkFilename.c_str(),fstream::in);
if (test.fail())
{
test.close();
return false;
}
else
{
test.close();
return true;
}
}

View File

@ -254,7 +254,6 @@ extern bool movie_readonly;
extern bool autoMovieBackup;
extern int pauseframe;
//--------------------------------------------------
bool CheckFileExists(const char* filename); //Receives a filename (fullpath) and checks to see if that file exists
void FCEUI_MakeBackupMovie(bool dispMessage);
void FCEUI_CreateMovieFile(std::string fn);
void FCEUI_SaveMovie(const char *fname, EMOVIE_FLAG flags, std::wstring author);

View File

@ -621,6 +621,10 @@ bool FCEUSS_LoadFP(std::istream* is, ENUM_SSLOADPARAMS params)
//maybe make a backup savestate
memorystream msBackupSavestate;
bool backup = (params == SSLOADPARAM_BACKUP);
if(!is)
return false;
if(backup)
FCEUSS_SaveMS(&msBackupSavestate,Z_NO_COMPRESSION);

Binary file not shown.