279 lines
8.6 KiB
Luau
279 lines
8.6 KiB
Luau
-- 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()
|