[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 #pragma once
#include <nx/aset.h> #include <nx/aset.h>
#include <fnd/MemoryBlob.h>
#include <fnd/List.h> #include <fnd/List.h>
#include <fnd/ISerialiseableBinary.h> #include <fnd/ISerialisable.h>
namespace nx namespace nx
{ {
class AssetHeader : class AssetHeader :
public fnd::ISerialiseableBinary public fnd::ISerialisable
{ {
public: public:
struct sSection struct sSection
{ {
uint64_t offset; uint64_t offset;
uint64_t size; 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();
AssetHeader(const AssetHeader& other); AssetHeader(const AssetHeader& other);
AssetHeader(const byte_t* bytes, size_t len);
bool operator==(const AssetHeader& other) const; bool operator==(const AssetHeader& other) const;
bool operator!=(const AssetHeader& other) const; bool operator!=(const AssetHeader& other) const;
void operator=(const AssetHeader& other); void operator=(const AssetHeader& other);
// to be used after export
const byte_t* getBytes() const;
size_t getSize() const;
// export/import binary // export/import binary
void exportBinary(); void toBytes();
void importBinary(const byte_t* bytes, size_t len); void fromBytes(const byte_t* bytes, size_t len);
const fnd::Vec<byte_t>& getBytes() const;
// variables // variables
void clear(); void clear();
@ -64,16 +42,12 @@ namespace nx
const std::string kModuleName = "NRO_ASSET_HEADER"; const std::string kModuleName = "NRO_ASSET_HEADER";
// binary // binary
fnd::MemoryBlob mBinaryBlob; fnd::Vec<byte_t> mRawBinary;
// data // data
sSection mIconInfo; sSection mIconInfo;
sSection mNacpInfo; sSection mNacpInfo;
sSection mRomfsInfo; 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) nx::AssetHeader::AssetHeader(const AssetHeader& other)
{ {
copyFrom(other); *this = other;
}
nx::AssetHeader::AssetHeader(const byte_t* bytes, size_t len)
{
importBinary(bytes, len);
} }
bool nx::AssetHeader::operator==(const AssetHeader& other) const 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 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) 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(); mRawBinary.alloc(sizeof(sAssetHeader));
} nx::sAssetHeader* hdr = (nx::sAssetHeader*)mRawBinary.data();
size_t nx::AssetHeader::getSize() const
{
return mBinaryBlob.getSize();
}
void nx::AssetHeader::exportBinary()
{
mBinaryBlob.alloc(sizeof(sAssetHeader));
nx::sAssetHeader* hdr = (nx::sAssetHeader*)mBinaryBlob.getBytes();
// set header identifers // set header identifers
hdr->signature = aset::kAssetSig; hdr->signature = aset::kAssetSig;
@ -62,7 +52,7 @@ void nx::AssetHeader::exportBinary()
hdr->romfs.size = mRomfsInfo.size; 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 // check input data size
if (len < sizeof(sAssetHeader)) if (len < sizeof(sAssetHeader))
@ -74,11 +64,11 @@ void nx::AssetHeader::importBinary(const byte_t* bytes, size_t len)
clear(); clear();
// allocate internal local binary copy // allocate internal local binary copy
mBinaryBlob.alloc(sizeof(sAssetHeader)); mRawBinary.alloc(sizeof(sAssetHeader));
memcpy(mBinaryBlob.getBytes(), bytes, mBinaryBlob.getSize()); memcpy(mRawBinary.data(), bytes, mRawBinary.size());
// get sAssetHeader ptr // get sAssetHeader ptr
const nx::sAssetHeader* hdr = (const nx::sAssetHeader*)mBinaryBlob.getBytes(); const nx::sAssetHeader* hdr = (const nx::sAssetHeader*)mRawBinary.data();
// check NRO signature // check NRO signature
if (hdr->signature.get() != aset::kAssetSig) 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(); mRomfsInfo.size = hdr->romfs.size.get();
} }
const fnd::Vec<byte_t>& nx::AssetHeader::getBytes() const
{
return mRawBinary;
}
void nx::AssetHeader::clear() void nx::AssetHeader::clear()
{ {
mBinaryBlob.clear(); mRawBinary.clear();
memset(&mIconInfo, 0, sizeof(mIconInfo)); memset(&mIconInfo, 0, sizeof(mIconInfo));
memset(&mNacpInfo, 0, sizeof(mNacpInfo)); memset(&mNacpInfo, 0, sizeof(mNacpInfo));
memset(&mRomfsInfo, 0, sizeof(mRomfsInfo)); memset(&mRomfsInfo, 0, sizeof(mRomfsInfo));
@ -137,17 +132,3 @@ void nx::AssetHeader::setRomfsInfo(const sSection& info)
{ {
mRomfsInfo = 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;
}