Files
rosetta/tools/fixtures/docs_voxel.py
T

85 lines
4.1 KiB
Python

# Docs inputs for the voxel guide page (docs/guide/voxel.md): a level.dat-like gzip NBT file and a Sponge v3 .schem
# (nbtlib), a two-region .litematic (litemapy), a small Anvil region from tools/fixtures/mca.py and a Qubicle file
# from tools/fixtures/qb.py's writer.
# tests/gen/docs_voxel.luau { files = { [name] = bytes }, vars = { levelDatBytes = ..., ... } }
import importlib.util, os, pickle, subprocess
INPUTS = ["tools/docs_check.py", "tools/fixtures/mca.py", "tools/fixtures/qb.py"]
BODY = r'''
import nbtlib, pickle, sys, io, gzip, os, tempfile
from nbtlib import Byte, Short, Int, Long, Float, Double, String, List, Compound, IntArray, ByteArray
import numpy as np
from litemapy import Schematic, Region, BlockState
files = {}
level = Compound({"Data": Compound({"LevelName": String("My World"), "DataVersion": Int(3953), "Difficulty": Byte(2),
"RandomSeed": Long(-4011880356862312345), "SpawnX": Int(16), "SpawnY": Int(64), "SpawnZ": Int(-8),
"GameRules": Compound({"doDaylightCycle": String("true")}),
"Player": Compound({"Pos": List[Double]([Double(16.5), Double(64.0), Double(-7.5)]), "Health": Float(20.0)})})})
f = nbtlib.File(level, root_name="")
buf = io.BytesIO()
f.write(buf)
files["level.dat"] = gzip.compress(buf.getvalue(), mtime=0)
# a 5 x 4 x 3 Sponge v3 schematic: a stone floor, oak planks walls, one glass block
w, h, l = 5, 4, 3
states = ["minecraft:air", "minecraft:stone", "minecraft:oak_planks", "minecraft:glass"]
idx = []
for y in range(h):
for z in range(l):
for x in range(w):
idx.append(1 if y == 0 else (3 if (x, y, z) == (2, 2, 0) else (2 if x in (0, w - 1) else 0)))
s = Compound({"Version": Int(3), "DataVersion": Int(3953), "Width": Short(w), "Height": Short(h), "Length": Short(l),
"Offset": IntArray([0, 0, 0]), "Metadata": Compound({"Name": String("hut")}),
"Blocks": Compound({"Palette": Compound({st: Int(i) for i, st in enumerate(states)}),
"Data": ByteArray(idx), "BlockEntities": List[Compound]([])})})
buf = io.BytesIO()
nbtlib.File({"Schematic": s}, root_name="").write(buf)
files["hut.schem"] = gzip.compress(buf.getvalue(), mtime=0)
a = Region(0, 0, 0, 3, 2, 3)
for x in range(3):
for z in range(3):
a[x, 0, z] = BlockState("minecraft:cobblestone")
b = Region(5, 0, 0, 2, 3, 1)
for y in range(3):
b[0, y, 0] = BlockState("minecraft:oak_log", axis="y")
tmp = tempfile.mkdtemp()
p = os.path.join(tmp, "build.litematic")
Schematic(name="build", author="docs", regions={"floor": a, "pillar": b}).save(p)
files["build.litematic"] = open(p, "rb").read()
sys.stdout.buffer.write(pickle.dumps(files, protocol=4))
'''
def load(ctx, name):
spec = importlib.util.spec_from_file_location("docs_voxel_" + name, os.path.join(ctx.root, "tools", "fixtures", name + ".py"))
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
return m
def docs_check(ctx):
spec = importlib.util.spec_from_file_location("docs_check", os.path.join(ctx.root, "tools", "docs_check.py"))
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
return m
def generate(ctx):
try:
files = pickle.loads(ctx.python(["nbtlib", "litemapy", "numpy"], BODY))
except subprocess.CalledProcessError as e:
raise RuntimeError("fixture python failed:\n" + e.stderr.decode(errors="replace")[-3000:])
mca = load(ctx, "mca")
regions = pickle.loads(ctx.python(["nbtlib", "numpy", "lz4", "xxhash"], mca.BODY))["regions"]
files["r.0.0.mca"] = regions[0]["data"] # 1.18+ chunks, every compression type
qb = load(ctx, "qb")
cells = {(x, y, z): ((200, 40, 40, 255) if y == 0 else (240, 240, 240, 255)) for x in range(3) for y in range(2) for z in range(3)}
files["box.qb"] = qb.write_qb([("box", (3, 2, 3), (0, 0, 0), cells)], False, True, True, False)
docs_check(ctx).emit_inputs(ctx, files, {
"levelDatBytes": "level.dat",
"schemBytes": "hut.schem",
"litematicBytes": "build.litematic",
"regionBytes": "r.0.0.mca",
"qbBytes": "box.qb",
})