From f3a23db0e0e89c1d8dc2dd3904e6d71c4b3726f0 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Tue, 5 Apr 2022 02:15:03 +0200 Subject: [PATCH] Core/State: Minor cleanup around the undo load state buffer. --- Source/Core/Core/State.cpp | 42 +++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/Source/Core/Core/State.cpp b/Source/Core/Core/State.cpp index b99e8ab298..a1fc21f766 100644 --- a/Source/Core/Core/State.cpp +++ b/Source/Core/Core/State.cpp @@ -66,10 +66,10 @@ static HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS); static AfterLoadCallbackFunc s_on_after_load_callback; // Temporary undo state buffer -static std::vector g_undo_load_buffer; -static std::mutex s_load_or_save_in_progress_mutex; +static std::vector s_undo_load_buffer; +static std::mutex s_undo_load_buffer_mutex; -static std::mutex g_cs_undo_load_buffer; +static std::mutex s_load_or_save_in_progress_mutex; struct CompressAndDumpState_args { @@ -650,12 +650,13 @@ void LoadAs(const std::string& filename) // Save temp buffer for undo load state if (!Movie::IsJustStartingRecordingInputFromSaveState()) { - std::lock_guard lk2(g_cs_undo_load_buffer); - SaveToBuffer(g_undo_load_buffer); + std::lock_guard lk2(s_undo_load_buffer_mutex); + SaveToBuffer(s_undo_load_buffer); + const std::string dtmpath = File::GetUserPath(D_STATESAVES_IDX) + "undo.dtm"; if (Movie::IsMovieActive()) - Movie::SaveRecording(File::GetUserPath(D_STATESAVES_IDX) + "undo.dtm"); - else if (File::Exists(File::GetUserPath(D_STATESAVES_IDX) + "undo.dtm")) - File::Delete(File::GetUserPath(D_STATESAVES_IDX) + "undo.dtm"); + Movie::SaveRecording(dtmpath); + else if (File::Exists(dtmpath)) + File::Delete(dtmpath); } bool loaded = false; @@ -734,8 +735,8 @@ void Shutdown() // this gives a better guarantee to free the allocated memory right NOW (as opposed to, actually, // never) { - std::lock_guard lk(g_cs_undo_load_buffer); - std::vector().swap(g_undo_load_buffer); + std::lock_guard lk(s_undo_load_buffer_mutex); + std::vector().swap(s_undo_load_buffer); } } @@ -789,18 +790,25 @@ void SaveFirstSaved() // Load the last state before loading the state void UndoLoadState() { - std::lock_guard lk(g_cs_undo_load_buffer); - if (!g_undo_load_buffer.empty()) + std::lock_guard lk(s_undo_load_buffer_mutex); + if (!s_undo_load_buffer.empty()) { - if (File::Exists(File::GetUserPath(D_STATESAVES_IDX) + "undo.dtm") || (!Movie::IsMovieActive())) + if (Movie::IsMovieActive()) { - LoadFromBuffer(g_undo_load_buffer); - if (Movie::IsMovieActive()) - Movie::LoadInput(File::GetUserPath(D_STATESAVES_IDX) + "undo.dtm"); + const std::string dtmpath = File::GetUserPath(D_STATESAVES_IDX) + "undo.dtm"; + if (File::Exists(dtmpath)) + { + LoadFromBuffer(s_undo_load_buffer); + Movie::LoadInput(dtmpath); + } + else + { + PanicAlertFmtT("No undo.dtm found, aborting undo load state to prevent movie desyncs"); + } } else { - PanicAlertFmtT("No undo.dtm found, aborting undo load state to prevent movie desyncs"); + LoadFromBuffer(s_undo_load_buffer); } } else