Port some of hsfload.c

This commit is contained in:
dbalatoni13 2025-04-10 01:09:20 +02:00
parent 909c743527
commit f31f250258
6 changed files with 1118 additions and 228 deletions

View file

@ -1,3 +1,4 @@
#include "game/hsfformat.h"
#include <cassert>
#include <cstdio>
#include <cstdlib>
@ -5,8 +6,7 @@
#include <ext_math.h>
#include <unordered_set>
extern "C"
{
extern "C" {
#include "port/byteswap.h"
typedef struct AnimData32b {
@ -54,7 +54,7 @@ template <typename T> [[nodiscard]] constexpr T bswap16(T val) noexcept
union {
u16 u;
T t;
} v{.t = val};
} v { .t = val };
#if __GNUC__
v.u = __builtin_bswap16(v.u);
#elif _WIN32
@ -71,14 +71,13 @@ template <typename T> [[nodiscard]] constexpr T bswap32(T val) noexcept
union {
u32 u;
T t;
} v{.t = val};
} v { .t = val };
#if __GNUC__
v.u = __builtin_bswap32(v.u);
#elif _WIN32
v.u = _byteswap_ulong(v.u);
#else
v.u = ((v.u & 0x0000FFFF) << 16) | ((v.u & 0xFFFF0000) >> 16) | ((v.u & 0x00FF00FF) << 8) |
((v.u & 0xFF00FF00) >> 8);
v.u = ((v.u & 0x0000FFFF) << 16) | ((v.u & 0xFFFF0000) >> 16) | ((v.u & 0x00FF00FF) << 8) | ((v.u & 0xFF00FF00) >> 8);
#endif
return v.t;
}
@ -104,14 +103,11 @@ static std::unordered_set<void *> sVisitedPtrs;
template <typename B, typename T> T *offset_ptr(B &base, T *ptr)
{
return reinterpret_cast<T *>(reinterpret_cast<uintptr_t>(&base) +
reinterpret_cast<uintptr_t>(ptr));
return reinterpret_cast<T *>(reinterpret_cast<uintptr_t>(&base) + reinterpret_cast<uintptr_t>(ptr));
}
template <typename B, typename T> T *offset_ptr(B &base, T *ptr, void *extra)
{
return reinterpret_cast<T *>(reinterpret_cast<uintptr_t>(&base) +
reinterpret_cast<uintptr_t>(ptr) +
reinterpret_cast<uintptr_t>(extra));
return reinterpret_cast<T *>(reinterpret_cast<uintptr_t>(&base) + reinterpret_cast<uintptr_t>(ptr) + reinterpret_cast<uintptr_t>(extra));
}
template <typename B, typename T> static inline void bswap(B &base, T &data);
@ -122,15 +118,12 @@ template <typename B, typename P> void bswap(B &base, P *&ptr)
template <typename B, typename T> void bswap(B &base, T *&ptr, s32 count)
{
ptr = bswap32(ptr);
if (ptr == nullptr)
{
if (ptr == nullptr) {
return;
}
T *objBase = offset_ptr(base, ptr);
for (s32 i = 0; i < count; ++i)
{
if (sVisitedPtrs.contains(objBase))
{
for (s32 i = 0; i < count; ++i) {
if (sVisitedPtrs.contains(objBase)) {
continue;
}
sVisitedPtrs.insert(objBase);
@ -141,13 +134,11 @@ template <typename B, typename T> void bswap(B &base, T *&ptr, s32 count)
template <typename B, typename T> void bswap_list(B &base, T **&ptr)
{
ptr = bswap32(ptr);
if (ptr == nullptr)
{
if (ptr == nullptr) {
return;
}
T **objBase = offset_ptr(base, ptr);
while (*objBase != nullptr)
{
while (*objBase != nullptr) {
bswap(base, *objBase, 1);
++objBase;
}
@ -155,8 +146,7 @@ template <typename B, typename T> void bswap_list(B &base, T **&ptr)
template <typename B, typename T> void bswap_list(B &base, T *(&ptr)[])
{
T **objBase = ptr;
while (*objBase != nullptr)
{
while (*objBase != nullptr) {
bswap(base, *objBase, 1);
++objBase;
}
@ -164,8 +154,7 @@ template <typename B, typename T> void bswap_list(B &base, T *(&ptr)[])
template <typename B, typename T> void bswap_flat(B &base, T *start, s32 count)
{
T *objBase = start;
for (s32 i = 0; i < count; ++i)
{
for (s32 i = 0; i < count; ++i) {
bswap(base, objBase[i]);
}
}
@ -342,6 +331,282 @@ template <typename B> void bswap(B &base, HsfHeader &obj)
bswap(base, obj.string);
}
template <typename B> void bswap(B &base, HsfCluster32b &obj, HsfCluster &dest)
{
bswap(base, obj.name[0]);
bswap(base, obj.name[1]);
bswap(base, obj.targetName);
bswap(base, obj.part);
bswap(base, obj.index);
bswap_flat(base, obj.weight, sizeof(obj.weight) / sizeof(float));
bswap(base, obj.type);
bswap(base, obj.vertexCnt);
bswap(base, obj.vertex);
dest.name[0] = reinterpret_cast<char *>(obj.name[0]);
dest.name[1] = reinterpret_cast<char *>(obj.name[1]);
dest.targetName = reinterpret_cast<char *>(obj.targetName);
dest.index = obj.index;
std::copy(std::begin(obj.weight), std::end(obj.weight), dest.weight);
dest.adjusted = obj.adjusted;
dest.unk95 = obj.unk95;
dest.type = obj.type;
dest.vertexCnt = obj.vertexCnt;
dest.vertex = reinterpret_cast<HsfBuffer **>(obj.vertex);
}
template <typename B> void bswap(B &base, HsfAttribute32b &obj, HsfAttribute &dest)
{
bswap(base, obj.name);
bswap(base, obj.unk04);
bswap(base, obj.unk0C);
bswap(base, obj.unk14);
bswap(base, obj.unk20);
bswap(base, obj.unk28);
bswap(base, obj.unk2C);
bswap(base, obj.unk30);
bswap(base, obj.unk34);
bswap(base, obj.wrap_s);
bswap(base, obj.wrap_t);
bswap(base, obj.unk78);
bswap(base, obj.flag);
bswap(base, obj.bitmap);
dest.name = reinterpret_cast<char *>(obj.name);
dest.unk04 = reinterpret_cast<struct hsfdraw_struct_01 *>(obj.unk04);
std::copy(std::begin(obj.unk8), std::end(obj.unk8), dest.unk8);
dest.unk0C = obj.unk0C;
std::copy(std::begin(obj.unk10), std::end(obj.unk10), dest.unk10);
dest.unk14 = obj.unk14;
std::copy(std::begin(obj.unk18), std::end(obj.unk18), dest.unk18);
dest.unk20 = obj.unk20;
std::copy(std::begin(obj.unk24), std::end(obj.unk24), dest.unk24);
dest.unk28 = obj.unk28;
dest.unk2C = obj.unk2C;
dest.unk30 = obj.unk30;
dest.unk34 = obj.unk34;
std::copy(std::begin(obj.unk38), std::end(obj.unk38), dest.unk38);
dest.wrap_s = obj.wrap_s;
dest.wrap_t = obj.wrap_t;
std::copy(std::begin(obj.unk6C), std::end(obj.unk6C), dest.unk6C);
dest.unk78 = obj.unk78;
dest.flag = obj.flag;
dest.bitmap = reinterpret_cast<HsfBitmap *>(obj.bitmap);
}
template <typename B> void bswap(B &base, HsfMaterial32b &obj, HsfMaterial &dest)
{
bswap(base, obj.name);
bswap(base, obj.pass);
bswap(base, obj.hilite_scale);
bswap(base, obj.unk18);
bswap(base, obj.invAlpha);
bswap_flat(base, obj.unk20, sizeof(obj.unk20) / sizeof(float));
bswap(base, obj.refAlpha);
bswap(base, obj.unk2C);
bswap(base, obj.flags);
bswap(base, obj.numAttrs);
bswap(base, obj.attrs);
dest.name = reinterpret_cast<char *>(obj.name);
std::copy(std::begin(obj.unk4), std::end(obj.unk4), dest.unk4);
dest.pass = obj.pass;
dest.vtxMode = obj.vtxMode;
std::copy(std::begin(obj.litColor), std::end(obj.litColor), dest.litColor);
std::copy(std::begin(obj.color), std::end(obj.color), dest.color);
std::copy(std::begin(obj.shadowColor), std::end(obj.shadowColor), dest.shadowColor);
dest.hilite_scale = obj.hilite_scale;
dest.unk18 = obj.unk18;
dest.invAlpha = obj.invAlpha;
std::copy(std::begin(obj.unk20), std::end(obj.unk20), dest.unk20);
dest.refAlpha = obj.refAlpha;
dest.unk2C = obj.unk2C;
dest.flags = obj.flags;
dest.numAttrs = obj.numAttrs;
dest.attrs = reinterpret_cast<s32 *>(obj.attrs);
}
template <typename B> void bswap(B &base, HsfScene &obj)
{
u32 fogType = static_cast<u32>(obj.fogType);
fogType = bswap32(fogType);
obj.fogType = static_cast<GXFogType>(fogType);
bswap(base, obj.start);
bswap(base, obj.end);
}
template <typename B> void bswap(B &base, HsfBuffer32b &obj, HsfBuffer &dest)
{
bswap(base, obj.name);
bswap(base, obj.count);
bswap(base, obj.data);
dest.name = reinterpret_cast<char *>(obj.name);
dest.count = obj.count;
dest.data = reinterpret_cast<void *>(obj.data);
}
template <typename B> void bswap(B &base, HsfMatrix &obj)
{
bswap(base, obj.base_idx);
bswap(base, obj.count);
obj.data = (Mtx *)((uintptr_t)&obj + sizeof(0xC)); // hardcoded for 64 bit support
for (s32 i = 0; i < obj.count; i++) {
for (s32 j = 0; j < 3; j++) {
bswap_flat(base, obj.data[i][j], 4);
}
}
}
template <typename B> void bswap(B &base, HsfPalette32b &obj, HsfPalette &dest)
{
bswap(base, obj.name);
bswap(base, obj.unk);
bswap(base, obj.palSize);
bswap(base, obj.data);
dest.name = reinterpret_cast<char *>(obj.name);
dest.unk = obj.unk;
dest.palSize = obj.palSize;
dest.data = reinterpret_cast<u16 *>(obj.data);
}
template <typename B> void bswap(B &base, HsfPart32b &obj, HsfPart &dest)
{
bswap(base, obj.name);
bswap(base, obj.count);
bswap(base, obj.vertex);
dest.name = reinterpret_cast<char *>(obj.name);
dest.count = obj.count;
dest.vertex = reinterpret_cast<u16 *>(obj.vertex);
}
template <typename B> void bswap(B &base, HsfBitmap32b &obj, HsfBitmap &dest)
{
bswap(base, obj.name);
bswap(base, obj.maxLod);
bswap(base, obj.sizeX);
bswap(base, obj.sizeY);
bswap(base, obj.palSize);
bswap(base, obj.palData);
bswap(base, obj.unk);
bswap(base, obj.data);
dest.name = reinterpret_cast<char *>(obj.name);
dest.maxLod = obj.maxLod;
dest.dataFmt = obj.dataFmt;
dest.pixSize = obj.pixSize;
dest.sizeX = obj.sizeX;
dest.sizeY = obj.sizeY;
dest.palSize = obj.palSize;
dest.tint = obj.tint;
dest.palData = reinterpret_cast<void *>(obj.palData);
dest.unk = obj.unk;
dest.data = reinterpret_cast<void *>(obj.data);
}
template <typename B> void bswap(B &base, HsfMapAttr32b &obj, HsfMapAttr &dest)
{
bswap(base, obj.minX);
bswap(base, obj.minZ);
bswap(base, obj.maxX);
bswap(base, obj.maxZ);
bswap(base, obj.data);
bswap(base, obj.dataLen);
dest.minX = obj.minX;
dest.minZ = obj.minZ;
dest.maxX = obj.maxZ;
dest.data = reinterpret_cast<u16 *>(obj.data);
dest.dataLen = obj.dataLen;
}
template <typename B> void bswap(B &base, HsfTransform &obj)
{
bswap(base, obj.pos);
bswap(base, obj.rot);
bswap(base, obj.scale);
}
template <typename B> void bswap(B &base, HsfSkeleton32b &obj, HsfSkeleton &dest)
{
bswap(base, obj.name);
bswap(base, obj.transform);
dest.name = reinterpret_cast<char *>(obj.name);
dest.transform = obj.transform;
}
template <typename B> void bswap(B &base, HsfShape32b &obj, HsfShape &dest)
{
bswap(base, obj.name);
bswap(base, obj.vertex);
dest.name = reinterpret_cast<char *>(obj.name);
dest.vertex = reinterpret_cast<HsfBuffer **>(obj.vertex);
}
template <typename B> void bswap(B &base, HsfCenvSingle &obj)
{
bswap(base, obj.target);
bswap(base, obj.pos);
bswap(base, obj.posCnt);
bswap(base, obj.normal);
bswap(base, obj.normalCnt);
}
template <typename B> void bswap(B &base, HsfCenv32b &obj, HsfCenv &dest)
{
bswap(base, obj.name);
bswap(base, obj.singleData);
bswap(base, obj.dualData);
bswap(base, obj.multiData);
bswap(base, obj.singleCount);
bswap(base, obj.dualCount);
bswap(base, obj.multiCount);
bswap(base, obj.vtxCount);
bswap(base, obj.copyCount);
dest.name = reinterpret_cast<char *>(obj.name);
dest.singleData = reinterpret_cast<HsfCenvSingle *>(obj.singleData);
dest.dualData = reinterpret_cast<HsfCenvDual *>(obj.dualData);
dest.multiData = reinterpret_cast<HsfCenvMulti *>(obj.multiData);
dest.singleCount = obj.singleCount;
dest.dualCount = obj.dualCount;
dest.multiCount = obj.multiCount;
dest.vtxCount = obj.vtxCount;
dest.copyCount = obj.copyCount;
}
template <typename B> void bswap(B &base, HsfObject32b &obj, HsfObject &dest)
{
bswap(base, obj.name);
bswap(base, obj.type);
bswap(base, obj.constData);
bswap(base, obj.flags);
dest.name = reinterpret_cast<char *>(obj.name);
dest.type = obj.type;
dest.constData = reinterpret_cast<void *>(obj.constData);
dest.flags = obj.flags;
}
void byteswap_u16(u16 *src)
{
bswap(*src, *src);
sVisitedPtrs.clear();
}
void byteswap_s16(s16 *src)
{
bswap(*src, *src);
sVisitedPtrs.clear();
}
void byteswap_u32(u32 *src)
{
bswap(*src, *src);
@ -354,6 +619,20 @@ void byteswap_s32(s32 *src)
sVisitedPtrs.clear();
}
void byteswap_hsfvec3f(HsfVector3f *src)
{
auto *vec = reinterpret_cast<Vec *>(src);
bswap(*vec, *vec);
sVisitedPtrs.clear();
}
void byteswap_hsfvec2f(HsfVector2f *src)
{
auto *vec = reinterpret_cast<Vec2f *>(src);
bswap(*vec, *vec);
sVisitedPtrs.clear();
}
void byteswap_animdata(void *src, AnimData *dest)
{
auto *anim = reinterpret_cast<AnimData32b *>(src);
@ -399,3 +678,93 @@ void byteswap_hsfheader(HsfHeader *src)
bswap(*src, *src);
sVisitedPtrs.clear();
}
void byteswap_hsfcluster(HsfCluster32b *src, HsfCluster *dest)
{
bswap(*src, *src, *dest);
sVisitedPtrs.clear();
}
void byteswap_hsfattribute(HsfAttribute32b *src, HsfAttribute *dest)
{
bswap(*src, *src, *dest);
sVisitedPtrs.clear();
}
void byteswap_hsfmaterial(HsfMaterial32b *src, HsfMaterial *dest)
{
bswap(*src, *src, *dest);
sVisitedPtrs.clear();
}
void byteswap_hsfscene(HsfScene *src)
{
bswap(*src, *src);
sVisitedPtrs.clear();
}
void byteswap_hsfbuffer(HsfBuffer32b *src, HsfBuffer *dest)
{
bswap(*src, *src, *dest);
sVisitedPtrs.clear();
}
void byteswap_hsfmatrix(HsfMatrix *src)
{
bswap(*src, *src);
sVisitedPtrs.clear();
}
void byteswap_hsfpalette(HsfPalette32b *src, HsfPalette *dest)
{
bswap(*src, *src, *dest);
sVisitedPtrs.clear();
}
void byteswap_hsfpart(HsfPart32b *src, HsfPart *dest)
{
bswap(*src, *src, *dest);
sVisitedPtrs.clear();
}
void byteswap_hsfbitmap(HsfBitmap32b *src, HsfBitmap *dest)
{
bswap(*src, *src, *dest);
sVisitedPtrs.clear();
}
void byteswap_hsfmapattr(HsfMapAttr32b *src, HsfMapAttr *dest)
{
bswap(*src, *src, *dest);
sVisitedPtrs.clear();
}
void byteswap_hsfskeleton(HsfSkeleton32b *src, HsfSkeleton *dest)
{
bswap(*src, *src, *dest);
sVisitedPtrs.clear();
}
void byteswap_hsfshape(HsfShape32b *src, HsfShape *dest)
{
bswap(*src, *src, *dest);
sVisitedPtrs.clear();
}
void byteswap_hsfcenvsingle(HsfCenvSingle *src)
{
bswap(*src, *src);
sVisitedPtrs.clear();
}
void byteswap_hsfcenv(HsfCenv32b *src, HsfCenv *dest)
{
bswap(*src, *src, *dest);
sVisitedPtrs.clear();
}
void byteswap_hsfobject(HsfObject32b *src, HsfObject *dest)
{
bswap(*src, *src, *dest);
sVisitedPtrs.clear();
}