registered_cache.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_funcs.h"
  13. #include "common/common_types.h"
  14. #include "content_archive.h"
  15. #include "core/file_sys/nca_metadata.h"
  16. #include "core/file_sys/vfs.h"
  17. namespace FileSys {
  18. class XCI;
  19. class CNMT;
  20. using NcaID = std::array<u8, 0x10>;
  21. using RegisteredCacheParsingFunction = std::function<VirtualFile(const VirtualFile&, const NcaID&)>;
  22. using VfsCopyFunction = std::function<bool(VirtualFile, VirtualFile)>;
  23. enum class InstallResult {
  24. Success,
  25. ErrorAlreadyExists,
  26. ErrorCopyFailed,
  27. ErrorMetaFailed,
  28. };
  29. struct RegisteredCacheEntry {
  30. u64 title_id;
  31. ContentRecordType type;
  32. std::string DebugInfo() const;
  33. };
  34. // boost flat_map requires operator< for O(log(n)) lookups.
  35. bool operator<(const RegisteredCacheEntry& lhs, const RegisteredCacheEntry& rhs);
  36. /*
  37. * A class that catalogues NCAs in the registered directory structure.
  38. * Nintendo's registered format follows this structure:
  39. *
  40. * Root
  41. * | 000000XX <- XX is the ____ two digits of the NcaID
  42. * | <hash>.nca <- hash is the NcaID (first half of SHA256 over entire file) (folder)
  43. * | 00
  44. * | 01 <- Actual content split along 4GB boundaries. (optional)
  45. *
  46. * (This impl also supports substituting the nca dir for an nca file, as that's more convenient when
  47. * 4GB splitting can be ignored.)
  48. */
  49. class RegisteredCache {
  50. public:
  51. // Parsing function defines the conversion from raw file to NCA. If there are other steps
  52. // besides creating the NCA from the file (e.g. NAX0 on SD Card), that should go in a custom
  53. // parsing function.
  54. explicit RegisteredCache(VirtualDir dir,
  55. RegisteredCacheParsingFunction parsing_function =
  56. [](const VirtualFile& file, const NcaID& id) { return file; });
  57. void Refresh();
  58. bool HasEntry(u64 title_id, ContentRecordType type) const;
  59. bool HasEntry(RegisteredCacheEntry entry) const;
  60. VirtualFile GetEntryRaw(u64 title_id, ContentRecordType type) const;
  61. VirtualFile GetEntryRaw(RegisteredCacheEntry entry) const;
  62. std::shared_ptr<NCA> GetEntry(u64 title_id, ContentRecordType type) const;
  63. std::shared_ptr<NCA> GetEntry(RegisteredCacheEntry entry) const;
  64. std::vector<RegisteredCacheEntry> ListEntries() const;
  65. // If a parameter is not boost::none, it will be filtered for from all entries.
  66. std::vector<RegisteredCacheEntry> ListEntriesFilter(
  67. boost::optional<TitleType> title_type = boost::none,
  68. boost::optional<ContentRecordType> record_type = boost::none,
  69. boost::optional<u64> title_id = boost::none) const;
  70. // Raw copies all the ncas from the xci to the csache. Does some quick checks to make sure there
  71. // is a meta NCA and all of them are accessible.
  72. InstallResult InstallEntry(std::shared_ptr<XCI> xci, bool overwrite_if_exists = false,
  73. const VfsCopyFunction& copy = &VfsRawCopy);
  74. // Due to the fact that we must use Meta-type NCAs to determine the existance of files, this
  75. // poses quite a challenge. Instead of creating a new meta NCA for this file, yuzu will create a
  76. // dir inside the NAND called 'yuzu_meta' and store the raw CNMT there.
  77. // TODO(DarkLordZach): Author real meta-type NCAs and install those.
  78. InstallResult InstallEntry(std::shared_ptr<NCA> nca, TitleType type,
  79. bool overwrite_if_exists = false,
  80. const VfsCopyFunction& copy = &VfsRawCopy);
  81. private:
  82. template <typename T>
  83. void IterateAllMetadata(std::vector<T>& out,
  84. std::function<T(const CNMT&, const ContentRecord&)> proc,
  85. std::function<bool(const CNMT&, const ContentRecord&)> filter) const;
  86. std::vector<NcaID> AccumulateFiles() const;
  87. void ProcessFiles(const std::vector<NcaID>& ids);
  88. void AccumulateYuzuMeta();
  89. boost::optional<NcaID> GetNcaIDFromMetadata(u64 title_id, ContentRecordType type) const;
  90. VirtualFile GetFileAtID(NcaID id) const;
  91. VirtualFile OpenFileOrDirectoryConcat(const VirtualDir& dir, std::string_view path) const;
  92. InstallResult RawInstallNCA(std::shared_ptr<NCA> nca, const VfsCopyFunction& copy,
  93. bool overwrite_if_exists,
  94. boost::optional<NcaID> override_id = boost::none);
  95. bool RawInstallYuzuMeta(const CNMT& cnmt);
  96. VirtualDir dir;
  97. RegisteredCacheParsingFunction parser;
  98. // maps tid -> NcaID of meta
  99. boost::container::flat_map<u64, NcaID> meta_id;
  100. // maps tid -> meta
  101. boost::container::flat_map<u64, CNMT> meta;
  102. // maps tid -> meta for CNMT in yuzu_meta
  103. boost::container::flat_map<u64, CNMT> yuzu_meta;
  104. };
  105. } // namespace FileSys