#!/usr/bin/python import os import sys # see: http://pico-8.wikia.com/wiki/P8FileFormat # # pico-8 cartridge // http://www.pico-8.com # version 16 # __lua__ # __gfx__ # __gff__ # __label__ # __map__ # __sfx__ # __music__ # the order of chunks in .p8 file. do NOT modify! chunkorder = ['__ver__','__lua__','__gfx__','__gff__','__label__','__map__','__sfx__','__music__'] # ----------------------------------------------------------------------- # edit these to match your project # output configuration reconstruct = { '__ver__': 'ver0', # header of .p8 file (incl version number) '__lua__': 'ver0', # Lua source code '__gfx__': 'ver1', # sprite sheet '__gff__': 'ver1', # sprite flags '__label__': 'ver1', # saved screen shot (for PNG cartridge) '__map__': 'ver1', # map '__sfx__': 'ver2', # sounds '__music__': 'ver2', # music } # where the final copy should go # (also checks each file against this to detect changes) fn_final = 'gamename' # ----------------------------------------------------------------------- # collect all the files to be loaded fns_input = set(reconstruct.values()) # checks if .p8 file exists def p8_exists(fn): return os.path.exists('%s.p8'%fn) # reads .p8 data and splits into chunks based on chunkorder def read_chunks(fn): text = open('%s.p8'%fn, 'rt').read() chunks = { k:[] for k in chunkorder } currentchunk = '__ver__' # always start with .p8 header for line in text.splitlines(): if line in chunkorder: currentchunk = line chunks[currentchunk].append(line) chunks = { k:'\n'.join(chunks[k]) for k in chunkorder } return chunks # writes chunks to .p8 file def write_chunks(fn, chunks): output = '\n'.join(chunks[k] for k in chunkorder) open('%s.p8'%fn,'wt').write(output) while True: while raw_input('\nType "combine" to combine: ') != 'combine': pass while raw_input('Have you saved, yet? ') != 'yes': pass # make sure that all required files are found missing = sorted([fn for fn in fns_input if not p8_exists(fn)]) if missing: print('\nMissing the following files: %s' % (','.join(missing))) continue # load data and split into chunks print('\nLoading...') chunksets = { fn:read_chunks(fn) for fn in fns_input } # if final copy file exists, check each version against it # to make sure that no one has changed something that they # should not have changed if p8_exists(fn_final): final = read_chunks() mods = {} for fn in fns_input: chunks = chunksets[fn] for k in chunkorder: if reconstruct[k] == fn: continue if final[k] == chunks[k]: continue if fn not in mods: mods[fn] = [] # chunk has been modified (will lose information!) mods[fn].append(k) if mods: # a chunk is modified! report! print('\nChunks have been modified that will be overwritten!') for fn in mods: print('%s: %s' % (fn, ', '.join(mods[fn]))) action = '' while True: action = raw_input('Type in action ("cancel", "overwrite"): ') if action in {'cancel', 'overwrite'}: break if action == 'cancel': continue # reconstruct based on header order and configuration chunks = { k:chunksets[reconstruct[k]][k] for k in chunkorder } # write new files print('\nSaving...') write_chunks(fn_final, chunks) #for fn in fns_input: write_chunks(fn, chunks) # done! print('\nDone!\nReload your PICO-8 files!')