From 0a29e1bce309eebfed8596b2b830edce29ca9c47 Mon Sep 17 00:00:00 2001 From: adelikat Date: Tue, 23 Dec 2008 21:28:06 +0000 Subject: [PATCH] Backup savestate fixes. --- src/movie.cpp | 2 +- src/state.cpp | 30 ++++++++++++++++-------------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/movie.cpp b/src/movie.cpp index eafb742e..f2bde6c2 100644 --- a/src/movie.cpp +++ b/src/movie.cpp @@ -1304,7 +1304,7 @@ bool CheckFileExists(const char* filename) if (filename) checkFilename = filename; - FCEUI_printf("Checking file %s\n",checkFilename.c_str()); + //Check if this filename exists fstream test; test.open(checkFilename.c_str(),fstream::in); diff --git a/src/state.cpp b/src/state.cpp index abf80226..71981e0d 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -452,7 +452,6 @@ void FCEUSS_Save(const char *fname) { CreateBackupSaveState(fn); //Make a backup of previous savestate before overwriting it strcpy(lastSavestateMade,fn); //Remember what the last savestate filename was (for undoing later) - FCEUI_printf("Last save made: %s\n",lastSavestateMade); redoSS = true; //Backup was created so redo is possible } else @@ -883,7 +882,7 @@ string GenerateBackupSaveStateFn(const char *fname) filename = fname; //Convert fname to a string object int x = filename.find_last_of("."); //Find file extension filename.insert(x,"-bak"); //add "-bak" before the dot. - FCEUI_printf("Generating filename of %s\n",filename.c_str()); + return filename; } @@ -920,33 +919,36 @@ void CreateBackupSaveState(const char *fname) void SwapSaveState() { - if (!lastSavestateMade) return; //If there is no last savestate, can't undo + //-------------------------------------------------------------------------------------------- + //Both files must exist + //-------------------------------------------------------------------------------------------- + + if (!lastSavestateMade) + { + FCEUI_DispMessage("Can't Undo"); + FCEUI_printf("Undo savestate was attempted but unsuccessful because there was not a recently used savestate.\n"); + return; //If there is no last savestate, can't undo + } string backup = GenerateBackupSaveStateFn(lastSavestateMade); //Get filename of backup state if (!CheckFileExists(backup.c_str())) { - FCEUI_printf("%s exists",backup.c_str()); - return; //If no backup, can't undo + FCEUI_DispMessage("Can't Undo"); + FCEUI_printf("Undo savestate was attempted but unsuccessful because there was not a backup of the last used savestate.\n"); + return; //If no backup, can't undo } //-------------------------------------------------------------------------------------------- //So both exists, now swap the last savestate and its backup //-------------------------------------------------------------------------------------------- - string temp; //Create a temp string object - temp = backup; //Put backup filename in temp + string temp = backup; //Put backup filename in temp temp.append("x"); //Add x - FILE* backupf = fopen(backup.c_str(),"r"); //Open backup file - FILE* currentf = fopen(lastSavestateMade,"w"); //create temp file - rename(backup.c_str(),temp.c_str()); //rename backup file to temp file rename(lastSavestateMade,backup.c_str()); //rename current as backup rename(temp.c_str(),lastSavestateMade); //rename backup as current - - fclose(backupf); //Cleanup, close backup file - fclose(currentf); //Cleanup, close current file + FCEUI_DispMessage("%s restored",backup.c_str()); FCEUI_printf("%s restored\n",backup.c_str()); - //TODO: deal with error handling if any of these files does/doesn't exist unexpectedly } //------------------------------------------------------------------------------------------------------------------------------------------------------