2018-08-10 00:52:27 +00:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <array>
|
2018-08-10 03:10:32 +00:00
|
|
|
#include <functional>
|
2018-08-10 00:52:27 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2018-08-10 03:10:32 +00:00
|
|
|
#include <vector>
|
2018-08-10 00:52:27 +00:00
|
|
|
#include <boost/container/flat_map.hpp>
|
2018-08-10 03:10:32 +00:00
|
|
|
#include "common/common_types.h"
|
2018-11-02 00:23:38 +00:00
|
|
|
#include "core/crypto/key_manager.h"
|
2018-08-10 00:52:27 +00:00
|
|
|
#include "core/file_sys/vfs.h"
|
|
|
|
|
|
|
|
namespace FileSys {
|
|
|
|
class CNMT;
|
2018-09-04 01:58:19 +00:00
|
|
|
class NCA;
|
2018-09-04 18:44:40 +00:00
|
|
|
class NSP;
|
2018-09-04 01:58:19 +00:00
|
|
|
class XCI;
|
|
|
|
|
|
|
|
enum class ContentRecordType : u8;
|
|
|
|
enum class TitleType : u8;
|
|
|
|
|
|
|
|
struct ContentRecord;
|
2018-08-10 00:52:27 +00:00
|
|
|
|
|
|
|
using NcaID = std::array<u8, 0x10>;
|
|
|
|
using RegisteredCacheParsingFunction = std::function<VirtualFile(const VirtualFile&, const NcaID&)>;
|
2018-09-20 02:09:23 +00:00
|
|
|
using VfsCopyFunction = std::function<bool(const VirtualFile&, const VirtualFile&, size_t)>;
|
2018-08-10 00:52:27 +00:00
|
|
|
|
2018-08-12 03:01:38 +00:00
|
|
|
enum class InstallResult {
|
|
|
|
Success,
|
|
|
|
ErrorAlreadyExists,
|
|
|
|
ErrorCopyFailed,
|
|
|
|
ErrorMetaFailed,
|
|
|
|
};
|
|
|
|
|
2018-08-10 00:52:27 +00:00
|
|
|
struct RegisteredCacheEntry {
|
|
|
|
u64 title_id;
|
|
|
|
ContentRecordType type;
|
|
|
|
|
|
|
|
std::string DebugInfo() const;
|
|
|
|
};
|
|
|
|
|
2018-08-26 14:53:31 +00:00
|
|
|
constexpr u64 GetUpdateTitleID(u64 base_title_id) {
|
2018-08-25 23:00:36 +00:00
|
|
|
return base_title_id | 0x800;
|
|
|
|
}
|
|
|
|
|
2018-08-10 00:52:27 +00:00
|
|
|
// boost flat_map requires operator< for O(log(n)) lookups.
|
|
|
|
bool operator<(const RegisteredCacheEntry& lhs, const RegisteredCacheEntry& rhs);
|
|
|
|
|
2018-10-17 18:04:18 +00:00
|
|
|
// std unique requires operator== to identify duplicates.
|
|
|
|
bool operator==(const RegisteredCacheEntry& lhs, const RegisteredCacheEntry& rhs);
|
2018-10-17 22:27:23 +00:00
|
|
|
bool operator!=(const RegisteredCacheEntry& lhs, const RegisteredCacheEntry& rhs);
|
2018-10-17 18:04:18 +00:00
|
|
|
|
2018-08-10 00:52:27 +00:00
|
|
|
/*
|
|
|
|
* A class that catalogues NCAs in the registered directory structure.
|
|
|
|
* Nintendo's registered format follows this structure:
|
|
|
|
*
|
|
|
|
* Root
|
|
|
|
* | 000000XX <- XX is the ____ two digits of the NcaID
|
|
|
|
* | <hash>.nca <- hash is the NcaID (first half of SHA256 over entire file) (folder)
|
|
|
|
* | 00
|
|
|
|
* | 01 <- Actual content split along 4GB boundaries. (optional)
|
|
|
|
*
|
2018-10-17 18:04:18 +00:00
|
|
|
* (This impl also supports substituting the nca dir for an nca file, as that's more convenient
|
|
|
|
* when 4GB splitting can be ignored.)
|
2018-08-10 00:52:27 +00:00
|
|
|
*/
|
|
|
|
class RegisteredCache {
|
2018-08-25 23:00:36 +00:00
|
|
|
friend class RegisteredCacheUnion;
|
|
|
|
|
2018-08-10 00:52:27 +00:00
|
|
|
public:
|
|
|
|
// Parsing function defines the conversion from raw file to NCA. If there are other steps
|
|
|
|
// besides creating the NCA from the file (e.g. NAX0 on SD Card), that should go in a custom
|
|
|
|
// parsing function.
|
2018-08-10 03:10:32 +00:00
|
|
|
explicit RegisteredCache(VirtualDir dir,
|
|
|
|
RegisteredCacheParsingFunction parsing_function =
|
|
|
|
[](const VirtualFile& file, const NcaID& id) { return file; });
|
2018-08-19 01:16:20 +00:00
|
|
|
~RegisteredCache();
|
2018-08-10 00:52:27 +00:00
|
|
|
|
|
|
|
void Refresh();
|
|
|
|
|
|
|
|
bool HasEntry(u64 title_id, ContentRecordType type) const;
|
|
|
|
bool HasEntry(RegisteredCacheEntry entry) const;
|
|
|
|
|
2018-10-30 04:03:25 +00:00
|
|
|
std::optional<u32> GetEntryVersion(u64 title_id) const;
|
2018-08-25 23:00:36 +00:00
|
|
|
|
2018-08-16 21:07:37 +00:00
|
|
|
VirtualFile GetEntryUnparsed(u64 title_id, ContentRecordType type) const;
|
|
|
|
VirtualFile GetEntryUnparsed(RegisteredCacheEntry entry) const;
|
|
|
|
|
2018-08-10 00:52:27 +00:00
|
|
|
VirtualFile GetEntryRaw(u64 title_id, ContentRecordType type) const;
|
|
|
|
VirtualFile GetEntryRaw(RegisteredCacheEntry entry) const;
|
|
|
|
|
2018-10-16 13:05:47 +00:00
|
|
|
std::unique_ptr<NCA> GetEntry(u64 title_id, ContentRecordType type) const;
|
|
|
|
std::unique_ptr<NCA> GetEntry(RegisteredCacheEntry entry) const;
|
2018-08-10 00:52:27 +00:00
|
|
|
|
|
|
|
std::vector<RegisteredCacheEntry> ListEntries() const;
|
2018-10-30 04:03:25 +00:00
|
|
|
// If a parameter is not std::nullopt, it will be filtered for from all entries.
|
2018-08-10 00:52:27 +00:00
|
|
|
std::vector<RegisteredCacheEntry> ListEntriesFilter(
|
2018-10-30 04:03:25 +00:00
|
|
|
std::optional<TitleType> title_type = {}, std::optional<ContentRecordType> record_type = {},
|
|
|
|
std::optional<u64> title_id = {}) const;
|
2018-08-10 00:52:27 +00:00
|
|
|
|
2018-08-25 15:50:04 +00:00
|
|
|
// Raw copies all the ncas from the xci/nsp to the csache. Does some quick checks to make sure
|
|
|
|
// there is a meta NCA and all of them are accessible.
|
2018-11-27 18:52:13 +00:00
|
|
|
InstallResult InstallEntry(const XCI& xci, bool overwrite_if_exists = false,
|
2018-08-12 03:01:38 +00:00
|
|
|
const VfsCopyFunction& copy = &VfsRawCopy);
|
2018-11-27 18:52:13 +00:00
|
|
|
InstallResult InstallEntry(const NSP& nsp, bool overwrite_if_exists = false,
|
2018-08-25 15:50:04 +00:00
|
|
|
const VfsCopyFunction& copy = &VfsRawCopy);
|
2018-08-10 00:52:27 +00:00
|
|
|
|
|
|
|
// Due to the fact that we must use Meta-type NCAs to determine the existance of files, this
|
|
|
|
// poses quite a challenge. Instead of creating a new meta NCA for this file, yuzu will create a
|
|
|
|
// dir inside the NAND called 'yuzu_meta' and store the raw CNMT there.
|
|
|
|
// TODO(DarkLordZach): Author real meta-type NCAs and install those.
|
2018-11-27 18:52:13 +00:00
|
|
|
InstallResult InstallEntry(const NCA& nca, TitleType type, bool overwrite_if_exists = false,
|
2018-08-12 03:01:38 +00:00
|
|
|
const VfsCopyFunction& copy = &VfsRawCopy);
|
2018-08-10 00:52:27 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename T>
|
|
|
|
void IterateAllMetadata(std::vector<T>& out,
|
|
|
|
std::function<T(const CNMT&, const ContentRecord&)> proc,
|
|
|
|
std::function<bool(const CNMT&, const ContentRecord&)> filter) const;
|
2018-08-10 03:10:32 +00:00
|
|
|
std::vector<NcaID> AccumulateFiles() const;
|
2018-08-10 00:52:27 +00:00
|
|
|
void ProcessFiles(const std::vector<NcaID>& ids);
|
|
|
|
void AccumulateYuzuMeta();
|
2018-10-30 04:03:25 +00:00
|
|
|
std::optional<NcaID> GetNcaIDFromMetadata(u64 title_id, ContentRecordType type) const;
|
2018-08-10 00:52:27 +00:00
|
|
|
VirtualFile GetFileAtID(NcaID id) const;
|
|
|
|
VirtualFile OpenFileOrDirectoryConcat(const VirtualDir& dir, std::string_view path) const;
|
2018-11-27 18:52:13 +00:00
|
|
|
InstallResult RawInstallNCA(const NCA& nca, const VfsCopyFunction& copy,
|
2018-10-30 04:03:25 +00:00
|
|
|
bool overwrite_if_exists, std::optional<NcaID> override_id = {});
|
2018-08-10 00:52:27 +00:00
|
|
|
bool RawInstallYuzuMeta(const CNMT& cnmt);
|
|
|
|
|
|
|
|
VirtualDir dir;
|
|
|
|
RegisteredCacheParsingFunction parser;
|
2018-11-02 00:23:38 +00:00
|
|
|
Core::Crypto::KeyManager keys;
|
|
|
|
|
2018-08-10 00:52:27 +00:00
|
|
|
// maps tid -> NcaID of meta
|
|
|
|
boost::container::flat_map<u64, NcaID> meta_id;
|
|
|
|
// maps tid -> meta
|
|
|
|
boost::container::flat_map<u64, CNMT> meta;
|
|
|
|
// maps tid -> meta for CNMT in yuzu_meta
|
|
|
|
boost::container::flat_map<u64, CNMT> yuzu_meta;
|
|
|
|
};
|
|
|
|
|
2018-08-25 23:00:36 +00:00
|
|
|
// Combines multiple RegisteredCaches (i.e. SysNAND, UserNAND, SDMC) into one interface.
|
|
|
|
class RegisteredCacheUnion {
|
|
|
|
public:
|
2018-10-16 13:05:47 +00:00
|
|
|
explicit RegisteredCacheUnion(std::vector<RegisteredCache*> caches);
|
2018-08-25 23:00:36 +00:00
|
|
|
|
|
|
|
void Refresh();
|
|
|
|
|
|
|
|
bool HasEntry(u64 title_id, ContentRecordType type) const;
|
|
|
|
bool HasEntry(RegisteredCacheEntry entry) const;
|
|
|
|
|
2018-10-30 04:03:25 +00:00
|
|
|
std::optional<u32> GetEntryVersion(u64 title_id) const;
|
2018-08-25 23:00:36 +00:00
|
|
|
|
|
|
|
VirtualFile GetEntryUnparsed(u64 title_id, ContentRecordType type) const;
|
|
|
|
VirtualFile GetEntryUnparsed(RegisteredCacheEntry entry) const;
|
|
|
|
|
|
|
|
VirtualFile GetEntryRaw(u64 title_id, ContentRecordType type) const;
|
|
|
|
VirtualFile GetEntryRaw(RegisteredCacheEntry entry) const;
|
|
|
|
|
2018-10-16 13:05:47 +00:00
|
|
|
std::unique_ptr<NCA> GetEntry(u64 title_id, ContentRecordType type) const;
|
|
|
|
std::unique_ptr<NCA> GetEntry(RegisteredCacheEntry entry) const;
|
2018-08-25 23:00:36 +00:00
|
|
|
|
|
|
|
std::vector<RegisteredCacheEntry> ListEntries() const;
|
2018-10-30 04:03:25 +00:00
|
|
|
// If a parameter is not std::nullopt, it will be filtered for from all entries.
|
2018-08-25 23:00:36 +00:00
|
|
|
std::vector<RegisteredCacheEntry> ListEntriesFilter(
|
2018-10-30 04:03:25 +00:00
|
|
|
std::optional<TitleType> title_type = {}, std::optional<ContentRecordType> record_type = {},
|
|
|
|
std::optional<u64> title_id = {}) const;
|
2018-08-25 23:00:36 +00:00
|
|
|
|
|
|
|
private:
|
2018-10-16 13:05:47 +00:00
|
|
|
std::vector<RegisteredCache*> caches;
|
2018-08-25 23:00:36 +00:00
|
|
|
};
|
|
|
|
|
2018-08-10 00:52:27 +00:00
|
|
|
} // namespace FileSys
|