From 0d65ea6050de2d47f62a8eb0148864ee6583d2fc Mon Sep 17 00:00:00 2001 From: zeromus Date: Thu, 15 Oct 2020 16:46:16 -0400 Subject: [PATCH] add code to make a backup savestate every time a slot savestate is loaded. I don't need to spell out for you which users this is for, and why they need it. --- desmume/src/addons/slot1_r4.cpp | 8 ------- desmume/src/commandline.cpp | 3 +++ desmume/src/path.cpp | 2 -- desmume/src/saves.cpp | 37 +++++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/desmume/src/addons/slot1_r4.cpp b/desmume/src/addons/slot1_r4.cpp index 48b2cc593..c4d3c53a1 100644 --- a/desmume/src/addons/slot1_r4.cpp +++ b/desmume/src/addons/slot1_r4.cpp @@ -44,14 +44,6 @@ public: static Slot1InfoSimple info("R4", "Slot1 R4 emulation", 0x03); return &info; } - - //called once when the emulator starts up, or when the device springs into existence - virtual bool init() - { - //strange to do this here but we need to make sure its done at some point - srand(time(NULL)); - return true; - } virtual void connect() { diff --git a/desmume/src/commandline.cpp b/desmume/src/commandline.cpp index 069d9fdbb..cfe87f775 100644 --- a/desmume/src/commandline.cpp +++ b/desmume/src/commandline.cpp @@ -230,6 +230,9 @@ ENDL bool CommandLine::parse(int argc,char **argv) { + //closest thing to a portable main() we have, I guess. + srand((unsigned)time(nullptr)); + std::string _render3d; int opt_help = 0; diff --git a/desmume/src/path.cpp b/desmume/src/path.cpp index 9646fcc38..babd51b99 100644 --- a/desmume/src/path.cpp +++ b/desmume/src/path.cpp @@ -411,8 +411,6 @@ void PathInfo::formatname(char *output) time_t now = time(NULL); tm *time_struct = localtime(&now); - srand((unsigned)now); - for (char* p = screenshotFormat, *end = p + sizeof(screenshotFormat); p < end; p++) { diff --git a/desmume/src/saves.cpp b/desmume/src/saves.cpp index a04ba4c19..a795e48c7 100644 --- a/desmume/src/saves.cpp +++ b/desmume/src/saves.cpp @@ -57,6 +57,7 @@ #ifdef HOST_WINDOWS #include "frontend/windows/main.h" +extern char IniName[MAX_PATH]; #endif int lastSaveState = 0; //Keeps track of last savestate used for quick save/load functions @@ -726,8 +727,44 @@ void loadstate_slot(int num) path.getpathnoext(path.STATE_SLOTS, filename); + //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)) + #endif + { + if(movieMode == MOVIEMODE_INACTIVE) + { + std::string dirname = (std::string)filename + " (backups)"; + #ifdef HOST_WINDOWS + static const char PSS = '\\'; + #else + 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()); + break; + } + } + } + if (strlen(filename) + strlen(".dsx") + strlen("-2147483648") /* = biggest string for num */ > MAX_PATH) return; sprintf(filename + strlen(filename), ".ds%d", num); + if (savestate_load(filename)) { driver->SetLineColor(255, 255, 255);