CDROM: Fix crash when loading save state from different system

This commit is contained in:
Connor McLaughlin 2020-04-08 13:08:27 +10:00
parent 9851b75368
commit 08567fedf4
3 changed files with 11 additions and 4 deletions

View File

@ -195,9 +195,9 @@ void CDROM::InsertMedia(std::unique_ptr<CDImage> media)
m_reader.SetMedia(std::move(media)); m_reader.SetMedia(std::move(media));
} }
void CDROM::RemoveMedia() void CDROM::RemoveMedia(bool force /* = false */)
{ {
if (!m_reader.HasMedia()) if (!m_reader.HasMedia() && !force)
return; return;
Log_InfoPrintf("Removing CD..."); Log_InfoPrintf("Removing CD...");

View File

@ -31,7 +31,7 @@ public:
bool HasMedia() const; bool HasMedia() const;
std::string GetMediaFileName() const; std::string GetMediaFileName() const;
void InsertMedia(std::unique_ptr<CDImage> media); void InsertMedia(std::unique_ptr<CDImage> media);
void RemoveMedia(); void RemoveMedia(bool force = false);
// I/O // I/O
u8 ReadRegister(u32 offset); u8 ReadRegister(u32 offset);

View File

@ -327,6 +327,7 @@ bool System::DoState(StateWrapper& sw)
std::string media_filename = m_cdrom->GetMediaFileName(); std::string media_filename = m_cdrom->GetMediaFileName();
sw.Do(&media_filename); sw.Do(&media_filename);
bool media_is_bad = false;
if (sw.IsReading()) if (sw.IsReading())
{ {
std::unique_ptr<CDImage> media; std::unique_ptr<CDImage> media;
@ -334,7 +335,10 @@ bool System::DoState(StateWrapper& sw)
{ {
media = CDImage::Open(media_filename.c_str()); media = CDImage::Open(media_filename.c_str());
if (!media) if (!media)
Log_ErrorPrintf("Failed to open CD image from save state: '%s'", media_filename.c_str()); {
Log_ErrorPrintf("Failed to open CD image from save state: '%s'. Disc will be removed.", media_filename.c_str());
media_is_bad = true;
}
} }
UpdateRunningGame(media_filename.c_str(), media.get()); UpdateRunningGame(media_filename.c_str(), media.get());
@ -384,6 +388,9 @@ bool System::DoState(StateWrapper& sw)
if (!sw.DoMarker("Events") || !DoEventsState(sw)) if (!sw.DoMarker("Events") || !DoEventsState(sw))
return false; return false;
if (media_is_bad)
m_cdrom->RemoveMedia(true);
return !sw.HasError(); return !sw.HasError();
} }