2018-06-10 15:48:04 +00:00
|
|
|
#include "AssetProcess.h"
|
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
#include "utils.h"
|
2018-06-10 15:48:04 +00:00
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
nstool::AssetProcess::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-28 11:15:54 +00:00
|
|
|
void nstool::AssetProcess::setIconExtractPath(const std::string& path)
|
2018-06-10 15:48:04 +00:00
|
|
|
{
|
|
|
|
mIconExtractPath = path;
|
|
|
|
}
|
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
void nstool::AssetProcess::setNacpExtractPath(const std::string& path)
|
2018-06-10 15:48:04 +00:00
|
|
|
{
|
|
|
|
mNacpExtractPath = path;
|
|
|
|
}
|
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
void nstool::AssetProcess::setRomfsExtractPath(const std::string& 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
|
|
|
tc::ByteData scratch;
|
2018-08-13 17:14:21 +00:00
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
if (mFile == nullptr)
|
2018-08-13 17:14:21 +00:00
|
|
|
{
|
2021-09-28 11:15:54 +00:00
|
|
|
throw tc::Exception(kModuleName, "No file reader set.");
|
2018-08-13 17:14:21 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
size_t file_size = tc::io::IOUtil::castInt64ToSize(mFile->length());
|
|
|
|
|
|
|
|
if (file_size < sizeof(nn::hac::sAssetHeader))
|
2018-06-10 15:48:04 +00:00
|
|
|
{
|
2021-09-28 11:15:54 +00:00
|
|
|
throw tc::Exception(kModuleName, "Corrupt ASET: file too small");
|
2018-06-10 15:48:04 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 07:17:51 +00:00
|
|
|
scratch.alloc(sizeof(nn::hac::sAssetHeader));
|
2021-09-28 11:15:54 +00:00
|
|
|
mFile->read(scratch.data(), 0, 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-28 11:15:54 +00:00
|
|
|
size_t file_size = tc::io::IOUtil::castInt64ToSize(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)
|
|
|
|
throw tc::Exception(kModuleName, "ASET geometry for icon beyond file size");
|
2018-06-10 15:48:04 +00:00
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
writeSubStreamToFile(mFile, tc::io::IOUtil::castSizeToInt64(mHdr.getIconInfo().offset), tc::io::IOUtil::castSizeToInt64(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)
|
|
|
|
throw tc::Exception(kModuleName, "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-28 11:15:54 +00:00
|
|
|
writeSubStreamToFile(mFile, tc::io::IOUtil::castSizeToInt64(mHdr.getNacpInfo().offset), tc::io::IOUtil::castSizeToInt64(mHdr.getNacpInfo().size), mNacpExtractPath.get());
|
2018-06-11 15:01:54 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
mNacp.setInputFile(std::make_shared<tc::io::SubStream>(mFile, tc::io::IOUtil::castSizeToInt64(mHdr.getNacpInfo().offset), tc::io::IOUtil::castSizeToInt64(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)
|
|
|
|
throw tc::Exception(kModuleName, "ASET geometry for romfs beyond file size");
|
2018-06-10 15:48:04 +00:00
|
|
|
|
2021-09-28 11:15:54 +00:00
|
|
|
mRomfs.setInputFile(std::make_shared<tc::io::SubStream>(mFile, tc::io::IOUtil::castSizeToInt64(mHdr.getRomfsInfo().offset), tc::io::IOUtil::castSizeToInt64(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
|
|
|
{
|
2018-08-12 05:22:07 +00:00
|
|
|
std::cout << "[ASET Header]" << std::endl;
|
|
|
|
std::cout << " Icon:" << std::endl;
|
|
|
|
std::cout << " Offset: 0x" << std::hex << mHdr.getIconInfo().offset << std::endl;
|
|
|
|
std::cout << " Size: 0x" << std::hex << mHdr.getIconInfo().size << std::endl;
|
|
|
|
std::cout << " NACP:" << std::endl;
|
|
|
|
std::cout << " Offset: 0x" << std::hex << mHdr.getNacpInfo().offset << std::endl;
|
|
|
|
std::cout << " Size: 0x" << std::hex << mHdr.getNacpInfo().size << std::endl;
|
|
|
|
std::cout << " RomFS:" << std::endl;
|
|
|
|
std::cout << " Offset: 0x" << std::hex << mHdr.getRomfsInfo().offset << std::endl;
|
|
|
|
std::cout << " Size: 0x" << std::hex << mHdr.getRomfsInfo().size << std::endl;
|
2018-06-10 15:48:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|