file_sys: Comply to style guidelines

This commit is contained in:
Zach Hilman 2018-08-09 23:10:32 -04:00
parent bfb945c243
commit 167bfddafa
8 changed files with 60 additions and 47 deletions

View file

@ -5,6 +5,8 @@
#pragma once
#include <array>
#include <cstddef>
#include <string>
#include <fmt/format.h>
#include "common/common_types.h"

View file

@ -2,6 +2,7 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <cstring>
#include "common/common_funcs.h"
#include "common/swap.h"
#include "content_archive.h"
@ -67,9 +68,10 @@ const std::vector<MetaRecord>& CNMT::GetMetaRecords() const {
bool CNMT::UnionRecords(const CNMT& other) {
bool change = false;
for (const auto& rec : other.content_records) {
const auto iter = std::find_if(
content_records.begin(), content_records.end(),
[rec](const ContentRecord& r) { return r.nca_id == rec.nca_id && r.type == rec.type; });
const auto iter = std::find_if(content_records.begin(), content_records.end(),
[&rec](const ContentRecord& r) {
return r.nca_id == rec.nca_id && r.type == rec.type;
});
if (iter == content_records.end()) {
content_records.emplace_back(rec);
++header->number_content_entries;
@ -78,7 +80,7 @@ bool CNMT::UnionRecords(const CNMT& other) {
}
for (const auto& rec : other.meta_records) {
const auto iter =
std::find_if(meta_records.begin(), meta_records.end(), [rec](const MetaRecord& r) {
std::find_if(meta_records.begin(), meta_records.end(), [&rec](const MetaRecord& r) {
return r.title_id == rec.title_id && r.title_version == rec.title_version &&
r.type == rec.type;
});

View file

@ -4,7 +4,11 @@
#pragma once
#include <cstring>
#include <memory>
#include <vector>
#include "common/common_types.h"
#include "common/swap.h"
#include "core/file_sys/vfs.h"
namespace FileSys {

View file

@ -23,13 +23,13 @@ bool operator<(const RegisteredCacheEntry& lhs, const RegisteredCacheEntry& rhs)
}
static bool FollowsTwoDigitDirFormat(std::string_view name) {
const static std::regex two_digit_regex(
static const std::regex two_digit_regex(
"000000[0123456789abcdefABCDEF][0123456789abcdefABCDEF]");
return std::regex_match(name.begin(), name.end(), two_digit_regex);
}
static bool FollowsNcaIdFormat(std::string_view name) {
const static std::regex nca_id_regex("[0123456789abcdefABCDEF]+.nca");
static const std::regex nca_id_regex("[0123456789abcdefABCDEF]+.nca");
return name.size() == 36 && std::regex_match(name.begin(), name.end(), nca_id_regex);
}
@ -56,7 +56,7 @@ static std::string GetCNMTName(TitleType type, u64 title_id) {
"" ///< Currently unknown 'DeltaTitle'
};
size_t index = static_cast<size_t>(type);
auto index = static_cast<size_t>(type);
if (index >= 0x80)
index -= 0x80;
return fmt::format("{}_{:016x}.cnmt", TITLE_TYPE_NAMES[index], title_id);
@ -90,15 +90,15 @@ VirtualFile RegisteredCache::OpenFileOrDirectoryConcat(const VirtualDir& dir,
VirtualFile file = nullptr;
const auto files = nca_dir->GetFiles();
if (files.size() == 1 && files[0]->GetName() == "00")
if (files.size() == 1 && files[0]->GetName() == "00") {
file = files[0];
else {
} else {
std::vector<VirtualFile> concat;
for (u8 i = 0; i < 0x10; ++i) {
auto next = nca_dir->GetFile(fmt::format("{:02X}", i));
if (next != nullptr)
if (next != nullptr) {
concat.push_back(std::move(next));
else {
} else {
next = nca_dir->GetFile(fmt::format("{:02x}", i));
if (next != nullptr)
concat.push_back(std::move(next));
@ -146,7 +146,8 @@ boost::optional<NcaID> RegisteredCache::GetNcaIDFromMetadata(u64 title_id,
return boost::make_optional(iter->nca_id);
}
void RegisteredCache::AccumulateFiles(std::vector<NcaID>& ids) const {
std::vector<NcaID> RegisteredCache::AccumulateFiles() const {
std::vector<NcaID> ids;
for (const auto& d2_dir : dir->GetSubdirectories()) {
if (FollowsNcaIdFormat(d2_dir->GetName())) {
ids.push_back(HexStringToArray<0x10, true>(d2_dir->GetName().substr(0, 0x20)));
@ -175,6 +176,7 @@ void RegisteredCache::AccumulateFiles(std::vector<NcaID>& ids) const {
if (FollowsNcaIdFormat(d2_file->GetName()))
ids.push_back(HexStringToArray<0x10, true>(d2_file->GetName().substr(0, 0x20)));
}
return ids;
}
void RegisteredCache::ProcessFiles(const std::vector<NcaID>& ids) {
@ -185,8 +187,9 @@ void RegisteredCache::ProcessFiles(const std::vector<NcaID>& ids) {
continue;
const auto nca = std::make_shared<NCA>(parser(file, id));
if (nca->GetStatus() != Loader::ResultStatus::Success ||
nca->GetType() != NCAContentType::Meta)
nca->GetType() != NCAContentType::Meta) {
continue;
}
const auto section0 = nca->GetSubdirectories()[0];
@ -218,8 +221,7 @@ void RegisteredCache::AccumulateYuzuMeta() {
void RegisteredCache::Refresh() {
if (dir == nullptr)
return;
std::vector<NcaID> ids;
AccumulateFiles(ids);
const auto ids = AccumulateFiles();
ProcessFiles(ids);
AccumulateYuzuMeta();
}

View file

@ -5,14 +5,17 @@
#pragma once
#include <array>
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <boost/container/flat_map.hpp>
#include "common/common_funcs.h"
#include "common/common_types.h"
#include "content_archive.h"
#include "core/file_sys/nca_metadata.h"
#include "core/file_sys/vfs.h"
#include "nca_metadata.h"
namespace FileSys {
class XCI;
@ -49,7 +52,7 @@ 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.
RegisteredCache(VirtualDir dir,
explicit RegisteredCache(VirtualDir dir,
RegisteredCacheParsingFunction parsing_function =
[](const VirtualFile& file, const NcaID& id) { return file; });
@ -86,7 +89,7 @@ private:
void IterateAllMetadata(std::vector<T>& out,
std::function<T(const CNMT&, const ContentRecord&)> proc,
std::function<bool(const CNMT&, const ContentRecord&)> filter) const;
void AccumulateFiles(std::vector<NcaID>& ids) const;
std::vector<NcaID> AccumulateFiles() const;
void ProcessFiles(const std::vector<NcaID>& ids);
void AccumulateYuzuMeta();
boost::optional<NcaID> GetNcaIDFromMetadata(u64 title_id, ContentRecordType type) const;

View file

@ -9,17 +9,17 @@
namespace FileSys {
VirtualFile ConcatenateFiles(std::vector<VirtualFile> files, std::string_view name) {
VirtualFile ConcatenateFiles(std::vector<VirtualFile> files, std::string name) {
if (files.empty())
return nullptr;
if (files.size() == 1)
return files[0];
return std::shared_ptr<VfsFile>(new ConcatenatedVfsFile(std::move(files), name));
return std::shared_ptr<VfsFile>(new ConcatenatedVfsFile(std::move(files), std::move(name)));
}
ConcatenatedVfsFile::ConcatenatedVfsFile(std::vector<VirtualFile> files_, std::string_view name)
: name(name) {
ConcatenatedVfsFile::ConcatenatedVfsFile(std::vector<VirtualFile> files_, std::string name)
: name(std::move(name)) {
size_t next_offset = 0;
for (const auto& file : files_) {
files[next_offset] = file;

View file

@ -12,14 +12,14 @@
namespace FileSys {
// Wrapper function to allow for more efficient handling of files.size() == 0, 1 cases.
VirtualFile ConcatenateFiles(std::vector<VirtualFile> files, std::string_view name = "");
VirtualFile ConcatenateFiles(std::vector<VirtualFile> files, std::string name = "");
// Class that wraps multiple vfs files and concatenates them, making reads seamless. Currently
// read-only.
class ConcatenatedVfsFile : public VfsFile {
friend VirtualFile ConcatenateFiles(std::vector<VirtualFile> files, std::string_view name);
friend VirtualFile ConcatenateFiles(std::vector<VirtualFile> files, std::string name);
ConcatenatedVfsFile(std::vector<VirtualFile> files, std::string_view name);
ConcatenatedVfsFile(std::vector<VirtualFile> files, std::string name);
public:
std::string GetName() const override;

View file

@ -619,7 +619,7 @@ void GMainWindow::OnMenuLoadFolder() {
}
void GMainWindow::OnMenuInstallToNAND() {
const static QString file_filter =
const QString file_filter =
tr("Installable Switch File (*.nca *.xci);;Nintendo Content Archive (*.nca);;NX Cartridge "
"Image (*.xci)");
QString filename = QFileDialog::getOpenFileName(this, tr("Install File"),
@ -629,36 +629,36 @@ void GMainWindow::OnMenuInstallToNAND() {
const auto xci = std::make_shared<FileSys::XCI>(
vfs->OpenFile(filename.toStdString(), FileSys::Mode::Read));
if (xci->GetStatus() != Loader::ResultStatus::Success) {
QMessageBox::critical(
QMessageBox::warning(
this, tr("Failed to Install XCI"),
tr("The XCI file you provided is invalid. Please double-check your encryption "
"keys and the file and try again."));
return;
}
if (!Service::FileSystem::GetUserNANDContents()->InstallEntry(xci)) {
QMessageBox::critical(
this, tr("Failed to Install XCI"),
tr("There was an error while attempting to install the provided XCI file. It "
"could have an incorrect format or be missing a metadata entry. Please "
"double-check your file and try again."));
} else {
if (Service::FileSystem::GetUserNANDContents()->InstallEntry(xci)) {
QMessageBox::information(this, tr("Successfully Installed XCI"),
tr("The file was successfully installed."));
game_list->PopulateAsync(UISettings::values.gamedir,
UISettings::values.gamedir_deepscan);
} else {
QMessageBox::warning(
this, tr("Failed to Install XCI"),
tr("There was an error while attempting to install the provided XCI file. It "
"could have an incorrect format or be missing a metadata entry. Please "
"double-check your file and try again."));
}
} else {
const auto nca = std::make_shared<FileSys::NCA>(
vfs->OpenFile(filename.toStdString(), FileSys::Mode::Read));
if (nca->GetStatus() != Loader::ResultStatus::Success) {
QMessageBox::critical(
QMessageBox::warning(
this, tr("Failed to Install NCA"),
tr("The NCA file you provided is invalid. Please double-check your encryption "
"keys and the file and try again."));
return;
}
const static QStringList tt_options{"System Application",
static const QStringList tt_options{"System Application",
"System Archive",
"System Application Update",
"Firmware Package (Type A)",
@ -676,7 +676,7 @@ void GMainWindow::OnMenuInstallToNAND() {
auto index = tt_options.indexOf(item);
if (!ok || index == -1) {
QMessageBox::critical(this, tr("Failed to Install NCA"),
QMessageBox::warning(this, tr("Failed to Install NCA"),
tr("The title type you selected for the NCA is invalid."));
return;
}
@ -684,18 +684,18 @@ void GMainWindow::OnMenuInstallToNAND() {
if (index >= 5)
index += 0x80;
if (!Service::FileSystem::GetUserNANDContents()->InstallEntry(
if (Service::FileSystem::GetUserNANDContents()->InstallEntry(
nca, static_cast<FileSys::TitleType>(index))) {
QMessageBox::critical(this, tr("Failed to Install NCA"),
tr("There was an error while attempting to install the "
"provided NCA file. An error might have occured creating "
"the metadata file or parsing the NCA. Please "
"double-check your file and try again."));
} else {
QMessageBox::information(this, tr("Successfully Installed NCA"),
tr("The file was successfully installed."));
game_list->PopulateAsync(UISettings::values.gamedir,
UISettings::values.gamedir_deepscan);
} else {
QMessageBox::warning(this, tr("Failed to Install NCA"),
tr("There was an error while attempting to install the "
"provided NCA file. An error might have occured creating "
"the metadata file or parsing the NCA. Please "
"double-check your file and try again."));
}
}
}