diff --git a/configure.py b/configure.py index 8160fd40b..540e2517e 100644 --- a/configure.py +++ b/configure.py @@ -575,7 +575,7 @@ def MatchingFor(*versions): Object(NonMatching, "SB/Core/x/xClumpColl.cpp"), Object(NonMatching, "SB/Core/x/xEntBoulder.cpp"), Object(NonMatching, "SB/Core/x/xGrid.cpp"), - Object(NonMatching, "SB/Core/x/xJSP.cpp"), + Object(Equivalent, "SB/Core/x/xJSP.cpp"), Object(Matching, "SB/Core/x/xLightKit.cpp"), Object(Matching, "SB/Game/zCamMarker.cpp"), Object(NonMatching, "SB/Game/zGoo.cpp"), diff --git a/include/rwsdk/rwplcore.h b/include/rwsdk/rwplcore.h index 9ae185e01..19c1ca81e 100644 --- a/include/rwsdk/rwplcore.h +++ b/include/rwsdk/rwplcore.h @@ -531,6 +531,12 @@ enum RwStreamAccessType rwSTREAMACCESSTYPEFORCEENUMSIZEINT = RWFORCEENUMSIZEINT }; +struct RwMemory +{ + RwUInt8* start; + RwUInt32 length; +}; + struct RwStreamMemory { RwUInt32 position; diff --git a/src/SB/Core/x/xJSP.cpp b/src/SB/Core/x/xJSP.cpp index aa68a9a9d..533b6bdff 100644 --- a/src/SB/Core/x/xJSP.cpp +++ b/src/SB/Core/x/xJSP.cpp @@ -1,62 +1,208 @@ #include "xJSP.h" +#include "zGlobals.h" #include #include -#include "xClumpColl.h" +static RwV3d* sCurrVert; +static S32 sAtomicStartCount; +static RwV3d** sAtomicStartVert; -extern S32 sAtomicStartCount; // not exactly sure of the type -extern RwV3d* sCurrVert; // not sure if correct type. not sure what this is. -extern RwV3d* sAtomicStartVert; // I'm just going based on matt's assumption -extern RwGlobals* RwEngineInstance; - -// No dwarf info -// ghidra said return type and type of param_2 was void -// but changing it to return atomic matches. -RpAtomic* CountAtomicCB(RpAtomic* atomic, U32* param_2) +static RpAtomic* CountAtomicCB(RpAtomic* atomic, void* data) { sAtomicStartCount++; - *param_2 += atomic->geometry->mesh->totalIndicesInMesh; + *(U32*)data += atomic->geometry->mesh->totalIndicesInMesh; return atomic; } -static RpMesh* AddMeshCB(RpMesh* mesh, RpMeshHeader* header, void* param_3) +static RpMesh* AddMeshCB(RpMesh* mesh, RpMeshHeader*, void* pData) { - for (int i = 0; i < mesh->numIndices; i++) - { - **(RwV3d**)(param_3) = sCurrVert[mesh->indices[i]]; - *((S32*)(param_3)) += sizeof(RwV3d); + RwV3d** stripVert = (RwV3d**)pData; + for (U32 i = 0; i < mesh->numIndices; i++) { + **stripVert = sCurrVert[mesh->indices[i]]; + (*stripVert)++; } - return mesh; } -RpAtomic* AddAtomicCB(RpAtomic* atomic, void* data) +static RpAtomic* AddAtomicCB(RpAtomic* atomic, void* data) { // The dwarf defines this variable but it looks like it isnt used // TempAtomicList** tmpList; - sAtomicStartCount--; - sAtomicStartVert[sAtomicStartCount] = *(RwV3d*)data; + sAtomicStartVert[--sAtomicStartCount] = *(RwV3d**)data; sCurrVert = atomic->geometry->morphTarget->verts; _rpMeshHeaderForAllMeshes(atomic->geometry->mesh, (RpMeshCallBack)&AddMeshCB, data); return atomic; } -RpAtomic* AddAtomicPrecalcedVertCB(RpAtomic* atomic, void* data) +static RpAtomic* AddAtomicPrecalcedVertCB(RpAtomic* atomic, void* data) { - sAtomicStartCount--; - sAtomicStartVert[sAtomicStartCount] = *(RwV3d*)data; - data = &data + atomic->geometry->mesh->totalIndicesInMesh * 0xc; - + sAtomicStartVert[--sAtomicStartCount] = *(RwV3d**)data; + *(RwV3d**)data += atomic->geometry->mesh->totalIndicesInMesh; return atomic; } -RpAtomic* ListAtomicCB(RpAtomic* atomic, void* data) +static RpAtomic* ListAtomicCB(RpAtomic* atomic, void* data) { - // RpAtomic*** aList; <- declared in dwarf data - // ¯\_(ツ)_/¯ idk what's going on - **(RpAtomic***)data = atomic; - *(S32*)data += 4; + RpAtomic*** aList = (RpAtomic***)data; + **aList = atomic; + (*aList)++; return atomic; } + +void xJSP_MultiStreamRead(void* data, U32 size, xJSPHeader** jsp) +{ + U32 i; + RpClump* clump; + xClumpCollBSPTree* colltree; + + if (!*jsp) + { + *jsp = (xJSPHeaderGC*)RwMalloc(sizeof(xJSPHeaderGC)); + memset(*jsp, 0, sizeof(xJSPHeaderGC)); + } + + xJSPHeaderGC* hdr = (xJSPHeaderGC*)*jsp; + + __rwMark mark; + __rwMark* mp = (__rwMark*)data; + memmove(&mark, mp, sizeof(__rwMark)); + RwMemNative32(&mark, sizeof(__rwMark)); + + if (mark.type == 0xBEEF01) + { + data = mp + 1; + colltree = xClumpColl_StaticBufferInit(data, mark.length); + + size -= mark.length + sizeof(__rwMark); + data = (U8*)data + mark.length; + + mp = (__rwMark*)data; + memmove(&mark, mp, sizeof(__rwMark)); + RwMemNative32(&mark, sizeof(__rwMark)); + + data = mp + 1; + + xJSPHeader* tmphdr = (xJSPHeader*)data; + strncpy(hdr->idtag, tmphdr->idtag, sizeof(hdr->idtag)); + hdr->version = tmphdr->version; + hdr->jspNodeCount = tmphdr->jspNodeCount; + hdr->colltree = colltree; + hdr->jspNodeList = (xJSPNodeInfo*)(tmphdr + 1); + + size -= mark.length + sizeof(__rwMark); + + for (i = 0; i < colltree->numTriangles; i++) + { + colltree->triangles[i].matIndex = + hdr->jspNodeList[hdr->jspNodeCount - 1 - colltree->triangles[i].v.i.atomIndex] + .originalMatIndex; + } + + if (size >= sizeof(__rwMark)) + { + mp = (__rwMark*)((U8*)data + mark.length); + memmove(&mark, mp, sizeof(__rwMark)); + RwMemNative32(&mark, sizeof(__rwMark)); + + data = mp + 1; + + if (mark.type == 0xBEEF03) + { + hdr->stripVecCount = *(U32*)data; + data = (U32*)data + 1; + hdr->stripVecList = (RwV3d*)data; + + sAtomicStartCount = RpClumpGetNumAtomics(hdr->clump); + sAtomicStartVert = (RwV3d**)RwMalloc(sAtomicStartCount * sizeof(RwV3d*)); + + RwV3d* currVec = hdr->stripVecList; + RpClumpForAllAtomics(hdr->clump, AddAtomicPrecalcedVertCB, &currVec); + } + } + + if (hdr->stripVecCount == 0) + { + hdr->stripVecCount = 0; + sAtomicStartCount = 0; + + RpClumpForAllAtomics(hdr->clump, CountAtomicCB, &hdr->stripVecCount); + + hdr->stripVecList = (RwV3d*)RwMalloc(hdr->stripVecCount * sizeof(RwV3d)); + sAtomicStartVert = (RwV3d**)RwMalloc(sAtomicStartCount * sizeof(RwV3d**)); + + RwV3d* currVec = hdr->stripVecList; + RpClumpForAllAtomics(hdr->clump, AddAtomicCB, &currVec); + } + + for (i = 0; i < colltree->numTriangles; i++) + { + colltree->triangles[i].v.p = sAtomicStartVert[colltree->triangles[i].v.i.atomIndex] + + colltree->triangles[i].v.i.meshVertIndex; + } + + RwFree(sAtomicStartVert); + } + else + { + RwMemory rwmem; + rwmem.start = (RwUInt8*)data; + rwmem.length = size; + + RwStream* stream = RwStreamOpen(rwSTREAMMEMORY, rwSTREAMREAD, &rwmem); + RwStreamFindChunk(stream, rwID_CLUMP, NULL, NULL); + clump = RpClumpStreamRead(stream); + RwStreamClose(stream, NULL); + + if (!hdr->clump) + { + hdr->clump = clump; + } + else + { + S32 i, atomCount; + + atomCount = RpClumpGetNumAtomics(clump); + if (atomCount) + { + RpAtomic** atomList = (RpAtomic**)RwMalloc(atomCount * sizeof(RpAtomic*)); + RpAtomic** atomCurr = atomList; + RpClumpForAllAtomics(clump, ListAtomicCB, &atomCurr); + + for (i = atomCount - 1; i >= 0; i--) + { + RpClumpRemoveAtomic(clump, atomList[i]); + RpClumpAddAtomic(hdr->clump, atomList[i]); + RpAtomicSetFrame(atomList[i], RpClumpGetFrame(hdr->clump)); + } + + RwFree(atomList); + } + + RpClumpDestroy(clump); + } + } +} + +void xJSP_Destroy(xJSPHeader* jsp) +{ + if (globals.sceneCur->env->geom->jsp == jsp && globals.sceneCur->env->geom->world) + { + RpWorldDestroy(globals.sceneCur->env->geom->world); + globals.sceneCur->env->geom->world = NULL; + } + + RpClumpDestroy(jsp->clump); + RwFree(jsp->colltree); + + U32* tp = (U32*)((xJSPHeaderGC*)jsp)->stripVecList; + U32 test = *(tp - sizeof(U32)); + RwMemNative32(&test, sizeof(U32)); + if (test != 0xBEEF03) + { + RwFree(((xJSPHeaderGC*)jsp)->stripVecList); + } + + RwFree(jsp); +} diff --git a/src/SB/Core/x/xJSP.h b/src/SB/Core/x/xJSP.h index fad7731dd..0161cf6e8 100644 --- a/src/SB/Core/x/xJSP.h +++ b/src/SB/Core/x/xJSP.h @@ -6,6 +6,13 @@ #include #include "xClumpColl.h" +struct __rwMark +{ + U32 type; + U32 length; + U32 libraryID; +}; + struct xJSPNodeInfo { S32 originalMatIndex; @@ -22,7 +29,12 @@ struct xJSPHeader xJSPNodeInfo* jspNodeList; }; -RpMesh* AddMeshCB(RpMesh* mesh, RpMeshHeader* header, RwV3d** param_3); +struct xJSPHeaderGC : xJSPHeader +{ + U32 stripVecCount; + RwV3d* stripVecList; +}; + void xJSP_MultiStreamRead(void* data, U32 size, xJSPHeader** jsp); void xJSP_Destroy(xJSPHeader* jsp); diff --git a/src/SB/Game/zAssetTypes.h b/src/SB/Game/zAssetTypes.h index c7fe6d565..72dbbe8d1 100644 --- a/src/SB/Game/zAssetTypes.h +++ b/src/SB/Game/zAssetTypes.h @@ -27,12 +27,6 @@ #include "zNPCTypeTest.h" #include "zNPCTypeTiki.h" -struct RwMemory -{ - U8* start; - U32 length; -}; - class HackModelRadius { public: