model formats (mca..x3d): comments are specs only

This commit is contained in:
4DBug committed 2026-11-01 01:54:32 -05:00
1 parent 0bd7b87d2b
commit b952c10222
49 files changed
+310 -936

No files matched your search

+5 -15
View File
@@ -1,28 +1,25 @@
--!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, ... }).
-- model/schematic/Common: shared region record, NBT access and block entities as Sponge v3's { pos = { x, y, z }, id,
-- data } (MCEdit / Litematica use flat { x, y, z, id, ... }, Sponge v2 { 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
-- a block region: u16 indices 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
size: { number },
position: { number }, -- 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
@@ -37,14 +34,11 @@ function Common.dims(a: any, b: any, c: any, max: number): (number?, any, number
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
@@ -52,7 +46,6 @@ function Common.list(v: any): { any }
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
@@ -64,8 +57,6 @@ function Common.compound(fields: { { any } }): { [string]: any }
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)
@@ -98,7 +89,6 @@ function Common.merge(base: any, fields: { { any } }, drop: { [string]: boolean
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 } = {}
+3 -9
View File
@@ -1,9 +1,7 @@
--!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).
-- model/schematic/Litematic: .litematic, from LitematicaSchematic / LitematicaBitArray as documented by litemapy:
-- Metadata, named Regions (Position, Size may be negative), palette (0 = air), BlockStates spanning longs (>= 2 bits),
-- TileEntities relative to the region's minimum corner, Entities and pending ticks.
local Types = require("@rosetta/Types")
local Nbt = require("@rosetta/codecs/text/Nbt")
local Mc = require("@rosetta/codecs/model/Minecraft")
@@ -28,7 +26,6 @@ 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"
@@ -106,8 +103,6 @@ function Litematic.read(root: any, maxCells: number): ({ Common.Region }?, any)
}
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 = {}, {}
@@ -163,7 +158,6 @@ function Litematic.write(regions: { Common.Region }, version: number, dataVersio
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]
+3 -10
View File
@@ -1,10 +1,7 @@
--!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).
-- model/schematic/Mcedit: MCEdit / WorldEdit .schematic (pre-1.13; Minecraft wiki "Schematic file format"): Width /
-- Height / Length, Blocks + Data (+ AddBlocks high nibbles, high nibble first), Materials = "Alpha", TileEntities,
-- Entities, WEOffset*, and the SchematicaMapping / BlockIDs name tables of Schematica and MCEdit-Unified.
local Types = require("@rosetta/Types")
local Nbt = require("@rosetta/codecs/text/Nbt")
local Mc = require("@rosetta/codecs/model/Minecraft")
@@ -14,7 +11,6 @@ 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
@@ -152,15 +148,12 @@ local function nearest(state: string): (number, number)
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
+3 -7
View File
@@ -1,8 +1,7 @@
--!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.
-- model/schematic/Sponge: .schem (WorldEdit 7+, FAWE, Amulet), from SpongePowered/Schematic-Specification v1-3: Width /
-- Height / Length, Offset, 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")
@@ -12,7 +11,6 @@ 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
@@ -81,8 +79,6 @@ function Sponge.read(s: any, maxCells: number): ({ Common.Region }?, any)
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
+11 -26
View File
@@ -1,19 +1,7 @@
--!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).
-- model/schematic: Minecraft schematics, read + write: MCEdit / WorldEdit .schematic (pre-1.13), Sponge .schem v1-3
-- (WorldEdit 7+) and Litematica .litematic. Not supported: block properties to pre-1.13 data values, Sponge biome
-- conversion between versions, rotated nodes.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
@@ -38,18 +26,18 @@ local Schematic = {
-- ------------------------------------------------------------------------------------------------ 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)
maxCells: number?, -- default 2^26
maxBytes: number?, -- 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
version: number?, -- Sponge default 3, Litematica default 6 (or the source's)
dataVersion: number?, -- default the source's, else 3953 = 1.21
name: string?,
title: string?,
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", ...)
level: number?,
format: string?,
}
-- ------------------------------------------------------------------------------------------------ constants
@@ -79,7 +67,6 @@ function Schematic.sniff(r: Bytes.Reader): number
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
@@ -98,7 +85,6 @@ function Schematic.variant(r: Bytes.Reader): string?
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
@@ -233,7 +219,6 @@ local function regionsOf(value: any): ({ Common.Region }?, string?)
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]