Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import struct
- import argparse
- from pathlib import Path
- SPEC = [
- {'key': 'version', 'type': str, 'width': 25},
- {'key': 'sceneWidth', 'type': int},
- {'key': 'sceneHeight', 'type': int},
- {'key': 'sheetWidth', 'type': int},
- {'key': 'sheetHeight', 'type': int},
- {'key': 'maxSprites', 'type': int},
- {'key': 'colorsExported', 'type': bool},
- {'key': 'colorOperationsCombined', 'type': bool},
- {'key': 'clips', 'type': list, 'child': [
- {'key': 'name', 'type': str, 'width': 25},
- {'key': 'startFrame', 'type': int},
- {'key': 'duration', 'type': int}
- ]},
- {'key': 'triggers', 'type': list, 'child': [
- {'key': 'frame', 'type': int},
- {'key': 'name', 'type': str, 'width': 25}
- ]},
- {'key': 'sprites', 'type': list, 'child': [
- {'key': 'id', 'type': int},
- {'key': 'name', 'type': str, 'width': 25},
- {'key': 'x', 'type': int},
- {'key': 'y', 'type': int},
- {'key': 'width', 'type': int},
- {'key': 'height', 'type': int},
- {'key': 'rotated', 'type': bool}
- ]},
- {'key': 'animationData', 'type': list, 'child': [
- [
- {'key': 'spriteID', 'type': int},
- {'key': 'color', 'type': list, 'child': [
- {'key': 'r', 'type': float},
- {'key': 'g', 'type': float},
- {'key': 'b', 'type': float},
- {'key': 'a', 'type': float}
- ]},
- {'key': 'colorAdd', 'type': list, 'child': [
- {'key': 'r', 'type': float},
- {'key': 'g', 'type': float},
- {'key': 'b', 'type': float},
- {'key': 'a', 'type': float}
- ]},
- {'key': 'scaleX', 'type': float},
- {'key': 'scaleY', 'type': float},
- {'key': 'skewX', 'type': float},
- {'key': 'skewY', 'type': float},
- {'key': 'positionX', 'type': float},
- {'key': 'positionY', 'type': float},
- {'key': 'positionZ', 'type': float}
- ]
- ]},
- {'key': 'trackObjects', 'type': list, 'child': [
- {'key': 'name', 'type': str, 'width': 25},
- {'key': 'transform', 'type': list, 'child': [
- {'key': 'frame', 'type': int},
- {'key': 'position', 'type': list, 'child': [
- {'key': 'x', 'type': float},
- {'key': 'y', 'type': float},
- {'key': 'z', 'type': float}
- ]},
- {'key': 'scale', 'type': list, 'child': [
- {'key': 'x', 'type': float},
- {'key': 'y', 'type': float}
- ]},
- {'key': 'rotation', 'type': float}
- ]}
- ]}
- ]
- SPEC_TYPES = {
- int: 'i',
- float: 'f',
- chr: 'c',
- str: '{0}s',
- bool: 'B'
- }
- CAST_TYPES = {
- int: int,
- float: float,
- chr: lambda x: bytes([x] * 1),
- str: lambda x: bytearray(x.encode('ascii')),
- bool: lambda x: 1 if x else 0
- }
- DEFAULT_VALS = {
- int: -1,
- float: 0.0,
- chr: b'\x00',
- str: '',
- bool: 0
- }
- def convert_to_bytes(json_file, spec):
- final_bytes = bytearray()
- for spec_val in spec:
- t = spec_val['type']
- k = spec_val['key']
- print (k)
- if t == list:
- length = len(json_file[k])
- # Write the length of the array to the bytes file
- final_bytes += struct.pack('i', length)
- for i in range(length):
- final_bytes += convert_to_bytes(json_file[k][i], spec_val['child'])
- elif t == str:
- val = CAST_TYPES[t](json_file[k]) if k in json_file else DEFAULT_VALS[t]
- final_bytes += bytearray(struct.pack(SPEC_TYPES[t].format(spec_val['width']), val))
- elif t in SPEC_TYPES:
- val = CAST_TYPES[t](json_file[k]) if k in json_file else DEFAULT_VALS[t]
- final_bytes += bytearray(struct.pack(SPEC_TYPES[t], val))
- return final_bytes
- def open_json(file_name):
- with open(file_name) as f:
- try:
- file = f.read()
- json_obj = json.loads(file)
- return json_obj
- except:
- pass
- return None
- def write_bytes(path, all_bytes):
- with path.open('wb') as f:
- f.write(bytearray(all_bytes))
- if __name__ == '__main__':
- parser = argparse.ArgumentParser(description="Converts a Unim JSON export to bytes")
- parser.add_argument('files', type=str, nargs='+', help="The JSON file to convert")
- args = parser.parse_args()
- json_files = args.files
- for json_file in json_files:
- unim_json = open_json(json_file)
- if unim_json:
- all_bytes = convert_to_bytes(unim_json, SPEC)
- path = Path(json_file)
- write_bytes(path.with_suffix('.bytes'), all_bytes)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement