nsp: Comply with style and performance guidelines

This commit is contained in:
Zach Hilman 2018-08-25 22:42:54 -04:00
parent 58473309a0
commit e4e55d064e
7 changed files with 48 additions and 29 deletions

View File

@ -237,7 +237,7 @@ void KeyManager::SetKey(S128KeyType id, Key128 key, u64 field1, u64 field2) {
Key128 rights_id; Key128 rights_id;
std::memcpy(rights_id.data(), &field2, sizeof(u64)); std::memcpy(rights_id.data(), &field2, sizeof(u64));
std::memcpy(rights_id.data() + sizeof(u64), &field1, sizeof(u64)); std::memcpy(rights_id.data() + sizeof(u64), &field1, sizeof(u64));
WriteKeyToFile(true, fmt::format("{}", Common::HexArrayToString(rights_id)), key); WriteKeyToFile(true, Common::HexArrayToString(rights_id), key);
} }
const auto iter2 = std::find_if( const auto iter2 = std::find_if(
s128_file_id.begin(), s128_file_id.end(), s128_file_id.begin(), s128_file_id.end(),

View File

@ -12,6 +12,7 @@
#include "core/file_sys/content_archive.h" #include "core/file_sys/content_archive.h"
#include "core/file_sys/nca_metadata.h" #include "core/file_sys/nca_metadata.h"
#include "core/file_sys/partition_filesystem.h" #include "core/file_sys/partition_filesystem.h"
#include "core/file_sys/submission_package.h"
#include "core/file_sys/vfs_offset.h" #include "core/file_sys/vfs_offset.h"
#include "core/loader/loader.h" #include "core/loader/loader.h"

View File

@ -11,7 +11,6 @@
#include "common/swap.h" #include "common/swap.h"
#include "core/file_sys/vfs.h" #include "core/file_sys/vfs.h"
#include "core/loader/loader.h" #include "core/loader/loader.h"
#include "submission_package.h"
namespace Loader { namespace Loader {
enum class ResultStatus : u16; enum class ResultStatus : u16;
@ -21,6 +20,7 @@ namespace FileSys {
class NCA; class NCA;
enum class NCAContentType : u8; enum class NCAContentType : u8;
class NSP;
enum class GamecardSize : u8 { enum class GamecardSize : u8 {
S_1GB = 0xFA, S_1GB = 0xFA,

View File

@ -72,21 +72,21 @@ NSP::NSP(VirtualFile file_)
ncas_title[ContentRecordType::Meta] = nca; ncas_title[ContentRecordType::Meta] = nca;
for (const auto& rec : cnmt.GetContentRecords()) { for (const auto& rec : cnmt.GetContentRecords()) {
const auto next_file = pfs->GetFile( const auto id_string = Common::HexArrayToString(rec.nca_id, false);
fmt::format("{}.nca", Common::HexArrayToString(rec.nca_id, false))); const auto next_file = pfs->GetFile(fmt::format("{}.nca", id_string));
if (next_file == nullptr) { if (next_file == nullptr) {
LOG_WARNING(Service_FS, LOG_WARNING(Service_FS,
"NCA with ID {}.nca is listed in content metadata, but cannot " "NCA with ID {}.nca is listed in content metadata, but cannot "
"be found in PFS. NSP appears to be corrupted.", "be found in PFS. NSP appears to be corrupted.",
Common::HexArrayToString(rec.nca_id, false)); id_string);
continue; continue;
} }
const auto next_nca = std::make_shared<NCA>(next_file); auto next_nca = std::make_shared<NCA>(next_file);
if (next_nca->GetType() == NCAContentType::Program) if (next_nca->GetType() == NCAContentType::Program)
program_status[cnmt.GetTitleID()] = next_nca->GetStatus(); program_status[cnmt.GetTitleID()] = next_nca->GetStatus();
if (next_nca->GetStatus() == Loader::ResultStatus::Success) if (next_nca->GetStatus() == Loader::ResultStatus::Success)
ncas_title[rec.type] = next_nca; ncas_title[rec.type] = std::move(next_nca);
} }
break; break;
@ -95,14 +95,17 @@ NSP::NSP(VirtualFile file_)
} }
} }
NSP::~NSP() = default;
Loader::ResultStatus NSP::GetStatus() const { Loader::ResultStatus NSP::GetStatus() const {
return status; return status;
} }
Loader::ResultStatus NSP::GetProgramStatus(u64 title_id) const { Loader::ResultStatus NSP::GetProgramStatus(u64 title_id) const {
if (program_status.find(title_id) != program_status.end()) const auto iter = program_status.find(title_id);
return program_status.at(title_id); if (iter == program_status.end())
return Loader::ResultStatus::ErrorNSPMissingProgramNCA; return Loader::ResultStatus::ErrorNSPMissingProgramNCA;
return iter->second;
} }
u64 NSP::GetFirstTitleID() const { u64 NSP::GetFirstTitleID() const {
@ -112,16 +115,19 @@ u64 NSP::GetFirstTitleID() const {
} }
u64 NSP::GetProgramTitleID() const { u64 NSP::GetProgramTitleID() const {
auto out = GetFirstTitleID(); const auto out = GetFirstTitleID();
for (const auto other_tid : GetTitleIDs()) { if ((out & 0x800) == 0)
if ((out & 0x800) != 0) return out;
out = other_tid;
} const auto ids = GetTitleIDs();
return out; const auto iter =
std::find_if(ids.begin(), ids.end(), [](u64 tid) { return (tid & 0x800) == 0; });
return iter == ids.end() ? out : *iter;
} }
std::vector<u64> NSP::GetTitleIDs() const { std::vector<u64> NSP::GetTitleIDs() const {
std::vector<u64> out; std::vector<u64> out;
out.reserve(ncas.size());
for (const auto& kv : ncas) for (const auto& kv : ncas)
out.push_back(kv.first); out.push_back(kv.first);
return out; return out;
@ -156,7 +162,7 @@ std::multimap<u64, std::shared_ptr<NCA>> NSP::GetNCAsByTitleID() const {
std::multimap<u64, std::shared_ptr<NCA>> out; std::multimap<u64, std::shared_ptr<NCA>> out;
for (const auto& map : ncas) { for (const auto& map : ncas) {
for (const auto& inner_map : map.second) for (const auto& inner_map : map.second)
out.insert({map.first, inner_map.second}); out.emplace(map.first, inner_map.second);
} }
return out; return out;
} }
@ -168,13 +174,16 @@ std::map<u64, std::map<ContentRecordType, std::shared_ptr<NCA>>> NSP::GetNCAs()
std::shared_ptr<NCA> NSP::GetNCA(u64 title_id, ContentRecordType type) const { std::shared_ptr<NCA> NSP::GetNCA(u64 title_id, ContentRecordType type) const {
if (extracted) if (extracted)
LOG_WARNING(Service_FS, "called on an NSP that is of type extracted."); LOG_WARNING(Service_FS, "called on an NSP that is of type extracted.");
if (ncas.find(title_id) != ncas.end()) {
const auto& inner_map = ncas.at(title_id);
if (inner_map.find(type) != inner_map.end())
return inner_map.at(type);
}
return nullptr; const auto title_id_iter = ncas.find(title_id);
if (title_id_iter == ncas.end())
return nullptr;
const auto type_iter = title_id_iter->second.find(type);
if (type_iter == title_id_iter->second.end())
return nullptr;
return type_iter->second;
} }
VirtualFile NSP::GetNCAFile(u64 title_id, ContentRecordType type) const { VirtualFile NSP::GetNCAFile(u64 title_id, ContentRecordType type) const {
@ -197,9 +206,9 @@ std::vector<Core::Crypto::Key128> NSP::GetTitlekey() const {
continue; continue;
} }
Core::Crypto::Key128 key{}; out.emplace_back();
ticket_file->Read(key.data(), key.size(), Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET); ticket_file->Read(out.back().data(), out.back().size(),
out.push_back(key); Core::Crypto::TICKET_FILE_TITLEKEY_OFFSET);
} }
return out; return out;
} }

View File

@ -10,6 +10,7 @@
#include "common/common_types.h" #include "common/common_types.h"
#include "common/swap.h" #include "common/swap.h"
#include "core/file_sys/content_archive.h" #include "core/file_sys/content_archive.h"
#include "core/file_sys/romfs_factory.h"
#include "core/file_sys/vfs.h" #include "core/file_sys/vfs.h"
#include "core/loader/loader.h" #include "core/loader/loader.h"
#include "romfs_factory.h" #include "romfs_factory.h"
@ -19,6 +20,7 @@ namespace FileSys {
class NSP : public ReadOnlyVfsDirectory { class NSP : public ReadOnlyVfsDirectory {
public: public:
explicit NSP(VirtualFile file); explicit NSP(VirtualFile file);
~NSP();
Loader::ResultStatus GetStatus() const; Loader::ResultStatus GetStatus() const;
Loader::ResultStatus GetProgramStatus(u64 title_id) const; Loader::ResultStatus GetProgramStatus(u64 title_id) const;

View File

@ -21,22 +21,27 @@ namespace Loader {
AppLoader_NSP::AppLoader_NSP(FileSys::VirtualFile file) AppLoader_NSP::AppLoader_NSP(FileSys::VirtualFile file)
: AppLoader(file), nsp(std::make_unique<FileSys::NSP>(file)), : AppLoader(file), nsp(std::make_unique<FileSys::NSP>(file)),
title_id(nsp->GetProgramTitleID()) { title_id(nsp->GetProgramTitleID()) {
if (nsp->GetStatus() != ResultStatus::Success) if (nsp->GetStatus() != ResultStatus::Success)
return; return;
if (nsp->IsExtractedType()) if (nsp->IsExtractedType())
return; return;
const auto control_nca = const auto control_nca =
nsp->GetNCA(nsp->GetFirstTitleID(), FileSys::ContentRecordType::Control); nsp->GetNCA(nsp->GetFirstTitleID(), FileSys::ContentRecordType::Control);
if (control_nca == nullptr || control_nca->GetStatus() != ResultStatus::Success) if (control_nca == nullptr || control_nca->GetStatus() != ResultStatus::Success)
return; return;
const auto romfs = FileSys::ExtractRomFS(control_nca->GetRomFS()); const auto romfs = FileSys::ExtractRomFS(control_nca->GetRomFS());
if (romfs == nullptr) if (romfs == nullptr)
return; return;
for (const auto& language : FileSys::LANGUAGE_NAMES) { for (const auto& language : FileSys::LANGUAGE_NAMES) {
icon_file = romfs->GetFile("icon_" + std::string(language) + ".dat"); icon_file = romfs->GetFile("icon_" + std::string(language) + ".dat");
if (icon_file != nullptr) if (icon_file != nullptr)
break; break;
} }
const auto nacp_raw = romfs->GetFile("control.nacp"); const auto nacp_raw = romfs->GetFile("control.nacp");
if (nacp_raw == nullptr) if (nacp_raw == nullptr)
return; return;
@ -51,15 +56,17 @@ FileType AppLoader_NSP::IdentifyType(const FileSys::VirtualFile& file) {
if (nsp.GetStatus() == ResultStatus::Success) { if (nsp.GetStatus() == ResultStatus::Success) {
// Extracted Type case // Extracted Type case
if (nsp.IsExtractedType() && nsp.GetExeFS() != nullptr && if (nsp.IsExtractedType() && nsp.GetExeFS() != nullptr &&
FileSys::IsDirectoryExeFS(nsp.GetExeFS()) && nsp.GetRomFS() != nullptr) FileSys::IsDirectoryExeFS(nsp.GetExeFS()) && nsp.GetRomFS() != nullptr) {
return FileType::NSP; return FileType::NSP;
}
// Non-Ectracted Type case // Non-Ectracted Type case
if (!nsp.IsExtractedType() && if (!nsp.IsExtractedType() &&
nsp.GetNCA(nsp.GetFirstTitleID(), FileSys::ContentRecordType::Program) != nullptr && nsp.GetNCA(nsp.GetFirstTitleID(), FileSys::ContentRecordType::Program) != nullptr &&
AppLoader_NCA::IdentifyType(nsp.GetNCAFile( AppLoader_NCA::IdentifyType(nsp.GetNCAFile(
nsp.GetFirstTitleID(), FileSys::ContentRecordType::Program)) == FileType::NCA) nsp.GetFirstTitleID(), FileSys::ContentRecordType::Program)) == FileType::NCA) {
return FileType::NSP; return FileType::NSP;
}
} }
return FileType::Error; return FileType::Error;

View File

@ -22,7 +22,7 @@ class AppLoader_NCA;
class AppLoader_NSP final : public AppLoader { class AppLoader_NSP final : public AppLoader {
public: public:
explicit AppLoader_NSP(FileSys::VirtualFile file); explicit AppLoader_NSP(FileSys::VirtualFile file);
~AppLoader_NSP(); ~AppLoader_NSP() override;
/** /**
* Returns the type of the file * Returns the type of the file