From c58227752dbaaed5c43332501015f2f0317bac78 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Wed, 3 Mar 2021 01:14:05 +1000 Subject: [PATCH] CommonHostInterface: Move save state on exit logic to base class --- .../app/src/cpp/android_host_interface.cpp | 6 +----- src/core/host_interface.cpp | 5 ----- src/core/host_interface.h | 1 - .../nogui_host_interface.cpp | 6 +----- src/duckstation-qt/qthostinterface.cpp | 7 ++----- src/frontend-common/common_host_interface.cpp | 19 ++++++++++++------- src/frontend-common/common_host_interface.h | 10 +++++++--- src/frontend-common/fullscreen_ui.cpp | 4 +--- .../http_downloader_winhttp.cpp | 2 +- 9 files changed, 25 insertions(+), 35 deletions(-) diff --git a/android/app/src/cpp/android_host_interface.cpp b/android/app/src/cpp/android_host_interface.cpp index 507351ef0..72ae1e1fa 100644 --- a/android/app/src/cpp/android_host_interface.cpp +++ b/android/app/src/cpp/android_host_interface.cpp @@ -374,11 +374,7 @@ void AndroidHostInterface::EmulationThreadEntryPoint(JNIEnv* env, jobject emulat { // System is ready to go. EmulationThreadLoop(env); - - if (g_settings.save_state_on_exit) - SaveResumeSaveState(); - - PowerOffSystem(); + PowerOffSystem(ShouldSaveResumeState()); } // Drain any callbacks so we don't leave things in a screwed-up state for next boot. diff --git a/src/core/host_interface.cpp b/src/core/host_interface.cpp index 00f63a20b..f482c664b 100644 --- a/src/core/host_interface.cpp +++ b/src/core/host_interface.cpp @@ -142,11 +142,6 @@ void HostInterface::ResetSystem() AddOSDMessage(TranslateStdString("OSDMessage", "System reset.")); } -void HostInterface::PowerOffSystem() -{ - DestroySystem(); -} - void HostInterface::PauseSystem(bool paused) { if (paused == System::IsPaused() || System::IsShutdown()) diff --git a/src/core/host_interface.h b/src/core/host_interface.h index 573a495bd..c5e87712d 100644 --- a/src/core/host_interface.h +++ b/src/core/host_interface.h @@ -52,7 +52,6 @@ public: virtual void Shutdown(); virtual bool BootSystem(const SystemBootParameters& parameters); - virtual void PowerOffSystem(); virtual void PauseSystem(bool paused); virtual void ResetSystem(); virtual void DestroySystem(); diff --git a/src/duckstation-nogui/nogui_host_interface.cpp b/src/duckstation-nogui/nogui_host_interface.cpp index 2438f95de..ec3e0c702 100644 --- a/src/duckstation-nogui/nogui_host_interface.cpp +++ b/src/duckstation-nogui/nogui_host_interface.cpp @@ -238,11 +238,7 @@ void NoGUIHostInterface::Run() // Save state on exit so it can be resumed if (!System::IsShutdown()) - { - if (g_settings.save_state_on_exit) - SaveResumeSaveState(); - DestroySystem(); - } + PowerOffSystem(ShouldSaveResumeState()); } void NoGUIHostInterface::ReportMessage(const char* message) diff --git a/src/duckstation-qt/qthostinterface.cpp b/src/duckstation-qt/qthostinterface.cpp index 3bb17fe42..baec13e53 100644 --- a/src/duckstation-qt/qthostinterface.cpp +++ b/src/duckstation-qt/qthostinterface.cpp @@ -812,10 +812,7 @@ void QtHostInterface::powerOffSystem() return; } - if (g_settings.save_state_on_exit) - SaveResumeSaveState(); - - PowerOffSystem(); + PowerOffSystem(ShouldSaveResumeState()); } void QtHostInterface::powerOffSystemWithoutSaving() @@ -827,7 +824,7 @@ void QtHostInterface::powerOffSystemWithoutSaving() return; } - PowerOffSystem(); + PowerOffSystem(false); } void QtHostInterface::synchronousPowerOffSystem() diff --git a/src/frontend-common/common_host_interface.cpp b/src/frontend-common/common_host_interface.cpp index 9bbbe4056..96f98c7a2 100644 --- a/src/frontend-common/common_host_interface.cpp +++ b/src/frontend-common/common_host_interface.cpp @@ -199,12 +199,15 @@ void CommonHostInterface::DestroySystem() HostInterface::DestroySystem(); } -void CommonHostInterface::PowerOffSystem() +void CommonHostInterface::PowerOffSystem(bool save_resume_state) { if (System::IsShutdown()) return; - HostInterface::PowerOffSystem(); + if (save_resume_state) + SaveResumeSaveState(); + + DestroySystem(); if (InBatchMode()) RequestExit(); @@ -753,6 +756,11 @@ bool CommonHostInterface::ResumeSystemFromMostRecentState() return LoadState(path.c_str()); } +bool CommonHostInterface::ShouldSaveResumeState() const +{ + return g_settings.save_state_on_exit; +} + bool CommonHostInterface::IsRunningAtNonStandardSpeed() const { if (!System::IsValid()) @@ -1810,7 +1818,7 @@ void CommonHostInterface::RegisterGeneralHotkeys() { SmallString confirmation_message( TranslateString("CommonHostInterface", "Are you sure you want to stop emulation?")); - if (g_settings.save_state_on_exit) + if (ShouldSaveResumeState()) { confirmation_message.AppendString("\n\n"); confirmation_message.AppendString( @@ -1824,10 +1832,7 @@ void CommonHostInterface::RegisterGeneralHotkeys() } } - if (g_settings.save_state_on_exit) - SaveResumeSaveState(); - - PowerOffSystem(); + PowerOffSystem(ShouldSaveResumeState()); } }); #else diff --git a/src/frontend-common/common_host_interface.h b/src/frontend-common/common_host_interface.h index 9d4d832ac..4647f6cc9 100644 --- a/src/frontend-common/common_host_interface.h +++ b/src/frontend-common/common_host_interface.h @@ -125,7 +125,6 @@ public: virtual void Shutdown() override; virtual bool BootSystem(const SystemBootParameters& parameters) override; - virtual void PowerOffSystem() override; virtual void ResetSystem() override; virtual void DestroySystem() override; @@ -169,6 +168,9 @@ public: /// Saves the current input configuration to the specified profile name. bool SaveInputProfile(const char* profile_path); + /// Powers off the system, optionally saving the resume state. + void PowerOffSystem(bool save_resume_state); + /// Loads state from the specified filename. bool LoadState(const char* filename); @@ -184,8 +186,7 @@ public: /// Loads the most recent resume save state. This may be global or per-game. bool ResumeSystemFromMostRecentState(); - /// Saves the resume save state, call when shutting down. Not called automatically on DestroySystem() since that can - /// be called as a result of an error. + /// Saves the resume save state, call when shutting down. bool SaveResumeSaveState(); /// Returns a list of save states for the specified game code. @@ -268,6 +269,9 @@ public: /// Converts a fullscreen mode to a string. static std::string GetFullscreenModeString(u32 width, u32 height, float refresh_rate); + /// Returns true if the state should be saved on shutdown. + bool ShouldSaveResumeState() const; + /// Returns true if fast forwarding or slow motion is currently active. bool IsRunningAtNonStandardSpeed() const; diff --git a/src/frontend-common/fullscreen_ui.cpp b/src/frontend-common/fullscreen_ui.cpp index fedf2422a..fb966bb71 100644 --- a/src/frontend-common/fullscreen_ui.cpp +++ b/src/frontend-common/fullscreen_ui.cpp @@ -550,9 +550,7 @@ static void DoPowerOff() if (!System::IsValid()) return; - if (g_settings.save_state_on_exit) - s_host_interface->SaveResumeSaveState(); - s_host_interface->PowerOffSystem(); + s_host_interface->PowerOffSystem(s_host_interface->ShouldSaveResumeState()); ReturnToMainWindow(); }); diff --git a/src/frontend-common/http_downloader_winhttp.cpp b/src/frontend-common/http_downloader_winhttp.cpp index ea6a4bb32..634e3ab94 100644 --- a/src/frontend-common/http_downloader_winhttp.cpp +++ b/src/frontend-common/http_downloader_winhttp.cpp @@ -45,7 +45,7 @@ bool HTTPDownloaderWinHttp::Initialize() if (WinHttpSetStatusCallback(m_hSession, HTTPStatusCallback, notification_flags, NULL) == WINHTTP_INVALID_STATUS_CALLBACK) { - Log_ErrorPrint("WinHttpSetStatusCallback() failed: %u", GetLastError()); + Log_ErrorPrintf("WinHttpSetStatusCallback() failed: %u", GetLastError()); return false; }