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,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("@rosetta/Types")
|
||||
local Nbt = require("@rosetta/codecs/text/Nbt")
|
||||
local Mc = require("@rosetta/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
|
||||
Reference in new issue
Block a user