|
1 | 1 | #include "xJSP.h" |
| 2 | +#include "zGlobals.h" |
2 | 3 |
|
3 | 4 | #include <types.h> |
4 | 5 | #include <string.h> |
5 | 6 |
|
6 | | -#include "xClumpColl.h" |
| 7 | +static RwV3d* sCurrVert; |
| 8 | +static S32 sAtomicStartCount; |
| 9 | +static RwV3d** sAtomicStartVert; |
7 | 10 |
|
8 | | -extern S32 sAtomicStartCount; // not exactly sure of the type |
9 | | -extern RwV3d* sCurrVert; // not sure if correct type. not sure what this is. |
10 | | -extern RwV3d* sAtomicStartVert; // I'm just going based on matt's assumption |
11 | 11 |
|
12 | | -extern RwGlobals* RwEngineInstance; |
13 | | - |
14 | | -// No dwarf info |
15 | | -// ghidra said return type and type of param_2 was void |
16 | | -// but changing it to return atomic matches. |
17 | | -RpAtomic* CountAtomicCB(RpAtomic* atomic, U32* param_2) |
| 12 | +static RpAtomic* CountAtomicCB(RpAtomic* atomic, void* data) |
18 | 13 | { |
19 | 14 | sAtomicStartCount++; |
20 | | - *param_2 += atomic->geometry->mesh->totalIndicesInMesh; |
| 15 | + *(U32*)data += atomic->geometry->mesh->totalIndicesInMesh; |
21 | 16 | return atomic; |
22 | 17 | } |
23 | 18 |
|
24 | | -static RpMesh* AddMeshCB(RpMesh* mesh, RpMeshHeader* header, void* param_3) |
| 19 | +static RpMesh* AddMeshCB(RpMesh* mesh, RpMeshHeader*, void* pData) |
25 | 20 | { |
26 | | - for (int i = 0; i < mesh->numIndices; i++) |
27 | | - { |
28 | | - **(RwV3d**)(param_3) = sCurrVert[mesh->indices[i]]; |
29 | | - *((S32*)(param_3)) += sizeof(RwV3d); |
| 21 | + RwV3d** stripVert = (RwV3d**)pData; |
| 22 | + for (U32 i = 0; i < mesh->numIndices; i++) { |
| 23 | + **stripVert = sCurrVert[mesh->indices[i]]; |
| 24 | + (*stripVert)++; |
30 | 25 | } |
31 | | - |
32 | 26 | return mesh; |
33 | 27 | } |
34 | 28 |
|
35 | | -RpAtomic* AddAtomicCB(RpAtomic* atomic, void* data) |
| 29 | +static RpAtomic* AddAtomicCB(RpAtomic* atomic, void* data) |
36 | 30 | { |
37 | 31 | // The dwarf defines this variable but it looks like it isnt used |
38 | 32 | // TempAtomicList** tmpList; |
39 | | - sAtomicStartCount--; |
40 | | - sAtomicStartVert[sAtomicStartCount] = *(RwV3d*)data; |
| 33 | + sAtomicStartVert[--sAtomicStartCount] = *(RwV3d**)data; |
41 | 34 | sCurrVert = atomic->geometry->morphTarget->verts; |
42 | 35 | _rpMeshHeaderForAllMeshes(atomic->geometry->mesh, (RpMeshCallBack)&AddMeshCB, data); |
43 | 36 | return atomic; |
44 | 37 | } |
45 | 38 |
|
46 | | -RpAtomic* AddAtomicPrecalcedVertCB(RpAtomic* atomic, void* data) |
| 39 | +static RpAtomic* AddAtomicPrecalcedVertCB(RpAtomic* atomic, void* data) |
47 | 40 | { |
48 | | - sAtomicStartCount--; |
49 | | - sAtomicStartVert[sAtomicStartCount] = *(RwV3d*)data; |
50 | | - data = &data + atomic->geometry->mesh->totalIndicesInMesh * 0xc; |
51 | | - |
| 41 | + sAtomicStartVert[--sAtomicStartCount] = *(RwV3d**)data; |
| 42 | + *(RwV3d**)data += atomic->geometry->mesh->totalIndicesInMesh; |
52 | 43 | return atomic; |
53 | 44 | } |
54 | 45 |
|
55 | | -RpAtomic* ListAtomicCB(RpAtomic* atomic, void* data) |
| 46 | +static RpAtomic* ListAtomicCB(RpAtomic* atomic, void* data) |
56 | 47 | { |
57 | | - // RpAtomic*** aList; <- declared in dwarf data |
58 | | - // ¯\_(ツ)_/¯ idk what's going on |
59 | | - **(RpAtomic***)data = atomic; |
60 | | - *(S32*)data += 4; |
| 48 | + RpAtomic*** aList = (RpAtomic***)data; |
| 49 | + **aList = atomic; |
| 50 | + (*aList)++; |
61 | 51 | return atomic; |
62 | 52 | } |
| 53 | + |
| 54 | +void xJSP_MultiStreamRead(void* data, U32 size, xJSPHeader** jsp) |
| 55 | +{ |
| 56 | + U32 i; |
| 57 | + RpClump* clump; |
| 58 | + xClumpCollBSPTree* colltree; |
| 59 | + |
| 60 | + if (!*jsp) |
| 61 | + { |
| 62 | + *jsp = (xJSPHeaderGC*)RwMalloc(sizeof(xJSPHeaderGC)); |
| 63 | + memset(*jsp, 0, sizeof(xJSPHeaderGC)); |
| 64 | + } |
| 65 | + |
| 66 | + xJSPHeaderGC* hdr = (xJSPHeaderGC*)*jsp; |
| 67 | + |
| 68 | + __rwMark mark; |
| 69 | + __rwMark* mp = (__rwMark*)data; |
| 70 | + memmove(&mark, mp, sizeof(__rwMark)); |
| 71 | + RwMemNative32(&mark, sizeof(__rwMark)); |
| 72 | + |
| 73 | + if (mark.type == 0xBEEF01) |
| 74 | + { |
| 75 | + data = mp + 1; |
| 76 | + colltree = xClumpColl_StaticBufferInit(data, mark.length); |
| 77 | + |
| 78 | + size -= mark.length + sizeof(__rwMark); |
| 79 | + data = (U8*)data + mark.length; |
| 80 | + |
| 81 | + mp = (__rwMark*)data; |
| 82 | + memmove(&mark, mp, sizeof(__rwMark)); |
| 83 | + RwMemNative32(&mark, sizeof(__rwMark)); |
| 84 | + |
| 85 | + data = mp + 1; |
| 86 | + |
| 87 | + xJSPHeader* tmphdr = (xJSPHeader*)data; |
| 88 | + strncpy(hdr->idtag, tmphdr->idtag, sizeof(hdr->idtag)); |
| 89 | + hdr->version = tmphdr->version; |
| 90 | + hdr->jspNodeCount = tmphdr->jspNodeCount; |
| 91 | + hdr->colltree = colltree; |
| 92 | + hdr->jspNodeList = (xJSPNodeInfo*)(tmphdr + 1); |
| 93 | + |
| 94 | + size -= mark.length + sizeof(__rwMark); |
| 95 | + |
| 96 | + for (i = 0; i < colltree->numTriangles; i++) |
| 97 | + { |
| 98 | + colltree->triangles[i].matIndex = |
| 99 | + hdr->jspNodeList[hdr->jspNodeCount - 1 - colltree->triangles[i].v.i.atomIndex] |
| 100 | + .originalMatIndex; |
| 101 | + } |
| 102 | + |
| 103 | + if (size >= sizeof(__rwMark)) |
| 104 | + { |
| 105 | + mp = (__rwMark*)((U8*)data + mark.length); |
| 106 | + memmove(&mark, mp, sizeof(__rwMark)); |
| 107 | + RwMemNative32(&mark, sizeof(__rwMark)); |
| 108 | + |
| 109 | + data = mp + 1; |
| 110 | + |
| 111 | + if (mark.type == 0xBEEF03) |
| 112 | + { |
| 113 | + hdr->stripVecCount = *(U32*)data; |
| 114 | + data = (U32*)data + 1; |
| 115 | + hdr->stripVecList = (RwV3d*)data; |
| 116 | + |
| 117 | + sAtomicStartCount = RpClumpGetNumAtomics(hdr->clump); |
| 118 | + sAtomicStartVert = (RwV3d**)RwMalloc(sAtomicStartCount * sizeof(RwV3d*)); |
| 119 | + |
| 120 | + RwV3d* currVec = hdr->stripVecList; |
| 121 | + RpClumpForAllAtomics(hdr->clump, AddAtomicPrecalcedVertCB, &currVec); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if (hdr->stripVecCount == 0) |
| 126 | + { |
| 127 | + hdr->stripVecCount = 0; |
| 128 | + sAtomicStartCount = 0; |
| 129 | + |
| 130 | + RpClumpForAllAtomics(hdr->clump, CountAtomicCB, &hdr->stripVecCount); |
| 131 | + |
| 132 | + hdr->stripVecList = (RwV3d*)RwMalloc(hdr->stripVecCount * sizeof(RwV3d)); |
| 133 | + sAtomicStartVert = (RwV3d**)RwMalloc(sAtomicStartCount * sizeof(RwV3d**)); |
| 134 | + |
| 135 | + RwV3d* currVec = hdr->stripVecList; |
| 136 | + RpClumpForAllAtomics(hdr->clump, AddAtomicCB, &currVec); |
| 137 | + } |
| 138 | + |
| 139 | + for (i = 0; i < colltree->numTriangles; i++) |
| 140 | + { |
| 141 | + colltree->triangles[i].v.p = sAtomicStartVert[colltree->triangles[i].v.i.atomIndex] + |
| 142 | + colltree->triangles[i].v.i.meshVertIndex; |
| 143 | + } |
| 144 | + |
| 145 | + RwFree(sAtomicStartVert); |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + RwMemory rwmem; |
| 150 | + rwmem.start = (RwUInt8*)data; |
| 151 | + rwmem.length = size; |
| 152 | + |
| 153 | + RwStream* stream = RwStreamOpen(rwSTREAMMEMORY, rwSTREAMREAD, &rwmem); |
| 154 | + RwStreamFindChunk(stream, rwID_CLUMP, NULL, NULL); |
| 155 | + clump = RpClumpStreamRead(stream); |
| 156 | + RwStreamClose(stream, NULL); |
| 157 | + |
| 158 | + if (!hdr->clump) |
| 159 | + { |
| 160 | + hdr->clump = clump; |
| 161 | + } |
| 162 | + else |
| 163 | + { |
| 164 | + S32 i, atomCount; |
| 165 | + |
| 166 | + atomCount = RpClumpGetNumAtomics(clump); |
| 167 | + if (atomCount) |
| 168 | + { |
| 169 | + RpAtomic** atomList = (RpAtomic**)RwMalloc(atomCount * sizeof(RpAtomic*)); |
| 170 | + RpAtomic** atomCurr = atomList; |
| 171 | + RpClumpForAllAtomics(clump, ListAtomicCB, &atomCurr); |
| 172 | + |
| 173 | + for (i = atomCount - 1; i >= 0; i--) |
| 174 | + { |
| 175 | + RpClumpRemoveAtomic(clump, atomList[i]); |
| 176 | + RpClumpAddAtomic(hdr->clump, atomList[i]); |
| 177 | + RpAtomicSetFrame(atomList[i], RpClumpGetFrame(hdr->clump)); |
| 178 | + } |
| 179 | + |
| 180 | + RwFree(atomList); |
| 181 | + } |
| 182 | + |
| 183 | + RpClumpDestroy(clump); |
| 184 | + } |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +void xJSP_Destroy(xJSPHeader* jsp) |
| 189 | +{ |
| 190 | + if (globals.sceneCur->env->geom->jsp == jsp && globals.sceneCur->env->geom->world) |
| 191 | + { |
| 192 | + RpWorldDestroy(globals.sceneCur->env->geom->world); |
| 193 | + globals.sceneCur->env->geom->world = NULL; |
| 194 | + } |
| 195 | + |
| 196 | + RpClumpDestroy(jsp->clump); |
| 197 | + RwFree(jsp->colltree); |
| 198 | + |
| 199 | + U32* tp = (U32*)((xJSPHeaderGC*)jsp)->stripVecList; |
| 200 | + U32 test = *(tp - sizeof(U32)); |
| 201 | + RwMemNative32(&test, sizeof(U32)); |
| 202 | + if (test != 0xBEEF03) |
| 203 | + { |
| 204 | + RwFree(((xJSPHeaderGC*)jsp)->stripVecList); |
| 205 | + } |
| 206 | + |
| 207 | + RwFree(jsp); |
| 208 | +} |
0 commit comments