-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathDtsShape.py
624 lines (520 loc) · 16.8 KB
/
DtsShape.py
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
from struct import pack, unpack, calcsize
from array import array
from ctypes import c_byte, c_short, c_int
from .DtsTypes import *
# Shortcut for reading & writing struct data from & to a file descriptor
def ws(fd, spec, *values):
fd.write(pack(spec, *values))
def read_multi(fd, count, spec):
spec = str(count) + spec
return unpack(spec, fd.read(calcsize(spec)))
class DtsOutputStream(object):
def __init__(self, dtsVersion=24, exporterVersion=0):
self.dtsVersion = dtsVersion
self.exporterVersion = exporterVersion
self.sequence32 = c_int(0)
self.sequence16 = c_short(0)
self.sequence8 = c_byte(0)
self.buffer32 = []
self.buffer16 = []
self.buffer8 = []
def guard(self, specific=None):
if specific != None:
assert c_int(specific).value == self.sequence32.value
self.write32(self.sequence32.value)
self.write16(self.sequence16.value)
self.write8(self.sequence8.value)
self.sequence32.value += 1
self.sequence16.value += 1
self.sequence8.value += 1
def flush(self, fd):
# Force all buffers to have a size multiple of 4 bytes
if len(self.buffer16) % 2 == 1: self.buffer16.append(0)
while len(self.buffer8) % 4 != 0: self.buffer8.append(0)
end32 = len(self.buffer32)
end16 = end32 + len(self.buffer16) // 2
end8 = end16 + len(self.buffer8 ) // 4
fd.write(pack("hhiii",
self.dtsVersion, self.exporterVersion,
end8, end32, end16))
fd.write(pack(str(len(self.buffer32)) + "i", *self.buffer32))
fd.write(pack(str(len(self.buffer16)) + "h", *self.buffer16))
fd.write(pack(str(len(self.buffer8 )) + "b", *self.buffer8 ))
def write32(self, *values):
for value in values:
assert -2147483648 <= value <= 2147483647, "value {} out of range".format(value)
assert type(value) == int, "type is {}, must be {}".format(type(value), int)
self.buffer32.extend(values)
def write16(self, *values):
#for value in values:
# assert -32768 <= value <= 32767, "value {} out of range".format(value)
# assert type(value) == int, "type is {}, must be {}".format(type(value), int)
#self.buffer16.extend(values)
self.buffer16.extend(map(lambda v: c_short(int(v)).value, values))
def write8(self, *values):
for value in values:
assert -128 <= value <= 127, "value {} out of range".format(value)
assert type(value) == int, "type is {}, must be {}".format(type(value), int)
self.buffer8.extend(values)
def write_u8(self, num):
assert 0 <= num <= 255, num
self.write8(unpack("b", pack("B", num))[0])
def write_float(self, *values):
self.write32(*map(lambda f: unpack("i", pack("f", f))[0], values))
def write_string(self, string):
self.write8(*string.encode("cp1252"))
self.write8(0)
def write_vec3(self, v):
self.write_float(v.x, v.y, v.z)
def write_vec2(self, v):
self.write_float(v.x, v.y)
def write_box(self, box):
self.write_vec3(box.min)
self.write_vec3(box.max)
def write_quat(self, quat):
self.write16(
c_short(int(quat.x * 32767)).value,
c_short(int(quat.y * 32767)).value,
c_short(int(quat.z * 32767)).value,
c_short(int(quat.w * -32767)).value)
class DtsInputStream(object):
def __init__(self, fd):
self.sequence32 = c_int(0)
self.sequence16 = c_short(0)
self.sequence8 = c_byte(0)
self.dtsVersion, self.exporterVersion = unpack("hh", fd.read(4))
end8, end32, end16 = unpack("iii", fd.read(12))
num32 = end32
num16 = (end16 - end32) * 2
num8 = (end8 - end16) * 4
self.buffer32 = array("i", read_multi(fd, num32, "i"))
self.buffer16 = array("h", read_multi(fd, num16, "h"))
self.buffer8 = array("b", read_multi(fd, num8 , "b"))
self.tell32 = 0
self.tell16 = 0
self.tell8 = 0
def guard(self, specific=None):
if specific != None:
assert c_int(specific).value == self.sequence32.value
assert self.sequence32.value == self.read32()
assert self.sequence16.value == self.read16()
assert self.sequence8.value == self.read8()
self.sequence32.value += 1
self.sequence16.value += 1
self.sequence8.value += 1
def read32(self):
if self.tell32 >= len(self.buffer32):
raise EOFError()
data = self.buffer32[self.tell32]
self.tell32 += 1
return data
def read16(self):
if self.tell16 >= len(self.buffer16):
raise EOFError()
data = self.buffer16[self.tell16]
self.tell16 += 1
return data
def read8(self):
if self.tell8 >= len(self.buffer8):
raise EOFError()
data = self.buffer8[self.tell8]
self.tell8 += 1
return data
def read_float(self):
return unpack("f", pack("i", self.read32()))[0]
def read_string(self):
buf = bytearray()
while True:
byte = self.read8()
if byte == 0:
break
else:
buf.append(byte)
return buf.decode("cp1252")
def read_vec3(self):
return Vector((self.read_float(), self.read_float(), self.read_float()))
def read_vec2(self):
return Vector((self.read_float(), self.read_float()))
def read_box(self):
return Box(self.read_vec3(), self.read_vec3())
def read_quat(self):
x = self.read16() / 32767
y = self.read16() / 32767
z = self.read16() / 32767
w = self.read16() / -32767
return Quaternion((w, x, y, z))
class DtsShape(object):
def __init__(self):
self.nodes = []
self.objects = []
self.decals = []
self.subshapes = []
self.iflmaterials = []
self.materials = []
self.default_rotations = []
self.default_translations = []
self.node_rotations = []
self.node_translations = []
self.node_uniform_scales = []
self.node_aligned_scales = []
self.node_arbitrary_scale_factors = []
self.node_arbitrary_scale_rots = []
self.ground_translations = []
self.ground_rotations = []
self.objectstates = []
self.decalstates = []
self.triggers = []
self.detail_levels = []
self.meshes = []
self.sequences = []
self.names = []
self._names_lookup = {}
self.smallest_size = 0.0
self.smallest_detail_level = 0
self.radius = 0.0
self.radius_tube = 0.0
self.center = Vector()
self.bounds = Box(Vector(), Vector())
def name(self, string):
index = self._names_lookup.get(string.lower())
if index == None:
index = len(self.names)
self.names.append(string)
self._names_lookup[string.lower()] = index
return index
def name_resolve(self, string):
index = self.name(string)
return (index, self.names[index])
def get_world_mat(self, nodeid):
matrix = Matrix()
while nodeid != -1:
cur = Matrix.Translation(self.default_translations[nodeid]) * self.default_rotations[nodeid].to_matrix()
matrix = cur * matrix
nodeid = self.nodes[nodeid].parent
return mat
def verify(self):
assert self.detail_levels
assert self.subshapes
assert len(self.nodes) == len(self.default_translations)
assert len(self.nodes) == len(self.default_rotations)
assert len(self.objects) == len(self.objectstates)
assert len(self.node_arbitrary_scale_factors) == len(self.node_arbitrary_scale_rots)
assert len(self.ground_translations) == len(self.ground_rotations)
def save(self, fd, dtsVersion=24):
stream = DtsOutputStream(dtsVersion)
# Header
stream.write32(
len(self.nodes),
len(self.objects),
len(self.decals),
len(self.subshapes),
len(self.iflmaterials),
len(self.node_rotations),
len(self.node_translations),
len(self.node_uniform_scales),
len(self.node_aligned_scales),
len(self.node_arbitrary_scale_factors),
len(self.ground_translations),
len(self.objectstates),
len(self.decalstates),
len(self.triggers),
len(self.detail_levels),
len(self.meshes),
len(self.names),
)
stream.write_float(self.smallest_size)
stream.write32(self.smallest_detail_level)
if dtsVersion > 24:
# write morphs
pass
stream.guard(0)
# Bounds
stream.write_float(self.radius, self.radius_tube)
stream.write_vec3(self.center)
stream.write_box(self.bounds)
stream.guard(1)
# Nodes
for node in self.nodes:
node.write(stream)
stream.guard(2)
# Objects
for obj in self.objects:
obj.write(stream)
stream.guard(3)
# Decals
for decal in self.decals:
decal.write(stream)
stream.guard(4)
# IFL materials
for ifl in self.iflmaterials:
ifl.write(stream)
stream.guard(5)
# Subshapes
for sub in self.subshapes:
stream.write32(sub.firstNode)
for sub in self.subshapes:
stream.write32(sub.firstObject)
for sub in self.subshapes:
stream.write32(sub.firstDecal)
stream.guard(6)
for sub in self.subshapes:
stream.write32(sub.numNodes)
for sub in self.subshapes:
stream.write32(sub.numObjects)
for sub in self.subshapes:
stream.write32(sub.numDecals)
stream.guard(7)
# Default translations and rotations
assert len(self.default_rotations) == len(self.nodes)
assert len(self.default_translations) == len(self.nodes)
for i in range(len(self.nodes)):
stream.write_quat(self.default_rotations[i])
stream.write_vec3(self.default_translations[i])
# Animation translations and rotations
for point in self.node_translations:
stream.write_vec3(point)
for quat in self.node_rotations:
stream.write_quat(quat)
stream.guard(8)
# Default scales
for point in self.node_uniform_scales:
stream.write_float(point)
for point in self.node_aligned_scales:
stream.write_vec3(point)
for point in self.node_arbitrary_scale_factors:
stream.write_vec3(point)
# if dtsVersion >= 26:
for quat in self.node_arbitrary_scale_rots:
stream.write_quat(quat)
stream.guard(9)
# Ground transformations
assert len(self.ground_translations) == len(self.ground_rotations)
for point in self.ground_translations:
self.write_vec3(point)
for quat in self.ground_rotations:
self.write_quat(quat)
stream.guard(10)
# Object states
for state in self.objectstates:
state.write(stream)
stream.guard(11)
# Decal states
for state in self.decalstates:
state.write(stream)
stream.guard(12)
# Triggers
for trigger in self.triggers:
trigger.write(stream)
stream.guard(13)
# Detail levels
for lod in self.detail_levels:
lod.write(stream)
stream.guard(14)
# Meshes
for mesh in self.meshes:
mesh.write(stream)
stream.guard()
# Names
for name in self.names:
stream.write_string(name)
stream.guard()
# Finished with the 3-buffer section
stream.flush(fd)
# Sequences
ws(fd, "<i", len(self.sequences))
for seq in self.sequences:
seq.write(fd)
# Materials
ws(fd, "b", 0x1)
ws(fd, "i", len(self.materials))
for mat in self.materials:
if dtsVersion >= 26:
ws(fd, "i", len(mat.name))
else:
ws(fd, "b", len(mat.name))
fd.write(mat.name.encode("cp1252"))
for mat in self.materials:
ws(fd, "i", mat.flags)
for mat in self.materials:
ws(fd, "i", mat.reflectanceMap)
for mat in self.materials:
ws(fd, "i", mat.bumpMap)
for mat in self.materials:
ws(fd, "i", mat.detailMap)
if dtsVersion == 25:
for mat in self.materials:
fd.write(b"\x00\x00\x00\x00")
for mat in self.materials:
ws(fd, "f", mat.detailScale)
for mat in self.materials:
ws(fd, "f", mat.reflectance)
def load(self, fd):
stream = DtsInputStream(fd)
# Header
n_node = stream.read32()
n_object = stream.read32()
n_decal = stream.read32()
n_subshape = stream.read32()
n_ifl = stream.read32()
if stream.dtsVersion < 22:
n_noderotation = stream.read32()
n_noderotation -= n_node
n_nodetranslation = n_noderotation
n_nodescaleuniform = 0
n_nodescalealigned = 0
n_nodescalearbitrary = 0
else:
n_noderotation = stream.read32()
n_nodetranslation = stream.read32()
n_nodescaleuniform = stream.read32()
n_nodescalealigned = stream.read32()
n_nodescalearbitrary = stream.read32()
if stream.dtsVersion > 23:
n_groundframe = stream.read32()
else:
n_groundframe = 0
n_objectstate = stream.read32()
n_decalstate = stream.read32()
n_trigger = stream.read32()
n_detaillevel = stream.read32()
n_mesh = stream.read32()
if stream.dtsVersion < 23:
n_skin = stream.read32()
else:
n_skin = 0
n_name = stream.read32()
self.smallest_size = stream.read_float()
self.smallest_detail_level = stream.read32()
stream.guard()
# Misc geometry properties
self.radius = stream.read_float()
self.radius_tube = stream.read_float()
self.center = stream.read_vec3()
self.bounds = stream.read_box()
stream.guard()
# Primary data
self.nodes = [Node.read(stream) for i in range(n_node)]
stream.guard()
self.objects = [Object.read(stream) for i in range(n_object)]
stream.guard()
self.decals = [Decal.read(stream) for i in range(n_decal)]
stream.guard()
self.iflmaterials = [IflMaterial.read(stream) for i in range(n_ifl)]
stream.guard()
# Subshapes
self.subshapes = [Subshape(0, 0, 0, 0, 0, 0) for i in range(n_subshape)]
for i in range(n_subshape):
self.subshapes[i].firstNode = stream.read32()
for i in range(n_subshape):
self.subshapes[i].firstObject = stream.read32()
for i in range(n_subshape):
self.subshapes[i].firstDecal = stream.read32()
stream.guard()
for i in range(n_subshape):
self.subshapes[i].numNodes = stream.read32()
for i in range(n_subshape):
self.subshapes[i].numObjects = stream.read32()
for i in range(n_subshape):
self.subshapes[i].numDecals = stream.read32()
stream.guard()
# MeshIndexList (obsolete data)
if stream.dtsVersion < 16:
for i in range(stream.read32()):
stream.read32()
# Default translations and rotations
self.default_rotations = [None] * n_node
self.default_translations = [None] * n_node
for i in range(n_node):
self.default_rotations[i] = stream.read_quat()
self.default_translations[i] = stream.read_vec3()
# Animation translations and rotations
self.node_translations = [stream.read_vec3() for i in range(n_nodetranslation)]
self.node_rotations = [stream.read_quat() for i in range(n_noderotation)]
stream.guard()
# Default scales
if stream.dtsVersion > 21:
self.node_uniform_scales = [stream.read_float() for i in range(n_nodescaleuniform)]
self.node_aligned_scales = [stream.read_vec3() for i in range(n_nodescalealigned)]
self.node_arbitrary_scale_factors = [stream.read_vec3() for i in range(n_nodescalearbitrary)]
self.node_arbitrary_scale_rots = [stream.read_quat() for i in range(n_nodescalearbitrary)]
stream.guard()
else:
self.node_uniform_scales = [None] * n_nodescaleuniform
self.node_aligned_scales = [None] * n_nodescalealigned
self.node_arbitrary_scale_factors = [None] * n_nodescalearbitrary
self.node_arbitrary_scale_rots = [None] * n_nodescalearbitrary
# ???
# print(stream.dtsVersion)
# print(stream.sequence)
# if stream.dtsVersion > 21:
# what1 = stream.read32()
# what2 = stream.read32()
# what3 = stream.read32()
# stream.guard()
# Ground transformations
if stream.dtsVersion > 23:
self.ground_translations = [stream.read_vec3() for i in range(n_groundframe)]
self.ground_rotations = [stream.read_quat() for i in range(n_groundframe)]
stream.guard()
else:
self.ground_translations = [None] * n_groundframe
self.ground_rotations = [None] * n_groundframe
# Object states
self.objectstates = [ObjectState.read(stream) for i in range(n_objectstate)]
stream.guard()
# Decal states
self.decalstates = [stream.read32() for i in range(n_decalstate)]
stream.guard()
# Triggers
self.triggers = [Trigger.read(stream) for i in range(n_trigger)]
stream.guard()
# Detail levels
self.detail_levels = [DetailLevel.read(stream) for i in range(n_detaillevel)]
stream.guard()
# Meshes
self.meshes = [Mesh.read(stream) for i in range(n_mesh)]
stream.guard()
# Names
self.names = [None] * n_name
self._names_lookup = {}
for i in range(n_name):
self.names[i] = stream.read_string()
self._names_lookup[self.names[i]] = i
stream.guard()
self.alpha_in = [None] * n_detaillevel
self.alpha_out = [None] * n_detaillevel
if stream.dtsVersion >= 26:
for i in range(n_detaillevel):
self.alphaIn[i] = stream.read32()
for i in range(n_detaillevel):
self.alphaOut[i] = stream.read32()
# Done with the tribuffer section
n_sequence = unpack("i", fd.read(4))[0]
self.sequences = [None] * n_sequence
for i in range(n_sequence):
self.sequences[i] = Sequence.read(fd)
material_type = unpack("b", fd.read(1))[0]
assert material_type == 0x1
n_material = unpack("i", fd.read(4))[0]
self.materials = [Material() for i in range(n_material)]
for i in range(n_material):
if stream.dtsVersion >= 26:
length = unpack("i", fd.read(4))[0]
else:
length = unpack("B", fd.read(1))[0]
self.materials[i].name = fd.read(length).decode("cp1252")
for i in range(n_material):
self.materials[i].flags = unpack("I", fd.read(4))[0]
for i in range(n_material):
self.materials[i].reflectanceMap = unpack("i", fd.read(4))[0]
for i in range(n_material):
self.materials[i].bumpMap = unpack("i", fd.read(4))[0]
for i in range(n_material):
self.materials[i].detailMap = unpack("i", fd.read(4))[0]
if stream.dtsVersion == 25:
for i in range(n_material):
fd.read(4)
for i in range(n_material):
self.materials[i].detailScale = unpack("f", fd.read(4))[0]
for i in range(n_material):
self.materials[i].reflectance = unpack("f", fd.read(4))[0]