registered_cache.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. /*
  44. * A class that catalogues NCAs in the registered directory structure.
  45. * Nintendo's registered format follows this structure:
  46. *
  47. * Root
  48. * | 000000XX <- XX is the ____ two digits of the NcaID
  49. * | <hash>.nca <- hash is the NcaID (first half of SHA256 over entire file) (folder)
  50. * | 00
  51. * | 01 <- Actual content split along 4GB boundaries. (optional)
  52. *
  53. * (This impl also supports substituting the nca dir for an nca file, as that's more convenient
  54. * when 4GB splitting can be ignored.)
  55. */
  56. class RegisteredCache {
  57. friend class RegisteredCacheUnion;
  58. public:
  59. // Parsing function defines the conversion from raw file to NCA. If there are other steps
  60. // besides creating the NCA from the file (e.g. NAX0 on SD Card), that should go in a custom
  61. // parsing function.
  62. explicit RegisteredCache(VirtualDir dir,
  63. RegisteredCacheParsingFunction parsing_function =
  64. [](const VirtualFile& file, const NcaID& id) { return file; });
  65. ~RegisteredCache();
  66. void Refresh();
  67. bool HasEntry(u64 title_id, ContentRecordType type) const;
  68. bool HasEntry(RegisteredCacheEntry entry) const;
  69. boost::optional<u32> GetEntryVersion(u64 title_id) const;
  70. VirtualFile GetEntryUnparsed(u64 title_id, ContentRecordType type) const;
  71. VirtualFile GetEntryUnparsed(RegisteredCacheEntry entry) const;
  72. VirtualFile GetEntryRaw(u64 title_id, ContentRecordType type) const;
  73. VirtualFile GetEntryRaw(RegisteredCacheEntry entry) const;
  74. std::unique_ptr<NCA> GetEntry(u64 title_id, ContentRecordType type) const;
  75. std::unique_ptr<NCA> GetEntry(RegisteredCacheEntry entry) const;
  76. std::vector<RegisteredCacheEntry> ListEntries() const;
  77. // If a parameter is not boost::none, it will be filtered for from all entries.
  78. std::vector<RegisteredCacheEntry> ListEntriesFilter(
  79. boost::optional<TitleType> title_type = boost::none,
  80. boost::optional<ContentRecordType> record_type = boost::none,
  81. boost::optional<u64> title_id = boost::none) 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. boost::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,
  108. boost::optional<NcaID> override_id = boost::none);
  109. bool RawInstallYuzuMeta(const CNMT& cnmt);
  110. VirtualDir dir;
  111. RegisteredCacheParsingFunction parser;
  112. // maps tid -> NcaID of meta
  113. boost::container::flat_map<u64, NcaID> meta_id;
  114. // maps tid -> meta
  115. boost::container::flat_map<u64, CNMT> meta;
  116. // maps tid -> meta for CNMT in yuzu_meta
  117. boost::container::flat_map<u64, CNMT> yuzu_meta;
  118. };
  119. // Combines multiple RegisteredCaches (i.e. SysNAND, UserNAND, SDMC) into one interface.
  120. class RegisteredCacheUnion {
  121. public:
  122. explicit RegisteredCacheUnion(std::vector<RegisteredCache*> caches);
  123. void Refresh();
  124. bool HasEntry(u64 title_id, ContentRecordType type) const;
  125. bool HasEntry(RegisteredCacheEntry entry) const;
  126. boost::optional<u32> GetEntryVersion(u64 title_id) const;
  127. VirtualFile GetEntryUnparsed(u64 title_id, ContentRecordType type) const;
  128. VirtualFile GetEntryUnparsed(RegisteredCacheEntry entry) const;
  129. VirtualFile GetEntryRaw(u64 title_id, ContentRecordType type) const;
  130. VirtualFile GetEntryRaw(RegisteredCacheEntry entry) const;
  131. std::unique_ptr<NCA> GetEntry(u64 title_id, ContentRecordType type) const;
  132. std::unique_ptr<NCA> GetEntry(RegisteredCacheEntry entry) const;
  133. std::vector<RegisteredCacheEntry> ListEntries() const;
  134. // If a parameter is not boost::none, it will be filtered for from all entries.
  135. std::vector<RegisteredCacheEntry> ListEntriesFilter(
  136. boost::optional<TitleType> title_type = boost::none,
  137. boost::optional<ContentRecordType> record_type = boost::none,
  138. boost::optional<u64> title_id = boost::none) const;
  139. private:
  140. std::vector<RegisteredCache*> caches;
  141. };
  142. } // namespace FileSys