registered_cache.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <regex>
  5. #include <mbedtls/sha256.h>
  6. #include "common/assert.h"
  7. #include "common/file_util.h"
  8. #include "common/hex_util.h"
  9. #include "common/logging/log.h"
  10. #include "core/crypto/key_manager.h"
  11. #include "core/file_sys/card_image.h"
  12. #include "core/file_sys/content_archive.h"
  13. #include "core/file_sys/nca_metadata.h"
  14. #include "core/file_sys/registered_cache.h"
  15. #include "core/file_sys/submission_package.h"
  16. #include "core/file_sys/vfs_concat.h"
  17. #include "core/loader/loader.h"
  18. namespace FileSys {
  19. // The size of blocks to use when vfs raw copying into nand.
  20. constexpr size_t VFS_RC_LARGE_COPY_BLOCK = 0x400000;
  21. std::string RegisteredCacheEntry::DebugInfo() const {
  22. return fmt::format("title_id={:016X}, content_type={:02X}", title_id, static_cast<u8>(type));
  23. }
  24. bool operator<(const RegisteredCacheEntry& lhs, const RegisteredCacheEntry& rhs) {
  25. return (lhs.title_id < rhs.title_id) || (lhs.title_id == rhs.title_id && lhs.type < rhs.type);
  26. }
  27. static bool FollowsTwoDigitDirFormat(std::string_view name) {
  28. static const std::regex two_digit_regex("000000[0-9A-F]{2}", std::regex_constants::ECMAScript |
  29. std::regex_constants::icase);
  30. return std::regex_match(name.begin(), name.end(), two_digit_regex);
  31. }
  32. static bool FollowsNcaIdFormat(std::string_view name) {
  33. static const std::regex nca_id_regex("[0-9A-F]{32}\\.nca", std::regex_constants::ECMAScript |
  34. std::regex_constants::icase);
  35. return name.size() == 36 && std::regex_match(name.begin(), name.end(), nca_id_regex);
  36. }
  37. static std::string GetRelativePathFromNcaID(const std::array<u8, 16>& nca_id, bool second_hex_upper,
  38. bool within_two_digit) {
  39. if (!within_two_digit)
  40. return fmt::format("/{}.nca", Common::HexArrayToString(nca_id, second_hex_upper));
  41. Core::Crypto::SHA256Hash hash{};
  42. mbedtls_sha256(nca_id.data(), nca_id.size(), hash.data(), 0);
  43. return fmt::format("/000000{:02X}/{}.nca", hash[0],
  44. Common::HexArrayToString(nca_id, second_hex_upper));
  45. }
  46. static std::string GetCNMTName(TitleType type, u64 title_id) {
  47. constexpr std::array<const char*, 9> TITLE_TYPE_NAMES{
  48. "SystemProgram",
  49. "SystemData",
  50. "SystemUpdate",
  51. "BootImagePackage",
  52. "BootImagePackageSafe",
  53. "Application",
  54. "Patch",
  55. "AddOnContent",
  56. "" ///< Currently unknown 'DeltaTitle'
  57. };
  58. auto index = static_cast<std::size_t>(type);
  59. // If the index is after the jump in TitleType, subtract it out.
  60. if (index >= static_cast<std::size_t>(TitleType::Application)) {
  61. index -= static_cast<std::size_t>(TitleType::Application) -
  62. static_cast<std::size_t>(TitleType::FirmwarePackageB);
  63. }
  64. return fmt::format("{}_{:016x}.cnmt", TITLE_TYPE_NAMES[index], title_id);
  65. }
  66. static ContentRecordType GetCRTypeFromNCAType(NCAContentType type) {
  67. switch (type) {
  68. case NCAContentType::Program:
  69. // TODO(DarkLordZach): Differentiate between Program and Patch
  70. return ContentRecordType::Program;
  71. case NCAContentType::Meta:
  72. return ContentRecordType::Meta;
  73. case NCAContentType::Control:
  74. return ContentRecordType::Control;
  75. case NCAContentType::Data:
  76. case NCAContentType::Data_Unknown5:
  77. return ContentRecordType::Data;
  78. case NCAContentType::Manual:
  79. // TODO(DarkLordZach): Peek at NCA contents to differentiate Manual and Legal.
  80. return ContentRecordType::Manual;
  81. default:
  82. UNREACHABLE_MSG("Invalid NCAContentType={:02X}", static_cast<u8>(type));
  83. }
  84. }
  85. VirtualFile RegisteredCache::OpenFileOrDirectoryConcat(const VirtualDir& dir,
  86. std::string_view path) const {
  87. if (dir->GetFileRelative(path) != nullptr)
  88. return dir->GetFileRelative(path);
  89. if (dir->GetDirectoryRelative(path) != nullptr) {
  90. const auto nca_dir = dir->GetDirectoryRelative(path);
  91. VirtualFile file = nullptr;
  92. const auto files = nca_dir->GetFiles();
  93. if (files.size() == 1 && files[0]->GetName() == "00") {
  94. file = files[0];
  95. } else {
  96. std::vector<VirtualFile> concat;
  97. // Since the files are a two-digit hex number, max is FF.
  98. for (std::size_t i = 0; i < 0x100; ++i) {
  99. auto next = nca_dir->GetFile(fmt::format("{:02X}", i));
  100. if (next != nullptr) {
  101. concat.push_back(std::move(next));
  102. } else {
  103. next = nca_dir->GetFile(fmt::format("{:02x}", i));
  104. if (next != nullptr)
  105. concat.push_back(std::move(next));
  106. else
  107. break;
  108. }
  109. }
  110. if (concat.empty())
  111. return nullptr;
  112. file = ConcatenatedVfsFile::MakeConcatenatedFile(concat, concat.front()->GetName());
  113. }
  114. return file;
  115. }
  116. return nullptr;
  117. }
  118. VirtualFile RegisteredCache::GetFileAtID(NcaID id) const {
  119. VirtualFile file;
  120. // Try all four modes of file storage:
  121. // (bit 1 = uppercase/lower, bit 0 = within a two-digit dir)
  122. // 00: /000000**/{:032X}.nca
  123. // 01: /{:032X}.nca
  124. // 10: /000000**/{:032x}.nca
  125. // 11: /{:032x}.nca
  126. for (u8 i = 0; i < 4; ++i) {
  127. const auto path = GetRelativePathFromNcaID(id, (i & 0b10) == 0, (i & 0b01) == 0);
  128. file = OpenFileOrDirectoryConcat(dir, path);
  129. if (file != nullptr)
  130. return file;
  131. }
  132. return file;
  133. }
  134. static boost::optional<NcaID> CheckMapForContentRecord(
  135. const boost::container::flat_map<u64, CNMT>& map, u64 title_id, ContentRecordType type) {
  136. if (map.find(title_id) == map.end())
  137. return boost::none;
  138. const auto& cnmt = map.at(title_id);
  139. const auto iter = std::find_if(cnmt.GetContentRecords().begin(), cnmt.GetContentRecords().end(),
  140. [type](const ContentRecord& rec) { return rec.type == type; });
  141. if (iter == cnmt.GetContentRecords().end())
  142. return boost::none;
  143. return boost::make_optional(iter->nca_id);
  144. }
  145. boost::optional<NcaID> RegisteredCache::GetNcaIDFromMetadata(u64 title_id,
  146. ContentRecordType type) const {
  147. if (type == ContentRecordType::Meta && meta_id.find(title_id) != meta_id.end())
  148. return meta_id.at(title_id);
  149. const auto res1 = CheckMapForContentRecord(yuzu_meta, title_id, type);
  150. if (res1 != boost::none)
  151. return res1;
  152. return CheckMapForContentRecord(meta, title_id, type);
  153. }
  154. std::vector<NcaID> RegisteredCache::AccumulateFiles() const {
  155. std::vector<NcaID> ids;
  156. for (const auto& d2_dir : dir->GetSubdirectories()) {
  157. if (FollowsNcaIdFormat(d2_dir->GetName())) {
  158. ids.push_back(Common::HexStringToArray<0x10, true>(d2_dir->GetName().substr(0, 0x20)));
  159. continue;
  160. }
  161. if (!FollowsTwoDigitDirFormat(d2_dir->GetName()))
  162. continue;
  163. for (const auto& nca_dir : d2_dir->GetSubdirectories()) {
  164. if (!FollowsNcaIdFormat(nca_dir->GetName()))
  165. continue;
  166. ids.push_back(Common::HexStringToArray<0x10, true>(nca_dir->GetName().substr(0, 0x20)));
  167. }
  168. for (const auto& nca_file : d2_dir->GetFiles()) {
  169. if (!FollowsNcaIdFormat(nca_file->GetName()))
  170. continue;
  171. ids.push_back(
  172. Common::HexStringToArray<0x10, true>(nca_file->GetName().substr(0, 0x20)));
  173. }
  174. }
  175. for (const auto& d2_file : dir->GetFiles()) {
  176. if (FollowsNcaIdFormat(d2_file->GetName()))
  177. ids.push_back(Common::HexStringToArray<0x10, true>(d2_file->GetName().substr(0, 0x20)));
  178. }
  179. return ids;
  180. }
  181. void RegisteredCache::ProcessFiles(const std::vector<NcaID>& ids) {
  182. for (const auto& id : ids) {
  183. const auto file = GetFileAtID(id);
  184. if (file == nullptr)
  185. continue;
  186. const auto nca = std::make_shared<NCA>(parser(file, id));
  187. if (nca->GetStatus() != Loader::ResultStatus::Success ||
  188. nca->GetType() != NCAContentType::Meta) {
  189. continue;
  190. }
  191. const auto section0 = nca->GetSubdirectories()[0];
  192. for (const auto& section0_file : section0->GetFiles()) {
  193. if (section0_file->GetExtension() != "cnmt")
  194. continue;
  195. meta.insert_or_assign(nca->GetTitleId(), CNMT(section0_file));
  196. meta_id.insert_or_assign(nca->GetTitleId(), id);
  197. break;
  198. }
  199. }
  200. }
  201. void RegisteredCache::AccumulateYuzuMeta() {
  202. const auto dir = this->dir->GetSubdirectory("yuzu_meta");
  203. if (dir == nullptr)
  204. return;
  205. for (const auto& file : dir->GetFiles()) {
  206. if (file->GetExtension() != "cnmt")
  207. continue;
  208. CNMT cnmt(file);
  209. yuzu_meta.insert_or_assign(cnmt.GetTitleID(), std::move(cnmt));
  210. }
  211. }
  212. void RegisteredCache::Refresh() {
  213. if (dir == nullptr)
  214. return;
  215. const auto ids = AccumulateFiles();
  216. ProcessFiles(ids);
  217. AccumulateYuzuMeta();
  218. }
  219. RegisteredCache::RegisteredCache(VirtualDir dir_, RegisteredCacheParsingFunction parsing_function)
  220. : dir(std::move(dir_)), parser(std::move(parsing_function)) {
  221. Refresh();
  222. }
  223. RegisteredCache::~RegisteredCache() = default;
  224. bool RegisteredCache::HasEntry(u64 title_id, ContentRecordType type) const {
  225. return GetEntryRaw(title_id, type) != nullptr;
  226. }
  227. bool RegisteredCache::HasEntry(RegisteredCacheEntry entry) const {
  228. return GetEntryRaw(entry) != nullptr;
  229. }
  230. VirtualFile RegisteredCache::GetEntryUnparsed(u64 title_id, ContentRecordType type) const {
  231. const auto id = GetNcaIDFromMetadata(title_id, type);
  232. if (id == boost::none)
  233. return nullptr;
  234. return GetFileAtID(id.get());
  235. }
  236. VirtualFile RegisteredCache::GetEntryUnparsed(RegisteredCacheEntry entry) const {
  237. return GetEntryUnparsed(entry.title_id, entry.type);
  238. }
  239. boost::optional<u32> RegisteredCache::GetEntryVersion(u64 title_id) const {
  240. const auto meta_iter = meta.find(title_id);
  241. if (meta_iter != meta.end())
  242. return meta_iter->second.GetTitleVersion();
  243. const auto yuzu_meta_iter = yuzu_meta.find(title_id);
  244. if (yuzu_meta_iter != yuzu_meta.end())
  245. return yuzu_meta_iter->second.GetTitleVersion();
  246. return boost::none;
  247. }
  248. VirtualFile RegisteredCache::GetEntryRaw(u64 title_id, ContentRecordType type) const {
  249. const auto id = GetNcaIDFromMetadata(title_id, type);
  250. if (id == boost::none)
  251. return nullptr;
  252. return parser(GetFileAtID(id.get()), id.get());
  253. }
  254. VirtualFile RegisteredCache::GetEntryRaw(RegisteredCacheEntry entry) const {
  255. return GetEntryRaw(entry.title_id, entry.type);
  256. }
  257. std::shared_ptr<NCA> RegisteredCache::GetEntry(u64 title_id, ContentRecordType type) const {
  258. const auto raw = GetEntryRaw(title_id, type);
  259. if (raw == nullptr)
  260. return nullptr;
  261. return std::make_shared<NCA>(raw);
  262. }
  263. std::shared_ptr<NCA> RegisteredCache::GetEntry(RegisteredCacheEntry entry) const {
  264. return GetEntry(entry.title_id, entry.type);
  265. }
  266. template <typename T>
  267. void RegisteredCache::IterateAllMetadata(
  268. std::vector<T>& out, std::function<T(const CNMT&, const ContentRecord&)> proc,
  269. std::function<bool(const CNMT&, const ContentRecord&)> filter) const {
  270. for (const auto& kv : meta) {
  271. const auto& cnmt = kv.second;
  272. if (filter(cnmt, EMPTY_META_CONTENT_RECORD))
  273. out.push_back(proc(cnmt, EMPTY_META_CONTENT_RECORD));
  274. for (const auto& rec : cnmt.GetContentRecords()) {
  275. if (GetFileAtID(rec.nca_id) != nullptr && filter(cnmt, rec)) {
  276. out.push_back(proc(cnmt, rec));
  277. }
  278. }
  279. }
  280. for (const auto& kv : yuzu_meta) {
  281. const auto& cnmt = kv.second;
  282. for (const auto& rec : cnmt.GetContentRecords()) {
  283. if (GetFileAtID(rec.nca_id) != nullptr && filter(cnmt, rec)) {
  284. out.push_back(proc(cnmt, rec));
  285. }
  286. }
  287. }
  288. }
  289. std::vector<RegisteredCacheEntry> RegisteredCache::ListEntries() const {
  290. std::vector<RegisteredCacheEntry> out;
  291. IterateAllMetadata<RegisteredCacheEntry>(
  292. out,
  293. [](const CNMT& c, const ContentRecord& r) {
  294. return RegisteredCacheEntry{c.GetTitleID(), r.type};
  295. },
  296. [](const CNMT& c, const ContentRecord& r) { return true; });
  297. return out;
  298. }
  299. std::vector<RegisteredCacheEntry> RegisteredCache::ListEntriesFilter(
  300. boost::optional<TitleType> title_type, boost::optional<ContentRecordType> record_type,
  301. boost::optional<u64> title_id) const {
  302. std::vector<RegisteredCacheEntry> out;
  303. IterateAllMetadata<RegisteredCacheEntry>(
  304. out,
  305. [](const CNMT& c, const ContentRecord& r) {
  306. return RegisteredCacheEntry{c.GetTitleID(), r.type};
  307. },
  308. [&title_type, &record_type, &title_id](const CNMT& c, const ContentRecord& r) {
  309. if (title_type != boost::none && title_type.get() != c.GetType())
  310. return false;
  311. if (record_type != boost::none && record_type.get() != r.type)
  312. return false;
  313. if (title_id != boost::none && title_id.get() != c.GetTitleID())
  314. return false;
  315. return true;
  316. });
  317. return out;
  318. }
  319. static std::shared_ptr<NCA> GetNCAFromNSPForID(std::shared_ptr<NSP> nsp, const NcaID& id) {
  320. const auto file = nsp->GetFile(fmt::format("{}.nca", Common::HexArrayToString(id, false)));
  321. if (file == nullptr)
  322. return nullptr;
  323. return std::make_shared<NCA>(file);
  324. }
  325. InstallResult RegisteredCache::InstallEntry(std::shared_ptr<XCI> xci, bool overwrite_if_exists,
  326. const VfsCopyFunction& copy) {
  327. return InstallEntry(xci->GetSecurePartitionNSP(), overwrite_if_exists, copy);
  328. }
  329. InstallResult RegisteredCache::InstallEntry(std::shared_ptr<NSP> nsp, bool overwrite_if_exists,
  330. const VfsCopyFunction& copy) {
  331. const auto& ncas = nsp->GetNCAsCollapsed();
  332. const auto& meta_iter = std::find_if(ncas.begin(), ncas.end(), [](std::shared_ptr<NCA> nca) {
  333. return nca->GetType() == NCAContentType::Meta;
  334. });
  335. if (meta_iter == ncas.end()) {
  336. LOG_ERROR(Loader, "The XCI you are attempting to install does not have a metadata NCA and "
  337. "is therefore malformed. Double check your encryption keys.");
  338. return InstallResult::ErrorMetaFailed;
  339. }
  340. // Install Metadata File
  341. const auto meta_id_raw = (*meta_iter)->GetName().substr(0, 32);
  342. const auto meta_id = Common::HexStringToArray<16>(meta_id_raw);
  343. const auto res = RawInstallNCA(*meta_iter, copy, overwrite_if_exists, meta_id);
  344. if (res != InstallResult::Success)
  345. return res;
  346. // Install all the other NCAs
  347. const auto section0 = (*meta_iter)->GetSubdirectories()[0];
  348. const auto cnmt_file = section0->GetFiles()[0];
  349. const CNMT cnmt(cnmt_file);
  350. for (const auto& record : cnmt.GetContentRecords()) {
  351. const auto nca = GetNCAFromNSPForID(nsp, record.nca_id);
  352. if (nca == nullptr)
  353. return InstallResult::ErrorCopyFailed;
  354. const auto res2 = RawInstallNCA(nca, copy, overwrite_if_exists, record.nca_id);
  355. if (res2 != InstallResult::Success)
  356. return res2;
  357. }
  358. Refresh();
  359. return InstallResult::Success;
  360. }
  361. InstallResult RegisteredCache::InstallEntry(std::shared_ptr<NCA> nca, TitleType type,
  362. bool overwrite_if_exists, const VfsCopyFunction& copy) {
  363. CNMTHeader header{
  364. nca->GetTitleId(), ///< Title ID
  365. 0, ///< Ignore/Default title version
  366. type, ///< Type
  367. {}, ///< Padding
  368. 0x10, ///< Default table offset
  369. 1, ///< 1 Content Entry
  370. 0, ///< No Meta Entries
  371. {}, ///< Padding
  372. };
  373. OptionalHeader opt_header{0, 0};
  374. ContentRecord c_rec{{}, {}, {}, GetCRTypeFromNCAType(nca->GetType()), {}};
  375. const auto& data = nca->GetBaseFile()->ReadBytes(0x100000);
  376. mbedtls_sha256(data.data(), data.size(), c_rec.hash.data(), 0);
  377. memcpy(&c_rec.nca_id, &c_rec.hash, 16);
  378. const CNMT new_cnmt(header, opt_header, {c_rec}, {});
  379. if (!RawInstallYuzuMeta(new_cnmt))
  380. return InstallResult::ErrorMetaFailed;
  381. return RawInstallNCA(nca, copy, overwrite_if_exists, c_rec.nca_id);
  382. }
  383. InstallResult RegisteredCache::RawInstallNCA(std::shared_ptr<NCA> nca, const VfsCopyFunction& copy,
  384. bool overwrite_if_exists,
  385. boost::optional<NcaID> override_id) {
  386. const auto in = nca->GetBaseFile();
  387. Core::Crypto::SHA256Hash hash{};
  388. // Calculate NcaID
  389. // NOTE: Because computing the SHA256 of an entire NCA is quite expensive (especially if the
  390. // game is massive), we're going to cheat and only hash the first MB of the NCA.
  391. // Also, for XCIs the NcaID matters, so if the override id isn't none, use that.
  392. NcaID id{};
  393. if (override_id == boost::none) {
  394. const auto& data = in->ReadBytes(0x100000);
  395. mbedtls_sha256(data.data(), data.size(), hash.data(), 0);
  396. memcpy(id.data(), hash.data(), 16);
  397. } else {
  398. id = override_id.get();
  399. }
  400. std::string path = GetRelativePathFromNcaID(id, false, true);
  401. if (GetFileAtID(id) != nullptr && !overwrite_if_exists) {
  402. LOG_WARNING(Loader, "Attempting to overwrite existing NCA. Skipping...");
  403. return InstallResult::ErrorAlreadyExists;
  404. }
  405. if (GetFileAtID(id) != nullptr) {
  406. LOG_WARNING(Loader, "Overwriting existing NCA...");
  407. VirtualDir c_dir;
  408. { c_dir = dir->GetFileRelative(path)->GetContainingDirectory(); }
  409. c_dir->DeleteFile(FileUtil::GetFilename(path));
  410. }
  411. auto out = dir->CreateFileRelative(path);
  412. if (out == nullptr)
  413. return InstallResult::ErrorCopyFailed;
  414. return copy(in, out, VFS_RC_LARGE_COPY_BLOCK) ? InstallResult::Success
  415. : InstallResult::ErrorCopyFailed;
  416. }
  417. bool RegisteredCache::RawInstallYuzuMeta(const CNMT& cnmt) {
  418. // Reasoning behind this method can be found in the comment for InstallEntry, NCA overload.
  419. const auto dir = this->dir->CreateDirectoryRelative("yuzu_meta");
  420. const auto filename = GetCNMTName(cnmt.GetType(), cnmt.GetTitleID());
  421. if (dir->GetFile(filename) == nullptr) {
  422. auto out = dir->CreateFile(filename);
  423. const auto buffer = cnmt.Serialize();
  424. out->Resize(buffer.size());
  425. out->WriteBytes(buffer);
  426. } else {
  427. auto out = dir->GetFile(filename);
  428. CNMT old_cnmt(out);
  429. // Returns true on change
  430. if (old_cnmt.UnionRecords(cnmt)) {
  431. out->Resize(0);
  432. const auto buffer = old_cnmt.Serialize();
  433. out->Resize(buffer.size());
  434. out->WriteBytes(buffer);
  435. }
  436. }
  437. Refresh();
  438. return std::find_if(yuzu_meta.begin(), yuzu_meta.end(),
  439. [&cnmt](const std::pair<u64, CNMT>& kv) {
  440. return kv.second.GetType() == cnmt.GetType() &&
  441. kv.second.GetTitleID() == cnmt.GetTitleID();
  442. }) != yuzu_meta.end();
  443. }
  444. RegisteredCacheUnion::RegisteredCacheUnion(std::vector<std::shared_ptr<RegisteredCache>> caches)
  445. : caches(std::move(caches)) {}
  446. void RegisteredCacheUnion::Refresh() {
  447. for (const auto& c : caches)
  448. c->Refresh();
  449. }
  450. bool RegisteredCacheUnion::HasEntry(u64 title_id, ContentRecordType type) const {
  451. return std::any_of(caches.begin(), caches.end(), [title_id, type](const auto& cache) {
  452. return cache->HasEntry(title_id, type);
  453. });
  454. }
  455. bool RegisteredCacheUnion::HasEntry(RegisteredCacheEntry entry) const {
  456. return HasEntry(entry.title_id, entry.type);
  457. }
  458. boost::optional<u32> RegisteredCacheUnion::GetEntryVersion(u64 title_id) const {
  459. for (const auto& c : caches) {
  460. const auto res = c->GetEntryVersion(title_id);
  461. if (res != boost::none)
  462. return res;
  463. }
  464. return boost::none;
  465. }
  466. VirtualFile RegisteredCacheUnion::GetEntryUnparsed(u64 title_id, ContentRecordType type) const {
  467. for (const auto& c : caches) {
  468. const auto res = c->GetEntryUnparsed(title_id, type);
  469. if (res != nullptr)
  470. return res;
  471. }
  472. return nullptr;
  473. }
  474. VirtualFile RegisteredCacheUnion::GetEntryUnparsed(RegisteredCacheEntry entry) const {
  475. return GetEntryUnparsed(entry.title_id, entry.type);
  476. }
  477. VirtualFile RegisteredCacheUnion::GetEntryRaw(u64 title_id, ContentRecordType type) const {
  478. for (const auto& c : caches) {
  479. const auto res = c->GetEntryRaw(title_id, type);
  480. if (res != nullptr)
  481. return res;
  482. }
  483. return nullptr;
  484. }
  485. VirtualFile RegisteredCacheUnion::GetEntryRaw(RegisteredCacheEntry entry) const {
  486. return GetEntryRaw(entry.title_id, entry.type);
  487. }
  488. std::shared_ptr<NCA> RegisteredCacheUnion::GetEntry(u64 title_id, ContentRecordType type) const {
  489. const auto raw = GetEntryRaw(title_id, type);
  490. if (raw == nullptr)
  491. return nullptr;
  492. return std::make_shared<NCA>(raw);
  493. }
  494. std::shared_ptr<NCA> RegisteredCacheUnion::GetEntry(RegisteredCacheEntry entry) const {
  495. return GetEntry(entry.title_id, entry.type);
  496. }
  497. std::vector<RegisteredCacheEntry> RegisteredCacheUnion::ListEntries() const {
  498. std::vector<RegisteredCacheEntry> out;
  499. for (const auto& c : caches) {
  500. c->IterateAllMetadata<RegisteredCacheEntry>(
  501. out,
  502. [](const CNMT& c, const ContentRecord& r) {
  503. return RegisteredCacheEntry{c.GetTitleID(), r.type};
  504. },
  505. [](const CNMT& c, const ContentRecord& r) { return true; });
  506. }
  507. return out;
  508. }
  509. std::vector<RegisteredCacheEntry> RegisteredCacheUnion::ListEntriesFilter(
  510. boost::optional<TitleType> title_type, boost::optional<ContentRecordType> record_type,
  511. boost::optional<u64> title_id) const {
  512. std::vector<RegisteredCacheEntry> out;
  513. for (const auto& c : caches) {
  514. c->IterateAllMetadata<RegisteredCacheEntry>(
  515. out,
  516. [](const CNMT& c, const ContentRecord& r) {
  517. return RegisteredCacheEntry{c.GetTitleID(), r.type};
  518. },
  519. [&title_type, &record_type, &title_id](const CNMT& c, const ContentRecord& r) {
  520. if (title_type != boost::none && title_type.get() != c.GetType())
  521. return false;
  522. if (record_type != boost::none && record_type.get() != r.type)
  523. return false;
  524. if (title_id != boost::none && title_id.get() != c.GetTitleID())
  525. return false;
  526. return true;
  527. });
  528. }
  529. return out;
  530. }
  531. } // namespace FileSys