-
Notifications
You must be signed in to change notification settings - Fork 0
/
testbck.py
356 lines (269 loc) · 11.1 KB
/
testbck.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
import struct
BCKFILEMAGIC = b"J3D1bck1"
PADDING = b"This is padding data to align"
def read_uint32(f):
return struct.unpack(">I", f.read(4))[0]
def read_uint16(f):
return struct.unpack(">H", f.read(2))[0]
def read_sint16(f):
return struct.unpack(">h", f.read(2))[0]
def read_uint8(f):
return struct.unpack(">B", f.read(1))[0]
def read_sint8(f):
return struct.unpack(">b", f.read(1))[0]
def read_float(f):
return struct.unpack(">f", f.read(4))[0]
def write_uint32(f, val):
f.write(struct.pack(">I", val))
def write_uint16(f, val):
f.write(struct.pack(">H", val))
def write_sint16(f, val):
f.write(struct.pack(">h", val))
def write_uint8(f, val):
f.write(struct.pack(">B", val))
def write_sint8(f, val):
f.write(struct.pack(">b", val))
def write_float(f, val):
f.write(struct.pack(">f", val))
def write_padding(f, multiple):
next_aligned = (f.tell() + (multiple - 1)) & ~(multiple - 1)
diff = next_aligned - f.tell()
for i in range(diff):
pos = i%len(PADDING)
f.write(PADDING[pos:pos+1])
# Find the start of the sequence seq in the list in_list, if the sequence exists
def find_sequence(in_list, seq):
matchup = 0
start = -1
found = False
started = False
for i, val in enumerate(in_list):
if val == seq[matchup]:
if not started:
start = i
started = True
matchup += 1
if matchup == len(seq):
#start = i-matchup
found = True
break
else:
matchup = 0
start = -1
started = False
if not found:
start = -1
return start
class StringTable(object):
def __init__(self):
self.strings = []
@classmethod
def from_file(cls, f):
stringtable = cls()
start = f.tell()
string_count = read_uint16(f)
f.read(2) # 0xFFFF
offsets = []
print("string count", string_count)
for i in range(string_count):
hash = read_uint16(f)
string_offset = read_uint16(f)
offsets.append(string_offset)
for offset in offsets:
f.seek(start+offset)
# Read 0-terminated string
string_start = f.tell()
string_length = 0
while f.read(1) != b"\x00":
string_length += 1
f.seek(start+offset)
if string_length == 0:
stringtable.strings.append("")
else:
stringtable.strings.append(f.read(string_length).decode("shift-jis"))
return stringtable
def hash_string(self, string):
hash = 0
for char in string:
hash *= 3
hash += ord(char)
hash = 0xFFFF & hash # cast to short
return hash
def write(self, f):
start = f.tell()
f.write(struct.pack(">HH", len(self.strings), 0xFFFF))
for string in self.strings:
hash = self.hash_string(string)
f.write(struct.pack(">HH", hash, 0xABCD))
offsets = []
for string in self.strings:
offsets.append(f.tell())
f.write(string.encode("shift-jis"))
f.write(b"\x00")
end = f.tell()
for i, offset in enumerate(offsets):
f.seek(start+4 + (i*4) + 2)
write_uint16(f, offset-start)
f.seek(end)
class JointAnimation(object):
def __init__(self, jointindex):
self.jointindex = jointindex
self.scale = {"X": [], "Y": [], "Z": []}
self.rotation = {"X": [], "Y": [], "Z": []}
self.translation = {"X": [], "Y": [], "Z": []}
self._scale_offsets = {}
self._rotation_offsets = {}
self._translation_offsets = {}
def serialize(self):
return [self.jointindex, self.scale, self.rotation, self.translation]
def add_scale(self, axis, scale):
self.scale[axis].append(scale)
def add_rotation(self, axis, rotation):
self.rotation[axis].append(rotation)
def add_translation(self, axis, translation):
self.translation[axis].append(translation)
def add_scale_vec(self, u, v, w):
self.add_scale("X", u)
self.add_scale("Y", v)
self.add_scale("Z", w)
def add_rotation_vec(self, u, v, w):
self.add_rotation("X", u)
self.add_rotation("Y", v)
self.add_rotation("Z", w)
def add_translate_vec(self, u, v, w):
self.add_rotation("X", u)
self.add_rotation("Y", v)
self.add_rotation("Z", w)
def _set_scale_offset(self, axis, val):
self._scale_offsets[axis] = val
def _set_rotation_offset(self, axis, val):
self._rotation_offsets[axis] = val
def _set_translation_offset(self, axis, val):
self._translation_offsets[axis] = val
from json import JSONEncoder
class MyEncoder(JSONEncoder):
def default(self, o):
return o.__dict__
class AnimComponent(object):
def __init__(self, time, value, tangentIn, tangentOut=0.0):
self.time = time
self.value = value
self.tangentIn = tangentIn
self.tangentOut = tangentOut
def convert_rotation(self, rotscale):
self.value *= rotscale
self.tangentIn *= rotscale
self.tangentOut *= rotscale
def serialize(self):
return [self.time, self.value, self.tangentIn, self.tangentOut]
@classmethod
def from_array(cls, offset, index, count, valarray, tanType):
if count == 1:
return cls(0.0, valarray[offset+index], 0.0, 0.0)
else:
if tanType == 0:
return cls(valarray[offset + index*3], valarray[offset + index*3 + 1], valarray[offset + index*3 + 2])
elif tanType == 1:
return cls(valarray[offset + index*4], valarray[offset + index*4 + 1], valarray[offset + index*4 + 2, offset + index*4 + 3])
else:
raise RuntimeError("unknown tangent type: {0}".format(tanType))
def read_bck(f):
header = f.read(8)
if header != BCKFILEMAGIC:
raise RuntimeError("Invalid header. Expected {} but found {}".format(BCKFILEMAGIC, header))
size = read_uint32(f)
print("Size of btk: {} bytes".format(size))
sectioncount = read_uint32(f)
assert sectioncount == 1
svr_data = f.read(16)
ttk_start = f.tell()
ttk_magic = f.read(4)
ttk_sectionsize = read_uint32(f)
loop_mode = read_uint8(f)
anglescale = read_sint8(f)
duration = read_uint16(f)
rotScale = (2.0**anglescale) * (180.0 / 32768.0);
jointAnimCount = read_uint16(f)
scaleFloatCount = read_uint16(f)
rotationShortsCount = read_uint16(f)
translateFloatCount = read_uint16(f)
print(hex(f.tell()))
jointAnimationEntriesOffset = read_uint32(f) + ttk_start
scaleFloatsOffset = read_uint32(f) + ttk_start
rotationShortsOffset = read_uint32(f) + ttk_start
translateFloatsOffset = read_uint32(f) + ttk_start
scaleDefault = None
rotationDefault = None
translateDefault = None
scaleFloats = []
rotationShorts = []
translateFloats = []
# Scale value bank
f.seek(scaleFloatsOffset)
print("Scale count:", scaleFloatCount, scaleFloatCount)
for i in range(scaleFloatCount):
scaleFloats.append(read_float(f))
# Rotation value bank
f.seek(rotationShortsOffset)
for i in range(rotationShortsCount):
rotationShorts.append(read_sint16(f))
# Translate value bank
f.seek(translateFloatsOffset)
print(hex(translateFloatsOffset), translateFloatCount)
for i in range(translateFloatCount):
translateFloats.append(read_float(f))
animations = []
f.seek(jointAnimationEntriesOffset)
for i in range(jointAnimCount):
jointanim = JointAnimation(i)
values = struct.unpack(">"+"H"*27, f.read(0x36))
x_scale, x_rot, x_trans = values[:3], values[3:6], values[6:9]
y_scale, y_rot, y_trans = values[9:12], values[12:15], values[15:18]
z_scale, z_rot, z_trans = values[18:21], values[21:24], values[24:27]
# Scale
countX, offsetX, tanTypeX = x_scale
countY, offsetY, tanTypeY = y_scale
countZ, offsetZ, tanTypeZ = z_scale
print("Scale")
for j in range(countX):
jointanim.add_scale("X", AnimComponent.from_array(offsetX, j, countX, scaleFloats, tanTypeX))
for j in range(countY):
jointanim.add_scale("Y", AnimComponent.from_array(offsetY, j, countY, scaleFloats, tanTypeY))
for j in range(countZ):
jointanim.add_scale("Z", AnimComponent.from_array(offsetZ, j, countZ, scaleFloats, tanTypeZ))
# Rotation
countX, offsetX, tanTypeX = x_rot
countY, offsetY, tanTypeY = y_rot
countZ, offsetZ, tanTypeZ = z_rot
print("Rotation")
for j in range(countX):
comp = AnimComponent.from_array(offsetX, j, countX, rotationShorts, tanTypeX)
comp.convert_rotation(rotScale)
jointanim.add_rotation("X", comp)
for j in range(countY):
comp = AnimComponent.from_array(offsetY, j, countY, rotationShorts, tanTypeY)
comp.convert_rotation(rotScale)
jointanim.add_rotation("Y", comp)
for j in range(countZ):
comp = AnimComponent.from_array(offsetZ, j, countZ, rotationShorts, tanTypeZ)
comp.convert_rotation(rotScale)
jointanim.add_rotation("Z", comp)
# Translate
countX, offsetX, tanTypeX = x_trans
countY, offsetY, tanTypeY = y_trans
countZ, offsetZ, tanTypeZ = z_trans
print("Translation")
for j in range(countX):
jointanim.add_translation("X", AnimComponent.from_array(offsetX, j, countX, translateFloats, tanTypeX))
for j in range(countY):
jointanim.add_translation("Y", AnimComponent.from_array(offsetY, j, countY, translateFloats, tanTypeY))
for j in range(countZ):
jointanim.add_translation("Z", AnimComponent.from_array(offsetZ, j, countZ, translateFloats, tanTypeZ))
animations.append(jointanim)
return loop_mode, anglescale, duration, animations
"""
import json
with open("ma_jump.bck", "rb") as f:
anims = read_bck(f)
with open("databck.json", "w") as f:
json.dump(anims, f, cls=MyEncoder, indent=" "*4)"""