-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdump-bin.cpp
424 lines (361 loc) · 13 KB
/
dump-bin.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include <stdio.h>
#include "stdio__iob_func.h"
#include <Common/Base/keycode.cxx>
#include <Common/Base/Config/hkProductFeatures.cxx>
#include <Common/Base/Memory/System/hkMemorySystem.h>
#include <Common/Base/Memory/System/Util/hkMemoryInitUtil.h>
#include <Common/Base/Memory/Allocator/Malloc/hkMallocAllocator.h>
#include <Common/Base/System/Io/IStream/hkIStream.h>
#include <Common/Base/System/Io/Reader/hkStreamReader.h>
#include <Common/Base/System/Io/OStream/hkOStream.h>
#include <Common/Base/System/Io/Writer/hkStreamWriter.h>
//#include <Common/Base/Reflection/Registry/hkDynamicClassNameRegistry.h>
#include <Common/Base/Reflection/Registry/hkDefaultClassNameRegistry.h>
// Serialize Loader
#include <Common/Serialize/Util/hkSerializeUtil.h>
#include <Common/Serialize/Util/hkRootLevelContainer.h>
#include <Common/Serialize/Util/hkNativePackfileUtils.h>
#include <Common/Serialize/Util/hkLoader.h>
#include <Common/Serialize/Version/hkVersionPatchManager.h>
#include <Common/Compat/Deprecated/Packfile/Binary/hkBinaryPackfileReader.h>
#include <Common/Compat/Deprecated/Packfile/Xml/hkXmlPackfileReader.h>
// Animation
#include <Animation/Animation/hkaAnimationContainer.h>
#include <Animation/Animation/Rig/hkaPose.h>
#include <Animation/Animation/Rig/hkaSkeleton.h>
#include <Animation/Animation/Rig/hkaSkeletonUtils.h>
#pragma comment (lib, "hkBase.lib")
#pragma comment (lib, "hkSerialize.lib")
#pragma comment (lib, "hkSceneData.lib")
#pragma comment (lib, "hkInternal.lib")
#pragma comment (lib, "hkGeometryUtilities.lib")
//o1: #pragma comment (lib, "hkVisualize.lib")
#pragma comment (lib, "hkCompat.lib")
#pragma comment (lib, "hkpCollide.lib")
#pragma comment (lib, "hkpConstraintSolver.lib")
#pragma comment (lib, "hkpDynamics.lib")
#pragma comment (lib, "hkpInternal.lib")
#pragma comment (lib, "hkpUtilities.lib")
#pragma comment (lib, "hkpVehicle.lib")
#pragma comment (lib, "hkaAnimation.lib")
#pragma comment (lib, "hkaRagdoll.lib")
#pragma comment (lib, "hkaInternal.lib")
//o1: #pragma comment (lib, "hkgBridge.lib")
void HK_CALL errorReport(const char* msg, void* userContext)
{
fputs(msg, stderr); fputs("\n", stderr);
}
hkResource* hkSerializeUtilLoad( hkStreamReader* stream
, hkSerializeUtil::ErrorDetails* detailsOut/*=HK_NULL*/
, const hkClassNameRegistry* classReg/*=HK_NULL*/
, hkSerializeUtil::LoadOptions options/*=hkSerializeUtil::LOAD_DEFAULT*/ )
{
__try
{
return hkSerializeUtil::load(stream, detailsOut, classReg, options);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
if (detailsOut == NULL)
detailsOut->id = hkSerializeUtil::ErrorDetails::ERRORID_LOAD_FAILED;
return NULL;
}
}
hkResult hkSerializeLoad(hkStreamReader *reader
, hkVariant &root
, hkResource *&resource)
{
hkTypeInfoRegistry &defaultTypeRegistry = hkTypeInfoRegistry::getInstance();
hkDefaultClassNameRegistry &defaultRegistry = hkDefaultClassNameRegistry::getInstance();
hkBinaryPackfileReader bpkreader;
hkXmlPackfileReader xpkreader;
resource = NULL;
hkSerializeUtil::FormatDetails formatDetails;
hkSerializeUtil::detectFormat( reader, formatDetails );
hkBool32 isLoadable = hkSerializeUtil::isLoadable( reader );
if (!isLoadable && formatDetails.m_formatType != hkSerializeUtil::FORMAT_TAGFILE_XML)
{
return HK_FAILURE;
}
else
{
switch ( formatDetails.m_formatType )
{
case hkSerializeUtil::FORMAT_PACKFILE_BINARY:
{
bpkreader.loadEntireFile(reader);
bpkreader.finishLoadedObjects(defaultTypeRegistry);
if ( hkPackfileData* pkdata = bpkreader.getPackfileData() )
{
hkArray<hkVariant>& obj = bpkreader.getLoadedObjects();
for ( int i =0,n=obj.getSize(); i<n; ++i)
{
hkVariant& value = obj[i];
if ( value.m_class->hasVtable() )
defaultTypeRegistry.finishLoadedObject(value.m_object, value.m_class->getName());
}
resource = pkdata;
resource->addReference();
}
root = bpkreader.getTopLevelObject();
}
break;
case hkSerializeUtil::FORMAT_PACKFILE_XML:
{
xpkreader.loadEntireFileWithRegistry(reader, &defaultRegistry);
if ( hkPackfileData* pkdata = xpkreader.getPackfileData() )
{
hkArray<hkVariant>& obj = xpkreader.getLoadedObjects();
for ( int i =0,n=obj.getSize(); i<n; ++i)
{
hkVariant& value = obj[i];
if ( value.m_class->hasVtable() )
defaultTypeRegistry.finishLoadedObject(value.m_object, value.m_class->getName());
}
resource = pkdata;
resource->addReference();
root = xpkreader.getTopLevelObject();
}
}
break;
case hkSerializeUtil::FORMAT_TAGFILE_BINARY:
case hkSerializeUtil::FORMAT_TAGFILE_XML:
default:
{
hkSerializeUtil::ErrorDetails detailsOut;
hkSerializeUtil::LoadOptions loadflags = hkSerializeUtil::LOAD_FAIL_IF_VERSIONING;
resource = hkSerializeUtilLoad(reader, &detailsOut, &defaultRegistry, loadflags);
root.m_object = resource->getContents<hkRootLevelContainer>();
if (root.m_object != NULL)
root.m_class = &((hkRootLevelContainer*)root.m_object)->staticClass();
}
break;
}
}
return root.m_object != NULL ? HK_SUCCESS : HK_FAILURE;
}
hkResource *hkSerializeLoadResource(hkStreamReader *reader)
{
hkResource *resource = NULL;
hkVariant root;
hkSerializeLoad(reader, root, resource);
return resource;
}
void write(hkStreamWriter *writer, hkQsTransform& transform)
{
const hkVector4& translation = transform.getTranslation();
const hkQuaternion& rotation = transform.getRotation();
const hkVector4& scale = transform.getScale();
writer->write(&translation, sizeof(hkVector4));
writer->write(&rotation.m_vec, sizeof(hkVector4));
writer->write(&scale, sizeof(hkVector4));
}
void write(hkStreamWriter *writer, hkaSkeleton *skeleton)
{
/// A user name to aid in identifying the skeleton
hkStringPtr name = skeleton->m_name;
writer->write(name.cString(), name.getLength()+1);
/// Parent relationship
int nparentIndices = skeleton->m_parentIndices.getSize();
writer->write(&nparentIndices, sizeof(int));
for (int i=0; i<nparentIndices; ++i)
{
writer->write(&skeleton->m_parentIndices[i], sizeof(hkInt16));
}
/// Bones for this skeleton
int nbones = skeleton->m_bones.getSize();
writer->write(&nbones, sizeof(int));
for (int i=0; i<nbones; ++i)
{
hkStringPtr name = skeleton->m_bones[i].m_name;
writer->write(name.cString(), name.getLength()+1);
}
/// The reference pose for the bones of this skeleton. This pose is stored in local space.
int nreferencePose = skeleton->m_referencePose.getSize();
writer->write(&nreferencePose, sizeof(int));
for (int i=0; i<nreferencePose; ++i)
{
write(writer, skeleton->m_referencePose[i]);
}
/// The reference values for the float slots of this skeleton. This pose is stored in local space.
int nreferenceFloats = skeleton->m_referenceFloats.getSize();
writer->write(&nreferenceFloats, sizeof(int));
for (int i=0; i<nreferenceFloats; ++i)
{
writer->write(&skeleton->m_referenceFloats[i], sizeof(hkReal));
}
/// Floating point track slots. Often used for auxiliary float data or morph target parameters etc.
/// This defines the target when binding animations to a particular rig.
int nfloatSlots = skeleton->m_floatSlots.getSize();
writer->write(&nfloatSlots, sizeof(int));
for (int i=0; i<nfloatSlots; ++i)
{
hkStringPtr name = skeleton->m_floatSlots[i];
writer->write(name.cString(), name.getLength()+1);
}
}
void write(hkStreamWriter *writer, hkaAnimation *anim)
{
/// Returns the number of original samples / frames of animation.
int numOriginalFrames = anim->getNumOriginalFrames();
/// The length of the animation cycle in seconds
hkReal duration = anim->m_duration;
/// The number of bone tracks to be animated.
int numTransforms = anim->m_numberOfTransformTracks;
/// The number of float tracks to be animated
int numFloats = anim->m_numberOfFloatTracks;
writer->write(&numOriginalFrames, sizeof(int));
writer->write(&duration, sizeof(hkReal));
writer->write(&numTransforms, sizeof(int));
writer->write(&numFloats, sizeof(int));
hkLocalArray<hkQsTransform> transformOut(numTransforms);
hkLocalArray<hkReal> floatsOut(numFloats);
transformOut.setSize(numTransforms);
floatsOut.setSize(numFloats);
hkReal time = 0.0f;
for (int f=0; f<numOriginalFrames; ++f, time = (hkReal)f * duration / (hkReal)(numOriginalFrames-1))
{
/// Get a subset of the first 'maxNumTracks' transform tracks (all tracks from 0 to maxNumTracks-1 inclusive), and the first 'maxNumFloatTracks' float tracks of a pose at a given time.
anim->samplePartialTracks(time, numTransforms, transformOut.begin(), numFloats, floatsOut.begin(), HK_NULL);
hkaSkeletonUtils::normalizeRotations(transformOut.begin(), numTransforms);
writer->write(&time, sizeof(hkReal));
for (int i=0; i<numTransforms; ++i)
{
write(writer, transformOut[i]);
}
for (int i=0; i<numFloats; ++i)
{
writer->write(&floatsOut[i], sizeof(hkReal));
}
}
/// The annotation tracks associated with this skeletal animation.
const int numAnnotationTracks = anim->m_annotationTracks.getSize();
writer->write(&numAnnotationTracks, sizeof(int));
for (hkArray< class hkaAnnotationTrack >::const_iterator annotationTrack = anim->m_annotationTracks.begin(); annotationTrack != anim->m_annotationTracks.end(); ++annotationTrack)
{
const int numAnnotations = annotationTrack->m_annotations.getSize();
writer->write(&numAnnotations, sizeof(int));
for (hkArray< class hkaAnnotationTrack::Annotation >::const_iterator annotation = annotationTrack->m_annotations.begin(); annotation != annotationTrack->m_annotations.end(); ++annotation)
{
writer->write(&annotation->m_time, sizeof(hkReal));
hkStringPtr text = annotation->m_text;
writer->write(text.cString(), text.getLength()+1);
}
}
}
void write(hkStreamWriter *writer, hkaAnimationBinding *binding)
{
/// (Optional) The name of the skeleton from which the binding was constructed.
hkStringPtr name = binding->m_originalSkeletonName;
writer->write(name.cString(), name.getLength()+1);
/// A handle to the animation bound to the skeleton.
hkaAnimation *anim = binding->m_animation;
/// A mapping from animation track indices to bone indices.
int ntransformToBoneIndices = binding->m_transformTrackToBoneIndices.getSize();
writer->write(&ntransformToBoneIndices, sizeof(int));
for (int i=0; i<ntransformToBoneIndices; i++)
{
writer->write(&binding->m_transformTrackToBoneIndices[i], sizeof(hkInt16));
}
/// A mapping from animation float track indices to float slot indices.
int nfloatTrackToFloatSlotIndices = binding->m_floatTrackToFloatSlotIndices.getSize();
writer->write(&nfloatTrackToFloatSlotIndices, sizeof(int));
for (int i=0; i<nfloatTrackToFloatSlotIndices; i++)
{
writer->write(&binding->m_floatTrackToFloatSlotIndices[i], sizeof(hkInt16));
}
}
void dump(const char* filename, const char* destname)
{
hkOstream deststream(destname);
hkStreamWriter *writer = deststream.getStreamWriter();
hkStringPtr head = "hkdump File Format, Version 1.0.2.0\n";
writer->write(head.cString(), head.getLength());
const unsigned int version = 0x01000200;
writer->write(&version, sizeof(unsigned int));
hkIstream stream(filename);
hkStreamReader *reader = stream.getStreamReader();
hkResource *resource = hkSerializeLoadResource(reader);
if (resource == NULL)
{
fprintf(stderr, "File is not loadable\n");
}
else
{
if (hkRootLevelContainer* rootCont = resource->getContents<hkRootLevelContainer>())
{
if (hkaAnimationContainer *animCont = rootCont->findObject<hkaAnimationContainer>())
{
int nskeletons = animCont->m_skeletons.getSize();
writer->write(&nskeletons, sizeof(int));
if (nskeletons != 0)
{
hkaSkeleton *skeleton = animCont->m_skeletons[0];
write(writer, skeleton);
}
int nanimations = animCont->m_animations.getSize();
writer->write(&nanimations, sizeof(int));
if (nanimations != 0)
{
hkaAnimation *animation = animCont->m_animations[0];
write(writer, animation);
}
/*
int nbindings = animCont->m_bindings.getSize();
writer->write(&nbindings, sizeof(int));
if (nbindings != 0)
{
hkaAnimationBinding *binding = animCont->m_bindings[0];
write(writer, binding);
}
*/
}
}
resource->removeReference();
}
}
int main( int argc, char *argv[], char *envp[] )
{
char *ofile = NULL;
char *ifile = NULL;
for (int i=1; i<argc; ++i)
{
if (argv[i][0] == '-')
{
switch (argv[i][1])
{
case '?':
{
break;
}
case 'o':
{
if (++i<argc)
{
ofile = argv[i];
}
break;
}
}
}
else
{
ifile = argv[i];
}
}
if (ifile == NULL)
{
puts("Usage: hkdump-bin.exe -o out.bin src.hkx");
return 1;
}
if (ofile == NULL)
ofile = "out.bin";
// Need to have memory allocated for the solver. Allocate 1mb for it.
hkMemoryRouter *memoryRouter = hkMemoryInitUtil::initDefault( hkMallocAllocator::m_defaultMallocAllocator, hkMemorySystem::FrameInfo(1024 * 1024) );
hkBaseSystem::init( memoryRouter, errorReport );
dump(ifile, ofile);
// Shutdown
hkBaseSystem::quit();
hkMemoryInitUtil::quit();
return 0;
}