Difference between revisions of "PythonAfBkParser"

From WTFwiki
Jump to navigation Jump to search
m (3 revisions)
 
(One intermediate revision by one other user not shown)
Line 13: Line 13:
 
def read_af_file(f):
 
def read_af_file(f):
 
     header = struct.unpack('<HhlBHllllBB', f.read(29))
 
     header = struct.unpack('<HhlBHllllBB', f.read(29))
     anims = []
+
     anims = {}
 
     while True:
 
     while True:
 
         animNum = struct.unpack('<B', f.read(1))[0]
 
         animNum = struct.unpack('<B', f.read(1))[0]
Line 22: Line 22:
 
         n = struct.unpack('<H', f.read(2))[0]
 
         n = struct.unpack('<H', f.read(2))[0]
 
         anim.footerString = f.read(n)
 
         anim.footerString = f.read(n)
         anims.append(anim)
+
         anims[animNum] = anim
 
     footer = f.read(30)
 
     footer = f.read(30)
 
     return Obj(header=header, anims=anims, footer=footer)
 
     return Obj(header=header, anims=anims, footer=footer)
Line 28: Line 28:
 
def read_bk_file(f):
 
def read_bk_file(f):
 
     header = struct.unpack('<LBHH', f.read(9))
 
     header = struct.unpack('<LBHH', f.read(9))
     anims = []
+
     anims = {}
 
     while True:
 
     while True:
 
         (nextOffset, animNum) = struct.unpack('<LB', f.read(5))
 
         (nextOffset, animNum) = struct.unpack('<LB', f.read(5))
Line 35: Line 35:
 
         animHeaderString = f.read(animHeader[6])
 
         animHeaderString = f.read(animHeader[6])
 
         anim = read_animation(f)
 
         anim = read_animation(f)
         anims.append(Obj(header=animHeader, headerStr=animHeaderString, anim=anim))
+
         anims[animNum] = Obj(header=animHeader, headerStr=animHeaderString, anim=anim)
 
     background = f.read(header[2] * header[3])
 
     background = f.read(header[2] * header[3])
 
     numPalettes = struct.unpack('B', f.read(1))[0]
 
     numPalettes = struct.unpack('B', f.read(1))[0]

Latest revision as of 21:49, 4 January 2013

This python script will load .af and .bk files.

Code

import struct

class Obj(object):
    def __init__(self, **kw):
        for k, v in kw.items():
            self.__setattr__(k, v)

def read_af_file(f):
    header = struct.unpack('<HhlBHllllBB', f.read(29))
    anims = {}
    while True:
        animNum = struct.unpack('<B', f.read(1))[0]
        if animNum >= 70: break
        anim = read_animation(f)
        anim.animAttrs = f.read(21)
        anim.moveString = f.read(21)
        n = struct.unpack('<H', f.read(2))[0]
        anim.footerString = f.read(n)
        anims[animNum] = anim
    footer = f.read(30)
    return Obj(header=header, anims=anims, footer=footer)

def read_bk_file(f):
    header = struct.unpack('<LBHH', f.read(9))
    anims = {}
    while True:
        (nextOffset, animNum) = struct.unpack('<LB', f.read(5))
        if animNum >= 50: break
        animHeader = struct.unpack('<BBBBHBH', f.read(9))
        animHeaderString = f.read(animHeader[6])
        anim = read_animation(f)
        anims[animNum] = Obj(header=animHeader, headerStr=animHeaderString, anim=anim)
    background = f.read(header[2] * header[3])
    numPalettes = struct.unpack('B', f.read(1))[0]
    palettes = []
    for i in range(numPalettes):
        palettes.append(struct.unpack('<768B1024B3840B', f.read(768+1024+3840)))
    footer = f.read(30)
    return Obj(header=header, anims=anims, background=background, palettes=palettes, footer=footer)

def read_cntbs(f):
    strlen = struct.unpack('<H', f.read(2))[0]
    return f.read(strlen+1)[:-1]

def read_animation(f):
    header = struct.unpack('<8BHB', f.read(11))
    numOverlays = header[8]
    numSprites = header[9]
    overlays = f.read(numOverlays * 4)
    animStr = read_cntbs(f)
    numExtras = struct.unpack('B', f.read(1))[0]
    extraAnimStrs = [read_cntbs(f) for n in range(numExtras)]
    sprites = [read_sprite(f) for n in range(numSprites)]
    return Obj(header=header, overlays=overlays, animStr=animStr, extraAnimStrs=extraAnimStrs, sprites=sprites)

def read_sprite(f):
    header = struct.unpack('<HhhHHBB', f.read(12))
    data = None
    if not header[6]:
        data = f.read(header[0])
    return Obj(header=header, data=data)