route savestate stuff through EmuThread

This commit is contained in:
Arisotura 2024-10-24 19:27:45 +02:00
parent 3fc065d72d
commit 75e6856af4
3 changed files with 73 additions and 52 deletions

View File

@ -557,31 +557,31 @@ void EmuThread::handleMessages()
break; break;
case msg_BootROM: case msg_BootROM:
bootResult = 0; msgResult = 0;
if (!emuInstance->loadROM(msg.param.value<QStringList>(), true)) if (!emuInstance->loadROM(msg.param.value<QStringList>(), true))
break; break;
assert(emuInstance->nds != nullptr); assert(emuInstance->nds != nullptr);
emuInstance->nds->Start(); emuInstance->nds->Start();
bootResult = 1; msgResult = 1;
break; break;
case msg_BootFirmware: case msg_BootFirmware:
bootResult = 0; msgResult = 0;
if (!emuInstance->bootToMenu()) if (!emuInstance->bootToMenu())
break; break;
assert(emuInstance->nds != nullptr); assert(emuInstance->nds != nullptr);
emuInstance->nds->Start(); emuInstance->nds->Start();
bootResult = 1; msgResult = 1;
break; break;
case msg_InsertCart: case msg_InsertCart:
bootResult = 0; msgResult = 0;
if (!emuInstance->loadROM(msg.param.value<QStringList>(), false)) if (!emuInstance->loadROM(msg.param.value<QStringList>(), false))
break; break;
bootResult = 1; msgResult = 1;
break; break;
case msg_EjectCart: case msg_EjectCart:
@ -589,22 +589,35 @@ void EmuThread::handleMessages()
break; break;
case msg_InsertGBACart: case msg_InsertGBACart:
bootResult = 0; msgResult = 0;
if (!emuInstance->loadGBAROM(msg.param.value<QStringList>())) if (!emuInstance->loadGBAROM(msg.param.value<QStringList>()))
break; break;
bootResult = 1; msgResult = 1;
break; break;
case msg_InsertGBAAddon: case msg_InsertGBAAddon:
bootResult = 0; msgResult = 0;
emuInstance->loadGBAAddon(msg.param.value<int>()); emuInstance->loadGBAAddon(msg.param.value<int>());
bootResult = 1; msgResult = 1;
break; break;
case msg_EjectGBACart: case msg_EjectGBACart:
emuInstance->ejectGBACart(); emuInstance->ejectGBACart();
break; break;
case msg_SaveState:
msgResult = emuInstance->saveState(msg.param.value<QString>().toStdString());
break;
case msg_LoadState:
msgResult = emuInstance->loadState(msg.param.value<QString>().toStdString());
break;
case msg_UndoStateLoad:
emuInstance->undoStateLoad();
msgResult = 1;
break;
} }
msgSemaphore.release(); msgSemaphore.release();
@ -695,24 +708,24 @@ int EmuThread::bootROM(const QStringList& filename)
{ {
sendMessage({.type = msg_BootROM, .param = filename}); sendMessage({.type = msg_BootROM, .param = filename});
waitMessage(); waitMessage();
if (!bootResult) if (!msgResult)
return bootResult; return msgResult;
sendMessage(msg_EmuRun); sendMessage(msg_EmuRun);
waitMessage(); waitMessage();
return bootResult; return msgResult;
} }
int EmuThread::bootFirmware() int EmuThread::bootFirmware()
{ {
sendMessage(msg_BootFirmware); sendMessage(msg_BootFirmware);
waitMessage(); waitMessage();
if (!bootResult) if (!msgResult)
return bootResult; return msgResult;
sendMessage(msg_EmuRun); sendMessage(msg_EmuRun);
waitMessage(); waitMessage();
return bootResult; return msgResult;
} }
int EmuThread::insertCart(const QStringList& filename, bool gba) 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}); sendMessage({.type = msgtype, .param = filename});
waitMessage(); waitMessage();
return bootResult; return msgResult;
} }
void EmuThread::ejectCart(bool gba) void EmuThread::ejectCart(bool gba)
@ -734,7 +747,28 @@ int EmuThread::insertGBAAddon(int type)
{ {
sendMessage({.type = msg_InsertGBAAddon, .param = type}); sendMessage({.type = msg_InsertGBAAddon, .param = type});
waitMessage(); 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() void EmuThread::updateRenderer()

View File

@ -76,6 +76,10 @@ public:
msg_InsertGBACart, msg_InsertGBACart,
msg_InsertGBAAddon, msg_InsertGBAAddon,
msg_EjectGBACart, msg_EjectGBACart,
msg_LoadState,
msg_SaveState,
msg_UndoStateLoad,
}; };
struct Message struct Message
@ -111,6 +115,10 @@ public:
void ejectCart(bool gba); void ejectCart(bool gba);
int insertGBAAddon(int type); int insertGBAAddon(int type);
int saveState(const QString& filename);
int loadState(const QString& filename);
int undoStateLoad();
bool emuIsRunning(); bool emuIsRunning();
bool emuIsActive(); bool emuIsActive();
@ -164,7 +172,7 @@ private:
constexpr static int emuPauseStackPauseThreshold = 1; constexpr static int emuPauseStackPauseThreshold = 1;
int emuPauseStack; int emuPauseStack;
int bootResult = 0; int msgResult = 0;
QMutex msgMutex; QMutex msgMutex;
QSemaphore msgSemaphore; QSemaphore msgSemaphore;

View File

@ -1387,30 +1387,23 @@ void MainWindow::onSaveState()
{ {
int slot = ((QAction*)sender())->data().toInt(); int slot = ((QAction*)sender())->data().toInt();
emuThread->emuPause(); QString filename;
std::string filename;
if (slot > 0) if (slot > 0)
{ {
filename = emuInstance->getSavestateName(slot); filename = QString::fromStdString(emuInstance->getSavestateName(slot));
} }
else else
{ {
// TODO: specific 'last directory' for savestate files? // TODO: specific 'last directory' for savestate files?
QString qfilename = QFileDialog::getSaveFileName(this, filename = QFileDialog::getSaveFileName(this,
"Save state", "Save state",
globalCfg.GetQString("LastROMFolder"), globalCfg.GetQString("LastROMFolder"),
"melonDS savestates (*.mln);;Any file (*.*)"); "melonDS savestates (*.mln);;Any file (*.*)");
if (qfilename.isEmpty()) if (filename.isEmpty())
{
emuThread->emuUnpause();
return; return;
} }
filename = qfilename.toStdString(); if (emuThread->saveState(filename))
}
if (emuInstance->saveState(filename))
{ {
if (slot > 0) emuInstance->osdAddMessage(0, "State saved to slot %d", slot); if (slot > 0) emuInstance->osdAddMessage(0, "State saved to slot %d", slot);
else emuInstance->osdAddMessage(0, "State saved to file"); else emuInstance->osdAddMessage(0, "State saved to file");
@ -1421,47 +1414,37 @@ void MainWindow::onSaveState()
{ {
emuInstance->osdAddMessage(0xFFA0A0, "State save failed"); emuInstance->osdAddMessage(0xFFA0A0, "State save failed");
} }
emuThread->emuUnpause();
} }
void MainWindow::onLoadState() void MainWindow::onLoadState()
{ {
int slot = ((QAction*)sender())->data().toInt(); int slot = ((QAction*)sender())->data().toInt();
emuThread->emuPause(); QString filename;
std::string filename;
if (slot > 0) if (slot > 0)
{ {
filename = emuInstance->getSavestateName(slot); filename = QString::fromStdString(emuInstance->getSavestateName(slot));
} }
else else
{ {
// TODO: specific 'last directory' for savestate files? // TODO: specific 'last directory' for savestate files?
QString qfilename = QFileDialog::getOpenFileName(this, filename = QFileDialog::getOpenFileName(this,
"Load state", "Load state",
globalCfg.GetQString("LastROMFolder"), globalCfg.GetQString("LastROMFolder"),
"melonDS savestates (*.ml*);;Any file (*.*)"); "melonDS savestates (*.ml*);;Any file (*.*)");
if (qfilename.isEmpty()) if (filename.isEmpty())
{
emuThread->emuUnpause();
return; return;
} }
filename = qfilename.toStdString(); if (!Platform::FileExists(filename.toStdString()))
}
if (!Platform::FileExists(filename))
{ {
if (slot > 0) emuInstance->osdAddMessage(0xFFA0A0, "State slot %d is empty", slot); if (slot > 0) emuInstance->osdAddMessage(0xFFA0A0, "State slot %d is empty", slot);
else emuInstance->osdAddMessage(0xFFA0A0, "State file does not exist"); else emuInstance->osdAddMessage(0xFFA0A0, "State file does not exist");
emuThread->emuUnpause();
return; return;
} }
if (emuInstance->loadState(filename)) if (emuThread->loadState(filename))
{ {
if (slot > 0) emuInstance->osdAddMessage(0, "State loaded from slot %d", slot); if (slot > 0) emuInstance->osdAddMessage(0, "State loaded from slot %d", slot);
else emuInstance->osdAddMessage(0, "State loaded from file"); else emuInstance->osdAddMessage(0, "State loaded from file");
@ -1472,15 +1455,11 @@ void MainWindow::onLoadState()
{ {
emuInstance->osdAddMessage(0xFFA0A0, "State load failed"); emuInstance->osdAddMessage(0xFFA0A0, "State load failed");
} }
emuThread->emuUnpause();
} }
void MainWindow::onUndoStateLoad() void MainWindow::onUndoStateLoad()
{ {
emuThread->emuPause(); emuThread->undoStateLoad();
emuInstance->undoStateLoad();
emuThread->emuUnpause();
emuInstance->osdAddMessage(0, "State load undone"); emuInstance->osdAddMessage(0, "State load undone");
} }