pineapple/src/core/loader/nca.cpp

137 lines
3.8 KiB
C++
Raw Normal View History

2020-12-28 16:15:37 +01:00
// Copyright 2018 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <utility>
#include "common/file_util.h"
#include "common/logging/log.h"
#include "core/core.h"
#include "core/file_sys/content_archive.h"
#include "core/file_sys/romfs_factory.h"
2021-05-02 08:03:43 +02:00
#include "core/hle/kernel/k_process.h"
2020-12-28 16:15:37 +01:00
#include "core/hle/service/filesystem/filesystem.h"
#include "core/loader/deconstructed_rom_directory.h"
#include "core/loader/nca.h"
namespace Loader {
AppLoader_NCA::AppLoader_NCA(FileSys::VirtualFile file_)
: AppLoader(std::move(file_)), nca(std::make_unique<FileSys::NCA>(file)) {}
AppLoader_NCA::~AppLoader_NCA() = default;
2021-04-28 05:28:56 +02:00
FileType AppLoader_NCA::IdentifyType(const FileSys::VirtualFile& nca_file) {
const FileSys::NCA nca(nca_file);
2020-12-28 16:15:37 +01:00
if (nca.GetStatus() == ResultStatus::Success &&
2021-04-28 05:28:56 +02:00
nca.GetType() == FileSys::NCAContentType::Program) {
2020-12-28 16:15:37 +01:00
return FileType::NCA;
2021-04-28 05:28:56 +02:00
}
2020-12-28 16:15:37 +01:00
return FileType::Error;
}
2021-05-02 08:03:43 +02:00
AppLoader_NCA::LoadResult AppLoader_NCA::Load(Kernel::KProcess& process, Core::System& system) {
2020-12-28 16:15:37 +01:00
if (is_loaded) {
return {ResultStatus::ErrorAlreadyLoaded, {}};
}
const auto result = nca->GetStatus();
if (result != ResultStatus::Success) {
return {result, {}};
}
if (nca->GetType() != FileSys::NCAContentType::Program) {
return {ResultStatus::ErrorNCANotProgram, {}};
}
const auto exefs = nca->GetExeFS();
if (exefs == nullptr) {
return {ResultStatus::ErrorNoExeFS, {}};
}
directory_loader = std::make_unique<AppLoader_DeconstructedRomDirectory>(exefs, true);
const auto load_result = directory_loader->Load(process, system);
if (load_result.first != ResultStatus::Success) {
return load_result;
}
if (nca->GetRomFS() != nullptr && nca->GetRomFS()->GetSize() > 0) {
system.GetFileSystemController().RegisterRomFS(std::make_unique<FileSys::RomFSFactory>(
*this, system.GetContentProvider(), system.GetFileSystemController()));
}
is_loaded = true;
return load_result;
}
ResultStatus AppLoader_NCA::ReadRomFS(FileSys::VirtualFile& dir) {
2021-04-28 05:28:56 +02:00
if (nca == nullptr) {
2020-12-28 16:15:37 +01:00
return ResultStatus::ErrorNotInitialized;
2021-04-28 05:28:56 +02:00
}
if (nca->GetRomFS() == nullptr || nca->GetRomFS()->GetSize() == 0) {
2020-12-28 16:15:37 +01:00
return ResultStatus::ErrorNoRomFS;
2021-04-28 05:28:56 +02:00
}
2020-12-28 16:15:37 +01:00
dir = nca->GetRomFS();
return ResultStatus::Success;
}
u64 AppLoader_NCA::ReadRomFSIVFCOffset() const {
2021-04-28 05:28:56 +02:00
if (nca == nullptr) {
2020-12-28 16:15:37 +01:00
return 0;
2021-04-28 05:28:56 +02:00
}
2020-12-28 16:15:37 +01:00
return nca->GetBaseIVFCOffset();
}
ResultStatus AppLoader_NCA::ReadProgramId(u64& out_program_id) {
2021-04-28 05:28:56 +02:00
if (nca == nullptr || nca->GetStatus() != ResultStatus::Success) {
2020-12-28 16:15:37 +01:00
return ResultStatus::ErrorNotInitialized;
2021-04-28 05:28:56 +02:00
}
2020-12-28 16:15:37 +01:00
out_program_id = nca->GetTitleId();
return ResultStatus::Success;
}
ResultStatus AppLoader_NCA::ReadBanner(std::vector<u8>& buffer) {
2021-04-28 05:28:56 +02:00
if (nca == nullptr || nca->GetStatus() != ResultStatus::Success) {
2020-12-28 16:15:37 +01:00
return ResultStatus::ErrorNotInitialized;
2021-04-28 05:28:56 +02:00
}
2020-12-28 16:15:37 +01:00
const auto logo = nca->GetLogoPartition();
2021-04-28 05:28:56 +02:00
if (logo == nullptr) {
2020-12-28 16:15:37 +01:00
return ResultStatus::ErrorNoIcon;
2021-04-28 05:28:56 +02:00
}
2020-12-28 16:15:37 +01:00
buffer = logo->GetFile("StartupMovie.gif")->ReadAllBytes();
return ResultStatus::Success;
}
ResultStatus AppLoader_NCA::ReadLogo(std::vector<u8>& buffer) {
2021-04-28 05:28:56 +02:00
if (nca == nullptr || nca->GetStatus() != ResultStatus::Success) {
2020-12-28 16:15:37 +01:00
return ResultStatus::ErrorNotInitialized;
2021-04-28 05:28:56 +02:00
}
2020-12-28 16:15:37 +01:00
const auto logo = nca->GetLogoPartition();
2021-04-28 05:28:56 +02:00
if (logo == nullptr) {
2020-12-28 16:15:37 +01:00
return ResultStatus::ErrorNoIcon;
2021-04-28 05:28:56 +02:00
}
2020-12-28 16:15:37 +01:00
buffer = logo->GetFile("NintendoLogo.png")->ReadAllBytes();
return ResultStatus::Success;
}
ResultStatus AppLoader_NCA::ReadNSOModules(Modules& modules) {
if (directory_loader == nullptr) {
return ResultStatus::ErrorNotInitialized;
}
return directory_loader->ReadNSOModules(modules);
}
} // namespace Loader