From 6636f3ff4717886f48b7cd238cd75315952cb112 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 9 Oct 2018 14:22:31 -0400 Subject: [PATCH] patch_manager: Return a std::unique_ptr from ParseControlNCA() and GetControlMetadata() instead of a std::shared_ptr Neither of these functions require the use of shared ownership of the returned pointer. This makes it more difficult to create reference cycles with, and makes the interface more generic, as std::shared_ptr instances can be created from a std::unique_ptr, but the vice-versa isn't possible. This also alters relevant functions to take NCA arguments by const reference rather than a const reference to a std::shared_ptr. These functions don't alter the ownership of the memory used by the NCA instance, so we can make the interface more generic by not assuming anything about the type of smart pointer the NCA is contained within and make it the caller's responsibility to ensure the supplied NCA is valid. --- src/core/file_sys/patch_manager.cpp | 15 +++++++-------- src/core/file_sys/patch_manager.h | 5 ++--- src/core/loader/nsp.cpp | 2 +- src/core/loader/nsp.h | 2 +- src/core/loader/xci.cpp | 2 +- src/core/loader/xci.h | 2 +- src/yuzu/game_list_worker.cpp | 11 +++++------ 7 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cpp index b14d7cb0a9..019caebe93 100644 --- a/src/core/file_sys/patch_manager.cpp +++ b/src/core/file_sys/patch_manager.cpp @@ -345,23 +345,22 @@ std::map> PatchManager::GetPatchVersionNam return out; } -std::pair, VirtualFile> PatchManager::GetControlMetadata() const { +std::pair, VirtualFile> PatchManager::GetControlMetadata() const { const auto& installed{Service::FileSystem::GetUnionContents()}; const auto base_control_nca = installed->GetEntry(title_id, ContentRecordType::Control); if (base_control_nca == nullptr) return {}; - return ParseControlNCA(base_control_nca); + return ParseControlNCA(*base_control_nca); } -std::pair, VirtualFile> PatchManager::ParseControlNCA( - const std::shared_ptr& nca) const { - const auto base_romfs = nca->GetRomFS(); +std::pair, VirtualFile> PatchManager::ParseControlNCA(const NCA& nca) const { + const auto base_romfs = nca.GetRomFS(); if (base_romfs == nullptr) return {}; - const auto romfs = PatchRomFS(base_romfs, nca->GetBaseIVFCOffset(), ContentRecordType::Control); + const auto romfs = PatchRomFS(base_romfs, nca.GetBaseIVFCOffset(), ContentRecordType::Control); if (romfs == nullptr) return {}; @@ -373,7 +372,7 @@ std::pair, VirtualFile> PatchManager::ParseControlNCA( if (nacp_file == nullptr) nacp_file = extracted->GetFile("Control.nacp"); - const auto nacp = nacp_file == nullptr ? nullptr : std::make_shared(nacp_file); + auto nacp = nacp_file == nullptr ? nullptr : std::make_unique(nacp_file); VirtualFile icon_file; for (const auto& language : FileSys::LANGUAGE_NAMES) { @@ -382,6 +381,6 @@ std::pair, VirtualFile> PatchManager::ParseControlNCA( break; } - return {nacp, icon_file}; + return {std::move(nacp), icon_file}; } } // namespace FileSys diff --git a/src/core/file_sys/patch_manager.h b/src/core/file_sys/patch_manager.h index eb6fc4607f..7d168837f8 100644 --- a/src/core/file_sys/patch_manager.h +++ b/src/core/file_sys/patch_manager.h @@ -57,11 +57,10 @@ public: // 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. - std::pair, VirtualFile> GetControlMetadata() const; + std::pair, VirtualFile> GetControlMetadata() const; // Version of GetControlMetadata that takes an arbitrary NCA - std::pair, VirtualFile> ParseControlNCA( - const std::shared_ptr& nca) const; + std::pair, VirtualFile> ParseControlNCA(const NCA& nca) const; private: u64 title_id; diff --git a/src/core/loader/nsp.cpp b/src/core/loader/nsp.cpp index 5534ce01c4..13e57848db 100644 --- a/src/core/loader/nsp.cpp +++ b/src/core/loader/nsp.cpp @@ -35,7 +35,7 @@ AppLoader_NSP::AppLoader_NSP(FileSys::VirtualFile file) return; std::tie(nacp_file, icon_file) = - FileSys::PatchManager(nsp->GetProgramTitleID()).ParseControlNCA(control_nca); + FileSys::PatchManager(nsp->GetProgramTitleID()).ParseControlNCA(*control_nca); } AppLoader_NSP::~AppLoader_NSP() = default; diff --git a/src/core/loader/nsp.h b/src/core/loader/nsp.h index b006594a69..db91cd01ed 100644 --- a/src/core/loader/nsp.h +++ b/src/core/loader/nsp.h @@ -49,7 +49,7 @@ private: std::unique_ptr secondary_loader; FileSys::VirtualFile icon_file; - std::shared_ptr nacp_file; + std::unique_ptr nacp_file; u64 title_id; }; diff --git a/src/core/loader/xci.cpp b/src/core/loader/xci.cpp index ee5452eb97..7a619acb4e 100644 --- a/src/core/loader/xci.cpp +++ b/src/core/loader/xci.cpp @@ -30,7 +30,7 @@ AppLoader_XCI::AppLoader_XCI(FileSys::VirtualFile file) return; std::tie(nacp_file, icon_file) = - FileSys::PatchManager(xci->GetProgramTitleID()).ParseControlNCA(control_nca); + FileSys::PatchManager(xci->GetProgramTitleID()).ParseControlNCA(*control_nca); } AppLoader_XCI::~AppLoader_XCI() = default; diff --git a/src/core/loader/xci.h b/src/core/loader/xci.h index 770ed14374..46f8dfc9e3 100644 --- a/src/core/loader/xci.h +++ b/src/core/loader/xci.h @@ -49,7 +49,7 @@ private: std::unique_ptr nca_loader; FileSys::VirtualFile icon_file; - std::shared_ptr nacp_file; + std::unique_ptr nacp_file; }; } // namespace Loader diff --git a/src/yuzu/game_list_worker.cpp b/src/yuzu/game_list_worker.cpp index d2b3de6835..8f99a1c789 100644 --- a/src/yuzu/game_list_worker.cpp +++ b/src/yuzu/game_list_worker.cpp @@ -27,9 +27,8 @@ #include "yuzu/ui_settings.h" namespace { -void GetMetadataFromControlNCA(const FileSys::PatchManager& patch_manager, - const std::shared_ptr& nca, std::vector& icon, - std::string& name) { +void GetMetadataFromControlNCA(const FileSys::PatchManager& patch_manager, const FileSys::NCA& nca, + std::vector& icon, std::string& name) { auto [nacp, icon_file] = patch_manager.ParseControlNCA(nca); if (icon_file != nullptr) icon = icon_file->ReadAllBytes(); @@ -110,7 +109,7 @@ void GameListWorker::AddInstalledTitlesToGameList() { const FileSys::PatchManager patch{program_id}; const auto& control = cache->GetEntry(game.title_id, FileSys::ContentRecordType::Control); if (control != nullptr) - GetMetadataFromControlNCA(patch, control, icon, name); + GetMetadataFromControlNCA(patch, *control, icon, name); auto it = FindMatchingCompatibilityEntry(compatibility_list, program_id); @@ -197,8 +196,8 @@ void GameListWorker::AddFstEntriesToGameList(const std::string& dir_path, unsign res2 == Loader::ResultStatus::Success) { // Use from metadata pool. if (nca_control_map.find(program_id) != nca_control_map.end()) { - const auto nca = nca_control_map[program_id]; - GetMetadataFromControlNCA(patch, nca, icon, name); + const auto& nca = nca_control_map[program_id]; + GetMetadataFromControlNCA(patch, *nca, icon, name); } }