Core/State: Minor cleanup around the undo load state buffer.

This commit is contained in:
Admiral H. Curtiss 2022-04-05 02:15:03 +02:00
parent e0d30e371c
commit f3a23db0e0
No known key found for this signature in database
GPG Key ID: F051B4C4044F33FB

View File

@ -66,10 +66,10 @@ static HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS);
static AfterLoadCallbackFunc s_on_after_load_callback; static AfterLoadCallbackFunc s_on_after_load_callback;
// Temporary undo state buffer // Temporary undo state buffer
static std::vector<u8> g_undo_load_buffer; static std::vector<u8> s_undo_load_buffer;
static std::mutex s_load_or_save_in_progress_mutex; 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 struct CompressAndDumpState_args
{ {
@ -650,12 +650,13 @@ void LoadAs(const std::string& filename)
// Save temp buffer for undo load state // Save temp buffer for undo load state
if (!Movie::IsJustStartingRecordingInputFromSaveState()) if (!Movie::IsJustStartingRecordingInputFromSaveState())
{ {
std::lock_guard lk2(g_cs_undo_load_buffer); std::lock_guard lk2(s_undo_load_buffer_mutex);
SaveToBuffer(g_undo_load_buffer); SaveToBuffer(s_undo_load_buffer);
const std::string dtmpath = File::GetUserPath(D_STATESAVES_IDX) + "undo.dtm";
if (Movie::IsMovieActive()) if (Movie::IsMovieActive())
Movie::SaveRecording(File::GetUserPath(D_STATESAVES_IDX) + "undo.dtm"); Movie::SaveRecording(dtmpath);
else if (File::Exists(File::GetUserPath(D_STATESAVES_IDX) + "undo.dtm")) else if (File::Exists(dtmpath))
File::Delete(File::GetUserPath(D_STATESAVES_IDX) + "undo.dtm"); File::Delete(dtmpath);
} }
bool loaded = false; 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, // this gives a better guarantee to free the allocated memory right NOW (as opposed to, actually,
// never) // never)
{ {
std::lock_guard lk(g_cs_undo_load_buffer); std::lock_guard lk(s_undo_load_buffer_mutex);
std::vector<u8>().swap(g_undo_load_buffer); std::vector<u8>().swap(s_undo_load_buffer);
} }
} }
@ -789,14 +790,16 @@ void SaveFirstSaved()
// Load the last state before loading the state // Load the last state before loading the state
void UndoLoadState() void UndoLoadState()
{ {
std::lock_guard lk(g_cs_undo_load_buffer); std::lock_guard lk(s_undo_load_buffer_mutex);
if (!g_undo_load_buffer.empty()) if (!s_undo_load_buffer.empty())
{ {
if (File::Exists(File::GetUserPath(D_STATESAVES_IDX) + "undo.dtm") || (!Movie::IsMovieActive()))
{
LoadFromBuffer(g_undo_load_buffer);
if (Movie::IsMovieActive()) 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 else
{ {
@ -804,6 +807,11 @@ void UndoLoadState()
} }
} }
else else
{
LoadFromBuffer(s_undo_load_buffer);
}
}
else
{ {
PanicAlertFmtT("There is nothing to undo!"); PanicAlertFmtT("There is nothing to undo!");
} }