From a449ef4e1159651d04bae48d693f1073303b43af Mon Sep 17 00:00:00 2001 From: BhaaL Date: Sun, 20 Mar 2016 13:09:21 +0100 Subject: [PATCH] properly open/close the file to avoid rapid open/close cycles ES_OPENCONTENT and ES_CLOSECONTENT now call Open and Close respectively, as the old code did. --- .../Core/IPC_HLE/WII_IPC_HLE_Device_es.cpp | 10 +++++- Source/Core/DiscIO/NANDContentLoader.cpp | 33 ++++++++++++++----- Source/Core/DiscIO/NANDContentLoader.h | 7 ++++ 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/Source/Core/Core/IPC_HLE/WII_IPC_HLE_Device_es.cpp b/Source/Core/Core/IPC_HLE/WII_IPC_HLE_Device_es.cpp index 87a8c41351..408b7545d0 100644 --- a/Source/Core/Core/IPC_HLE/WII_IPC_HLE_Device_es.cpp +++ b/Source/Core/Core/IPC_HLE/WII_IPC_HLE_Device_es.cpp @@ -220,7 +220,7 @@ u32 CWII_IPC_HLE_Device_es::OpenTitleContent(u32 CFD, u64 TitleID, u16 Index) Access.m_Size = pContent->m_Size; Access.m_TitleID = TitleID; - + pContent->m_Data->Open(); m_ContentAccessMap[CFD] = Access; return CFD; @@ -425,6 +425,14 @@ IPCCommandResult CWII_IPC_HLE_Device_es::IOCtlV(u32 _CommandAddress) return GetDefaultReply(); } + const DiscIO::CNANDContentLoader& ContentLoader = AccessContentDevice(itr->second.m_TitleID); + // ContentLoader should never be invalid; we shouldn't be here if ES_OPENCONTENT failed before. + if (ContentLoader.IsValid()) + { + const DiscIO::SNANDContent* pContent = ContentLoader.GetContentByIndex(itr->second.m_Index); + pContent->m_Data->Close(); + } + m_ContentAccessMap.erase(itr); Memory::Write_U32(0, _CommandAddress + 0x4); diff --git a/Source/Core/DiscIO/NANDContentLoader.cpp b/Source/Core/DiscIO/NANDContentLoader.cpp index 6a9fc29bdf..b7f5deb9c1 100644 --- a/Source/Core/DiscIO/NANDContentLoader.cpp +++ b/Source/Core/DiscIO/NANDContentLoader.cpp @@ -91,34 +91,49 @@ std::string CSharedContent::AddSharedContent(const u8* hash) return filename; } - +void CNANDContentDataFile::EnsureOpen() +{ + if (!m_file) + m_file = std::make_unique(m_filename, "rb"); + else if (!m_file->IsOpen()) + m_file->Open(m_filename, "rb"); +} +void CNANDContentDataFile::Open() +{ + EnsureOpen(); +} const std::vector CNANDContentDataFile::Get() { std::vector result; - File::IOFile file(m_filename, "rb"); - if (!file.IsGood()) + EnsureOpen(); + if (!m_file->IsGood()) return result; - u64 size = file.GetSize(); + u64 size = m_file->GetSize(); if (size == 0) return result; result.resize(size); - file.ReadBytes(result.data(), result.size()); + m_file->ReadBytes(result.data(), result.size()); return result; } bool CNANDContentDataFile::GetRange(u32 start, u32 size, u8* buffer) { - File::IOFile file(m_filename, "rb"); - if (!file.IsGood()) + EnsureOpen(); + if (!m_file->IsGood()) return false; - if (!file.Seek(start, SEEK_SET)) + if (!m_file->Seek(start, SEEK_SET)) return false; - return file.ReadBytes(buffer, static_cast(size)); + return m_file->ReadBytes(buffer, static_cast(size)); +} +void CNANDContentDataFile::Close() +{ + if (m_file && m_file->IsOpen()) + m_file->Close(); } diff --git a/Source/Core/DiscIO/NANDContentLoader.h b/Source/Core/DiscIO/NANDContentLoader.h index c82d46db1f..85b6de5117 100644 --- a/Source/Core/DiscIO/NANDContentLoader.h +++ b/Source/Core/DiscIO/NANDContentLoader.h @@ -20,8 +20,10 @@ bool AddTicket(u64 title_id, const std::vector& ticket); class CNANDContentData { public: + virtual void Open() { }; virtual const std::vector Get() = 0; virtual bool GetRange(u32 start, u32 size, u8* buffer) = 0; + virtual void Close() { }; }; class CNANDContentDataFile final : public CNANDContentData @@ -29,10 +31,15 @@ class CNANDContentDataFile final : public CNANDContentData public: CNANDContentDataFile(const std::string& filename) : m_filename(filename) { }; + void Open() override; const std::vector Get() override; bool GetRange(u32 start, u32 size, u8* buffer) override; + void Close() override; private: + void EnsureOpen(); + const std::string m_filename; + std::unique_ptr m_file; }; class CNANDContentDataBuffer final : public CNANDContentData {