diff --git a/src/core/cdrom.cpp b/src/core/cdrom.cpp index 8a886db38..58b5a3106 100644 --- a/src/core/cdrom.cpp +++ b/src/core/cdrom.cpp @@ -120,8 +120,6 @@ bool CDROM::DoState(StateWrapper& sw) sw.Do(&m_sector_buffer); u32 media_lba = m_media ? m_media->GetPositionOnDisc() : 0; - std::string media_filename = m_media ? m_media->GetFileName() : std::string(); - sw.Do(&media_filename); sw.Do(&media_lba); if (sw.IsReading()) @@ -132,21 +130,24 @@ bool CDROM::DoState(StateWrapper& sw) m_system->SetDowncount(m_drive_remaining_ticks); // load up media if we had something in there before - m_media.reset(); - if (!media_filename.empty()) + if (m_media && !m_media->Seek(media_lba)) { - m_media = CDImage::Open(media_filename.c_str()); - if (!m_media || !m_media->Seek(media_lba)) - { - Log_ErrorPrintf("Failed to re-insert CD media from save state: '%s'. Ejecting.", media_filename.c_str()); - RemoveMedia(); - } + Log_ErrorPrint("Failed to seek CD media from save state. Ejecting."); + RemoveMedia(); } } return !sw.HasError(); } +std::string CDROM::GetMediaFileName() const +{ + if (!m_media) + return std::string(); + + return m_media->GetFileName(); +} + void CDROM::InsertMedia(std::unique_ptr media) { if (HasMedia()) diff --git a/src/core/cdrom.h b/src/core/cdrom.h index 95f3a9f34..02a95cca7 100644 --- a/src/core/cdrom.h +++ b/src/core/cdrom.h @@ -27,6 +27,7 @@ public: bool DoState(StateWrapper& sw); bool HasMedia() const { return static_cast(m_media); } + std::string GetMediaFileName() const; void InsertMedia(std::unique_ptr media); void RemoveMedia(); diff --git a/src/core/host_interface.cpp b/src/core/host_interface.cpp index 115968b30..ecd271df8 100644 --- a/src/core/host_interface.cpp +++ b/src/core/host_interface.cpp @@ -497,7 +497,7 @@ void HostInterface::ResetPerformanceCounters() void HostInterface::UpdateRunningGame(const char* path, CDImage* image) { - if (!path) + if (!path || std::strlen(path) == 0) { OnRunningGameChanged("", "", ""); return; diff --git a/src/core/system.cpp b/src/core/system.cpp index db1befc6d..5a7e41b93 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -238,6 +238,26 @@ bool System::DoState(StateWrapper& sw) sw.Do(&m_internal_frame_number); sw.Do(&m_global_tick_counter); + std::string media_filename = m_cdrom->GetMediaFileName(); + sw.Do(&media_filename); + + if (sw.IsReading()) + { + std::unique_ptr media; + if (!media_filename.empty()) + { + media = CDImage::Open(media_filename.c_str()); + if (!media) + Log_ErrorPrintf("Failed to open CD image from save state: '%s'", media_filename.c_str()); + } + + m_host_interface->UpdateRunningGame(media_filename.c_str(), media.get()); + if (media) + m_cdrom->InsertMedia(std::move(media)); + else + m_cdrom->RemoveMedia(); + } + if (!sw.DoMarker("CPU") || !m_cpu->DoState(sw)) return false;