also fix crashes when inserting/ejecting a NDS cart while nothing is loaded

This commit is contained in:
Arisotura 2024-11-17 15:43:22 +01:00
parent 0a4287c6ad
commit 871a167d8b
4 changed files with 41 additions and 22 deletions

View File

@ -77,6 +77,8 @@ EmuInstance::EmuInstance(int inst) : deleting(false),
baseROMDir = ""; baseROMDir = "";
baseROMName = ""; baseROMName = "";
baseAssetName = ""; baseAssetName = "";
nextCart = nullptr;
changeCart = false;
gbaSave = nullptr; gbaSave = nullptr;
gbaCartType = -1; gbaCartType = -1;
@ -120,7 +122,7 @@ EmuInstance::EmuInstance(int inst) : deleting(false),
mpAudioMode = globalCfg.GetInt("MP.AudioMode"); mpAudioMode = globalCfg.GetInt("MP.AudioMode");
nds = nullptr; nds = nullptr;
//updateConsole(nullptr); //updateConsole();
audioInit(); audioInit();
inputInit(); inputInit();
@ -1217,23 +1219,22 @@ void EmuInstance::setDateTime()
time.time().hour(), time.time().minute(), time.time().second()); time.time().hour(), time.time().minute(), time.time().second());
} }
bool EmuInstance::updateConsole(UpdateConsoleNDSArgs&& _ndsargs) noexcept bool EmuInstance::updateConsole() noexcept
{ {
// update the console type // update the console type
consoleType = globalCfg.GetInt("Emu.ConsoleType"); consoleType = globalCfg.GetInt("Emu.ConsoleType");
// Let's get the cart we want to use; // Let's get the cart we want to use;
// if we wnat to keep the cart, we'll eject it from the existing console first. // if we want to keep the cart, we'll eject it from the existing console first.
std::unique_ptr<NDSCart::CartCommon> nextndscart; std::unique_ptr<NDSCart::CartCommon> nextndscart;
if (std::holds_alternative<Keep>(_ndsargs)) if (!changeCart)
{ // If we want to keep the existing cart (if any)... { // If we want to keep the existing cart (if any)...
nextndscart = nds ? nds->EjectCart() : nullptr; nextndscart = nds ? nds->EjectCart() : nullptr;
_ndsargs = {};
} }
else if (const auto ptr = std::get_if<std::unique_ptr<NDSCart::CartCommon>>(&_ndsargs)) else
{ {
nextndscart = std::move(*ptr); nextndscart = std::move(nextCart);
_ndsargs = {}; changeCart = false;
} }
if (auto* cartsd = dynamic_cast<NDSCart::CartSD*>(nextndscart.get())) if (auto* cartsd = dynamic_cast<NDSCart::CartSD*>(nextndscart.get()))
@ -1388,12 +1389,14 @@ bool EmuInstance::updateConsole(UpdateConsoleNDSArgs&& _ndsargs) noexcept
} }
renderLock.unlock(); renderLock.unlock();
loadCheats();
return true; return true;
} }
void EmuInstance::reset() void EmuInstance::reset()
{ {
updateConsole(Keep {}); updateConsole();
if (consoleType == 1) ejectGBACart(); if (consoleType == 1) ejectGBACart();
@ -1457,7 +1460,7 @@ void EmuInstance::reset()
bool EmuInstance::bootToMenu() bool EmuInstance::bootToMenu()
{ {
// Keep whatever cart is in the console, if any. // Keep whatever cart is in the console, if any.
if (!updateConsole(Keep {})) 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...
return false; return false;
@ -1912,7 +1915,10 @@ bool EmuInstance::loadROM(QStringList filepath, bool reset)
if (reset) if (reset)
{ {
if (!updateConsole(std::move(cart))) nextCart = std::move(cart);
changeCart = true;
if (!updateConsole())
{ {
QMessageBox::critical(mainWindow, "melonDS", "Failed to load the DS ROM."); QMessageBox::critical(mainWindow, "melonDS", "Failed to load the DS ROM.");
return false; return false;
@ -1931,13 +1937,20 @@ bool EmuInstance::loadROM(QStringList filepath, bool reset)
} }
else else
{ {
assert(nds != nullptr); if (emuIsActive())
{
nds->SetNDSCart(std::move(cart)); nds->SetNDSCart(std::move(cart));
loadCheats();
}
else
{
nextCart = std::move(cart);
changeCart = true;
}
} }
cartType = 0; cartType = 0;
ndsSave = std::make_unique<SaveManager>(savname); ndsSave = std::make_unique<SaveManager>(savname);
loadCheats();
return true; // success return true; // success
} }
@ -1946,9 +1959,16 @@ void EmuInstance::ejectCart()
{ {
ndsSave = nullptr; ndsSave = nullptr;
unloadCheats(); if (emuIsActive())
{
nds->EjectCart(); nds->EjectCart();
unloadCheats();
}
else
{
nextCart = nullptr;
changeCart = true;
}
cartType = -1; cartType = -1;
baseROMDir = ""; baseROMDir = "";

View File

@ -120,7 +120,7 @@ public:
// return: empty string = setup OK, non-empty = error message // return: empty string = setup OK, non-empty = error message
QString verifySetup(); QString verifySetup();
bool updateConsole(UpdateConsoleNDSArgs&& ndsargs) noexcept; bool updateConsole() noexcept;
void enableCheats(bool enable); void enableCheats(bool enable);
melonDS::ARCodeFile* getCheatFile(); melonDS::ARCodeFile* getCheatFile();
@ -261,6 +261,8 @@ private:
std::string baseROMDir; std::string baseROMDir;
std::string baseROMName; std::string baseROMName;
std::string baseAssetName; std::string baseAssetName;
bool changeCart;
std::unique_ptr<melonDS::NDSCart::CartCommon> nextCart;
int gbaCartType; int gbaCartType;
std::string baseGBAROMDir; std::string baseGBAROMDir;

View File

@ -109,7 +109,7 @@ void EmuThread::run()
Config::Table& globalCfg = emuInstance->getGlobalConfig(); Config::Table& globalCfg = emuInstance->getGlobalConfig();
u32 mainScreenPos[3]; u32 mainScreenPos[3];
//emuInstance->updateConsole(nullptr); //emuInstance->updateConsole();
// No carts are inserted when melonDS first boots // No carts are inserted when melonDS first boots
mainScreenPos[0] = 0; mainScreenPos[0] = 0;

View File

@ -33,9 +33,6 @@
#include "NDSCart.h" #include "NDSCart.h"
#include "GBACart.h" #include "GBACart.h"
using Keep = std::monostate;
using UpdateConsoleNDSArgs = std::variant<Keep, std::unique_ptr<melonDS::NDSCart::CartCommon>>;
using UpdateConsoleGBAArgs = std::variant<Keep, std::unique_ptr<melonDS::GBACart::CartCommon>>;
namespace melonDS namespace melonDS
{ {
class NDS; class NDS;