DirectoryBlob: Let DiscContent be backed by memory instead of file

This commit is contained in:
JosJuice 2017-06-08 16:37:51 +02:00
parent e1321b131d
commit 936ef5b9dd
2 changed files with 30 additions and 5 deletions

View File

@ -14,6 +14,7 @@
#include <set>
#include <string>
#include <utility>
#include <variant>
#include <vector>
#include "Common/Align.h"
@ -50,7 +51,12 @@ const std::array<u32, 10> PARTITION_TABLE = {
Common::swap32(GAME_PARTITION_ADDRESS >> 2), 0}};
DiscContent::DiscContent(u64 offset, u64 size, const std::string& path)
: m_offset(offset), m_size(size), m_path(path)
: m_offset(offset), m_size(size), m_content_source(path)
{
}
DiscContent::DiscContent(u64 offset, u64 size, const u8* data)
: m_offset(offset), m_size(size), m_content_source(data)
{
}
@ -80,10 +86,18 @@ bool DiscContent::Read(u64* offset, u64* length, u8** buffer) const
{
const u64 bytes_to_read = std::min(m_size - offset_in_content, *length);
File::IOFile file(m_path, "rb");
file.Seek(offset_in_content, SEEK_SET);
if (!file.ReadBytes(*buffer, bytes_to_read))
return false;
if (std::holds_alternative<std::string>(m_content_source))
{
File::IOFile file(std::get<std::string>(m_content_source), "rb");
file.Seek(offset_in_content, SEEK_SET);
if (!file.ReadBytes(*buffer, bytes_to_read))
return false;
}
else
{
const u8* const content_pointer = std::get<const u8*>(m_content_source) + offset_in_content;
std::copy(content_pointer, content_pointer + bytes_to_read, *buffer);
}
*length -= bytes_to_read;
*buffer += bytes_to_read;

View File

@ -9,6 +9,7 @@
#include <optional>
#include <set>
#include <string>
#include <variant>
#include <vector>
#include "Common/CommonTypes.h"
@ -26,7 +27,10 @@ namespace DiscIO
class DiscContent
{
public:
using ContentSource = std::variant<std::string, const u8*>;
DiscContent(u64 offset, u64 size, const std::string& path);
DiscContent(u64 offset, u64 size, const u8* data);
// Provided because it's convenient when searching for DiscContent in an std::set
explicit DiscContent(u64 offset);
@ -45,6 +49,7 @@ private:
u64 m_offset;
u64 m_size = 0;
std::string m_path;
ContentSource m_content_source;
};
class DirectoryBlobReader : public BlobReader
@ -53,6 +58,12 @@ public:
static bool IsValidDirectoryBlob(const std::string& dol_path);
static std::unique_ptr<DirectoryBlobReader> Create(File::IOFile dol, const std::string& dol_path);
// We do not allow copying, because it might mess up the pointers inside DiscContents
DirectoryBlobReader(const DirectoryBlobReader&) = delete;
DirectoryBlobReader& operator=(const DirectoryBlobReader&) = delete;
DirectoryBlobReader(DirectoryBlobReader&&) = default;
DirectoryBlobReader& operator=(DirectoryBlobReader&&) = default;
bool Read(u64 offset, u64 length, u8* buffer) override;
bool SupportsReadWiiDecrypted() const override;
bool ReadWiiDecrypted(u64 offset, u64 size, u8* buffer, u64 partition_offset) override;