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

+9 -24
View File
@@ -1,15 +1,7 @@
--!strict
-- model/rbxm: the Roblox binary model / place format (.rbxm / .rbxl, "<roblox!"), read + write, to and from the instance
-- tree of codecs/model/Rbx (an RbxTree, kind "rbxtree"; not a Scene: Roblox files hold instances, not meshes).
-- Written from the rbx-dom format notes (docs/binary.md); checked against rojo 7.7 and lune 0.11 (rbx-dom) both ways.
-- Decode: the header, META (tree.metadata), SSTR (shared strings), INST (classes, referents, service flags), PROP (every
-- property type of codecs/model/Rbx/Binary), PRNT (the hierarchy), END; chunk bodies raw, LZ4 or Zstandard. Unknown
-- chunks are kept raw in tree.extra and written back; properties of unknown types become "Raw" values that are
-- written back verbatim while the class keeps its instances. tree.place is true when the file marks services (rbxl).
-- Encode: every instance depth first (referents 0..n-1), classes sorted by name, properties sorted by name, chunks
-- LZ4-compressed (opts.compression = "none" writes them raw), SharedStrings deduplicated (MD5 keys). An instance
-- without a property its class's other instances have gets the type's default (the format stores one value per
-- instance). "XmlRaw" values (unknown XML types) are skipped. Not supported: writing Zstandard chunks.
-- model/rbxm: Roblox binary model / place (.rbxm / .rbxl, "<roblox!") to and from a codecs/model/Rbx RbxTree, from the
-- rbx-dom notes (docs/binary.md), checked against rojo 7.7 and lune 0.11. Supports: META, SSTR, INST, PROP, PRNT, END;
-- raw / LZ4 / Zstandard chunks; unknown chunks and types round-trip raw. Not supported: writing Zstandard chunks.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
@@ -32,13 +24,13 @@ local Rbxm = {
-- ------------------------------------------------------------------------------------------------ types
export type DecodeOptions = {
maxBytes: number?, -- the largest decompressed chunk (default 2^28)
maxBytes: number?, -- default 2^28
maxInstances: number?, -- default 2^22
}
export type EncodeOptions = {
compression: ("lz4" | "none")?, -- chunk bodies (default "lz4"; END is always raw)
place: boolean?, -- mark the roots as services (an .rbxl; default tree.place, or format = "rbxl")
format: string?, -- "rbxl" writes a place
compression: ("lz4" | "none")?, -- default "lz4"; END is always raw
place: boolean?, -- default tree.place, or format = "rbxl"
format: string?,
}
-- ------------------------------------------------------------------------------------------------ constants
@@ -50,7 +42,7 @@ function Rbxm.sniff(r: Bytes.Reader): number
return if r:peek(14) == MAGIC then 1 else 0
end
-- variant(r) -> "rbxl" for a place (an INST chunk marks services), else "rbxm" (F.detect); reads only the INST chunks
-- an INST chunk that marks services makes a place
function Rbxm.variant(r: Bytes.Reader): string?
local b, p, e = r.buf, r.base + 32, r.limit
if r:peek(14) ~= MAGIC then
@@ -83,7 +75,6 @@ end
-- ------------------------------------------------------------------------------------------------ decode
type Class = { id: number, name: string, refs: { number }, service: boolean }
-- a length-prefixed string at p of b (limit e): string, next | nil
local function str(b: buffer, p: number, e: number): (string?, number)
if p + 4 > e then
return nil, p
@@ -292,7 +283,6 @@ function Rbxm.decode(r: Bytes.Reader, opts: DecodeOptions?): (Rbx.Tree?, Err.Err
return nil, Err.new(Err.CORRUPT, "an instance with two parents", at)
end
parented[child] = true
-- refuse cycles: the parent may not be a descendant of the child
local up: Rbx.Instance? = parent
while up do
if up == child then
@@ -311,7 +301,6 @@ function Rbxm.decode(r: Bytes.Reader, opts: DecodeOptions?): (Rbx.Tree?, Err.Err
if not ended then
return nil, Err.new(Err.TRUNCATED, "no END chunk", limit)
end
-- instances PRNT does not mention are roots
for _, inst in order do
if not parented[inst] then
table.insert(tree.roots, inst)
@@ -329,7 +318,6 @@ function Rbxm.decode(r: Bytes.Reader, opts: DecodeOptions?): (Rbx.Tree?, Err.Err
end
-- ------------------------------------------------------------------------------------------------ encode
-- the binary type of a property over a class's instances: type id, type name | nil, message
local function columnType(insts: { Rbx.Instance }, pname: string): (number?, string?)
local id, tname = nil, nil
for _, inst in insts do
@@ -381,7 +369,6 @@ function Rbxm.encode(tree: Rbx.Tree, opts: EncodeOptions?): (buffer?, Err.Err?)
local o: any = opts or {}
local compress = o.compression ~= "none"
local place = if o.place ~= nil then o.place == true elseif o.format == "rbxl" then true else tree.place == true
-- every instance depth first; referents in that order
local insts: { Rbx.Instance } = {}
local refOf: { [any]: number } = {}
local parentOf: { [Rbx.Instance]: Rbx.Instance? } = {}
@@ -409,7 +396,6 @@ function Rbxm.encode(tree: Rbx.Tree, opts: EncodeOptions?): (buffer?, Err.Err?)
return nil, e
end
end
-- classes by name
local byClass: { [string]: { Rbx.Instance } } = {}
local names = {}
for _, inst in insts do
@@ -422,7 +408,6 @@ function Rbxm.encode(tree: Rbx.Tree, opts: EncodeOptions?): (buffer?, Err.Err?)
table.insert(list, inst)
end
table.sort(names)
-- shared strings, in order of first use
local sharedIdx: { [string]: number } = {}
local sharedList: { string } = {}
local ctx: Binary.Ctx = {
@@ -497,7 +482,7 @@ function Rbxm.encode(tree: Rbx.Tree, opts: EncodeOptions?): (buffer?, Err.Err?)
putStr(pw, pn)
local tid = t :: number
if tid == -1 then
-- Raw: the column read from a file, written back only when this class still has exactly its instances
-- Raw: written back only when this class still has exactly its instances
local first = list[1].properties[pn]
local raw = first and first.value
local ok = type(raw) == "table" and raw.count == #list and type(raw.data) == "buffer"