28 lines
1.7 KiB
Python
28 lines
1.7 KiB
Python
"""python3 fd1.py src.m2 dst.m2 T : key $FD1 (CreatureSoundFidget index 0) at T ms on every anim-94 slot of an MD21 model.
|
|
Existing $FD1 event: its per-sequence table is copied with the anim-94 slots replaced. Otherwise a new event is appended.
|
|
All additions go to the end of the MD21 chunk (MD21-relative offsets); other chunks move whole."""
|
|
import struct,sys
|
|
src,dst,T=sys.argv[1],sys.argv[2],int(sys.argv[3])
|
|
b=open(src,'rb').read();o=0;ch={}
|
|
while o+8<=len(b):
|
|
t,l=b[o:o+4],struct.unpack_from('<I',b,o+4)[0];ch[t]=(o+8,l);o+=8+l
|
|
mo,mn=ch[b'MD21'];md=bytearray(b[mo:mo+mn])
|
|
ns,so=struct.unpack_from('<II',md,0x1C);slots=[i for i in range(ns) if struct.unpack_from('<H',md,so+64*i)[0]==94];assert slots
|
|
ne,eo=struct.unpack_from('<II',md,0x100)
|
|
fd=[k for k in range(ne) if md[eo+36*k:eo+36*k+4]==b'$FD1']
|
|
def pad():md.extend(bytes((-len(md))%16))
|
|
pad();key_at=len(md);md.extend(struct.pack('<I',T))
|
|
pad();outer=len(md)
|
|
if fd:
|
|
k=fd[0];nt,to=struct.unpack_from('<II',md,eo+36*k+28);assert nt==ns,(nt,ns)
|
|
old=[struct.unpack_from('<II',md,to+8*i) for i in range(ns)]
|
|
for i in range(ns):md.extend(struct.pack('<II',1,key_at) if i in slots else struct.pack('<II',*old[i]))
|
|
struct.pack_into('<II',md,eo+36*k+28,ns,outer)
|
|
else:
|
|
for i in range(ns):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,ns,outer))
|
|
struct.pack_into('<II',md,0x100,ne+1,new_eo)
|
|
out=b[:mo-8]+b'MD21'+struct.pack('<I',len(md))+bytes(md)+b[mo+mn:]
|
|
open(dst,'wb').write(out);print(src,'existing' if fd else 'new','slots',slots,len(b),'->',len(out))
|