avoid spawning message boxes from the emuthread

This commit is contained in:
Arisotura 2024-11-19 00:33:39 +01:00
parent 259eb4b408
commit 317b91533b
5 changed files with 94 additions and 46 deletions

View File

@ -29,7 +29,6 @@
#include <fstream> #include <fstream>
#include <QDateTime> #include <QDateTime>
#include <QMessageBox>
#include <zstd.h> #include <zstd.h>
#ifdef ARCHIVE_SUPPORT_ENABLED #ifdef ARCHIVE_SUPPORT_ENABLED
@ -1457,16 +1456,22 @@ void EmuInstance::reset()
} }
bool EmuInstance::bootToMenu() bool EmuInstance::bootToMenu(QString& errorstr)
{ {
// Keep whatever cart is in the console, if any. // Keep whatever cart is in the console, if any.
if (!updateConsole()) if (!updateConsole())
{
// Try to update the console, but keep the existing cart. If that fails... // Try to update the console, but keep the existing cart. If that fails...
errorstr = "Failed to boot the firmware.";
return false; return false;
}
// BIOS and firmware files are loaded, patched, and installed in UpdateConsole // BIOS and firmware files are loaded, patched, and installed in UpdateConsole
if (nds->NeedsDirectBoot()) if (nds->NeedsDirectBoot())
{
errorstr = "This firmware is not bootable.";
return false; return false;
}
initFirmwareSaveManager(); initFirmwareSaveManager();
nds->Reset(); nds->Reset();
@ -1843,7 +1848,7 @@ QString EmuInstance::getSavErrorString(std::string& filepath, bool gba)
return QString::fromStdString(err1); return QString::fromStdString(err1);
} }
bool EmuInstance::loadROM(QStringList filepath, bool reset) bool EmuInstance::loadROM(QStringList filepath, bool reset, QString& errorstr)
{ {
unique_ptr<u8[]> filedata = nullptr; unique_ptr<u8[]> filedata = nullptr;
u32 filelen; u32 filelen;
@ -1852,7 +1857,7 @@ bool EmuInstance::loadROM(QStringList filepath, bool reset)
if (!loadROMData(filepath, filedata, filelen, basepath, romname)) if (!loadROMData(filepath, filedata, filelen, basepath, romname))
{ {
QMessageBox::critical(mainWindow, "melonDS", "Failed to load the DS ROM."); errorstr = "Failed to load the DS ROM.";
return false; return false;
} }
@ -1874,7 +1879,7 @@ bool EmuInstance::loadROM(QStringList filepath, bool reset)
{ {
if (!Platform::CheckFileWritable(origsav)) if (!Platform::CheckFileWritable(origsav))
{ {
QMessageBox::critical(mainWindow, "melonDS", getSavErrorString(origsav, false)); errorstr = getSavErrorString(origsav, false);
return false; return false;
} }
@ -1882,7 +1887,7 @@ bool EmuInstance::loadROM(QStringList filepath, bool reset)
} }
else if (!Platform::CheckFileWritable(savname)) else if (!Platform::CheckFileWritable(savname))
{ {
QMessageBox::critical(mainWindow, "melonDS", getSavErrorString(savname, false)); errorstr = getSavErrorString(savname, false);
return false; return false;
} }
@ -1909,7 +1914,7 @@ bool EmuInstance::loadROM(QStringList filepath, bool reset)
if (!cart) if (!cart)
{ {
// If we couldn't parse the ROM... // If we couldn't parse the ROM...
QMessageBox::critical(mainWindow, "melonDS", "Failed to load the DS ROM."); errorstr = "Failed to load the DS ROM.";
return false; return false;
} }
@ -1920,7 +1925,7 @@ bool EmuInstance::loadROM(QStringList filepath, bool reset)
if (!updateConsole()) if (!updateConsole())
{ {
QMessageBox::critical(mainWindow, "melonDS", "Failed to load the DS ROM."); errorstr = "Failed to load the DS ROM.";
return false; return false;
} }
@ -1996,11 +2001,11 @@ QString EmuInstance::cartLabel()
} }
bool EmuInstance::loadGBAROM(QStringList filepath) bool EmuInstance::loadGBAROM(QStringList filepath, QString& errorstr)
{ {
if (consoleType == 1) if (consoleType == 1)
{ {
QMessageBox::critical(mainWindow, "melonDS", "The DSi doesn't have a GBA slot."); errorstr = "The DSi doesn't have a GBA slot.";
return false; return false;
} }
@ -2011,7 +2016,7 @@ bool EmuInstance::loadGBAROM(QStringList filepath)
if (!loadROMData(filepath, filedata, filelen, basepath, romname)) if (!loadROMData(filepath, filedata, filelen, basepath, romname))
{ {
QMessageBox::critical(mainWindow, "melonDS", "Failed to load the GBA ROM."); errorstr = "Failed to load the GBA ROM.";
return false; return false;
} }
@ -2033,7 +2038,7 @@ bool EmuInstance::loadGBAROM(QStringList filepath)
{ {
if (!Platform::CheckFileWritable(origsav)) if (!Platform::CheckFileWritable(origsav))
{ {
QMessageBox::critical(mainWindow, "melonDS", getSavErrorString(origsav, true)); errorstr = getSavErrorString(origsav, true);
return false; return false;
} }
@ -2041,7 +2046,7 @@ bool EmuInstance::loadGBAROM(QStringList filepath)
} }
else if (!Platform::CheckFileWritable(savname)) else if (!Platform::CheckFileWritable(savname))
{ {
QMessageBox::critical(mainWindow, "melonDS", getSavErrorString(savname, true)); errorstr = getSavErrorString(savname, true);
return false; return false;
} }
@ -2061,7 +2066,7 @@ bool EmuInstance::loadGBAROM(QStringList filepath)
auto cart = GBACart::ParseROM(std::move(filedata), filelen, std::move(savedata), savelen, this); auto cart = GBACart::ParseROM(std::move(filedata), filelen, std::move(savedata), savelen, this);
if (!cart) if (!cart)
{ {
QMessageBox::critical(mainWindow, "melonDS", "Failed to load the GBA ROM."); errorstr = "Failed to load the GBA ROM.";
return false; return false;
} }
@ -2080,14 +2085,14 @@ bool EmuInstance::loadGBAROM(QStringList filepath)
return true; return true;
} }
void EmuInstance::loadGBAAddon(int type) void EmuInstance::loadGBAAddon(int type, QString& errorstr)
{ {
if (consoleType == 1) return; if (consoleType == 1) return;
auto cart = GBACart::LoadAddon(type, this); auto cart = GBACart::LoadAddon(type, this);
if (!cart) if (!cart)
{ {
QMessageBox::critical(mainWindow, "melonDS", "Failed to load the GBA addon."); errorstr = "Failed to load the GBA addon.";
return; return;
} }

View File

@ -182,7 +182,7 @@ private:
std::optional<melonDS::FATStorage> loadSDCard(const std::string& key) noexcept; std::optional<melonDS::FATStorage> loadSDCard(const std::string& key) noexcept;
void setBatteryLevels(); void setBatteryLevels();
void reset(); void reset();
bool bootToMenu(); bool bootToMenu(QString& errorstr);
melonDS::u32 decompressROM(const melonDS::u8* inContent, const melonDS::u32 inSize, std::unique_ptr<melonDS::u8[]>& outContent); melonDS::u32 decompressROM(const melonDS::u8* inContent, const melonDS::u32 inSize, std::unique_ptr<melonDS::u8[]>& outContent);
void clearBackupState(); void clearBackupState();
std::pair<std::unique_ptr<melonDS::Firmware>, std::string> generateDefaultFirmware(); std::pair<std::unique_ptr<melonDS::Firmware>, std::string> generateDefaultFirmware();
@ -191,13 +191,13 @@ private:
bool loadROMData(const QStringList& filepath, std::unique_ptr<melonDS::u8[]>& filedata, melonDS::u32& filelen, std::string& basepath, std::string& romname) noexcept; bool loadROMData(const QStringList& filepath, std::unique_ptr<melonDS::u8[]>& filedata, melonDS::u32& filelen, std::string& basepath, std::string& romname) noexcept;
QString getSavErrorString(std::string& filepath, bool gba); QString getSavErrorString(std::string& filepath, bool gba);
bool loadROM(QStringList filepath, bool reset); bool loadROM(QStringList filepath, bool reset, QString& errorstr);
void ejectCart(); void ejectCart();
bool cartInserted(); bool cartInserted();
QString cartLabel(); QString cartLabel();
bool loadGBAROM(QStringList filepath); bool loadGBAROM(QStringList filepath, QString& errorstr);
void loadGBAAddon(int type); void loadGBAAddon(int type, QString& errorstr);
void ejectGBACart(); void ejectGBACart();
bool gbaCartInserted(); bool gbaCartInserted();
QString gbaAddonName(int addon); QString gbaAddonName(int addon);

View File

@ -577,7 +577,7 @@ void EmuThread::handleMessages()
case msg_BootROM: case msg_BootROM:
msgResult = 0; msgResult = 0;
if (!emuInstance->loadROM(msg.param.value<QStringList>(), true)) if (!emuInstance->loadROM(msg.param.value<QStringList>(), true, msgError))
break; break;
assert(emuInstance->nds != nullptr); assert(emuInstance->nds != nullptr);
@ -587,7 +587,7 @@ void EmuThread::handleMessages()
case msg_BootFirmware: case msg_BootFirmware:
msgResult = 0; msgResult = 0;
if (!emuInstance->bootToMenu()) if (!emuInstance->bootToMenu(msgError))
break; break;
assert(emuInstance->nds != nullptr); assert(emuInstance->nds != nullptr);
@ -597,7 +597,7 @@ void EmuThread::handleMessages()
case msg_InsertCart: case msg_InsertCart:
msgResult = 0; msgResult = 0;
if (!emuInstance->loadROM(msg.param.value<QStringList>(), false)) if (!emuInstance->loadROM(msg.param.value<QStringList>(), false, msgError))
break; break;
msgResult = 1; msgResult = 1;
@ -609,7 +609,7 @@ void EmuThread::handleMessages()
case msg_InsertGBACart: case msg_InsertGBACart:
msgResult = 0; msgResult = 0;
if (!emuInstance->loadGBAROM(msg.param.value<QStringList>())) if (!emuInstance->loadGBAROM(msg.param.value<QStringList>(), msgError))
break; break;
msgResult = 1; msgResult = 1;
@ -617,7 +617,7 @@ void EmuThread::handleMessages()
case msg_InsertGBAAddon: case msg_InsertGBAAddon:
msgResult = 0; msgResult = 0;
emuInstance->loadGBAAddon(msg.param.value<int>()); emuInstance->loadGBAAddon(msg.param.value<int>(), msgError);
msgResult = 1; msgResult = 1;
break; break;
@ -753,36 +753,45 @@ bool EmuThread::emuIsActive()
return emuActive; return emuActive;
} }
int EmuThread::bootROM(const QStringList& filename) int EmuThread::bootROM(const QStringList& filename, QString& errorstr)
{ {
sendMessage({.type = msg_BootROM, .param = filename}); sendMessage({.type = msg_BootROM, .param = filename});
waitMessage(); waitMessage();
if (!msgResult) if (!msgResult)
{
errorstr = msgError;
return msgResult; return msgResult;
}
sendMessage(msg_EmuRun); sendMessage(msg_EmuRun);
waitMessage(); waitMessage();
errorstr = "";
return msgResult; return msgResult;
} }
int EmuThread::bootFirmware() int EmuThread::bootFirmware(QString& errorstr)
{ {
sendMessage(msg_BootFirmware); sendMessage(msg_BootFirmware);
waitMessage(); waitMessage();
if (!msgResult) if (!msgResult)
{
errorstr = msgError;
return msgResult; return msgResult;
}
sendMessage(msg_EmuRun); sendMessage(msg_EmuRun);
waitMessage(); waitMessage();
errorstr = "";
return msgResult; return msgResult;
} }
int EmuThread::insertCart(const QStringList& filename, bool gba) int EmuThread::insertCart(const QStringList& filename, bool gba, QString& errorstr)
{ {
MessageType msgtype = gba ? msg_InsertGBACart : msg_InsertCart; MessageType msgtype = gba ? msg_InsertGBACart : msg_InsertCart;
sendMessage({.type = msgtype, .param = filename}); sendMessage({.type = msgtype, .param = filename});
waitMessage(); waitMessage();
errorstr = msgResult ? "" : msgError;
return msgResult; return msgResult;
} }
@ -792,10 +801,11 @@ void EmuThread::ejectCart(bool gba)
waitMessage(); waitMessage();
} }
int EmuThread::insertGBAAddon(int type) int EmuThread::insertGBAAddon(int type, QString& errorstr)
{ {
sendMessage({.type = msg_InsertGBAAddon, .param = type}); sendMessage({.type = msg_InsertGBAAddon, .param = type});
waitMessage(); waitMessage();
errorstr = msgResult ? "" : msgError;
return msgResult; return msgResult;
} }

View File

@ -111,11 +111,11 @@ public:
void emuFrameStep(); void emuFrameStep();
void emuReset(); void emuReset();
int bootROM(const QStringList& filename); int bootROM(const QStringList& filename, QString& errorstr);
int bootFirmware(); int bootFirmware(QString& errorstr);
int insertCart(const QStringList& filename, bool gba); int insertCart(const QStringList& filename, bool gba, QString& errorstr);
void ejectCart(bool gba); void ejectCart(bool gba);
int insertGBAAddon(int type); int insertGBAAddon(int type, QString& errorstr);
int saveState(const QString& filename); int saveState(const QString& filename);
int loadState(const QString& filename); int loadState(const QString& filename);
@ -179,6 +179,7 @@ private:
int emuPauseStack; int emuPauseStack;
int msgResult = 0; int msgResult = 0;
QString msgError;
QMutex msgMutex; QMutex msgMutex;
QSemaphore msgSemaphore; QSemaphore msgSemaphore;

View File

@ -986,10 +986,12 @@ void MainWindow::dropEvent(QDropEvent* event)
isNdsRom |= ZstdNdsRomByExtension(filename); isNdsRom |= ZstdNdsRomByExtension(filename);
isGbaRom |= ZstdGbaRomByExtension(filename); isGbaRom |= ZstdGbaRomByExtension(filename);
QString errorstr;
if (isNdsRom) if (isNdsRom)
{ {
if (!emuThread->bootROM(file)) if (!emuThread->bootROM(file, errorstr))
{ {
QMessageBox::critical(this, "melonDS", errorstr);
return; return;
} }
@ -1002,8 +1004,9 @@ void MainWindow::dropEvent(QDropEvent* event)
} }
else if (isGbaRom) else if (isGbaRom)
{ {
if (!emuThread->insertCart(file, true)) if (!emuThread->insertCart(file, true, errorstr))
{ {
QMessageBox::critical(this, "melonDS", errorstr);
return; return;
} }
@ -1071,6 +1074,8 @@ bool MainWindow::verifySetup()
bool MainWindow::preloadROMs(QStringList file, QStringList gbafile, bool boot) bool MainWindow::preloadROMs(QStringList file, QStringList gbafile, bool boot)
{ {
QString errorstr;
if (file.isEmpty() && gbafile.isEmpty()) if (file.isEmpty() && gbafile.isEmpty())
return false; return false;
@ -1082,8 +1087,11 @@ bool MainWindow::preloadROMs(QStringList file, QStringList gbafile, bool boot)
bool gbaloaded = false; bool gbaloaded = false;
if (!gbafile.isEmpty()) if (!gbafile.isEmpty())
{ {
if (!emuThread->insertCart(gbafile, true)) if (!emuThread->insertCart(gbafile, true, errorstr))
{
QMessageBox::critical(this, "melonDS", errorstr);
return false; return false;
}
gbaloaded = true; gbaloaded = true;
} }
@ -1093,13 +1101,19 @@ bool MainWindow::preloadROMs(QStringList file, QStringList gbafile, bool boot)
{ {
if (boot) if (boot)
{ {
if (!emuThread->bootROM(file)) if (!emuThread->bootROM(file, errorstr))
{
QMessageBox::critical(this, "melonDS", errorstr);
return false; return false;
}
} }
else else
{ {
if (!emuThread->insertCart(file, false)) if (!emuThread->insertCart(file, false, errorstr))
{
QMessageBox::critical(this, "melonDS", errorstr);
return false; return false;
}
} }
recentFileList.removeAll(file.join("|")); recentFileList.removeAll(file.join("|"));
@ -1109,8 +1123,11 @@ bool MainWindow::preloadROMs(QStringList file, QStringList gbafile, bool boot)
} }
else if (boot) else if (boot)
{ {
if (!emuThread->bootFirmware()) if (!emuThread->bootFirmware(errorstr))
{
QMessageBox::critical(this, "melonDS", errorstr);
return false; return false;
}
} }
updateCartInserted(false); updateCartInserted(false);
@ -1308,8 +1325,10 @@ void MainWindow::onOpenFile()
if (file.isEmpty()) if (file.isEmpty())
return; return;
if (!emuThread->bootROM(file)) QString errorstr;
if (!emuThread->bootROM(file, errorstr))
{ {
QMessageBox::critical(this, "melonDS", errorstr);
return; return;
} }
@ -1419,8 +1438,10 @@ void MainWindow::onClickRecentFile()
if (file.isEmpty()) if (file.isEmpty())
return; return;
if (!emuThread->bootROM(file)) QString errorstr;
if (!emuThread->bootROM(file, errorstr))
{ {
QMessageBox::critical(this, "melonDS", errorstr);
return; return;
} }
@ -1436,9 +1457,10 @@ void MainWindow::onBootFirmware()
if (!verifySetup()) if (!verifySetup())
return; return;
if (!emuThread->bootFirmware()) QString errorstr;
if (!emuThread->bootFirmware(errorstr))
{ {
QMessageBox::critical(this, "melonDS", "This firmware is not bootable."); QMessageBox::critical(this, "melonDS", errorstr);
return; return;
} }
} }
@ -1449,8 +1471,10 @@ void MainWindow::onInsertCart()
if (file.isEmpty()) if (file.isEmpty())
return; return;
if (!emuThread->insertCart(file, false)) QString errorstr;
if (!emuThread->insertCart(file, false, errorstr))
{ {
QMessageBox::critical(this, "melonDS", errorstr);
return; return;
} }
@ -1469,8 +1493,10 @@ void MainWindow::onInsertGBACart()
if (file.isEmpty()) if (file.isEmpty())
return; return;
if (!emuThread->insertCart(file, true)) QString errorstr;
if (!emuThread->insertCart(file, true, errorstr))
{ {
QMessageBox::critical(this, "melonDS", errorstr);
return; return;
} }
@ -1482,7 +1508,13 @@ void MainWindow::onInsertGBAAddon()
QAction* act = (QAction*)sender(); QAction* act = (QAction*)sender();
int type = act->data().toInt(); int type = act->data().toInt();
emuThread->insertGBAAddon(type); QString errorstr;
if (!emuThread->insertGBAAddon(type, errorstr))
{
QMessageBox::critical(this, "melonDS", errorstr);
return;
}
updateCartInserted(true); updateCartInserted(true);
} }