diff --git a/Source/Core/Core/HW/DVDInterface.cpp b/Source/Core/Core/HW/DVDInterface.cpp index ad85343a82..16cb23c6bb 100644 --- a/Source/Core/Core/HW/DVDInterface.cpp +++ b/Source/Core/Core/HW/DVDInterface.cpp @@ -460,48 +460,40 @@ static void EjectDiscCallback(u64 userdata, s64 cyclesLate) static void InsertDiscCallback(u64 userdata, s64 cyclesLate) { - std::string& SavedFileName = SConfig::GetInstance().m_strFilename; - std::string* _FileName = (std::string*)userdata; + const std::string& old_path = SConfig::GetInstance().m_strFilename; + std::string* new_path = reinterpret_cast(userdata); - if (!SetVolumeName(*_FileName)) + if (!SetVolumeName(*new_path)) { // Put back the old one - SetVolumeName(SavedFileName); + SetVolumeName(old_path); PanicAlertT("Invalid file"); } SetDiscInside(VolumeIsValid()); - delete _FileName; + delete new_path; } // Can only be called by the host thread -void ChangeDiscAsHost(const std::string& newFileName) +void ChangeDiscAsHost(const std::string& new_path) { bool was_unpaused = Core::PauseAndLock(true); // The host thread is now temporarily the CPU thread - ChangeDiscAsCPU(newFileName); + ChangeDiscAsCPU(new_path); Core::PauseAndLock(false, was_unpaused); } // Can only be called by the CPU thread -void ChangeDiscAsCPU(const std::string& newFileName) +void ChangeDiscAsCPU(const std::string& new_path) { - std::string* _FileName = new std::string(newFileName); + // TODO: This is bad. Pointers in CoreTiming userdata require + // manual memory management and aren't savestate-safe. + u64 new_path_pointer = reinterpret_cast(new std::string(new_path)); CoreTiming::ScheduleEvent(0, s_eject_disc); - CoreTiming::ScheduleEvent(SystemTimers::GetTicksPerSecond(), s_insert_disc, (u64)_FileName); - if (Movie::IsRecordingInput()) - { - std::string fileName = newFileName; - auto sizeofpath = fileName.find_last_of("/\\") + 1; - if (fileName.substr(sizeofpath).length() > 40) - { - PanicAlertT("The disc change to \"%s\" could not be saved in the .dtm file.\n" - "The filename of the disc image must not be longer than 40 characters.", - newFileName.c_str()); - } - Movie::SignalDiscChange(fileName.substr(sizeofpath)); - } + CoreTiming::ScheduleEvent(SystemTimers::GetTicksPerSecond(), s_insert_disc, new_path_pointer); + + Movie::SignalDiscChange(new_path); } void SetLidOpen(bool open) diff --git a/Source/Core/Core/HW/DVDInterface.h b/Source/Core/Core/HW/DVDInterface.h index 640afe1d84..e67e30ea9a 100644 --- a/Source/Core/Core/HW/DVDInterface.h +++ b/Source/Core/Core/HW/DVDInterface.h @@ -108,8 +108,8 @@ bool VolumeIsValid(); // Disc detection and swapping void SetDiscInside(bool _DiscInside); bool IsDiscInside(); -void ChangeDiscAsHost(const std::string& path); // Can only be called by the host thread -void ChangeDiscAsCPU(const std::string& path); // Can only be called by the CPU thread +void ChangeDiscAsHost(const std::string& new_path); // Can only be called by the host thread +void ChangeDiscAsCPU(const std::string& new_path); // Can only be called by the CPU thread // DVD Access Functions bool ChangePartition(u64 offset); diff --git a/Source/Core/Core/Movie.cpp b/Source/Core/Core/Movie.cpp index c32ead6fa6..72642f8a51 100644 --- a/Source/Core/Core/Movie.cpp +++ b/Source/Core/Core/Movie.cpp @@ -415,10 +415,22 @@ void SetClearSave(bool enabled) s_bClearSave = enabled; } -void SignalDiscChange(const std::string& new_disc_filename) +void SignalDiscChange(const std::string& new_path) { - s_discChange = new_disc_filename; - s_bDiscChange = true; + if (Movie::IsRecordingInput()) + { + size_t size_of_path_without_filename = new_path.find_last_of("/\\") + 1; + std::string filename = new_path.substr(size_of_path_without_filename); + constexpr size_t maximum_length = sizeof(DTMHeader::discChange); + if (filename.length() > maximum_length) + { + PanicAlertT("The disc change to \"%s\" could not be saved in the .dtm file.\n" + "The filename of the disc image must not be longer than 40 characters.", + filename.c_str()); + } + s_discChange = filename; + s_bDiscChange = true; + } } void SetReset(bool reset) diff --git a/Source/Core/Core/Movie.h b/Source/Core/Core/Movie.h index 8bb31461e3..f21745464e 100644 --- a/Source/Core/Core/Movie.h +++ b/Source/Core/Core/Movie.h @@ -129,7 +129,7 @@ u64 GetCurrentLagCount(); u64 GetTotalLagCount(); void SetClearSave(bool enabled); -void SignalDiscChange(const std::string& new_disc_filename); +void SignalDiscChange(const std::string& new_path); void SetReset(bool reset); void SetTitleId(u64 title_id);