registered_cache.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <functional>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include <vector>
  11. #include <boost/container/flat_map.hpp>
  12. #include "common/common_types.h"
  13. #include "core/file_sys/vfs.h"
  14. namespace FileSys {
  15. class CNMT;
  16. class NCA;
  17. class NSP;
  18. class XCI;
  19. enum class ContentRecordType : u8;
  20. enum class TitleType : u8;
  21. struct ContentRecord;
  22. using NcaID = std::array<u8, 0x10>;
  23. using RegisteredCacheParsingFunction = std::function<VirtualFile(const VirtualFile&, const NcaID&)>;
  24. using VfsCopyFunction = std::function<bool(const VirtualFile&, const VirtualFile&, size_t)>;
  25. enum class InstallResult {
  26. Success,
  27. ErrorAlreadyExists,
  28. ErrorCopyFailed,
  29. ErrorMetaFailed,
  30. };
  31. struct RegisteredCacheEntry {
  32. u64 title_id;
  33. ContentRecordType type;
  34. std::string DebugInfo() const;
  35. };
  36. constexpr u64 GetUpdateTitleID(u64 base_title_id) {
  37. return base_title_id | 0x800;
  38. }
  39. // boost flat_map requires operator< for O(log(n)) lookups.
  40. bool operator<(const RegisteredCacheEntry& lhs, const RegisteredCacheEntry& rhs);
  41. // std unique requires operator== to identify duplicates.
  42. bool operator==(const RegisteredCacheEntry& lhs, const RegisteredCacheEntry& rhs);
  43. bool operator!=(const RegisteredCacheEntry& lhs, const RegisteredCacheEntry& rhs);
  44. /*
  45. * A class that catalogues NCAs in the registered directory structure.
  46. * Nintendo's registered format follows this structure:
  47. *
  48. * Root
  49. * | 000000XX <- XX is the ____ two digits of the NcaID
  50. * | <hash>.nca <- hash is the NcaID (first half of SHA256 over entire file) (folder)
  51. * | 00
  52. * | 01 <- Actual content split along 4GB boundaries. (optional)
  53. *
  54. * (This impl also supports substituting the nca dir for an nca file, as that's more convenient
  55. * when 4GB splitting can be ignored.)
  56. */
  57. class RegisteredCache {
  58. friend class RegisteredCacheUnion;
  59. public:
  60. // Parsing function defines the conversion from raw file to NCA. If there are other steps
  61. // besides creating the NCA from the file (e.g. NAX0 on SD Card), that should go in a custom
  62. // parsing function.
  63. explicit RegisteredCache(VirtualDir dir,
  64. RegisteredCacheParsingFunction parsing_function =
  65. [](const VirtualFile& file, const NcaID& id) { return file; });
  66. ~RegisteredCache();
  67. void Refresh();
  68. bool HasEntry(u64 title_id, ContentRecordType type) const;
  69. bool HasEntry(RegisteredCacheEntry entry) const;
  70. std::optional<u32> GetEntryVersion(u64 title_id) const;
  71. VirtualFile GetEntryUnparsed(u64 title_id, ContentRecordType type) const;
  72. VirtualFile GetEntryUnparsed(RegisteredCacheEntry entry) const;
  73. VirtualFile GetEntryRaw(u64 title_id, ContentRecordType type) const;
  74. VirtualFile GetEntryRaw(RegisteredCacheEntry entry) const;
  75. std::unique_ptr<NCA> GetEntry(u64 title_id, ContentRecordType type) const;
  76. std::unique_ptr<NCA> GetEntry(RegisteredCacheEntry entry) const;
  77. std::vector<RegisteredCacheEntry> ListEntries() const;
  78. // If a parameter is not std::nullopt, it will be filtered for from all entries.
  79. std::vector<RegisteredCacheEntry> ListEntriesFilter(
  80. std::optional<TitleType> title_type = {}, std::optional<ContentRecordType> record_type = {},
  81. std::optional<u64> title_id = {}) const;
  82. // Raw copies all the ncas from the xci/nsp to the csache. Does some quick checks to make sure
  83. // there is a meta NCA and all of them are accessible.
  84. InstallResult InstallEntry(std::shared_ptr<XCI> xci, bool overwrite_if_exists = false,
  85. const VfsCopyFunction& copy = &VfsRawCopy);
  86. InstallResult InstallEntry(std::shared_ptr<NSP> nsp, bool overwrite_if_exists = false,
  87. const VfsCopyFunction& copy = &VfsRawCopy);
  88. // Due to the fact that we must use Meta-type NCAs to determine the existance of files, this
  89. // poses quite a challenge. Instead of creating a new meta NCA for this file, yuzu will create a
  90. // dir inside the NAND called 'yuzu_meta' and store the raw CNMT there.
  91. // TODO(DarkLordZach): Author real meta-type NCAs and install those.
  92. InstallResult InstallEntry(std::shared_ptr<NCA> nca, TitleType type,
  93. bool overwrite_if_exists = false,
  94. const VfsCopyFunction& copy = &VfsRawCopy);
  95. private:
  96. template <typename T>
  97. void IterateAllMetadata(std::vector<T>& out,
  98. std::function<T(const CNMT&, const ContentRecord&)> proc,
  99. std::function<bool(const CNMT&, const ContentRecord&)> filter) const;
  100. std::vector<NcaID> AccumulateFiles() const;
  101. void ProcessFiles(const std::vector<NcaID>& ids);
  102. void AccumulateYuzuMeta();
  103. std::optional<NcaID> GetNcaIDFromMetadata(u64 title_id, ContentRecordType type) const;
  104. VirtualFile GetFileAtID(NcaID id) const;
  105. VirtualFile OpenFileOrDirectoryConcat(const VirtualDir& dir, std::string_view path) const;
  106. InstallResult RawInstallNCA(std::shared_ptr<NCA> nca, const VfsCopyFunction& copy,
  107. bool overwrite_if_exists, std::optional<NcaID> override_id = {});
  108. bool RawInstallYuzuMeta(const CNMT& cnmt);
  109. VirtualDir dir;
  110. RegisteredCacheParsingFunction parser;
  111. // maps tid -> NcaID of meta
  112. boost::container::flat_map<u64, NcaID> meta_id;
  113. // maps tid -> meta
  114. boost::container::flat_map<u64, CNMT> meta;
  115. // maps tid -> meta for CNMT in yuzu_meta
  116. boost::container::flat_map<u64, CNMT> yuzu_meta;
  117. };
  118. // Combines multiple RegisteredCaches (i.e. SysNAND, UserNAND, SDMC) into one interface.
  119. class RegisteredCacheUnion {
  120. public:
  121. explicit RegisteredCacheUnion(std::vector<RegisteredCache*> caches);
  122. void Refresh();
  123. bool HasEntry(u64 title_id, ContentRecordType type) const;
  124. bool HasEntry(RegisteredCacheEntry entry) const;
  125. std::optional<u32> GetEntryVersion(u64 title_id) const;
  126. VirtualFile GetEntryUnparsed(u64 title_id, ContentRecordType type) const;
  127. VirtualFile GetEntryUnparsed(RegisteredCacheEntry entry) const;
  128. VirtualFile GetEntryRaw(u64 title_id, ContentRecordType type) const;
  129. VirtualFile GetEntryRaw(RegisteredCacheEntry entry) const;
  130. std::unique_ptr<NCA> GetEntry(u64 title_id, ContentRecordType type) const;
  131. std::unique_ptr<NCA> GetEntry(RegisteredCacheEntry entry) const;
  132. std::vector<RegisteredCacheEntry> ListEntries() const;
  133. // If a parameter is not std::nullopt, it will be filtered for from all entries.
  134. std::vector<RegisteredCacheEntry> ListEntriesFilter(
  135. std::optional<TitleType> title_type = {}, std::optional<ContentRecordType> record_type = {},
  136. std::optional<u64> title_id = {}) const;
  137. private:
  138. std::vector<RegisteredCache*> caches;
  139. };
  140. } // namespace FileSys