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). Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
c908ed9ad7
commit
166f3f8eee
11 files changed
+524
-40
No files matched your search
@@ -0,0 +1,155 @@
|
||||
-- codecs/model/Minecraft tests: packed index arrays (spanning and not, 1..16 bits, against a bit-by-bit reference),
|
||||
-- Sponge varints, nibble arrays, YZX reordering, block-state strings and their NBT form, every legacy id / data pair
|
||||
-- through legacyState / legacyId, colours (air, families, fallbacks), voxels / blocksOf and nearest-colour blocks.
|
||||
-- The formats using it: tests/model/schematic.luau, tests/model/mca.luau.
|
||||
local T = require("@tests/T")
|
||||
local Mc = require("@formats/codecs/model/Minecraft")
|
||||
local Types = require("@formats/Types")
|
||||
|
||||
local t = T.suite("minecraft")
|
||||
local P = Mc.Packed
|
||||
local rand = T.rng(3)
|
||||
|
||||
-- 1. packed arrays against a reference that walks the bits one at a time
|
||||
local function refBit(longs: buffer, p: number): number
|
||||
return bit32.extract(buffer.readu8(longs, p // 8), p % 8, 1)
|
||||
end
|
||||
for bits = 1, 16 do
|
||||
for _, spanning in { true, false } do
|
||||
local count = 100 + rand(300)
|
||||
local max = math.min(2 ^ bits, 70000)
|
||||
local idx = buffer.create(count * 2)
|
||||
for i = 0, count - 1 do
|
||||
buffer.writeu16(idx, i * 2, rand(max))
|
||||
end
|
||||
local longs = P.pack(idx, count, bits, spanning)
|
||||
t:eq(buffer.len(longs), P.longsNeeded(count, bits, spanning) * 8, bits .. " bits: size")
|
||||
local per = 64 // bits
|
||||
local bad = 0
|
||||
for i = 0, count - 1 do
|
||||
local p = if spanning then i * bits else (i // per) * 64 + (i % per) * bits
|
||||
local v = 0
|
||||
for k = 0, bits - 1 do
|
||||
v += refBit(longs, p + k) * 2 ^ k
|
||||
end
|
||||
if v ~= buffer.readu16(idx, i * 2) then
|
||||
bad += 1
|
||||
end
|
||||
end
|
||||
t:eq(bad, 0, bits .. " bits " .. tostring(spanning) .. ": layout")
|
||||
local back = buffer.create(count * 2)
|
||||
local ok, e = P.unpack(longs, bits, count, spanning, back, max)
|
||||
t:check(ok, tostring(e))
|
||||
t:eq(buffer.tostring(back), buffer.tostring(idx), bits .. " bits " .. tostring(spanning) .. ": round trip")
|
||||
end
|
||||
end
|
||||
do
|
||||
local out = buffer.create(40)
|
||||
t:check(not P.unpack(buffer.create(8), 4, 20, false, out, 16), "too few longs")
|
||||
local l = P.pack(buffer.fromstring("\5\0"), 1, 4, false)
|
||||
t:check(not P.unpack(l, 4, 1, false, out, 3), "index past the palette")
|
||||
t:eq(P.bitsFor(17, 4), 5, "bitsFor")
|
||||
t:eq(P.bitsFor(1, 2), 2, "bitsFor minimum")
|
||||
end
|
||||
|
||||
-- 2. varints, nibbles, YZX
|
||||
do
|
||||
local idx = buffer.create(400)
|
||||
for i = 0, 199 do
|
||||
buffer.writeu16(idx, i * 2, (i * 997) % 20000)
|
||||
end
|
||||
local v = P.toVarints(idx, 200)
|
||||
local back = buffer.create(400)
|
||||
t:check(P.varints(v, 0, buffer.len(v), 200, back, 20000), "varints")
|
||||
t:eq(buffer.tostring(back), buffer.tostring(idx), "varints round trip")
|
||||
t:check(not P.varints(v, 0, buffer.len(v) - 1, 200, back, 20000), "varints truncated")
|
||||
local nib = buffer.create(2)
|
||||
P.setNibble(nib, 0, 0, 0xA, true)
|
||||
P.setNibble(nib, 0, 1, 0x3, true)
|
||||
P.setNibble(nib, 0, 2, 0x7, false)
|
||||
t:eq(buffer.readu8(nib, 0), 0xA3, "high nibble first")
|
||||
t:eq(buffer.readu8(nib, 1), 0x07, "low nibble first")
|
||||
t:eq(P.nibble(nib, 0, 1, true), 3, "read nibble")
|
||||
local sx, sy, sz = 3, 4, 5
|
||||
local yzx = buffer.create(sx * sy * sz * 2)
|
||||
for y = 0, sy - 1 do
|
||||
for z = 0, sz - 1 do
|
||||
for x = 0, sx - 1 do
|
||||
buffer.writeu16(yzx, (x + z * sx + y * sx * sz) * 2, x * 100 + y * 10 + z)
|
||||
end
|
||||
end
|
||||
end
|
||||
local vox = P.fromYzx(yzx, sx, sy, sz)
|
||||
t:eq(buffer.readu16(vox, (2 + sx * (3 + sy * 4)) * 2), 234, "fromYzx")
|
||||
t:eq(buffer.tostring(P.toYzx(vox, sx, sy, sz)), buffer.tostring(yzx), "toYzx")
|
||||
end
|
||||
|
||||
-- 3. states
|
||||
do
|
||||
local name, props = Mc.parseState("oak_log[axis=y, foo = bar]")
|
||||
t:eq(name, "minecraft:oak_log", "namespace added")
|
||||
t:eq(#props, 2, "props")
|
||||
t:eq(Mc.stateString(name, props), "minecraft:oak_log[axis=y,foo=bar]", "stateString")
|
||||
local c = Mc.stateToNbt("minecraft:chest[facing=north,type=single]")
|
||||
t:eq(table.concat(Types.keys(c.Properties), ","), "facing,type", "NBT property order")
|
||||
t:eq(Mc.stateFromNbt(c), "minecraft:chest[facing=north,type=single]", "stateFromNbt")
|
||||
t:eq(Mc.baseName("minecraft:stone"), "stone", "baseName")
|
||||
t:check(Mc.isAir("minecraft:cave_air") and not Mc.isAir("minecraft:stone"), "isAir")
|
||||
end
|
||||
|
||||
-- 4. legacy ids: every id / data pair reads back exactly
|
||||
do
|
||||
local bad = 0
|
||||
for id = 0, 255 do
|
||||
for d = 0, 15 do
|
||||
local s = Mc.legacyState(id, d)
|
||||
local i2, d2 = Mc.legacyId(s)
|
||||
if i2 ~= id or d2 ~= d then
|
||||
bad += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(bad, 0, "legacy round trip")
|
||||
t:eq(Mc.legacyState(35, 14), "minecraft:red_wool", "wool 14")
|
||||
t:eq(Mc.legacyState(5, 2), "minecraft:birch_planks", "planks 2")
|
||||
t:eq(Mc.legacyState(17, 5), "minecraft:spruce_log[data=5]", "log axis bits kept")
|
||||
t:eq(select(1, Mc.legacyId("minecraft:white_concrete")), 251, "modern name -> legacy id")
|
||||
end
|
||||
|
||||
-- 5. colours
|
||||
do
|
||||
t:eq(Mc.color("minecraft:air"), nil, "air")
|
||||
local function hex(s: string): string
|
||||
local c = Mc.color(s) :: { number }
|
||||
return string.format("%02x%02x%02x%02x", c[1], c[2], c[3], c[4])
|
||||
end
|
||||
t:eq(hex("minecraft:stone"), "7d7d7dff", "exact")
|
||||
t:eq(hex("minecraft:stone_brick_stairs[facing=east]"), hex("minecraft:stone_bricks"), "stairs -> base")
|
||||
t:eq(hex("minecraft:red_concrete"), "8e2020ff", "dye family")
|
||||
t:eq(hex("minecraft:dark_oak_log"), "3e2f17ff", "wood bark")
|
||||
t:eq(string.sub(hex("minecraft:red_stained_glass"), 7, 8), "80", "stained glass translucent")
|
||||
t:eq(hex("minecraft:wool[data=14]"), hex("minecraft:red_wool"), "legacy data colour")
|
||||
t:eq(hex("mod:something_odd"), "909090ff", "fallback")
|
||||
end
|
||||
|
||||
-- 6. voxels / blocksOf
|
||||
do
|
||||
local states = { "minecraft:air", "minecraft:stone", "minecraft:red_wool", "minecraft:stone" }
|
||||
local idx = buffer.create(8 * 2)
|
||||
for i = 0, 7 do
|
||||
buffer.writeu16(idx, i * 2, i % 4)
|
||||
end
|
||||
local v = Mc.voxels(2, 2, 2, states, idx)
|
||||
t:eq(buffer.readu8(v.data, 0), 0, "air -> 0")
|
||||
t:eq(buffer.readu8(v.data, 1), buffer.readu8(v.data, 3), "same colour, same index")
|
||||
t:check(buffer.readu8(v.data, 2) ~= buffer.readu8(v.data, 1), "different colours")
|
||||
local s2, i2 = Mc.blocksOf(v)
|
||||
t:check(s2 == states and i2 == idx, "blocksOf keeps the blocks")
|
||||
local g = Types.Voxels.new(2, 1, 1)
|
||||
g.palette[7] = { 240, 118, 19, 255 }
|
||||
buffer.writeu8(g.data, 1, 7)
|
||||
local s3, i3 = Mc.blocksOf(g)
|
||||
t:eq((s3 :: { string })[buffer.readu16(i3, 2) + 1], "minecraft:orange_wool", "nearest block by colour")
|
||||
t:eq((s3 :: { string })[buffer.readu16(i3, 0) + 1], "minecraft:air", "empty -> air")
|
||||
end
|
||||
t:done()
|
||||
@@ -0,0 +1,221 @@
|
||||
-- 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()
|
||||
Reference in new issue
Block a user