From 53be5f69687cbd1c23a6287c76ea81a7fbd400dd Mon Sep 17 00:00:00 2001 From: Keppl <8963514+Keppl@users.noreply.github.com> Date: Sun, 10 Apr 2022 22:36:10 -0400 Subject: [PATCH 1/4] Changed loadstate_slot function to keep only 5 latest backups when saving curring state before loading. Once more than 5 exist, oldest one will be overwritten. --- desmume/src/saves.cpp | 45 ++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/desmume/src/saves.cpp b/desmume/src/saves.cpp index 9280cd961..5e4e6040a 100644 --- a/desmume/src/saves.cpp +++ b/desmume/src/saves.cpp @@ -744,25 +744,34 @@ void loadstate_slot(int num) static const char PSS = '/'; #endif mkdir(dirname.c_str(),0777); - static unsigned seed; - for(;;) - { - std::string fname = dirname + PSS; - char mini[100]; - sprintf(mini,"%u",seed); - fname += mini + (std::string)".dst"; - FILE* f = fopen(fname.c_str(),"rb"); - if(f) - { - seed = rand()*16000+rand(); - fclose(f); - continue; - } - seed++; - savestate_save(fname.c_str()); - printf("Creating backup of current state prior to loadstate as path: %s\n",fname.c_str()); - break; + + int cur_index = -1; // setup index, -1 in case it is first instance + int max_index = 5; // Would be better to get from config instead of hardcodding + + std::string index_fname = dirname + PSS + "backup.index"; + FILE* index_file = fopen(index_fname.c_str(), "r+"); // Read/update but don't create + if (index_file) { + fscanf(index_file, "%d", &cur_index); + rewind(index_file); // prepare to overwrite } + else + { + index_file = fopen(index_fname.c_str(), "w"); // Create if doesn't exist + } + + cur_index = (cur_index + 1) % max_index; // next + + fprintf(index_file, "%d", cur_index); // Store new index + fclose(index_file); + + std::string fname = dirname + PSS; + char mini[100]; + sprintf(mini,"%u", cur_index); + fname += mini + (std::string)".dst"; + + savestate_save(fname.c_str()); + printf("Creating backup of current state prior to loadstate as path: %s\n",fname.c_str()); + } } From 970663bf711e3687c6647b283eb6e9403593aecd Mon Sep 17 00:00:00 2001 From: Keppl <8963514+Keppl@users.noreply.github.com> Date: Mon, 11 Apr 2022 17:59:17 -0400 Subject: [PATCH 2/4] Fixed bug when writing index, set default max to 200 --- desmume/src/saves.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/desmume/src/saves.cpp b/desmume/src/saves.cpp index 5e4e6040a..76448a5c0 100644 --- a/desmume/src/saves.cpp +++ b/desmume/src/saves.cpp @@ -746,7 +746,7 @@ void loadstate_slot(int num) mkdir(dirname.c_str(),0777); int cur_index = -1; // setup index, -1 in case it is first instance - int max_index = 5; // Would be better to get from config instead of hardcodding + int max_index = 200; // Would be better to get from config instead of hardcodding std::string index_fname = dirname + PSS + "backup.index"; FILE* index_file = fopen(index_fname.c_str(), "r+"); // Read/update but don't create @@ -762,6 +762,7 @@ void loadstate_slot(int num) cur_index = (cur_index + 1) % max_index; // next fprintf(index_file, "%d", cur_index); // Store new index + fprintf(index_file, "%d", EOF); // Avoid overwriting just most significant digits(e.g.: 1 -> 200 = 100 ) fclose(index_file); std::string fname = dirname + PSS; From dec93fecfad7603dde78e2ada28bc2463c8d2cc3 Mon Sep 17 00:00:00 2001 From: Keppl <8963514+Keppl@users.noreply.github.com> Date: Mon, 11 Apr 2022 22:26:16 -0400 Subject: [PATCH 3/4] Added configuration option to define number of max number of backups. Windows only at the moment. --- desmume/src/saves.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/desmume/src/saves.cpp b/desmume/src/saves.cpp index 76448a5c0..e937d3fe0 100644 --- a/desmume/src/saves.cpp +++ b/desmume/src/saves.cpp @@ -732,7 +732,11 @@ void loadstate_slot(int num) //save the state before we load the state, to give people a path for recovery in case they hit the wrong key #ifdef HOST_WINDOWS - if(!GetPrivateProfileInt("General", "BackupSavestateSuppress", 0, IniName)) + // Maximum backup number can be set in config: [General] BackupSavesMax= + // Setting BackupSavesMax=0 would disable backups + int max_index = GetPrivateProfileInt("General", "BackupSavesMax", -1, IniName); // 0 for disabled + int suppress_backups = GetPrivateProfileInt("General", "BackupSavestateSuppress", 0, IniName); + if(!suppress_backups && max_index != 0 ) #endif { if(movieMode == MOVIEMODE_INACTIVE) @@ -746,7 +750,9 @@ void loadstate_slot(int num) mkdir(dirname.c_str(),0777); int cur_index = -1; // setup index, -1 in case it is first instance - int max_index = 200; // Would be better to get from config instead of hardcodding + if (max_index == -1) { // If not set + max_index = 200; + } std::string index_fname = dirname + PSS + "backup.index"; FILE* index_file = fopen(index_fname.c_str(), "r+"); // Read/update but don't create From 631f2a265e62c93bd694aadda7f127f4ebde6e10 Mon Sep 17 00:00:00 2001 From: Keppl <8963514+Keppl@users.noreply.github.com> Date: Tue, 12 Apr 2022 19:07:06 -0400 Subject: [PATCH 4/4] Added check that index file was actually openned for writing. If not, skip creating backup part of loading. --- desmume/src/saves.cpp | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/desmume/src/saves.cpp b/desmume/src/saves.cpp index e937d3fe0..85daf0a13 100644 --- a/desmume/src/saves.cpp +++ b/desmume/src/saves.cpp @@ -765,19 +765,24 @@ void loadstate_slot(int num) index_file = fopen(index_fname.c_str(), "w"); // Create if doesn't exist } - cur_index = (cur_index + 1) % max_index; // next + if (index_file) { + cur_index = (cur_index + 1) % max_index; // next - fprintf(index_file, "%d", cur_index); // Store new index - fprintf(index_file, "%d", EOF); // Avoid overwriting just most significant digits(e.g.: 1 -> 200 = 100 ) - fclose(index_file); + fprintf(index_file, "%d", cur_index); // Store new index + fprintf(index_file, "%d", EOF); // Avoid overwriting just most significant digits(e.g.: 1 -> 200 = 100 ) + fclose(index_file); - std::string fname = dirname + PSS; - char mini[100]; - sprintf(mini,"%u", cur_index); - fname += mini + (std::string)".dst"; + std::string fname = dirname + PSS; + char mini[100]; + sprintf(mini, "%u", cur_index); + fname += mini + (std::string)".dst"; - savestate_save(fname.c_str()); - printf("Creating backup of current state prior to loadstate as path: %s\n",fname.c_str()); + savestate_save(fname.c_str()); + printf("Creating backup of current state prior to loadstate as path: %s\n", fname.c_str()); + } + else { + printf("Failed to open indexing file %s\n Prior state backup not created, check location write permissions.\n", index_fname.c_str()); + } } }