Files

26 lines
1.3 KiB
Python

"""Add a $FD1 event (CreatureSoundFidget index 0) keyed t=0 on the anim-94 slot of an MD21 model.
Old events are copied unchanged; the new array and key tables are appended to the MD21 chunk
(offsets are MD21-relative, so later chunks such as SFID/TXID only move as whole chunks)."""
import struct,sys
from m2ev import chunks,events
src,dst=sys.argv[1],sys.argv[2]
b=open(src,'rb').read();ch=chunks(b)
mo,mn=ch['MD21'];md=bytearray(b[mo:mo+mn])
seqs,ev=events(b)
assert not any(e[0]=='$FD1' for e in ev),'model already has $FD1'
slots=[i for i,s in enumerate(seqs) if s[0]==94];assert slots,'no anim 94'
ne,eo=struct.unpack_from('<II',md,0x100)
def pad():
md.extend(bytes((-len(md))%16))
pad();key_at=len(md);md.extend(struct.pack('<I',0))
pad();outer_at=len(md)
for i in range(len(seqs)):md.extend(struct.pack('<II',1,key_at) if i in slots else struct.pack('<II',0,0))
pad();new_eo=len(md)
md.extend(md[eo:eo+36*ne])
md.extend(b'$FD1'+struct.pack('<II',0,0)+bytes(12)+struct.pack('<hhII',0,-1,len(seqs),outer_at))
struct.pack_into('<II',md,0x100,ne+1,new_eo)
out=bytearray(b[:mo-8])+b'MD21'+struct.pack('<I',len(md))+md+b[mo+mn:]
open(dst,'wb').write(out)
s2,e2=events(bytes(out))
print('events',len(ev),'->',len(e2),[(e[0],e[2]) for e in e2 if e[0]=='$FD1'],'slots',slots,len(b),'->',len(out))