| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DiscIO/VolumeFileBlobReader.h" |
| 5 | |
| 6 | #include <memory> |
| 7 | #include <string_view> |
| 8 | |
| 9 | #include "DiscIO/Filesystem.h" |
| 10 | #include "DiscIO/Volume.h" |
| 11 | |
| 12 | namespace DiscIO |
| 13 | { |
| 14 | std::unique_ptr<VolumeFileBlobReader> VolumeFileBlobReader::Create(const Volume& volume, |
| 15 | const Partition& partition, |
| 16 | std::string_view file_path) |
| 17 | { |
| 18 | const FileSystem* file_system = volume.GetFileSystem(partition); |
| 19 | if (!file_system) |
| 20 | return nullptr; |
| 21 | |
| 22 | std::unique_ptr<FileInfo> file_info = file_system->FindFileInfo(file_path); |
| 23 | if (!file_info || file_info->IsDirectory()) |
| 24 | return nullptr; |
| 25 | |
| 26 | return std::unique_ptr<VolumeFileBlobReader>{ |
| 27 | new VolumeFileBlobReader(volume, partition, std::move(file_info))}; |
| 28 | } |
| 29 | |
| 30 | VolumeFileBlobReader::VolumeFileBlobReader(const Volume& volume, const Partition& partition, |
| 31 | std::unique_ptr<FileInfo> file_info) |
| 32 | : m_volume(volume), m_partition(partition), m_file_info(std::move(file_info)) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | // This is defined here instead of the header so that the definition of FileInfo is visible when |
| 37 | // m_file_info is destroyed, preventing a compile error caused by calling unique_ptr's destructor |
| 38 | // with an incomplete type. |
| 39 | VolumeFileBlobReader::~VolumeFileBlobReader() = default; |
| 40 | |
| 41 | std::unique_ptr<BlobReader> VolumeFileBlobReader::CopyReader() const |
| 42 | { |
| 43 | ASSERT_MSG(DISCIO, false, "Unimplemented"); |
| 44 | return nullptr; |
| 45 | } |
| 46 | |
| 47 | u64 VolumeFileBlobReader::GetDataSize() const |
| 48 | { |
| 49 | return m_file_info->GetSize(); |
| 50 | } |
| 51 | |
| 52 | u64 VolumeFileBlobReader::GetRawSize() const |
| 53 | { |
| 54 | return GetDataSize(); |
| 55 | } |
| 56 | |
| 57 | u64 VolumeFileBlobReader::GetBlockSize() const |
| 58 | { |
| 59 | return m_volume.GetBlobReader().GetBlockSize(); |
| 60 | } |
| 61 | |
| 62 | bool VolumeFileBlobReader::HasFastRandomAccessInBlock() const |
| 63 | { |
| 64 | return m_volume.GetBlobReader().HasFastRandomAccessInBlock(); |
| 65 | } |
| 66 | |
| 67 | std::string VolumeFileBlobReader::GetCompressionMethod() const |
| 68 | { |
| 69 | return m_volume.GetBlobReader().GetCompressionMethod(); |
| 70 | } |
| 71 | |
| 72 | std::optional<int> VolumeFileBlobReader::GetCompressionLevel() const |
| 73 | { |
| 74 | return m_volume.GetBlobReader().GetCompressionLevel(); |
| 75 | } |
| 76 | |
| 77 | bool VolumeFileBlobReader::Read(u64 offset, u64 length, u8* out_ptr) |
| 78 | { |
| 79 | if (offset + length > m_file_info->GetSize()) |
| 80 | return false; |
| 81 | |
| 82 | return m_volume.Read(m_file_info->GetOffset() + offset, length, out_ptr, m_partition); |
| 83 | } |
| 84 | } // namespace DiscIO |