From 75e6856af479b81f7066abc97e20d83f781a64c4 Mon Sep 17 00:00:00 2001 From: Arisotura Date: Thu, 24 Oct 2024 19:27:45 +0200 Subject: [PATCH] route savestate stuff through EmuThread --- src/frontend/qt_sdl/EmuThread.cpp | 70 +++++++++++++++++++++++-------- src/frontend/qt_sdl/EmuThread.h | 10 ++++- src/frontend/qt_sdl/Window.cpp | 45 ++++++-------------- 3 files changed, 73 insertions(+), 52 deletions(-) diff --git a/src/frontend/qt_sdl/EmuThread.cpp b/src/frontend/qt_sdl/EmuThread.cpp index 590d10f5..d73cd6ea 100644 --- a/src/frontend/qt_sdl/EmuThread.cpp +++ b/src/frontend/qt_sdl/EmuThread.cpp @@ -557,31 +557,31 @@ void EmuThread::handleMessages() break; case msg_BootROM: - bootResult = 0; + msgResult = 0; if (!emuInstance->loadROM(msg.param.value(), true)) break; assert(emuInstance->nds != nullptr); emuInstance->nds->Start(); - bootResult = 1; + msgResult = 1; break; case msg_BootFirmware: - bootResult = 0; + msgResult = 0; if (!emuInstance->bootToMenu()) break; assert(emuInstance->nds != nullptr); emuInstance->nds->Start(); - bootResult = 1; + msgResult = 1; break; case msg_InsertCart: - bootResult = 0; + msgResult = 0; if (!emuInstance->loadROM(msg.param.value(), false)) break; - bootResult = 1; + msgResult = 1; break; case msg_EjectCart: @@ -589,22 +589,35 @@ void EmuThread::handleMessages() break; case msg_InsertGBACart: - bootResult = 0; + msgResult = 0; if (!emuInstance->loadGBAROM(msg.param.value())) break; - bootResult = 1; + msgResult = 1; break; case msg_InsertGBAAddon: - bootResult = 0; + msgResult = 0; emuInstance->loadGBAAddon(msg.param.value()); - bootResult = 1; + msgResult = 1; break; case msg_EjectGBACart: emuInstance->ejectGBACart(); break; + + case msg_SaveState: + msgResult = emuInstance->saveState(msg.param.value().toStdString()); + break; + + case msg_LoadState: + msgResult = emuInstance->loadState(msg.param.value().toStdString()); + break; + + case msg_UndoStateLoad: + emuInstance->undoStateLoad(); + msgResult = 1; + break; } msgSemaphore.release(); @@ -695,24 +708,24 @@ int EmuThread::bootROM(const QStringList& filename) { sendMessage({.type = msg_BootROM, .param = filename}); waitMessage(); - if (!bootResult) - return bootResult; + if (!msgResult) + return msgResult; sendMessage(msg_EmuRun); waitMessage(); - return bootResult; + return msgResult; } int EmuThread::bootFirmware() { sendMessage(msg_BootFirmware); waitMessage(); - if (!bootResult) - return bootResult; + if (!msgResult) + return msgResult; sendMessage(msg_EmuRun); waitMessage(); - return bootResult; + return msgResult; } int EmuThread::insertCart(const QStringList& filename, bool gba) @@ -721,7 +734,7 @@ int EmuThread::insertCart(const QStringList& filename, bool gba) sendMessage({.type = msgtype, .param = filename}); waitMessage(); - return bootResult; + return msgResult; } void EmuThread::ejectCart(bool gba) @@ -734,7 +747,28 @@ int EmuThread::insertGBAAddon(int type) { sendMessage({.type = msg_InsertGBAAddon, .param = type}); waitMessage(); - return bootResult; + return msgResult; +} + +int EmuThread::saveState(const QString& filename) +{ + sendMessage({.type = msg_SaveState, .param = filename}); + waitMessage(); + return msgResult; +} + +int EmuThread::loadState(const QString& filename) +{ + sendMessage({.type = msg_LoadState, .param = filename}); + waitMessage(); + return msgResult; +} + +int EmuThread::undoStateLoad() +{ + sendMessage(msg_UndoStateLoad); + waitMessage(); + return msgResult; } void EmuThread::updateRenderer() diff --git a/src/frontend/qt_sdl/EmuThread.h b/src/frontend/qt_sdl/EmuThread.h index a21cc511..52648520 100644 --- a/src/frontend/qt_sdl/EmuThread.h +++ b/src/frontend/qt_sdl/EmuThread.h @@ -76,6 +76,10 @@ public: msg_InsertGBACart, msg_InsertGBAAddon, msg_EjectGBACart, + + msg_LoadState, + msg_SaveState, + msg_UndoStateLoad, }; struct Message @@ -111,6 +115,10 @@ public: void ejectCart(bool gba); int insertGBAAddon(int type); + int saveState(const QString& filename); + int loadState(const QString& filename); + int undoStateLoad(); + bool emuIsRunning(); bool emuIsActive(); @@ -164,7 +172,7 @@ private: constexpr static int emuPauseStackPauseThreshold = 1; int emuPauseStack; - int bootResult = 0; + int msgResult = 0; QMutex msgMutex; QSemaphore msgSemaphore; diff --git a/src/frontend/qt_sdl/Window.cpp b/src/frontend/qt_sdl/Window.cpp index 65e85f1c..cf65fe2c 100644 --- a/src/frontend/qt_sdl/Window.cpp +++ b/src/frontend/qt_sdl/Window.cpp @@ -1387,30 +1387,23 @@ void MainWindow::onSaveState() { int slot = ((QAction*)sender())->data().toInt(); - emuThread->emuPause(); - - std::string filename; + QString filename; if (slot > 0) { - filename = emuInstance->getSavestateName(slot); + filename = QString::fromStdString(emuInstance->getSavestateName(slot)); } else { // TODO: specific 'last directory' for savestate files? - QString qfilename = QFileDialog::getSaveFileName(this, + filename = QFileDialog::getSaveFileName(this, "Save state", globalCfg.GetQString("LastROMFolder"), "melonDS savestates (*.mln);;Any file (*.*)"); - if (qfilename.isEmpty()) - { - emuThread->emuUnpause(); + if (filename.isEmpty()) return; - } - - filename = qfilename.toStdString(); } - if (emuInstance->saveState(filename)) + if (emuThread->saveState(filename)) { if (slot > 0) emuInstance->osdAddMessage(0, "State saved to slot %d", slot); else emuInstance->osdAddMessage(0, "State saved to file"); @@ -1421,47 +1414,37 @@ void MainWindow::onSaveState() { emuInstance->osdAddMessage(0xFFA0A0, "State save failed"); } - - emuThread->emuUnpause(); } void MainWindow::onLoadState() { int slot = ((QAction*)sender())->data().toInt(); - emuThread->emuPause(); - - std::string filename; + QString filename; if (slot > 0) { - filename = emuInstance->getSavestateName(slot); + filename = QString::fromStdString(emuInstance->getSavestateName(slot)); } else { // TODO: specific 'last directory' for savestate files? - QString qfilename = QFileDialog::getOpenFileName(this, + filename = QFileDialog::getOpenFileName(this, "Load state", globalCfg.GetQString("LastROMFolder"), "melonDS savestates (*.ml*);;Any file (*.*)"); - if (qfilename.isEmpty()) - { - emuThread->emuUnpause(); + if (filename.isEmpty()) return; - } - - filename = qfilename.toStdString(); } - if (!Platform::FileExists(filename)) + if (!Platform::FileExists(filename.toStdString())) { if (slot > 0) emuInstance->osdAddMessage(0xFFA0A0, "State slot %d is empty", slot); else emuInstance->osdAddMessage(0xFFA0A0, "State file does not exist"); - emuThread->emuUnpause(); return; } - if (emuInstance->loadState(filename)) + if (emuThread->loadState(filename)) { if (slot > 0) emuInstance->osdAddMessage(0, "State loaded from slot %d", slot); else emuInstance->osdAddMessage(0, "State loaded from file"); @@ -1472,15 +1455,11 @@ void MainWindow::onLoadState() { emuInstance->osdAddMessage(0xFFA0A0, "State load failed"); } - - emuThread->emuUnpause(); } void MainWindow::onUndoStateLoad() { - emuThread->emuPause(); - emuInstance->undoStateLoad(); - emuThread->emuUnpause(); + emuThread->undoStateLoad(); emuInstance->osdAddMessage(0, "State load undone"); }