import struct
d=open("content/VIDEO/MAT/BLXSKIN.BMF","rb").read()
TAGS={0x2008:"NAME",0x11:"TEXTURE_MAP",0x21:"MAT_TEXTURE",0x18:"BITSLICE",0x28:"RAMP_REF",0x20:"MATERIAL",
0x30:"RAMP",0x24:"DIFFUSE",0x26:"EMISSIVE",0x23:"AMBIENT",0x2037:"SV_SPECIAL",0x2030:"SV_MATERIAL"}
CONT={0x20,0x30,0x10}
def parse(p,end,depth=0):
    while p+3<=end:
        tw=struct.unpack_from("<H",d,p)[0]; p+=2
        cid=tw&0x2fff; lw=4 if(tw&0x8000)else(2 if(tw&0x4000)else 1)
        if p+lw>end:return
        ln=d[p] if lw==1 else (struct.unpack_from("<H",d,p)[0] if lw==2 else struct.unpack_from("<I",d,p)[0])
        p+=lw
        if p+ln>end:return
        nm=TAGS.get(cid,hex(cid))
        body=d[p:p+ln]
        txt=''.join(chr(b) if 32<=b<127 else '.' for b in body[:40])
        print("  "*depth+f"{nm}({hex(cid)}) len={ln}  {txt!r}")
        if cid in CONT: parse(p,p+ln,depth+1)
        p+=ln
        if cid==0x0005:break
parse(8,len(d))
