diff --git a/src/core/system.cpp b/src/core/system.cpp index 5c2cc05ab..4ed19e5d3 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -132,6 +132,25 @@ void System::SetCPUExecutionMode(CPUExecutionMode mode) m_cpu_code_cache->SetUseRecompiler(mode == CPUExecutionMode::Recompiler); } +std::unique_ptr System::OpenCDImage(const char* path, bool force_preload) +{ + std::unique_ptr media = CDImage::Open(path); + if (!media) + return {}; + + if (force_preload || GetSettings().cdrom_load_image_to_ram) + { + HostInterfaceProgressCallback callback(m_host_interface); + std::unique_ptr memory_image = CDImage::CreateMemoryImage(media.get(), &callback); + if (memory_image) + media = std::move(memory_image); + else + Log_WarningPrintf("Failed to preload image '%s' to RAM", path); + } + + return media; +} + bool System::Boot(const SystemBootParameters& params) { if (params.state_stream) @@ -157,21 +176,13 @@ bool System::Boot(const SystemBootParameters& params) else { Log_InfoPrintf("Loading CD image '%s'...", params.filename.c_str()); - media = CDImage::Open(params.filename.c_str()); + media = OpenCDImage(params.filename.c_str(), params.override_load_image_to_ram.value_or(false)); if (!media) { m_host_interface->ReportFormattedError("Failed to load CD image '%s'", params.filename.c_str()); return false; } - if (params.override_load_image_to_ram.value_or(GetSettings().cdrom_load_image_to_ram)) - { - HostInterfaceProgressCallback callback(m_host_interface); - std::unique_ptr memory_image = CDImage::CreateMemoryImage(media.get(), &callback); - if (memory_image) - media = std::move(memory_image); - } - if (m_region == ConsoleRegion::Auto) { const DiscRegion disc_region = GameList::GetRegionForImage(media.get()); @@ -444,8 +455,7 @@ bool System::DoLoadState(ByteStream* state, bool init_components, bool force_sof media = m_cdrom->RemoveMedia(); if (!media || media->GetFileName() != media_filename) { - media.reset(); - media = CDImage::Open(media_filename.c_str()); + media = OpenCDImage(media_filename.c_str(), false); if (!media) { m_host_interface->ReportFormattedError("Failed to open CD image from save state: '%s'.", @@ -955,7 +965,7 @@ bool System::HasMedia() const bool System::InsertMedia(const char* path) { - std::unique_ptr image = CDImage::Open(path); + std::unique_ptr image = OpenCDImage(path, false); if (!image) return false; diff --git a/src/core/system.h b/src/core/system.h index 8ae6f71e1..895c8c0b1 100644 --- a/src/core/system.h +++ b/src/core/system.h @@ -155,6 +155,9 @@ public: private: System(HostInterface* host_interface); + /// Opens CD image, preloading if needed. + std::unique_ptr OpenCDImage(const char* path, bool force_preload); + bool DoLoadState(ByteStream* stream, bool init_components, bool force_software_renderer); bool DoState(StateWrapper& sw); bool CreateGPU(GPURenderer renderer);