summaryrefslogtreecommitdiff
path: root/scripts/gen_blocks
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/gen_blocks')
-rwxr-xr-xscripts/gen_blocks55
1 files changed, 55 insertions, 0 deletions
diff --git a/scripts/gen_blocks b/scripts/gen_blocks
new file mode 100755
index 0000000..08bc64a
--- /dev/null
+++ b/scripts/gen_blocks
@@ -0,0 +1,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("};")