-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimationFile.cpp
284 lines (251 loc) · 6.12 KB
/
AnimationFile.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
#include "pch.h"
#include "AnimationFile.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 */
AnimationFile::AnimationFile(const char* filename)
{
ifstream str(filename, ios::binary);
init(str);
str.close();
}
#ifdef _MSC_VER
AnimationFile::AnimationFile(const wchar_t* filename)
{
ifstream str(filename, ios::binary);
init(str);
str.close();
}
#endif /* _MSC_VER */
AnimationFile::AnimationFile(const string& filename)
{
ifstream str(filename, ios::binary);
init(str);
str.close();
}
#ifdef _MSC_VER
AnimationFile::AnimationFile(const wstring& filename)
{
ifstream str(filename, ios::binary);
init(str);
str.close();
}
#endif /* _MSC_VER */
AnimationFile::AnimationFile(istream& stream) { init(stream); }
NJS_MOTION* AnimationFile::getmotion() const { return motion; }
int AnimationFile::getmodelcount() const { return modelcount; }
bool AnimationFile::isshortrot() const { return shortrot; }
const string& AnimationFile::getlabel()
{
return getlabel(motion);
}
static const string empty;
const string& AnimationFile::getlabel(void* data)
{
auto elem = labels1.find(data);
if (elem == labels1.end())
return empty;
else
return elem->second;
}
void* AnimationFile::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*>* AnimationFile::getlabels() const
{
return &labels2;
}
static string getstring(istream& stream)
{
auto start = stream.tellg();
while (stream.get() != 0) {}
const auto size = static_cast<size_t>(stream.tellg() - start);
char* buf = new char[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*)((uint8_t*)ptr + base);
}
}
inline void fixmkeypointers(NJS_MKEY_P* mkey, intptr_t base, int count)
{
for (int i = 0; i < count; i++)
fixptr(mkey[i].key, base);
}
template <typename T>
inline void fixmdatapointers(T* mdata, intptr_t base, int count, int vertoff, int normoff)
{
for (int c = 0; c < count; c++)
{
for (int i = 0; i < (int)LengthOfArray(mdata->p); i++)
{
fixptr(mdata->p[i], base);
if (i == vertoff || i == normoff)
fixmkeypointers((NJS_MKEY_P*)mdata->p[i], base, mdata->nb[i]);
}
++mdata;
}
}
template <typename T>
static inline void readdata(istream& stream, T& data)
{
stream.read((char*)&data, sizeof(T));
}
void AnimationFile::init(istream& stream)
{
uint64_t magic;
readdata(stream, magic);
uint8_t version = magic >> 56;
magic &= FormatMask;
if (version > CurrentVersion) // unrecognized file version
{
return;
}
if (magic != SAANIM)
{
return;
}
uint32_t landtableoff;
readdata(stream, landtableoff);
landtableoff -= headersize;
uint32_t tmpaddr;
readdata(stream, tmpaddr);
readdata(stream, modelcount);
shortrot = modelcount < 0;
modelcount &= INT32_MAX;
size_t mdlsize = tmpaddr - headersize;
auto motionbuf = new uint8_t[mdlsize];
allocatedmem.push_back(shared_ptr<uint8_t>(motionbuf, default_delete<uint8_t[]>()));
stream.read((char*)motionbuf, mdlsize);
motion = (NJS_MOTION*)(motionbuf + landtableoff);
intptr_t motionbase = (intptr_t)motionbuf - headersize;
if (motion->mdata != nullptr)
{
motion->mdata = (uint8_t*)motion->mdata + motionbase;
if (fixedpointers.find(motion->mdata) == fixedpointers.end())
{
fixedpointers.insert(motion->mdata);
int vertoff = -1;
int normoff = -1;
if (motion->type & NJD_MTYPE_VERT_4)
{
vertoff = 0;
if (motion->type & NJD_MTYPE_POS_0)
++vertoff;
if (motion->type & NJD_MTYPE_ANG_1)
++vertoff;
if (motion->type & NJD_MTYPE_QUAT_1)
++vertoff;
if (motion->type & NJD_MTYPE_SCL_2)
++vertoff;
if (motion->type & NJD_MTYPE_TARGET_3)
++vertoff;
if (motion->type & NJD_MTYPE_VEC_3)
++vertoff;
}
if (motion->type & NJD_MTYPE_NORM_5)
{
normoff = 0;
if (motion->type & NJD_MTYPE_POS_0)
++normoff;
if (motion->type & NJD_MTYPE_ANG_1)
++normoff;
if (motion->type & NJD_MTYPE_QUAT_1)
++normoff;
if (motion->type & NJD_MTYPE_SCL_2)
++normoff;
if (motion->type & NJD_MTYPE_TARGET_3)
++normoff;
if (motion->type & NJD_MTYPE_VEC_3)
++normoff;
if (motion->type & NJD_MTYPE_VERT_4)
++normoff;
}
switch (motion->inp_fn & 0xF)
{
case 1:
fixmdatapointers((NJS_MDATA1*)motion->mdata, motionbase, modelcount, vertoff, normoff);
break;
case 2:
fixmdatapointers((NJS_MDATA2*)motion->mdata, motionbase, modelcount, vertoff, normoff);
break;
case 3:
fixmdatapointers((NJS_MDATA3*)motion->mdata, motionbase, modelcount, vertoff, normoff);
break;
case 4:
fixmdatapointers((NJS_MDATA4*)motion->mdata, motionbase, modelcount, vertoff, normoff);
break;
case 5:
fixmdatapointers((NJS_MDATA5*)motion->mdata, motionbase, modelcount, vertoff, normoff);
break;
default:
throw;
}
}
}
fixedpointers.clear();
if (version < 2)
{
string label = getstring(stream);
labels1[motion] = label;
labels2[label] = motion;
}
else
{
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 + motionbase;
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;
}
stream.seekg(nextchunk);
readdata(stream, chunktype);
}
}
}