Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. import json
  2. import struct
  3. import argparse
  4. from pathlib import Path
  5.  
  6. SPEC = [
  7. {'key': 'version', 'type': str, 'width': 25},
  8. {'key': 'sceneWidth', 'type': int},
  9. {'key': 'sceneHeight', 'type': int},
  10. {'key': 'sheetWidth', 'type': int},
  11. {'key': 'sheetHeight', 'type': int},
  12. {'key': 'maxSprites', 'type': int},
  13. {'key': 'colorsExported', 'type': bool},
  14. {'key': 'colorOperationsCombined', 'type': bool},
  15. {'key': 'clips', 'type': list, 'child': [
  16. {'key': 'name', 'type': str, 'width': 25},
  17. {'key': 'startFrame', 'type': int},
  18. {'key': 'duration', 'type': int}
  19. ]},
  20. {'key': 'triggers', 'type': list, 'child': [
  21. {'key': 'frame', 'type': int},
  22. {'key': 'name', 'type': str, 'width': 25}
  23. ]},
  24. {'key': 'sprites', 'type': list, 'child': [
  25. {'key': 'id', 'type': int},
  26. {'key': 'name', 'type': str, 'width': 25},
  27. {'key': 'x', 'type': int},
  28. {'key': 'y', 'type': int},
  29. {'key': 'width', 'type': int},
  30. {'key': 'height', 'type': int},
  31. {'key': 'rotated', 'type': bool}
  32. ]},
  33. {'key': 'animationData', 'type': list, 'child': [
  34. [
  35. {'key': 'spriteID', 'type': int},
  36. {'key': 'color', 'type': list, 'child': [
  37. {'key': 'r', 'type': float},
  38. {'key': 'g', 'type': float},
  39. {'key': 'b', 'type': float},
  40. {'key': 'a', 'type': float}
  41. ]},
  42. {'key': 'colorAdd', 'type': list, 'child': [
  43. {'key': 'r', 'type': float},
  44. {'key': 'g', 'type': float},
  45. {'key': 'b', 'type': float},
  46. {'key': 'a', 'type': float}
  47. ]},
  48. {'key': 'scaleX', 'type': float},
  49. {'key': 'scaleY', 'type': float},
  50. {'key': 'skewX', 'type': float},
  51. {'key': 'skewY', 'type': float},
  52. {'key': 'positionX', 'type': float},
  53. {'key': 'positionY', 'type': float},
  54. {'key': 'positionZ', 'type': float}
  55. ]
  56. ]},
  57. {'key': 'trackObjects', 'type': list, 'child': [
  58. {'key': 'name', 'type': str, 'width': 25},
  59. {'key': 'transform', 'type': list, 'child': [
  60. {'key': 'frame', 'type': int},
  61. {'key': 'position', 'type': list, 'child': [
  62. {'key': 'x', 'type': float},
  63. {'key': 'y', 'type': float},
  64. {'key': 'z', 'type': float}
  65. ]},
  66. {'key': 'scale', 'type': list, 'child': [
  67. {'key': 'x', 'type': float},
  68. {'key': 'y', 'type': float}
  69. ]},
  70. {'key': 'rotation', 'type': float}
  71. ]}
  72. ]}
  73. ]
  74.  
  75. SPEC_TYPES = {
  76. int: 'i',
  77. float: 'f',
  78. chr: 'c',
  79. str: '{0}s',
  80. bool: 'B'
  81. }
  82.  
  83. CAST_TYPES = {
  84. int: int,
  85. float: float,
  86. chr: lambda x: bytes([x] * 1),
  87. str: lambda x: bytearray(x.encode('ascii')),
  88. bool: lambda x: 1 if x else 0
  89. }
  90.  
  91. DEFAULT_VALS = {
  92. int: -1,
  93. float: 0.0,
  94. chr: b'\x00',
  95. str: '',
  96. bool: 0
  97. }
  98.  
  99. def convert_to_bytes(json_file, spec):
  100. final_bytes = bytearray()
  101. for spec_val in spec:
  102. t = spec_val['type']
  103. k = spec_val['key']
  104. print (k)
  105.  
  106. if t == list:
  107. length = len(json_file[k])
  108.  
  109. # Write the length of the array to the bytes file
  110. final_bytes += struct.pack('i', length)
  111. for i in range(length):
  112. final_bytes += convert_to_bytes(json_file[k][i], spec_val['child'])
  113. elif t == str:
  114. val = CAST_TYPES[t](json_file[k]) if k in json_file else DEFAULT_VALS[t]
  115. final_bytes += bytearray(struct.pack(SPEC_TYPES[t].format(spec_val['width']), val))
  116. elif t in SPEC_TYPES:
  117. val = CAST_TYPES[t](json_file[k]) if k in json_file else DEFAULT_VALS[t]
  118. final_bytes += bytearray(struct.pack(SPEC_TYPES[t], val))
  119.  
  120. return final_bytes
  121.  
  122.  
  123. def open_json(file_name):
  124. with open(file_name) as f:
  125. try:
  126. file = f.read()
  127. json_obj = json.loads(file)
  128. return json_obj
  129. except:
  130. pass
  131.  
  132. return None
  133.  
  134.  
  135. def write_bytes(path, all_bytes):
  136. with path.open('wb') as f:
  137. f.write(bytearray(all_bytes))
  138.  
  139. if __name__ == '__main__':
  140. parser = argparse.ArgumentParser(description="Converts a Unim JSON export to bytes")
  141. parser.add_argument('files', type=str, nargs='+', help="The JSON file to convert")
  142.  
  143. args = parser.parse_args()
  144. json_files = args.files
  145.  
  146. for json_file in json_files:
  147. unim_json = open_json(json_file)
  148. if unim_json:
  149. all_bytes = convert_to_bytes(unim_json, SPEC)
  150.  
  151. path = Path(json_file)
  152. write_bytes(path.with_suffix('.bytes'), all_bytes)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement