-- model/mca tests: region files with 1.18+ chunks (each compression: zlib, gzip, stored, LZ4 block streams), -- 1.16 / 1.13 / 1.12 chunks (palettes non-spanning and spanning, numeric ids with Add nibbles) and an external .mcc -- chunk: every cell of the assembled grid against the fixture (a CRC of the cell texts), placement, the chunk list, -- block entities, opts.chunks / opts.area / maxCells, Mca.list / Mca.chunk, detection, errors and the fuzz pass. -- Fixtures: tools/fixtures/mca.py. local T = require("@tests/T") local F = require("@rosetta") local Mca = require("@rosetta/model/mca") local Mc = require("@rosetta/codecs/model/Minecraft") local Bytes = require("@rosetta/core/Bytes") local Crc32 = require("@rosetta/core/Crc32") local Err = require("@rosetta/core/Err") local Types = require("@rosetta/Types") local Fx = require("@tests/gen/mca") local t = T.suite("mca") t:eq(Mca.name, "mca", "name") t:check(Mca.caps.decode and not Mca.caps.encode, "caps: read-only") local function reader(s: string): Bytes.Reader return Bytes.reader(buffer.fromstring(s)) end -- the fixture's cell texts: the state, or "legacy::" inside chunks without a DataVersion local function cellText(scene: Types.Scene): string local v = (scene.voxels :: { Types.Voxels })[1] local b = v.blocks :: Types.VoxelBlocks local o = scene.nodes[1].translation :: { number } local legacyChunk: { [string]: boolean } = {} for _, c in scene.meta.source.chunks do if c.dataVersion == nil then legacyChunk[c.worldX .. "," .. c.worldZ] = true end end local sx, sy, sz = v.size[1], v.size[2], v.size[3] local parts = table.create(sx * sy * sz) local i = 0 for z = 0, sz - 1 do for y = 0, sy - 1 do for x = 0, sx - 1 do local st = b.palette[buffer.readu16(b.data, i * 2) + 1] local wx, wz = o[1] + x, o[3] + z if legacyChunk[(wx // 16) .. "," .. (wz // 16)] then local id, d = Mc.legacyId(st) st = "legacy:" .. tostring(id) .. ":" .. tostring(d) end i += 1 parts[i] = st end end end return table.concat(parts, "\n") end for _, c in Fx.regions do local opts = { name = c.name, resolve = function(name: string): buffer? local f = c.files and c.files[name] return if f then buffer.fromstring(f) else nil end } local scene, err = Mca.decode(reader(c.data), opts) t:check(scene ~= nil, c.name .. ": " .. tostring(err)) local s = scene :: Types.Scene local v = (s.voxels :: { Types.Voxels })[1] t:eq(table.concat(v.size, ","), table.concat(c.size, ","), c.name .. ": size") t:eq(table.concat(s.nodes[1].translation :: { number }, ","), table.concat(c.min, ","), c.name .. ": minimum corner") t:eq(Crc32.of(buffer.fromstring(cellText(s))), c.crc, c.name .. ": every cell") t:eq(#s.meta.source.chunks, #c.chunks, c.name .. ": chunks") for i, want in c.chunks do local got = s.meta.source.chunks[i] t:check(got.x == want.x and got.z == want.z, c.name .. ": chunk " .. i .. " position") t:eq(got.timestamp, want.timestamp, c.name .. ": chunk " .. i .. " timestamp") t:eq(got.compression, want.compression, c.name .. ": chunk " .. i .. " compression") t:eq(got.dataVersion, want.dataVersion, c.name .. ": chunk " .. i .. " DataVersion") t:check(got.nbt ~= nil, c.name .. ": chunk " .. i .. " NBT kept") end t:eq(#((v.blocks :: Types.VoxelBlocks).blockEntities :: { any }), c.blockEntities, c.name .. ": block entities") -- colours follow the blocks local air = 0 for i = 0, buffer.len(v.data) - 1 do if buffer.readu8(v.data, i) == 0 then air += 1 end end t:check(air > 0 and air < buffer.len(v.data), c.name .. ": colour grid") end -- details on the 1.18+ region do local c = Fx.regions[1] local list = assert(Mca.list(reader(c.data))) t:eq(#list, 4, "list") local nbt, comp = Mca.chunk(reader(c.data), 3, 2) t:check(nbt and nbt.xPos == 3 and nbt.Status == "minecraft:full", "Mca.chunk") t:eq(comp, "lz4", "Mca.chunk compression") t:eq(Mca.chunk(reader(c.data), 9, 9), nil, "absent chunk") -- only some chunks / an area local one = assert(Mca.decode(reader(c.data), { chunks = { { 1, 0 } }, keepNbt = false })) t:eq(table.concat((one.voxels :: { Types.Voxels })[1].size, ","), "16,48,16", "opts.chunks") t:eq(one.meta.source.chunks[1].nbt, nil, "keepNbt = false") local part = assert(Mca.decode(reader(c.data), { area = { 4, -64, 4, 19, -60, 9 } })) local pv = (part.voxels :: { Types.Voxels })[1] t:eq(table.concat(pv.size, ","), "16,5,6", "opts.area size") t:eq(table.concat(part.nodes[1].translation :: { number }, ","), "4,-64,4", "opts.area corner") -- the area's cells equal the same cells of the whole grid local whole = assert(Mca.decode(reader(c.data))) local wv = (whole.voxels :: { Types.Voxels })[1] local wo = whole.nodes[1].translation :: { number } local bad = 0 for z = 0, 5 do for y = 0, 4 do for x = 0, 15 do local a = pv.blocks.palette[buffer.readu16(pv.blocks.data, (x + 16 * (y + 5 * z)) * 2) + 1] local gx, gy, gz = 4 + x - wo[1], -64 + y - wo[2], 4 + z - wo[3] local w = wv.blocks.palette[buffer.readu16(wv.blocks.data, (gx + wv.size[1] * (gy + wv.size[2] * gz)) * 2) + 1] if a ~= w then bad += 1 end end end end t:eq(bad, 0, "area cells") local _, e = Mca.decode(reader(c.data), { maxCells = 1000 }) t:check(e and e.code == Err.LIMIT, "maxCells") t:eq(F.detect(c.data), "mca", "F.detect") local viaApi = F.load(c.data, { name = "r.0.0.mca" }) t:check(viaApi and viaApi.kind == "scene", "F.load") t:check(F.save(viaApi, nil, { format = "schem" }) ~= nil, "region -> .schem") end -- errors do local ext = Fx.regions[3] local _, e1 = Mca.decode(reader(ext.data)) t:check(e1 and e1.code == Err.UNSUPPORTED, "external chunk without resolve") local _, e2 = Mca.decode(reader(string.sub(Fx.regions[1].data, 1, 9000))) t:check(e2 and e2.code == Err.TRUNCATED, "truncated region") local _, e3 = Mca.decode(reader(string.rep("\0", 100))) t:check(e3 and e3.code == Err.TRUNCATED, "short header") local _, e4 = Mca.chunk(reader(Fx.regions[1].data), 40, 0) t:check(e4 and e4.code == Err.ARG, "bad chunk coordinates") end local samples = { Fx.regions[2].data, string.sub(Fx.regions[1].data, 1, 20000) } t:fuzz("decode", function(b: buffer) return Mca.decode(Bytes.reader(b)) end, samples, { keepHeader = 0 }) t:done()