rename: src/Formats -> src/Rosetta, @formats -> @rosetta requires, build/luajit/rosetta + rosetta._compat, build/roblox/Rosetta, ROSETTA_* env/globals, writer vendor strings
This commit is contained in:
1 parent
2956576d7f
commit
22ddcd78fa
941 files changed
+3364
-3364
No files matched your search
@@ -0,0 +1,128 @@
|
||||
--!strict
|
||||
-- model/schematic/Common: what the three schematic flavours share: the region record they read into and write from,
|
||||
-- NBT field access with type checks, and block entities in one shape ({ pos = { x, y, z }, id, data }: Sponge v3's,
|
||||
-- converted from / to MCEdit's and Litematica's flat { x, y, z, id, ... } and Sponge v2's { Pos, Id, ... }).
|
||||
local Types = require("@rosetta/Types")
|
||||
local Nbt = require("@rosetta/codecs/text/Nbt")
|
||||
local Mc = require("@rosetta/codecs/model/Minecraft")
|
||||
|
||||
local Common = {}
|
||||
|
||||
-- a block region: indices are u16 per cell in Types.Voxels order (x fastest, then y, then z) into states
|
||||
export type Region = {
|
||||
name: string?,
|
||||
size: { number }, -- x, y, z cells
|
||||
position: { number }, -- the minimum corner relative to the schematic origin
|
||||
states: { string },
|
||||
indices: buffer,
|
||||
blockEntities: { any },
|
||||
entities: { any },
|
||||
}
|
||||
|
||||
-- int(v) -> the integer value of an NBT number (byte / short / int / long tag or plain number) | nil
|
||||
Common.int = Mc.int
|
||||
|
||||
-- dims(a, b, c, max) -> sx, sy, sz | nil, message: positive sizes whose product stays under max cells
|
||||
function Common.dims(a: any, b: any, c: any, max: number): (number?, any, number?)
|
||||
local x, y, z = Common.int(a), Common.int(b), Common.int(c)
|
||||
if not x or not y or not z then
|
||||
return nil, "missing or non-integer size", nil
|
||||
end
|
||||
if x < 0 or y < 0 or z < 0 then
|
||||
return nil, "negative size", nil
|
||||
end
|
||||
if x * y * z > max then
|
||||
return nil, "limit: " .. x .. " x " .. y .. " x " .. z .. " cells is over maxCells", nil
|
||||
end
|
||||
return x, y, z
|
||||
end
|
||||
|
||||
-- ints(v) -> the numbers of an intArray tag or a list of numbers | nil
|
||||
Common.ints = Mc.ints
|
||||
|
||||
-- blockEntity(c, style) / flatBlockEntity(be, style): codecs/model/Minecraft's (shared with model/mca)
|
||||
Common.blockEntity = Mc.blockEntity
|
||||
Common.flatBlockEntity = Mc.flatBlockEntity
|
||||
|
||||
-- list(v) -> v when it is an NBT list (array), else an empty list
|
||||
function Common.list(v: any): { any }
|
||||
if type(v) == "table" and not Nbt.isTag(v) and not Types.isObject(v) then
|
||||
return v
|
||||
end
|
||||
return {}
|
||||
end
|
||||
|
||||
-- compound({ { key, value } }) -> a Types.object with these keys in this order (nil values skipped)
|
||||
function Common.compound(fields: { { any } }): { [string]: any }
|
||||
local o, keys = {}, {}
|
||||
for _, kv in fields do
|
||||
if kv[2] ~= nil then
|
||||
o[kv[1]] = kv[2]
|
||||
table.insert(keys, kv[1])
|
||||
end
|
||||
end
|
||||
return Types.object(o, keys)
|
||||
end
|
||||
|
||||
-- merge(base, fields, drop?) -> a compound of fields ({ { key, value } }, nil values left out) laid out in base's key
|
||||
-- order (a decoded source compound: its other keys are kept, except those in drop), new keys after
|
||||
function Common.merge(base: any, fields: { { any } }, drop: { [string]: boolean }?): { [string]: any }
|
||||
if type(base) ~= "table" or not Types.isObject(base) then
|
||||
return Common.compound(fields)
|
||||
end
|
||||
local mine: { [string]: any } = {}
|
||||
local present: { [string]: boolean } = {}
|
||||
for _, kv in fields do
|
||||
present[kv[1]] = true
|
||||
mine[kv[1]] = kv[2]
|
||||
end
|
||||
local out = {}
|
||||
for _, k in Types.keys(base) do
|
||||
if present[k] then
|
||||
if mine[k] ~= nil then
|
||||
table.insert(out, { k, mine[k] })
|
||||
end
|
||||
elseif not (drop and drop[k]) then
|
||||
table.insert(out, { k, base[k] })
|
||||
end
|
||||
end
|
||||
local seen: { [string]: boolean } = {}
|
||||
for _, kv in out do
|
||||
seen[kv[1]] = true
|
||||
end
|
||||
for _, kv in fields do
|
||||
if not seen[kv[1]] and kv[2] ~= nil then
|
||||
table.insert(out, kv)
|
||||
end
|
||||
end
|
||||
return Common.compound(out)
|
||||
end
|
||||
|
||||
-- dedupe(states, indices, n) -> states, indices with every state once (indices remapped when needed)
|
||||
function Common.dedupe(states: { string }, indices: buffer, n: number): ({ string }, buffer)
|
||||
local first: { [string]: number } = {}
|
||||
local remap: { number } = {}
|
||||
local out: { string } = {}
|
||||
local dup = false
|
||||
for i, s in states do
|
||||
local at = first[s]
|
||||
if at == nil then
|
||||
table.insert(out, s)
|
||||
at = #out - 1
|
||||
first[s] = at
|
||||
else
|
||||
dup = true
|
||||
end
|
||||
remap[i] = at
|
||||
end
|
||||
if not dup then
|
||||
return states, indices
|
||||
end
|
||||
local res = buffer.create(n * 2)
|
||||
for i = 0, n - 1 do
|
||||
buffer.writeu16(res, i * 2, remap[buffer.readu16(indices, i * 2) + 1] or 0)
|
||||
end
|
||||
return out, res
|
||||
end
|
||||
|
||||
return Common
|
||||
@@ -0,0 +1,215 @@
|
||||
--!strict
|
||||
-- model/schematic/Litematic: Litematica's .litematic, from the mod's sources (LitematicaSchematic,
|
||||
-- LitematicaBitArray) as documented by the litemapy project: Metadata (name, author, description, enclosing size,
|
||||
-- counts, times, preview), several named Regions with a Position and a Size that may be negative, a block-state
|
||||
-- palette (index 0 = air) and BlockStates packed spanning long boundaries (at least 2 bits), TileEntities (relative
|
||||
-- to the region's minimum corner), Entities and pending ticks (kept as they are).
|
||||
local Types = require("@rosetta/Types")
|
||||
local Nbt = require("@rosetta/codecs/text/Nbt")
|
||||
local Mc = require("@rosetta/codecs/model/Minecraft")
|
||||
local Common = require("@rosetta/model/schematic/Common")
|
||||
|
||||
local Litematic = {}
|
||||
|
||||
local Packed = Mc.Packed
|
||||
|
||||
local function xyz(c: any): { number }?
|
||||
if type(c) ~= "table" then
|
||||
return nil
|
||||
end
|
||||
local x, y, z = Common.int(c.x), Common.int(c.y), Common.int(c.z)
|
||||
if not x or not y or not z then
|
||||
return nil
|
||||
end
|
||||
return { x, y, z }
|
||||
end
|
||||
|
||||
local function vec(v: { number }): any
|
||||
return Common.compound({ { "x", v[1] }, { "y", v[2] }, { "z", v[3] } })
|
||||
end
|
||||
|
||||
-- read(root, maxCells) -> { Region }, extra | nil, message
|
||||
function Litematic.read(root: any, maxCells: number): ({ Common.Region }?, any)
|
||||
if not Types.isObject(root.Regions) then
|
||||
return nil, "no Regions"
|
||||
end
|
||||
local out: { Common.Region } = {}
|
||||
local total = 0
|
||||
local raw: { [string]: any } = {}
|
||||
for _, name in Types.keys(root.Regions) do
|
||||
local reg = root.Regions[name]
|
||||
if type(reg) ~= "table" then
|
||||
return nil, "region " .. name .. " is not a compound"
|
||||
end
|
||||
local pos, size = xyz(reg.Position), xyz(reg.Size)
|
||||
if not pos or not size then
|
||||
return nil, "region " .. name .. ": no Position / Size"
|
||||
end
|
||||
local ax, ay, az = math.abs(size[1]), math.abs(size[2]), math.abs(size[3])
|
||||
local sx, sy, sz = Common.dims(ax, ay, az, maxCells - total)
|
||||
if not sx then
|
||||
return nil, "region " .. name .. ": " .. tostring(sy)
|
||||
end
|
||||
total += ax * ay * az
|
||||
local states: { string } = {}
|
||||
for i, c in Common.list(reg.BlockStatePalette) do
|
||||
local st = Mc.stateFromNbt(c)
|
||||
if not st then
|
||||
return nil, "region " .. name .. ": bad palette entry " .. i
|
||||
end
|
||||
states[i] = st
|
||||
end
|
||||
if #states == 0 then
|
||||
states[1] = "minecraft:air"
|
||||
end
|
||||
local n = ax * ay * az
|
||||
local yzx = buffer.create(n * 2)
|
||||
local bs = reg.BlockStates
|
||||
if n > 0 then
|
||||
if not (Nbt.isTag(bs) and bs.type == "longArray") then
|
||||
return nil, "region " .. name .. ": no BlockStates"
|
||||
end
|
||||
local bits = Packed.bitsFor(#states, 2)
|
||||
local ok, e = Packed.unpack(bs.value, bits, n, true, yzx, #states)
|
||||
if not ok then
|
||||
return nil, "region " .. name .. ": " .. tostring(e)
|
||||
end
|
||||
end
|
||||
local bes = {}
|
||||
for _, c in Common.list(reg.TileEntities) do
|
||||
local be = Common.blockEntity(c, "flat")
|
||||
if be then
|
||||
table.insert(bes, be)
|
||||
end
|
||||
end
|
||||
table.insert(out, {
|
||||
name = name,
|
||||
size = { ax, ay, az },
|
||||
position = {
|
||||
pos[1] + (if size[1] < 0 then size[1] + 1 else 0),
|
||||
pos[2] + (if size[2] < 0 then size[2] + 1 else 0),
|
||||
pos[3] + (if size[3] < 0 then size[3] + 1 else 0),
|
||||
},
|
||||
states = states,
|
||||
indices = Packed.fromYzx(yzx, ax, ay, az),
|
||||
blockEntities = bes,
|
||||
entities = Common.list(reg.Entities),
|
||||
})
|
||||
raw[name] = reg
|
||||
end
|
||||
return out, {
|
||||
version = Common.int(root.Version),
|
||||
subVersion = Common.int(root.SubVersion),
|
||||
dataVersion = Common.int(root.MinecraftDataVersion),
|
||||
metadata = root.Metadata,
|
||||
regions = raw,
|
||||
}
|
||||
end
|
||||
|
||||
-- write(regions, version, dataVersion, base?, name?) -> the root compound | nil, message. base: the decoded root
|
||||
-- (Metadata fields and each region's Position / Size signs and pending ticks are kept)
|
||||
function Litematic.write(regions: { Common.Region }, version: number, dataVersion: number, base: any, title: string?): (any, string?)
|
||||
local baseRegions = if type(base) == "table" and Types.isObject(base.Regions) then base.Regions else nil
|
||||
local regs, order = {}, {}
|
||||
local lo, hi = { math.huge, math.huge, math.huge }, { -math.huge, -math.huge, -math.huge }
|
||||
local blocks, volume = 0, 0
|
||||
for i, r in regions do
|
||||
local name = r.name or ("Region" .. (if i == 1 then "" else tostring(i)))
|
||||
while regs[name] ~= nil do
|
||||
name ..= "_"
|
||||
end
|
||||
local sx, sy, sz = r.size[1], r.size[2], r.size[3]
|
||||
local n = sx * sy * sz
|
||||
local states, indices = Common.dedupe(r.states, r.indices, n)
|
||||
if states[1] ~= "minecraft:air" then
|
||||
-- Litematica keeps air at index 0
|
||||
local at = table.find(states, "minecraft:air")
|
||||
local shifted = buffer.create(n * 2)
|
||||
local nst = table.clone(states)
|
||||
if at then
|
||||
nst[1], nst[at] = states[at], states[1]
|
||||
else
|
||||
table.insert(nst, 1, "minecraft:air")
|
||||
end
|
||||
for c = 0, n - 1 do
|
||||
local v = buffer.readu16(indices, c * 2)
|
||||
local w = if at then (if v == 0 then at - 1 elseif v == at - 1 then 0 else v) else v + 1
|
||||
buffer.writeu16(shifted, c * 2, w)
|
||||
end
|
||||
states, indices = nst, shifted
|
||||
end
|
||||
local palette = Nbt.list("compound")
|
||||
for k, st in states do
|
||||
palette[k] = Mc.stateToNbt(st)
|
||||
end
|
||||
local airLike: { [number]: boolean } = {}
|
||||
for k, st in states do
|
||||
airLike[k - 1] = Mc.isAir(st)
|
||||
end
|
||||
for c = 0, n - 1 do
|
||||
if not airLike[buffer.readu16(indices, c * 2)] then
|
||||
blocks += 1
|
||||
end
|
||||
end
|
||||
volume += n
|
||||
local bits = Packed.bitsFor(#states, 2)
|
||||
local longs = Packed.pack(Packed.toYzx(indices, sx, sy, sz), n, bits, true)
|
||||
local tiles = Nbt.list("compound")
|
||||
for _, be in r.blockEntities do
|
||||
table.insert(tiles, Common.flatBlockEntity(be, "flat"))
|
||||
end
|
||||
local p = r.position
|
||||
local pos, size = { p[1], p[2], p[3] }, { sx, sy, sz }
|
||||
local b = if baseRegions then baseRegions[name] else nil
|
||||
local bsize = if type(b) == "table" then xyz(b.Size) else nil
|
||||
if bsize and math.abs(bsize[1]) == sx and math.abs(bsize[2]) == sy and math.abs(bsize[3]) == sz then
|
||||
-- keep the source's corner and signs
|
||||
for k = 1, 3 do
|
||||
if bsize[k] < 0 then
|
||||
size[k] = bsize[k]
|
||||
pos[k] = p[k] - bsize[k] - 1
|
||||
end
|
||||
end
|
||||
end
|
||||
for k = 1, 3 do
|
||||
lo[k] = math.min(lo[k], p[k])
|
||||
hi[k] = math.max(hi[k], p[k] + r.size[k])
|
||||
end
|
||||
regs[name] = Common.merge(b, {
|
||||
{ "BlockStatePalette", palette },
|
||||
{ "BlockStates", Nbt.longArray(longs) },
|
||||
{ "Entities", if #r.entities > 0 then r.entities else Nbt.list("compound") },
|
||||
{ "PendingBlockTicks", if type(b) == "table" then b.PendingBlockTicks else Nbt.list("compound") },
|
||||
{ "PendingFluidTicks", if type(b) == "table" then b.PendingFluidTicks else Nbt.list("compound") },
|
||||
{ "Position", vec(pos) },
|
||||
{ "Size", vec(size) },
|
||||
{ "TileEntities", tiles },
|
||||
})
|
||||
table.insert(order, name)
|
||||
end
|
||||
if #order == 0 then
|
||||
lo, hi = { 0, 0, 0 }, { 0, 0, 0 }
|
||||
end
|
||||
local bm = if type(base) == "table" and type(base.Metadata) == "table" then base.Metadata else nil
|
||||
local metadata = Common.merge(bm, {
|
||||
{ "EnclosingSize", vec({ hi[1] - lo[1], hi[2] - lo[2], hi[3] - lo[3] }) },
|
||||
{ "Author", if bm and bm.Author then bm.Author else "" },
|
||||
{ "Description", if bm and bm.Description then bm.Description else "" },
|
||||
{ "Name", title or (if bm and bm.Name then bm.Name else "Unnamed") },
|
||||
{ "RegionCount", #order },
|
||||
{ "TimeCreated", if bm and bm.TimeCreated then bm.TimeCreated else Nbt.long(0) },
|
||||
{ "TimeModified", if bm and bm.TimeModified then bm.TimeModified else Nbt.long(0) },
|
||||
{ "TotalBlocks", blocks },
|
||||
{ "TotalVolume", volume },
|
||||
})
|
||||
local regions_ = Types.object(regs, order)
|
||||
return Common.merge(base, {
|
||||
{ "MinecraftDataVersion", dataVersion },
|
||||
{ "Version", version },
|
||||
{ "SubVersion", if type(base) == "table" and base.SubVersion ~= nil then base.SubVersion elseif version >= 6 then 1 else nil },
|
||||
{ "Metadata", metadata },
|
||||
{ "Regions", regions_ },
|
||||
}), nil
|
||||
end
|
||||
|
||||
return Litematic
|
||||
@@ -0,0 +1,249 @@
|
||||
--!strict
|
||||
-- model/schematic/Mcedit: the MCEdit / WorldEdit .schematic layout (Minecraft before 1.13), from the Minecraft wiki
|
||||
-- "Schematic file format" page: Width / Height / Length, Blocks + Data (+ WorldEdit's AddBlocks high nibbles, high
|
||||
-- nibble first), Materials = "Alpha", TileEntities, Entities, WEOffset*, and the SchematicaMapping / BlockIDs name
|
||||
-- tables of Schematica and MCEdit-Unified. Block ids become state strings through codecs/model/Minecraft
|
||||
-- (legacyState: wool 14 -> minecraft:red_wool) and back (legacyId; blocks unknown before 1.13 become the legacy block
|
||||
-- nearest in colour).
|
||||
local Types = require("@rosetta/Types")
|
||||
local Nbt = require("@rosetta/codecs/text/Nbt")
|
||||
local Mc = require("@rosetta/codecs/model/Minecraft")
|
||||
local Common = require("@rosetta/model/schematic/Common")
|
||||
|
||||
local Mcedit = {}
|
||||
|
||||
local Packed = Mc.Packed
|
||||
|
||||
-- read(root, maxCells) -> { Region }, extra | nil, message
|
||||
function Mcedit.read(root: any, maxCells: number): ({ Common.Region }?, any)
|
||||
local blocks = root.Blocks
|
||||
if type(blocks) ~= "buffer" then
|
||||
return nil, "no Blocks byte array"
|
||||
end
|
||||
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(root.Width), u16(root.Height), u16(root.Length), maxCells)
|
||||
if not sx then
|
||||
return nil, sy
|
||||
end
|
||||
local n = sx * sy :: number * (sz :: number)
|
||||
if buffer.len(blocks) < n then
|
||||
return nil, "truncated: Blocks has " .. buffer.len(blocks) .. " bytes for " .. n .. " cells"
|
||||
end
|
||||
local data = if type(root.Data) == "buffer" and buffer.len(root.Data) >= n then root.Data else nil
|
||||
local add = if type(root.AddBlocks) == "buffer" and buffer.len(root.AddBlocks) >= (n + 1) // 2 then root.AddBlocks else nil
|
||||
-- id -> name tables of Schematica (name -> id) and MCEdit-Unified (id text -> name)
|
||||
local names: { [number]: string } = {}
|
||||
if Types.isObject(root.SchematicaMapping) then
|
||||
for _, k in Types.keys(root.SchematicaMapping) do
|
||||
local id = Common.int(root.SchematicaMapping[k])
|
||||
if id then
|
||||
names[id] = k
|
||||
end
|
||||
end
|
||||
end
|
||||
if Types.isObject(root.BlockIDs) then
|
||||
for _, k in Types.keys(root.BlockIDs) do
|
||||
local id = tonumber(k)
|
||||
if id and type(root.BlockIDs[k]) == "string" then
|
||||
names[id] = root.BlockIDs[k]
|
||||
end
|
||||
end
|
||||
end
|
||||
local function state(id: number, d: number): string
|
||||
local name = names[id]
|
||||
if name then
|
||||
local base = string.match(name, "^minecraft:(.*)$") or (if string.find(name, ":", 1, true) then nil else name)
|
||||
if base then
|
||||
local std = table.find(Mc.Legacy.NAMES, base)
|
||||
if std then
|
||||
return Mc.legacyState(std - 1, d)
|
||||
end
|
||||
end
|
||||
return Mc.stateString(if string.find(name, ":", 1, true) then name else "minecraft:" .. name, if d ~= 0 then { { "data", tostring(d) } } else nil)
|
||||
end
|
||||
return Mc.legacyState(id, d)
|
||||
end
|
||||
local slots: { [number]: number } = {}
|
||||
local states: { string } = {}
|
||||
local yzx = buffer.create(n * 2)
|
||||
for i = 0, n - 1 do
|
||||
local id = buffer.readu8(blocks, i)
|
||||
if add then
|
||||
id += Packed.nibble(add, 0, i, true) * 256
|
||||
end
|
||||
local d = if data then buffer.readu8(data, i) % 16 else 0
|
||||
local key = id * 16 + d
|
||||
local s = slots[key]
|
||||
if not s then
|
||||
table.insert(states, state(id, d))
|
||||
s = #states - 1
|
||||
slots[key] = s
|
||||
end
|
||||
buffer.writeu16(yzx, i * 2, s :: number)
|
||||
end
|
||||
local bes = {}
|
||||
for _, c in Common.list(root.TileEntities) do
|
||||
local be = Common.blockEntity(c, "flat")
|
||||
if be then
|
||||
table.insert(bes, be)
|
||||
end
|
||||
end
|
||||
local regions: { Common.Region } = {
|
||||
{
|
||||
name = nil,
|
||||
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),
|
||||
},
|
||||
}
|
||||
return regions, { materials = root.Materials }
|
||||
end
|
||||
|
||||
-- the legacy blocks a colour can become (full blocks of distinct colours)
|
||||
local CANDIDATES: { { any } }? = nil
|
||||
local function candidates(): { { any } }
|
||||
if CANDIDATES then
|
||||
return CANDIDATES
|
||||
end
|
||||
local out = {}
|
||||
local function add(id: number, d: number)
|
||||
local c = Mc.color(Mc.legacyState(id, d))
|
||||
if c then
|
||||
table.insert(out, { id, d, c })
|
||||
end
|
||||
end
|
||||
for d = 0, 15 do
|
||||
add(35, d) -- wool
|
||||
add(159, d) -- stained clay
|
||||
add(251, d) -- concrete
|
||||
end
|
||||
for d = 0, 5 do
|
||||
add(5, d) -- planks
|
||||
end
|
||||
for _, id in { 1, 2, 3, 4, 7, 12, 13, 24, 41, 42, 45, 48, 49, 57, 80, 82, 87, 88, 89, 98, 121, 133, 152, 155, 168, 172, 173, 174, 179, 201, 216 } do
|
||||
add(id, 0)
|
||||
end
|
||||
CANDIDATES = out
|
||||
return out
|
||||
end
|
||||
|
||||
local function nearest(state: string): (number, number)
|
||||
local c = Mc.color(state)
|
||||
if not c then
|
||||
return 0, 0
|
||||
end
|
||||
if c[4] < 200 then
|
||||
return 20, 0 -- glass
|
||||
end
|
||||
local bid, bd, best = 1, 0, math.huge
|
||||
for _, k in candidates() do
|
||||
local kc = k[3]
|
||||
local dist = (kc[1] - c[1]) ^ 2 + (kc[2] - c[2]) ^ 2 + (kc[3] - c[3]) ^ 2
|
||||
if dist < best then
|
||||
bid, bd, best = k[1], k[2], dist
|
||||
end
|
||||
end
|
||||
return bid, bd
|
||||
end
|
||||
|
||||
-- write(region, base?) -> the root compound (named "Schematic") | nil, message. base: a decoded root whose other
|
||||
-- fields are kept
|
||||
function Mcedit.write(r: Common.Region, 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 .schematic is at most 65535 cells on each axis"
|
||||
end
|
||||
local n = sx * sy * sz
|
||||
-- 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
|
||||
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 :: number, d :: number
|
||||
if (id :: number) > 255 then
|
||||
high = true
|
||||
end
|
||||
end
|
||||
local yzx = Packed.toYzx(r.indices, sx, sy, sz)
|
||||
local blocks, data = buffer.create(n), buffer.create(n)
|
||||
local add = if high then buffer.create((n + 1) // 2) else nil
|
||||
for i = 0, n - 1 do
|
||||
local s = buffer.readu16(yzx, i * 2) + 1
|
||||
local id = ids[s] or 0
|
||||
buffer.writeu8(blocks, i, id % 256)
|
||||
buffer.writeu8(data, i, datas[s] or 0)
|
||||
if add then
|
||||
Packed.setNibble(add, 0, i, id // 256, true)
|
||||
end
|
||||
end
|
||||
local tiles = Nbt.list("compound")
|
||||
for _, be in r.blockEntities do
|
||||
table.insert(tiles, Common.flatBlockEntity(be, "flat"))
|
||||
end
|
||||
local ents = if #r.entities > 0 then r.entities else Nbt.list("compound")
|
||||
local fields: { { any } } = {
|
||||
{ "Width", Nbt.short(sx) },
|
||||
{ "Height", Nbt.short(sy) },
|
||||
{ "Length", Nbt.short(sz) },
|
||||
{ "Materials", "Alpha" },
|
||||
{ "Blocks", blocks },
|
||||
{ "Data", data },
|
||||
{ "AddBlocks", add },
|
||||
{ "Entities", ents },
|
||||
{ "TileEntities", tiles },
|
||||
}
|
||||
local p = r.position
|
||||
if p[1] ~= 0 or p[2] ~= 0 or p[3] ~= 0 or (type(base) == "table" and base.WEOffsetX ~= nil) then
|
||||
table.insert(fields, { "WEOffsetX", p[1] })
|
||||
table.insert(fields, { "WEOffsetY", p[2] })
|
||||
table.insert(fields, { "WEOffsetZ", p[3] })
|
||||
end
|
||||
local root = Common.merge(base, fields, { Add = true })
|
||||
return root, nil
|
||||
end
|
||||
|
||||
return Mcedit
|
||||
@@ -0,0 +1,131 @@
|
||||
--!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("@rosetta/Types")
|
||||
local Nbt = require("@rosetta/codecs/text/Nbt")
|
||||
local Mc = require("@rosetta/codecs/model/Minecraft")
|
||||
local Common = require("@rosetta/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
|
||||
@@ -0,0 +1,334 @@
|
||||
--!strict
|
||||
-- model/schematic: Minecraft schematics: MCEdit / WorldEdit .schematic (before 1.13), Sponge .schem (versions 1, 2, 3:
|
||||
-- WorldEdit 7+) and Litematica .litematic, read and write. New code over codecs/text/Nbt (gzip NBT) and
|
||||
-- codecs/model/Minecraft (block states, colours, legacy ids, packed arrays); the layouts: model/schematic/Mcedit,
|
||||
-- Sponge, Litematic.
|
||||
-- decode -> a Scene (meta.up = "y", one unit = one block): scene.voxels holds one Types.Voxels per region (a .litematic
|
||||
-- has several), each with voxels.blocks (the exact block states, u16 per cell, plus block entities and entities as
|
||||
-- NBT) and data / palette coloured from the default block colour table; a node per region places it (translation =
|
||||
-- the region's minimum corner: Sponge Offset, WorldEdit WEOffset, Litematica Position). meta.source keeps the
|
||||
-- variant, version, DataVersion, Metadata and the whole root NBT (source.nbt).
|
||||
-- encode <- a Scene with voxels or one Types.Voxels: opts.variant (or the file name's extension, or the source's)
|
||||
-- "schem" (default, version 3) / "schematic" / "litematic". Grids without blocks (a .vox) get, per palette colour,
|
||||
-- the block nearest in colour. Several regions are merged into one grid for .schem / .schematic. Fields of the
|
||||
-- source that are not blocks (Metadata, biomes, pending ticks, ...) are written back when the variant is the same.
|
||||
-- Not supported: converting block properties to pre-1.13 data values (legacy writes keep the data a state came with
|
||||
-- or 0), Sponge biome conversion between versions, rotated nodes (only translations place a grid).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Nbt = require("@rosetta/codecs/text/Nbt")
|
||||
local Mc = require("@rosetta/codecs/model/Minecraft")
|
||||
local Common = require("@rosetta/model/schematic/Common")
|
||||
local Mcedit = require("@rosetta/model/schematic/Mcedit")
|
||||
local Sponge = require("@rosetta/model/schematic/Sponge")
|
||||
local Litematic = require("@rosetta/model/schematic/Litematic")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Schematic = {
|
||||
name = "schematic",
|
||||
category = "model",
|
||||
extensions = { "schem", "schematic", "litematic" },
|
||||
mime = { "application/x-minecraft-schematic" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/text/Nbt", "codecs/model/Minecraft" },
|
||||
caps = { decode = true, encode = true, voxels = true, blocks = true, variants = { "schem", "schematic", "litematic" } },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
maxCells: number?, -- refuse schematics with more cells in total (default 2^26)
|
||||
maxBytes: number?, -- refuse NBT that inflates past this (default 2^28)
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
variant: ("schem" | "schematic" | "litematic")?, -- default: the extension of opts.name, the source's, else "schem"
|
||||
version: number?, -- Sponge 1 / 2 / 3 (default 3, or the source's), Litematica version (default 6, or the source's)
|
||||
dataVersion: number?, -- Minecraft's DataVersion (default the source's, else 3953 = 1.21)
|
||||
name: string?, -- the file name (its extension picks the variant); for .litematic also the default Metadata.Name
|
||||
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
|
||||
local p = Nbt.peek(r.buf, r.base, r.limit - r.base)
|
||||
if not p or p.rootType ~= "compound" or p.endian ~= "big" then
|
||||
return 0
|
||||
end
|
||||
if p.name == "Schematic" or (p.firstName == "Schematic" and p.firstType == "compound") then
|
||||
return 1
|
||||
end
|
||||
local head = buffer.tostring(p.head)
|
||||
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
|
||||
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
|
||||
-- variantOf(root, name) -> "schem" | "schematic" | "litematic", the compound holding the Sponge fields | nil
|
||||
local function variantOf(root: any): (string?, any)
|
||||
if type(root) ~= "table" or not Types.isObject(root) then
|
||||
return nil, nil
|
||||
end
|
||||
if Types.isObject(root.Regions) then
|
||||
return "litematic", root
|
||||
end
|
||||
if Types.isObject(root.Schematic) and (Common.int(root.Schematic.Version) or 0) >= 3 then
|
||||
return "schem", root.Schematic
|
||||
end
|
||||
if root.Version ~= nil and (root.Palette ~= nil or root.BlockData ~= nil) then
|
||||
return "schem", root
|
||||
end
|
||||
if type(root.Blocks) == "buffer" then
|
||||
return "schematic", root
|
||||
end
|
||||
return nil, nil
|
||||
end
|
||||
|
||||
function Schematic.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
|
||||
local o: DecodeOptions = opts or {}
|
||||
local root, info = Nbt.decode(r.buf, { maxBytes = o.maxBytes, endian = "big" }, r.base, r.limit - r.base)
|
||||
if root == nil then
|
||||
return nil, info
|
||||
end
|
||||
local variant, s = variantOf(root)
|
||||
if not variant then
|
||||
return nil, Err.new(Err.CORRUPT, "not a schematic (no Regions, Schematic / Palette or Blocks)")
|
||||
end
|
||||
local maxCells = o.maxCells or 2 ^ 26
|
||||
local regions, extra
|
||||
if variant == "litematic" then
|
||||
regions, extra = Litematic.read(s, maxCells)
|
||||
elseif variant == "schem" then
|
||||
regions, extra = Sponge.read(s, maxCells)
|
||||
else
|
||||
regions, extra = Mcedit.read(s, maxCells)
|
||||
end
|
||||
if not regions then
|
||||
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 = {}
|
||||
for i, reg in regions do
|
||||
local v = Mc.voxels(reg.size[1], reg.size[2], reg.size[3], reg.states, reg.indices)
|
||||
v.name = reg.name
|
||||
local b = v.blocks :: Types.VoxelBlocks
|
||||
b.blockEntities = reg.blockEntities
|
||||
b.entities = reg.entities
|
||||
table.insert(scene.voxels :: { Types.Voxels }, v)
|
||||
table.insert(scene.nodes, { name = reg.name, children = {}, voxels = i, translation = { reg.position[1], reg.position[2], reg.position[3] } })
|
||||
table.insert(scene.roots, #scene.nodes)
|
||||
end
|
||||
scene.meta.up = "y"
|
||||
scene.meta.unit = 1
|
||||
local src: { [string]: any } = { format = "schematic", variant = variant, nbt = root, rootName = info.name, compression = info.compression }
|
||||
for k, v in extra do
|
||||
src[k] = v
|
||||
end
|
||||
scene.meta.source = src :: any
|
||||
return scene, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
local function regionsOf(value: any): ({ Common.Region }?, string?)
|
||||
local out: { Common.Region } = {}
|
||||
local function add(v: Types.Voxels, pos: { number }, name: string?): string?
|
||||
if type(v) ~= "table" or type(v.size) ~= "table" or type(v.data) ~= "buffer" then
|
||||
return "expected Types.Voxels grids"
|
||||
end
|
||||
local states, indices = Mc.blocksOf(v)
|
||||
if not states then
|
||||
return indices
|
||||
end
|
||||
local b = v.blocks
|
||||
table.insert(out, {
|
||||
name = name or v.name,
|
||||
size = { v.size[1], v.size[2], v.size[3] },
|
||||
position = { math.floor(pos[1] + 0.5), math.floor(pos[2] + 0.5), math.floor(pos[3] + 0.5) },
|
||||
states = states,
|
||||
indices = indices,
|
||||
blockEntities = if b and b.blockEntities then b.blockEntities else {},
|
||||
entities = if b and b.entities then b.entities else {},
|
||||
})
|
||||
return nil
|
||||
end
|
||||
if type(value) == "table" and value.kind == "voxels" then
|
||||
local e = add(value, { 0, 0, 0 }, nil)
|
||||
return if e then nil else out, e
|
||||
end
|
||||
if type(value) ~= "table" or value.kind ~= "scene" or type(value.voxels) ~= "table" or #value.voxels == 0 then
|
||||
return nil, "expected a Scene with voxels or a Types.Voxels"
|
||||
end
|
||||
local placed = false
|
||||
local function walk(i: number, base: { number }, depth: number): string?
|
||||
local node = value.nodes[i]
|
||||
if type(node) ~= "table" or depth > 64 then
|
||||
return nil
|
||||
end
|
||||
local t = node.translation or { 0, 0, 0 }
|
||||
local p = { base[1] + (t[1] or 0), base[2] + (t[2] or 0), base[3] + (t[3] or 0) }
|
||||
if node.voxels and value.voxels[node.voxels] then
|
||||
placed = true
|
||||
local e = add(value.voxels[node.voxels], p, node.name)
|
||||
if e then
|
||||
return e
|
||||
end
|
||||
end
|
||||
for _, c in node.children or {} do
|
||||
local e = walk(c, p, depth + 1)
|
||||
if e then
|
||||
return e
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
for _, root in value.roots or {} do
|
||||
local e = walk(root, { 0, 0, 0 }, 0)
|
||||
if e then
|
||||
return nil, e
|
||||
end
|
||||
end
|
||||
if not placed then
|
||||
for _, v in value.voxels do
|
||||
local e = add(v, { 0, 0, 0 }, nil)
|
||||
if e then
|
||||
return nil, e
|
||||
end
|
||||
end
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- merge(regions) -> one region covering them all (air where none is)
|
||||
local function merge(regions: { Common.Region }): Common.Region
|
||||
if #regions == 1 then
|
||||
return regions[1]
|
||||
end
|
||||
local lo, hi = { math.huge, math.huge, math.huge }, { -math.huge, -math.huge, -math.huge }
|
||||
for _, r in regions do
|
||||
for k = 1, 3 do
|
||||
lo[k] = math.min(lo[k], r.position[k])
|
||||
hi[k] = math.max(hi[k], r.position[k] + r.size[k])
|
||||
end
|
||||
end
|
||||
local sx, sy, sz = hi[1] - lo[1], hi[2] - lo[2], hi[3] - lo[3]
|
||||
local states = { "minecraft:air" }
|
||||
local out = buffer.create(sx * sy * sz * 2)
|
||||
local bes, ents = {}, {}
|
||||
for _, r in regions do
|
||||
local base = #states
|
||||
for _, st in r.states do
|
||||
table.insert(states, st)
|
||||
end
|
||||
local rx, ry, rz = r.size[1], r.size[2], r.size[3]
|
||||
local ox, oy, oz = r.position[1] - lo[1], r.position[2] - lo[2], r.position[3] - lo[3]
|
||||
for z = 0, rz - 1 do
|
||||
for y = 0, ry - 1 do
|
||||
local s = (rx * (y + ry * z)) * 2
|
||||
local d = (ox + sx * ((oy + y) + sy * (oz + z))) * 2
|
||||
for _ = 0, rx - 1 do
|
||||
buffer.writeu16(out, d, buffer.readu16(r.indices, s) + base)
|
||||
s += 2
|
||||
d += 2
|
||||
end
|
||||
end
|
||||
end
|
||||
for _, be in r.blockEntities do
|
||||
table.insert(bes, { pos = { be.pos[1] + ox, be.pos[2] + oy, be.pos[3] + oz }, id = be.id, data = be.data })
|
||||
end
|
||||
for _, e in r.entities do
|
||||
table.insert(ents, e)
|
||||
end
|
||||
end
|
||||
local st, idx = Common.dedupe(states, out, sx * sy * sz)
|
||||
return { name = regions[1].name, size = { sx, sy, sz }, position = lo, states = st, indices = idx, blockEntities = bes, entities = ents }
|
||||
end
|
||||
|
||||
function Schematic.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
local o: EncodeOptions = opts or {}
|
||||
local regions, rerr = regionsOf(value)
|
||||
if not regions then
|
||||
return nil, Err.new(Err.ARG, tostring(rerr))
|
||||
end
|
||||
local src = if type(value) == "table" and type(value.meta) == "table" and type(value.meta.source) == "table" and value.meta.source.format == "schematic" then value.meta.source else nil
|
||||
local ext = if o.name then string.lower(string.match(o.name, "%.([%w]+)$") or "") else nil
|
||||
local key = o.format and string.lower(o.format)
|
||||
local variant = o.variant
|
||||
or (if ext == "schem" or ext == "schematic" or ext == "litematic" then ext else nil)
|
||||
or (if key == "schem" or key == "litematic" then key else nil)
|
||||
or (src and src.variant)
|
||||
or "schem"
|
||||
if variant ~= "schem" and variant ~= "schematic" and variant ~= "litematic" then
|
||||
return nil, Err.new(Err.ARG, "unknown variant " .. tostring(variant))
|
||||
end
|
||||
local same = src ~= nil and src.variant == variant
|
||||
local base = if same then (src :: any).nbt else nil
|
||||
local dataVersion = o.dataVersion or (src and src.dataVersion) or DEFAULT_DATA_VERSION
|
||||
local root, rootName, e
|
||||
if variant == "litematic" then
|
||||
local version = o.version or (if same then (src :: any).version else nil) or 6
|
||||
local title = o.title or (if o.name then string.match(o.name, "([^/\\]-)%.[%w]+$") else nil)
|
||||
root, e = Litematic.write(regions, version, dataVersion, base, title)
|
||||
rootName = ""
|
||||
elseif variant == "schem" then
|
||||
local version = o.version or (if same then (src :: any).version else nil) or 3
|
||||
if version ~= 1 and version ~= 2 and version ~= 3 then
|
||||
return nil, Err.new(Err.ARG, "Sponge schematic version must be 1, 2 or 3")
|
||||
end
|
||||
local sameVersion = same and (src :: any).version == version
|
||||
local inner = if sameVersion and base then (if version >= 3 then base.Schematic else base) else nil
|
||||
local sch
|
||||
sch, e = Sponge.write(merge(regions), version, dataVersion, inner)
|
||||
if sch and version >= 3 then
|
||||
root, rootName = Common.merge(if sameVersion then base else nil, { { "Schematic", sch } }), ""
|
||||
else
|
||||
root, rootName = sch, "Schematic"
|
||||
end
|
||||
else
|
||||
root, e = Mcedit.write(merge(regions), base)
|
||||
rootName = "Schematic"
|
||||
end
|
||||
if not root then
|
||||
return nil, Err.new(Err.ARG, tostring(e))
|
||||
end
|
||||
if same and (src :: any).rootName ~= nil then
|
||||
rootName = (src :: any).rootName
|
||||
end
|
||||
return Nbt.encode(root, { name = rootName, endian = "big", compression = o.compression or "gzip", level = o.level })
|
||||
end
|
||||
|
||||
return Schematic
|
||||
Reference in new issue
Block a user