From bf35138d1d0579d7d105988ca1f863b59335e937 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 10 Jun 2019 22:56:00 -0400 Subject: [PATCH 1/7] file_sys/card_image: Use std::array deduction guides Same thing, less code. --- src/core/file_sys/card_image.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/core/file_sys/card_image.cpp b/src/core/file_sys/card_image.cpp index 2c145bd09..1fec9b0e9 100644 --- a/src/core/file_sys/card_image.cpp +++ b/src/core/file_sys/card_image.cpp @@ -18,7 +18,12 @@ namespace FileSys { -constexpr std::array partition_names = {"update", "normal", "secure", "logo"}; +constexpr std::array partition_names{ + "update", + "normal", + "secure", + "logo", +}; XCI::XCI(VirtualFile file_) : file(std::move(file_)), program_nca_status{Loader::ResultStatus::ErrorXCIMissingProgramNCA}, From 825ffd7b1f25792c68a92a6966a05a461e30041a Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 10 Jun 2019 22:58:05 -0400 Subject: [PATCH 2/7] file_sys/card_image: Get rid of a magic number We can just use the size of the array to dehardcode it. --- src/core/file_sys/card_image.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/file_sys/card_image.cpp b/src/core/file_sys/card_image.cpp index 1fec9b0e9..fd00e5457 100644 --- a/src/core/file_sys/card_image.cpp +++ b/src/core/file_sys/card_image.cpp @@ -27,7 +27,7 @@ constexpr std::array partition_names{ XCI::XCI(VirtualFile file_) : file(std::move(file_)), program_nca_status{Loader::ResultStatus::ErrorXCIMissingProgramNCA}, - partitions(0x4) { + partitions(partition_names.size()) { if (file->ReadObject(&header) != sizeof(GamecardHeader)) { status = Loader::ResultStatus::ErrorBadXCIHeader; return; From 288d027e892959f84cb0aead7570f8f789663f3a Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 10 Jun 2019 23:02:48 -0400 Subject: [PATCH 3/7] file_sys/card_image: Deduplicate type cast Same thing, less duplication. We can also std::move raw into the PartitionFilesystem constructor. --- src/core/file_sys/card_image.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/core/file_sys/card_image.cpp b/src/core/file_sys/card_image.cpp index fd00e5457..4375e01fa 100644 --- a/src/core/file_sys/card_image.cpp +++ b/src/core/file_sys/card_image.cpp @@ -48,10 +48,12 @@ XCI::XCI(VirtualFile file_) for (XCIPartition partition : {XCIPartition::Update, XCIPartition::Normal, XCIPartition::Secure, XCIPartition::Logo}) { - auto raw = main_hfs.GetFile(partition_names[static_cast(partition)]); - if (raw != nullptr) - partitions[static_cast(partition)] = - std::make_shared(raw); + const auto partition_idx = static_cast(partition); + auto raw = main_hfs.GetFile(partition_names[partition_idx]); + + if (raw != nullptr) { + partitions[partition_idx] = std::make_shared(std::move(raw)); + } } secure_partition = std::make_shared( From e34368249f74bd3afa28d9153a8931b811c8f91b Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 10 Jun 2019 23:08:14 -0400 Subject: [PATCH 4/7] file_sys/card_image: Assign collapsed NCA contents directly to ncas member Same thing, significantly less noisy. --- src/core/file_sys/card_image.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/core/file_sys/card_image.cpp b/src/core/file_sys/card_image.cpp index 4375e01fa..d116d79c1 100644 --- a/src/core/file_sys/card_image.cpp +++ b/src/core/file_sys/card_image.cpp @@ -59,9 +59,7 @@ XCI::XCI(VirtualFile file_) secure_partition = std::make_shared( main_hfs.GetFile(partition_names[static_cast(XCIPartition::Secure)])); - const auto secure_ncas = secure_partition->GetNCAsCollapsed(); - std::copy(secure_ncas.begin(), secure_ncas.end(), std::back_inserter(ncas)); - + ncas = secure_partition->GetNCAsCollapsed(); program = secure_partition->GetNCA(secure_partition->GetProgramTitleID(), ContentRecordType::Program); program_nca_status = secure_partition->GetProgramStatus(secure_partition->GetProgramTitleID()); From 81d361d9f89ba8eada4bf2e414cf610873045ac6 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 10 Jun 2019 23:13:14 -0400 Subject: [PATCH 5/7] file_sys/card_image: Make bracing consistent Makes for more consistent reading. --- src/core/file_sys/card_image.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/core/file_sys/card_image.cpp b/src/core/file_sys/card_image.cpp index d116d79c1..1a5faa43a 100644 --- a/src/core/file_sys/card_image.cpp +++ b/src/core/file_sys/card_image.cpp @@ -63,8 +63,9 @@ XCI::XCI(VirtualFile file_) program = secure_partition->GetNCA(secure_partition->GetProgramTitleID(), ContentRecordType::Program); program_nca_status = secure_partition->GetProgramStatus(secure_partition->GetProgramTitleID()); - if (program_nca_status == Loader::ResultStatus::ErrorNSPMissingProgramNCA) + if (program_nca_status == Loader::ResultStatus::ErrorNSPMissingProgramNCA) { program_nca_status = Loader::ResultStatus::ErrorXCIMissingProgramNCA; + } auto result = AddNCAFromPartition(XCIPartition::Update); if (result != Loader::ResultStatus::Success) { @@ -152,8 +153,9 @@ std::shared_ptr XCI::GetNCAByType(NCAContentType type) const { VirtualFile XCI::GetNCAFileByType(NCAContentType type) const { auto nca = GetNCAByType(type); - if (nca != nullptr) + if (nca != nullptr) { return nca->GetBaseFile(); + } return nullptr; } @@ -179,12 +181,14 @@ Loader::ResultStatus XCI::AddNCAFromPartition(XCIPartition part) { } for (const VirtualFile& file : partitions[static_cast(part)]->GetFiles()) { - if (file->GetExtension() != "nca") + if (file->GetExtension() != "nca") { continue; + } auto nca = std::make_shared(file, nullptr, 0, keys); // TODO(DarkLordZach): Add proper Rev1+ Support - if (nca->IsUpdate()) + if (nca->IsUpdate()) { continue; + } if (nca->GetType() == NCAContentType::Program) { program_nca_status = nca->GetStatus(); } From 7bdef6106ee7b9dc30149f9c0d38c295d788bb0a Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 10 Jun 2019 23:18:17 -0400 Subject: [PATCH 6/7] file_sys/card_image: Deduplicate casts within AddNCAFromPartition() Makes for nicer reading. --- src/core/file_sys/card_image.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/core/file_sys/card_image.cpp b/src/core/file_sys/card_image.cpp index 1a5faa43a..df9e0c434 100644 --- a/src/core/file_sys/card_image.cpp +++ b/src/core/file_sys/card_image.cpp @@ -176,11 +176,14 @@ VirtualDir XCI::GetParentDirectory() const { } Loader::ResultStatus XCI::AddNCAFromPartition(XCIPartition part) { - if (partitions[static_cast(part)] == nullptr) { + const auto partition_index = static_cast(part); + const auto& partition = partitions[partition_index]; + + if (partition == nullptr) { return Loader::ResultStatus::ErrorXCIMissingPartition; } - for (const VirtualFile& file : partitions[static_cast(part)]->GetFiles()) { + for (const VirtualFile& file : partition->GetFiles()) { if (file->GetExtension() != "nca") { continue; } @@ -197,7 +200,7 @@ Loader::ResultStatus XCI::AddNCAFromPartition(XCIPartition part) { } else { const u16 error_id = static_cast(nca->GetStatus()); LOG_CRITICAL(Loader, "Could not load NCA {}/{}, failed with error code {:04X} ({})", - partition_names[static_cast(part)], nca->GetName(), error_id, + partition_names[partition_index], nca->GetName(), error_id, nca->GetStatus()); } } From c7daddb715d286c7236fc3ab9dc6e2ae623e8ebb Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 12 Jun 2019 16:52:15 -0400 Subject: [PATCH 7/7] file_sys/card_image: Remove obsolete TODO We already support Rev 1+. --- src/core/file_sys/card_image.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/file_sys/card_image.cpp b/src/core/file_sys/card_image.cpp index df9e0c434..626ed0042 100644 --- a/src/core/file_sys/card_image.cpp +++ b/src/core/file_sys/card_image.cpp @@ -187,8 +187,8 @@ Loader::ResultStatus XCI::AddNCAFromPartition(XCIPartition part) { if (file->GetExtension() != "nca") { continue; } + auto nca = std::make_shared(file, nullptr, 0, keys); - // TODO(DarkLordZach): Add proper Rev1+ Support if (nca->IsUpdate()) { continue; }