128 lines
5.0 KiB
Python
128 lines
5.0 KiB
Python
"""#56 (2026-11-25): add a new external sequence to a .skel-based native character (Undead / Blood Elf male HD).
|
|
|
|
skel: SKS1 gets the new M2Sequence (template copy) and the lookup slot; every SKB1 bone track and SKA1 attachment track
|
|
sized to the old sequence count grows by one empty entry; AFID gets (anim id, 0, new .anim FDID). Track offsets are
|
|
relative to their own chunk, so each chunk grows independently.
|
|
m2: the model keeps per-sequence arrays of its own (events, cameras) sized to the skel sequence count; grow them too.
|
|
|
|
usage: add_sequence_skel.py skel_in m2_in template_seq_index anim_id anim_fdid skel_out m2_out [sub]
|
|
(sub>0: a further variation; the previous variation's next points to it, the lookup keeps the first)
|
|
"""
|
|
import struct
|
|
import sys
|
|
|
|
|
|
def chunks(b):
|
|
at, out = 0, []
|
|
while at + 8 <= len(b):
|
|
tag, n = struct.unpack_from('<4sI', b, at)
|
|
out.append([tag, bytearray(b[at + 8:at + 8 + n])])
|
|
at += 8 + n
|
|
assert at == len(b), 'trailing bytes'
|
|
return out
|
|
|
|
|
|
def get(parts, tag):
|
|
return next(p[1] for p in parts if p[0] == tag)
|
|
|
|
|
|
def appender(D):
|
|
def append(data):
|
|
D.extend(bytes((-len(D)) % 16))
|
|
off = len(D)
|
|
D.extend(data)
|
|
return off
|
|
return append
|
|
|
|
|
|
def grower(D, K, counter):
|
|
append = appender(D)
|
|
|
|
def full(at):
|
|
tc, to, vc, vo = struct.unpack_from('<IIII', D, at + 4)
|
|
if tc != K:
|
|
return
|
|
assert vc == K, ('values count', vc, K)
|
|
nt = append(bytes(D[to:to + 8 * K]) + bytes(8))
|
|
nv = append(bytes(D[vo:vo + 8 * K]) + bytes(8))
|
|
struct.pack_into('<IIII', D, at + 4, K + 1, nt, K + 1, nv)
|
|
counter['full'] += 1
|
|
|
|
def base(at):
|
|
tc, to = struct.unpack_from('<II', D, at + 4)
|
|
if tc != K:
|
|
return
|
|
nt = append(bytes(D[to:to + 8 * K]) + bytes(8))
|
|
struct.pack_into('<II', D, at + 4, K + 1, nt)
|
|
counter['base'] += 1
|
|
return full, base
|
|
|
|
|
|
def main(skel_in, m2_in, template, anim_id, anim_fdid, skel_out, m2_out, sub='0'):
|
|
template, anim_id, anim_fdid, sub = int(template), int(anim_id), int(anim_fdid), int(sub)
|
|
parts = chunks(open(skel_in, 'rb').read())
|
|
S = get(parts, b'SKS1')
|
|
K, so = struct.unpack_from('<II', S, 8)
|
|
ids = [struct.unpack_from('<H', S, so + 64 * i)[0] for i in range(K)]
|
|
subs = [struct.unpack_from('<H', S, so + 64 * i + 2)[0] for i in range(K)]
|
|
assert (anim_id, sub) not in zip(ids, subs), 'sequence already present'
|
|
entry = bytearray(S[so + 64 * template:so + 64 * template + 64])
|
|
struct.pack_into('<HH', entry, 0, anim_id, sub)
|
|
struct.pack_into('<hH', entry, 60, -1, K)
|
|
new_so = appender(S)(bytes(S[so:so + 64 * K]) + bytes(entry))
|
|
struct.pack_into('<II', S, 8, K + 1, new_so)
|
|
lc, lo = struct.unpack_from('<II', S, 16)
|
|
if sub:
|
|
prev = next(i for i in range(K) if (ids[i], subs[i]) == (anim_id, sub - 1))
|
|
struct.pack_into('<h', S, new_so + 64 * prev + 60, K)
|
|
# The first variation's alias is itself; later variations keep their own index.
|
|
lc = 0
|
|
# Client probe: i += p*p (see add_sequence.py).
|
|
s, p = (anim_id % lc if lc else -1), 0
|
|
while lc and struct.unpack_from('<h', S, lo + 2 * s)[0] != -1:
|
|
p += 1
|
|
if p >= lc:
|
|
raise SystemExit('lookup table full')
|
|
s = (s + p * p) % lc
|
|
if lc:
|
|
struct.pack_into('<h', S, lo + 2 * s, K)
|
|
|
|
counter = {'full': 0, 'base': 0}
|
|
Bn = get(parts, b'SKB1')
|
|
full, _ = grower(Bn, K, counter)
|
|
bc, bo = struct.unpack_from('<II', Bn, 0)
|
|
for j in range(bc):
|
|
for f in (16, 36, 56):
|
|
full(bo + 88 * j + f)
|
|
if any(p[0] == b'SKA1' for p in parts):
|
|
A = get(parts, b'SKA1')
|
|
full, _ = grower(A, K, counter)
|
|
ac, ao = struct.unpack_from('<II', A, 0)
|
|
for k in range(ac):
|
|
full(ao + 40 * k + 20)
|
|
get(parts, b'AFID').extend(struct.pack('<HHI', anim_id, 0, anim_fdid))
|
|
open(skel_out, 'wb').write(b''.join(struct.pack('<4sI', t, len(d)) + bytes(d) for t, d in parts))
|
|
|
|
mparts = chunks(open(m2_in, 'rb').read())
|
|
N = get(mparts, b'MD21')
|
|
assert struct.unpack_from('<I', N, 0x1C)[0] == 0, 'm2 carries its own sequences'
|
|
mcount = {'full': 0, 'base': 0}
|
|
full, base = grower(N, K, mcount)
|
|
for off, size, fields in ((0x48, 40, (0, 20)), (0x58, 20, (0,)), (0x60, 60, (0, 20, 40)), (0xF0, 40, (20,)),
|
|
(0x108, 156, (16, 36, 56, 76, 96, 116, 136)), (0x110, 116, (12, 44, 76, 96))):
|
|
c, po = struct.unpack_from('<II', N, off)
|
|
for k in range(c):
|
|
for f in fields:
|
|
full(po + size * k + f)
|
|
ec, eo = struct.unpack_from('<II', N, 0x100)
|
|
for k in range(ec):
|
|
base(eo + 36 * k + 24)
|
|
for off in (0x120, 0x128):
|
|
assert struct.unpack_from('<I', N, off)[0] == 0, 'ribbons/particles not handled'
|
|
open(m2_out, 'wb').write(b''.join(struct.pack('<4sI', t, len(d)) + bytes(d) for t, d in mparts))
|
|
print('seq index', K, 'skel grown', counter, 'm2 grown', mcount, 'lookup slot', s, 'of', lc)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main(*sys.argv[1:9])
|