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).
This commit is contained in:
1 parent
7fc5d3f5f0
commit
fea56f4391
11 files changed
+524
-40
No files matched your search
@@ -53,21 +53,18 @@ Legacy.RENAMED = {
|
||||
melon_block = "melon", lit_pumpkin = "jack_o_lantern", slime = "slime_block", magma = "magma_block",
|
||||
red_nether_brick = "red_nether_bricks", nether_brick = "nether_bricks", end_bricks = "end_stone_bricks",
|
||||
mob_spawner = "spawner", web = "cobweb", snow_layer = "snow", snow = "snow_block", noteblock = "note_block",
|
||||
quartz_ore = "nether_quartz_ore", reeds = "sugar_cane", waterlily = "lily_pad", flowing_water = "water",
|
||||
flowing_lava = "lava", tallgrass = "short_grass", grass_path = "dirt_path", double_stone_slab = "smooth_stone",
|
||||
stone_slab = "smooth_stone_slab", deadbush = "dead_bush", yellow_flower = "dandelion",
|
||||
|
||||
quartz_ore = "nether_quartz_ore", reeds = "sugar_cane", waterlily = "lily_pad",
|
||||
tallgrass = "short_grass", grass_path = "dirt_path", double_stone_slab = "smooth_stone",
|
||||
stone_slab = "smooth_stone_slab", deadbush = "dead_bush", yellow_flower = "dandelion",
|
||||
fence = "oak_fence", fence_gate = "oak_fence_gate",
|
||||
wooden_door = "oak_door", trapdoor = "oak_trapdoor", wooden_pressure_plate = "oak_pressure_plate",
|
||||
wooden_button = "oak_button", stone_stairs = "cobblestone_stairs", standing_sign = "oak_sign",
|
||||
wall_sign = "oak_wall_sign", golden_rail = "powered_rail", portal = "nether_portal", piston_extension = "moving_piston",
|
||||
|
||||
silver_shulker_box = "light_gray_shulker_box", silver_glazed_terracotta = "light_gray_glazed_terracotta",
|
||||
|
||||
monster_egg = "infested_stone",
|
||||
standing_banner = "white_banner", wall_banner = "white_wall_banner", skull = "skeleton_skull",
|
||||
bed = "red_bed",
|
||||
}
|
||||
} :: { [string]: string }
|
||||
|
||||
-- legacy names whose data value selects the variant: the modern names by data value (0-based)
|
||||
local DYED = "white orange magenta light_blue yellow lime pink gray light_gray cyan purple blue brown green red black"
|
||||
@@ -105,7 +102,7 @@ Legacy.VARIANTS = {
|
||||
stained_hardened_clay = dyed("terracotta"),
|
||||
concrete = dyed("concrete"),
|
||||
concrete_powder = dyed("concrete_powder"),
|
||||
}
|
||||
Legacy.MASK = { log = 4, log2 = 4, leaves = 4, leaves2 = 4, sapling = 8, wooden_slab = 8, quartz_block = 8, double_plant = 8 }
|
||||
} :: { [string]: { string } }
|
||||
Legacy.MASK = { log = 4, log2 = 4, leaves = 4, leaves2 = 4, sapling = 8, wooden_slab = 8, quartz_block = 8, double_plant = 8 } :: { [string]: number }
|
||||
|
||||
return Legacy
|
||||
@@ -121,18 +121,18 @@ end
|
||||
|
||||
local EXACT: { [string]: { number } } = {}
|
||||
for name, h in string.gmatch(Colors.BLOCKS, "([%w_]+)=(%x+)") do
|
||||
EXACT[name] = hex(h)
|
||||
EXACT[name :: string] = hex(h :: string)
|
||||
end
|
||||
local WOOL, CONCRETE, TERRACOTTA = list(Colors.WOOL), list(Colors.CONCRETE), list(Colors.TERRACOTTA)
|
||||
local PLANKS, BARK, LEAVES = list(Colors.PLANKS), list(Colors.BARK), list(Colors.LEAVES)
|
||||
-- longest prefixes first, so light_blue wins over blue and dark_oak over oak
|
||||
local function byLength(names: { string }): { { any } }
|
||||
local out = {}
|
||||
local out: { { any } } = {}
|
||||
for i, n in names do
|
||||
table.insert(out, { n, i })
|
||||
end
|
||||
table.sort(out, function(a, b)
|
||||
return #a[1] > #b[1]
|
||||
table.sort(out, function(a: { any }, b: { any }): boolean
|
||||
return #(a[1] :: string) > #(b[1] :: string)
|
||||
end)
|
||||
return out
|
||||
end
|
||||
@@ -141,8 +141,8 @@ local SUFFIXES = {
|
||||
"_wall_hanging_sign", "_hanging_sign", "_wall_sign", "_sign", "_fence_gate", "_fence", "_pressure_plate", "_button",
|
||||
"_trapdoor", "_door", "_stairs", "_slab", "_wall", "_pane", "_carpet", "_wall_banner", "_banner", "_bars",
|
||||
}
|
||||
local PREFIXES = { "waxed_", "potted_", "infested_", "lit_", "unlit_", "unpowered_", "powered_", "chipped_", "damaged_" }
|
||||
local CORAL = { tube = hex("3157d0"), brain = hex("cf5b9f"), bubble = hex("a118a0"), fire = hex("a6252f"), horn = hex("d8c53e") }
|
||||
local PREFIXES = { "waxed_", "potted_", "flowing_", "infested_", "lit_", "unlit_", "unpowered_", "powered_", "chipped_", "damaged_" }
|
||||
local CORAL: { [string]: { number } } = { tube = hex("3157d0"), brain = hex("cf5b9f"), bubble = hex("a118a0"), fire = hex("a6252f"), horn = hex("d8c53e") }
|
||||
local FALLBACK = hex("909090")
|
||||
|
||||
local function mix(c: { number }, w: { number }, t: number): { number }
|
||||
|
||||
@@ -46,9 +46,9 @@ end
|
||||
function Common.ints(v: any): { number }?
|
||||
if Nbt.isTag(v) and v.type == "intArray" then
|
||||
return v.value
|
||||
elseif type(v) == "table" and not Nbt.isTag(v) and v[1] ~= nil then
|
||||
local out = {}
|
||||
for i, x in v do
|
||||
elseif type(v) == "table" and not Nbt.isTag(v) and (v :: any)[1] ~= nil then
|
||||
local out: { number } = {}
|
||||
for i, x in v :: { any } do
|
||||
out[i] = Nbt.number(x) or 0
|
||||
end
|
||||
return out
|
||||
|
||||
@@ -91,18 +91,18 @@ function Mcedit.read(root: any, maxCells: number): ({ Common.Region }?, any)
|
||||
table.insert(bes, be)
|
||||
end
|
||||
end
|
||||
return {
|
||||
local regions: { Common.Region } = {
|
||||
{
|
||||
name = nil,
|
||||
size = { sx, sy, sz },
|
||||
size = { sx, sy :: number, sz :: number },
|
||||
position = { Common.int(root.WEOffsetX) or 0, Common.int(root.WEOffsetY) or 0, Common.int(root.WEOffsetZ) or 0 },
|
||||
states = states,
|
||||
indices = Packed.fromYzx(yzx, sx, sy :: number, sz :: number),
|
||||
blockEntities = bes,
|
||||
entities = Common.list(root.Entities),
|
||||
},
|
||||
},
|
||||
{ materials = root.Materials }
|
||||
}
|
||||
return regions, { materials = root.Materials }
|
||||
end
|
||||
|
||||
-- the legacy blocks a colour can become (full blocks of distinct colours)
|
||||
@@ -160,14 +160,50 @@ function Mcedit.write(r: Common.Region, base: any): (any, string?)
|
||||
return nil, "a .schematic is at most 65535 cells on each axis"
|
||||
end
|
||||
local n = sx * sy * sz
|
||||
local ids, datas = table.create(#r.states), table.create(#r.states)
|
||||
-- the source's name tables (Schematica / MCEdit-Unified) give the ids for the names they list
|
||||
local map: { [string]: number } = {}
|
||||
if type(base) == "table" then
|
||||
if Types.isObject(base.SchematicaMapping) then
|
||||
for _, k in Types.keys(base.SchematicaMapping) do
|
||||
local id = Common.int(base.SchematicaMapping[k])
|
||||
if id then
|
||||
map[k] = id
|
||||
end
|
||||
end
|
||||
end
|
||||
if Types.isObject(base.BlockIDs) then
|
||||
for _, k in Types.keys(base.BlockIDs) do
|
||||
local id = tonumber(k)
|
||||
if id and type(base.BlockIDs[k]) == "string" then
|
||||
map[base.BlockIDs[k]] = id
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
local ids, datas = table.create(#r.states) :: { number }, table.create(#r.states) :: { number }
|
||||
local high = false
|
||||
for i, s in r.states do
|
||||
local id, d = Mc.legacyId(s)
|
||||
if id == nil then
|
||||
id, d = nearest(s)
|
||||
if id ~= nil then
|
||||
local lname = Mc.Legacy.NAMES[id + 1]
|
||||
local mapped = lname and map["minecraft:" .. lname]
|
||||
if mapped then
|
||||
id = mapped
|
||||
end
|
||||
else
|
||||
local name, props = Mc.parseState(s)
|
||||
if map[name] then
|
||||
id, d = map[name], 0
|
||||
for _, kv in props do
|
||||
if kv[1] == "data" then
|
||||
d = (tonumber(kv[2]) or 0) % 16
|
||||
end
|
||||
end
|
||||
else
|
||||
id, d = nearest(s)
|
||||
end
|
||||
end
|
||||
ids[i], datas[i] = id, d
|
||||
ids[i], datas[i] = id :: number, d :: number
|
||||
if (id :: number) > 255 then
|
||||
high = true
|
||||
end
|
||||
@@ -189,7 +225,7 @@ function Mcedit.write(r: Common.Region, base: any): (any, string?)
|
||||
table.insert(tiles, Common.flatBlockEntity(be, "flat"))
|
||||
end
|
||||
local ents = if #r.entities > 0 then r.entities else Nbt.list("compound")
|
||||
local fields = {
|
||||
local fields: { { any } } = {
|
||||
{ "Width", Nbt.short(sx) },
|
||||
{ "Height", Nbt.short(sy) },
|
||||
{ "Length", Nbt.short(sz) },
|
||||
@@ -206,7 +242,7 @@ function Mcedit.write(r: Common.Region, base: any): (any, string?)
|
||||
table.insert(fields, { "WEOffsetY", p[2] })
|
||||
table.insert(fields, { "WEOffsetZ", p[3] })
|
||||
end
|
||||
local root = Common.merge(base, fields, { SchematicaMapping = true, BlockIDs = true, Add = true })
|
||||
local root = Common.merge(base, fields, { Add = true })
|
||||
return root, nil
|
||||
end
|
||||
|
||||
|
||||
@@ -67,18 +67,18 @@ function Sponge.read(s: any, maxCells: number): ({ Common.Region }?, any)
|
||||
end
|
||||
end
|
||||
local off = Common.ints(s.Offset) or { 0, 0, 0 }
|
||||
return {
|
||||
local regions: { Common.Region } = {
|
||||
{
|
||||
name = nil,
|
||||
size = { sx, sy, sz },
|
||||
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),
|
||||
},
|
||||
},
|
||||
{ version = version, dataVersion = Common.int(s.DataVersion), metadata = s.Metadata }
|
||||
}
|
||||
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,
|
||||
@@ -101,7 +101,7 @@ function Sponge.write(r: Common.Region, version: number, dataVersion: number, ba
|
||||
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 nil
|
||||
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 },
|
||||
|
||||
@@ -49,11 +49,22 @@ export type EncodeOptions = {
|
||||
title: string?, -- .litematic Metadata.Name
|
||||
compression: ("gzip" | "zlib" | "none")?, -- default gzip, as every tool writes them
|
||||
level: number?, -- deflate level
|
||||
format: string?, -- set by the api: the format key the encoder was picked by ("schem", "litematic", ...)
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local DEFAULT_DATA_VERSION = 3953
|
||||
local LITEMATIC_HINTS = { "Regions", "EnclosingSize", "RegionCount", "TotalVolume", "MinecraftDataVersion" }
|
||||
local SPONGE_HINTS = { "BlockData", "PaletteMax" } -- v1 / v2 fields (some writers leave the root unnamed)
|
||||
|
||||
local function hinted(head: string, hints: { string }): boolean
|
||||
for _, h in hints do
|
||||
if string.find(head, string.char(0, #h) .. h, 1, true) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Schematic.sniff(r: Bytes.Reader): number
|
||||
@@ -65,12 +76,25 @@ function Schematic.sniff(r: Bytes.Reader): number
|
||||
return 1
|
||||
end
|
||||
local head = buffer.tostring(p.head)
|
||||
for _, h in LITEMATIC_HINTS do
|
||||
if string.find(head, string.char(0, #h) .. h, 1, true) then
|
||||
return 1
|
||||
end
|
||||
return if hinted(head, LITEMATIC_HINTS) or hinted(head, SPONGE_HINTS) then 1 else 0
|
||||
end
|
||||
|
||||
-- variant(r) -> "schem" | "litematic" | "schematic" | nil: the flavour, for F.detect (from the first inflated bytes)
|
||||
function Schematic.variant(r: Bytes.Reader): string?
|
||||
local p = Nbt.peek(r.buf, r.base, r.limit - r.base)
|
||||
if not p then
|
||||
return nil
|
||||
end
|
||||
return 0
|
||||
if p.firstName == "Schematic" then
|
||||
return "schem"
|
||||
end
|
||||
local head = buffer.tostring(p.head)
|
||||
if hinted(head, LITEMATIC_HINTS) then
|
||||
return "litematic"
|
||||
elseif hinted(head, SPONGE_HINTS) or (p.name == "Schematic" and hinted(head, { "Palette", "Version" })) then
|
||||
return "schem"
|
||||
end
|
||||
return if p.name == "Schematic" then "schematic" else nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
@@ -114,7 +138,8 @@ function Schematic.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?,
|
||||
regions, extra = Mcedit.read(s, maxCells)
|
||||
end
|
||||
if not regions then
|
||||
return nil, Err.from(extra)
|
||||
local msg = tostring(extra)
|
||||
return nil, Err.from(msg, nil, if string.find(msg, "limit: ", 1, true) then "limit" else nil)
|
||||
end
|
||||
local scene = Types.Scene.new()
|
||||
scene.voxels = {}
|
||||
|
||||
Reference in new issue
Block a user