# Godot fixtures (tools/fixtures.py), with Godot 4 itself (nixpkgs#godot_4, headless) as the reference: # tools/fixtures/_godot_make.gd builds scenes in Godot and saves them with Godot's .tscn writer; # tools/fixtures/_godot_dump.gd loads scenes with Godot's loader and prints every node, mesh surface (the arrays # Godot decoded), material, camera and light as JSON. A hand-written format-2 .escn (the Blender exporter's layout: # "arrays" surfaces, SpatialMaterial, Godot 3 node types) and .tres resources are added. Then our writer's .tscn output # (tools/fixtures/_godot_encode.luau) is loaded and dumped by Godot. Resources are loaded by Godot # (tools/fixtures/_godot_tres.gd: every stored property as var_to_str text), ours too. # tests/gen/godot.luau { cases = { { name, data, files = { [rel] = bytes }, dump? } }, tres = { { name, data, godot } } } # tests/gen/godot_ours.luau { { name, data, dump } }; ours_*.tres entries carry godot (the resource dump) instead import importlib.util, json, os, shutil, struct, subprocess, tempfile, zlib INPUTS = ["src/Rosetta/model/godot", "src/Rosetta/data/tres", "src/Rosetta/codecs/text/GodotVariant.luau", "tools/fixtures/_godot_make.gd", "tools/fixtures/_godot_dump.gd", "tools/fixtures/_godot_tres.gd", "tools/fixtures/_godot_encode.luau", "tools/fixtures/_model_ref.py"] ESCN = """[gd_scene load_steps=4 format=2] [sub_resource id=1 type="SpatialMaterial"] resource_name = "Material" albedo_color = Color(0.8, 0.2, 0.1, 1.0) metallic = 0.5 roughness = 0.25 flags_transparent = false params_cull_mode = 2 [sub_resource id=2 type="ArrayMesh"] resource_name = "Cube" surfaces/0 = { \t"material":SubResource(1), \t"primitive":4, \t"arrays":[ \t\tVector3Array(1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0), \t\tVector3Array(0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0), \t\tFloatArray(1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0), \t\tnull, ; no Vertex Colors, \t\tVector2Array(1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0), \t\tnull, ; No UV2, \t\tnull, ; No Bones, \t\tnull, ; No Weights, \t\tIntArray(0, 2, 1, 0, 3, 2) \t], \t"morph_arrays":[] } [sub_resource id=3 type="ArrayMesh"] surfaces/0 = { \t"primitive":5, \t"arrays":[ \t\tVector3Array(0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0), \t\tnull, null, ColorArray(1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1), null, null, null, null, null \t], \t"morph_arrays":[] } [node type="Spatial" name="Scene"] [node name="Cube" type="MeshInstance" parent="."] mesh = SubResource(2) visible = true transform = Transform(1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0, 2.0, 3.0, 4.0) [node name="Strip" type="MeshInstance" parent="Cube"] mesh = SubResource(3) transform = Transform(2.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 1.0) [node name="Camera" type="Camera" parent="."] fov = 60.0 far = 250.0 [node name="Lamp" type="OmniLight" parent="."] light_color = Color(1, 1, 0.5, 1) light_energy = 3.0 omni_range = 9.0 transform = Transform(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 5, 0) """ TRES = [ ("material.tres", """[gd_resource type="StandardMaterial3D" load_steps=2 format=3 uid="uid://b1x2y3z4"] [ext_resource type="Texture2D" path="res://checker.png" id="1_ab"] [resource] resource_name = "Brick" albedo_color = Color(0.8, 0.4, 0.2, 1) albedo_texture = ExtResource("1_ab") metallic = 0.25 uv1_scale = Vector3(2, 2, 1) """), ("data.tres", """[gd_resource type="Resource" script_class="Stats" load_steps=3 format=3] [ext_resource type="Script" path="res://stats.gd" id="1"] [sub_resource type="Curve" id="Curve_1"] _data = [Vector2(0, 0), 0.0, 1.0, 0, 0, Vector2(1, 1), 1.0, 0.0, 0, 0] point_count = 2 [resource] script = ExtResource("1") name = "Goblin \\"Grr\\"\\nline two" hp = 12 speed = 3.5 tags = PackedStringArray("green", "small") weights = PackedFloat32Array(0.5, 0.25, -1e-05, inf) spawn = Vector3i(1, -2, 3) rect = Rect2(0, 0, 32, 16) curve = SubResource("Curve_1") drops = { "gold": 5, "items": [&"sword", NodePath("../Path"), null, true], 3: Color(1, 0, 0, 1) } typed = Array[int]([1, 2, 3]) basis = Basis(1, 0, 0, 0, 1, 0, 0, 0, 1) quat = Quaternion(0, 0, 0, 1) obj = Object(Resource,"resource_local_to_scene":false,"resource_name":"x") """), ] def png(w, h): def chunk(t, d): return struct.pack(">I", len(d)) + t + d + struct.pack(">I", zlib.crc32(t + d) & 0xffffffff) raw = b"".join(b"\0" + bytes(((x + y) % 2) * 255 for x in range(w) for _ in range(3)) for y in range(h)) return b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", w, h, 8, 2, 0, 0, 0)) + chunk(b"IDAT", zlib.compress(raw)) + chunk(b"IEND", b"") def godot(ctx, project, args): cmd = ["nix", "shell", "nixpkgs#godot_4", "-c", "godot", "--headless", "--path", project] + args return subprocess.run(cmd, capture_output=True, timeout=600).stdout.decode("utf-8", "replace") def dump(ctx, project, files): out = godot(ctx, project, ["-s", os.path.join(ctx.root, "tools", "fixtures", "_godot_dump.gd"), "--"] + ["res://" + f for f in files]) res = {} for line in out.split("\n"): if line.startswith("JSON:"): d = json.loads(line[5:]) res[d["file"][6:]] = d return res STATS_GD = """class_name Stats extends Resource @export var name: String @export var hp: int @export var speed: float @export var tags: PackedStringArray @export var weights: PackedFloat32Array @export var spawn: Vector3i @export var rect: Rect2 @export var curve: Curve @export var drops: Dictionary @export var typed: Array[int] @export var basis: Basis @export var quat: Quaternion @export var obj: Resource """ def tres_dump(ctx, project, files): out = godot(ctx, project, ["-s", os.path.join(ctx.root, "tools", "fixtures", "_godot_tres.gd"), "--"] + ["res://" + f for f in files]) res = {} for line in out.split("\n"): if line.startswith("JSON:"): d = json.loads(line[5:]) res[d["file"][6:]] = d return res def clean(v): # None cannot sit in a Luau table: drop null fields if isinstance(v, dict): return {k: clean(x) for k, x in v.items() if x is not None} if isinstance(v, list): return [clean(x) for x in v if x is not None] return v def generate(ctx): tmp = tempfile.mkdtemp(prefix="rosetta-godot-") try: with open(os.path.join(tmp, "project.godot"), "w") as f: f.write('config_version=5\n[application]\nconfig/name="rosetta"\n') checker = png(4, 4) with open(os.path.join(tmp, "checker.png"), "wb") as f: f.write(checker) godot(ctx, tmp, ["--import"]) godot(ctx, tmp, ["-s", os.path.join(ctx.root, "tools", "fixtures", "_godot_make.gd")]) made = ["part.tscn", "main.tscn"] dumps = dump(ctx, tmp, made) read = lambda f: open(os.path.join(tmp, f), "rb").read() cases = [] for f in made: cases.append({"name": f, "data": read(f), "files": {"part.tscn": read("part.tscn"), "checker.png": checker}, "dump": clean(dumps.get(f, {}))}) cases.append({"name": "hand.escn", "data": ESCN.encode(), "files": {}}) with open(os.path.join(tmp, "stats.gd"), "w") as f: f.write(STATS_GD) for n, t in TRES: with open(os.path.join(tmp, n), "w") as f: f.write(t) td = tres_dump(ctx, tmp, [n for n, _ in TRES]) tres = [{"name": n, "data": t.encode(), "godot": td.get(n, {"ok": False})} for n, t in TRES] ctx.emit("godot", {"cases": cases, "tres": tres}) spec = importlib.util.spec_from_file_location("_model_ref", os.path.join(ctx.root, "tools", "fixtures", "_model_ref.py")) M = importlib.util.module_from_spec(spec) spec.loader.exec_module(M) ours = M.luau_encode(ctx, "tools/fixtures/_godot_encode.luau") names = sorted(ours) for n in names: with open(os.path.join(tmp, n), "wb") as f: f.write(ours[n]) scenes = [n for n in names if n.endswith(".tscn")] od = dump(ctx, tmp, scenes) rd = tres_dump(ctx, tmp, [n for n in names if n.endswith(".tres")]) ctx.emit("godot_ours", [{"name": n, "data": ours[n], "dump": clean(od.get(n, {"ok": False}))} for n in scenes] + [{"name": n, "data": ours[n], "godot": rd.get(n, {"ok": False})} for n in names if n.endswith(".tres")]) finally: shutil.rmtree(tmp)