Files
rosetta/src/Formats/model/schematic/Sponge.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

132 lines
5.1 KiB
Luau

--!strict
-- model/schematic/Sponge: the Sponge schematic format (.schem: WorldEdit 7+, FAWE, Amulet), from the
-- SpongePowered/Schematic-Specification documents for versions 1, 2 and 3: Width / Height / Length, Offset, the
-- block-state Palette with varint BlockData (v1 / v2) or the Blocks container (v3), block entities (TileEntities /
-- BlockEntities, v3 with Data), Entities, Metadata, DataVersion; biomes are kept as they are.
local Types = require("@formats/Types")
local Nbt = require("@formats/codecs/text/Nbt")
local Mc = require("@formats/codecs/model/Minecraft")
local Common = require("@formats/model/schematic/Common")
local Sponge = {}
local Packed = Mc.Packed
-- read(s, maxCells) -> { Region }, extra | nil, message. s: the Schematic compound (v3: the root's Schematic child)
function Sponge.read(s: any, maxCells: number): ({ Common.Region }?, any)
local version = Common.int(s.Version) or 1
local function u16(v: any): any
local n = Common.int(v)
return if n and n < 0 then n + 65536 else n
end
local sx, sy, sz = Common.dims(u16(s.Width), u16(s.Height), u16(s.Length), maxCells)
if not sx then
return nil, sy
end
local container = if version >= 3 then s.Blocks else s
if version >= 3 and type(container) ~= "table" then
return nil, "no Blocks container"
end
local palette = if version >= 3 then container.Palette else s.Palette
local data = if version >= 3 then container.Data else s.BlockData
if not Types.isObject(palette) then
return nil, "no block palette"
end
if type(data) ~= "buffer" then
return nil, "no block data"
end
local states: { string } = {}
local max = -1
for _, k in Types.keys(palette) do
local i = Common.int(palette[k])
if i == nil or i < 0 or i > 65535 then
return nil, "bad palette index for " .. k
end
states[i + 1] = k
if i > max then
max = i
end
end
for i = 1, max + 1 do
if states[i] == nil then
states[i] = "minecraft:air"
end
end
local n = sx * sy :: number * (sz :: number)
local yzx = buffer.create(n * 2)
local ok, e = Packed.varints(data, 0, buffer.len(data), n, yzx, max + 1)
if not ok then
return nil, e
end
local bes = {}
local raw = if version >= 3 then container.BlockEntities elseif version == 2 then s.BlockEntities else s.TileEntities
for _, c in Common.list(raw) do
local be = Common.blockEntity(c, if version >= 3 then "data" else "pos")
if be then
table.insert(bes, be)
end
end
local off = Common.ints(s.Offset) or { 0, 0, 0 }
local regions: { Common.Region } = {
{
name = nil,
size = { sx, sy :: number, sz :: number },
position = { off[1] or 0, off[2] or 0, off[3] or 0 },
states = states,
indices = Packed.fromYzx(yzx, sx, sy :: number, sz :: number),
blockEntities = bes,
entities = Common.list(s.Entities),
},
}
return regions, { version = version, dataVersion = Common.int(s.DataVersion), metadata = s.Metadata }
end
-- write(region, version, dataVersion, base?) -> the Schematic compound (v3: the caller wraps it in the root) | nil,
-- message. base: the decoded Schematic compound whose other fields (Metadata, Biomes, ...) are kept
function Sponge.write(r: Common.Region, version: number, dataVersion: number, base: any): (any, string?)
local sx, sy, sz = r.size[1], r.size[2], r.size[3]
if sx > 65535 or sy > 65535 or sz > 65535 then
return nil, "a Sponge schematic is at most 65535 cells on each axis"
end
local n = sx * sy * sz
local states, indices = Common.dedupe(r.states, r.indices, n)
local palette, keys = {}, {}
for i, st in states do
palette[st] = i - 1
keys[i] = st
end
local pal = Types.object(palette, keys)
local data = Packed.toVarints(Packed.toYzx(indices, sx, sy, sz), n)
local bes = Nbt.list("compound")
for _, be in r.blockEntities do
table.insert(bes, Common.flatBlockEntity(be, if version >= 3 then "data" else "pos"))
end
local ents = if #r.entities > 0 then r.entities else Nbt.list("compound") -- optional, but readers expect it
local off = Nbt.intArray({ r.position[1], r.position[2], r.position[3] })
local fields: { { any } } = {
{ "Version", version },
{ "DataVersion", if version >= 2 then dataVersion else nil },
{ "Width", Nbt.short(sx) },
{ "Height", Nbt.short(sy) },
{ "Length", Nbt.short(sz) },
{ "Offset", off },
}
if version >= 3 then
local blocksBase = if type(base) == "table" then base.Blocks else nil
table.insert(fields, { "Blocks", Common.merge(blocksBase, { { "Palette", pal }, { "Data", data }, { "BlockEntities", bes } }) })
table.insert(fields, { "Entities", ents })
else
table.insert(fields, { "PaletteMax", #states })
table.insert(fields, { "Palette", pal })
table.insert(fields, { "BlockData", data })
table.insert(fields, { if version == 2 then "BlockEntities" else "TileEntities", bes })
table.insert(fields, { "Entities", if version == 2 then ents else nil })
end
-- the other version's block fields never carry over
local drop = if version >= 3 then { PaletteMax = true, Palette = true, BlockData = true, BlockEntities = true, TileEntities = true }
else { Blocks = true, Biomes = true, TileEntities = version ~= 1, BlockEntities = version == 1 }
return Common.merge(base, fields, drop :: any), nil
end
return Sponge