2018-06-10 15:48:04 +00:00
|
|
|
#include "AssetProcess.h"
|
|
|
|
|
2021-09-30 11:41:17 +00:00
|
|
|
#include "util.h"
|
2018-06-10 15:48:04 +00:00
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
nstool::AssetProcess::AssetProcess() :
|
2021-09-30 11:41:17 +00:00
|
|
|
mModuleName("nstool::AssetProcess::"),
|
2018-09-23 03:29:22 +00:00
|
|
|
mFile(),
|
2021-09-28 11:15:54 +00:00
|
|
|
mCliOutputMode(true, false, false, false),
|
2018-06-10 15:48:04 +00:00
|
|
|
mVerify(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
void nstool::AssetProcess::process()
|
2018-06-10 15:48:04 +00:00
|
|
|
{
|
|
|
|
importHeader();
|
2021-09-28 11:15:54 +00:00
|
|
|
if (mCliOutputMode.show_basic_info)
|
2018-06-18 15:30:19 +00:00
|
|
|
displayHeader();
|
2018-06-10 15:48:04 +00:00
|
|
|
processSections();
|
2021-09-28 11:15:54 +00:00
|
|
|
}
|
2018-06-10 15:48:04 +00:00
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
void nstool::AssetProcess::setInputFile(const std::shared_ptr<tc::io::IStream>& file)
|
2018-06-10 15:48:04 +00:00
|
|
|
{
|
|
|
|
mFile = file;
|
|
|
|
}
|
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
void nstool::AssetProcess::setCliOutputMode(CliOutputMode type)
|
2018-06-10 15:48:04 +00:00
|
|
|
{
|
2018-06-18 15:30:19 +00:00
|
|
|
mCliOutputMode = type;
|
2018-06-10 15:48:04 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
void nstool::AssetProcess::setVerifyMode(bool verify)
|
2018-06-10 15:48:04 +00:00
|
|
|
{
|
|
|
|
mVerify = verify;
|
|
|
|
}
|
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
void nstool::AssetProcess::setListFs(bool list)
|
2018-06-10 15:48:04 +00:00
|
|
|
{
|
|
|
|
mRomfs.setListFs(list);
|
|
|
|
}
|
|
|
|
|
2021-09-30 11:41:17 +00:00
|
|
|
void nstool::AssetProcess::setIconExtractPath(const tc::io::Path& path)
|
2018-06-10 15:48:04 +00:00
|
|
|
{
|
|
|
|
mIconExtractPath = path;
|
|
|
|
}
|
|
|
|
|
2021-09-30 11:41:17 +00:00
|
|
|
void nstool::AssetProcess::setNacpExtractPath(const tc::io::Path& path)
|
2018-06-10 15:48:04 +00:00
|
|
|
{
|
|
|
|
mNacpExtractPath = path;
|
|
|
|
}
|
|
|
|
|
2021-09-30 11:41:17 +00:00
|
|
|
void nstool::AssetProcess::setRomfsExtractPath(const tc::io::Path& path)
|
2018-06-10 15:48:04 +00:00
|
|
|
{
|
|
|
|
mRomfs.setExtractPath(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
void nstool::AssetProcess::importHeader()
|
2018-06-10 15:48:04 +00:00
|
|
|
{
|
2021-09-28 11:15:54 +00:00
|
|
|
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)))
|
2018-06-10 15:48:04 +00:00
|
|
|
{
|
2021-09-30 11:41:17 +00:00
|
|
|
throw tc::Exception(mModuleName, "Corrupt ASET: file too small");
|
2018-06-10 15:48:04 +00:00
|
|
|
}
|
|
|
|
|
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());
|
2018-06-10 15:48:04 +00:00
|
|
|
|
2018-06-24 15:01:16 +00:00
|
|
|
mHdr.fromBytes(scratch.data(), scratch.size());
|
2018-06-10 15:48:04 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
void nstool::AssetProcess::processSections()
|
2018-06-10 15:48:04 +00:00
|
|
|
{
|
2021-09-30 11:41:17 +00:00
|
|
|
int64_t file_size = mFile->length();
|
2018-06-10 15:48:04 +00:00
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
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");
|
2018-06-10 15:48:04 +00:00
|
|
|
|
2021-09-30 11:41:17 +00:00
|
|
|
writeSubStreamToFile(mFile, mHdr.getIconInfo().offset, mHdr.getIconInfo().size, mIconExtractPath.get());
|
2018-06-10 15:48:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 15:01:54 +00:00
|
|
|
if (mHdr.getNacpInfo().size > 0)
|
2018-06-10 15:48:04 +00:00
|
|
|
{
|
2021-09-28 11:15:54 +00:00
|
|
|
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");
|
2018-06-10 15:48:04 +00:00
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
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));
|
2018-06-18 15:30:19 +00:00
|
|
|
mNacp.setCliOutputMode(mCliOutputMode);
|
2018-06-11 15:01:54 +00:00
|
|
|
mNacp.setVerifyMode(mVerify);
|
|
|
|
|
|
|
|
mNacp.process();
|
2018-06-10 15:48:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mHdr.getRomfsInfo().size > 0)
|
|
|
|
{
|
2021-09-28 11:15:54 +00:00
|
|
|
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");
|
2018-06-10 15:48:04 +00:00
|
|
|
|
2021-09-30 11:41:17 +00:00
|
|
|
mRomfs.setInputFile(std::make_shared<tc::io::SubStream>(mFile, mHdr.getRomfsInfo().offset, mHdr.getRomfsInfo().size));
|
2018-06-18 15:30:19 +00:00
|
|
|
mRomfs.setCliOutputMode(mCliOutputMode);
|
2018-06-10 15:48:04 +00:00
|
|
|
mRomfs.setVerifyMode(mVerify);
|
|
|
|
|
|
|
|
mRomfs.process();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
void nstool::AssetProcess::displayHeader()
|
2018-06-10 15:48:04 +00:00
|
|
|
{
|
2021-09-28 11:15:54 +00:00
|
|
|
if (mCliOutputMode.show_layout)
|
2018-06-10 15:48:04 +00:00
|
|
|
{
|
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);
|
2018-06-10 15:48:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|