ws-voxel: model/mca (Anvil regions read-only: gzip / zlib / LZ4Block / stored / external .mcc chunks; 1.18+, 1.13-1.17 and pre-1.13 chunk layouts) into Voxels with exact block states; block entity shapes moved into codecs/model/Minecraft
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
1eeca02fed
commit
4cbde068d2
9 files changed
+1032
-80
No files matched your search
@@ -410,6 +410,18 @@ local list: { Entry } = {
|
||||
return require("@formats/model/gltf")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "mca",
|
||||
category = "model",
|
||||
path = "model/mca",
|
||||
extensions = { "mca", "mcr" },
|
||||
mime = { "application/x-minecraft-region" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/compress/Deflate", "codecs/text/Nbt", "codecs/model/Minecraft", "codecs/compress/Lz4" },
|
||||
caps = { decode = true, encode = false, voxels = true, blocks = true },
|
||||
load = function()
|
||||
return require("@formats/model/mca")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "obj",
|
||||
category = "model",
|
||||
|
||||
@@ -497,4 +497,91 @@ function Minecraft.blocksOf(v: Types.Voxels): ({ string }?, any)
|
||||
return states, out
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ block entities
|
||||
-- Block entity records in one shape, { pos = { x, y, z }, id, data } (Sponge v3's), from / to the flat
|
||||
-- { x, y, z, id, ... } of MCEdit, Litematica and chunks, and Sponge v1 / v2's { Pos, Id, ... }.
|
||||
local function int(v: any): number?
|
||||
local n = Nbt.number(v)
|
||||
if n == nil or n ~= n or n % 1 ~= 0 then
|
||||
return nil
|
||||
end
|
||||
return n
|
||||
end
|
||||
local function 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 :: any)[1] ~= nil then
|
||||
local out: { number } = {}
|
||||
for i, x in v :: { any } do
|
||||
out[i] = Nbt.number(x) or 0
|
||||
end
|
||||
return out
|
||||
end
|
||||
return nil
|
||||
end
|
||||
Minecraft.int = int -- the integer value of an NBT number | nil
|
||||
Minecraft.ints = ints -- the numbers of an intArray or a list | nil
|
||||
|
||||
local function rest(c: any, skip: { [string]: boolean }): { [string]: any }
|
||||
local out, keys = {}, {}
|
||||
for _, k in Types.keys(c) do
|
||||
if not skip[k] then
|
||||
out[k] = c[k]
|
||||
table.insert(keys, k)
|
||||
end
|
||||
end
|
||||
return Types.object(out, keys)
|
||||
end
|
||||
|
||||
local FLAT_SKIP = { x = true, y = true, z = true, id = true }
|
||||
local POS_SKIP = { Pos = true, Id = true }
|
||||
|
||||
-- blockEntity(c, style) -> the common shape of one record: style "flat" (MCEdit, Litematica), "pos" (Sponge v1 / v2),
|
||||
-- "data" (Sponge v3)
|
||||
function Minecraft.blockEntity(c: any, style: string): any
|
||||
if type(c) ~= "table" or Nbt.isTag(c) then
|
||||
return nil
|
||||
end
|
||||
if style == "flat" then
|
||||
return { pos = { int(c.x) or 0, int(c.y) or 0, int(c.z) or 0 }, id = if type(c.id) == "string" then c.id else nil, data = rest(c, FLAT_SKIP) }
|
||||
end
|
||||
local p = ints(c.Pos) or { 0, 0, 0 }
|
||||
local data = if style == "data" then (if type(c.Data) == "table" then c.Data else Types.object()) else rest(c, POS_SKIP)
|
||||
return { pos = { p[1] or 0, p[2] or 0, p[3] or 0 }, id = if type(c.Id) == "string" then c.Id else nil, data = data }
|
||||
end
|
||||
|
||||
-- flatBlockEntity(be, style) -> the record in a flavour's shape (the inverse of blockEntity)
|
||||
function Minecraft.flatBlockEntity(be: any, style: string): any
|
||||
local o = Types.object()
|
||||
local data = if type(be.data) == "table" then be.data else Types.object()
|
||||
if style == "flat" then
|
||||
Types.set(o, "x", be.pos[1])
|
||||
Types.set(o, "y", be.pos[2])
|
||||
Types.set(o, "z", be.pos[3])
|
||||
if be.id then
|
||||
Types.set(o, "id", be.id)
|
||||
end
|
||||
for _, k in Types.keys(data) do
|
||||
if not FLAT_SKIP[k] then
|
||||
Types.set(o, k, data[k])
|
||||
end
|
||||
end
|
||||
return o
|
||||
end
|
||||
Types.set(o, "Pos", Nbt.intArray({ be.pos[1], be.pos[2], be.pos[3] }))
|
||||
if be.id then
|
||||
Types.set(o, "Id", be.id)
|
||||
end
|
||||
if style == "data" then
|
||||
Types.set(o, "Data", data)
|
||||
else
|
||||
for _, k in Types.keys(data) do
|
||||
if not POS_SKIP[k] then
|
||||
Types.set(o, k, data[k])
|
||||
end
|
||||
end
|
||||
end
|
||||
return o
|
||||
end
|
||||
|
||||
return Minecraft
|
||||
@@ -0,0 +1,135 @@
|
||||
--!strict
|
||||
-- model/mca/Chunk: the blocks of one Anvil chunk's NBT, from the Minecraft wiki "Chunk format" page, in the three
|
||||
-- layouts it has had: 1.18+ (root sections[] with block_states { palette, data }, non-spanning packing, at least
|
||||
-- 4 bits, yPos), 1.13 - 1.17 (Level.Sections[] with Palette + BlockStates, spanning before DataVersion 2529 =
|
||||
-- 20w17a), and before 1.13 (Level.Sections[] with Blocks / Add / Data nibble arrays, numeric ids through
|
||||
-- codecs/model/Minecraft legacyState). Each section is 16 x 16 x 16 cells in Minecraft's order (x, then z, then y).
|
||||
local Types = require("@formats/Types")
|
||||
local Nbt = require("@formats/codecs/text/Nbt")
|
||||
local Mc = require("@formats/codecs/model/Minecraft")
|
||||
|
||||
local Chunk = {}
|
||||
|
||||
local Packed = Mc.Packed
|
||||
|
||||
export type Section = { y: number, states: { string }, cells: buffer } -- cells: u16 per cell, 4096, x-z-y order
|
||||
export type Blocks = {
|
||||
x: number, -- chunk coordinates (xPos / zPos)
|
||||
z: number,
|
||||
dataVersion: number?,
|
||||
sections: { Section },
|
||||
blockEntities: { any }, -- the chunk's records as they are (absolute x, y, z)
|
||||
entities: { any }, -- entities stored in the chunk (before 1.17)
|
||||
}
|
||||
|
||||
local function int(v: any): number?
|
||||
local n = Nbt.number(v)
|
||||
return if n and n == n and n % 1 == 0 then n else nil
|
||||
end
|
||||
|
||||
local function list(v: any): { any }
|
||||
return if type(v) == "table" and not Nbt.isTag(v) and not Types.isObject(v) then v else {}
|
||||
end
|
||||
|
||||
-- a palette section (1.13+): states from the palette compounds, cells from the packed array
|
||||
local function paletted(y: number, palette: { any }, data: any, minBits: number, spanning: boolean): (Section?, string?)
|
||||
local states = table.create(#palette) :: { string }
|
||||
for i, c in palette do
|
||||
local s = Mc.stateFromNbt(c)
|
||||
if not s then
|
||||
return nil, "section " .. y .. ": bad palette entry " .. i
|
||||
end
|
||||
states[i] = s
|
||||
end
|
||||
if #states == 0 then
|
||||
return nil, nil
|
||||
end
|
||||
local cells = buffer.create(8192)
|
||||
if #states > 1 or data ~= nil then
|
||||
if not (Nbt.isTag(data) and data.type == "longArray") then
|
||||
return nil, "section " .. y .. ": no block state data"
|
||||
end
|
||||
local bits = Packed.bitsFor(#states, minBits)
|
||||
local ok, e = Packed.unpack(data.value, bits, 4096, spanning, cells, #states)
|
||||
if not ok then
|
||||
return nil, "section " .. y .. ": " .. tostring(e)
|
||||
end
|
||||
end
|
||||
return { y = y, states = states, cells = cells }, nil
|
||||
end
|
||||
|
||||
-- blocks(root) -> Blocks | nil, message: the sections of a decoded chunk
|
||||
function Chunk.blocks(root: any): (Blocks?, string?)
|
||||
if type(root) ~= "table" or not Types.isObject(root) then
|
||||
return nil, "chunk NBT is not a compound"
|
||||
end
|
||||
local dv = int(root.DataVersion)
|
||||
local level = if Types.isObject(root.Level) then root.Level else root
|
||||
local x, z = int(level.xPos), int(level.zPos)
|
||||
if not x or not z then
|
||||
return nil, "chunk without xPos / zPos"
|
||||
end
|
||||
local out: Blocks = { x = x, z = z, dataVersion = dv, sections = {}, blockEntities = {}, entities = list(level.Entities) }
|
||||
if root.sections ~= nil then
|
||||
-- 1.18+
|
||||
for _, s in list(root.sections) do
|
||||
local y = int(s.Y)
|
||||
local bs = s.block_states
|
||||
if y and type(bs) == "table" and not Nbt.isTag(bs) then
|
||||
local sec, e = paletted(y, list(bs.palette), bs.data, 4, false)
|
||||
if e then
|
||||
return nil, e
|
||||
end
|
||||
if sec then
|
||||
table.insert(out.sections, sec)
|
||||
end
|
||||
end
|
||||
end
|
||||
out.blockEntities = list(root.block_entities)
|
||||
return out, nil
|
||||
end
|
||||
for _, s in list(level.Sections) do
|
||||
local y = int(s.Y)
|
||||
if y then
|
||||
if s.Palette ~= nil then
|
||||
local sec, e = paletted(y, list(s.Palette), s.BlockStates, 4, (dv or 0) < 2529)
|
||||
if e then
|
||||
return nil, e
|
||||
end
|
||||
if sec then
|
||||
table.insert(out.sections, sec)
|
||||
end
|
||||
elseif type(s.Blocks) == "buffer" then
|
||||
local blocks = s.Blocks :: buffer
|
||||
if buffer.len(blocks) < 4096 then
|
||||
return nil, "section " .. y .. ": short Blocks array"
|
||||
end
|
||||
local data = if type(s.Data) == "buffer" and buffer.len(s.Data) >= 2048 then s.Data else nil
|
||||
local add = if type(s.Add) == "buffer" and buffer.len(s.Add) >= 2048 then s.Add else nil
|
||||
local slots: { [number]: number } = {}
|
||||
local states: { string } = {}
|
||||
local cells = buffer.create(8192)
|
||||
for i = 0, 4095 do
|
||||
local id = buffer.readu8(blocks, i)
|
||||
if add then
|
||||
id += Packed.nibble(add, 0, i, false) * 256
|
||||
end
|
||||
local d = if data then Packed.nibble(data, 0, i, false) else 0
|
||||
local key = id * 16 + d
|
||||
local slot = slots[key]
|
||||
if not slot then
|
||||
table.insert(states, Mc.legacyState(id, d))
|
||||
slot = #states - 1
|
||||
slots[key] = slot
|
||||
end
|
||||
buffer.writeu16(cells, i * 2, slot :: number)
|
||||
end
|
||||
table.insert(out.sections, { y = y, states = states, cells = cells })
|
||||
end
|
||||
end
|
||||
end
|
||||
out.blockEntities = list(level.TileEntities)
|
||||
return out, nil
|
||||
end
|
||||
|
||||
return Chunk
|
||||
@@ -0,0 +1,70 @@
|
||||
--!strict
|
||||
-- model/mca/Lz4Block: the lz4-java LZ4BlockOutputStream framing Minecraft (24w04a+) uses for compression type 4
|
||||
-- chunks, from lz4-java's LZ4BlockOutputStream source: blocks of "LZ4Block", a token (method 0x10 raw / 0x20 LZ4 |
|
||||
-- level), compressed and decompressed lengths and an XXH32 checksum (little-endian i32s; not verified), then the
|
||||
-- block, each block independent; an empty block ends the stream. The blocks decode through codecs/compress/Lz4.
|
||||
local Lz4 = require("@formats/codecs/compress/Lz4")
|
||||
|
||||
local Lz4Block = {}
|
||||
|
||||
local MAGIC = "LZ4Block"
|
||||
|
||||
-- decode(buf, offset, length, maxBytes) -> buffer | nil, message
|
||||
function Lz4Block.decode(b: buffer, offset: number, length: number, maxBytes: number): (buffer?, string?)
|
||||
local p, e = offset, offset + length
|
||||
local parts, total = {}, 0
|
||||
while true do
|
||||
if p + 21 > e then
|
||||
if #parts > 0 and p >= e then
|
||||
break -- some writers stop without the empty end block
|
||||
end
|
||||
return nil, "truncated LZ4 block header"
|
||||
end
|
||||
if buffer.readstring(b, p, 8) ~= MAGIC then
|
||||
return nil, "bad LZ4Block magic"
|
||||
end
|
||||
local token = buffer.readu8(b, p + 8)
|
||||
local clen, dlen = buffer.readi32(b, p + 9), buffer.readi32(b, p + 13)
|
||||
p += 21
|
||||
local method = token // 16 * 16
|
||||
if clen < 0 or dlen < 0 or p + clen > e then
|
||||
return nil, "truncated LZ4 block"
|
||||
end
|
||||
if dlen == 0 and clen == 0 then
|
||||
break
|
||||
end
|
||||
total += dlen
|
||||
if total > maxBytes then
|
||||
return nil, "limit: chunk inflates past maxBytes"
|
||||
end
|
||||
if method == 0x10 then
|
||||
if clen ~= dlen then
|
||||
return nil, "raw LZ4 block length mismatch"
|
||||
end
|
||||
local out = buffer.create(dlen)
|
||||
buffer.copy(out, 0, b, p, clen)
|
||||
table.insert(parts, out)
|
||||
elseif method == 0x20 then
|
||||
local out, err = Lz4.decompress(b, p, clen, dlen)
|
||||
if not out then
|
||||
return nil, "LZ4: " .. tostring(err and err.message)
|
||||
end
|
||||
table.insert(parts, out)
|
||||
else
|
||||
return nil, "unsupported: LZ4Block method " .. method
|
||||
end
|
||||
p += clen
|
||||
end
|
||||
if #parts == 1 then
|
||||
return parts[1], nil
|
||||
end
|
||||
local res = buffer.create(total)
|
||||
local w = 0
|
||||
for _, part in parts do
|
||||
buffer.copy(res, w, part)
|
||||
w += buffer.len(part)
|
||||
end
|
||||
return res, nil
|
||||
end
|
||||
|
||||
return Lz4Block
|
||||
@@ -0,0 +1,355 @@
|
||||
--!strict
|
||||
-- model/mca: Minecraft Anvil region files (r.<x>.<z>.mca: 32 x 32 chunks), read-only. New code, from the Minecraft
|
||||
-- wiki "Region file format" and "Chunk format" pages. Supports: the location / timestamp header, chunk payloads
|
||||
-- compressed with gzip, zlib (codecs/compress/Deflate), LZ4 (lz4-java block streams, 24w04a+: model/mca/Lz4Block over
|
||||
-- codecs/compress/Lz4) or stored, external .mcc chunks through opts.resolve, and every chunk layout: 1.18+ sections,
|
||||
-- 1.13 - 1.17 palettes (spanning and not) and pre-1.13 numeric ids (model/mca/Chunk).
|
||||
-- decode -> a Scene (meta.up = "y", one unit = one block) with one Types.Voxels covering the chosen chunks
|
||||
-- (opts.chunks, opts.area; by default every chunk, between the lowest and highest section holding blocks) with
|
||||
-- voxels.blocks (exact states, block entities relative to the grid), placed by its node at the world position of its
|
||||
-- minimum corner. meta.source.chunks lists every chunk read (local and world coordinates, timestamp, compression,
|
||||
-- DataVersion) with its whole NBT (chunk.nbt; opts.keepNbt = false drops it). Mca.list / Mca.chunk read the header
|
||||
-- and one chunk's NBT without building voxels.
|
||||
-- Not supported: writing; custom (127) chunk compression; LZ4 checksums are not verified; entities stored in the
|
||||
-- separate entities/ region files (1.17+) are another file (load it with Mca.chunk).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Deflate = require("@formats/codecs/compress/Deflate")
|
||||
local Nbt = require("@formats/codecs/text/Nbt")
|
||||
local Mc = require("@formats/codecs/model/Minecraft")
|
||||
local Chunk = require("@formats/model/mca/Chunk")
|
||||
local Lz4Block = require("@formats/model/mca/Lz4Block")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Mca = {
|
||||
name = "mca",
|
||||
category = "model",
|
||||
extensions = { "mca", "mcr" },
|
||||
mime = { "application/x-minecraft-region" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/compress/Deflate", "codecs/text/Nbt", "codecs/model/Minecraft", "codecs/compress/Lz4" },
|
||||
caps = { decode = true, encode = false, voxels = true, blocks = true },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
chunks: { { number } }?, -- only these chunks: { { x, z } } local coordinates 0 .. 31 (default all)
|
||||
area: { number }?, -- only blocks inside { minX, minY, minZ, maxX, maxY, maxZ } (world coordinates, inclusive)
|
||||
maxCells: number?, -- refuse grids with more cells (default 2^25: pick chunks or an area for big regions)
|
||||
maxBytes: number?, -- refuse a chunk that inflates past this (default 2^26)
|
||||
keepNbt: boolean?, -- keep each chunk's NBT in meta.source.chunks[i].nbt (default true)
|
||||
region: { number }?, -- the region's coordinates { x, z } (default: from opts.name "r.<x>.<z>.mca"), for .mcc files
|
||||
name: string?, -- the file name (the api fills it in)
|
||||
resolve: ((string) -> buffer?)?, -- reads a sibling file: external chunks c.<x>.<z>.mcc
|
||||
}
|
||||
export type Entry = {
|
||||
x: number, -- local chunk coordinates 0 .. 31
|
||||
z: number,
|
||||
sector: number, -- 4 KiB sector of the chunk
|
||||
sectors: number,
|
||||
timestamp: number, -- seconds since 1970
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local COMPRESSION = { [1] = "gzip", [2] = "zlib", [3] = "none", [4] = "lz4", [127] = "custom" }
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Mca.sniff(r: Bytes.Reader): number
|
||||
local n = r.limit - r.base
|
||||
if n < 8192 then
|
||||
return 0
|
||||
end
|
||||
local sectors = (n + 4095) // 4096
|
||||
local used, b = 0, r.buf
|
||||
for i = 0, 1023 do
|
||||
local e = bit32.byteswap(buffer.readu32(b, r.base + i * 4))
|
||||
if e ~= 0 then
|
||||
local off, cnt = e // 256, e % 256
|
||||
if off < 2 or cnt == 0 or off + cnt > sectors then
|
||||
return 0
|
||||
end
|
||||
used += 1
|
||||
if used == 1 then
|
||||
local p = r.base + off * 4096
|
||||
if p + 5 > r.limit then
|
||||
return 0
|
||||
end
|
||||
local c = buffer.readu8(b, p + 4) % 128
|
||||
if COMPRESSION[c] == nil then
|
||||
return 0
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return if used > 0 then 0.8 else 0.2
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- list(r) -> { Entry } | nil, Err: the chunks present in the region (location and timestamp tables)
|
||||
function Mca.list(r: Bytes.Reader): ({ Entry }?, Err.Err?)
|
||||
local n = r.limit - r.base
|
||||
if n < 8192 then
|
||||
return nil, Err.new(Err.TRUNCATED, "region header is 8192 bytes, file has " .. n)
|
||||
end
|
||||
local out: { Entry } = {}
|
||||
for i = 0, 1023 do
|
||||
local e = bit32.byteswap(buffer.readu32(r.buf, r.base + i * 4))
|
||||
if e ~= 0 then
|
||||
table.insert(out, {
|
||||
x = i % 32,
|
||||
z = i // 32,
|
||||
sector = e // 256,
|
||||
sectors = e % 256,
|
||||
timestamp = bit32.byteswap(buffer.readu32(r.buf, r.base + 4096 + i * 4)),
|
||||
})
|
||||
end
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
|
||||
local function regionCoords(o: DecodeOptions): (number?, number?)
|
||||
if o.region then
|
||||
return o.region[1], o.region[2]
|
||||
end
|
||||
if o.name then
|
||||
local x, z = string.match(o.name, "r%.(%-?%d+)%.(%-?%d+)%.mc[ar]$")
|
||||
if x then
|
||||
return tonumber(x), tonumber(z)
|
||||
end
|
||||
end
|
||||
return nil, nil
|
||||
end
|
||||
|
||||
-- payload(r, e, o) -> the chunk's uncompressed NBT bytes, compression name | nil, Err
|
||||
local function payload(r: Bytes.Reader, e: Entry, o: DecodeOptions): (buffer?, any)
|
||||
local p = r.base + e.sector * 4096
|
||||
if p + 5 > r.limit then
|
||||
return nil, Err.new(Err.TRUNCATED, string.format("chunk %d,%d starts past the end", e.x, e.z), p - r.base)
|
||||
end
|
||||
local len = bit32.byteswap(buffer.readu32(r.buf, p))
|
||||
local ctype = buffer.readu8(r.buf, p + 4)
|
||||
local data: buffer, start: number, size: number = r.buf, p + 5, len - 1
|
||||
if ctype >= 128 then
|
||||
-- stored in c.<x>.<z>.mcc next to the region (whole file = the compressed payload)
|
||||
local rx, rz = regionCoords(o)
|
||||
if not o.resolve or rx == nil then
|
||||
return nil, Err.new(Err.UNSUPPORTED, string.format("chunk %d,%d is in an external .mcc file (pass opts.resolve and the region coordinates)", e.x, e.z))
|
||||
end
|
||||
local name = string.format("c.%d.%d.mcc", (rx :: number) * 32 + e.x, (rz :: number) * 32 + e.z)
|
||||
local ext = o.resolve(name)
|
||||
if type(ext) ~= "buffer" then
|
||||
return nil, Err.new(Err.IO, "cannot read " .. name)
|
||||
end
|
||||
data, start, size = ext, 0, buffer.len(ext)
|
||||
ctype -= 128
|
||||
elseif len < 1 or p + 4 + len > r.limit then
|
||||
return nil, Err.new(Err.TRUNCATED, string.format("chunk %d,%d runs past the end", e.x, e.z), p - r.base)
|
||||
end
|
||||
local comp = COMPRESSION[ctype]
|
||||
local maxBytes = o.maxBytes or 2 ^ 26
|
||||
local out, err
|
||||
if comp == "gzip" then
|
||||
out, err = Deflate.gunzip(data, start, size)
|
||||
elseif comp == "zlib" then
|
||||
out, err = Deflate.zlib(data, start, size)
|
||||
elseif comp == "none" then
|
||||
out = buffer.create(size :: number)
|
||||
buffer.copy(out, 0, data, start, size)
|
||||
elseif comp == "lz4" then
|
||||
local msg
|
||||
out, msg = Lz4Block.decode(data, start, size :: number, maxBytes)
|
||||
if not out then
|
||||
err = Err.from(msg, start - r.base)
|
||||
end
|
||||
else
|
||||
return nil, Err.new(Err.UNSUPPORTED, string.format("chunk %d,%d: compression type %d", e.x, e.z, ctype))
|
||||
end
|
||||
if not out then
|
||||
return nil, err
|
||||
end
|
||||
if buffer.len(out) > maxBytes then
|
||||
return nil, Err.new(Err.LIMIT, string.format("chunk %d,%d inflates past maxBytes", e.x, e.z))
|
||||
end
|
||||
return out, comp
|
||||
end
|
||||
|
||||
-- chunk(r, x, z, opts?) -> the chunk's NBT root, compression | nil, Err (nil, nil when the chunk is absent)
|
||||
function Mca.chunk(r: Bytes.Reader, x: number, z: number, opts: DecodeOptions?): (any, any)
|
||||
local o: DecodeOptions = opts or {}
|
||||
if x < 0 or x > 31 or z < 0 or z > 31 or x % 1 ~= 0 or z % 1 ~= 0 then
|
||||
return nil, Err.new(Err.ARG, "chunk coordinates are 0 .. 31")
|
||||
end
|
||||
local entries, lerr = Mca.list(r)
|
||||
if not entries then
|
||||
return nil, lerr
|
||||
end
|
||||
for _, e in entries do
|
||||
if e.x == x and e.z == z then
|
||||
local raw, comp = payload(r, e, o)
|
||||
if not raw then
|
||||
return nil, comp
|
||||
end
|
||||
local v, info = Nbt.decode(raw, { endian = "big", maxBytes = o.maxBytes })
|
||||
if v == nil then
|
||||
return nil, info
|
||||
end
|
||||
return v, comp
|
||||
end
|
||||
end
|
||||
return nil, nil
|
||||
end
|
||||
|
||||
function Mca.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
|
||||
local o: DecodeOptions = opts or {}
|
||||
local entries, lerr = Mca.list(r)
|
||||
if not entries then
|
||||
return nil, lerr
|
||||
end
|
||||
local want: { [number]: boolean }? = nil
|
||||
if o.chunks then
|
||||
local w: { [number]: boolean } = {}
|
||||
for _, c in o.chunks do
|
||||
w[(c[1] or 0) + 32 * (c[2] or 0)] = true
|
||||
end
|
||||
want = w
|
||||
end
|
||||
local area = o.area
|
||||
if area and #area ~= 6 then
|
||||
return nil, Err.new(Err.ARG, "area is { minX, minY, minZ, maxX, maxY, maxZ }")
|
||||
end
|
||||
local keep = o.keepNbt ~= false
|
||||
local chunks: { Chunk.Blocks } = {}
|
||||
local infos = {}
|
||||
for _, e in entries do
|
||||
if not want or want[e.x + 32 * e.z] then
|
||||
local raw, comp = payload(r, e, o)
|
||||
if not raw then
|
||||
return nil, comp
|
||||
end
|
||||
local v, info = Nbt.decode(raw, { endian = "big", maxBytes = o.maxBytes })
|
||||
if v == nil then
|
||||
return nil, Err.new(info.code, string.format("chunk %d,%d: %s", e.x, e.z, info.message))
|
||||
end
|
||||
local blocks, berr = Chunk.blocks(v)
|
||||
if not blocks then
|
||||
return nil, Err.from(string.format("chunk %d,%d: %s", e.x, e.z, tostring(berr)))
|
||||
end
|
||||
local inArea = not area or (blocks.x * 16 <= area[4] and blocks.x * 16 + 15 >= area[1] and blocks.z * 16 <= area[6] and blocks.z * 16 + 15 >= area[3])
|
||||
if inArea then
|
||||
table.insert(chunks, blocks)
|
||||
table.insert(infos, { x = e.x, z = e.z, worldX = blocks.x, worldZ = blocks.z, timestamp = e.timestamp, compression = comp, dataVersion = blocks.dataVersion, nbt = if keep then v else nil })
|
||||
end
|
||||
end
|
||||
end
|
||||
-- the bounds: chunks in x / z, sections holding anything but air in y, cut to the area
|
||||
local lo, hi = { math.huge, math.huge, math.huge }, { -math.huge, -math.huge, -math.huge }
|
||||
for _, c in chunks do
|
||||
lo[1], hi[1] = math.min(lo[1], c.x * 16), math.max(hi[1], c.x * 16 + 15)
|
||||
lo[3], hi[3] = math.min(lo[3], c.z * 16), math.max(hi[3], c.z * 16 + 15)
|
||||
for _, s in c.sections do
|
||||
local solid = false
|
||||
for _, st in s.states do
|
||||
if not Mc.isAir(st) then
|
||||
solid = true
|
||||
break
|
||||
end
|
||||
end
|
||||
if solid then
|
||||
lo[2], hi[2] = math.min(lo[2], s.y * 16), math.max(hi[2], s.y * 16 + 15)
|
||||
end
|
||||
end
|
||||
end
|
||||
if area then
|
||||
for k = 1, 3 do
|
||||
lo[k], hi[k] = math.max(lo[k], area[k]), math.min(hi[k], area[k + 3])
|
||||
if area[k] > area[k + 3] then
|
||||
return nil, Err.new(Err.ARG, "area minimum past its maximum")
|
||||
end
|
||||
end
|
||||
if lo[2] == math.huge then
|
||||
lo[2], hi[2] = area[2], area[5]
|
||||
end
|
||||
end
|
||||
if lo[2] == math.huge then
|
||||
lo[2], hi[2] = 0, -1 -- no blocks at all
|
||||
end
|
||||
if #chunks == 0 then
|
||||
lo, hi = { 0, 0, 0 }, { -1, -1, -1 }
|
||||
end
|
||||
local sx, sy, sz = math.max(0, hi[1] - lo[1] + 1), math.max(0, hi[2] - lo[2] + 1), math.max(0, hi[3] - lo[3] + 1)
|
||||
local n = sx * sy * sz
|
||||
if n > (o.maxCells or 2 ^ 25) then
|
||||
return nil, Err.new(Err.LIMIT, string.format("%d x %d x %d cells is over maxCells: choose opts.chunks or opts.area", sx, sy, sz))
|
||||
end
|
||||
-- one palette for the grid (air first), each section copied in through a remap table
|
||||
local states = { "minecraft:air" }
|
||||
local index: { [string]: number } = { ["minecraft:air"] = 0 }
|
||||
local grid = buffer.create(n * 2)
|
||||
local bes, ents = {}, {}
|
||||
for _, c in chunks do
|
||||
local cx, cz = c.x * 16 - lo[1], c.z * 16 - lo[3]
|
||||
for _, s in c.sections do
|
||||
local cy = s.y * 16 - lo[2]
|
||||
if cy + 15 >= 0 and cy < sy and cx + 15 >= 0 and cx < sx and cz + 15 >= 0 and cz < sz then
|
||||
local lut = table.create(#s.states) :: { number }
|
||||
for i, st in s.states do
|
||||
local g = index[st]
|
||||
if g == nil then
|
||||
table.insert(states, st)
|
||||
g = #states - 1
|
||||
index[st] = g
|
||||
end
|
||||
lut[i] = g
|
||||
end
|
||||
if #states > 65536 then
|
||||
return nil, Err.new(Err.LIMIT, "more than 65536 block states")
|
||||
end
|
||||
local cells = s.cells
|
||||
local y0, y1 = math.max(0, -cy), math.min(15, sy - 1 - cy)
|
||||
local z0, z1 = math.max(0, -cz), math.min(15, sz - 1 - cz)
|
||||
local x0, x1 = math.max(0, -cx), math.min(15, sx - 1 - cx)
|
||||
for y = y0, y1 do
|
||||
for z = z0, z1 do
|
||||
local src = ((y * 16 + z) * 16 + x0) * 2
|
||||
local dst = ((cx + x0) + sx * ((cy + y) + sy * (cz + z))) * 2
|
||||
for _ = x0, x1 do
|
||||
buffer.writeu16(grid, dst, lut[buffer.readu16(cells, src) + 1] or 0)
|
||||
src += 2
|
||||
dst += 2
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
for _, rec in c.blockEntities do
|
||||
local be = Mc.blockEntity(rec, "flat")
|
||||
if be then
|
||||
local px, py, pz = be.pos[1] - lo[1], be.pos[2] - lo[2], be.pos[3] - lo[3]
|
||||
if px >= 0 and px < sx and py >= 0 and py < sy and pz >= 0 and pz < sz then
|
||||
be.pos = { px, py, pz }
|
||||
table.insert(bes, be)
|
||||
end
|
||||
end
|
||||
end
|
||||
for _, ent in c.entities do
|
||||
table.insert(ents, ent)
|
||||
end
|
||||
end
|
||||
local v = Mc.voxels(sx, sy, sz, states, grid)
|
||||
local b = v.blocks :: Types.VoxelBlocks
|
||||
b.blockEntities = bes
|
||||
b.entities = ents
|
||||
local scene = Types.Scene.new()
|
||||
scene.voxels = { v }
|
||||
scene.nodes = { { name = o.name, children = {}, voxels = 1, translation = { lo[1], lo[2], lo[3] } } }
|
||||
scene.roots = { 1 }
|
||||
scene.meta.up = "y"
|
||||
scene.meta.unit = 1
|
||||
local rx, rz = regionCoords(o)
|
||||
scene.meta.source = { format = "mca", region = if rx then { rx, rz } else nil, chunks = infos }
|
||||
return scene, nil
|
||||
end
|
||||
|
||||
return Mca
|
||||
@@ -4,6 +4,7 @@
|
||||
-- converted from / to MCEdit's and Litematica's flat { x, y, z, id, ... } and Sponge v2's { Pos, Id, ... }).
|
||||
local Types = require("@formats/Types")
|
||||
local Nbt = require("@formats/codecs/text/Nbt")
|
||||
local Mc = require("@formats/codecs/model/Minecraft")
|
||||
|
||||
local Common = {}
|
||||
|
||||
@@ -19,13 +20,7 @@ export type Region = {
|
||||
}
|
||||
|
||||
-- int(v) -> the integer value of an NBT number (byte / short / int / long tag or plain number) | nil
|
||||
function Common.int(v: any): number?
|
||||
local n = Nbt.number(v)
|
||||
if n == nil or n ~= n or n % 1 ~= 0 then
|
||||
return nil
|
||||
end
|
||||
return n
|
||||
end
|
||||
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?)
|
||||
@@ -43,80 +38,11 @@ function Common.dims(a: any, b: any, c: any, max: number): (number?, any, number
|
||||
end
|
||||
|
||||
-- ints(v) -> the numbers of an intArray tag or a list of numbers | nil
|
||||
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 :: any)[1] ~= nil then
|
||||
local out: { number } = {}
|
||||
for i, x in v :: { any } do
|
||||
out[i] = Nbt.number(x) or 0
|
||||
end
|
||||
return out
|
||||
end
|
||||
return nil
|
||||
end
|
||||
Common.ints = Mc.ints
|
||||
|
||||
local function rest(c: any, skip: { [string]: boolean }): { [string]: any }
|
||||
local out, keys = {}, {}
|
||||
for _, k in Types.keys(c) do
|
||||
if not skip[k] then
|
||||
out[k] = c[k]
|
||||
table.insert(keys, k)
|
||||
end
|
||||
end
|
||||
return Types.object(out, keys)
|
||||
end
|
||||
|
||||
local FLAT_SKIP = { x = true, y = true, z = true, id = true }
|
||||
local POS_SKIP = { Pos = true, Id = true }
|
||||
|
||||
-- blockEntity(c, style) -> the common shape of one record: style "flat" (MCEdit, Litematica), "pos" (Sponge v1 / v2),
|
||||
-- "data" (Sponge v3)
|
||||
function Common.blockEntity(c: any, style: string): any
|
||||
if type(c) ~= "table" or Nbt.isTag(c) then
|
||||
return nil
|
||||
end
|
||||
if style == "flat" then
|
||||
return { pos = { Common.int(c.x) or 0, Common.int(c.y) or 0, Common.int(c.z) or 0 }, id = if type(c.id) == "string" then c.id else nil, data = rest(c, FLAT_SKIP) }
|
||||
end
|
||||
local p = Common.ints(c.Pos) or { 0, 0, 0 }
|
||||
local data = if style == "data" then (if type(c.Data) == "table" then c.Data else Types.object()) else rest(c, POS_SKIP)
|
||||
return { pos = { p[1] or 0, p[2] or 0, p[3] or 0 }, id = if type(c.Id) == "string" then c.Id else nil, data = data }
|
||||
end
|
||||
|
||||
-- flatBlockEntity(be, style) -> the record in a flavour's shape (the inverse of blockEntity)
|
||||
function Common.flatBlockEntity(be: any, style: string): any
|
||||
local o = Types.object()
|
||||
local data = if type(be.data) == "table" then be.data else Types.object()
|
||||
if style == "flat" then
|
||||
Types.set(o, "x", be.pos[1])
|
||||
Types.set(o, "y", be.pos[2])
|
||||
Types.set(o, "z", be.pos[3])
|
||||
if be.id then
|
||||
Types.set(o, "id", be.id)
|
||||
end
|
||||
for _, k in Types.keys(data) do
|
||||
if not FLAT_SKIP[k] then
|
||||
Types.set(o, k, data[k])
|
||||
end
|
||||
end
|
||||
return o
|
||||
end
|
||||
Types.set(o, "Pos", Nbt.intArray({ be.pos[1], be.pos[2], be.pos[3] }))
|
||||
if be.id then
|
||||
Types.set(o, "Id", be.id)
|
||||
end
|
||||
if style == "data" then
|
||||
Types.set(o, "Data", data)
|
||||
else
|
||||
for _, k in Types.keys(data) do
|
||||
if not POS_SKIP[k] then
|
||||
Types.set(o, k, data[k])
|
||||
end
|
||||
end
|
||||
end
|
||||
return o
|
||||
end
|
||||
-- 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 }
|
||||
|
||||
Reference in new issue
Block a user