From 979f1ed6159af0aae2768dc7c4c2711fa551f946 Mon Sep 17 00:00:00 2001 From: Arisotura Date: Fri, 25 Oct 2024 10:52:47 +0200 Subject: [PATCH] same shit with Import Savefile --- src/frontend/qt_sdl/EmuThread.cpp | 28 ++++++++++++++++++++++++++++ src/frontend/qt_sdl/EmuThread.h | 4 ++++ src/frontend/qt_sdl/Window.cpp | 27 ++++++--------------------- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/frontend/qt_sdl/EmuThread.cpp b/src/frontend/qt_sdl/EmuThread.cpp index d73cd6ea..589f11e2 100644 --- a/src/frontend/qt_sdl/EmuThread.cpp +++ b/src/frontend/qt_sdl/EmuThread.cpp @@ -618,6 +618,26 @@ void EmuThread::handleMessages() emuInstance->undoStateLoad(); msgResult = 1; break; + + case msg_ImportSavefile: + { + msgResult = 0; + auto f = Platform::OpenFile(msg.param.value().toStdString(), Platform::FileMode::Read); + if (!f) break; + + u32 len = FileLength(f); + + std::unique_ptr data = std::make_unique(len); + Platform::FileRewind(f); + Platform::FileRead(data.get(), len, 1, f); + + assert(emuInstance->nds != nullptr); + emuInstance->nds->SetNDSSave(data.get(), len); + + CloseFile(f); + msgResult = 1; + } + break; } msgSemaphore.release(); @@ -771,6 +791,14 @@ int EmuThread::undoStateLoad() return msgResult; } +int EmuThread::importSavefile(const QString& filename) +{ + sendMessage(msg_EmuReset); + sendMessage({.type = msg_ImportSavefile, .param = filename}); + waitMessage(2); + return msgResult; +} + void EmuThread::updateRenderer() { if (videoRenderer != lastVideoRenderer) diff --git a/src/frontend/qt_sdl/EmuThread.h b/src/frontend/qt_sdl/EmuThread.h index 52648520..7a07c42c 100644 --- a/src/frontend/qt_sdl/EmuThread.h +++ b/src/frontend/qt_sdl/EmuThread.h @@ -80,6 +80,8 @@ public: msg_LoadState, msg_SaveState, msg_UndoStateLoad, + + msg_ImportSavefile, }; struct Message @@ -119,6 +121,8 @@ public: int loadState(const QString& filename); int undoStateLoad(); + int importSavefile(const QString& filename); + bool emuIsRunning(); bool emuIsActive(); diff --git a/src/frontend/qt_sdl/Window.cpp b/src/frontend/qt_sdl/Window.cpp index 2a694879..08a9474b 100644 --- a/src/frontend/qt_sdl/Window.cpp +++ b/src/frontend/qt_sdl/Window.cpp @@ -1483,23 +1483,17 @@ void MainWindow::onUndoStateLoad() void MainWindow::onImportSavefile() { - emuThread->emuPause(); QString path = QFileDialog::getOpenFileName(this, "Select savefile", globalCfg.GetQString("LastROMFolder"), "Savefiles (*.sav *.bin *.dsv);;Any file (*.*)"); if (path.isEmpty()) - { - emuThread->emuUnpause(); return; - } - Platform::FileHandle* f = Platform::OpenFile(path.toStdString(), Platform::FileMode::Read); - if (!f) + if (!Platform::FileExists(path.toStdString())) { QMessageBox::critical(this, "melonDS", "Could not open the given savefile."); - emuThread->emuUnpause(); return; } @@ -1510,24 +1504,15 @@ void MainWindow::onImportSavefile() "The emulation will be reset and the current savefile overwritten.", QMessageBox::Ok, QMessageBox::Cancel) != QMessageBox::Ok) { - emuThread->emuUnpause(); return; } - - emuInstance->reset(); } - u32 len = FileLength(f); - - std::unique_ptr data = std::make_unique(len); - Platform::FileRewind(f); - Platform::FileRead(data.get(), len, 1, f); - - assert(emuInstance->nds != nullptr); - emuInstance->nds->SetNDSSave(data.get(), len); - - CloseFile(f); - emuThread->emuUnpause(); + if (!emuThread->importSavefile(path)) + { + QMessageBox::critical(this, "melonDS", "Could not import the given savefile."); + return; + } } void MainWindow::onQuit()