Files
rosetta/tests/model/schematic.luau
T
4DBug fea56f4391 ws-voxel: model/schematic (.schem v1-3, .schematic, .litematic read + write), codecs/model/Minecraft (block states, default colours, legacy ids, packed arrays)
Types change (backward compatible): Types.Voxels gains optional blocks (VoxelBlocks: exact block states as
u16 indices into a state palette, block entities, entities) and colors (exact u32 RGBA per cell).
2026-10-31 14:37:43 -05:00

222 lines
10 KiB
Luau

-- model/schematic tests: .litematic files written by litemapy (several regions, negative sizes, a 40-state palette,
-- tile entities), Sponge .schem v2 (litemapy) and v1 / v3 (nbtlib, by the spec; two-byte varints), MCEdit
-- .schematic (legacy ids + data, WorldEdit AddBlocks, a Schematica name table): every cell's block state, placement,
-- block entities; our re-encodings give the same NBT for the same variant, and the reference readers (litemapy /
-- nbtlib, in the fixture step) read our output, across variants and from colour-only grids, to the same cells;
-- detection, the api, error codes and the fuzz pass. Fixtures: tools/fixtures/schematic.py.
local T = require("@tests/T")
local F = require("@formats")
local Schematic = require("@formats/model/schematic")
local Mc = require("@formats/codecs/model/Minecraft")
local Nbt = require("@formats/codecs/text/Nbt")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local Types = require("@formats/Types")
local Fx = require("@tests/gen/schematic")
local Ours = require("@tests/gen/schematic_ours")
local t = T.suite("schematic")
t:eq(Schematic.name, "schematic", "name")
t:check(Schematic.caps.decode and Schematic.caps.encode, "caps")
local function dec(data: string | buffer, opts: any?): (Types.Scene?, any)
local b = if type(data) == "string" then buffer.fromstring(data) else data
return Schematic.decode(Bytes.reader(b), opts)
end
-- the region record of node i: { name, min, size, cells (state per cell, "\n"-joined), legacy (id * 16 + data) }
local function regions(scene: Types.Scene): { any }
local out = {}
for _, node in scene.nodes do
local v = (scene.voxels :: { Types.Voxels })[node.voxels :: number]
local b = v.blocks :: Types.VoxelBlocks
local n = v.size[1] * v.size[2] * v.size[3]
local cells, legacy = table.create(n), table.create(n)
for i = 0, n - 1 do
local st = b.palette[buffer.readu16(b.data, i * 2) + 1]
cells[i + 1] = st
local id, d = Mc.legacyId(st)
legacy[i + 1] = if id then id * 16 + (d :: number) else -1
end
local tr = node.translation :: { number }
table.insert(out, { name = node.name, min = tr, size = v.size, cells = table.concat(cells, "\n"), legacy = legacy, voxels = v, blockEntities = #(b.blockEntities or {}) })
end
return out
end
local function sameRegion(got: any, want: any, label: string)
t:eq(got.size[1] .. "," .. got.size[2] .. "," .. got.size[3], want.size[1] .. "," .. want.size[2] .. "," .. want.size[3], label .. ": size")
t:eq(got.min[1] .. "," .. got.min[2] .. "," .. got.min[3], want.min[1] .. "," .. want.min[2] .. "," .. want.min[3], label .. ": minimum corner")
if want.cells then
t:eq(got.cells, want.cells, label .. ": cells")
else
local bad = 0
for i, x in want.legacy do
if got.legacy[i] ~= x then
bad += 1
end
end
t:eq(bad, 0, label .. ": legacy ids")
end
end
-- 1. reference files
local decoded: { [string]: Types.Scene } = {}
for _, c in Fx.cases do
local scene, err = dec(c.data)
t:check(scene ~= nil, c.name .. ": " .. tostring(err))
local s = scene :: Types.Scene
decoded[c.name] = s
t:eq(s.meta.source.variant, c.variant, c.name .. ": variant")
if c.version > 0 then
t:eq(s.meta.source.version, c.version, c.name .. ": version")
end
local regs = regions(s)
t:eq(#regs, #c.regions, c.name .. ": regions")
local bes = 0
for i, want in c.regions do
sameRegion(regs[i], want, c.name .. " region " .. i)
if want.name ~= "" then
t:eq(regs[i].name, want.name, c.name .. " region " .. i .. ": name")
end
bes += regs[i].blockEntities
end
t:eq(bes, c.blockEntities, c.name .. ": block entities")
-- colours: air is empty, the rest coloured from the default table
local v = regs[1].voxels
local b = v.blocks
for i = 0, v.size[1] * v.size[2] * v.size[3] - 1 do
local st = b.palette[buffer.readu16(b.data, i * 2) + 1]
local idx = buffer.readu8(v.data, i)
if Mc.isAir(st) ~= (idx == 0) then
t:check(false, c.name .. ": cell " .. i .. " colour index " .. idx .. " for " .. st)
end
if idx > 0 then
local col, p = Mc.color(st) :: { number }, v.palette[idx]
if col[1] ~= p[1] or col[2] ~= p[2] or col[3] ~= p[3] then
t:check(false, c.name .. ": cell " .. i .. " colour of " .. st)
end
end
end
-- the same variant again: the same NBT (every field), the same cells
local out, eerr = Schematic.encode(s)
t:check(out ~= nil, c.name .. ": encode " .. tostring(eerr))
local again = assert(dec(out :: buffer))
t:eq(Nbt.print(again.meta.source.nbt), Nbt.print(s.meta.source.nbt), c.name .. ": same NBT")
t:eq(again.meta.source.rootName, s.meta.source.rootName, c.name .. ": root name")
for i, r in regions(again) do
sameRegion(r, regs[i], c.name .. " again region " .. i)
end
-- api: detection by content (the flavour) and loading
t:eq(F.detect(c.data), c.variant, c.name .. ": F.detect")
local viaApi = F.load(c.data)
t:check(viaApi and viaApi.kind == "scene", c.name .. ": F.load")
end
-- 2. our encodings read back by litemapy / nbtlib (fixture step)
local outputs: { [string]: () -> (buffer?, any) } = {}
local function colorGrid(): Types.Voxels
local v = Types.Voxels.new(4, 3, 2)
v.palette[1] = { 160, 40, 35, 255 }
v.palette[2] = { 125, 125, 125, 255 }
v.palette[3] = { 60, 110, 230, 120 }
for i = 0, 4 * 3 * 2 - 1 do
buffer.writeu8(v.data, i, i % 4)
end
return v
end
outputs["litemapy.litematic"] = function() return Schematic.encode(decoded.litemapy) end
outputs["litemapy.schem"] = function() return Schematic.encode(decoded.litemapy, { variant = "schem", version = 2 }) end
outputs["sponge2.schem"] = function() return Schematic.encode(decoded.sponge2) end
outputs["sponge2.litematic"] = function() return Schematic.encode(decoded.sponge2, { variant = "litematic" }) end
outputs["sponge3.schem"] = function() return Schematic.encode(decoded.sponge3) end
outputs["sponge3v2.schem"] = function() return Schematic.encode(decoded.sponge3, { version = 2 }) end
outputs["sponge1.schem"] = function() return Schematic.encode(decoded.sponge1) end
outputs["mcedit.schematic"] = function() return Schematic.encode(decoded.mcedit) end
outputs["worldedit_add.schematic"] = function() return Schematic.encode(decoded.worldedit_add) end
outputs["sponge2.schematic"] = function() return Schematic.encode(decoded.sponge2, { variant = "schematic" }) end
outputs["mcedit.schem"] = function() return Schematic.encode(decoded.mcedit, { variant = "schem" }) end
outputs["colors.schem"] = function() return Schematic.encode(colorGrid()) end
outputs["colors.litematic"] = function() return Schematic.encode(colorGrid(), { variant = "litematic" }) end
outputs["colors.schematic"] = function() return Schematic.encode(colorGrid(), { variant = "schematic" }) end
local count = 0
for name, ref in Ours do
local make = outputs[name]
t:check(make ~= nil, name .. ": known output")
local mine = assert(dec(assert(make())))
local regs = regions(mine)
t:eq(#regs, #ref.regions, name .. ": regions read back")
for i, want in ref.regions do
sameRegion(regs[i], want, name .. " (reference) region " .. i)
end
if ref.litemapy then
t:eq(ref.litemapy, regs[1].cells, name .. ": litemapy's Sponge reader")
end
count += 1
end
t:eq(count, 14, "every output checked")
-- the source cells survive the conversions
do
local function cellsVia(name: string): string
return regions(assert(dec(assert(outputs[name]()))))[1].cells
end
t:eq(cellsVia("sponge2.litematic"), regions(decoded.sponge2)[1].cells, "schem -> litematic cells")
t:eq(cellsVia("sponge3v2.schem"), regions(decoded.sponge3)[1].cells, "v3 -> v2 cells")
local legacy = regions(assert(dec(assert(outputs["mcedit.schem"]()))))[1].legacy
local src = regions(decoded.mcedit)[1].legacy
t:eq(table.concat(legacy, ","), table.concat(src, ","), "schematic -> schem keeps legacy states")
-- litematic -> schem merges the regions into one grid covering them all
local merged = regions(assert(dec(assert(outputs["litemapy.schem"]()))))[1]
t:eq(merged.size[1] .. "," .. merged.size[2] .. "," .. merged.size[3], "31,5,12", "merged size")
-- colour grid -> blocks by colour
local cg = regions(assert(dec(assert(outputs["colors.schem"]()))))[1]
local first = string.split(cg.cells, "\n")
t:eq(first[1], "minecraft:air", "empty cell -> air")
t:eq(first[2], "minecraft:red_wool", "red -> red wool")
t:eq(first[4], "minecraft:light_blue_stained_glass", "translucent blue -> stained glass")
end
-- 3. through the api
do
local scene = assert(F.load(Fx.cases[1].data, { name = "x.litematic" }))
local schem = assert(F.save(scene, nil, { format = "schem" })) :: buffer
t:eq(F.detect(schem), "schem", "F.save format = schem")
local lite = assert(F.save(scene, nil, { format = "litematic" })) :: buffer
t:eq(F.detect(lite), "litematic", "F.save format = litematic")
local legacy = assert(F.save(scene, nil, { format = "schematic", variant = "schematic" })) :: buffer
t:eq(F.detect(legacy), "schematic", "variant schematic")
t:eq(F.detect(legacy, { name = "a.schematic" }), "schematic", "detect with a name")
-- a vox-like grid without blocks through F.save
local g = Types.Voxels.new(2, 2, 2)
buffer.writeu8(g.data, 0, 5)
t:check(F.save(g, nil, { format = "schem" }) ~= nil, "save a bare Voxels")
end
-- 4. errors
do
local _, e1 = dec(assert(Nbt.encode(Nbt.compound({ a = 1 }, { "a" }))))
t:check(e1 and e1.code == Err.CORRUPT, "not a schematic")
local _, e2 = dec(Fx.cases[1].data, { maxCells = 10 })
t:check(e2 and e2.code == Err.LIMIT, "maxCells")
local _, e3 = Schematic.encode({ kind = "scene" } :: any)
t:check(e3 and e3.code == Err.ARG, "encode without voxels")
local _, e4 = Schematic.encode(colorGrid(), { variant = "x" :: any })
t:check(e4 and e4.code == Err.ARG, "unknown variant")
local bad = Nbt.compound({ Width = Nbt.short(4), Height = Nbt.short(4), Length = Nbt.short(4), Blocks = buffer.create(10) }, { "Width", "Height", "Length", "Blocks" })
local _, e5 = dec(assert(Nbt.encode(bad, { name = "Schematic" })))
t:check(e5 and e5.code == Err.TRUNCATED, "short Blocks")
end
-- 5. fuzz (the uncompressed NBT too, so mutations reach the layouts, not just gzip's CRC)
local samples = {}
for _, c in Fx.cases do
local s = assert(dec(c.data))
table.insert(samples, (assert(Nbt.encode(s.meta.source.nbt, { name = s.meta.source.rootName, compression = "none" }))))
end
table.insert(samples, Fx.cases[1].data)
t:fuzz("decode", function(b: buffer)
return Schematic.decode(Bytes.reader(b))
end, samples)
t:done()