diff --git a/Core/Config.cpp b/Core/Config.cpp index 2c6b5f67d3..e7c22fe0ad 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -398,7 +398,7 @@ static ConfigSetting generalSettings[] = { ConfigSetting("ForceLagSync", &g_Config.bForceLagSync, false, true, true), ReportedConfigSetting("NumWorkerThreads", &g_Config.iNumWorkerThreads, &DefaultNumWorkers, true, true), - ConfigSetting("EnableAutoLoad", &g_Config.bEnableAutoLoad, false, true, true), + ConfigSetting("AutoLoadSaveState", &g_Config.iAutoLoadSaveState, 0, true, true), ReportedConfigSetting("EnableCheats", &g_Config.bEnableCheats, false, true, true), ConfigSetting("CwCheatRefreshRate", &g_Config.iCwCheatRefreshRate, 77, true, true), diff --git a/Core/Config.h b/Core/Config.h index 9223cfde7a..d39957e7fb 100644 --- a/Core/Config.h +++ b/Core/Config.h @@ -175,7 +175,7 @@ public: int iCurrentStateSlot; int iRewindFlipFrequency; bool bEnableStateUndo; - bool bEnableAutoLoad; + int iAutoLoadSaveState; // 0 = off, 1 = oldest, 2 = newest, >2 = slot number + 3 bool bEnableCheats; bool bReloadCheats; int iCwCheatRefreshRate; diff --git a/Core/ConfigValues.h b/Core/ConfigValues.h index bc2df61f37..4074b3e758 100644 --- a/Core/ConfigValues.h +++ b/Core/ConfigValues.h @@ -71,3 +71,9 @@ enum class SmallDisplayZoom { AUTO = 2, MANUAL = 3, }; + +enum AutoLoadSaveState { + OFF = 0, + OLDEST = 1, + NEWEST = 2, +}; diff --git a/Core/SaveState.cpp b/Core/SaveState.cpp index 7d336dd993..bb39f8304e 100644 --- a/Core/SaveState.cpp +++ b/Core/SaveState.cpp @@ -550,6 +550,27 @@ namespace SaveState return false; } + bool operator > (const tm &t1, const tm &t2) { + if (t1.tm_year > t2.tm_year) return true; + if (t1.tm_year < t2.tm_year) return false; + if (t1.tm_mon > t2.tm_mon) return true; + if (t1.tm_mon < t2.tm_mon) return false; + if (t1.tm_mday > t2.tm_mday) return true; + if (t1.tm_mday < t2.tm_mday) return false; + if (t1.tm_hour > t2.tm_hour) return true; + if (t1.tm_hour < t2.tm_hour) return false; + if (t1.tm_min > t2.tm_min) return true; + if (t1.tm_min < t2.tm_min) return false; + if (t1.tm_sec > t2.tm_sec) return true; + if (t1.tm_sec < t2.tm_sec) return false; + return false; + } + + bool operator ! (const tm &t1) { + if (t1.tm_year || t1.tm_mon || t1.tm_mday || t1.tm_hour || t1.tm_min || t1.tm_sec) return false; + return true; + } + int GetNewestSlot(const std::string &gameFilename) { int newestSlot = -1; tm newestDate = {0}; @@ -567,6 +588,23 @@ namespace SaveState return newestSlot; } + int GetOldestSlot(const std::string &gameFilename) { + int oldestSlot = -1; + tm oldestDate = {0}; + for (int i = 0; i < NUM_SLOTS; i++) { + std::string fn = GenerateSaveSlotFilename(gameFilename, i, STATE_EXTENSION); + if (File::Exists(fn)) { + tm time; + bool success = File::GetModifTime(fn, time); + if (success && (!oldestDate || oldestDate > time)) { + oldestDate = time; + oldestSlot = i; + } + } + } + return oldestSlot; + } + std::string GetSlotDateAsString(const std::string &gameFilename, int slot) { std::string fn = GenerateSaveSlotFilename(gameFilename, slot, STATE_EXTENSION); if (File::Exists(fn)) { diff --git a/Core/SaveState.h b/Core/SaveState.h index ec9efe7bb5..e190e6e65d 100644 --- a/Core/SaveState.h +++ b/Core/SaveState.h @@ -51,9 +51,10 @@ namespace SaveState int GetCurrentSlot(); - // Returns -1 if there's no newest slot. + // Returns -1 if there's no oldest/newest slot. int GetNewestSlot(const std::string &gameFilename); - + int GetOldestSlot(const std::string &gameFilename); + std::string GetSlotDateAsString(const std::string &gameFilename, int slot); std::string GenerateSaveSlotFilename(const std::string &gameFilename, int slot, const char *extension); diff --git a/UI/EmuScreen.cpp b/UI/EmuScreen.cpp index 9d38a4b859..8c4814857a 100644 --- a/UI/EmuScreen.cpp +++ b/UI/EmuScreen.cpp @@ -1337,11 +1337,26 @@ void EmuScreen::renderUI() { } void EmuScreen::autoLoad() { + int autoSlot = -1; + //check if save state has save, if so, load - int lastSlot = SaveState::GetNewestSlot(gamePath_); - if (g_Config.bEnableAutoLoad && lastSlot != -1) { - SaveState::LoadSlot(gamePath_, lastSlot, &AfterSaveStateAction); - g_Config.iCurrentStateSlot = lastSlot; + switch (g_Config.iAutoLoadSaveState) { + case AutoLoadSaveState::OFF: // "AutoLoad Off" + return; + case AutoLoadSaveState::OLDEST: // "Oldest Save" + autoSlot = SaveState::GetOldestSlot(gamePath_); + break; + case AutoLoadSaveState::NEWEST: // "Newest Save" + autoSlot = SaveState::GetNewestSlot(gamePath_); + break; + default: // try the specific save state slot specified + autoSlot = (SaveState::HasSaveInSlot(gamePath_, g_Config.iAutoLoadSaveState - 3)) ? (g_Config.iAutoLoadSaveState - 3) : -1; + break; + } + + if (g_Config.iAutoLoadSaveState && autoSlot != -1) { + SaveState::LoadSlot(gamePath_, autoSlot, &AfterSaveStateAction); + g_Config.iCurrentStateSlot = autoSlot; } } diff --git a/UI/GameSettingsScreen.cpp b/UI/GameSettingsScreen.cpp index 6e76db39e7..8ccec4b5e7 100644 --- a/UI/GameSettingsScreen.cpp +++ b/UI/GameSettingsScreen.cpp @@ -734,8 +734,8 @@ void GameSettingsScreen::CreateViews() { systemSettings->Add(new Choice(sy->T("Restore Default Settings")))->OnClick.Handle(this, &GameSettingsScreen::OnRestoreDefaultSettings); systemSettings->Add(new CheckBox(&g_Config.bEnableStateUndo, sy->T("Savestate slot backups"))); - systemSettings->Add(new CheckBox(&g_Config.bEnableAutoLoad, sy->T("Auto Load Newest Savestate"))); - + static const char *autoLoadSaveStateChoices[] = { "Off", "Oldest Save", "Newest Save", "Slot 1", "Slot 2", "Slot 3", "Slot 4", "Slot 5" }; + systemSettings->Add(new PopupMultiChoice(&g_Config.iAutoLoadSaveState, sy->T("Auto Load Savestate"), autoLoadSaveStateChoices, 0, ARRAY_SIZE(autoLoadSaveStateChoices), sy->GetName(), screenManager())); #if defined(USING_WIN_UI) systemSettings->Add(new CheckBox(&g_Config.bBypassOSKWithKeyboard, sy->T("Enable Windows native keyboard", "Enable Windows native keyboard"))); #endif