summaryrefslogtreecommitdiff
path: root/scripts/gen_blocks
blob: 08bc64a54fd79d132d2a42a3405e9b359c9d05a0 (plain)
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
#!/usr/bin/env python3
import json
import os
import sys


reportsdir = sys.argv[1]
with open(os.path.join(reportsdir, "blocks.json")) as f:
    blocks = json.load(f)

nstates = 0
for blkname, block in blocks.items():
    nstates += len(block["states"])
bstates = [None] * nstates

props = set()
valueses = set()

for blkname, block in blocks.items():
    for propname, values in blocks[blkname].get("properties", {}).items():
        props.add(propname)
        valueses.add(tuple(values))

    for state in block["states"]:
        bstates[state["id"]] = blkname

print("// generated by scripts/gen_blocks")
print()
print("fn register_blocks() void = {")

for propname in props:
    print(f"\tstatic const p_{propname} = {json.dumps(propname)};")

print()

for values in valueses:
    init = ", ".join(f"{json.dumps(v)}" for v in values)
    print(f"\tstatic const v_{'_'.join(values)} = [{init}];")

lastblk = None
for i, blkname in enumerate(bstates):
    if blkname == lastblk:
        continue

    print()

    print(f"\tconst blk = blocks_register({json.dumps(blkname)}); // {i}")
    props = list(blocks[blkname].get("properties", {}).items())
    props.sort(key=lambda p: p[0], reverse=True);
    for propname, values in props:
        print(f"\tblock_addprop(blk, p_{propname}, v_{'_'.join(values)});")

    lastblk = blkname

print("};")