From 3c9e70fefae0754c7484484fc8f58011e5b14aaa Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sat, 29 Sep 2018 22:12:53 -0400 Subject: [PATCH 1/7] nso: Replace NSOHeader padding bytes with build ID --- src/core/loader/nso.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp index cbe2a3e53..512954d40 100644 --- a/src/core/loader/nso.cpp +++ b/src/core/loader/nso.cpp @@ -36,8 +36,7 @@ struct NsoHeader { INSERT_PADDING_WORDS(1); u8 flags; std::array segments; // Text, RoData, Data (in that order) - u32_le bss_size; - INSERT_PADDING_BYTES(0x1c); + std::array build_id; std::array segments_compressed_size; bool IsSegmentCompressed(size_t segment_num) const { From 21b2411c44276c9955833e987d2098924c4dfc8a Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sat, 29 Sep 2018 22:13:15 -0400 Subject: [PATCH 2/7] file_sys: Implement function to apply IPS patches --- src/core/file_sys/ips_layer.cpp | 88 +++++++++++++++++++++++++++++++++ src/core/file_sys/ips_layer.h | 15 ++++++ 2 files changed, 103 insertions(+) create mode 100644 src/core/file_sys/ips_layer.cpp create mode 100644 src/core/file_sys/ips_layer.h 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& magic) { + if (magic.size() != 5) + return IPSFileType::Error; + if (magic == std::vector{'P', 'A', 'T', 'C', 'H'}) + return IPSFileType::IPS; + if (magic == std::vector{'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 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{'E', 'E', 'O', 'F'} || + type == IPSFileType::IPS && temp == std::vector{'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{'E', 'E', 'O', 'F'} && temp != std::vector{'E', 'O', 'F'}) + return nullptr; + return std::make_shared(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 + +#include "core/file_sys/vfs.h" + +namespace FileSys { + +VirtualFile PatchIPS(const VirtualFile& in, const VirtualFile& ips); + +} // namespace FileSys From 4c2a94fa94e0192402c816c74f5a09061e3df520 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sat, 29 Sep 2018 22:14:01 -0400 Subject: [PATCH 3/7] patch_manager: Use strings for patch type instead of enum --- src/core/file_sys/patch_manager.cpp | 50 ++++++++++++++++++----------- src/core/file_sys/patch_manager.h | 12 ++----- src/yuzu/game_list_worker.cpp | 7 ++-- 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cpp index 57b7741f8..10b4acc92 100644 --- a/src/core/file_sys/patch_manager.cpp +++ b/src/core/file_sys/patch_manager.cpp @@ -34,16 +34,6 @@ std::string FormatTitleVersion(u32 version, TitleVersionFormat format) { return fmt::format("v{}.{}.{}", bytes[3], bytes[2], bytes[1]); } -constexpr std::array PATCH_TYPE_NAMES{ - "Update", - "LayeredFS", - "DLC", -}; - -std::string FormatPatchTypeName(PatchType type) { - return PATCH_TYPE_NAMES.at(static_cast(type)); -} - PatchManager::PatchManager(u64 title_id) : title_id(title_id) {} PatchManager::~PatchManager() = default; @@ -138,8 +128,19 @@ VirtualFile PatchManager::PatchRomFS(VirtualFile romfs, u64 ivfc_offset, return romfs; } -std::map PatchManager::GetPatchVersionNames() const { - std::map out; +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 PatchManager::GetPatchVersionNames() const { + std::map out; const auto installed = Service::FileSystem::GetUnionContents(); // Game Updates @@ -148,23 +149,34 @@ std::map PatchManager::GetPatchVersionNames() const { auto [nacp, discard_icon_file] = update.GetControlMetadata(); if (nacp != nullptr) { - out[PatchType::Update] = nacp->GetVersionString(); + out["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["Update"] = ""; } else { - out[PatchType::Update] = + out["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, ""); + 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); diff --git a/src/core/file_sys/patch_manager.h b/src/core/file_sys/patch_manager.h index 3a2a9d212..7807515f9 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: @@ -49,8 +41,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 GetPatchVersionNames() const; + // i.e. Update 3.2.2 will return {"Update", "3.2.2"} + std::map 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. diff --git a/src/yuzu/game_list_worker.cpp b/src/yuzu/game_list_worker.cpp index e228d61bd..1947bdb93 100644 --- a/src/yuzu/game_list_worker.cpp +++ b/src/yuzu/game_list_worker.cpp @@ -60,14 +60,13 @@ QString FormatGameName(const std::string& physical_name) { QString FormatPatchNameVersions(const FileSys::PatchManager& patch_manager, bool updatable = true) { QString out; for (const auto& kv : patch_manager.GetPatchVersionNames()) { - if (!updatable && kv.first == FileSys::PatchType::Update) + if (!updatable && kv.first == "Update") continue; if (kv.second.empty()) { - out.append(fmt::format("{}\n", FileSys::FormatPatchTypeName(kv.first)).c_str()); + out.append(fmt::format("{}\n", kv.first).c_str()); } else { - out.append(fmt::format("{} ({})\n", FileSys::FormatPatchTypeName(kv.first), kv.second) - .c_str()); + out.append(fmt::format("{} ({})\n", kv.first, kv.second).c_str()); } } From 42fb4e82d3239f9d2fa4e5369ac58436c96b7466 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sat, 29 Sep 2018 22:15:16 -0400 Subject: [PATCH 4/7] patch_manager: Add PatchNSO function While PatchExeFS operated on the entire directory, this function operates on the uncompressed NSO. Avoids copying decompression code to PatchManager. --- src/core/CMakeLists.txt | 2 + src/core/file_sys/patch_manager.cpp | 94 +++++++++++++++++++++++++++++ src/core/file_sys/patch_manager.h | 8 +++ 3 files changed, 104 insertions(+) diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 23fd6e920..a98dbb9ab 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -34,6 +34,8 @@ add_library(core STATIC file_sys/errors.h file_sys/fsmitm_romfsbuild.cpp file_sys/fsmitm_romfsbuild.h + file_sys/ips_layer.cpp + file_sys/ips_layer.h file_sys/mode.h file_sys/nca_metadata.cpp file_sys/nca_metadata.h diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cpp index 10b4acc92..044e01ae9 100644 --- a/src/core/file_sys/patch_manager.cpp +++ b/src/core/file_sys/patch_manager.cpp @@ -6,13 +6,16 @@ #include #include +#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 +24,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 build_id; + INSERT_PADDING_BYTES(0xA0); +}; +static_assert(sizeof(NSOBuildHeader) == 0x100, "NSOBuildHeader has incorrect size."); + std::string FormatTitleVersion(u32 version, TitleVersionFormat format) { std::array bytes{}; bytes[0] = version % SINGLE_BYTE_MODULUS; @@ -61,6 +72,89 @@ VirtualDir PatchManager::PatchExeFS(VirtualDir exefs) const { return exefs; } +std::vector PatchManager::PatchNSO(const std::vector& 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(); }); + + std::vector ips; + ips.reserve(patch_dirs.size() - 1); + 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_first_of('.')); + 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); + } + } + } + + 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(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& 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(); }); + + 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_first_of('.')); + const auto this_build_id = p1.substr(0, p1.find_last_not_of('0') + 1); + + if (build_id == this_build_id) + return true; + } + } + } + + return false; +} + 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) { diff --git a/src/core/file_sys/patch_manager.h b/src/core/file_sys/patch_manager.h index 7807515f9..254f7bfc9 100644 --- a/src/core/file_sys/patch_manager.h +++ b/src/core/file_sys/patch_manager.h @@ -34,6 +34,14 @@ public: // - Game Updates VirtualDir PatchExeFS(VirtualDir exefs) const; + // Currently tracked NSO patches: + // - IPS + std::vector PatchNSO(const std::vector& 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& build_id) const; + // Currently tracked RomFS patches: // - Game Updates // - LayeredFS From 003b44822a309c0ef57fc6dd41e401e18ecb92bf Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sat, 29 Sep 2018 22:16:09 -0400 Subject: [PATCH 5/7] nso: Add framework to support patching of uncompressed NSOs --- src/core/loader/nso.cpp | 15 ++++++++++++++- src/core/loader/nso.h | 4 +++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp index 512954d40..d22a88af1 100644 --- a/src/core/loader/nso.cpp +++ b/src/core/loader/nso.cpp @@ -10,6 +10,7 @@ #include "common/logging/log.h" #include "common/swap.h" #include "core/core.h" +#include "core/file_sys/patch_manager.h" #include "core/gdbstub/gdbstub.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/process.h" @@ -92,7 +93,8 @@ static constexpr u32 PageAlignSize(u32 size) { return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK; } -VAddr AppLoader_NSO::LoadModule(FileSys::VirtualFile file, VAddr load_base) { +VAddr AppLoader_NSO::LoadModule(FileSys::VirtualFile file, VAddr load_base, + std::shared_ptr pm) { if (file == nullptr) return {}; @@ -141,6 +143,17 @@ VAddr AppLoader_NSO::LoadModule(FileSys::VirtualFile file, VAddr load_base) { const u32 image_size{PageAlignSize(static_cast(program_image.size()) + bss_size)}; program_image.resize(image_size); + // Apply patches if necessary + if (pm != nullptr && pm->HasNSOPatch(nso_header.build_id)) { + std::vector pi_header(program_image.size() + 0x100); + std::memcpy(pi_header.data(), &nso_header, sizeof(NsoHeader)); + std::memcpy(pi_header.data() + 0x100, program_image.data(), program_image.size()); + + pi_header = pm->PatchNSO(pi_header); + + std::memcpy(program_image.data(), pi_header.data() + 0x100, program_image.size()); + } + // Load codeset for current process codeset->name = file->GetName(); codeset->memory = std::make_shared>(std::move(program_image)); diff --git a/src/core/loader/nso.h b/src/core/loader/nso.h index 7f142405b..7c26aa4ba 100644 --- a/src/core/loader/nso.h +++ b/src/core/loader/nso.h @@ -5,6 +5,7 @@ #pragma once #include "common/common_types.h" +#include "core/file_sys/patch_manager.h" #include "core/loader/linker.h" #include "core/loader/loader.h" @@ -26,7 +27,8 @@ public: return IdentifyType(file); } - static VAddr LoadModule(FileSys::VirtualFile file, VAddr load_base); + static VAddr LoadModule(FileSys::VirtualFile file, VAddr load_base, + std::shared_ptr pm = nullptr); ResultStatus Load(Kernel::Process& process) override; }; From 6d441828e6eab50fea7041af8cfa5299fc78b7eb Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sat, 29 Sep 2018 22:16:32 -0400 Subject: [PATCH 6/7] deconstructed_rom_directory: Force NSO loader to patch NSOs --- src/core/loader/deconstructed_rom_directory.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/core/loader/deconstructed_rom_directory.cpp b/src/core/loader/deconstructed_rom_directory.cpp index c1824b9c3..338f6b01b 100644 --- a/src/core/loader/deconstructed_rom_directory.cpp +++ b/src/core/loader/deconstructed_rom_directory.cpp @@ -139,7 +139,9 @@ ResultStatus AppLoader_DeconstructedRomDirectory::Load(Kernel::Process& process) const FileSys::VirtualFile module_file = dir->GetFile(module); if (module_file != nullptr) { const VAddr load_addr = next_load_addr; - next_load_addr = AppLoader_NSO::LoadModule(module_file, load_addr); + next_load_addr = AppLoader_NSO::LoadModule( + module_file, load_addr, + std::make_shared(metadata.GetTitleID())); LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", module, load_addr); // Register module with GDBStub GDBStub::RegisterModule(module, load_addr, next_load_addr - 1, false); From 215b65fe75810b72bb76ae8dbb75ea59ac16f13f Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Tue, 2 Oct 2018 17:03:34 -0400 Subject: [PATCH 7/7] nso: Optimize loading of IPS patches Avoid resource-heavy classes and remove quasi-duplicated code. --- src/core/file_sys/patch_manager.cpp | 81 +++++++++---------- src/core/file_sys/patch_manager.h | 2 +- .../loader/deconstructed_rom_directory.cpp | 5 +- src/core/loader/nso.cpp | 4 +- src/core/loader/nso.h | 2 +- 5 files changed, 43 insertions(+), 51 deletions(-) diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cpp index 044e01ae9..539698f6e 100644 --- a/src/core/file_sys/patch_manager.cpp +++ b/src/core/file_sys/patch_manager.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "common/hex_util.h" #include "common/logging/log.h" @@ -72,11 +73,34 @@ VirtualDir PatchManager::PatchExeFS(VirtualDir exefs) const { return exefs; } +static std::vector CollectIPSPatches(const std::vector& patch_dirs, + const std::string& build_id) { + std::vector 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 PatchManager::PatchNSO(const std::vector& nso) const { if (nso.size() < 0x100) return nso; - NSOBuildHeader header{}; + NSOBuildHeader header; std::memcpy(&header, nso.data(), sizeof(NSOBuildHeader)); if (header.magic != Common::MakeMagic('N', 'S', 'O', '0')) @@ -91,24 +115,7 @@ std::vector PatchManager::PatchNSO(const std::vector& nso) const { 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(); }); - - std::vector ips; - ips.reserve(patch_dirs.size() - 1); - 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_first_of('.')); - 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); - } - } - } + const auto ips = CollectIPSPatches(patch_dirs, build_id); auto out = nso; for (const auto& ips_file : ips) { @@ -136,23 +143,7 @@ bool PatchManager::HasNSOPatch(const std::array& build_id_) const { std::sort(patch_dirs.begin(), patch_dirs.end(), [](const VirtualDir& l, const VirtualDir& r) { return l->GetName() < r->GetName(); }); - 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_first_of('.')); - const auto this_build_id = p1.substr(0, p1.find_last_not_of('0') + 1); - - if (build_id == this_build_id) - return true; - } - } - } - - return false; + return !CollectIPSPatches(patch_dirs, build_id).empty(); } static void ApplyLayeredFS(VirtualFile& romfs, u64 title_id, ContentRecordType type) { @@ -222,7 +213,7 @@ VirtualFile PatchManager::PatchRomFS(VirtualFile romfs, u64 ivfc_offset, return romfs; } -void AppendCommaIfNotEmpty(std::string& to, const std::string& with) { +static void AppendCommaIfNotEmpty(std::string& to, const std::string& with) { if (to.empty()) to += with; else @@ -233,8 +224,8 @@ static bool IsDirValidAndNonEmpty(const VirtualDir& dir) { return dir != nullptr && (!dir->GetFiles().empty() || !dir->GetSubdirectories().empty()); } -std::map PatchManager::GetPatchVersionNames() const { - std::map out; +std::map> PatchManager::GetPatchVersionNames() const { + std::map> out; const auto installed = Service::FileSystem::GetUnionContents(); // Game Updates @@ -243,19 +234,21 @@ std::map PatchManager::GetPatchVersionNames() const { auto [nacp, discard_icon_file] = update.GetControlMetadata(); if (nacp != nullptr) { - out["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["Update"] = ""; + out.insert_or_assign("Update", ""); } else { - out["Update"] = - FormatTitleVersion(meta_ver.get(), TitleVersionFormat::ThreeElements); + out.insert_or_assign( + "Update", + FormatTitleVersion(meta_ver.get(), TitleVersionFormat::ThreeElements)); } } } + // 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()) { @@ -292,7 +285,7 @@ std::map 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 254f7bfc9..6a864ec43 100644 --- a/src/core/file_sys/patch_manager.h +++ b/src/core/file_sys/patch_manager.h @@ -50,7 +50,7 @@ public: // Returns a vector of pairs between patch names and patch versions. // i.e. Update 3.2.2 will return {"Update", "3.2.2"} - std::map GetPatchVersionNames() const; + std::map> 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. diff --git a/src/core/loader/deconstructed_rom_directory.cpp b/src/core/loader/deconstructed_rom_directory.cpp index 338f6b01b..9a86e5824 100644 --- a/src/core/loader/deconstructed_rom_directory.cpp +++ b/src/core/loader/deconstructed_rom_directory.cpp @@ -130,6 +130,7 @@ ResultStatus AppLoader_DeconstructedRomDirectory::Load(Kernel::Process& process) } process.LoadFromMetadata(metadata); + const FileSys::PatchManager pm(metadata.GetTitleID()); // Load NSO modules const VAddr base_address = process.VMManager().GetCodeRegionBaseAddress(); @@ -139,9 +140,7 @@ ResultStatus AppLoader_DeconstructedRomDirectory::Load(Kernel::Process& process) const FileSys::VirtualFile module_file = dir->GetFile(module); if (module_file != nullptr) { const VAddr load_addr = next_load_addr; - next_load_addr = AppLoader_NSO::LoadModule( - module_file, load_addr, - std::make_shared(metadata.GetTitleID())); + next_load_addr = AppLoader_NSO::LoadModule(module_file, load_addr, pm); LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", module, load_addr); // Register module with GDBStub GDBStub::RegisterModule(module, load_addr, next_load_addr - 1, false); diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp index d22a88af1..2186b02af 100644 --- a/src/core/loader/nso.cpp +++ b/src/core/loader/nso.cpp @@ -94,7 +94,7 @@ static constexpr u32 PageAlignSize(u32 size) { } VAddr AppLoader_NSO::LoadModule(FileSys::VirtualFile file, VAddr load_base, - std::shared_ptr pm) { + boost::optional pm) { if (file == nullptr) return {}; @@ -144,7 +144,7 @@ VAddr AppLoader_NSO::LoadModule(FileSys::VirtualFile file, VAddr load_base, program_image.resize(image_size); // Apply patches if necessary - if (pm != nullptr && pm->HasNSOPatch(nso_header.build_id)) { + if (pm != boost::none && pm->HasNSOPatch(nso_header.build_id)) { std::vector pi_header(program_image.size() + 0x100); std::memcpy(pi_header.data(), &nso_header, sizeof(NsoHeader)); std::memcpy(pi_header.data() + 0x100, program_image.data(), program_image.size()); diff --git a/src/core/loader/nso.h b/src/core/loader/nso.h index 7c26aa4ba..05353d4d9 100644 --- a/src/core/loader/nso.h +++ b/src/core/loader/nso.h @@ -28,7 +28,7 @@ public: } static VAddr LoadModule(FileSys::VirtualFile file, VAddr load_base, - std::shared_ptr pm = nullptr); + boost::optional pm = boost::none); ResultStatus Load(Kernel::Process& process) override; };