2018-08-10 02:52:27 +02:00
|
|
|
// Copyright 2018 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-10-17 20:04:18 +02:00
|
|
|
#include <algorithm>
|
2019-04-22 23:56:56 +02:00
|
|
|
#include <random>
|
2018-08-10 02:52:27 +02:00
|
|
|
#include <regex>
|
|
|
|
#include <mbedtls/sha256.h>
|
|
|
|
#include "common/assert.h"
|
2018-09-02 16:53:06 +02:00
|
|
|
#include "common/file_util.h"
|
2018-08-10 02:52:27 +02:00
|
|
|
#include "common/hex_util.h"
|
|
|
|
#include "common/logging/log.h"
|
2018-09-04 03:58:19 +02:00
|
|
|
#include "core/crypto/key_manager.h"
|
2018-08-10 02:52:27 +02:00
|
|
|
#include "core/file_sys/card_image.h"
|
2018-09-04 03:58:19 +02:00
|
|
|
#include "core/file_sys/content_archive.h"
|
2018-08-10 02:52:27 +02:00
|
|
|
#include "core/file_sys/nca_metadata.h"
|
|
|
|
#include "core/file_sys/registered_cache.h"
|
2018-09-04 20:44:40 +02:00
|
|
|
#include "core/file_sys/submission_package.h"
|
2018-08-10 02:52:27 +02:00
|
|
|
#include "core/file_sys/vfs_concat.h"
|
2018-09-04 03:58:19 +02:00
|
|
|
#include "core/loader/loader.h"
|
2018-08-10 02:52:27 +02:00
|
|
|
|
|
|
|
namespace FileSys {
|
2018-09-24 03:50:16 +02:00
|
|
|
|
|
|
|
// The size of blocks to use when vfs raw copying into nand.
|
|
|
|
constexpr size_t VFS_RC_LARGE_COPY_BLOCK = 0x400000;
|
|
|
|
|
2018-12-28 05:58:18 +01:00
|
|
|
std::string ContentProviderEntry::DebugInfo() const {
|
2018-08-10 02:52:27 +02:00
|
|
|
return fmt::format("title_id={:016X}, content_type={:02X}", title_id, static_cast<u8>(type));
|
|
|
|
}
|
|
|
|
|
2018-12-28 05:58:18 +01:00
|
|
|
bool operator<(const ContentProviderEntry& lhs, const ContentProviderEntry& rhs) {
|
2018-08-10 02:52:27 +02:00
|
|
|
return (lhs.title_id < rhs.title_id) || (lhs.title_id == rhs.title_id && lhs.type < rhs.type);
|
|
|
|
}
|
|
|
|
|
2018-12-28 05:58:18 +01:00
|
|
|
bool operator==(const ContentProviderEntry& lhs, const ContentProviderEntry& rhs) {
|
2018-10-17 20:04:18 +02:00
|
|
|
return std::tie(lhs.title_id, lhs.type) == std::tie(rhs.title_id, rhs.type);
|
|
|
|
}
|
|
|
|
|
2018-12-28 05:58:18 +01:00
|
|
|
bool operator!=(const ContentProviderEntry& lhs, const ContentProviderEntry& rhs) {
|
2018-10-18 00:27:23 +02:00
|
|
|
return !operator==(lhs, rhs);
|
|
|
|
}
|
|
|
|
|
2018-08-10 02:52:27 +02:00
|
|
|
static bool FollowsTwoDigitDirFormat(std::string_view name) {
|
2018-08-11 21:39:09 +02:00
|
|
|
static const std::regex two_digit_regex("000000[0-9A-F]{2}", std::regex_constants::ECMAScript |
|
|
|
|
std::regex_constants::icase);
|
2018-08-10 02:52:27 +02:00
|
|
|
return std::regex_match(name.begin(), name.end(), two_digit_regex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool FollowsNcaIdFormat(std::string_view name) {
|
2018-08-12 21:55:44 +02:00
|
|
|
static const std::regex nca_id_regex("[0-9A-F]{32}\\.nca", std::regex_constants::ECMAScript |
|
|
|
|
std::regex_constants::icase);
|
2019-04-10 18:24:44 +02:00
|
|
|
static const std::regex nca_id_cnmt_regex(
|
|
|
|
"[0-9A-F]{32}\\.cnmt.nca", std::regex_constants::ECMAScript | std::regex_constants::icase);
|
|
|
|
return (name.size() == 36 && std::regex_match(name.begin(), name.end(), nca_id_regex)) ||
|
|
|
|
(name.size() == 41 && std::regex_match(name.begin(), name.end(), nca_id_cnmt_regex));
|
2018-08-10 02:52:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::string GetRelativePathFromNcaID(const std::array<u8, 16>& nca_id, bool second_hex_upper,
|
2019-04-10 18:24:44 +02:00
|
|
|
bool within_two_digit, bool cnmt_suffix) {
|
|
|
|
if (!within_two_digit)
|
|
|
|
return fmt::format(cnmt_suffix ? "{}.cnmt.nca" : "/{}.nca",
|
2019-09-22 00:43:11 +02:00
|
|
|
Common::HexToString(nca_id, second_hex_upper));
|
2018-08-10 02:52:27 +02:00
|
|
|
|
|
|
|
Core::Crypto::SHA256Hash hash{};
|
|
|
|
mbedtls_sha256(nca_id.data(), nca_id.size(), hash.data(), 0);
|
2019-04-10 18:24:44 +02:00
|
|
|
return fmt::format(cnmt_suffix ? "/000000{:02X}/{}.cnmt.nca" : "/000000{:02X}/{}.nca", hash[0],
|
2019-09-22 00:43:11 +02:00
|
|
|
Common::HexToString(nca_id, second_hex_upper));
|
2018-08-10 02:52:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::string GetCNMTName(TitleType type, u64 title_id) {
|
|
|
|
constexpr std::array<const char*, 9> TITLE_TYPE_NAMES{
|
|
|
|
"SystemProgram",
|
|
|
|
"SystemData",
|
|
|
|
"SystemUpdate",
|
|
|
|
"BootImagePackage",
|
|
|
|
"BootImagePackageSafe",
|
|
|
|
"Application",
|
|
|
|
"Patch",
|
|
|
|
"AddOnContent",
|
|
|
|
"" ///< Currently unknown 'DeltaTitle'
|
|
|
|
};
|
|
|
|
|
2018-09-15 15:21:06 +02:00
|
|
|
auto index = static_cast<std::size_t>(type);
|
2018-08-11 21:39:09 +02:00
|
|
|
// If the index is after the jump in TitleType, subtract it out.
|
2018-09-15 15:21:06 +02:00
|
|
|
if (index >= static_cast<std::size_t>(TitleType::Application)) {
|
|
|
|
index -= static_cast<std::size_t>(TitleType::Application) -
|
|
|
|
static_cast<std::size_t>(TitleType::FirmwarePackageB);
|
2018-08-12 21:55:44 +02:00
|
|
|
}
|
2018-08-10 02:52:27 +02:00
|
|
|
return fmt::format("{}_{:016x}.cnmt", TITLE_TYPE_NAMES[index], title_id);
|
|
|
|
}
|
|
|
|
|
2018-12-28 05:58:18 +01:00
|
|
|
ContentRecordType GetCRTypeFromNCAType(NCAContentType type) {
|
2018-08-10 02:52:27 +02:00
|
|
|
switch (type) {
|
|
|
|
case NCAContentType::Program:
|
|
|
|
// TODO(DarkLordZach): Differentiate between Program and Patch
|
|
|
|
return ContentRecordType::Program;
|
|
|
|
case NCAContentType::Meta:
|
|
|
|
return ContentRecordType::Meta;
|
|
|
|
case NCAContentType::Control:
|
|
|
|
return ContentRecordType::Control;
|
|
|
|
case NCAContentType::Data:
|
2019-03-19 20:14:52 +01:00
|
|
|
case NCAContentType::PublicData:
|
2018-08-10 02:52:27 +02:00
|
|
|
return ContentRecordType::Data;
|
|
|
|
case NCAContentType::Manual:
|
|
|
|
// TODO(DarkLordZach): Peek at NCA contents to differentiate Manual and Legal.
|
2019-07-02 01:57:23 +02:00
|
|
|
return ContentRecordType::HtmlDocument;
|
2018-08-10 02:52:27 +02:00
|
|
|
default:
|
2018-08-19 03:28:17 +02:00
|
|
|
UNREACHABLE_MSG("Invalid NCAContentType={:02X}", static_cast<u8>(type));
|
2018-08-10 02:52:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 05:58:18 +01:00
|
|
|
ContentProvider::~ContentProvider() = default;
|
|
|
|
|
|
|
|
bool ContentProvider::HasEntry(ContentProviderEntry entry) const {
|
|
|
|
return HasEntry(entry.title_id, entry.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile ContentProvider::GetEntryUnparsed(ContentProviderEntry entry) const {
|
|
|
|
return GetEntryUnparsed(entry.title_id, entry.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile ContentProvider::GetEntryRaw(ContentProviderEntry entry) const {
|
|
|
|
return GetEntryRaw(entry.title_id, entry.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<NCA> ContentProvider::GetEntry(ContentProviderEntry entry) const {
|
|
|
|
return GetEntry(entry.title_id, entry.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ContentProviderEntry> ContentProvider::ListEntries() const {
|
|
|
|
return ListEntriesFilter(std::nullopt, std::nullopt, std::nullopt);
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:21:51 +02:00
|
|
|
PlaceholderCache::PlaceholderCache(VirtualDir dir_) : dir(std::move(dir_)) {}
|
|
|
|
|
|
|
|
bool PlaceholderCache::Create(const NcaID& id, u64 size) const {
|
|
|
|
const auto path = GetRelativePathFromNcaID(id, false, true, false);
|
|
|
|
|
|
|
|
if (dir->GetFileRelative(path) != nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Core::Crypto::SHA256Hash hash{};
|
|
|
|
mbedtls_sha256(id.data(), id.size(), hash.data(), 0);
|
|
|
|
const auto dirname = fmt::format("000000{:02X}", hash[0]);
|
|
|
|
|
|
|
|
const auto dir2 = GetOrCreateDirectoryRelative(dir, dirname);
|
|
|
|
|
|
|
|
if (dir2 == nullptr)
|
|
|
|
return false;
|
|
|
|
|
2019-09-22 00:43:11 +02:00
|
|
|
const auto file = dir2->CreateFile(fmt::format("{}.nca", Common::HexToString(id, false)));
|
2019-04-10 18:21:51 +02:00
|
|
|
|
|
|
|
if (file == nullptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return file->Resize(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PlaceholderCache::Delete(const NcaID& id) const {
|
|
|
|
const auto path = GetRelativePathFromNcaID(id, false, true, false);
|
|
|
|
|
|
|
|
if (dir->GetFileRelative(path) == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Core::Crypto::SHA256Hash hash{};
|
|
|
|
mbedtls_sha256(id.data(), id.size(), hash.data(), 0);
|
|
|
|
const auto dirname = fmt::format("000000{:02X}", hash[0]);
|
|
|
|
|
|
|
|
const auto dir2 = GetOrCreateDirectoryRelative(dir, dirname);
|
|
|
|
|
2019-09-22 00:43:11 +02:00
|
|
|
const auto res = dir2->DeleteFile(fmt::format("{}.nca", Common::HexToString(id, false)));
|
2019-04-10 18:21:51 +02:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PlaceholderCache::Exists(const NcaID& id) const {
|
|
|
|
const auto path = GetRelativePathFromNcaID(id, false, true, false);
|
|
|
|
|
|
|
|
return dir->GetFileRelative(path) != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PlaceholderCache::Write(const NcaID& id, u64 offset, const std::vector<u8>& data) const {
|
|
|
|
const auto path = GetRelativePathFromNcaID(id, false, true, false);
|
|
|
|
const auto file = dir->GetFileRelative(path);
|
|
|
|
|
|
|
|
if (file == nullptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return file->WriteBytes(data, offset) == data.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PlaceholderCache::Register(RegisteredCache* cache, const NcaID& placeholder,
|
|
|
|
const NcaID& install) const {
|
|
|
|
const auto path = GetRelativePathFromNcaID(placeholder, false, true, false);
|
|
|
|
const auto file = dir->GetFileRelative(path);
|
|
|
|
|
|
|
|
if (file == nullptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const auto res = cache->RawInstallNCA(NCA{file}, &VfsRawCopy, false, install);
|
|
|
|
|
|
|
|
if (res != InstallResult::Success)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return Delete(placeholder);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PlaceholderCache::CleanAll() const {
|
|
|
|
return dir->GetParentDirectory()->CleanSubdirectoryRecursive(dir->GetName());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::array<u8, 0x10>> PlaceholderCache::GetRightsID(const NcaID& id) const {
|
|
|
|
const auto path = GetRelativePathFromNcaID(id, false, true, false);
|
|
|
|
const auto file = dir->GetFileRelative(path);
|
|
|
|
|
|
|
|
if (file == nullptr)
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
NCA nca{file};
|
|
|
|
|
|
|
|
if (nca.GetStatus() != Loader::ResultStatus::Success &&
|
|
|
|
nca.GetStatus() != Loader::ResultStatus::ErrorMissingBKTRBaseRomFS) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto rights_id = nca.GetRightsId();
|
|
|
|
if (rights_id == NcaID{})
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
return rights_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 PlaceholderCache::Size(const NcaID& id) const {
|
|
|
|
const auto path = GetRelativePathFromNcaID(id, false, true, false);
|
|
|
|
const auto file = dir->GetFileRelative(path);
|
|
|
|
|
|
|
|
if (file == nullptr)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return file->GetSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PlaceholderCache::SetSize(const NcaID& id, u64 new_size) const {
|
|
|
|
const auto path = GetRelativePathFromNcaID(id, false, true, false);
|
|
|
|
const auto file = dir->GetFileRelative(path);
|
|
|
|
|
|
|
|
if (file == nullptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return file->Resize(new_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<NcaID> PlaceholderCache::List() const {
|
|
|
|
std::vector<NcaID> out;
|
|
|
|
for (const auto& sdir : dir->GetSubdirectories()) {
|
|
|
|
for (const auto& file : sdir->GetFiles()) {
|
|
|
|
const auto name = file->GetName();
|
|
|
|
if (name.length() == 36 && name[32] == '.' && name[33] == 'n' && name[34] == 'c' &&
|
|
|
|
name[35] == 'a') {
|
|
|
|
out.push_back(Common::HexStringToArray<0x10>(name.substr(0, 32)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
NcaID PlaceholderCache::Generate() {
|
|
|
|
std::random_device device;
|
|
|
|
std::mt19937 gen(device());
|
|
|
|
std::uniform_int_distribution<u64> distribution(1, std::numeric_limits<u64>::max());
|
|
|
|
|
|
|
|
NcaID out{};
|
|
|
|
|
|
|
|
const auto v1 = distribution(gen);
|
|
|
|
const auto v2 = distribution(gen);
|
|
|
|
std::memcpy(out.data(), &v1, sizeof(u64));
|
|
|
|
std::memcpy(out.data() + sizeof(u64), &v2, sizeof(u64));
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-08-10 02:52:27 +02:00
|
|
|
VirtualFile RegisteredCache::OpenFileOrDirectoryConcat(const VirtualDir& dir,
|
|
|
|
std::string_view path) const {
|
2018-11-02 01:23:38 +01:00
|
|
|
const auto file = dir->GetFileRelative(path);
|
2018-12-02 05:50:10 +01:00
|
|
|
if (file != nullptr) {
|
2018-11-02 01:23:38 +01:00
|
|
|
return file;
|
2018-12-02 05:50:10 +01:00
|
|
|
}
|
2018-11-02 01:23:38 +01:00
|
|
|
|
|
|
|
const auto nca_dir = dir->GetDirectoryRelative(path);
|
2018-12-02 05:50:10 +01:00
|
|
|
if (nca_dir == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-08-10 02:52:27 +02:00
|
|
|
|
2018-12-02 05:50:10 +01:00
|
|
|
const auto files = nca_dir->GetFiles();
|
|
|
|
if (files.size() == 1 && files[0]->GetName() == "00") {
|
|
|
|
return files[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<VirtualFile> concat;
|
|
|
|
// Since the files are a two-digit hex number, max is FF.
|
|
|
|
for (std::size_t i = 0; i < 0x100; ++i) {
|
|
|
|
auto next = nca_dir->GetFile(fmt::format("{:02X}", i));
|
|
|
|
if (next != nullptr) {
|
|
|
|
concat.push_back(std::move(next));
|
2018-08-10 05:10:32 +02:00
|
|
|
} else {
|
2018-12-02 05:50:10 +01:00
|
|
|
next = nca_dir->GetFile(fmt::format("{:02x}", i));
|
|
|
|
if (next != nullptr) {
|
|
|
|
concat.push_back(std::move(next));
|
|
|
|
} else {
|
|
|
|
break;
|
2018-08-10 02:52:27 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-02 05:50:10 +01:00
|
|
|
}
|
2018-08-10 02:52:27 +02:00
|
|
|
|
2018-12-02 05:50:10 +01:00
|
|
|
if (concat.empty()) {
|
|
|
|
return nullptr;
|
2018-08-10 02:52:27 +02:00
|
|
|
}
|
2018-12-02 05:50:10 +01:00
|
|
|
|
|
|
|
return ConcatenatedVfsFile::MakeConcatenatedFile(concat, concat.front()->GetName());
|
2018-08-10 02:52:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile RegisteredCache::GetFileAtID(NcaID id) const {
|
|
|
|
VirtualFile file;
|
2019-04-10 18:24:44 +02:00
|
|
|
// Try all five relevant modes of file storage:
|
|
|
|
// (bit 2 = uppercase/lower, bit 1 = within a two-digit dir, bit 0 = .cnmt suffix)
|
|
|
|
// 000: /000000**/{:032X}.nca
|
|
|
|
// 010: /{:032X}.nca
|
|
|
|
// 100: /000000**/{:032x}.nca
|
|
|
|
// 110: /{:032x}.nca
|
|
|
|
// 111: /{:032x}.cnmt.nca
|
|
|
|
for (u8 i = 0; i < 8; ++i) {
|
|
|
|
if ((i % 2) == 1 && i != 7)
|
|
|
|
continue;
|
|
|
|
const auto path =
|
|
|
|
GetRelativePathFromNcaID(id, (i & 0b100) == 0, (i & 0b010) == 0, (i & 0b001) == 0b001);
|
2018-08-11 21:39:09 +02:00
|
|
|
file = OpenFileOrDirectoryConcat(dir, path);
|
2018-08-10 02:52:27 +02:00
|
|
|
if (file != nullptr)
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2018-12-28 05:58:18 +01:00
|
|
|
static std::optional<NcaID> CheckMapForContentRecord(const std::map<u64, CNMT>& map, u64 title_id,
|
|
|
|
ContentRecordType type) {
|
2018-08-10 17:10:44 +02:00
|
|
|
if (map.find(title_id) == map.end())
|
2018-10-30 05:03:25 +01:00
|
|
|
return {};
|
2018-08-10 02:52:27 +02:00
|
|
|
|
2018-08-10 17:10:44 +02:00
|
|
|
const auto& cnmt = map.at(title_id);
|
2018-08-10 02:52:27 +02:00
|
|
|
|
|
|
|
const auto iter = std::find_if(cnmt.GetContentRecords().begin(), cnmt.GetContentRecords().end(),
|
|
|
|
[type](const ContentRecord& rec) { return rec.type == type; });
|
|
|
|
if (iter == cnmt.GetContentRecords().end())
|
2018-10-30 05:03:25 +01:00
|
|
|
return {};
|
2018-08-10 02:52:27 +02:00
|
|
|
|
2018-10-30 05:03:25 +01:00
|
|
|
return std::make_optional(iter->nca_id);
|
2018-08-10 02:52:27 +02:00
|
|
|
}
|
|
|
|
|
2018-10-30 05:03:25 +01:00
|
|
|
std::optional<NcaID> RegisteredCache::GetNcaIDFromMetadata(u64 title_id,
|
|
|
|
ContentRecordType type) const {
|
2018-08-10 17:10:44 +02:00
|
|
|
if (type == ContentRecordType::Meta && meta_id.find(title_id) != meta_id.end())
|
|
|
|
return meta_id.at(title_id);
|
|
|
|
|
|
|
|
const auto res1 = CheckMapForContentRecord(yuzu_meta, title_id, type);
|
2018-10-30 05:03:25 +01:00
|
|
|
if (res1)
|
2018-08-10 17:10:44 +02:00
|
|
|
return res1;
|
|
|
|
return CheckMapForContentRecord(meta, title_id, type);
|
|
|
|
}
|
|
|
|
|
2018-08-10 05:10:32 +02:00
|
|
|
std::vector<NcaID> RegisteredCache::AccumulateFiles() const {
|
|
|
|
std::vector<NcaID> ids;
|
2018-08-10 02:52:27 +02:00
|
|
|
for (const auto& d2_dir : dir->GetSubdirectories()) {
|
|
|
|
if (FollowsNcaIdFormat(d2_dir->GetName())) {
|
2018-08-16 05:16:11 +02:00
|
|
|
ids.push_back(Common::HexStringToArray<0x10, true>(d2_dir->GetName().substr(0, 0x20)));
|
2018-08-10 02:52:27 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!FollowsTwoDigitDirFormat(d2_dir->GetName()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (const auto& nca_dir : d2_dir->GetSubdirectories()) {
|
|
|
|
if (!FollowsNcaIdFormat(nca_dir->GetName()))
|
|
|
|
continue;
|
|
|
|
|
2018-08-16 05:16:11 +02:00
|
|
|
ids.push_back(Common::HexStringToArray<0x10, true>(nca_dir->GetName().substr(0, 0x20)));
|
2018-08-10 02:52:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& nca_file : d2_dir->GetFiles()) {
|
|
|
|
if (!FollowsNcaIdFormat(nca_file->GetName()))
|
|
|
|
continue;
|
|
|
|
|
2018-08-16 05:16:11 +02:00
|
|
|
ids.push_back(
|
|
|
|
Common::HexStringToArray<0x10, true>(nca_file->GetName().substr(0, 0x20)));
|
2018-08-10 02:52:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& d2_file : dir->GetFiles()) {
|
|
|
|
if (FollowsNcaIdFormat(d2_file->GetName()))
|
2018-08-16 05:16:11 +02:00
|
|
|
ids.push_back(Common::HexStringToArray<0x10, true>(d2_file->GetName().substr(0, 0x20)));
|
2018-08-10 02:52:27 +02:00
|
|
|
}
|
2018-08-10 05:10:32 +02:00
|
|
|
return ids;
|
2018-08-10 02:52:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RegisteredCache::ProcessFiles(const std::vector<NcaID>& ids) {
|
|
|
|
for (const auto& id : ids) {
|
|
|
|
const auto file = GetFileAtID(id);
|
|
|
|
|
|
|
|
if (file == nullptr)
|
|
|
|
continue;
|
2018-11-02 01:23:38 +01:00
|
|
|
const auto nca = std::make_shared<NCA>(parser(file, id), nullptr, 0, keys);
|
2018-08-10 02:52:27 +02:00
|
|
|
if (nca->GetStatus() != Loader::ResultStatus::Success ||
|
2018-08-10 05:10:32 +02:00
|
|
|
nca->GetType() != NCAContentType::Meta) {
|
2018-08-10 02:52:27 +02:00
|
|
|
continue;
|
2018-08-10 05:10:32 +02:00
|
|
|
}
|
2018-08-10 02:52:27 +02:00
|
|
|
|
|
|
|
const auto section0 = nca->GetSubdirectories()[0];
|
|
|
|
|
2018-08-27 21:55:43 +02:00
|
|
|
for (const auto& section0_file : section0->GetFiles()) {
|
|
|
|
if (section0_file->GetExtension() != "cnmt")
|
2018-08-10 02:52:27 +02:00
|
|
|
continue;
|
|
|
|
|
2018-08-27 21:55:43 +02:00
|
|
|
meta.insert_or_assign(nca->GetTitleId(), CNMT(section0_file));
|
2018-08-10 02:52:27 +02:00
|
|
|
meta_id.insert_or_assign(nca->GetTitleId(), id);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegisteredCache::AccumulateYuzuMeta() {
|
|
|
|
const auto dir = this->dir->GetSubdirectory("yuzu_meta");
|
|
|
|
if (dir == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (const auto& file : dir->GetFiles()) {
|
|
|
|
if (file->GetExtension() != "cnmt")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CNMT cnmt(file);
|
|
|
|
yuzu_meta.insert_or_assign(cnmt.GetTitleID(), std::move(cnmt));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegisteredCache::Refresh() {
|
|
|
|
if (dir == nullptr)
|
|
|
|
return;
|
2018-08-10 05:10:32 +02:00
|
|
|
const auto ids = AccumulateFiles();
|
2018-08-10 02:52:27 +02:00
|
|
|
ProcessFiles(ids);
|
|
|
|
AccumulateYuzuMeta();
|
|
|
|
}
|
|
|
|
|
2018-12-28 05:58:18 +01:00
|
|
|
RegisteredCache::RegisteredCache(VirtualDir dir_, ContentProviderParsingFunction parsing_function)
|
2018-08-10 02:52:27 +02:00
|
|
|
: dir(std::move(dir_)), parser(std::move(parsing_function)) {
|
|
|
|
Refresh();
|
|
|
|
}
|
|
|
|
|
2018-08-19 03:16:20 +02:00
|
|
|
RegisteredCache::~RegisteredCache() = default;
|
|
|
|
|
2018-08-10 02:52:27 +02:00
|
|
|
bool RegisteredCache::HasEntry(u64 title_id, ContentRecordType type) const {
|
|
|
|
return GetEntryRaw(title_id, type) != nullptr;
|
|
|
|
}
|
|
|
|
|
2018-08-16 23:07:37 +02:00
|
|
|
VirtualFile RegisteredCache::GetEntryUnparsed(u64 title_id, ContentRecordType type) const {
|
|
|
|
const auto id = GetNcaIDFromMetadata(title_id, type);
|
2018-10-30 05:03:25 +01:00
|
|
|
return id ? GetFileAtID(*id) : nullptr;
|
2018-08-16 23:07:37 +02:00
|
|
|
}
|
|
|
|
|
2018-10-30 05:03:25 +01:00
|
|
|
std::optional<u32> RegisteredCache::GetEntryVersion(u64 title_id) const {
|
2018-08-26 16:53:31 +02:00
|
|
|
const auto meta_iter = meta.find(title_id);
|
|
|
|
if (meta_iter != meta.end())
|
|
|
|
return meta_iter->second.GetTitleVersion();
|
|
|
|
|
|
|
|
const auto yuzu_meta_iter = yuzu_meta.find(title_id);
|
|
|
|
if (yuzu_meta_iter != yuzu_meta.end())
|
|
|
|
return yuzu_meta_iter->second.GetTitleVersion();
|
|
|
|
|
2018-10-30 05:03:25 +01:00
|
|
|
return {};
|
2018-08-26 01:00:36 +02:00
|
|
|
}
|
|
|
|
|
2018-08-10 02:52:27 +02:00
|
|
|
VirtualFile RegisteredCache::GetEntryRaw(u64 title_id, ContentRecordType type) const {
|
|
|
|
const auto id = GetNcaIDFromMetadata(title_id, type);
|
2018-10-30 05:03:25 +01:00
|
|
|
return id ? parser(GetFileAtID(*id), *id) : nullptr;
|
2018-08-10 02:52:27 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:05:47 +02:00
|
|
|
std::unique_ptr<NCA> RegisteredCache::GetEntry(u64 title_id, ContentRecordType type) const {
|
2018-08-10 02:52:27 +02:00
|
|
|
const auto raw = GetEntryRaw(title_id, type);
|
|
|
|
if (raw == nullptr)
|
|
|
|
return nullptr;
|
2018-11-02 01:23:38 +01:00
|
|
|
return std::make_unique<NCA>(raw, nullptr, 0, keys);
|
2018-08-10 02:52:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void RegisteredCache::IterateAllMetadata(
|
|
|
|
std::vector<T>& out, std::function<T(const CNMT&, const ContentRecord&)> proc,
|
|
|
|
std::function<bool(const CNMT&, const ContentRecord&)> filter) const {
|
|
|
|
for (const auto& kv : meta) {
|
|
|
|
const auto& cnmt = kv.second;
|
|
|
|
if (filter(cnmt, EMPTY_META_CONTENT_RECORD))
|
|
|
|
out.push_back(proc(cnmt, EMPTY_META_CONTENT_RECORD));
|
|
|
|
for (const auto& rec : cnmt.GetContentRecords()) {
|
|
|
|
if (GetFileAtID(rec.nca_id) != nullptr && filter(cnmt, rec)) {
|
|
|
|
out.push_back(proc(cnmt, rec));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const auto& kv : yuzu_meta) {
|
|
|
|
const auto& cnmt = kv.second;
|
|
|
|
for (const auto& rec : cnmt.GetContentRecords()) {
|
|
|
|
if (GetFileAtID(rec.nca_id) != nullptr && filter(cnmt, rec)) {
|
|
|
|
out.push_back(proc(cnmt, rec));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 05:58:18 +01:00
|
|
|
std::vector<ContentProviderEntry> RegisteredCache::ListEntriesFilter(
|
2018-10-30 05:03:25 +01:00
|
|
|
std::optional<TitleType> title_type, std::optional<ContentRecordType> record_type,
|
|
|
|
std::optional<u64> title_id) const {
|
2018-12-28 05:58:18 +01:00
|
|
|
std::vector<ContentProviderEntry> out;
|
|
|
|
IterateAllMetadata<ContentProviderEntry>(
|
2018-08-10 02:52:27 +02:00
|
|
|
out,
|
|
|
|
[](const CNMT& c, const ContentRecord& r) {
|
2018-12-28 05:58:18 +01:00
|
|
|
return ContentProviderEntry{c.GetTitleID(), r.type};
|
2018-08-10 02:52:27 +02:00
|
|
|
},
|
|
|
|
[&title_type, &record_type, &title_id](const CNMT& c, const ContentRecord& r) {
|
2018-10-30 05:03:25 +01:00
|
|
|
if (title_type && *title_type != c.GetType())
|
2018-08-10 02:52:27 +02:00
|
|
|
return false;
|
2018-10-30 05:03:25 +01:00
|
|
|
if (record_type && *record_type != r.type)
|
2018-08-10 02:52:27 +02:00
|
|
|
return false;
|
2018-10-30 05:03:25 +01:00
|
|
|
if (title_id && *title_id != c.GetTitleID())
|
2018-08-10 02:52:27 +02:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-11-27 19:52:13 +01:00
|
|
|
static std::shared_ptr<NCA> GetNCAFromNSPForID(const NSP& nsp, const NcaID& id) {
|
2019-06-12 23:27:06 +02:00
|
|
|
auto file = nsp.GetFile(fmt::format("{}.nca", Common::HexToString(id, false)));
|
|
|
|
if (file == nullptr) {
|
2018-08-25 17:50:04 +02:00
|
|
|
return nullptr;
|
2019-06-12 23:27:06 +02:00
|
|
|
}
|
|
|
|
return std::make_shared<NCA>(std::move(file));
|
2018-08-10 02:52:27 +02:00
|
|
|
}
|
|
|
|
|
2018-11-27 19:52:13 +01:00
|
|
|
InstallResult RegisteredCache::InstallEntry(const XCI& xci, bool overwrite_if_exists,
|
2018-08-12 05:01:38 +02:00
|
|
|
const VfsCopyFunction& copy) {
|
2018-11-27 19:52:13 +01:00
|
|
|
return InstallEntry(*xci.GetSecurePartitionNSP(), overwrite_if_exists, copy);
|
2018-08-25 17:50:04 +02:00
|
|
|
}
|
|
|
|
|
2018-11-27 19:52:13 +01:00
|
|
|
InstallResult RegisteredCache::InstallEntry(const NSP& nsp, bool overwrite_if_exists,
|
2018-08-25 17:50:04 +02:00
|
|
|
const VfsCopyFunction& copy) {
|
2018-11-27 19:52:13 +01:00
|
|
|
const auto ncas = nsp.GetNCAsCollapsed();
|
|
|
|
const auto meta_iter = std::find_if(ncas.begin(), ncas.end(), [](const auto& nca) {
|
2018-08-10 02:52:27 +02:00
|
|
|
return nca->GetType() == NCAContentType::Meta;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (meta_iter == ncas.end()) {
|
2019-07-01 08:31:32 +02:00
|
|
|
LOG_ERROR(Loader, "The file you are attempting to install does not have a metadata NCA and "
|
|
|
|
"is therefore malformed. Check your encryption keys.");
|
2018-08-12 05:01:38 +02:00
|
|
|
return InstallResult::ErrorMetaFailed;
|
2018-08-10 02:52:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Install Metadata File
|
|
|
|
const auto meta_id_raw = (*meta_iter)->GetName().substr(0, 32);
|
2018-08-16 05:16:11 +02:00
|
|
|
const auto meta_id = Common::HexStringToArray<16>(meta_id_raw);
|
2018-08-12 05:01:38 +02:00
|
|
|
|
2018-11-27 19:52:13 +01:00
|
|
|
const auto res = RawInstallNCA(**meta_iter, copy, overwrite_if_exists, meta_id);
|
2018-08-12 05:01:38 +02:00
|
|
|
if (res != InstallResult::Success)
|
|
|
|
return res;
|
2018-08-10 02:52:27 +02:00
|
|
|
|
|
|
|
// Install all the other NCAs
|
|
|
|
const auto section0 = (*meta_iter)->GetSubdirectories()[0];
|
|
|
|
const auto cnmt_file = section0->GetFiles()[0];
|
|
|
|
const CNMT cnmt(cnmt_file);
|
|
|
|
for (const auto& record : cnmt.GetContentRecords()) {
|
2019-07-01 07:37:22 +02:00
|
|
|
// Ignore DeltaFragments, they are not useful to us
|
|
|
|
if (record.type == ContentRecordType::DeltaFragment)
|
|
|
|
continue;
|
2018-08-25 17:50:04 +02:00
|
|
|
const auto nca = GetNCAFromNSPForID(nsp, record.nca_id);
|
2018-08-12 05:01:38 +02:00
|
|
|
if (nca == nullptr)
|
|
|
|
return InstallResult::ErrorCopyFailed;
|
2018-11-27 19:52:13 +01:00
|
|
|
const auto res2 = RawInstallNCA(*nca, copy, overwrite_if_exists, record.nca_id);
|
2018-08-12 05:01:38 +02:00
|
|
|
if (res2 != InstallResult::Success)
|
|
|
|
return res2;
|
2018-08-10 02:52:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Refresh();
|
2018-08-12 05:01:38 +02:00
|
|
|
return InstallResult::Success;
|
2018-08-10 02:52:27 +02:00
|
|
|
}
|
|
|
|
|
2018-11-27 19:52:13 +01:00
|
|
|
InstallResult RegisteredCache::InstallEntry(const NCA& nca, TitleType type,
|
2018-08-12 05:01:38 +02:00
|
|
|
bool overwrite_if_exists, const VfsCopyFunction& copy) {
|
2018-08-10 02:52:27 +02:00
|
|
|
CNMTHeader header{
|
2018-11-27 19:52:13 +01:00
|
|
|
nca.GetTitleId(), ///< Title ID
|
|
|
|
0, ///< Ignore/Default title version
|
|
|
|
type, ///< Type
|
|
|
|
{}, ///< Padding
|
|
|
|
0x10, ///< Default table offset
|
|
|
|
1, ///< 1 Content Entry
|
|
|
|
0, ///< No Meta Entries
|
|
|
|
{}, ///< Padding
|
2018-08-10 02:52:27 +02:00
|
|
|
};
|
|
|
|
OptionalHeader opt_header{0, 0};
|
2018-11-27 19:52:13 +01:00
|
|
|
ContentRecord c_rec{{}, {}, {}, GetCRTypeFromNCAType(nca.GetType()), {}};
|
|
|
|
const auto& data = nca.GetBaseFile()->ReadBytes(0x100000);
|
2018-08-10 02:52:27 +02:00
|
|
|
mbedtls_sha256(data.data(), data.size(), c_rec.hash.data(), 0);
|
|
|
|
memcpy(&c_rec.nca_id, &c_rec.hash, 16);
|
|
|
|
const CNMT new_cnmt(header, opt_header, {c_rec}, {});
|
2018-08-12 05:01:38 +02:00
|
|
|
if (!RawInstallYuzuMeta(new_cnmt))
|
|
|
|
return InstallResult::ErrorMetaFailed;
|
|
|
|
return RawInstallNCA(nca, copy, overwrite_if_exists, c_rec.nca_id);
|
2018-08-10 02:52:27 +02:00
|
|
|
}
|
|
|
|
|
2018-11-27 19:52:13 +01:00
|
|
|
InstallResult RegisteredCache::RawInstallNCA(const NCA& nca, const VfsCopyFunction& copy,
|
2018-08-12 05:01:38 +02:00
|
|
|
bool overwrite_if_exists,
|
2018-10-30 05:03:25 +01:00
|
|
|
std::optional<NcaID> override_id) {
|
2018-11-27 19:52:13 +01:00
|
|
|
const auto in = nca.GetBaseFile();
|
2018-08-10 02:52:27 +02:00
|
|
|
Core::Crypto::SHA256Hash hash{};
|
|
|
|
|
|
|
|
// Calculate NcaID
|
|
|
|
// NOTE: Because computing the SHA256 of an entire NCA is quite expensive (especially if the
|
|
|
|
// game is massive), we're going to cheat and only hash the first MB of the NCA.
|
|
|
|
// Also, for XCIs the NcaID matters, so if the override id isn't none, use that.
|
|
|
|
NcaID id{};
|
2018-10-30 05:03:25 +01:00
|
|
|
if (override_id) {
|
|
|
|
id = *override_id;
|
|
|
|
} else {
|
2018-08-10 02:52:27 +02:00
|
|
|
const auto& data = in->ReadBytes(0x100000);
|
|
|
|
mbedtls_sha256(data.data(), data.size(), hash.data(), 0);
|
|
|
|
memcpy(id.data(), hash.data(), 16);
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:24:44 +02:00
|
|
|
std::string path = GetRelativePathFromNcaID(id, false, true, false);
|
2018-08-10 02:52:27 +02:00
|
|
|
|
2018-08-12 05:01:38 +02:00
|
|
|
if (GetFileAtID(id) != nullptr && !overwrite_if_exists) {
|
2018-08-10 17:10:44 +02:00
|
|
|
LOG_WARNING(Loader, "Attempting to overwrite existing NCA. Skipping...");
|
2018-08-12 05:01:38 +02:00
|
|
|
return InstallResult::ErrorAlreadyExists;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetFileAtID(id) != nullptr) {
|
|
|
|
LOG_WARNING(Loader, "Overwriting existing NCA...");
|
|
|
|
VirtualDir c_dir;
|
|
|
|
{ c_dir = dir->GetFileRelative(path)->GetContainingDirectory(); }
|
|
|
|
c_dir->DeleteFile(FileUtil::GetFilename(path));
|
2018-08-10 02:52:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto out = dir->CreateFileRelative(path);
|
|
|
|
if (out == nullptr)
|
2018-08-12 05:01:38 +02:00
|
|
|
return InstallResult::ErrorCopyFailed;
|
2018-09-24 03:50:16 +02:00
|
|
|
return copy(in, out, VFS_RC_LARGE_COPY_BLOCK) ? InstallResult::Success
|
|
|
|
: InstallResult::ErrorCopyFailed;
|
2018-08-10 02:52:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RegisteredCache::RawInstallYuzuMeta(const CNMT& cnmt) {
|
2018-08-11 21:39:09 +02:00
|
|
|
// Reasoning behind this method can be found in the comment for InstallEntry, NCA overload.
|
2018-08-10 02:52:27 +02:00
|
|
|
const auto dir = this->dir->CreateDirectoryRelative("yuzu_meta");
|
|
|
|
const auto filename = GetCNMTName(cnmt.GetType(), cnmt.GetTitleID());
|
|
|
|
if (dir->GetFile(filename) == nullptr) {
|
|
|
|
auto out = dir->CreateFile(filename);
|
|
|
|
const auto buffer = cnmt.Serialize();
|
|
|
|
out->Resize(buffer.size());
|
|
|
|
out->WriteBytes(buffer);
|
|
|
|
} else {
|
|
|
|
auto out = dir->GetFile(filename);
|
|
|
|
CNMT old_cnmt(out);
|
|
|
|
// Returns true on change
|
|
|
|
if (old_cnmt.UnionRecords(cnmt)) {
|
|
|
|
out->Resize(0);
|
|
|
|
const auto buffer = old_cnmt.Serialize();
|
|
|
|
out->Resize(buffer.size());
|
|
|
|
out->WriteBytes(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Refresh();
|
|
|
|
return std::find_if(yuzu_meta.begin(), yuzu_meta.end(),
|
2018-08-10 03:33:13 +02:00
|
|
|
[&cnmt](const std::pair<u64, CNMT>& kv) {
|
2018-08-10 02:52:27 +02:00
|
|
|
return kv.second.GetType() == cnmt.GetType() &&
|
|
|
|
kv.second.GetTitleID() == cnmt.GetTitleID();
|
|
|
|
}) != yuzu_meta.end();
|
|
|
|
}
|
2018-08-26 01:00:36 +02:00
|
|
|
|
2018-12-28 05:58:18 +01:00
|
|
|
ContentProviderUnion::~ContentProviderUnion() = default;
|
2018-08-26 01:00:36 +02:00
|
|
|
|
2018-12-28 05:58:18 +01:00
|
|
|
void ContentProviderUnion::SetSlot(ContentProviderUnionSlot slot, ContentProvider* provider) {
|
|
|
|
providers[slot] = provider;
|
2018-08-26 01:00:36 +02:00
|
|
|
}
|
|
|
|
|
2018-12-28 05:58:18 +01:00
|
|
|
void ContentProviderUnion::ClearSlot(ContentProviderUnionSlot slot) {
|
|
|
|
providers[slot] = nullptr;
|
2018-08-26 01:00:36 +02:00
|
|
|
}
|
|
|
|
|
2018-12-28 05:58:18 +01:00
|
|
|
void ContentProviderUnion::Refresh() {
|
|
|
|
for (auto& provider : providers) {
|
|
|
|
if (provider.second == nullptr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
provider.second->Refresh();
|
|
|
|
}
|
2018-08-26 01:00:36 +02:00
|
|
|
}
|
|
|
|
|
2018-12-28 05:58:18 +01:00
|
|
|
bool ContentProviderUnion::HasEntry(u64 title_id, ContentRecordType type) const {
|
|
|
|
for (const auto& provider : providers) {
|
|
|
|
if (provider.second == nullptr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (provider.second->HasEntry(title_id, type))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<u32> ContentProviderUnion::GetEntryVersion(u64 title_id) const {
|
|
|
|
for (const auto& provider : providers) {
|
|
|
|
if (provider.second == nullptr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const auto res = provider.second->GetEntryVersion(title_id);
|
|
|
|
if (res != std::nullopt)
|
2018-08-26 01:00:36 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-12-28 05:58:18 +01:00
|
|
|
return std::nullopt;
|
2018-08-26 01:00:36 +02:00
|
|
|
}
|
|
|
|
|
2018-12-28 05:58:18 +01:00
|
|
|
VirtualFile ContentProviderUnion::GetEntryUnparsed(u64 title_id, ContentRecordType type) const {
|
|
|
|
for (const auto& provider : providers) {
|
|
|
|
if (provider.second == nullptr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const auto res = provider.second->GetEntryUnparsed(title_id, type);
|
2018-08-26 01:00:36 +02:00
|
|
|
if (res != nullptr)
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-12-28 05:58:18 +01:00
|
|
|
VirtualFile ContentProviderUnion::GetEntryRaw(u64 title_id, ContentRecordType type) const {
|
|
|
|
for (const auto& provider : providers) {
|
|
|
|
if (provider.second == nullptr)
|
|
|
|
continue;
|
2018-08-26 01:00:36 +02:00
|
|
|
|
2018-12-28 05:58:18 +01:00
|
|
|
const auto res = provider.second->GetEntryRaw(title_id, type);
|
2018-08-26 01:00:36 +02:00
|
|
|
if (res != nullptr)
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-12-28 05:58:18 +01:00
|
|
|
std::unique_ptr<NCA> ContentProviderUnion::GetEntry(u64 title_id, ContentRecordType type) const {
|
|
|
|
for (const auto& provider : providers) {
|
|
|
|
if (provider.second == nullptr)
|
|
|
|
continue;
|
2018-08-26 01:00:36 +02:00
|
|
|
|
2018-12-28 05:58:18 +01:00
|
|
|
auto res = provider.second->GetEntry(title_id, type);
|
|
|
|
if (res != nullptr)
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2018-08-26 01:00:36 +02:00
|
|
|
}
|
|
|
|
|
2018-12-28 05:58:18 +01:00
|
|
|
std::vector<ContentProviderEntry> ContentProviderUnion::ListEntriesFilter(
|
|
|
|
std::optional<TitleType> title_type, std::optional<ContentRecordType> record_type,
|
|
|
|
std::optional<u64> title_id) const {
|
|
|
|
std::vector<ContentProviderEntry> out;
|
|
|
|
|
|
|
|
for (const auto& provider : providers) {
|
|
|
|
if (provider.second == nullptr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const auto vec = provider.second->ListEntriesFilter(title_type, record_type, title_id);
|
|
|
|
std::copy(vec.begin(), vec.end(), std::back_inserter(out));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(out.begin(), out.end());
|
|
|
|
out.erase(std::unique(out.begin(), out.end()), out.end());
|
|
|
|
return out;
|
2018-08-26 01:00:36 +02:00
|
|
|
}
|
|
|
|
|
2018-12-28 05:58:18 +01:00
|
|
|
std::vector<std::pair<ContentProviderUnionSlot, ContentProviderEntry>>
|
|
|
|
ContentProviderUnion::ListEntriesFilterOrigin(std::optional<ContentProviderUnionSlot> origin,
|
|
|
|
std::optional<TitleType> title_type,
|
|
|
|
std::optional<ContentRecordType> record_type,
|
|
|
|
std::optional<u64> title_id) const {
|
|
|
|
std::vector<std::pair<ContentProviderUnionSlot, ContentProviderEntry>> out;
|
|
|
|
|
|
|
|
for (const auto& provider : providers) {
|
|
|
|
if (provider.second == nullptr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (origin.has_value() && *origin != provider.first)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const auto vec = provider.second->ListEntriesFilter(title_type, record_type, title_id);
|
|
|
|
std::transform(vec.begin(), vec.end(), std::back_inserter(out),
|
|
|
|
[&provider](const ContentProviderEntry& entry) {
|
|
|
|
return std::make_pair(provider.first, entry);
|
|
|
|
});
|
2018-08-26 01:00:36 +02:00
|
|
|
}
|
2018-10-17 20:04:18 +02:00
|
|
|
|
|
|
|
std::sort(out.begin(), out.end());
|
|
|
|
out.erase(std::unique(out.begin(), out.end()), out.end());
|
2018-08-26 01:00:36 +02:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2019-06-25 01:10:17 +02:00
|
|
|
std::optional<ContentProviderUnionSlot> ContentProviderUnion::GetSlotForEntry(
|
|
|
|
u64 title_id, ContentRecordType type) const {
|
2019-06-26 04:25:10 +02:00
|
|
|
const auto iter =
|
|
|
|
std::find_if(providers.begin(), providers.end(), [title_id, type](const auto& provider) {
|
|
|
|
return provider.second != nullptr && provider.second->HasEntry(title_id, type);
|
|
|
|
});
|
2019-06-25 01:10:17 +02:00
|
|
|
|
2019-06-26 04:25:10 +02:00
|
|
|
if (iter == providers.end()) {
|
|
|
|
return std::nullopt;
|
2019-06-25 01:10:17 +02:00
|
|
|
}
|
|
|
|
|
2019-06-26 04:25:10 +02:00
|
|
|
return iter->first;
|
2019-06-25 01:10:17 +02:00
|
|
|
}
|
|
|
|
|
2018-12-28 05:58:18 +01:00
|
|
|
ManualContentProvider::~ManualContentProvider() = default;
|
|
|
|
|
|
|
|
void ManualContentProvider::AddEntry(TitleType title_type, ContentRecordType content_type,
|
|
|
|
u64 title_id, VirtualFile file) {
|
|
|
|
entries.insert_or_assign({title_type, content_type, title_id}, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ManualContentProvider::ClearAllEntries() {
|
|
|
|
entries.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ManualContentProvider::Refresh() {}
|
|
|
|
|
|
|
|
bool ManualContentProvider::HasEntry(u64 title_id, ContentRecordType type) const {
|
|
|
|
return GetEntryRaw(title_id, type) != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<u32> ManualContentProvider::GetEntryVersion(u64 title_id) const {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile ManualContentProvider::GetEntryUnparsed(u64 title_id, ContentRecordType type) const {
|
|
|
|
return GetEntryRaw(title_id, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
VirtualFile ManualContentProvider::GetEntryRaw(u64 title_id, ContentRecordType type) const {
|
|
|
|
const auto iter =
|
|
|
|
std::find_if(entries.begin(), entries.end(), [title_id, type](const auto& entry) {
|
|
|
|
const auto [title_type, content_type, e_title_id] = entry.first;
|
|
|
|
return content_type == type && e_title_id == title_id;
|
|
|
|
});
|
|
|
|
if (iter == entries.end())
|
|
|
|
return nullptr;
|
|
|
|
return iter->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<NCA> ManualContentProvider::GetEntry(u64 title_id, ContentRecordType type) const {
|
|
|
|
const auto res = GetEntryRaw(title_id, type);
|
|
|
|
if (res == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
return std::make_unique<NCA>(res, nullptr, 0, keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ContentProviderEntry> ManualContentProvider::ListEntriesFilter(
|
2018-10-30 05:03:25 +01:00
|
|
|
std::optional<TitleType> title_type, std::optional<ContentRecordType> record_type,
|
|
|
|
std::optional<u64> title_id) const {
|
2018-12-28 05:58:18 +01:00
|
|
|
std::vector<ContentProviderEntry> out;
|
|
|
|
|
|
|
|
for (const auto& entry : entries) {
|
|
|
|
const auto [e_title_type, e_content_type, e_title_id] = entry.first;
|
|
|
|
if ((title_type == std::nullopt || e_title_type == *title_type) &&
|
|
|
|
(record_type == std::nullopt || e_content_type == *record_type) &&
|
|
|
|
(title_id == std::nullopt || e_title_id == *title_id)) {
|
|
|
|
out.emplace_back(ContentProviderEntry{e_title_id, e_content_type});
|
|
|
|
}
|
2018-08-26 01:00:36 +02:00
|
|
|
}
|
2018-10-17 20:04:18 +02:00
|
|
|
|
|
|
|
std::sort(out.begin(), out.end());
|
|
|
|
out.erase(std::unique(out.begin(), out.end()), out.end());
|
2018-08-26 01:00:36 +02:00
|
|
|
return out;
|
|
|
|
}
|
2018-08-10 02:52:27 +02:00
|
|
|
} // namespace FileSys
|