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

View File

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

View File

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

View File

@ -33,9 +33,6 @@
#include "NDSCart.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
{
class NDS;