def read(cls, f): print 'Loading map at', f chunks = cls.unpackchunks(f) hd = MPHD() # perhaps cpystruct should only read as many bytes as it can handle? hd.unpack(chunks['MPHD'].data[:len(hd)]) m = Tiled.Map(Tiled.Map.Orthogonal, hd.mapwidth, hd.mapheight, hd.blockwidth, hd.blockheight) if hd.type == 2: print 'Isometric maps not supported at the moment' return m m.setProperty('chunks', cls.picklechunks(chunks)) tset = Tiled.Tileset('Tiles', hd.blockwidth, hd.blockheight, 0, 0) cmap = list(FMPColormap.unpack(chunks['CMAP'].data)) tset.loadFromImage(FMPTileGfx.unpack(hd, chunks['BGFX'].data, cmap), "") blks = FMPBlocks(chunks['BKDT'].data, hd).blocks for c in ['LYR' + str(i) for i in range(7, 0, -1)] + ['BODY']: if not chunks.has_key(c): continue print 'populating', c lay = Tiled.TileLayer(c, 0, 0, hd.mapwidth, hd.mapheight) lvl = list(FMPLayer.unpack(hd, chunks[c].data)) FMPLayer.populate(lay, blks, tset, hd, lvl) m.addLayer(lay) m.addTileset(tset) return m
def write(cls, m, fn): if m.orientation() != Tiled.Map.Orthogonal: print 'Isometric maps not supported at the moment' return False if not m.property('chunks'): raise Exception( "Export depends on unparsed binary blobs from original " + "fmp to be stored in the map property 'chunks'") print 'Writing map at', fn chunks = cls.unpicklechunks(m.property('chunks')) hd = MPHD() hd.unpack(chunks['MPHD'].data[:len(hd)]) blks = FMPBlocks(chunks['BKDT'].data, hd).blocks for i in range(m.layerCount()): if not isTileLayerAt(m, i): continue l = tileLayerAt(m, i) chunks[l.name()].data = FMPLayer.pack(hd, blks, l, i) cls.packchunks(fn, chunks) return True
def read(cls, f): print 'Loading map at',f chunks = cls.unpackchunks(f) hd = MPHD() # perhaps cpystruct should only read as many bytes as it can handle? hd.unpack(chunks['MPHD'].data[:len(hd)]) m = Tiled.Map(Tiled.Map.Orthogonal, hd.mapwidth, hd.mapheight, hd.blockwidth, hd.blockheight) if hd.type == 2: print 'Isometric maps not supported at the moment' return m m.setProperty('chunks', cls.picklechunks(chunks)) tset = Tiled.Tileset('Tiles', hd.blockwidth, hd.blockheight, 0, 0) cmap = list(FMPColormap.unpack(chunks['CMAP'].data)) tset.loadFromImage(FMPTileGfx.unpack(hd, chunks['BGFX'].data, cmap), "") blks = FMPBlocks(chunks['BKDT'].data, hd).blocks for c in ['LYR'+str(i) for i in range(7,0,-1)]+['BODY']: if not chunks.has_key(c): continue print 'populating',c lay = Tiled.TileLayer(c,0,0,hd.mapwidth, hd.mapheight) lvl = list(FMPLayer.unpack(hd, chunks[c].data)) FMPLayer.populate(lay, blks, tset, hd, lvl) m.addLayer(lay) m.addTileset(tset) return m
def write(cls, m, fn): if m.orientation() != Tiled.Map.Orthogonal: print 'Isometric maps not supported at the moment' return False if not m.property('chunks'): raise Exception("Export depends on unparsed binary blobs from original " +"fmp to be stored in the map property 'chunks'") print 'Writing map at',fn chunks = cls.unpicklechunks(m.property('chunks')) hd = MPHD() hd.unpack(chunks['MPHD'].data[:len(hd)]) blks = FMPBlocks(chunks['BKDT'].data, hd).blocks for i in range(m.layerCount()): if not isTileLayerAt(m, i): continue l = tileLayerAt(m, i) chunks[l.name()].data = FMPLayer.pack(hd, blks, l, i) cls.packchunks(fn, chunks) return True
def readchunks(f): chunks = {} with open(f, 'rb') as fh: fc = fmpchunk() fc.unpack(fh) filelen = fc.len - 16 print fc, fh.read(4) while fh.tell() < filelen and fc.unpack(fh) != None: print fh.tell(), fc if fc.id == 'MPHD': mp = MPHD() mp.unpack(fh) realsz = struct.calcsize(mp.__fstr) #if fc.len > realsz: # raise Exception('Unread data: '+str(fc.len-realsz)) fh.read(fc.len-realsz) print mp chunks[fc.id] = mp else: chunks[fc.id] = fh.read(fc.len) return chunks
def readchunks(f): chunks = {} with open(f, 'rb') as fh: fc = fmpchunk() fc.unpack(fh) filelen = fc.len - 16 print fc, fh.read(4) while fh.tell() < filelen and fc.unpack(fh) != None: print fh.tell(), fc if fc.id == 'MPHD': mp = MPHD() mp.unpack(fh) realsz = struct.calcsize(mp.__fstr) #if fc.len > realsz: # raise Exception('Unread data: '+str(fc.len-realsz)) fh.read(fc.len - realsz) print mp chunks[fc.id] = mp else: chunks[fc.id] = fh.read(fc.len) return chunks