summaryrefslogtreecommitdiff
path: root/Source/Core/DiscIO/VolumeFileBlobReader.cpp
blob: 92eb9b4ae291277e5fae09cc2991ca3e53ed6630 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2017 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "DiscIO/VolumeFileBlobReader.h"

#include <memory>
#include <string_view>

#include "DiscIO/Filesystem.h"
#include "DiscIO/Volume.h"

namespace DiscIO
{
std::unique_ptr<VolumeFileBlobReader> VolumeFileBlobReader::Create(const Volume& volume,
                                                                   const Partition& partition,
                                                                   std::string_view file_path)
{
  const FileSystem* file_system = volume.GetFileSystem(partition);
  if (!file_system)
    return nullptr;

  std::unique_ptr<FileInfo> file_info = file_system->FindFileInfo(file_path);
  if (!file_info || file_info->IsDirectory())
    return nullptr;

  return std::unique_ptr<VolumeFileBlobReader>{
      new VolumeFileBlobReader(volume, partition, std::move(file_info))};
}

VolumeFileBlobReader::VolumeFileBlobReader(const Volume& volume, const Partition& partition,
                                           std::unique_ptr<FileInfo> file_info)
    : m_volume(volume), m_partition(partition), m_file_info(std::move(file_info))
{
}

// This is defined here instead of the header so that the definition of FileInfo is visible when
// m_file_info is destroyed, preventing a compile error caused by calling unique_ptr's destructor
// with an incomplete type.
VolumeFileBlobReader::~VolumeFileBlobReader() = default;

std::unique_ptr<BlobReader> VolumeFileBlobReader::CopyReader() const
{
  ASSERT_MSG(DISCIO, false, "Unimplemented");
  return nullptr;
}

u64 VolumeFileBlobReader::GetDataSize() const
{
  return m_file_info->GetSize();
}

u64 VolumeFileBlobReader::GetRawSize() const
{
  return GetDataSize();
}

u64 VolumeFileBlobReader::GetBlockSize() const
{
  return m_volume.GetBlobReader().GetBlockSize();
}

bool VolumeFileBlobReader::HasFastRandomAccessInBlock() const
{
  return m_volume.GetBlobReader().HasFastRandomAccessInBlock();
}

std::string VolumeFileBlobReader::GetCompressionMethod() const
{
  return m_volume.GetBlobReader().GetCompressionMethod();
}

std::optional<int> VolumeFileBlobReader::GetCompressionLevel() const
{
  return m_volume.GetBlobReader().GetCompressionLevel();
}

bool VolumeFileBlobReader::Read(u64 offset, u64 length, u8* out_ptr)
{
  if (offset + length > m_file_info->GetSize())
    return false;

  return m_volume.Read(m_file_info->GetOffset() + offset, length, out_ptr, m_partition);
}
}  // namespace DiscIO