nstool/src/AssetProcess.cpp

135 lines
3.5 KiB
C++
Raw Normal View History

#include "AssetProcess.h"
2021-09-30 11:41:17 +00:00
#include "util.h"
nstool::AssetProcess::AssetProcess() :
2021-09-30 11:41:17 +00:00
mModuleName("nstool::AssetProcess::"),
mFile(),
mCliOutputMode(true, false, false, false),
mVerify(false)
{
}
void nstool::AssetProcess::process()
{
importHeader();
if (mCliOutputMode.show_basic_info)
displayHeader();
processSections();
}
void nstool::AssetProcess::setInputFile(const std::shared_ptr<tc::io::IStream>& file)
{
mFile = file;
}
void nstool::AssetProcess::setCliOutputMode(CliOutputMode type)
{
mCliOutputMode = type;
}
void nstool::AssetProcess::setVerifyMode(bool verify)
{
mVerify = verify;
}
void nstool::AssetProcess::setListFs(bool list)
{
mRomfs.setListFs(list);
}
2021-09-30 11:41:17 +00:00
void nstool::AssetProcess::setIconExtractPath(const tc::io::Path& path)
{
mIconExtractPath = path;
}
2021-09-30 11:41:17 +00:00
void nstool::AssetProcess::setNacpExtractPath(const tc::io::Path& path)
{
mNacpExtractPath = path;
}
2021-09-30 11:41:17 +00:00
void nstool::AssetProcess::setRomfsExtractPath(const tc::io::Path& path)
{
mRomfs.setExtractPath(path);
}
void nstool::AssetProcess::importHeader()
{
if (mFile == nullptr)
2018-08-13 17:14:21 +00:00
{
2021-09-30 11:41:17 +00:00
throw tc::Exception(mModuleName, "No file reader set.");
2018-08-13 17:14:21 +00:00
}
2021-09-30 11:41:17 +00:00
if (mFile->length() < tc::io::IOUtil::castSizeToInt64(sizeof(nn::hac::sAssetHeader)))
{
2021-09-30 11:41:17 +00:00
throw tc::Exception(mModuleName, "Corrupt ASET: file too small");
}
2021-09-30 11:41:17 +00:00
tc::ByteData scratch = tc::ByteData(sizeof(nn::hac::sAssetHeader));
mFile->seek(0, tc::io::SeekOrigin::Begin);
mFile->read(scratch.data(), scratch.size());
mHdr.fromBytes(scratch.data(), scratch.size());
}
void nstool::AssetProcess::processSections()
{
2021-09-30 11:41:17 +00:00
int64_t file_size = mFile->length();
if (mHdr.getIconInfo().size > 0 && mIconExtractPath.isSet())
{
if ((mHdr.getIconInfo().size + mHdr.getIconInfo().offset) > file_size)
2021-09-30 11:41:17 +00:00
throw tc::Exception(mModuleName, "ASET geometry for icon beyond file size");
2021-09-30 11:41:17 +00:00
writeSubStreamToFile(mFile, mHdr.getIconInfo().offset, mHdr.getIconInfo().size, mIconExtractPath.get());
}
2018-06-11 15:01:54 +00:00
if (mHdr.getNacpInfo().size > 0)
{
if ((mHdr.getNacpInfo().size + mHdr.getNacpInfo().offset) > file_size)
2021-09-30 11:41:17 +00:00
throw tc::Exception(mModuleName, "ASET geometry for nacp beyond file size");
if (mNacpExtractPath.isSet())
2018-06-11 15:01:54 +00:00
{
2021-09-30 11:41:17 +00:00
writeSubStreamToFile(mFile, mHdr.getNacpInfo().offset, mHdr.getNacpInfo().size, mNacpExtractPath.get());
2018-06-11 15:01:54 +00:00
}
2021-09-30 11:41:17 +00:00
mNacp.setInputFile(std::make_shared<tc::io::SubStream>(mFile, mHdr.getNacpInfo().offset, mHdr.getNacpInfo().size));
mNacp.setCliOutputMode(mCliOutputMode);
2018-06-11 15:01:54 +00:00
mNacp.setVerifyMode(mVerify);
mNacp.process();
}
if (mHdr.getRomfsInfo().size > 0)
{
if ((mHdr.getRomfsInfo().size + mHdr.getRomfsInfo().offset) > file_size)
2021-09-30 11:41:17 +00:00
throw tc::Exception(mModuleName, "ASET geometry for romfs beyond file size");
2021-09-30 11:41:17 +00:00
mRomfs.setInputFile(std::make_shared<tc::io::SubStream>(mFile, mHdr.getRomfsInfo().offset, mHdr.getRomfsInfo().size));
mRomfs.setCliOutputMode(mCliOutputMode);
mRomfs.setVerifyMode(mVerify);
mRomfs.process();
}
}
void nstool::AssetProcess::displayHeader()
{
if (mCliOutputMode.show_layout)
{
2021-09-30 11:41:17 +00:00
fmt::print("[ASET Header]\n");
fmt::print(" Icon:\n");
fmt::print(" Offset: 0x{:x}\n", mHdr.getIconInfo().offset);
fmt::print(" Size: 0x{:x}\n", mHdr.getIconInfo().size);
fmt::print(" NACP:\n");
fmt::print(" Offset: 0x{:x}\n", mHdr.getNacpInfo().offset);
fmt::print(" Size: 0x{:x}\n", mHdr.getNacpInfo().size);
fmt::print(" RomFS:\n");
fmt::print(" Offset: 0x{:x}\n", mHdr.getRomfsInfo().offset);
fmt::print(" Size: 0x{:x}\n", mHdr.getRomfsInfo().size);
}
}