From 09fc39e2e550c0a6cc303c9ab2aecdb021665b4c Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Wed, 22 Sep 2021 05:43:05 +0200 Subject: [PATCH] DiscIO: Add CreateDisc/WAD/Volume() overloads that take a BlobReader directly. --- Source/Core/DiscIO/Volume.cpp | 42 +++++++++++++++++++++++------------ Source/Core/DiscIO/Volume.h | 3 +++ 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/Source/Core/DiscIO/Volume.cpp b/Source/Core/DiscIO/Volume.cpp index c1b1400876..bf7ef4e019 100644 --- a/Source/Core/DiscIO/Volume.cpp +++ b/Source/Core/DiscIO/Volume.cpp @@ -85,8 +85,11 @@ std::map Volume::ReadWiiNames(const std::vector return names; } -static std::unique_ptr CreateDisc(std::unique_ptr& reader) +static std::unique_ptr TryCreateDisc(std::unique_ptr& reader) { + if (!reader) + return nullptr; + if (reader->ReadSwapped(0x18) == WII_DISC_MAGIC) return std::make_unique(std::move(reader)); @@ -97,14 +100,21 @@ static std::unique_ptr CreateDisc(std::unique_ptr& reade return nullptr; } -std::unique_ptr CreateDisc(const std::string& path) +std::unique_ptr CreateDisc(std::unique_ptr reader) { - std::unique_ptr reader(CreateBlobReader(path)); - return reader ? CreateDisc(reader) : nullptr; + return TryCreateDisc(reader); } -static std::unique_ptr CreateWAD(std::unique_ptr& reader) +std::unique_ptr CreateDisc(const std::string& path) { + return CreateDisc(CreateBlobReader(path)); +} + +static std::unique_ptr TryCreateWAD(std::unique_ptr& reader) +{ + if (!reader) + return nullptr; + // Check for WAD // 0x206962 for boot2 wads const std::optional wad_magic = reader->ReadSwapped(0x02); @@ -115,27 +125,31 @@ static std::unique_ptr CreateWAD(std::unique_ptr& reader) return nullptr; } -std::unique_ptr CreateWAD(const std::string& path) +std::unique_ptr CreateWAD(std::unique_ptr reader) { - std::unique_ptr reader(CreateBlobReader(path)); - return reader ? CreateWAD(reader) : nullptr; + return TryCreateWAD(reader); } -std::unique_ptr CreateVolume(const std::string& path) +std::unique_ptr CreateWAD(const std::string& path) { - std::unique_ptr reader(CreateBlobReader(path)); - if (reader == nullptr) - return nullptr; + return CreateWAD(CreateBlobReader(path)); +} - std::unique_ptr disc = CreateDisc(reader); +std::unique_ptr CreateVolume(std::unique_ptr reader) +{ + std::unique_ptr disc = TryCreateDisc(reader); if (disc) return disc; - std::unique_ptr wad = CreateWAD(reader); + std::unique_ptr wad = TryCreateWAD(reader); if (wad) return wad; return nullptr; } +std::unique_ptr CreateVolume(const std::string& path) +{ + return CreateVolume(CreateBlobReader(path)); +} } // namespace DiscIO diff --git a/Source/Core/DiscIO/Volume.h b/Source/Core/DiscIO/Volume.h index 9e378bf879..574afd6f83 100644 --- a/Source/Core/DiscIO/Volume.h +++ b/Source/Core/DiscIO/Volume.h @@ -182,8 +182,11 @@ protected: static const std::vector INVALID_CERT_CHAIN; }; +std::unique_ptr CreateDisc(std::unique_ptr reader); std::unique_ptr CreateDisc(const std::string& path); +std::unique_ptr CreateWAD(std::unique_ptr reader); std::unique_ptr CreateWAD(const std::string& path); +std::unique_ptr CreateVolume(std::unique_ptr reader); std::unique_ptr CreateVolume(const std::string& path); } // namespace DiscIO