2018-08-12 05:22:07 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
2018-08-13 17:14:21 +00:00
|
|
|
#include <fnd/SimpleFile.h>
|
2018-10-06 08:45:09 +00:00
|
|
|
#include <fnd/OffsetAdjustedIFile.h>
|
2018-08-13 17:14:21 +00:00
|
|
|
#include <fnd/Vec.h>
|
2018-06-10 15:48:04 +00:00
|
|
|
#include "AssetProcess.h"
|
|
|
|
|
|
|
|
|
|
|
|
AssetProcess::AssetProcess() :
|
2018-09-23 03:29:22 +00:00
|
|
|
mFile(),
|
2018-06-18 15:30:19 +00:00
|
|
|
mCliOutputMode(_BIT(OUTPUT_BASIC)),
|
2018-06-10 15:48:04 +00:00
|
|
|
mVerify(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssetProcess::process()
|
|
|
|
{
|
|
|
|
importHeader();
|
2018-06-18 15:30:19 +00:00
|
|
|
if (_HAS_BIT(mCliOutputMode, OUTPUT_BASIC))
|
|
|
|
displayHeader();
|
2018-06-10 15:48:04 +00:00
|
|
|
processSections();
|
|
|
|
}
|
|
|
|
|
2018-09-23 03:29:22 +00:00
|
|
|
void AssetProcess::setInputFile(const fnd::SharedPtr<fnd::IFile>& file)
|
2018-06-10 15:48:04 +00:00
|
|
|
{
|
|
|
|
mFile = file;
|
|
|
|
}
|
|
|
|
|
2018-06-18 15:30:19 +00:00
|
|
|
void 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
|
|
|
}
|
|
|
|
|
|
|
|
void AssetProcess::setVerifyMode(bool verify)
|
|
|
|
{
|
|
|
|
mVerify = verify;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssetProcess::setListFs(bool list)
|
|
|
|
{
|
|
|
|
mRomfs.setListFs(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssetProcess::setIconExtractPath(const std::string& path)
|
|
|
|
{
|
|
|
|
mIconExtractPath = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssetProcess::setNacpExtractPath(const std::string& path)
|
|
|
|
{
|
|
|
|
mNacpExtractPath = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssetProcess::setRomfsExtractPath(const std::string& path)
|
|
|
|
{
|
|
|
|
mRomfs.setExtractPath(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AssetProcess::importHeader()
|
|
|
|
{
|
2018-06-24 15:01:16 +00:00
|
|
|
fnd::Vec<byte_t> scratch;
|
2018-08-13 17:14:21 +00:00
|
|
|
|
2018-09-23 03:29:22 +00:00
|
|
|
if (*mFile == nullptr)
|
2018-08-13 17:14:21 +00:00
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "No file reader set.");
|
|
|
|
}
|
|
|
|
|
2018-09-23 03:29:22 +00:00
|
|
|
if ((*mFile)->size() < sizeof(nn::hac::sAssetHeader))
|
2018-06-10 15:48:04 +00:00
|
|
|
{
|
|
|
|
throw fnd::Exception(kModuleName, "Corrupt ASET: file too small");
|
|
|
|
}
|
|
|
|
|
2018-08-07 07:17:51 +00:00
|
|
|
scratch.alloc(sizeof(nn::hac::sAssetHeader));
|
2018-09-23 03:29:22 +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
|
|
|
}
|
|
|
|
|
|
|
|
void AssetProcess::processSections()
|
|
|
|
{
|
|
|
|
if (mHdr.getIconInfo().size > 0 && mIconExtractPath.isSet)
|
|
|
|
{
|
2018-09-23 03:29:22 +00:00
|
|
|
if ((mHdr.getIconInfo().size + mHdr.getIconInfo().offset) > (*mFile)->size())
|
2018-06-10 15:48:04 +00:00
|
|
|
throw fnd::Exception(kModuleName, "ASET geometry for icon beyond file size");
|
|
|
|
|
|
|
|
fnd::SimpleFile outfile(mIconExtractPath.var, fnd::SimpleFile::Create);
|
2018-06-24 15:01:16 +00:00
|
|
|
fnd::Vec<byte_t> cache;
|
2018-06-10 15:48:04 +00:00
|
|
|
|
|
|
|
cache.alloc(mHdr.getIconInfo().size);
|
2018-09-23 03:29:22 +00:00
|
|
|
(*mFile)->read(cache.data(), mHdr.getIconInfo().offset, cache.size());
|
2018-06-24 15:01:16 +00:00
|
|
|
outfile.write(cache.data(), cache.size());
|
2018-06-10 15:48:04 +00:00
|
|
|
outfile.close();
|
|
|
|
}
|
|
|
|
|
2018-06-11 15:01:54 +00:00
|
|
|
if (mHdr.getNacpInfo().size > 0)
|
2018-06-10 15:48:04 +00:00
|
|
|
{
|
2018-09-23 03:29:22 +00:00
|
|
|
if ((mHdr.getNacpInfo().size + mHdr.getNacpInfo().offset) > (*mFile)->size())
|
2018-06-10 15:48:04 +00:00
|
|
|
throw fnd::Exception(kModuleName, "ASET geometry for nacp beyond file size");
|
|
|
|
|
2018-06-11 15:01:54 +00:00
|
|
|
if (mNacpExtractPath.isSet)
|
|
|
|
{
|
|
|
|
fnd::SimpleFile outfile(mNacpExtractPath.var, fnd::SimpleFile::Create);
|
2018-06-24 15:01:16 +00:00
|
|
|
fnd::Vec<byte_t> cache;
|
2018-06-11 15:01:54 +00:00
|
|
|
|
|
|
|
cache.alloc(mHdr.getNacpInfo().size);
|
2018-09-23 03:29:22 +00:00
|
|
|
(*mFile)->read(cache.data(), mHdr.getNacpInfo().offset, cache.size());
|
2018-06-24 15:01:16 +00:00
|
|
|
outfile.write(cache.data(), cache.size());
|
2018-06-11 15:01:54 +00:00
|
|
|
outfile.close();
|
|
|
|
}
|
|
|
|
|
2018-10-06 08:45:09 +00:00
|
|
|
mNacp.setInputFile(new fnd::OffsetAdjustedIFile(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)
|
|
|
|
{
|
2018-09-23 03:29:22 +00:00
|
|
|
if ((mHdr.getRomfsInfo().size + mHdr.getRomfsInfo().offset) > (*mFile)->size())
|
2018-06-10 15:48:04 +00:00
|
|
|
throw fnd::Exception(kModuleName, "ASET geometry for romfs beyond file size");
|
|
|
|
|
2018-10-06 08:45:09 +00:00
|
|
|
mRomfs.setInputFile(new fnd::OffsetAdjustedIFile(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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssetProcess::displayHeader()
|
|
|
|
{
|
2018-06-18 15:30:19 +00:00
|
|
|
if (_HAS_BIT(mCliOutputMode, OUTPUT_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
|
|
|
}
|
|
|
|
}
|
|
|
|
|