-
Notifications
You must be signed in to change notification settings - Fork 0
/
json2flat.py
59 lines (49 loc) · 2.02 KB
/
json2flat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import json
import struct
out = bytearray()
kinds = [
{ "inJson": "trees", "name": "tree", "floats": 2, "fields": ['x', 'y'] },
{ "inJson": "circles", "name": "circle", "floats": 3, "fields": ['x', 'y', 'radius' ] },
]
with open('build/map.h', 'w') as f:
f.write("#include <arpa/inet.h>\n\n");
for kd in kinds:
f.write(f"typedef struct {{" + '\n')
for field in kd['fields']:
f.write(f" float {field};" + '\n')
f.write(f"}} MapData_{kd['name'].capitalize()};" + '\n')
f.write('typedef struct {\n')
for kd in kinds:
f.write(f" MapData_{kd['name'].capitalize()} *{kd['inJson']};" + '\n');
f.write(f" uint32_t n{kd['inJson']};" + '\n');
f.write('} MapData;\n\n')
f.write('static MapData parse_map_data(FILE *f) {\n');
f.write(' MapData md = {0};\n');
f.write(' uint32_t sec_len = 0;\n');
for kd in kinds:
data_type = 'MapData_' + kd['name'].capitalize()
f.write(f" if (fread(&sec_len, sizeof(uint32_t), 1, f) < 1)\n")
f.write(f' perror("couldn\'t get map data section length"), exit(1);\n')
f.write(f" sec_len = md.n{kd['inJson']} = ntohl(sec_len);\n")
f.write(f" if (fread(\n")
f.write(f" md.{kd['inJson']} = calloc(sizeof({data_type}), sec_len),\n")
f.write(f" sizeof({data_type}),\n"),
f.write(f" sec_len,\n")
f.write(f" f\n")
f.write(f' ) < sec_len) perror("less map data than section length"), exit(1);\n')
f.write(' return md;\n');
f.write('}');
with open("map.json", "r") as f:
data = json.loads(f.read())
for kd in kinds:
inJson = data[kd['inJson']]
out += struct.pack('!L', len(inJson))
for pl in inJson:
if type(pl) is dict and pl['pos']:
en = pl['pos'] + [pl['radius']]
else:
en = pl
out += struct.pack('f'*kd['floats'], *en)
with open('build/map.bytes', 'wb') as f:
f.write(out)
print("done!")