from __future__ import print_function import os import sys import glob from struct import * def jp(*paths): return os.path.normcase(os.path.join(paths[0], *paths[1:])) def opencreate(path, mode): path = os.path.normcase(path) dir = os.path.dirname(path) if dir != '' and not os.path.exists(dir): os.makedirs(dir) return open(path, mode) def unbundlecol(path): bundle = open(path+".col", "rb") while True: header = bundle.read(8) if header == '': break fourcc, size = unpack('4sI', header) if fourcc != 'COLL': break name = unpack('24s', bundle.read(24))[0] name = name.decode('latin') pos = name.index('\x00') if pos > 0: name = name[:pos] data = bundle.read(size-24) print(name) col = opencreate(jp(path+'_col', name+'.col'), "wb") col.write(pack('4sI24s', fourcc, size, name.encode())) col.write(data) col.close() bundle.close() def bundlecol(path): bundle = opencreate(path+".col", "wb") cwd = os.getcwd() os.chdir(path+'_col') files = glob.glob('*') for fname in files: print(fname) f = open(fname, 'rb') data = f.read() f.close() bundle.write(data) bundle.close() os.chdir(cwd) if len(sys.argv) > 1: path = sys.argv[1]; if path[-4:] == '.col': unbundlecol(path[:-4]) print('unbundle', path[:-4]) elif path[-4:] == '_col': bundlecol(path[:-4]) print('bundle', path[:-4])