-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelInfo.cpp
341 lines (317 loc) · 8.37 KB
/
ModelInfo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "stdafx.h"
#include "ModelInfo.h"
#include <fstream>
#include <iostream>
using std::default_delete;
using std::ifstream;
using std::ios;
using std::istream;
using std::list;
using std::shared_ptr;
using std::streamoff;
using std::string;
#ifdef _MSC_VER
using std::wstring;
#endif /* _MSC_VER */
ModelInfo::ModelInfo(const char *filename)
{
ifstream str(filename, ios::binary);
init(str);
str.close();
}
ModelInfo::ModelInfo(const wchar_t *filename)
{
ifstream str(filename, ios::binary);
init(str);
str.close();
}
ModelInfo::ModelInfo(const string &filename)
{
ifstream str(filename, ios::binary);
init(str);
str.close();
}
ModelInfo::ModelInfo(const wstring &filename)
{
ifstream str(filename, ios::binary);
init(str);
str.close();
}
ModelInfo::ModelInfo(istream &stream) { init(stream); }
ModelFormat ModelInfo::getformat() { return format; }
NJS_OBJECT *ModelInfo::getmodel() { return model; }
const string &ModelInfo::getauthor() { return author; }
const string &ModelInfo::gettool() { return tool; }
const string &ModelInfo::getdescription() { return description; }
const uint8_t *ModelInfo::getmetadata(uint32_t identifier, uint32_t &size)
{
auto elem = metadata.find(identifier);
if (elem == metadata.end())
{
size = 0;
return nullptr;
}
else
{
size = elem->second.size;
return elem->second.data;
}
}
static const string empty;
const string &ModelInfo::getlabel(void *data)
{
auto elem = labels1.find(data);
if (elem == labels1.end())
return empty;
else
return elem->second;
}
void *ModelInfo::getdata(const string &label)
{
auto elem = labels2.find(label);
if (elem == labels2.end())
return nullptr;
else
return elem->second;
}
const std::unordered_map<std::string, void*>* ModelInfo::getlabels() const
{
return &labels2;
}
const list<string> &ModelInfo::getanimations() { return animations; }
const list<string> &ModelInfo::getmorphs() { return morphs; }
static string getstring(istream &stream)
{
auto start = stream.tellg();
while (stream.get() != 0)
;
auto size = stream.tellg() - start;
char *buf = new char[(unsigned int)size];
stream.seekg(start);
stream.read(buf, size);
string result(buf);
delete[] buf;
return result;
}
template<typename T>
static inline void fixptr(T *&ptr, intptr_t base)
{
if (ptr != nullptr)
ptr = (T *)((intptr_t)ptr + base);
}
void ModelInfo::fixbasicmodelpointers(NJS_MODEL *model, intptr_t base)
{
fixptr(model->points, base);
fixptr(model->normals, base);
if (model->meshsets != nullptr)
{
fixptr(model->meshsets, base);
if (fixedpointers.find(model->meshsets) == fixedpointers.end())
{
fixedpointers.insert(model->meshsets);
for (int i = 0; i < model->nbMeshset; i++)
{
fixptr(model->meshsets[i].meshes, base);
fixptr(model->meshsets[i].attrs, base);
fixptr(model->meshsets[i].normals, base);
fixptr(model->meshsets[i].vertcolor, base);
fixptr(model->meshsets[i].vertuv, base);
}
}
}
fixptr(model->mats, base);
}
void ModelInfo::fixchunkmodelpointers(NJS_CNK_MODEL *model, intptr_t base)
{
fixptr(model->vlist, base);
fixptr(model->plist, base);
}
void ModelInfo::fixsa2bmodelpointers(SA2B_Model* model, intptr_t base)
{
if (model->Vertices != nullptr)
{
fixptr(model->Vertices, base);
if (fixedpointers.find(model->Vertices) == fixedpointers.end())
{
fixedpointers.insert(model->Vertices);
for (SA2B_VertexData* vd = model->Vertices; vd->DataType != -1; ++vd)
fixptr(vd->Data, base);
}
}
if (model->OpaqueGeoData != nullptr)
{
fixptr(model->OpaqueGeoData, base);
if (fixedpointers.find(model->OpaqueGeoData) == fixedpointers.end())
{
fixedpointers.insert(model->OpaqueGeoData);
for (int i = 0; i < model->OpaqueGeometryCount; i++)
{
fixptr(model->OpaqueGeoData[i].PrimitiveOffset, base);
fixptr(model->OpaqueGeoData[i].ParameterOffset, base);
}
}
}
if (model->TranslucentGeoData != nullptr)
{
fixptr(model->TranslucentGeoData, base);
if (fixedpointers.find(model->TranslucentGeoData) == fixedpointers.end())
{
fixedpointers.insert(model->TranslucentGeoData);
for (int i = 0; i < model->TranslucentGeometryCount; i++)
{
fixptr(model->TranslucentGeoData[i].PrimitiveOffset, base);
fixptr(model->TranslucentGeoData[i].ParameterOffset, base);
}
}
}
}
void ModelInfo::fixobjectpointers(NJS_OBJECT *object, intptr_t base)
{
if (object->model != nullptr)
{
fixptr(object->model, base);
if (fixedpointers.find(object->model) == fixedpointers.end())
{
fixedpointers.insert(object->model);
if (format == ModelFormat_Basic)
fixbasicmodelpointers(object->basicmodel, base);
else if (format == ModelFormat_Chunk)
fixchunkmodelpointers(object->chunkmodel, base);
else if (format == ModelFormat_SA2B)
fixsa2bmodelpointers(object->sa2bmodel, base);
}
}
if (object->child != nullptr)
{
fixptr(object->child, base);
if (fixedpointers.find(object->child) == fixedpointers.end())
{
fixedpointers.insert(object->child);
fixobjectpointers(object->child, base);
}
}
if (object->sibling != nullptr)
{
fixptr(object->sibling, base);
if (fixedpointers.find(object->sibling) == fixedpointers.end())
{
fixedpointers.insert(object->sibling);
fixobjectpointers(object->sibling, base);
}
}
}
template<typename T>
static inline void readdata(istream &stream, T &data)
{
stream.read((char *)&data, sizeof(T));
}
void ModelInfo::init(istream &stream)
{
uint64_t magic;
readdata(stream, magic);
uint8_t version = magic >> 56;
magic &= FormatMask;
if (version != CurrentVersion) // unrecognized file version
return;
switch (magic)
{
case SA1MDL:
format = ModelFormat_Basic;
break;
case SA2MDL:
format = ModelFormat_Chunk;
break;
case SA2BMDL:
format = ModelFormat_SA2B;
break;
default:
return;
}
uint32_t modeloff;
readdata(stream, modeloff);
modeloff -= headersize;
uint32_t tmpaddr;
readdata(stream, tmpaddr);
int mdlsize = tmpaddr - headersize;
uint8_t *modelbuf = new uint8_t[mdlsize];
allocatedmem.push_back(shared_ptr<uint8_t>(modelbuf, default_delete<uint8_t[]>()));
stream.read((char *)modelbuf, mdlsize);
model = (NJS_OBJECT *)(modelbuf + modeloff);
intptr_t modelbase = (intptr_t)modelbuf - headersize;
fixobjectpointers(model, modelbase);
fixedpointers.clear();
uint32_t chunktype;
readdata(stream, chunktype);
while (chunktype != ChunkTypes_End)
{
uint32_t chunksz;
readdata(stream, chunksz);
auto chunkbase = stream.tellg();
auto nextchunk = chunkbase + (streamoff)chunksz;
switch (chunktype)
{
case ChunkTypes_Label:
while (true)
{
void *dataptr;
readdata(stream, dataptr);
uint32_t labelptr;
readdata(stream, labelptr);
if (dataptr == (void *)-1 && labelptr == UINT32_MAX)
break;
dataptr = (uint8_t *)dataptr + modelbase;
tmpaddr = (uint32_t)stream.tellg();
stream.seekg((uint32_t)chunkbase + labelptr);
string label = getstring(stream);
stream.seekg(tmpaddr);
labels1[dataptr] = label;
labels2[label] = dataptr;
}
break;
case ChunkTypes_Animation:
while (true)
{
uint32_t labelptr;
readdata(stream, labelptr);
if (labelptr == UINT32_MAX)
break;
tmpaddr = (uint32_t)stream.tellg();
stream.seekg((uint32_t)chunkbase + labelptr);
animations.push_back(getstring(stream));
stream.seekg(tmpaddr);
}
break;
case ChunkTypes_Morph:
while (true)
{
uint32_t labelptr;
readdata(stream, labelptr);
if (labelptr == UINT32_MAX)
break;
tmpaddr = (uint32_t)stream.tellg();
stream.seekg((uint32_t)chunkbase + labelptr);
morphs.push_back(getstring(stream));
stream.seekg(tmpaddr);
}
break;
case ChunkTypes_Author:
author = getstring(stream);
break;
case ChunkTypes_Tool:
tool = getstring(stream);
break;
case ChunkTypes_Description:
description = getstring(stream);
break;
default:
uint8_t *buf = new uint8_t[chunksz];
allocatedmem.push_back(shared_ptr<uint8_t>(buf, default_delete<uint8_t[]>()));
stream.read((char *)buf, chunksz);
Metadata meta = { chunksz, buf };
metadata[chunktype] = meta;
break;
}
stream.seekg(nextchunk);
readdata(stream, chunktype);
}
}