From 1bf62f38fc0a5d6fef38b577fcef6620b7962dc1 Mon Sep 17 00:00:00 2001 From: NovaSquirrel Date: Wed, 7 Oct 2020 21:12:39 -0400 Subject: [PATCH] Make History Viewer not lock up the program when closed --- Core/HistoryViewer.cpp | 12 ++++++------ InteropDLL/EmuApiWrapper.cpp | 4 ++-- InteropDLL/HistoryViewerWrapper.cpp | 12 ++++-------- UI/Forms/frmHistoryViewer.cs | 17 ----------------- UI/Interop/HistoryViewerApi.cs | 1 - 5 files changed, 12 insertions(+), 34 deletions(-) diff --git a/Core/HistoryViewer.cpp b/Core/HistoryViewer.cpp index fefeefe..2b11c8c 100644 --- a/Core/HistoryViewer.cpp +++ b/Core/HistoryViewer.cpp @@ -63,7 +63,7 @@ void HistoryViewer::SeekTo(uint32_t seekPosition) { //Seek to the specified position if (seekPosition < _history.size()) { - _console->Pause(); + _console->Lock(); bool wasPaused = _console->IsPaused(); _console->Resume(); @@ -78,7 +78,7 @@ void HistoryViewer::SeekTo(uint32_t seekPosition) _console->Pause(); } - _console->Resume(); + _console->Unlock(); } } @@ -104,7 +104,7 @@ bool HistoryViewer::SaveMovie(string movieFile, uint32_t startPosition, uint32_t //Take a savestate to be able to restore it after generating the movie file //(the movie generation uses the console's inputs, which could affect the emulation otherwise) stringstream state; - _console->Pause(); + _console->Lock(); _console->GetSaveStateManager()->SaveState(state); //Convert the rewind data to a .mmo file @@ -113,13 +113,13 @@ bool HistoryViewer::SaveMovie(string movieFile, uint32_t startPosition, uint32_t //Resume the state and resume _console->GetSaveStateManager()->LoadState(state); - _console->Resume(); + _console->Unlock(); return result; } void HistoryViewer::ResumeGameplay(shared_ptr console, uint32_t resumePosition) { - console->Pause(); + console->Lock(); if (_console->GetRomInfo().RomFile.GetSha1Hash() != console->GetRomInfo().RomFile.GetSha1Hash()) { //Load game on the main window if they aren't the same console->LoadRom(console->GetRomInfo().RomFile, console->GetRomInfo().PatchFile); @@ -131,7 +131,7 @@ void HistoryViewer::ResumeGameplay(shared_ptr console, uint32_t resumeP else { _history[_history.size() - 1].LoadState(console); } - console->Resume(); + console->Unlock(); } bool HistoryViewer::SetInput(BaseControlDevice* device) diff --git a/InteropDLL/EmuApiWrapper.cpp b/InteropDLL/EmuApiWrapper.cpp index 52db82b..003dbe7 100644 --- a/InteropDLL/EmuApiWrapper.cpp +++ b/InteropDLL/EmuApiWrapper.cpp @@ -264,9 +264,9 @@ extern "C" { return _logString.c_str(); } - DllExport ScreenSize __stdcall GetScreenSize(bool ignoreScale) + DllExport ScreenSize __stdcall GetScreenSize(bool ignoreScale, ConsoleId console) { - return _console->GetVideoDecoder()->GetScreenSize(ignoreScale); + return GetConsoleById(console)->GetVideoDecoder()->GetScreenSize(ignoreScale); } DllExport void __stdcall ClearCheats() { _console->GetCheatManager()->ClearCheats(); } diff --git a/InteropDLL/HistoryViewerWrapper.cpp b/InteropDLL/HistoryViewerWrapper.cpp index 828d3bd..073309d 100644 --- a/InteropDLL/HistoryViewerWrapper.cpp +++ b/InteropDLL/HistoryViewerWrapper.cpp @@ -34,8 +34,11 @@ extern "C" _historyConsole.reset(new Console()); // TODO: something about initializing with settings? _historyConsole->Initialize(); + + _historyConsole->Lock(); _historyConsole->LoadRom(_console->GetRomInfo().RomFile, _console->GetRomInfo().PatchFile); _historyConsole->CopyRewindData(_console); + _historyConsole->Unlock(); //Force some settings // TODO @@ -54,20 +57,13 @@ extern "C" DllExport void __stdcall HistoryViewerRelease() { - _historyConsole->Stop(false); // TODO: Check on this + _historyConsole->Stop(true); // TODO: Check on this _historyConsole->Release(); // had True, "For ShutDown" _historyRenderer.reset(); _historySoundManager.reset(); _historyConsole.reset(); } - DllExport void __stdcall HistoryViewerRun() - { - if (_historyConsole) { - _historyConsole->Run(); - } - } - DllExport uint32_t __stdcall HistoryViewerGetHistoryLength() { if (_historyConsole) { diff --git a/UI/Forms/frmHistoryViewer.cs b/UI/Forms/frmHistoryViewer.cs index 577cf10..f2a23a9 100644 --- a/UI/Forms/frmHistoryViewer.cs +++ b/UI/Forms/frmHistoryViewer.cs @@ -51,7 +51,6 @@ namespace Mesen.GUI.Forms HistoryViewerApi.HistoryViewerInitialize(this.Handle, ctrlRenderer.Handle); trkPosition.Maximum = (int)(HistoryViewerApi.HistoryViewerGetHistoryLength() / 60); UpdatePositionLabel(0); - StartEmuThread(); EmuApi.Resume(EmuApi.ConsoleId.HistoryViewer); tmrUpdatePosition.Start(); trkVolume.Value = ConfigManager.Config.HistoryViewer.Volume; @@ -70,22 +69,6 @@ namespace Mesen.GUI.Forms base.OnClosing(e); } - private void StartEmuThread() - { - if(_emuThread == null) { - _emuThread = new Thread(() => { - try { - HistoryViewerApi.HistoryViewerRun(); - _emuThread = null; - } catch(Exception ex) { - MesenMsgBox.Show("UnexpectedError", MessageBoxButtons.OK, MessageBoxIcon.Error, ex.ToString()); - _emuThread = null; - } - }); - _emuThread.Start(); - } - } - private void TogglePause() { if(trkPosition.Value == trkPosition.Maximum) { diff --git a/UI/Interop/HistoryViewerApi.cs b/UI/Interop/HistoryViewerApi.cs index d5524c5..a39ceb3 100644 --- a/UI/Interop/HistoryViewerApi.cs +++ b/UI/Interop/HistoryViewerApi.cs @@ -14,7 +14,6 @@ namespace Mesen.GUI [DllImport(DllPath)] [return: MarshalAs(UnmanagedType.I1)] public static extern bool HistoryViewerEnabled(); [DllImport(DllPath)] public static extern void HistoryViewerInitialize(IntPtr windowHandle, IntPtr viewerHandle); [DllImport(DllPath)] public static extern void HistoryViewerRelease(); - [DllImport(DllPath)] public static extern void HistoryViewerRun(); [DllImport(DllPath)] public static extern void HistoryViewerStop(); [DllImport(DllPath)] public static extern UInt32 HistoryViewerGetHistoryLength();