Skip to content

Commit

Permalink
Merge pull request #11202 from hissingshark/autoload
Browse files Browse the repository at this point in the history
Added "oldest save" and "slots 1-5" as options for "auto load savestate"
  • Loading branch information
unknownbrackets committed Jun 26, 2018
2 parents 28e3152 + 64e432e commit aa927e0
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Expand Down
2 changes: 1 addition & 1 deletion Core/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ struct Config {
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;
Expand Down
6 changes: 6 additions & 0 deletions Core/ConfigValues.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,9 @@ enum class SmallDisplayZoom {
AUTO = 2,
MANUAL = 3,
};

enum class AutoLoadSaveState {
OFF = 0,
OLDEST = 1,
NEWEST = 2,
};
38 changes: 38 additions & 0 deletions Core/SaveState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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)) {
Expand Down
5 changes: 3 additions & 2 deletions Core/SaveState.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
23 changes: 19 additions & 4 deletions UI/EmuScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 (int)AutoLoadSaveState::OFF: // "AutoLoad Off"
return;
case (int)AutoLoadSaveState::OLDEST: // "Oldest Save"
autoSlot = SaveState::GetOldestSlot(gamePath_);
break;
case (int)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;
}
}

Expand Down
4 changes: 2 additions & 2 deletions UI/GameSettingsScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit aa927e0

Please sign in to comment.