def export(self, array_to_export, json_file_name): wrapper_obj = OrderedDict() wrapper_obj['__json_type__'] = 'array' wrapper_obj['data'] = array_to_export mkdir(os.path.dirname(json_file_name)) with file(json_file_name, 'w') as json_file: json.dump(wrapper_obj, json_file, cls=ArrayJsonEncoder, indent=1)
def import_directory(self, in_dir, out_dir, options, file_dispatcher): logging.info('Packing all game files, from {} to {}'.format(in_dir, out_dir)) project_fname = os.path.join(in_dir, 'project.json') if not os.path.isfile(project_fname): raise RobinPackerException('Could not find a "project.json" file in {}.'.format(in_dir)) mkdir(out_dir) with file(os.path.join(in_dir, 'project.json')) as json_file: project_info = json.load(json_file) for entry in project_info: in_fname = os.path.join(in_dir, entry['unpackedFileName']) out_fname = os.path.join(out_dir, entry['packedFileName']) file_dispatcher.dispatch_file(in_fname, out_fname, options)
def export(self, in_dir, out_dir, options, file_dispatcher): logging.info('Unpacking all game files, from {} to {}'.format(in_dir, out_dir)) accepted_extensions = { '.prg' : '.json', '.gfx' : '.png', '.vga' : '.png', '.dta' : '.json' } file_list = (d for d in os.listdir(in_dir) if os.path.isfile(os.path.join(in_dir, d)) and os.path.splitext(d)[1].lower() in accepted_extensions) project_files = [] mkdir(out_dir) for in_fname in file_list: base, ext = os.path.splitext(in_fname) ext = ext.lower() out_ext = accepted_extensions[ext] out_fname = base + out_ext if ext == '.prg': mkdir(os.path.join(out_dir, base.lower())) out_fname = os.path.join(base.lower(), out_fname) elif ext in {'.gfx', '.vga'}: mkdir(os.path.join(out_dir, 'images')) out_fname = os.path.join('images', out_fname) elif ext == '.dta': mkdir(os.path.join(out_dir, 'data')) out_fname = os.path.join('data', out_fname) file_dispatcher.dispatch_file(os.path.join(in_dir, in_fname), os.path.join(out_dir, out_fname), options) project_files.append( { "packedFileName" : in_fname, "unpackedFileName" : out_fname } ) json_file_name = os.path.join(out_dir, 'project.json') with file(json_file_name, 'w') as json_file: json.dump(project_files, json_file, indent=1)
def pack(self, array_to_pack, fname): mkdir(os.path.dirname(fname)) with file(fname, 'wb') as output_file: array_to_pack.tofile(output_file)
def pack(self, rules, fname): mkdir(os.path.dirname(fname)) with file(fname, 'wb') as rfile: # Header rfile.write('\x00\x00') # Chunk 1 format = '<H' numEntries = len(rules.chunk1PointArray) * 2 pack(rfile, numEntries, format) format = '<2B' for point in rules.chunk1PointArray: pack(rfile, (point.y, point.x), format) # Chunk 2 - character data format = '<H' numEntries = len(rules.characters) assert numEntries <= 40 pack(rfile, numEntries, format) for character in rules.characters: format = '<4H2b8B' pack(rfile, ( character.posX if character.posX == 0xFFFF else ((character.posX - 4) >> 3), character.posY if character.posY == 0xFFFF else ((character.posY - 4) >> 3), character.posAltitude, character.frameArray, character._rulesBuffer2_5, character._rulesBuffer2_6, character._rulesBuffer2_7, character.spriteSize, character.direction, character._rulesBuffer2_10, character._rulesBuffer2_11, character._rulesBuffer2_12, character._rulesBuffer2_13_posX, character._rulesBuffer2_14_posY ), format ) format = '32B' pack(rfile, character.variables, format) pack(rfile, character._rulesBuffer2_16, format) # Chunk 3 & 4 - packed strings and associated indexes packedStrings = [] stringIndexes = [] totalSize = 0 for stringVal in rules.strings: stringVal = stringVal.encode('latin_1') # may not be quite right global packedStringLookup for i, lookupValue in enumerate(packedStringLookup): stringVal = stringVal.replace(lookupValue, chr(0xFF - i)) stringVal += '\x00' packedStrings.append(stringVal) stringIndexes.append(totalSize) totalSize += len(stringVal) format = '<2H' numEntries = len(packedStrings) pack(rfile, (numEntries, totalSize), format) format = '<' + str(numEntries) + 'H' pack(rfile, stringIndexes, format) for packedString in packedStrings: rfile.write(packedString) # Chunk 5 - scripts format = '<H' numEntries = len(rules.scripts.data) / 2 pack(rfile, numEntries, format) rfile.write(rules.scripts.data) # Chunk 6 - menu scripts format = '<H' numEntries = len(rules.menuScripts.data) / 2 pack(rfile, numEntries, format) rfile.write(rules.menuScripts.data) # Chunk 7 & 8 - game scripts and sizes format = '<H' numEntries = len(rules.gameScripts) pack(rfile, numEntries, format) totalSize = 0 for script_data in rules.gameScripts: pack(rfile, totalSize, format) totalSize += len(script_data.data) pack(rfile, totalSize, format) for script_data in rules.gameScripts: rfile.write(script_data.data) # Chunk 9 assert(len(rules.rulesChunk9) == 60) format = '60B' pack(rfile, rules.rulesChunk9, format) # Chunk 10 & 11 format = 'B' numEntries = len(rules.rulesChunk11) assert numEntries <= 20 pack(rfile, numEntries, format) for chunk in rules.rulesChunk11: size = len(chunk) assert size <= 0xFF pack(rfile, size, format) for chunk in rules.rulesChunk11: format = '{}B'.format(len(chunk)) pack(rfile, chunk, format) # Chunk 12 - rectangles format = '<H' numEntries = len(rules.rectangles) pack(rfile, numEntries, format) format = '8B' for rect in rules.rectangles: pack(rfile, ( rect.maxX, rect.minX, rect.maxY, rect.minY, rect.topLeftPosY, rect.topLeftPosX, rect.bottomRightPosY, rect.bottomRightPosX ), format ) # Chunk 13 - interface hotspots format = '20B' assert len(rules.interfaceTwoStepAction) == 20 pack(rfile, rules.interfaceTwoStepAction, format) format = '<20h' pack(rfile, rules.interfaceHotspotsX, format) pack(rfile, rules.interfaceHotspotsY, format) format = '20B' pack(rfile, rules.keyboardMapping, format)
def default(self, obj): if isinstance(obj, robinpacker.structs.rules.RulesData): result = OrderedDict() result['__json_type__'] = 'rules' # a bit gross, might need to put it in a wrapper dict result['__type__'] = 'RulesData' result['chunk1PointArray'] = obj.chunk1PointArray result['characters'] = obj.characters result['strings'] = obj.strings result['scripts'] = obj.scripts result['menuScripts'] = obj.menuScripts #result['gameScriptIndexes'] = obj.gameScriptIndexes result['gameScripts'] = obj.gameScripts result['rulesChunk9'] = obj.rulesChunk9 #result['chunk10Indexes'] = obj.chunk10Indexes result['rulesChunk11'] = obj.rulesChunk11 result['rectangles'] = obj.rectangles result['hotspots'] = OrderedDict() result['hotspots']['interfaceTwoStepAction'] = obj.interfaceTwoStepAction result['hotspots']['interfaceHotspotsX'] = obj.interfaceHotspotsX result['hotspots']['interfaceHotspotsY'] = obj.interfaceHotspotsY result['hotspots']['keyboardMapping'] = obj.keyboardMapping return result elif isinstance(obj, robinpacker.structs.point.PointData): result = OrderedDict() result['__type__'] = 'PointData' result['x'] = obj.x result['y'] = obj.y return result elif isinstance(obj, robinpacker.structs.character.CharacterData): result = OrderedDict() result['__type__'] = 'CharacterData' result['posX'] = obj.posX result['posY'] = obj.posY result['posAltitude'] = obj.posAltitude result['frameArray'] = obj.frameArray result['_rulesBuffer2_5'] = obj._rulesBuffer2_5 result['_rulesBuffer2_6'] = obj._rulesBuffer2_6 result['_rulesBuffer2_7'] = obj._rulesBuffer2_7 result['spriteSize'] = obj.spriteSize result['direction'] = obj.direction result['_rulesBuffer2_10'] = obj._rulesBuffer2_10 result['_rulesBuffer2_11'] = obj._rulesBuffer2_11 result['_rulesBuffer2_12'] = obj._rulesBuffer2_12 result['_rulesBuffer2_13_posX'] = obj._rulesBuffer2_13_posX result['_rulesBuffer2_14_posY'] = obj._rulesBuffer2_14_posY result['variables'] = obj.variables result['_rulesBuffer2_16'] = obj._rulesBuffer2_16 return result elif isinstance(obj, robinpacker.structs.rect.RectData): result = OrderedDict() result['__type__'] = 'RectData' result['maxX'] = obj.maxX result['minX'] = obj.minX result['maxY'] = obj.maxY result['minY'] = obj.minY result['topLeftPosY'] = obj.topLeftPosY result['topLeftPosX'] = obj.topLeftPosX result['bottomRightPosY'] = obj.bottomRightPosY result['bottomRightPosX'] = obj.bottomRightPosX return result elif isinstance(obj, robinpacker.structs.raw.RawData) or ( isinstance(obj, robinpacker.structs.script.ScriptData) and this.dump_scripts ): raw_fname = os.path.splitext(json_file_name)[0] + '_' + obj.id + '.dmp' logging.debug('Dumping raw data to {}'.format(raw_fname)) with file(raw_fname, 'wb') as raw_file: raw_file.write(obj.data) relative_fname = os.path.basename(raw_fname) result = OrderedDict() result['__type__'] = 'RawData' result['id'] = obj.id result['path'] = relative_fname return result elif isinstance(obj, robinpacker.structs.script.ScriptData): # Scripts are written to a "scripts" sub-directory, and each script's name is derived # from the JSON file's name and the script's ID property. dir_name, base_name = os.path.split(json_file_name) base_name = os.path.splitext(base_name)[0] dir_name = os.path.join(dir_name, 'scripts') mkdir(dir_name) script_fname = os.path.join(dir_name, '{}_{}.rrs'.format(base_name, obj.id)) logging.debug('Disassembling script data to {}'.format(script_fname)) script_exporter.export(obj, script_fname, string_table) relative_fname = os.path.basename(script_fname) result = OrderedDict() result['__type__'] = 'ScriptData' result['id'] = obj.id result['path'] = relative_fname return result return super(RulesJsonEncoder, self).default(obj)