[nx-hb] Update to libfnd redesign.

This commit is contained in:
jakcron 2018-06-24 12:52:06 +08:00
parent 313422bd57
commit 23bda04f35
2 changed files with 27 additions and 72 deletions

View file

@ -1,53 +1,31 @@
#pragma once
#include <nx/aset.h>
#include <fnd/MemoryBlob.h>
#include <fnd/List.h>
#include <fnd/ISerialiseableBinary.h>
#include <fnd/ISerialisable.h>
namespace nx
{
class AssetHeader :
public fnd::ISerialiseableBinary
public fnd::ISerialisable
{
public:
struct sSection
{
uint64_t offset;
uint64_t size;
void operator=(const sSection& other)
{
offset = other.offset;
size = other.size;
}
bool operator==(const sSection& other) const
{
return (offset == other.offset) \
&& (size == other.size);
}
bool operator!=(const sSection& other) const
{
return !(*this == other);
}
};
AssetHeader();
AssetHeader(const AssetHeader& other);
AssetHeader(const byte_t* bytes, size_t len);
bool operator==(const AssetHeader& other) const;
bool operator!=(const AssetHeader& other) const;
void operator=(const AssetHeader& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary
void exportBinary();
void importBinary(const byte_t* bytes, size_t len);
void toBytes();
void fromBytes(const byte_t* bytes, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
// variables
void clear();
@ -64,16 +42,12 @@ namespace nx
const std::string kModuleName = "NRO_ASSET_HEADER";
// binary
fnd::MemoryBlob mBinaryBlob;
fnd::Vec<byte_t> mRawBinary;
// data
sSection mIconInfo;
sSection mNacpInfo;
sSection mRomfsInfo;
// helpers
bool isEqual(const AssetHeader& other) const;
void copyFrom(const AssetHeader& other);
};
}

View file

@ -7,17 +7,14 @@ nx::AssetHeader::AssetHeader()
nx::AssetHeader::AssetHeader(const AssetHeader& other)
{
copyFrom(other);
}
nx::AssetHeader::AssetHeader(const byte_t* bytes, size_t len)
{
importBinary(bytes, len);
*this = other;
}
bool nx::AssetHeader::operator==(const AssetHeader& other) const
{
return isEqual(other);
return (mIconInfo == other.mIconInfo) \
&& (mNacpInfo == other.mNacpInfo) \
&& (mRomfsInfo == other.mRomfsInfo);
}
bool nx::AssetHeader::operator!=(const AssetHeader& other) const
@ -27,23 +24,16 @@ bool nx::AssetHeader::operator!=(const AssetHeader& other) const
void nx::AssetHeader::operator=(const AssetHeader& other)
{
copyFrom(other);
mRawBinary = other.mRawBinary;
mIconInfo = other.mIconInfo;
mNacpInfo = other.mNacpInfo;
mRomfsInfo = other.mRomfsInfo;
}
const byte_t* nx::AssetHeader::getBytes() const
void nx::AssetHeader::toBytes()
{
return mBinaryBlob.getBytes();
}
size_t nx::AssetHeader::getSize() const
{
return mBinaryBlob.getSize();
}
void nx::AssetHeader::exportBinary()
{
mBinaryBlob.alloc(sizeof(sAssetHeader));
nx::sAssetHeader* hdr = (nx::sAssetHeader*)mBinaryBlob.getBytes();
mRawBinary.alloc(sizeof(sAssetHeader));
nx::sAssetHeader* hdr = (nx::sAssetHeader*)mRawBinary.data();
// set header identifers
hdr->signature = aset::kAssetSig;
@ -62,7 +52,7 @@ void nx::AssetHeader::exportBinary()
hdr->romfs.size = mRomfsInfo.size;
}
void nx::AssetHeader::importBinary(const byte_t* bytes, size_t len)
void nx::AssetHeader::fromBytes(const byte_t* bytes, size_t len)
{
// check input data size
if (len < sizeof(sAssetHeader))
@ -74,11 +64,11 @@ void nx::AssetHeader::importBinary(const byte_t* bytes, size_t len)
clear();
// allocate internal local binary copy
mBinaryBlob.alloc(sizeof(sAssetHeader));
memcpy(mBinaryBlob.getBytes(), bytes, mBinaryBlob.getSize());
mRawBinary.alloc(sizeof(sAssetHeader));
memcpy(mRawBinary.data(), bytes, mRawBinary.size());
// get sAssetHeader ptr
const nx::sAssetHeader* hdr = (const nx::sAssetHeader*)mBinaryBlob.getBytes();
const nx::sAssetHeader* hdr = (const nx::sAssetHeader*)mRawBinary.data();
// check NRO signature
if (hdr->signature.get() != aset::kAssetSig)
@ -100,9 +90,14 @@ void nx::AssetHeader::importBinary(const byte_t* bytes, size_t len)
mRomfsInfo.size = hdr->romfs.size.get();
}
const fnd::Vec<byte_t>& nx::AssetHeader::getBytes() const
{
return mRawBinary;
}
void nx::AssetHeader::clear()
{
mBinaryBlob.clear();
mRawBinary.clear();
memset(&mIconInfo, 0, sizeof(mIconInfo));
memset(&mNacpInfo, 0, sizeof(mNacpInfo));
memset(&mRomfsInfo, 0, sizeof(mRomfsInfo));
@ -136,18 +131,4 @@ const nx::AssetHeader::sSection& nx::AssetHeader::getRomfsInfo() const
void nx::AssetHeader::setRomfsInfo(const sSection& info)
{
mRomfsInfo = info;
}
bool nx::AssetHeader::isEqual(const AssetHeader& other) const
{
return (mIconInfo == other.mIconInfo) \
&& (mNacpInfo == other.mNacpInfo) \
&& (mRomfsInfo == other.mRomfsInfo);
}
void nx::AssetHeader::copyFrom(const AssetHeader& other)
{
mIconInfo = other.mIconInfo;
mNacpInfo = other.mNacpInfo;
mRomfsInfo = other.mRomfsInfo;
}