# Tiled fixtures (tools/fixtures.py), with Tiled itself (nixpkgs#tiled, run headless) as the reference: # tests/gen/tiled.luau { cases = { { name, tmx (hand-written), tmj (Tiled's export), tiled_tmx (Tiled's re-save) } }, # tsx, tsj (an external tileset and Tiled's JSON export of it) } # tests/gen/tiled_ours.luau { checks = { { label, ok } } }: a map built and written by our encoder (TMX with # base64 + zlib data, and TMJ; tools/fixtures/_ours.luau) re-exported by Tiled as JSON and # compared with our own TMJ import base64, gzip, json, os, shutil, struct, subprocess, tempfile, zlib INPUTS = ["src/Formats/vector/tiled", "tools/fixtures/_ours.luau"] TSX = """ """ def layer_bytes(gids): return b"".join(struct.pack(" line one line two {csv} {z} {gz} {raw} Hello <Tiled> """ def iso(): W, H = 3, 3 tiles = "\n".join(' ' % ((x + y) % 4) for y in range(H) for x in range(W)) return f""" {tiles} """ def infinite(): def chunk(cx, cy): g = [(cx // 16 + cy // 16 + x + y) % 5 for y in range(16) for x in range(16)] rows = ",\n".join(",".join(str(v) for v in g[y * 16:(y + 1) * 16]) for y in range(16)) return f' \n{rows}\n' # Tiled's canonical layout: chunks by row, the layer's size = the chunks' bounds chunks = "\n".join(chunk(cx, cy) for cx, cy in ((16, -16), (-16, 0), (0, 0))) return f""" {chunks} """ def tiled(ctx, argv, cwd): env = dict(os.environ, QT_QPA_PLATFORM="offscreen") if shutil.which("tiled"): subprocess.run(["tiled"] + argv, check=True, capture_output=True, cwd=cwd, env=env) else: subprocess.run(["nix", "shell", "nixpkgs#tiled", "-c", "tiled"] + argv, check=True, capture_output=True, cwd=cwd, env=env) def magick(ctx, argv): try: subprocess.run(argv, check=True, capture_output=True) except (OSError, subprocess.CalledProcessError): ctx.nix("imagemagick", argv) def workdir(ctx, d): magick(ctx, ["magick", "-size", "64x32", "gradient:red-blue", os.path.join(d, "tiles.png")]) magick(ctx, ["magick", "-size", "40x20", "xc:skyblue", os.path.join(d, "sky.png")]) open(os.path.join(d, "ext.tsx"), "w").write(TSX) def run_ours(ctx, fmt): script = os.path.join(ctx.root, "tools", "fixtures", "_ours.luau") luau = shutil.which("luau") argv = [luau, script, "-a", fmt] if luau else ["nix", "shell", "nixpkgs#luau", "-c", "luau", script, "-a", fmt] return json.loads(subprocess.run(argv, check=True, capture_output=True, cwd=ctx.root).stdout) def gids(layer): """a JSON tile layer's gids (arrays, or base64 with compression) -> list""" d = layer.get("data") if isinstance(d, str): raw = base64.b64decode(d) c = layer.get("compression", "") raw = zlib.decompress(raw) if c == "zlib" else gzip.decompress(raw) if c == "gzip" else raw return list(struct.unpack("<%dI" % (len(raw) // 4), raw)) return d IGNORE = {"version", "tiledversion", "compressionlevel", "encoding", "compression", "opacity_obj"} DEFAULTS = {"offsetx": 0, "offsety": 0, "parallaxx": 1, "parallaxy": 1, "locked": False} def norm(v, obj=False): """a JSON map without the fields that only say which Tiled wrote it, or that hold defaults""" if isinstance(v, dict): out = {} if v.get("type") == "tilelayer" and "data" in v: v = dict(v, data=gids(v)) for k, x in v.items(): if k in IGNORE or (k in DEFAULTS and x == DEFAULTS[k]) or (obj and k == "opacity"): continue out[k] = [norm(o, True) for o in x] if k == "objects" else norm(x) return out if isinstance(v, list): return [norm(x) for x in v] if isinstance(v, float) and v.is_integer(): return int(v) return v def diff(a, b, path="", out=None): out = [] if out is None else out if isinstance(a, dict) and isinstance(b, dict): for k in sorted(set(a) | set(b)): if k not in a or k not in b: out.append("%s.%s: %s vs %s" % (path, k, a.get(k, ""), b.get(k, ""))) else: diff(a[k], b[k], path + "." + k, out) elif isinstance(a, list) and isinstance(b, list): if len(a) != len(b): out.append("%s: length %d vs %d" % (path, len(a), len(b))) for i, (x, y) in enumerate(zip(a, b)): diff(x, y, "%s[%d]" % (path, i), out) elif a != b: out.append("%s: %r vs %r" % (path, a, b)) return out def generate(ctx): cases = [] with tempfile.TemporaryDirectory() as d: workdir(ctx, d) for name, text in (("ortho", ortho()), ("hex", iso()), ("infinite", infinite())): open(os.path.join(d, name + ".tmx"), "w").write(text) tiled(ctx, ["--export-map", name + ".tmx", name + "-out.tmj"], d) tiled(ctx, ["--export-map", name + ".tmx", name + "-out.tmx"], d) cases.append({"name": name, "tmx": text, "tmj": open(os.path.join(d, name + "-out.tmj"), "rb").read(), "tiled_tmx": open(os.path.join(d, name + "-out.tmx"), "rb").read()}) tiled(ctx, ["--export-tileset", "ext.tsx", "ext.tsj"], d) tsj = open(os.path.join(d, "ext.tsj"), "rb").read() ctx.emit("tiled", {"cases": cases, "tsx": TSX, "tsj": tsj}) # our encoder's files, read by Tiled o = run_ours(ctx, "tiled") open(os.path.join(d, "ours.tmx"), "wb").write(bytes.fromhex(o["tmx"])) open(os.path.join(d, "ours.tmj"), "wb").write(bytes.fromhex(o["tmj"])) tiled(ctx, ["--export-map", "ours.tmx", "ours-from-tmx.tmj"], d) tiled(ctx, ["--export-map", "ours.tmj", "ours-from-tmj.tmj"], d) mine = norm(json.loads(bytes.fromhex(o["tmj"]))) checks = [] for label, f in (("Tiled on our TMX", "ours-from-tmx.tmj"), ("Tiled on our TMJ", "ours-from-tmj.tmj")): theirs = norm(json.load(open(os.path.join(d, f)))) dd = diff(mine, theirs) checks.append({"label": label + (": " + "; ".join(dd[:5]) if dd else ""), "ok": not dd}) ctx.emit("tiled_ours", {"checks": checks})