From 215b65fe75810b72bb76ae8dbb75ea59ac16f13f Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Tue, 2 Oct 2018 17:03:34 -0400 Subject: [PATCH] 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 044e01ae9b..539698f6e0 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 254f7bfc99..6a864ec43c 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 338f6b01b4..9a86e58242 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 d22a88af1d..2186b02af0 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 7c26aa4bae..05353d4d99 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; };