diff options
author | bunnei <bunneidev@gmail.com> | 2018-10-04 15:42:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-04 15:42:37 +0200 |
commit | f85f2b372807b9785bfe30b2b1e2f342d58bddf6 (patch) | |
tree | 90fbcbdc532c106407503531e38c736471272e1e /src/core/file_sys | |
parent | Merge pull request #1434 from DarkLordZach/dlc-edge-case (diff) | |
parent | nso: Optimize loading of IPS patches (diff) | |
download | yuzu-f85f2b372807b9785bfe30b2b1e2f342d58bddf6.tar yuzu-f85f2b372807b9785bfe30b2b1e2f342d58bddf6.tar.gz yuzu-f85f2b372807b9785bfe30b2b1e2f342d58bddf6.tar.bz2 yuzu-f85f2b372807b9785bfe30b2b1e2f342d58bddf6.tar.lz yuzu-f85f2b372807b9785bfe30b2b1e2f342d58bddf6.tar.xz yuzu-f85f2b372807b9785bfe30b2b1e2f342d58bddf6.tar.zst yuzu-f85f2b372807b9785bfe30b2b1e2f342d58bddf6.zip |
Diffstat (limited to 'src/core/file_sys')
-rw-r--r-- | src/core/file_sys/ips_layer.cpp | 88 | ||||
-rw-r--r-- | src/core/file_sys/ips_layer.h | 15 | ||||
-rw-r--r-- | src/core/file_sys/patch_manager.cpp | 141 | ||||
-rw-r--r-- | src/core/file_sys/patch_manager.h | 20 |
4 files changed, 233 insertions, 31 deletions
diff --git a/src/core/file_sys/ips_layer.cpp b/src/core/file_sys/ips_layer.cpp new file mode 100644 index 000000000..df933ee36 --- /dev/null +++ b/src/core/file_sys/ips_layer.cpp @@ -0,0 +1,88 @@ +// Copyright 2018 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "common/assert.h" +#include "common/swap.h" +#include "core/file_sys/ips_layer.h" +#include "core/file_sys/vfs_vector.h" + +namespace FileSys { + +enum class IPSFileType { + IPS, + IPS32, + Error, +}; + +static IPSFileType IdentifyMagic(const std::vector<u8>& magic) { + if (magic.size() != 5) + return IPSFileType::Error; + if (magic == std::vector<u8>{'P', 'A', 'T', 'C', 'H'}) + return IPSFileType::IPS; + if (magic == std::vector<u8>{'I', 'P', 'S', '3', '2'}) + return IPSFileType::IPS32; + return IPSFileType::Error; +} + +VirtualFile PatchIPS(const VirtualFile& in, const VirtualFile& ips) { + if (in == nullptr || ips == nullptr) + return nullptr; + + const auto type = IdentifyMagic(ips->ReadBytes(0x5)); + if (type == IPSFileType::Error) + return nullptr; + + auto in_data = in->ReadAllBytes(); + + std::vector<u8> temp(type == IPSFileType::IPS ? 3 : 4); + u64 offset = 5; // After header + while (ips->Read(temp.data(), temp.size(), offset) == temp.size()) { + offset += temp.size(); + if (type == IPSFileType::IPS32 && temp == std::vector<u8>{'E', 'E', 'O', 'F'} || + type == IPSFileType::IPS && temp == std::vector<u8>{'E', 'O', 'F'}) { + break; + } + + u32 real_offset{}; + if (type == IPSFileType::IPS32) + real_offset = (temp[0] << 24) | (temp[1] << 16) | (temp[2] << 8) | temp[3]; + else + real_offset = (temp[0] << 16) | (temp[1] << 8) | temp[2]; + + u16 data_size{}; + if (ips->ReadObject(&data_size, offset) != sizeof(u16)) + return nullptr; + data_size = Common::swap16(data_size); + offset += sizeof(u16); + + if (data_size == 0) { // RLE + u16 rle_size{}; + if (ips->ReadObject(&rle_size, offset) != sizeof(u16)) + return nullptr; + rle_size = Common::swap16(data_size); + offset += sizeof(u16); + + const auto data = ips->ReadByte(offset++); + if (data == boost::none) + return nullptr; + + if (real_offset + rle_size > in_data.size()) + rle_size = in_data.size() - real_offset; + std::memset(in_data.data() + real_offset, data.get(), rle_size); + } else { // Standard Patch + auto read = data_size; + if (real_offset + read > in_data.size()) + read = in_data.size() - real_offset; + if (ips->Read(in_data.data() + real_offset, read, offset) != data_size) + return nullptr; + offset += data_size; + } + } + + if (temp != std::vector<u8>{'E', 'E', 'O', 'F'} && temp != std::vector<u8>{'E', 'O', 'F'}) + return nullptr; + return std::make_shared<VectorVfsFile>(in_data, in->GetName(), in->GetContainingDirectory()); +} + +} // namespace FileSys diff --git a/src/core/file_sys/ips_layer.h b/src/core/file_sys/ips_layer.h new file mode 100644 index 000000000..81c163494 --- /dev/null +++ b/src/core/file_sys/ips_layer.h @@ -0,0 +1,15 @@ +// Copyright 2018 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include <memory> + +#include "core/file_sys/vfs.h" + +namespace FileSys { + +VirtualFile PatchIPS(const VirtualFile& in, const VirtualFile& ips); + +} // namespace FileSys diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cpp index 57b7741f8..539698f6e 100644 --- a/src/core/file_sys/patch_manager.cpp +++ b/src/core/file_sys/patch_manager.cpp @@ -5,14 +5,18 @@ #include <algorithm> #include <array> #include <cstddef> +#include <cstring> +#include "common/hex_util.h" #include "common/logging/log.h" #include "core/file_sys/content_archive.h" #include "core/file_sys/control_metadata.h" +#include "core/file_sys/ips_layer.h" #include "core/file_sys/patch_manager.h" #include "core/file_sys/registered_cache.h" #include "core/file_sys/romfs.h" #include "core/file_sys/vfs_layered.h" +#include "core/file_sys/vfs_vector.h" #include "core/hle/service/filesystem/filesystem.h" #include "core/loader/loader.h" @@ -21,6 +25,14 @@ namespace FileSys { constexpr u64 SINGLE_BYTE_MODULUS = 0x100; constexpr u64 DLC_BASE_TITLE_ID_MASK = 0xFFFFFFFFFFFFE000; +struct NSOBuildHeader { + u32_le magic; + INSERT_PADDING_BYTES(0x3C); + std::array<u8, 0x20> build_id; + INSERT_PADDING_BYTES(0xA0); +}; +static_assert(sizeof(NSOBuildHeader) == 0x100, "NSOBuildHeader has incorrect size."); + std::string FormatTitleVersion(u32 version, TitleVersionFormat format) { std::array<u8, sizeof(u32)> bytes{}; bytes[0] = version % SINGLE_BYTE_MODULUS; @@ -34,16 +46,6 @@ std::string FormatTitleVersion(u32 version, TitleVersionFormat format) { return fmt::format("v{}.{}.{}", bytes[3], bytes[2], bytes[1]); } -constexpr std::array<const char*, 3> PATCH_TYPE_NAMES{ - "Update", - "LayeredFS", - "DLC", -}; - -std::string FormatPatchTypeName(PatchType type) { - return PATCH_TYPE_NAMES.at(static_cast<std::size_t>(type)); -} - PatchManager::PatchManager(u64 title_id) : title_id(title_id) {} PatchManager::~PatchManager() = default; @@ -71,6 +73,79 @@ VirtualDir PatchManager::PatchExeFS(VirtualDir exefs) const { return exefs; } +static std::vector<VirtualFile> CollectIPSPatches(const std::vector<VirtualDir>& patch_dirs, + const std::string& build_id) { + std::vector<VirtualFile> ips; + ips.reserve(patch_dirs.size()); + for (const auto& subdir : patch_dirs) { + auto exefs_dir = subdir->GetSubdirectory("exefs"); + if (exefs_dir != nullptr) { + for (const auto& file : exefs_dir->GetFiles()) { + if (file->GetExtension() != "ips") + continue; + auto name = file->GetName(); + const auto p1 = name.substr(0, name.find('.')); + const auto this_build_id = p1.substr(0, p1.find_last_not_of('0') + 1); + + if (build_id == this_build_id) + ips.push_back(file); + } + } + } + + return ips; +} + +std::vector<u8> PatchManager::PatchNSO(const std::vector<u8>& nso) const { + if (nso.size() < 0x100) + return nso; + + NSOBuildHeader header; + std::memcpy(&header, nso.data(), sizeof(NSOBuildHeader)); + + if (header.magic != Common::MakeMagic('N', 'S', 'O', '0')) + return nso; + + const auto build_id_raw = Common::HexArrayToString(header.build_id); + const auto build_id = build_id_raw.substr(0, build_id_raw.find_last_not_of('0') + 1); + + LOG_INFO(Loader, "Patching NSO for build_id={}", build_id); + + const auto load_dir = Service::FileSystem::GetModificationLoadRoot(title_id); + auto patch_dirs = load_dir->GetSubdirectories(); + std::sort(patch_dirs.begin(), patch_dirs.end(), + [](const VirtualDir& l, const VirtualDir& r) { return l->GetName() < r->GetName(); }); + const auto ips = CollectIPSPatches(patch_dirs, build_id); + + auto out = nso; + for (const auto& ips_file : ips) { + LOG_INFO(Loader, " - Appling IPS patch from mod \"{}\"", + ips_file->GetContainingDirectory()->GetParentDirectory()->GetName()); + const auto patched = PatchIPS(std::make_shared<VectorVfsFile>(out), ips_file); + if (patched != nullptr) + out = patched->ReadAllBytes(); + } + + if (out.size() < 0x100) + return nso; + std::memcpy(out.data(), &header, sizeof(NSOBuildHeader)); + return out; +} + +bool PatchManager::HasNSOPatch(const std::array<u8, 32>& build_id_) const { + const auto build_id_raw = Common::HexArrayToString(build_id_); + const auto build_id = build_id_raw.substr(0, build_id_raw.find_last_not_of('0') + 1); + + LOG_INFO(Loader, "Querying NSO patch existence for build_id={}", build_id); + + const auto load_dir = Service::FileSystem::GetModificationLoadRoot(title_id); + auto patch_dirs = load_dir->GetSubdirectories(); + std::sort(patch_dirs.begin(), patch_dirs.end(), + [](const VirtualDir& l, const VirtualDir& r) { return l->GetName() < r->GetName(); }); + + return !CollectIPSPatches(patch_dirs, build_id).empty(); +} + static void ApplyLayeredFS(VirtualFile& romfs, u64 title_id, ContentRecordType type) { const auto load_dir = Service::FileSystem::GetModificationLoadRoot(title_id); if (type != ContentRecordType::Program || load_dir == nullptr || load_dir->GetSize() <= 0) { @@ -138,8 +213,19 @@ VirtualFile PatchManager::PatchRomFS(VirtualFile romfs, u64 ivfc_offset, return romfs; } -std::map<PatchType, std::string> PatchManager::GetPatchVersionNames() const { - std::map<PatchType, std::string> out; +static void AppendCommaIfNotEmpty(std::string& to, const std::string& with) { + if (to.empty()) + to += with; + else + to += ", " + with; +} + +static bool IsDirValidAndNonEmpty(const VirtualDir& dir) { + return dir != nullptr && (!dir->GetFiles().empty() || !dir->GetSubdirectories().empty()); +} + +std::map<std::string, std::string, std::less<>> PatchManager::GetPatchVersionNames() const { + std::map<std::string, std::string, std::less<>> out; const auto installed = Service::FileSystem::GetUnionContents(); // Game Updates @@ -148,23 +234,36 @@ std::map<PatchType, std::string> PatchManager::GetPatchVersionNames() const { auto [nacp, discard_icon_file] = update.GetControlMetadata(); if (nacp != nullptr) { - out[PatchType::Update] = nacp->GetVersionString(); + out.insert_or_assign("Update", nacp->GetVersionString()); } else { if (installed->HasEntry(update_tid, ContentRecordType::Program)) { const auto meta_ver = installed->GetEntryVersion(update_tid); if (meta_ver == boost::none || meta_ver.get() == 0) { - out[PatchType::Update] = ""; + out.insert_or_assign("Update", ""); } else { - out[PatchType::Update] = - FormatTitleVersion(meta_ver.get(), TitleVersionFormat::ThreeElements); + out.insert_or_assign( + "Update", + FormatTitleVersion(meta_ver.get(), TitleVersionFormat::ThreeElements)); } } } - // LayeredFS - const auto lfs_dir = Service::FileSystem::GetModificationLoadRoot(title_id); - if (lfs_dir != nullptr && lfs_dir->GetSize() > 0) - out.insert_or_assign(PatchType::LayeredFS, ""); + // General Mods (LayeredFS and IPS) + const auto mod_dir = Service::FileSystem::GetModificationLoadRoot(title_id); + if (mod_dir != nullptr && mod_dir->GetSize() > 0) { + for (const auto& mod : mod_dir->GetSubdirectories()) { + std::string types; + if (IsDirValidAndNonEmpty(mod->GetSubdirectory("exefs"))) + AppendCommaIfNotEmpty(types, "IPS"); + if (IsDirValidAndNonEmpty(mod->GetSubdirectory("romfs"))) + AppendCommaIfNotEmpty(types, "LayeredFS"); + + if (types.empty()) + continue; + + out.insert_or_assign(mod->GetName(), types); + } + } // DLC const auto dlc_entries = installed->ListEntriesFilter(TitleType::AOC, ContentRecordType::Data); @@ -186,7 +285,7 @@ std::map<PatchType, std::string> PatchManager::GetPatchVersionNames() const { list += fmt::format("{}", dlc_match.back().title_id & 0x7FF); - out.insert_or_assign(PatchType::DLC, std::move(list)); + out.insert_or_assign("DLC", std::move(list)); } return out; diff --git a/src/core/file_sys/patch_manager.h b/src/core/file_sys/patch_manager.h index 3a2a9d212..6a864ec43 100644 --- a/src/core/file_sys/patch_manager.h +++ b/src/core/file_sys/patch_manager.h @@ -24,14 +24,6 @@ enum class TitleVersionFormat : u8 { std::string FormatTitleVersion(u32 version, TitleVersionFormat format = TitleVersionFormat::ThreeElements); -enum class PatchType { - Update, - LayeredFS, - DLC, -}; - -std::string FormatPatchTypeName(PatchType type); - // A centralized class to manage patches to games. class PatchManager { public: @@ -42,6 +34,14 @@ public: // - Game Updates VirtualDir PatchExeFS(VirtualDir exefs) const; + // Currently tracked NSO patches: + // - IPS + std::vector<u8> PatchNSO(const std::vector<u8>& nso) const; + + // Checks to see if PatchNSO() will have any effect given the NSO's build ID. + // Used to prevent expensive copies in NSO loader. + bool HasNSOPatch(const std::array<u8, 0x20>& build_id) const; + // Currently tracked RomFS patches: // - Game Updates // - LayeredFS @@ -49,8 +49,8 @@ public: ContentRecordType type = ContentRecordType::Program) const; // Returns a vector of pairs between patch names and patch versions. - // i.e. Update v80 will return {Update, 80} - std::map<PatchType, std::string> GetPatchVersionNames() const; + // i.e. Update 3.2.2 will return {"Update", "3.2.2"} + std::map<std::string, std::string, std::less<>> GetPatchVersionNames() const; // Given title_id of the program, attempts to get the control data of the update and parse it, // falling back to the base control data. |