205 lines
5.7 KiB
Luau
205 lines
5.7 KiB
Luau
--!strict
|
|
--!native
|
|
--!optimize 2
|
|
-- codecs/model/Minecraft/Packed: Minecraft's packed index arrays. New code.
|
|
-- Minecraft wiki "Chunk format", Sponge / Litematica schematic specs: little-endian int64 longs of bits-wide indices,
|
|
-- spanning long boundaries (Litematica, chunks before 1.16) or floor(64 / bits) per long plus padding (since 1.16).
|
|
local Packed = {}
|
|
|
|
local readu8, readu16, readu32 = buffer.readu8, buffer.readu16, buffer.readu32
|
|
local writeu8, writeu16 = buffer.writeu8, buffer.writeu16
|
|
local extract, band, rshift, lshift, bor = bit32.extract, bit32.band, bit32.rshift, bit32.lshift, bit32.bor
|
|
|
|
function Packed.longsNeeded(count: number, bits: number, spanning: boolean): number
|
|
if spanning then
|
|
return (count * bits + 63) // 64
|
|
end
|
|
local per = 64 // bits
|
|
return (count + per - 1) // per
|
|
end
|
|
|
|
function Packed.unpack(longs: buffer, bits: number, count: number, spanning: boolean, out: buffer, max: number): (boolean, string?)
|
|
if bits < 1 or bits > 16 then
|
|
return false, "unsupported: " .. bits .. "-bit packed indices"
|
|
end
|
|
local need = Packed.longsNeeded(count, bits, spanning)
|
|
if buffer.len(longs) < need * 8 then
|
|
return false, "packed array too short (" .. buffer.len(longs) // 8 .. " longs, need " .. need .. ")"
|
|
end
|
|
if buffer.len(out) < count * 2 then
|
|
return false, "output too small"
|
|
end
|
|
local n = need * 8
|
|
local src = buffer.create(n + 4)
|
|
buffer.copy(src, 0, longs, 0, n)
|
|
if spanning then
|
|
local p = 0
|
|
for i = 0, count - 1 do
|
|
local v = extract(readu32(src, rshift(p, 3)), band(p, 7), bits)
|
|
if v >= max then
|
|
return false, "index " .. v .. " past the palette (" .. max .. ")"
|
|
end
|
|
writeu16(out, i * 2, v)
|
|
p += bits
|
|
end
|
|
else
|
|
local per = 64 // bits
|
|
local i = 0
|
|
local base = 0
|
|
while i < count do
|
|
local p = 0
|
|
for _ = 1, per do
|
|
if i >= count then
|
|
break
|
|
end
|
|
local v = extract(readu32(src, base + rshift(p, 3)), band(p, 7), bits)
|
|
if v >= max then
|
|
return false, "index " .. v .. " past the palette (" .. max .. ")"
|
|
end
|
|
writeu16(out, i * 2, v)
|
|
i += 1
|
|
p += bits
|
|
end
|
|
base += 8
|
|
end
|
|
end
|
|
return true, nil
|
|
end
|
|
|
|
function Packed.pack(indices: buffer, count: number, bits: number, spanning: boolean): buffer
|
|
local need = Packed.longsNeeded(count, bits, spanning)
|
|
local out = buffer.create(need * 8 + 4)
|
|
local per = 64 // bits
|
|
for i = 0, count - 1 do
|
|
local v = readu16(indices, i * 2)
|
|
local p = if spanning then i * bits else (i // per) * 64 + (i % per) * bits
|
|
local at = rshift(p, 3)
|
|
local sh = band(p, 7)
|
|
local x = lshift(v, sh)
|
|
for k = 0, 2 do
|
|
local bits8 = band(rshift(x, k * 8), 0xff)
|
|
if bits8 ~= 0 then
|
|
writeu8(out, at + k, bor(readu8(out, at + k), bits8))
|
|
end
|
|
end
|
|
end
|
|
local res = buffer.create(need * 8)
|
|
buffer.copy(res, 0, out, 0, need * 8)
|
|
return res
|
|
end
|
|
|
|
function Packed.bitsFor(n: number, min: number): number
|
|
local b = min
|
|
while lshift(1, b) < n and b < 31 do
|
|
b += 1
|
|
end
|
|
return b
|
|
end
|
|
|
|
-- Sponge BlockData: unsigned LEB128 varints
|
|
function Packed.varints(src: buffer, offset: number, length: number, count: number, out: buffer, max: number): (boolean, string?)
|
|
if buffer.len(out) < count * 2 or offset + length > buffer.len(src) then
|
|
return false, "bad varint array arguments"
|
|
end
|
|
local p, e = offset, offset + length
|
|
for i = 0, count - 1 do
|
|
local v, sh = 0, 0
|
|
while true do
|
|
if p >= e then
|
|
return false, "truncated: block data ends after " .. i .. " of " .. count .. " entries"
|
|
end
|
|
local c = readu8(src, p)
|
|
p += 1
|
|
v += band(c, 0x7f) * 2 ^ sh
|
|
if c < 128 then
|
|
break
|
|
end
|
|
sh += 7
|
|
if sh > 28 then
|
|
return false, "varint too long"
|
|
end
|
|
end
|
|
if v >= max then
|
|
return false, "index " .. v .. " past the palette (" .. max .. ")"
|
|
end
|
|
writeu16(out, i * 2, v)
|
|
end
|
|
return true, nil
|
|
end
|
|
|
|
function Packed.toVarints(indices: buffer, count: number): buffer
|
|
local out = buffer.create(count * 3)
|
|
local p = 0
|
|
for i = 0, count - 1 do
|
|
local v = readu16(indices, i * 2)
|
|
while v >= 128 do
|
|
writeu8(out, p, band(v, 0x7f) + 128)
|
|
p += 1
|
|
v = rshift(v, 7)
|
|
end
|
|
writeu8(out, p, v)
|
|
p += 1
|
|
end
|
|
local res = buffer.create(p)
|
|
buffer.copy(res, 0, out, 0, p)
|
|
return res
|
|
end
|
|
|
|
-- nibble arrays: Minecraft low nibble first; WorldEdit AddBlocks high nibble first
|
|
function Packed.nibble(src: buffer, offset: number, i: number, highFirst: boolean): number
|
|
local b = readu8(src, offset + rshift(i, 1))
|
|
local odd = band(i, 1) == 1
|
|
if odd == highFirst then
|
|
return band(b, 15)
|
|
end
|
|
return rshift(b, 4)
|
|
end
|
|
|
|
function Packed.setNibble(dst: buffer, offset: number, i: number, v: number, highFirst: boolean)
|
|
local at = offset + rshift(i, 1)
|
|
local b = readu8(dst, at)
|
|
local odd = band(i, 1) == 1
|
|
if odd == highFirst then
|
|
writeu8(dst, at, bor(band(b, 0xf0), band(v, 15)))
|
|
else
|
|
writeu8(dst, at, bor(band(b, 0x0f), lshift(band(v, 15), 4)))
|
|
end
|
|
end
|
|
|
|
-- Minecraft order: x fastest, then z, then y (schematics, chunk sections)
|
|
function Packed.fromYzx(src: buffer, sx: number, sy: number, sz: number, dst: buffer?, ox: number?, oy: number?, oz: number?, dsx: number?, dsy: number?): buffer
|
|
local out = dst or buffer.create(sx * sy * sz * 2)
|
|
local bx, by, bz = ox or 0, oy or 0, oz or 0
|
|
local wx, wy = dsx or sx, dsy or sy
|
|
local i = 0
|
|
for y = 0, sy - 1 do
|
|
for z = 0, sz - 1 do
|
|
local d = (bx + wx * ((by + y) + wy * (bz + z))) * 2
|
|
for _ = 0, sx - 1 do
|
|
writeu16(out, d, readu16(src, i))
|
|
i += 2
|
|
d += 2
|
|
end
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
function Packed.toYzx(src: buffer, sx: number, sy: number, sz: number): buffer
|
|
local out = buffer.create(sx * sy * sz * 2)
|
|
local i = 0
|
|
for y = 0, sy - 1 do
|
|
for z = 0, sz - 1 do
|
|
local s = (sx * (y + sy * z)) * 2
|
|
for _ = 0, sx - 1 do
|
|
writeu16(out, i, readu16(src, s))
|
|
i += 2
|
|
s += 2
|
|
end
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
return Packed
|