## LVL v0 ## import xml.etree.ElementTree as ET import sys import enum import os import shutil VERSION=0 LP_F=(1<<0) LP_FT=(1<<1) LP_G=(1<<2) LP_GT=(1<<3) def main(): xml = '' # parse xml file from argument try: xml = sys.argv[1] tree = ET.parse(xml) root = tree.getroot() except IndexError: print('Error: Please provide an argument to ' + sys.argv[0]) sys.exit(1) except IOError: print('Error: \'' + sys.argv[1] + '\' does not exist!') sys.exit(2) with open('C:\\Documents and Settings\\sveeion\\Desktop\\a.lvl', 'wb') as f: ## HEADER ## version = int(root.attrib['version']) if version != VERSION: print('Error: given version in lvlx file (%d) does not match current version (%d)\n' % (version, VERSION)) sys.exit(3) lodpath = root.attrib['lodpath'] maincxcpath = root.attrib['maincxcpath'] lptotal = 0 for i in root.findall('part'): lptotal+=1 version = version.to_bytes(2, byteorder='little', signed=False) lptotal = lptotal.to_bytes(2, byteorder='little', signed=False) sky_r = int(root.attrib['sky_r']).to_bytes(1, byteorder='little', signed=False) sky_g = int(root.attrib['sky_g']).to_bytes(1, byteorder='little', signed=False) sky_b = int(root.attrib['sky_b']).to_bytes(1, byteorder='little', signed=False) f.write(version + lptotal) f.write(sky_r + sky_g + sky_b + b'\xFF') lodsize = os.path.getsize(lodpath).to_bytes(4, byteorder='little', signed=False) maincxcsize = os.path.getsize(maincxcpath).to_bytes(4, byteorder='little', signed=False) f.write(lodsize) src = open(lodpath, 'rb') shutil.copyfileobj(src, f) src.close() f.write(maincxcsize) src = open(maincxcpath, 'rb') shutil.copyfileobj(src, f) src.close() ## BODY ## for lp in root.findall('part'): li = [LP_F, LP_FT, LP_G, LP_GT] ls = ['F', 'FT', 'G', 'GT'] lptype = int(lp.attrib['type']) lptimidx = int(lp.attrib['timidx']) hi = lp.find('hi') lo = lp.find('lo') f.write(lptype.to_bytes(4, byteorder='little', signed=False)) f.write(lptimidx.to_bytes(4, byteorder='little', signed=False)) for i in range(4): if lptype & li[i]: hi_size = int(os.path.getsize(hi.attrib[ls[i]])).to_bytes(4, byteorder='little', signed=False) f.write(hi_size) src=open(hi.attrib[ls[i]], 'rb') shutil.copyfileobj(src, f) src.close() print('hi_size == %d' % (int.from_bytes(hi_size, byteorder='little', signed=False))) lo_size = int(os.path.getsize(lo.attrib[ls[i]])).to_bytes(4, byteorder='little', signed=False) f.write(lo_size) src=open(lo.attrib[ls[i]], 'rb') shutil.copyfileobj(src, f) src.close() print('LVL file written to desktop!') if __name__ == '__main__': main()