merge ws/engines

This commit is contained in:
4DBug committed 2026-11-01 20:14:34 -06:00
commit 8bd15840d8
66 files changed
+31751 -1331

No files matched your search

+136
View File
@@ -0,0 +1,136 @@
-- archive/unitypackage tests: packages built with Python's tarfile against their asset tables (paths, guids, asset
-- and meta MD5s, folders); our writer's output read by Python's tarfile; api detection; errors; fuzz.
-- Fixtures: tools/fixtures/unitypackage.py.
local T = require("@tests/T")
local F = require("@rosetta")
local UnityPackage = require("@rosetta/archive/unitypackage")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Md5 = require("@rosetta/core/Md5")
local Fx = require("@tests/gen/unitypackage")
local t = T.suite("unitypackage")
local function decode(s: string | buffer, opts: any?): (any, any)
local b = if type(s) == "string" then buffer.fromstring(s) else s
return UnityPackage.decode(Bytes.reader(b), opts)
end
t:eq(UnityPackage.name, "unitypackage", "name")
t:eq(UnityPackage.category, "archive", "category")
t:check(UnityPackage.caps.decode and UnityPackage.caps.encode, "caps")
local function checkAssets(a: any, assets: { any }, label: string)
local entries = {}
for _, e in a:list() do
entries[e.name] = e
end
local n = 0
for _, want in assets do
local e = entries[want.path]
t:check(e, label .. ": " .. want.path .. " listed")
if e then
n += 1
t:eq(e.dir, want.dir, label .. ": " .. want.path .. " dir")
t:eq(e.guid, want.guid, label .. ": " .. want.path .. " guid")
if not want.dir then
local b = a:read(want.path)
t:eq(Md5.of(b), want.md5, label .. ": " .. want.path .. " data")
end
local m = a:read(want.path .. ".meta")
t:check(m and Md5.of(m) == want.metaMd5, label .. ": " .. want.path .. ".meta")
end
end
t:eq(#a:list(), n * 2, label .. ": an entry and a .meta per asset, nothing else")
end
local samples = {}
for _, c in Fx.cases do
local a, err = decode(c.data)
t:check(a, c.name .. ": decode: " .. tostring(err))
if a then
checkAssets(a, c.assets, c.name)
t:eq(a.meta.source.assets, #c.assets, c.name .. ": asset count")
local e
for _, x in a:list() do
if x.name == "Assets/Textures/brick.png" then
e = x
end
end
local pv: any = e and (e :: any).preview and (e :: any).preview()
t:check(pv and buffer.readstring(pv, 8, 7) == "preview", c.name .. ": preview.png")
-- round trip through our writer
local out, werr = UnityPackage.encode(a)
t:check(out, c.name .. ": encode: " .. tostring(werr))
local back = out and decode(out)
t:check(back, c.name .. ": re-decode")
if back then
checkAssets(back, c.assets, c.name .. " round trip")
end
end
t:eq(UnityPackage.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, c.name .. ": sniff")
t:eq(F.detect(c.data, { name = "pack.unitypackage" }), "unitypackage", c.name .. ": F.detect with the extension")
table.insert(samples, c.data)
end
-- Python's tarfile reads ours
for _, o in Fx.ours do
t:check(o.ok, o.name .. ": Python reads it: " .. tostring(o.error))
local want: any = nil
for _, c in Fx.cases do
if c.name .. ".unitypackage" == o.name then
want = c.assets
end
end
if want then
local got = {}
for _, x in o.assets do
got[x.path] = x
end
for _, w in want do
local g = got[w.path]
t:check(g and g.guid == w.guid and g.md5 == w.md5 and g.metaMd5 == w.metaMd5 and g.dir == w.dir, o.name .. ": " .. w.path)
end
t:eq(#o.assets, #want, o.name .. ": asset count")
elseif o.name == "scratch.unitypackage" then
local got = {}
for _, x in o.assets do
got[x.path] = x
end
t:check(got["Assets/Models"] and got["Assets/Models"].dir, "scratch: folder")
t:check(got["Assets/Models/cube.obj"] and not got["Assets/Models/cube.obj"].dir, "scratch: file")
t:eq(got["Assets/readme.txt"] and got["Assets/readme.txt"].guid, "0123456789abcdef0123456789abcdef", "scratch: guid from the .meta")
t:eq(#o.assets, 3, "scratch: assets")
end
end
-- from any archive (a tar) through F.save
do
local tar = F.format("tar").new()
tar:add("Assets/a.txt", "a")
tar:add("Assets/b/", "", { dir = true })
local out = assert(F.save(tar, nil, { format = "unitypackage" }))
local back = assert(decode(out))
local names = {}
for _, e in back:list() do
names[e.name] = e.dir
end
t:check(names["Assets/a.txt"] == false and names["Assets/a.txt.meta"] == false and names["Assets/b"] == true, "tar -> unitypackage")
local meta = buffer.tostring(back:read("Assets/b.meta"))
t:check(string.find(meta, "folderAsset: yes", 1, true) ~= nil, "folder meta")
end
do
local _, e = decode("\31\139\8\0\0\0\0\0\0\255garbage")
t:check(e ~= nil, "bad deflate data")
local _, e2 = decode(Fx.cases[1].data, { maxEntries = 2 })
t:check(e2 and e2.code == Err.LIMIT, "entry limit: " .. tostring(e2))
local _, e3 = UnityPackage.encode({} :: any)
t:check(e3 and e3.code == Err.ARG, "encode arg")
t:eq(UnityPackage.sniff(Bytes.reader(buffer.fromstring("hello"))), 0, "sniff text")
end
t:fuzz("decode", function(b: buffer)
return decode(b)
end, samples)
t:done()
+47
View File
@@ -0,0 +1,47 @@
-- codecs/text/GodotVariant tests: the Variant text syntax (strings and escapes, StringName / NodePath, arrays,
-- dictionaries with non-string keys, constructors, packed arrays incl. base64 bytes, typed arrays, Object), format
-- round trips, int / float keeping, section documents, limits, fuzz. Godot's own files: tests/model/godot.luau.
local T = require("@tests/T")
local GodotVariant = require("@rosetta/codecs/text/GodotVariant")
local t = T.suite("godotvariant")
local v = assert(GodotVariant.value("{\"a\": [1, 2.5, \"x\\ny\", &\"sn\", ^\"np\"], 3: Vector3(1, -2, 3e-05), \"b\": PackedByteArray(\"AAEC\")}"))
t:check(v.a[2] == 2.5 and v.a[3] == "x\ny" and v.a[4].type == "StringName" and v.a[5].type == "NodePath", "array values")
t:check(v[3].type == "Vector3" and v[3].args[3] == 3e-05, "number keys, constructors")
local b = GodotVariant.bytes(v.b)
t:check(b and buffer.len(b) == 3 and buffer.readu8(b, 2) == 2, "base64 PackedByteArray")
local again = assert(GodotVariant.value(GodotVariant.format(v)))
t:eq(GodotVariant.format(again), GodotVariant.format(v), "format round trip")
local typed = assert(GodotVariant.value("Array[int]([1, 2])"))
t:check(typed.type == "Array[int]" and #typed.args[1] == 2, "typed array")
t:eq(GodotVariant.format(typed), "Array[int]([1, 2])", "typed array format")
local _, e1 = GodotVariant.value("Vector3(1, 2")
t:check(e1 ~= nil, "unterminated constructor")
local deep = string.rep("[", 300) .. string.rep("]", 300)
local _, e2 = GodotVariant.value(deep)
t:check(e2 and string.find(e2, "limit", 1, true), "depth limit")
do
local doc = assert(GodotVariant.parse("[gd_scene format=3]\n\n; comment\n[node name=\"A\" type=\"Node3D\"]\nmetadata/x = 1.0\n\"quoted key\" = [1, 2.0]\n"))
t:eq(#doc.sections, 2, "sections")
local n = doc.sections[2]
t:check(n.attrs.name == "A" and n.props["metadata/x"] == 1 and n.props["quoted key"][2] == 2, "attributes and properties")
local text = GodotVariant.encode(doc)
t:check(string.find(text, "metadata/x = 1.0", 1, true) ~= nil and string.find(text, "[1, 2.0]", 1, true) ~= nil, "floats keep their .0")
local o = assert(GodotVariant.value("Object(Resource,\"a\":1,\"b\":\"x\")"))
t:check(o.type == "Object" and o.class == "Resource" and o.args[1].b == "x", "Object")
t:eq(GodotVariant.number(0.1), "0.1", "shortest float")
t:eq(GodotVariant.format("a\"b\\c"), "\"a\\\"b\\\\c\"", "string escapes")
local u = assert(GodotVariant.value("\"\\u00e9\\ud83d\\ude00\""))
t:eq(u, "\u{e9}\u{1f600}", "unicode escapes and surrogate pairs")
end
local samples = {
"[gd_scene format=3]\n[node name=\"A\" type=\"Node3D\"]\ntransform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)\n",
"[gd_resource type=\"X\" format=3]\n[resource]\nd = {\n\"k\": [1, &\"s\", NodePath(\"a\"), PackedFloat32Array(1, 2)]\n}\n",
}
t:fuzz("parse", function(b: buffer)
return GodotVariant.parse(buffer.tostring(b))
end, samples)
t:done()
+100
View File
@@ -0,0 +1,100 @@
-- data/tres tests: Godot text resources against Godot 4's loader (every stored property, var_to_str text parsed back
-- by codecs/text/GodotVariant), int / float keeping, our re-encoded files loaded by Godot with the same properties;
-- errors; fuzz. Fixtures: tools/fixtures/godot.py.
local T = require("@tests/T")
local F = require("@rosetta")
local Tres = require("@rosetta/data/tres")
local GodotVariant = require("@rosetta/codecs/text/GodotVariant")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Fx = require("@tests/gen/godot")
local Ours = require("@tests/gen/godot_ours")
local t = T.suite("tres")
local samples = {}
t:eq(Tres.name, "tres", "name")
t:eq(Tres.category, "data", "category")
t:check(Tres.caps.decode and Tres.caps.encode, "caps")
local function same(a: any, b: any): boolean
if type(a) == "number" and type(b) == "number" then
return a == b or math.abs(a - b) <= 1e-6 * math.max(1, math.abs(b)) or (a ~= a and b ~= b)
elseif type(a) == "table" and type(b) == "table" then
if GodotVariant.isCtor(a) ~= GodotVariant.isCtor(b) or (GodotVariant.isCtor(a) and a.type ~= b.type) then
return false
end
if GodotVariant.isCtor(a) and a.type == "Object" then
return a.class == b.class -- Godot lists every stored property of the object
end
local x, y = if GodotVariant.isCtor(a) then a.args else a, if GodotVariant.isCtor(b) then b.args else b
for k, v in x do
if not same(v, y[k]) then
return false
end
end
for k in y do
if x[k] == nil then
return false
end
end
return true
end
return a == b
end
for _, c in Fx.tres do
local v, err = Tres.decode(Bytes.reader(buffer.fromstring(c.data)))
t:check(v, c.name .. ": decode: " .. tostring(err))
t:eq(Tres.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, c.name .. ": sniff")
table.insert(samples, c.data)
local g = c.godot
t:check(g.ok, c.name .. ": Godot loads it")
if v and g.ok then
local n = 0
for k, text in g.props do
local mine = v.properties[k]
if mine ~= nil and not (GodotVariant.isCtor(mine) and (mine.type == "SubResource" or mine.type == "ExtResource")) then
local theirs, perr = GodotVariant.value(text)
t:check(perr == nil and same(mine, theirs), c.name .. ": " .. k .. " = Godot's " .. text)
n += 1
end
end
t:note("%s: %d properties against Godot", c.name, n)
local again = assert(Tres.decode(Bytes.reader(assert(Tres.encode(v)))))
t:eq(GodotVariant.format(again.properties), GodotVariant.format(v.properties), c.name .. ": round trip")
end
local ours
for _, o in Ours do
if o.name == "ours_" .. c.name then
ours = o
end
end
t:check(ours and ours.godot.ok, c.name .. ": Godot loads our .tres")
if ours and ours.godot.ok and g.ok then
for k, text in g.props do
t:eq(ours.godot.props[k], text, c.name .. ": our .tres in Godot: " .. k)
end
end
end
local data = assert(Tres.decode(Bytes.reader(buffer.fromstring(Fx.tres[2].data))))
t:check(data.type == "Resource" and data.scriptClass == "Stats" and data.properties.hp == 12, "tres header and values")
t:eq(data.subresources[1].properties.point_count, 2, "sub-resource properties")
t:eq(data.external[1].path, "res://stats.gd", "external resources")
local _, e = Tres.decode(Bytes.reader(buffer.fromstring(Fx.cases[1].data)))
t:check(e and e.code == Err.UNSUPPORTED, "a scene is not a resource")
t:eq(F.detect(Fx.tres[1].data), "tres", "F.detect tres")
do
local v = assert(Tres.decode(Bytes.reader(buffer.fromstring("[gd_resource type=\"Curve\" format=3]\n[resource]\n_data = [Vector2(0, 0), 0.0, 1.0, 0, 0]\nx = 2.0\n"))))
local s = buffer.tostring(assert(Tres.encode(v)))
t:check(string.find(s, "0.0, 1.0, 0, 0", 1, true) ~= nil and string.find(s, "x = 2.0", 1, true) ~= nil, "integral floats stay floats")
local _, e = Tres.decode(Bytes.reader(buffer.fromstring("[gd_resource type=\"X\" format=3]\n[resource]\nx = [1, 2\n")))
t:check(e ~= nil, "truncated array")
local _, e2 = Tres.encode({} :: any)
t:check(e2 and e2.code == Err.ARG, "encode arg")
end
t:fuzz("decode", function(b: buffer)
return Tres.decode(Bytes.reader(b))
end, samples, { mutations = 80, garbage = 20 })
t:done()
+58
View File
@@ -0,0 +1,58 @@
{
"ogmoVersion": "3.3.0",
"width": 640,
"height": 480,
"offsetX": 0,
"offsetY": 0,
"layers": [
{
"name": "entities",
"_eid": "40110095",
"offsetX": 0,
"offsetY": 0,
"gridCellWidth": 16,
"gridCellHeight": 16,
"gridCellsX": 40,
"gridCellsY": 30,
"entities": [
{"name": "player", "id": 0, "_eid": "40117707", "x": 128, "y": 144, "originX": 0, "originY": 0},
{"name": "coin", "id": 1, "_eid": "40304284", "x": 320, "y": 96, "originX": 0, "originY": 0},
{"name": "coin", "id": 2, "_eid": "40304284", "x": 352, "y": 80, "originX": 0, "originY": 0},
{"name": "coin", "id": 3, "_eid": "40304284", "x": 192, "y": 144, "originX": 0, "originY": 0},
{"name": "coin", "id": 4, "_eid": "40304284", "x": 224, "y": 144, "originX": 0, "originY": 0},
{"name": "coin", "id": 5, "_eid": "40304284", "x": 256, "y": 144, "originX": 0, "originY": 0},
{"name": "coin", "id": 6, "_eid": "40304284", "x": 288, "y": 144, "originX": 0, "originY": 0},
{"name": "coin", "id": 7, "_eid": "40304284", "x": 304, "y": 160, "originX": 0, "originY": 0},
{"name": "coin", "id": 8, "_eid": "40304284", "x": 304, "y": 192, "originX": 0, "originY": 0},
{"name": "coin", "id": 9, "_eid": "40304284", "x": 304, "y": 224, "originX": 0, "originY": 0},
{"name": "coin", "id": 10, "_eid": "40304284", "x": 304, "y": 256, "originX": 0, "originY": 0},
{"name": "coin", "id": 11, "_eid": "40304284", "x": 304, "y": 304, "originX": 0, "originY": 0},
{"name": "coin", "id": 12, "_eid": "40304284", "x": 320, "y": 288, "originX": 0, "originY": 0},
{"name": "coin", "id": 13, "_eid": "40304284", "x": 336, "y": 304, "originX": 0, "originY": 0},
{"name": "coin", "id": 14, "_eid": "40304284", "x": 288, "y": 288, "originX": 0, "originY": 0},
{"name": "coin", "id": 15, "_eid": "40304284", "x": 272, "y": 304, "originX": 0, "originY": 0},
{"name": "coin", "id": 16, "_eid": "40304284", "x": 256, "y": 224, "originX": 0, "originY": 0},
{"name": "coin", "id": 17, "_eid": "40304284", "x": 256, "y": 192, "originX": 0, "originY": 0},
{"name": "coin", "id": 18, "_eid": "40304284", "x": 208, "y": 192, "originX": 0, "originY": 0},
{"name": "coin", "id": 19, "_eid": "40304284", "x": 208, "y": 224, "originX": 0, "originY": 0},
{"name": "enemy", "id": 20, "_eid": "40444291", "x": 304, "y": 112, "originX": 0, "originY": 0},
{"name": "enemy", "id": 21, "_eid": "40444291", "x": 272, "y": 208, "originX": 0, "originY": 0},
{"name": "boss", "id": 22, "_eid": "40444462", "x": 304, "y": 288, "originX": 0, "originY": 0}
]
},
{
"name": "walls",
"_eid": "40116503",
"offsetX": 0,
"offsetY": 0,
"gridCellWidth": 16,
"gridCellHeight": 16,
"gridCellsX": 40,
"gridCellsY": 30,
"tileset": "tiles",
"data": [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 1, 1, 1, 1, 1, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 1, 1, 1, 1, 1, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, 2, 1, 1, 1, 1, 1, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 1, 1, 1, 1, 1, 2, -1, -1, -1, -1, -1, -1, 2, 1, 1, 1, 1, 1, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 1, 1, 1, 1, 1, 1, 1, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 1, 1, 1, 1, 1, 1, 1, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 1, 1, 1, 1, 1, 1, 1, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 1, 1, 1, 1, 1, 1, 1, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
"exportMode": 0,
"arrayMode": 0
}
]
}
+187
View File
@@ -0,0 +1,187 @@
{
"name": "HaxeFlixel Tutorial",
"ogmoVersion": "3.3.0",
"levelPaths": ["."],
"backgroundColor": "#282c34ff",
"gridColor": "#3c4049cc",
"anglesRadians": true,
"directoryDepth": 5,
"layerGridDefaultSize": {"x": 8, "y": 8},
"levelDefaultSize": {"x": 320, "y": 240},
"levelMinSize": {"x": 640, "y": 480},
"levelMaxSize": {"x": 640, "y": 480},
"levelValues": [],
"defaultExportMode": ".json",
"compactExport": false,
"externalScript": "",
"playCommand": "",
"entityTags": [],
"layers": [
{
"definition": "entity",
"name": "entities",
"gridSize": {"x": 16, "y": 16},
"exportID": "40110095",
"requiredTags": [],
"excludedTags": []
},
{
"definition": "tile",
"name": "walls",
"gridSize": {"x": 16, "y": 16},
"exportID": "40116503",
"exportMode": 0,
"arrayMode": 0,
"defaultTileset": "tiles"
}
],
"entities": [
{
"exportID": "40117707",
"name": "player",
"limit": -1,
"size": {"x": 16, "y": 16},
"origin": {"x": 0, "y": 0},
"originAnchored": true,
"shape": {
"label": "Rectangle",
"points": [
{"x": -1, "y": -1},
{"x": 1, "y": -1},
{"x": -1, "y": 1},
{"x": 1, "y": -1},
{"x": -1, "y": 1},
{"x": 1, "y": 1}
]
},
"color": "#ff0000ff",
"tileX": false,
"tileY": false,
"tileSize": {"x": 16, "y": 16},
"resizeableX": false,
"resizeableY": false,
"rotatable": false,
"rotationDegrees": 360,
"canFlipX": false,
"canFlipY": false,
"canSetColor": false,
"hasNodes": false,
"nodeLimit": 0,
"nodeDisplay": 0,
"nodeGhost": true,
"tags": [],
"values": []
},
{
"exportID": "40304284",
"name": "coin",
"limit": -1,
"size": {"x": 16, "y": 16},
"origin": {"x": 0, "y": 0},
"originAnchored": true,
"shape": {
"label": "Rectangle",
"points": [
{"x": -1, "y": -1},
{"x": 1, "y": -1},
{"x": -1, "y": 1},
{"x": 1, "y": -1},
{"x": -1, "y": 1},
{"x": 1, "y": 1}
]
},
"color": "#ffcc00ff",
"tileX": false,
"tileY": false,
"tileSize": {"x": 16, "y": 16},
"resizeableX": false,
"resizeableY": false,
"rotatable": false,
"rotationDegrees": 360,
"canFlipX": false,
"canFlipY": false,
"canSetColor": false,
"hasNodes": false,
"nodeLimit": 0,
"nodeDisplay": 0,
"nodeGhost": true,
"tags": [],
"values": []
},
{
"exportID": "40444291",
"name": "enemy",
"limit": -1,
"size": {"x": 16, "y": 16},
"origin": {"x": 0, "y": 0},
"originAnchored": true,
"shape": {
"label": "Rectangle",
"points": [
{"x": -1, "y": -1},
{"x": 1, "y": -1},
{"x": -1, "y": 1},
{"x": 1, "y": -1},
{"x": -1, "y": 1},
{"x": 1, "y": 1}
]
},
"color": "#0dbc0aff",
"tileX": false,
"tileY": false,
"tileSize": {"x": 16, "y": 16},
"resizeableX": false,
"resizeableY": false,
"rotatable": false,
"rotationDegrees": 360,
"canFlipX": false,
"canFlipY": false,
"canSetColor": false,
"hasNodes": false,
"nodeLimit": 0,
"nodeDisplay": 0,
"nodeGhost": true,
"tags": [],
"values": []
},
{
"exportID": "40444462",
"name": "boss",
"limit": -1,
"size": {"x": 16, "y": 16},
"origin": {"x": 0, "y": 0},
"originAnchored": true,
"shape": {
"label": "Rectangle",
"points": [
{"x": -1, "y": -1},
{"x": 1, "y": -1},
{"x": -1, "y": 1},
{"x": 1, "y": -1},
{"x": -1, "y": 1},
{"x": 1, "y": 1}
]
},
"color": "#00e3ffff",
"tileX": false,
"tileY": false,
"tileSize": {"x": 16, "y": 16},
"resizeableX": false,
"resizeableY": false,
"rotatable": false,
"rotationDegrees": 360,
"canFlipX": false,
"canFlipY": false,
"canSetColor": false,
"hasNodes": false,
"nodeLimit": 0,
"nodeDisplay": 0,
"nodeGhost": true,
"tags": [],
"values": []
}
],
"tilesets": [
{"label": "tiles", "path": "../images/tiles.png", "image": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAQCAYAAABQrvyxAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNiAoV2luZG93cykiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6ODRBQjNDODZENDhCMTFFMzk5N0RDMzAxQTc5NEM5RkMiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6ODRBQjNDODdENDhCMTFFMzk5N0RDMzAxQTc5NEM5RkMiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDo4NEFCM0M4NEQ0OEIxMUUzOTk3REMzMDFBNzk0QzlGQyIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDo4NEFCM0M4NUQ0OEIxMUUzOTk3REMzMDFBNzk0QzlGQyIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/PrOLMN8AAACUSURBVHja7JWhDoAgEEBPRrERbVYCX+If8Z8Eqo2IyYi7wOYYTncYuI3XD94bDKaUErSwWZtWY0izu3NAnc3zEhpBAVyIQvAeWuKPEEAAQ7I8IjjLswso5RHZq2iNUp7lFRoBI2AEQN+vEL5A+EM/MSvV7wm8ySNnjH0GfJGvRQhu8mWE/GPz2g/5hUVrkvw94hJgALO2Vr1s43dIAAAAAElFTkSuQmCC", "tileWidth": 16, "tileHeight": 16, "tileSeparationX": 0, "tileSeparationY": 0}
]
}
+256
View File
@@ -0,0 +1,256 @@
-- model/ase tests: assimp's ASE models and a hand-written file against assimp (meshes, faces, corners, cameras, lights,
-- world bounds); the hand file's values; round trips; assimp reading our output. Fixtures: tools/fixtures/ase.py.
local T = require("@tests/T")
local F = require("@rosetta")
local Ase = require("@rosetta/model/ase")
local MeshOps = require("@rosetta/ops/MeshOps")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Cases = require("@tests/gen/ase_cases")
local Xcheck = require("@tests/gen/ase_xcheck")
local t = T.suite("ase")
local S = Types.Scene
local function decode(data: any, opts: any?): (any, any)
local b = if type(data) == "string" then buffer.fromstring(data) else data
return Ase.decode(Bytes.reader(b), opts)
end
t:eq(Ase.name, "ase", "name")
t:eq(Ase.category, "model", "category")
t:check(Ase.caps.decode and Ase.caps.encode, "contract")
local function near(a: number, b: number, eps: number): boolean
return math.abs(a - b) <= eps * math.max(1, math.abs(a), math.abs(b))
end
local function counts(scene: any): (number, number, number)
local prims, faces, corners = 0, 0, 0
for _, pl in S.flatten(scene) do
prims += 1
faces += pl.primitive.indices.count // 3
corners += pl.primitive.indices.count
end
return prims, faces, corners
end
-- assimp's ASE root rotates Z-up to Y-up: (x, z, -y)
local function boundsMatch(lo: { number }, hi: { number }, want: any, eps: number): boolean
local a, b = { lo[1], lo[3], -hi[2] }, { hi[1], hi[3], -lo[2] }
for k = 1, 3 do
if not near(a[k], want.min[k], eps) or not near(b[k], want.max[k], eps) then
return false
end
end
return true
end
local function bstr(lo: any, hi: any): string
if not lo then
return "-"
end
return string.format("(%.4g %.4g %.4g) (%.4g %.4g %.4g)", lo[1], lo[2], lo[3], hi[1], hi[2], hi[3])
end
-- assimp mis-reads the UTF-16 copies of ThreeCubesGreen (one mesh of 8 faces, a bone): they are compared with the
-- UTF-8 file instead
local UTF16 = { ["assimp/models/ThreeCubesGreen_UTF16BE.ASE"] = true, ["assimp/models/ThreeCubesGreen_UTF16LE.ASE"] = true }
local scenes: { [number]: any } = {}
local byName: { [string]: any } = {}
local samples = {}
for i, c in Cases do
local scene, err = decode(c.data)
t:check(scene, c.name .. ": decode: " .. tostring(err))
t:eq(Ase.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, c.name .. ": sniff")
scenes[i] = scene
byName[c.name] = scene
if #c.data < 60000 then
table.insert(samples, c.data)
end
local a = c.assimp
if scene and not a.failed and not UTF16[c.name] then
local prims, faces, corners = counts(scene)
t:eq(prims, a.meshes, c.name .. ": meshes (assimp)")
t:eq(faces, a.faces, c.name .. ": faces (assimp)")
t:eq(corners, a.vertices, c.name .. ": corners (assimp)")
t:eq(#scene.cameras, a.cameras, c.name .. ": cameras (assimp)")
t:eq(#scene.lights, a.lights, c.name .. ": lights (assimp)")
if a.min then
local lo, hi = MeshOps.bounds(MeshOps.merge(scene))
t:check(lo and boundsMatch(lo, hi, a, 1e-3), c.name .. ": world bounds (assimp) " .. bstr(lo, hi) .. " want " .. bstr(a.min, a.max))
end
end
end
do
local u8 = byName["assimp/models/ThreeCubesGreen.ASE"]
for name in UTF16 do
local s = byName[name]
if s and u8 then
local p1, f1 = counts(s)
local p2, f2 = counts(u8)
local lo1, hi1 = MeshOps.bounds(MeshOps.merge(s))
local lo2, hi2 = MeshOps.bounds(MeshOps.merge(u8))
t:check(p1 == p2 and f1 == f2 and bstr(lo1, hi1) == bstr(lo2, hi2), name .. ": same scene as the UTF-8 file")
end
end
end
-- the hand-written file
do
local s = byName["hand.ase"]
t:eq(s.meta.up, "z", "hand: up")
t:eq(s.meta.source.frameSpeed, 25, "hand: frame speed")
t:eq(#s.materials, 2, "hand: sub-materials")
local brick, glass = s.materials[1], s.materials[2]
t:eq(brick.name, "Brick", "hand: material name")
t:check(near(brick.baseColor[1], 0.8, 1e-6) and near(brick.baseColor[3], 0.2, 1e-6), "hand: diffuse")
t:eq(s.textures[brick.baseColorTexture.texture].uri, "textures\\brick.png", "hand: bitmap path")
t:check(brick.baseColorTexture.transform and brick.baseColorTexture.transform.scale[2] == 3, "hand: UVW tiling")
t:check(glass.alphaMode == "blend" and near(glass.baseColor[4], 0.5, 1e-6) and glass.doubleSided, "hand: transparency, two-sided")
local quad
for _, n in s.nodes do
if n.name == "Quad" then
quad = n
end
end
local pivot = s.nodes[s.roots[1]]
t:eq(pivot.name, "Pivot", "hand: helper root")
t:check(table.find(pivot.children, table.find(s.nodes, quad)) ~= nil, "hand: NODE_PARENT")
-- local frame: Quad's world TM rotates 90 degrees about z at (10, 5, 0); relative to Pivot at (10, 0, 0)
t:check(near(quad.matrix[13], 0, 1e-6) and near(quad.matrix[14], 5, 1e-6) and near(quad.matrix[2], 1, 1e-6), "hand: matrix relative to the parent")
local mesh = s.meshes[quad.mesh]
t:eq(#mesh.primitives, 2, "hand: one primitive per sub-material")
local p1 = mesh.primitives[1]
t:eq(p1.material, 1, "hand: MESH_MTLID 0")
local P = S.reader(p1.attributes.POSITION) :: any
-- vertex 1 (10, 7, 0) world -> local: inverse of (rot z 90, t (10, 5, 0)) = (2, 0, 0)
t:check(near(P(1, 0), 2, 1e-6) and near(P(1, 1), 0, 1e-6), "hand: vertices in the node frame")
local UV = S.reader(p1.attributes.TEXCOORD_0) :: any
t:check(near(UV(1, 0), 1, 1e-6) and near(UV(1, 1), 1, 1e-6) and near(UV(2, 1), 0, 1e-6), "hand: UVs (v flipped)")
local UV2 = S.reader(p1.attributes.TEXCOORD_1) :: any
t:check(near(UV2(0, 0), 0.25, 1e-6) and near(UV2(0, 1), 0.25, 1e-6), "hand: mapping channel 2")
local C = S.reader(p1.attributes.COLOR_0) :: any
t:check(C(0, 0) == 1 and C(1, 1) == 1 and C(2, 0) == 1, "hand: vertex colours")
local N = S.reader(p1.attributes.NORMAL) :: any
-- (0, 0.6, 0.8) world under the inverse rotation: (0.6, 0, 0.8)
t:check(near(N(2, 0), 0.6, 1e-6) and near(N(2, 2), 0.8, 1e-6), "hand: explicit vertex normals")
t:eq(s.cameras[1].zfar, 500, "hand: camera far")
t:check(s.lights[1].type == "spot" and s.lights[1].intensity == 2 and near(s.lights[1].outerCone, math.rad(30), 1e-9), "hand: spot light")
end
-- round trips: ours -> ours keeps counts, bounds, materials, UVs
local function sameScene(a: any, b: any, label: string)
local p1, f1, c1 = counts(a)
local p2, f2, c2 = counts(b)
t:check(p1 == p2 and f1 == f2 and c1 == c2, label .. ": counts " .. p1 .. "/" .. f1 .. " vs " .. p2 .. "/" .. f2)
local lo1, hi1 = MeshOps.bounds(MeshOps.merge(a))
local lo2, hi2 = MeshOps.bounds(MeshOps.merge(b))
if lo1 and lo2 then
local ok = true
for k = 1, 3 do
ok = ok and near(lo1[k], lo2[k], 1e-5) and near(hi1[k], hi2[k], 1e-5)
end
t:check(ok, label .. ": bounds " .. bstr(lo1, hi1) .. " vs " .. bstr(lo2, hi2))
end
t:eq(#b.cameras, #a.cameras, label .. ": cameras")
t:eq(#b.lights, #a.lights, label .. ": lights")
end
for i, c in Cases do
local s = scenes[i]
if s then
local out, err = Ase.encode(s)
t:check(out, c.name .. ": encode: " .. tostring(err))
if out then
local back, berr = decode(out)
t:check(back, c.name .. ": re-decode: " .. tostring(berr))
if back then
sameScene(s, back, c.name .. ": round trip")
end
end
end
end
do
local s = byName["hand.ase"]
local back = decode(assert(Ase.encode(s)))
local m = back.materials[1]
t:eq(m.name, "Brick", "hand round trip: material")
t:eq(back.textures[m.baseColorTexture.texture].uri, "textures\\brick.png", "hand round trip: bitmap")
local q
for _, n in back.nodes do
if n.name == "Quad" then
q = n
end
end
local p = back.meshes[q.mesh].primitives[1]
t:check(p.attributes.TEXCOORD_1 and p.attributes.COLOR_0 and p.attributes.NORMAL, "hand round trip: attributes")
end
-- glTF -> ASE (any Scene): a mesh with no materials, an n-gon, a strip
do
local s = S.new()
local P = S.accessor(nil, "f32", 3, 5)
for i, v in { { 0, 0, 0 }, { 1, 0, 0 }, { 1, 1, 0 }, { 0.5, 1.5, 0 }, { 0, 1, 0 } } do
for k = 1, 3 do
buffer.writef32(P.data, ((i - 1) * 3 + k - 1) * 4, v[k])
end
end
local faces = S.accessor(nil, "u32", 1, 1)
buffer.writeu32(faces.data, 0, 5)
s.meshes[1] = { primitives = { { attributes = { POSITION = P }, mode = "polygons", faces = faces } } }
s.meshes[2] = { primitives = { { attributes = { POSITION = P }, mode = "triangle_strip" } } }
s.nodes = { { name = "ngon", children = { 2 }, mesh = 1, translation = { 0, 0, 3 } }, { name = "ngon", children = {}, mesh = 2, scale = { 2, 2, 2 } } }
s.roots = { 1 }
local out = assert(Ase.encode(s))
local back = assert(decode(out))
local _, faces2 = counts(back)
t:eq(faces2, 6, "n-gon (3) + strip (3) as triangles")
t:eq(back.nodes[2].name, "ngon_2", "duplicate names made unique")
local lo, hi = MeshOps.bounds(MeshOps.merge(back))
t:check(near(lo[3], 3, 1e-6) and near(hi[1], 2, 1e-6), "child world transform " .. bstr(lo, hi))
local via = F.save(s, nil, { format = "ase" })
t:check(type(via) == "buffer" and F.detect(via) == "ase", "F.save / F.detect")
end
-- assimp reads what we write
for _, x in Xcheck do
local c = Cases[x.case]
local s = scenes[x.case]
local a = x.assimp
t:check(not a.failed, x.name .. " (" .. c.name .. "): assimp reads it")
if s and not a.failed then
local prims, faces = counts(s)
t:eq(a.faces, faces, c.name .. ": assimp faces of ours")
t:eq(a.meshes, prims, c.name .. ": assimp meshes of ours")
t:eq(a.cameras, #s.cameras, c.name .. ": assimp cameras of ours")
t:eq(a.lights, #s.lights, c.name .. ": assimp lights of ours")
if a.min then
local lo, hi = MeshOps.bounds(MeshOps.merge(s))
t:check(lo and boundsMatch(lo, hi, a, 1e-3), c.name .. ": assimp bounds of ours " .. bstr(lo, hi) .. " vs " .. bstr(a.min, a.max))
end
end
end
-- errors
do
local _, e = decode("*3DSMAX_ASCIIEXPORT 200\n*GEOMOBJECT { *MESH { *MESH_NUMVERTEX 1 *MESH_NUMFACES 1 *MESH_FACE_LIST { *MESH_FACE 0: A: 0 B: 5 C: 0 } } }")
t:check(e and e.code == Err.CORRUPT, "face index out of range: " .. tostring(e))
local _, e2 = decode("*3DSMAX_ASCIIEXPORT 200\n}")
t:check(e2 and e2.code == Err.CORRUPT, "unbalanced brace")
local _, e3 = decode("hello")
t:check(e3 and e3.code == Err.CORRUPT, "not ASE")
local _, e4 = decode(Cases[1].data, { maxItems = 10 })
t:check(e4 and e4.code == Err.LIMIT, "item limit")
local _, e5 = Ase.encode({} :: any)
t:check(e5 and e5.code == Err.ARG, "encode arg")
end
t:fuzz("decode", function(b: buffer)
return Ase.decode(Bytes.reader(b))
end, samples)
t:done()
+356
View File
@@ -0,0 +1,356 @@
-- model/godot tests: scenes saved by Godot 4 against Godot's own loader (node transforms, surface arrays, materials,
-- cameras, lights, primitive meshes, instanced scenes, 2D nodes); a format-2 .escn; our .tscn writer's output loaded by
-- Godot and compared with the original; codecs/text/GodotVariant values; errors; fuzz. Fixtures: tools/fixtures/godot.py.
local T = require("@tests/T")
local F = require("@rosetta")
local Godot = require("@rosetta/model/godot")
local GodotVariant = require("@rosetta/codecs/text/GodotVariant")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Fx = require("@tests/gen/godot")
local Ours = require("@tests/gen/godot_ours")
local t = T.suite("godot")
local S = Types.Scene
local function decode(s: string | buffer, files: any?, opts: any?): (any, any)
local b = if type(s) == "string" then buffer.fromstring(s) else s
local o = opts or {}
o.resolve = function(p: string): buffer?
local f = files and files[p]
return if f then buffer.fromstring(f) else nil
end
return Godot.decode(Bytes.reader(b), o)
end
t:eq(Godot.name, "godot", "name")
t:eq(Godot.category, "model", "category")
t:check(Godot.caps.decode and Godot.caps.encode, "caps")
local function list(acc: any): { number }
if not acc then
return {}
end
local rd = S.reader(acc) :: any
local out = {}
for i = 0, acc.count - 1 do
for c = 0, acc.components - 1 do
table.insert(out, rd(i, c))
end
end
return out
end
-- our Scene in the dump's shape (tools/fixtures/_godot_dump.gd): paths, local transforms as basis rows + origin,
-- surface arrays with Godot's clockwise indices
local function asDump(scene: any): { [string]: any }
local out = {}
local function visit(i: number, path: string)
local n = scene.nodes[i]
local g = n.extras and n.extras.godot or {}
local e: any = { path = path, class = g.type }
local m = S.nodeMatrix(n)
e.xf = { m[1], m[5], m[9], m[2], m[6], m[10], m[3], m[7], m[11], m[13], m[14], m[15] }
e.visible = g.visible ~= false
if n.mesh then
local surfs = {}
for _, p in scene.meshes[n.mesh].primitives do
local idx = list(p.indices)
for k = 1, #idx - 2, 3 do
idx[k + 1], idx[k + 2] = idx[k + 2], idx[k + 1]
end
local mat = p.material and scene.materials[p.material]
local md = nil
if mat then
local x = mat.extras.godot
local tex = mat.baseColorTexture and scene.textures[mat.baseColorTexture.texture]
md = {
albedo = x.albedo,
metallic = mat.metallic,
roughness = mat.roughness,
emission = if x.emission then { x.emission[1], x.emission[2], x.emission[3] } else nil,
texture = if tex then "res://" .. tex.uri else nil,
transparency = if mat.alphaMode == "blend" then 1 else 0,
cull = if mat.doubleSided then 2 else 0,
}
end
local colors = {}
for k, v in list(p.attributes.COLOR_0) do
colors[k] = v
end
table.insert(surfs, {
vertex = list(p.attributes.POSITION),
normal = list(p.attributes.NORMAL),
tangent = list(p.attributes.TANGENT),
uv = list(p.attributes.TEXCOORD_0),
uv2 = list(p.attributes.TEXCOORD_1),
color = colors,
index = idx,
material = md,
})
end
e.surfaces = surfs
end
if n.camera then
local c = scene.cameras[n.camera]
e.camera = if c.type == "orthographic" then { projection = 1, size = c.ymag * 2, near = c.znear, far = c.zfar } else { projection = 0, fov = math.deg(c.yfov), near = c.znear, far = c.zfar }
end
if n.light then
local l = scene.lights[n.light]
local raw = g.lightColor or { 1, 1, 1 }
e.light = { color = { raw[1], raw[2], raw[3] }, energy = l.intensity, range = l.range, angle = if l.outerCone then math.deg(l.outerCone) else nil }
end
out[path] = e
for _, c in n.children do
local cn = scene.nodes[c].name
visit(c, if path == "." then cn else path .. "/" .. cn)
end
end
for _, r in scene.roots do
visit(r, ".")
end
return out
end
local function close(a: { number }?, b: { number }?, eps: number): (boolean, string)
a, b = a or {}, b or {}
if #a ~= #b then
return false, "length " .. #a .. " vs " .. #b
end
for i = 1, #a do
if math.abs(a[i] - b[i]) > eps * math.max(1, math.abs(b[i])) then
return false, "[" .. i .. "] " .. a[i] .. " vs " .. b[i]
end
end
return true, ""
end
-- the 3D facts of a dump node `got` against the reference `want`
local function compare(got: any, want: any, label: string, eps: number)
if want.xf then
local ok, why = close(got.xf, want.xf, 1e-5)
t:check(ok, label .. ": transform " .. why)
t:eq(got.visible, want.visible, label .. ": visible")
end
if want.surfaces then
t:eq(#(got.surfaces or {}), #want.surfaces, label .. ": surfaces")
for k, ws in want.surfaces do
local gs = (got.surfaces or {})[k]
if gs then
for _, f in { "vertex", "uv", "uv2" } do
local ok, why = close(gs[f], ws[f], 1e-5)
t:check(ok, label .. ": surface " .. k .. " " .. f .. " " .. why)
end
for _, f in { "normal", "tangent" } do
local ok, why = close(gs[f], ws[f], eps)
t:check(ok, label .. ": surface " .. k .. " " .. f .. " " .. why)
end
local ok, why = close(gs.color, ws.color, 1 / 255 + 1e-6)
t:check(ok, label .. ": surface " .. k .. " color " .. why)
local wi = ws.index
if #wi == 0 then
for i = 0, #ws.vertex // 3 - 1 do
wi[i + 1] = i
end
end
local gi = gs.index
if #gi == 0 then
for i = 0, #gs.vertex // 3 - 1 do
gi[i + 1] = i
end
end
local iok, iwhy = close(gi, wi, 0)
t:check(iok, label .. ": surface " .. k .. " index " .. iwhy)
local wm, gm = ws.material, gs.material
if wm then
t:check(gm ~= nil, label .. ": surface " .. k .. " material")
if gm then
local aok, awhy = close(gm.albedo, wm.albedo, 1e-5)
t:check(aok, label .. ": albedo " .. awhy)
t:check(math.abs(gm.metallic - wm.metallic) < 1e-5 and math.abs(gm.roughness - wm.roughness) < 1e-5, label .. ": metallic / roughness")
local eok, ewhy = close(gm.emission, wm.emission, 1e-5)
t:check(eok, label .. ": emission " .. ewhy)
t:eq(gm.texture, wm.texture, label .. ": albedo texture")
t:eq(gm.transparency, wm.transparency, label .. ": transparency")
t:eq(gm.cull, wm.cull, label .. ": cull")
end
end
end
end
end
if want.camera then
local gc, wc = got.camera or {}, want.camera
t:eq(gc.projection, wc.projection, label .. ": projection")
if wc.projection == 1 then
t:check(math.abs(gc.size - wc.size) < 1e-5, label .. ": ortho size")
else
t:check(math.abs(gc.fov - wc.fov) < 1e-5, label .. ": fov")
end
t:check(math.abs(gc.near - wc.near) < 1e-6 and math.abs(gc.far - wc.far) < 1e-3, label .. ": near / far")
end
if want.light then
local gl, wl = got.light or {}, want.light
local ok, why = close(gl.color, wl.color, 1e-5)
t:check(ok, label .. ": light colour " .. why)
t:check(math.abs((gl.energy or -1) - wl.energy) < 1e-5, label .. ": light energy")
if wl.range then
t:check(math.abs((gl.range or -1) - wl.range) < 1e-5, label .. ": light range")
end
if wl.angle then
t:check(math.abs((gl.angle or -1) - wl.angle) < 1e-4, label .. ": spot angle")
end
end
end
local scenes = {}
local samples = {}
for _, c in Fx.cases do
local scene, err = decode(c.data, c.files)
t:check(scene, c.name .. ": decode: " .. tostring(err))
t:eq(Godot.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, c.name .. ": sniff")
scenes[c.name] = scene
table.insert(samples, c.data)
if scene and c.dump then
t:check(c.dump.ok, c.name .. ": Godot loads it")
local mine = asDump(scene)
local n = 0
for _, want in c.dump.nodes do
local got = mine[want.path]
t:check(got, c.name .. ": node " .. want.path)
if got then
t:eq(got.class, want.class, c.name .. ": " .. want.path .. " class")
compare(got, want, c.name .. ": " .. want.path, 3e-4)
n += 1
end
end
t:note("%s: %d nodes against Godot", c.name, n)
end
end
-- Godot reads what we write
for _, o in Ours do
if not o.dump then
continue
end
t:check(o.dump.ok, o.name .. ": Godot loads our .tscn")
local base = string.match(o.name, "^ours_(.*)%.tscn$")
local ref
for _, c in Fx.cases do
if string.gsub(c.name, "%.%w+$", "") == base then
ref = c
end
end
if ref and ref.dump and o.dump.ok then
local byPath = {}
for _, nd in o.dump.nodes do
byPath[nd.path] = nd
end
local n = 0
for _, want in ref.dump.nodes do
if want.xf then
local got = byPath[want.path]
t:check(got, o.name .. ": node " .. want.path)
if got then
compare(got, want, o.name .. ": " .. want.path, 5e-4)
n += 1
end
end
end
t:note("%s: %d 3D nodes after our writer", o.name, n)
elseif o.dump.ok and base == "hand" then
-- the escn has no Godot reference: compare Godot's reading of ours with our reading of the escn
local mine = asDump(scenes["hand.escn"])
for _, got in o.dump.nodes do
local want = mine[got.path]
t:check(want, o.name .. ": node " .. got.path)
if want and want.xf then
compare(got, want, o.name .. ": " .. got.path, 5e-4)
end
end
end
end
-- details
do
local s = scenes["main.tscn"]
t:eq(s.meta.source.version, 4, "header format")
t:eq(s.meta.up, "y", "up axis")
local shape
for _, n in s.nodes do
if n.name == "Shape" then
shape = n
end
end
local m = s.materials[s.meshes[shape.mesh].primitives[1].material]
t:eq(m.name, "Painted", "material name")
t:check(math.abs(m.baseColor[1] - ((0.8 + 0.055) / 1.055) ^ 2.4) < 1e-6, "albedo sRGB -> linear")
t:eq(m.extensions.KHR_materials_emissive_strength.emissiveStrength, 2, "emission energy")
t:eq(s.textures[m.baseColorTexture.texture].uri, "checker.png", "texture path")
t:check(s.textures[m.baseColorTexture.texture].data ~= nil, "texture bytes through resolve")
local hud
for _, n in s.nodes do
if n.name == "Hud" then
hud = n
end
end
t:check(hud.extras.godot.is2D and hud.extras.godot.properties.rotation == 0.5, "2D node kept in extras")
local pos = hud.extras.godot.properties.position
t:check(GodotVariant.isCtor(pos) and pos.type == "Vector2" and pos.args[2] == 20, "2D position as a Vector2")
-- without resolve: the instance stays an empty node
local bare = assert(decode(Fx.cases[2].data))
local inst
for _, n in bare.nodes do
if n.name == "PartInstance" then
inst = n
end
end
t:check(inst and #inst.children == 0, "unresolved instance")
local e = scenes["hand.escn"]
t:eq(e.meta.source.version, 2, "escn format 2")
t:eq(#e.cameras, 1, "escn camera")
t:check(math.abs(math.deg(e.cameras[1].yfov) - 60) < 1e-9 and e.cameras[1].zfar == 250, "escn camera fov / far")
t:eq(e.lights[1].range, 9, "escn light range")
local cube = e.nodes[2]
local p = e.meshes[cube.mesh].primitives[1]
t:eq(p.indices.count, 6, "escn arrays surface")
t:check(e.materials[1].doubleSided and e.materials[1].metallic == 0.5, "SpatialMaterial")
local strip = e.meshes[e.nodes[3].mesh].primitives[1]
t:check(strip.mode == "triangles" and strip.indices.count == 6 and strip.attributes.COLOR_0 ~= nil, "escn triangle strip -> triangles")
t:check(cube.matrix[14] == 3 and cube.matrix[7] == 1, "Transform (format 2) rows")
end
-- any Scene: through F.save
do
local s = S.new()
local P = S.accessor(nil, "f32", 3, 3)
for i, v in { 0, 0, 0, 1, 0, 0, 0, 1, 0 } do
buffer.writef32(P.data, (i - 1) * 4, v)
end
s.meshes[1] = { primitives = { { attributes = { POSITION = P }, mode = "triangles" } } }
s.nodes = { { name = "a/b", children = {}, mesh = 1, translation = { 1, 2, 3 } }, { name = "a/b", children = {} } }
s.roots = { 1, 2 }
local out = assert(F.save(s, nil, { format = "godot" }))
local back = assert(decode(out))
t:eq(back.nodes[1].name, "Root", "two roots under Root")
t:check(back.nodes[2].name == "a_b" and back.nodes[3].name == "a_b2", "names sanitised and unique")
t:eq(back.nodes[2].matrix[14], 2, "transform")
t:eq(back.meshes[1].primitives[1].indices.count, 3, "triangle")
t:eq(F.detect(out), "godot", "F.detect")
end
do
local _, e = decode("[gd_resource type=\"Resource\" format=3]\n[resource]\nx = 1\n")
t:check(e and e.code == Err.UNSUPPORTED, "a .tres is not a scene")
local _, e2 = decode("[gd_scene format=3]\n[node name=\"A\" parent=\"missing\"]\n")
t:check(e2 and e2.code == Err.CORRUPT, "missing parent")
local _, e3 = decode("[gd_scene format=3]\nx = Vector3(1,\n")
t:check(e3 ~= nil, "truncated value")
local _, e4 = Godot.encode({} :: any)
t:check(e4 and e4.code == Err.ARG, "encode arg")
end
t:fuzz("decode", function(b: buffer)
return Godot.decode(Bytes.reader(b))
end, samples, { mutations = 80, garbage = 20 })
t:done()
+165
View File
@@ -0,0 +1,165 @@
-- model/t3d tests: UE1-3 and UE4 T3D maps against reference values computed by tools/fixtures/t3d.py (brush world
-- vertices through PrePivot / MainScale / FRotationMatrix / PostScale / Location, polygon areas per texture, bounds,
-- texel probes), actors, lights, components, static mesh references; errors; fuzz.
local T = require("@tests/T")
local F = require("@rosetta")
local T3d = require("@rosetta/model/t3d")
local MeshOps = require("@rosetta/ops/MeshOps")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Fx = require("@tests/gen/t3d")
local t = T.suite("t3d")
local S = Types.Scene
local function decode(s: string | buffer, opts: any?): (any, any)
local b = if type(s) == "string" then buffer.fromstring(s) else s
return T3d.decode(Bytes.reader(b), opts)
end
t:eq(T3d.name, "t3d", "name")
t:eq(T3d.category, "model", "category")
t:check(T3d.caps.decode and not T3d.caps.encode, "caps")
local function near(a: number, b: number, eps: number): boolean
return math.abs(a - b) <= eps * math.max(1, math.abs(b))
end
local function triArea(prim: any, m: { number }): number
local P = S.reader(prim.attributes.POSITION) :: any
local I = S.reader(prim.indices) :: any
local a = 0
local function w(i: number): (number, number, number)
local x, y, z = P(i, 0), P(i, 1), P(i, 2)
return m[1] * x + m[5] * y + m[9] * z + m[13], m[2] * x + m[6] * y + m[10] * z + m[14], m[3] * x + m[7] * y + m[11] * z + m[15]
end
for k = 0, prim.indices.count // 3 - 1 do
local ax, ay, az = w(I(k * 3, 0))
local bx, by, bz = w(I(k * 3 + 1, 0))
local cx, cy, cz = w(I(k * 3 + 2, 0))
local ux, uy, uz, vx, vy, vz = bx - ax, by - ay, bz - az, cx - ax, cy - ay, cz - az
local nx, ny, nz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
a += math.sqrt(nx * nx + ny * ny + nz * nz) / 2
end
return a
end
local samples = {}
for _, c in Fx.cases do
local scene, err = decode(c.data, { textureSize = c.sizes })
t:check(scene, c.name .. ": decode: " .. tostring(err))
t:eq(T3d.sniff(Bytes.reader(buffer.fromstring(c.data))), 0.9, c.name .. ": sniff")
table.insert(samples, c.data)
if scene then
t:eq(scene.meta.up, "z", c.name .. ": up")
local byName = {}
for _, n in scene.nodes do
byName[n.name] = n
end
for _, a in c.actors do
local n = byName[a.name]
t:check(n, c.name .. ": actor " .. a.name)
if n then
t:eq(n.extras.t3d.class, a.class, c.name .. ": " .. a.name .. " class")
local m = S.nodeMatrix(n)
if a.origin then
t:check(near(m[13], a.origin[1], 1e-9) and near(m[14], a.origin[2], 1e-9) and near(m[15], a.origin[3], 1e-9), c.name .. ": " .. a.name .. " location")
end
if a.staticMesh then
t:eq(n.extras.t3d.staticMesh, a.staticMesh, c.name .. ": static mesh reference")
end
t:eq(n.mesh ~= nil, a.mesh == true, c.name .. ": " .. a.name .. " has geometry")
if a.mesh and n.mesh then
local mesh = scene.meshes[n.mesh]
local areas, tris = {}, 0
for _, p in mesh.primitives do
local name = scene.materials[p.material].name or ""
areas[name] = (areas[name] or 0) + triArea(p, m)
tris += p.indices.count // 3
end
t:eq(tris, a.triangles, c.name .. ": " .. a.name .. " triangles")
for tex, want in a.area do
t:check(areas[tex] and near(areas[tex], want, 1e-6), c.name .. ": " .. a.name .. " area of " .. tex .. ": " .. tostring(areas[tex]) .. " vs " .. want)
end
local one = Types.Scene.new()
one.meshes = { mesh }
one.nodes = { { children = {}, mesh = 1, matrix = m } }
one.roots = { 1 }
local lo, hi = MeshOps.bounds(MeshOps.merge(one))
local ok = true
for k = 1, 3 do
ok = ok and near(lo[k], a.min[k], 1e-6) and near(hi[k], a.max[k], 1e-6)
end
t:check(ok, c.name .. ": " .. a.name .. " world bounds")
-- the probe vertex (local) has the reference texel coordinates
local pr = a.probe
local found = false
for _, p in mesh.primitives do
local P = S.reader(p.attributes.POSITION) :: any
local U = S.reader(p.attributes.TEXCOORD_0) :: any
for i = 0, p.attributes.POSITION.count - 1 do
if near(P(i, 0), pr[1], 1e-6) and near(P(i, 1), pr[2], 1e-6) and near(P(i, 2), pr[3], 1e-6) and near(U(i, 0), pr[4], 1e-5) and near(U(i, 1), pr[5], 1e-5) then
found = true
end
end
end
t:check(found, c.name .. ": " .. a.name .. " texel probe")
end
end
end
end
end
do
local s = assert(decode(Fx.cases[1].data, { textureSize = Fx.cases[1].sizes }))
local room, light
for _, n in s.nodes do
if n.name == "Brush0" then
room = n
elseif n.name == "Light0" then
light = n
end
end
t:eq(room.extras.t3d.csg, "CSG_Subtract", "csg operation")
-- a subtracted room faces inwards: the floor's normal points up (+z) in the brush frame
local prim
for _, p in s.meshes[room.mesh].primitives do
if s.materials[p.material].name == "Floor" then
prim = p
end
end
local N = S.reader(prim.attributes.NORMAL) :: any
local P = S.reader(prim.attributes.POSITION) :: any
t:check(P(0, 2) < 0 and N(0, 2) > 0.99, "subtractive brush faces inwards")
t:check(s.lights[s.nodes[table.find(s.nodes, light) :: number].light].type == "point", "UE1 light")
t:eq(s.nodes[1].extras.t3d.properties.Title, "Test Level", "actor properties")
local withBuilder = assert(decode(Fx.cases[1].data, { builder = true }))
t:eq(#withBuilder.meshes, #s.meshes + 1, "builder brush on request")
local u4 = assert(decode(Fx.cases[2].data))
t:eq(u4.meta.source.engine, "ue4", "UE4 text")
local pl
for _, n in u4.nodes do
if n.light then
pl = u4.lights[n.light]
end
end
t:check(pl and pl.intensity == 3000 and pl.range == 500 and near(pl.color[1], 1, 1e-9), "UE4 point light component")
local bare = assert(decode("Begin PolyList\nBegin Polygon Texture=X\nVertex 0,0,0\nVertex 0,1,0\nVertex 1,0,0\nEnd Polygon\nEnd PolyList\n"))
t:eq(#bare.meshes, 1, "a bare polylist")
t:eq(F.detect(Fx.cases[1].data), "t3d", "F.detect")
end
do
local _, e = decode("Begin Map\nBegin Actor Class=Brush Name=B\n")
t:check(e and e.code == Err.TRUNCATED, "unclosed blocks: " .. tostring(e))
local _, e2 = decode("hello")
t:check(e2 and e2.code == Err.CORRUPT, "no blocks")
local _, e3 = decode(Fx.cases[1].data, { maxBlocks = 3 })
t:check(e3 and e3.code == Err.LIMIT, "block limit")
end
t:fuzz("decode", function(b: buffer)
return T3d.decode(Bytes.reader(b))
end, samples)
t:done()
+278
View File
@@ -0,0 +1,278 @@
-- vector/ldtk tests: LDtk's sample projects (and a multi-world one) against the editor's own JSON (levels, layers,
-- tile lists, IntGrid values, entities), external levels through resolve; our writer's output summarised by Python;
-- round trips; a TileMap from Tiled written as LDtk; errors; fuzz. Fixtures: tools/fixtures/ldtk.py.
local T = require("@tests/T")
local F = require("@rosetta")
local Ldtk = require("@rosetta/vector/ldtk")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Fx = require("@tests/gen/ldtk")
local Ours = require("@tests/gen/ldtk_ours")
local t = T.suite("ldtk")
local function decode(s: string | buffer, opts: any?): (any, any)
local b = if type(s) == "string" then buffer.fromstring(s) else s
return Ldtk.decode(Bytes.reader(b), opts)
end
t:eq(Ldtk.name, "ldtk", "name")
t:eq(Ldtk.category, "vector", "category")
t:check(Ldtk.caps.decode and Ldtk.caps.encode, "caps")
local function levelsOf(map: any): { { group: any, world: string } }
local out = {}
for _, l in map.layers do
if l.world then
for _, lv in l.layers do
table.insert(out, { group = lv, world = l.name })
end
else
table.insert(out, { group = l, world = "" })
end
end
return out
end
-- our TileMap summarised like tools/fixtures/ldtk.py summarises the JSON
local function stats(map: any): any
local levels = {}
for _, it in levelsOf(map) do
local g = it.group
local layers = {}
for _, l in g.layers do
local tl = l.tiles or {}
local sum, flips = 0, 0
for i, x in tl do
sum += x.tile * ((i - 1) % 7 + 1)
if x.flip ~= 0 then
flips += 1
end
end
local ic, is = 0, 0
for _, v in l.intGrid or {} do
if v ~= 0 then
ic += 1
end
is += v
end
local names, epx = {}, 0
for _, o in l.objects or {} do
table.insert(names, o.class)
epx += (o.x + o.pivot[1] * o.width) * 3 + (o.y + o.pivot[2] * o.height)
end
table.insert(layers, {
identifier = l.name,
type = l.ldtkType,
cWid = l.width or 0,
cHei = l.height or 0,
grid = l.gridSize,
tiles = #tl,
tileSum = sum,
flips = flips,
intCount = ic,
intSum = is,
entities = #(l.objects or {}),
entityNames = table.concat(names, ","),
entityPx = epx,
})
end
local nf = 0
for _ in g.properties do
nf += 1
end
table.insert(levels, { identifier = g.name, worldX = g.offsetX, worldY = g.offsetY, pxWid = g.pxWidth, pxHei = g.pxHeight, world = it.world, fields = nf, layers = layers })
end
local ts = {}
for i, x in map.tilesets do
ts[i] = x.name
end
return { levels = levels, tilesets = ts }
end
local function diff(a: any, b: any, path: string, out: { string })
if #out > 6 then
return
end
if type(a) == "table" and type(b) == "table" then
for k, v in a do
diff(v, b[k], path .. "." .. tostring(k), out)
end
for k, v in b do
if a[k] == nil then
diff(nil, v, path .. "." .. tostring(k), out)
end
end
elseif a ~= b then
table.insert(out, path .. ": " .. tostring(a) .. " vs " .. tostring(b))
end
end
local function sameStats(got: any, want: any, label: string)
local out = {}
-- entity-less layers report cWid 0 in ours when unknown: compare only layers' known widths
for _, lv in got.levels do
for _, l in lv.layers do
if l.type == "Entities" then
l.cWid, l.cHei = nil, nil
end
end
end
for _, lv in want.levels do
for _, l in lv.layers do
if l.type == "Entities" then
l.cWid, l.cHei = nil, nil
end
end
end
diff(got, want, "", out)
t:check(#out == 0, label .. ": " .. table.concat(out, "; "))
end
local maps: { [string]: any } = {}
local samples = {}
for _, c in Fx.cases do
local function resolve(p: string): buffer?
local f = c.files[p]
return if f then buffer.fromstring(f) else nil
end
local map, err = decode(c.data, { resolve = resolve })
t:check(map, c.name .. ": decode: " .. tostring(err))
if map then
maps[c.name] = map
t:eq(map.kind, "tilemap", c.name .. ": kind")
sameStats(stats(map), c.stats, c.name .. ": = LDtk's JSON")
end
t:check(Ldtk.sniff(Bytes.reader(buffer.fromstring(c.data))) >= 0.8, c.name .. ": sniff")
if #c.data < 200000 then
table.insert(samples, c.data)
end
end
-- Python's summary of our output = LDtk's
for _, o in Ours do
local want
for _, c in Fx.cases do
if c.name == o.name then
want = c.stats
end
end
sameStats(o.stats, want, o.name .. ": our LDtk output (Python)")
end
-- details of the API sample
do
local m = maps["Test_file_for_API_showing_all_features.ldtk"]
t:eq(m.meta.source.version, "1.5.3", "json version")
local lv = m.layers[1]
t:check(lv.type == "group" and lv.level == true and lv.name ~= "", "level group")
local ents
for _, l in lv.layers do
if l.type == "objects" then
ents = l
end
end
t:check(ents and #ents.objects > 0, "entities layer")
local e = ents.objects[1]
t:eq(e.class, "EntityFieldsTest", "entity identifier")
t:eq(e.properties.Integer, 1, "Int field")
t:eq(e.properties.Float, 1.5, "Float field")
t:eq(e.properties.String_singleLine, "foo", "String field")
t:eq(e.propertyTypes.Enum, "LocalEnum.SomeEnum", "enum field type")
t:eq(e.properties.Color, "#D181E8", "Color field")
t:eq(e.x, 152 - 16, "entity x = px - pivot * width")
local top = lv.layers[#lv.layers]
t:eq(top.ldtkType, "Entities", "LDtk's first layer is drawn last")
local tiles
for _, l in lv.layers do
if l.ldtkType == "Tiles" then
tiles = l
end
end
local ts = m.tilesets[1]
t:eq(ts.imageSource, "atlas/Inca_front_by_Kronbits-extended.png", "tileset image path")
local first = tiles.tiles[1]
local cell = tiles.data[1 + first.x // 16 + (first.y // 16) * tiles.width]
t:check(bit32.band(cell, 0x0FFFFFFF) - ts.firstGid >= 0, "gid grid from the tile list")
end
do
local m = maps["SeparateLevelFiles.ldtk"]
t:check(m.meta.source.externalLevels, "external levels flag")
t:check(m.layers[1].external and #m.layers[1].layers > 0, "external level loaded through resolve")
local c
for _, x in Fx.cases do
if x.name == "SeparateLevelFiles.ldtk" then
c = x
end
end
local nores = decode(c.data)
t:check(nores and nores.layers[1].unresolved and #nores.layers[1].layers == 0, "external level without resolve")
end
do
local m = maps["multiworld.ldtk"]
t:check(m.meta.source.multiWorld and #m.layers == 2 and m.layers[1].world and m.layers[2].name == "Caves", "worlds")
end
-- edits survive a save
do
local m = maps["Test_file_for_API_showing_all_features.ldtk"]
local lv = m.layers[1]
lv.properties = lv.properties or {}
local ents
for _, l in lv.layers do
if l.type == "objects" then
ents = l
end
end
ents.objects[1].properties.Integer = 42
ents.objects[1].x += 16
local out = assert(Ldtk.encode(m))
local back = assert(decode(out))
local e2
for _, l in back.layers[1].layers do
if l.type == "objects" then
e2 = l.objects[1]
end
end
t:eq(e2.properties.Integer, 42, "edited field")
t:eq(e2.x, 152, "moved entity")
local s = buffer.tostring(out)
t:check(string.find(s, "\"V_Int\",%s*\"params\":%s*%[%s*42%s*%]") ~= nil, "realEditorValues updated")
t:check(F.detect(out) == "ldtk", "F.detect")
end
-- a Tiled map as LDtk: a tile layer and objects
do
local map = Types.TileMap.new(3, 2, 16, 16)
table.insert(map.tilesets, { name = "terrain", firstGid = 1, tileWidth = 16, tileHeight = 16, columns = 4, count = 8, imageSource = "terrain.png", imageWidth = 64, imageHeight = 32 })
table.insert(map.layers, { type = "tiles", name = "Ground", width = 3, height = 2, visible = true, opacity = 1, properties = {}, data = { 1, 2, 0, 0, 6, bit32.bor(8, 0x80000000) } })
table.insert(map.layers, { type = "objects", name = "Things", visible = true, opacity = 1, properties = {}, objects = { { name = "spawn", class = "Spawn", x = 16, y = 0, width = 16, height = 16, properties = { hp = 3 } } } })
local out = assert(F.save(map, nil, { format = "ldtk" }))
local back = assert(decode(out))
local lv = back.layers[1]
t:eq(#lv.layers, 2, "tiled -> ldtk: one level, two layers")
local g = lv.layers[1]
t:eq(g.ldtkType, "Tiles", "tile layer")
t:eq(#g.tiles, 4, "tiles")
t:eq(g.data[6], bit32.bor(8, 0x80000000), "gid + flip")
t:eq(g.tiles[4].srcX, 48, "src x from the tileset columns")
local o = lv.layers[2].objects[1]
t:check(o.class == "Spawn" and o.x == 16 and o.properties.hp == 3, "entity with a made field definition")
end
do
local _, e = decode("{\"jsonVersion\": \"1.5.3\"}")
t:check(e and e.code == Err.CORRUPT, "no defs")
local _, e2 = decode("{")
t:check(e2 ~= nil, "bad JSON")
local _, e3 = Ldtk.encode({} :: any)
t:check(e3 and e3.code == Err.ARG, "encode arg")
end
t:fuzz("decode", function(b: buffer)
return decode(b)
end, samples, { mutations = 60, garbage = 20 })
t:done()
+212
View File
@@ -0,0 +1,212 @@
-- vector/ogmo tests: Ogmo Editor 3 levels (HaxeFlixel's TurnBasedRPG and levels made in every save layout) against a
-- Python reading of the editor's format; projects; our writer's output (level + project) read by the same Python;
-- round trips; errors; fuzz. Fixtures: tools/fixtures/ogmo.py.
local T = require("@tests/T")
local F = require("@rosetta")
local Ogmo = require("@rosetta/vector/ogmo")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Fx = require("@tests/gen/ogmo")
local Ours = require("@tests/gen/ogmo_ours")
local t = T.suite("ogmo")
local function decode(s: string | buffer, opts: any?): (any, any)
local b = if type(s) == "string" then buffer.fromstring(s) else s
return Ogmo.decode(Bytes.reader(b), opts)
end
t:eq(Ogmo.name, "ogmo", "name")
t:eq(Ogmo.category, "vector", "category")
t:check(Ogmo.caps.decode and Ogmo.caps.encode, "caps")
local function fmt(v: number): string
if v == math.floor(v) then
return string.format("%d", v)
end
return string.format("%.6g", v)
end
local function jsonValue(v: any): string
if type(v) == "string" then
return "\"" .. v .. "\""
elseif type(v) == "boolean" then
return tostring(v)
end
return if v == math.floor(v) then string.format("%d", v) else string.format("%.17g", v)
end
local function valuesStr(props: any): string
local keys = {}
for k in props do
table.insert(keys, k)
end
table.sort(keys)
local out = {}
for _, k in keys do
table.insert(out, k .. "=" .. jsonValue(props[k]))
end
return table.concat(out, ",")
end
-- our TileMap summarised like tools/fixtures/ogmo.py summarises a level
local function stats(map: any): any
local layers = {}
for _, l in map.layers do
local d: any = { name = l.name, kind = l.ogmoType }
if l.ogmoType == "entity" then
d.items = {}
for _, o in l.objects do
table.insert(d.items, string.format("%s@%s,%s %sx%s r%d n%d %s", o.class, fmt(o.x), fmt(o.y), fmt(o.width), fmt(o.height), math.floor(o.rotation + 0.5), if o.nodes then #o.nodes else 0, valuesStr(o.properties)))
end
elseif l.ogmoType == "decal" then
d.items = {}
for _, o in l.objects do
table.insert(d.items, string.format("%s@%s,%s s%s,%s r%d", o.texture, fmt(o.x), fmt(o.y), fmt(o.scaleX), fmt(o.scaleY), math.floor(o.rotation + 0.5)))
end
elseif l.ogmoType == "grid" then
d.cells = table.concat(l.grid)
else
local ts
for _, x in map.tilesets do
if x.name == l.tileset then
ts = x
end
end
local ne, ids, fl = 0, 0, 0
for k, g in l.data do
local gid = bit32.band(g, 0x0FFFFFFF)
local id = if gid == 0 then -1 else gid - (if ts then ts.firstGid else 1)
if id >= 0 then
ne += 1
end
ids += (id + 1) * ((k - 1) % 7 + 1)
local f = (if bit32.btest(g, 0x80000000) then 4 else 0) + (if bit32.btest(g, 0x40000000) then 2 else 0) + (if bit32.btest(g, 0x20000000) then 1 else 0)
fl += f * ((k - 1) % 5 + 1)
end
d.tileset, d.nonEmpty, d.idSum, d.flagSum = l.tileset or "", ne, ids, fl
end
table.insert(layers, d)
end
return { width = map.pxWidth, height = map.pxHeight, values = valuesStr(map.properties), layers = layers }
end
local function diff(a: any, b: any, path: string, out: { string })
if #out > 6 then
return
end
if type(a) == "table" and type(b) == "table" then
for k, v in a do
diff(v, b[k], path .. "." .. tostring(k), out)
end
for k, v in b do
if a[k] == nil then
diff(nil, v, path .. "." .. tostring(k), out)
end
end
elseif a ~= b then
table.insert(out, path .. ": " .. tostring(a) .. " vs " .. tostring(b))
end
end
local function same(got: any, want: any, label: string)
-- w / h are compared through the layer data sizes
for _, x in { got, want } do
for _, l in x.layers do
l.w, l.h = nil, nil
end
end
local out = {}
diff(got, want, "", out)
t:check(#out == 0, label .. ": " .. table.concat(out, "; "))
end
local samples = {}
local maps = {}
for _, c in Fx.cases do
local map, err = decode(c.data, { project = c.project })
t:check(map, c.name .. ": decode: " .. tostring(err))
if map then
maps[c.name] = map
same(stats(map), c.stats, c.name .. ": = the editor's layout (Python)")
local out = assert(Ogmo.encode(map))
local back = assert(decode(out, { project = c.project }))
same(stats(back), c.stats, c.name .. ": round trip")
end
t:eq(Ogmo.sniff(Bytes.reader(buffer.fromstring(c.data))), 0.9, c.name .. ": sniff level")
table.insert(samples, c.data)
end
for _, o in Ours do
local want
for _, c in Fx.cases do
if c.name == o.name then
want = c.stats
end
end
same(o.stats, want, o.name .. ": our level + project (Python)")
end
t:eq(#Ours, #Fx.cases, "every level written")
for _, p in Fx.projects do
local m, err = decode(p.data)
t:check(m and m.meta.project, p.name .. ": project: " .. tostring(err))
t:eq(Ogmo.sniff(Bytes.reader(buffer.fromstring(p.data))), 1, p.name .. ": sniff project")
if m then
local back = assert(decode(assert(Ogmo.encode(m))))
t:check(back.meta.project and #back.layers == #m.layers and #back.tilesets == #m.tilesets, p.name .. ": project round trip")
end
table.insert(samples, p.data)
end
do
local m = maps["made/level0"]
local ents
for _, l in m.layers do
if l.ogmoType == "entity" then
ents = l
end
end
local p = ents.objects[1]
t:check(p.class == "player" and p.x == 16 and p.y == 16 and p.width == 8 and p.height == 16, "entity: origin, template size")
t:check(math.abs(p.rotation - 90) < 1e-9 and p.flippedX == true and #p.nodes == 2, "entity: radians -> degrees, flip, nodes")
t:eq(p.propertyTypes.hp, "int", "entity value types from the template")
t:eq(m.propertyTypes.gravity, "float", "level value types")
t:eq(m.layers[#m.layers].ogmoType, "entity", "Ogmo's first layer is on top")
local small = m.tilesets[2]
t:check(small.columns == 3 and small.imageWidth == 36 and type(small.imageData) == "buffer", "tileset from the embedded PNG")
local rpg = maps["turnBasedRPG/room-001"]
t:check(rpg.width == 80 and rpg.tileWidth == 8 and rpg.pxWidth == 640, "map size from the project grid")
-- without the project: tiles keep their ids
local c = Fx.cases[1]
local bare = assert(decode(c.data))
t:check(#bare.tilesets == 0 and bare.layers[1].data ~= nil, "level without a project")
end
-- a Tiled-style map as an Ogmo level
do
local map = Types.TileMap.new(2, 2, 16, 16)
table.insert(map.tilesets, { name = "t", firstGid = 1, tileWidth = 16, tileHeight = 16, columns = 2, count = 4 })
table.insert(map.layers, { type = "tiles", name = "ground", width = 2, height = 2, visible = true, opacity = 1, properties = {}, data = { 1, 0, bit32.bor(4, 0x80000000), 2 } })
table.insert(map.layers, { type = "objects", name = "things", visible = true, opacity = 1, properties = {}, objects = { { class = "coin", x = 4, y = 6, width = 8, height = 8, properties = { value = 10 } } } })
local out = assert(F.save(map, nil, { format = "ogmo" }))
local back = assert(decode(out, { project = assert(Ogmo.encode(map, { kind = "project" })) }))
t:eq(back.layers[1].data[3], bit32.bor(4, 0x80000000), "tile + flip")
t:check(back.layers[2].objects[1].properties.value == 10 and back.layers[2].objects[1].x == 4, "object")
end
do
local _, e = decode("{\"ogmoVersion\": \"3.4.0\"}")
t:check(e and e.code == Err.CORRUPT, "no layers")
local _, e2 = decode("[1]")
t:check(e2 and e2.code == Err.CORRUPT, "not an object")
local _, e3 = Ogmo.encode({} :: any)
t:check(e3 and e3.code == Err.ARG, "encode arg")
local _, e4 = Ogmo.encode(maps["made/level0"], { kind = "x" :: any })
t:check(e4 and e4.code == Err.ARG, "bad kind")
end
t:fuzz("decode", function(b: buffer)
return decode(b, { project = Fx.projects[2].data })
end, samples, { mutations = 60, garbage = 20 })
t:done()