282 lines
11 KiB
Luau
282 lines
11 KiB
Luau
--!strict
|
|
-- codecs/model/Rbx: the Roblox instance tree every Roblox file format maps to (model/rbxm binary rbxm / rbxl,
|
|
-- model/rbxmx XML rbxmx / rbxlx), the typed property values, the CFrame rotation IDs, and (codecs/model/Rbx/Binary) the
|
|
-- binary format's property column codecs. Shared here because a format cannot require another one. Written from the
|
|
-- rbx-dom format notes (docs/binary.md, docs/xml.md) and checked against rbx-dom through rojo / lune. A file library,
|
|
-- not a Roblox runtime: nothing here creates Instances; a Roblox game reads the tree itself.
|
|
--
|
|
-- local tree = Rbx.tree() -- { kind = "rbxtree", roots = {}, place = false, ... }
|
|
-- local part = Rbx.instance("Part", "Floor") -- { className, name, properties = {}, children = {} }
|
|
-- Rbx.set(part, "Size", "Vector3", { 64, 1, 64 }) -- properties.Size = { type = "Vector3", value = {...} }
|
|
-- Rbx.add(tree, part) -- a root; Rbx.add(parentInstance, child) nests
|
|
-- for inst, depth in Rbx.descendants(tree) do ... end
|
|
--
|
|
-- Property values are { type = <name>, value = <v> } with these value forms (arrays follow the Roblox constructor
|
|
-- argument order, so Vector3.new(table.unpack(v.value)) / CFrame.new(table.unpack(v.value)) rebuild them in Roblox):
|
|
-- String, BinaryString, ProtectedString, ContentId, Bytecode string (bytes)
|
|
-- SharedString string (the content; writers deduplicate)
|
|
-- Bool boolean
|
|
-- Int32, Int64, Float32, Float64 number (Int64 exact to 2^53)
|
|
-- Enum, BrickColor, SecurityCapabilities number
|
|
-- Faces number: bits Right 1, Top 2, Back 4, Left 8, Bottom 16, Front 32
|
|
-- Axes number: bits X 1, Y 2, Z 4
|
|
-- UDim { scale, offset } UDim2 { xScale, xOffset, yScale, yOffset } NumberRange { min, max }
|
|
-- Vector2 / Vector2int16 { x, y } Vector3 / Vector3int16 { x, y, z } Color3 { r, g, b } (0..1)
|
|
-- Color3uint8 { r, g, b } (0..255) Rect { minX, minY, maxX, maxY } Ray { ox, oy, oz, dx, dy, dz }
|
|
-- CFrame { x, y, z, r00, r01, r02, r10, r11, r12, r20, r21, r22 } OptionalCFrame: the same, or nil (none)
|
|
-- NumberSequence { { time, value, envelope } ... } ColorSequence { { time, r, g, b, envelope } ... }
|
|
-- PhysicalProperties nil (default) | { density, friction, elasticity, frictionWeight, elasticityWeight,
|
|
-- acousticAbsorption } (decoders fill acousticAbsorption = 1 when the file leaves it out)
|
|
-- Ref an Instance of the tree, or nil
|
|
-- UniqueId 32 hex digits: random (16), time (8), index (8)
|
|
-- Font { family, weight (100..900), style ("Normal" | "Italic"), cachedFaceId? }
|
|
-- Content nil (none) | string (a URI) | an Instance (object)
|
|
-- Raw a binary property of a type this library does not know: { typeId, data (the class's whole column),
|
|
-- index, count }; written back verbatim while the class keeps the same instances in the same order
|
|
-- XmlRaw an XML property of an unknown type: value = the element (codecs/text/Xml node), written back as it is
|
|
-- Name is not a property here: it is inst.name. Never throws.
|
|
|
|
local Rbx = {}
|
|
|
|
export type Value = { type: string, value: any }
|
|
export type Instance = {
|
|
className: string,
|
|
name: string,
|
|
properties: { [string]: Value },
|
|
children: { Instance },
|
|
parent: Instance?, -- nil for roots
|
|
referent: string?, -- the file's referent (XML "RBX..." id, binary index) when read; writers make their own
|
|
service: boolean?, -- a service (binary INST object format 1; rbxl roots)
|
|
}
|
|
export type Tree = {
|
|
kind: "rbxtree",
|
|
roots: { Instance },
|
|
place: boolean, -- a place (rbxl / rbxlx: the roots are services under the DataModel) or a model
|
|
metadata: { [string]: string }, -- binary META / XML <Meta> entries (ExplicitAutoJoints, ...)
|
|
extra: { any }, -- unknown binary chunks { name, data } / unknown top-level XML elements, written back
|
|
meta: { source: { format: string, [string]: any }?, [string]: any },
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ tree
|
|
function Rbx.tree(place: boolean?): Tree
|
|
return { kind = "rbxtree", roots = {}, place = place == true, metadata = {}, extra = {}, meta = {} }
|
|
end
|
|
|
|
function Rbx.instance(className: string, name: string?): Instance
|
|
return { className = className, name = name or className, properties = {}, children = {} }
|
|
end
|
|
|
|
-- add(parent, child) -> child: parent is a Tree (child becomes a root) or an Instance
|
|
function Rbx.add(parent: any, child: Instance): Instance
|
|
if parent.kind == "rbxtree" then
|
|
child.parent = nil
|
|
table.insert(parent.roots, child)
|
|
else
|
|
child.parent = parent
|
|
table.insert(parent.children, child)
|
|
end
|
|
return child
|
|
end
|
|
|
|
-- set(inst, name, type, value): a property (value nil = the type's nil form: Ref none, OptionalCFrame none, ...)
|
|
function Rbx.set(inst: Instance, name: string, typ: string, value: any)
|
|
inst.properties[name] = { type = typ, value = value }
|
|
end
|
|
|
|
-- get(inst, name) -> the property's value (not the Value table), nil when absent
|
|
function Rbx.get(inst: Instance, name: string): any
|
|
local v = inst.properties[name]
|
|
return if v then v.value else nil
|
|
end
|
|
|
|
-- descendants(treeOrInstance) -> iterator over (instance, depth) in depth-first order (roots / the instance's children
|
|
-- at depth 1)
|
|
function Rbx.descendants(root: any): () -> (Instance?, number?)
|
|
local stack: { any } = {}
|
|
local list = if root.kind == "rbxtree" then root.roots else root.children
|
|
for i = #list, 1, -1 do
|
|
table.insert(stack, { list[i], 1 })
|
|
end
|
|
return function(): (Instance?, number?)
|
|
local top = table.remove(stack)
|
|
if not top then
|
|
return nil, nil
|
|
end
|
|
local inst, depth = top[1], top[2]
|
|
for i = #inst.children, 1, -1 do
|
|
table.insert(stack, { inst.children[i], depth + 1 })
|
|
end
|
|
return inst, depth
|
|
end
|
|
end
|
|
|
|
-- list(tree) -> every instance, depth first
|
|
function Rbx.list(tree: any): { Instance }
|
|
local out = {}
|
|
for inst in Rbx.descendants(tree) do
|
|
table.insert(out, inst :: Instance)
|
|
end
|
|
return out
|
|
end
|
|
|
|
-- find(treeOrInstance, path) -> the instance at a "/"-separated path of names ("Workspace/Baseplate"), or nil
|
|
function Rbx.find(root: any, path: string): Instance?
|
|
local cur: any = root
|
|
for part in string.gmatch(path, "[^/]+") do
|
|
local list = if cur.kind == "rbxtree" then cur.roots else cur.children
|
|
local found = nil
|
|
for _, c in list do
|
|
if c.name == part then
|
|
found = c
|
|
break
|
|
end
|
|
end
|
|
if not found then
|
|
return nil
|
|
end
|
|
cur = found
|
|
end
|
|
return if cur.kind == "rbxtree" then nil else cur
|
|
end
|
|
|
|
-- fullName(inst) -> "Workspace.Model.Part" (names joined up to the root)
|
|
function Rbx.fullName(inst: Instance): string
|
|
local parts = {}
|
|
local cur: Instance? = inst
|
|
while cur do
|
|
table.insert(parts, 1, cur.name)
|
|
cur = cur.parent
|
|
end
|
|
return table.concat(parts, ".")
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ CFrame rotation ids
|
|
-- The binary format stores axis-aligned rotations as one byte: id = 6 * n(RightVector) + n(UpVector) + 1, where n is
|
|
-- the NormalId of a unit axis (+X 0, +Y 1, +Z 2, -X 3, -Y 4, -Z 5) and the third column is their cross product.
|
|
local AXES = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }, { -1, 0, 0 }, { 0, -1, 0 }, { 0, 0, -1 } }
|
|
|
|
-- the rotation of an id as { r00 .. r22 } (row major), or nil for an id that is not a rotation
|
|
local ROTATIONS: { [number]: { number } } = {}
|
|
for a = 0, 5 do
|
|
for b = 0, 5 do
|
|
if a % 3 ~= b % 3 then
|
|
local r, u = AXES[a + 1], AXES[b + 1]
|
|
local k = { r[2] * u[3] - r[3] * u[2], r[3] * u[1] - r[1] * u[3], r[1] * u[2] - r[2] * u[1] }
|
|
-- columns: right, up, back (= right x up)
|
|
ROTATIONS[6 * a + b + 1] = { r[1], u[1], k[1] + 0, r[2], u[2], k[2] + 0, r[3], u[3], k[3] + 0 }
|
|
end
|
|
end
|
|
end
|
|
Rbx.ROTATIONS = ROTATIONS
|
|
|
|
local function axisIndex(x: number, y: number, z: number): number?
|
|
for i, a in AXES do
|
|
if a[1] == x and a[2] == y and a[3] == z then
|
|
return i - 1
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
-- rotationId(cf) -> the id of the CFrame's rotation (cf = { x, y, z, r00 .. r22 }), or 0 when it is not axis aligned
|
|
function Rbx.rotationId(cf: { number }): number
|
|
local a = axisIndex(cf[4], cf[7], cf[10])
|
|
local b = axisIndex(cf[5], cf[8], cf[11])
|
|
if not a or not b then
|
|
return 0
|
|
end
|
|
local id = 6 * a + b + 1
|
|
local rot = ROTATIONS[id]
|
|
if not rot or rot[3] ~= cf[6] or rot[6] ~= cf[9] or rot[9] ~= cf[12] then
|
|
return 0
|
|
end
|
|
return id
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ values
|
|
-- binary type ids (rbx-dom docs/binary.md) by type name, and back
|
|
Rbx.TYPE_IDS = table.freeze({
|
|
String = 0x01,
|
|
BinaryString = 0x01,
|
|
ProtectedString = 0x01,
|
|
ContentId = 0x01,
|
|
Bool = 0x02,
|
|
Int32 = 0x03,
|
|
Float32 = 0x04,
|
|
Float64 = 0x05,
|
|
UDim = 0x06,
|
|
UDim2 = 0x07,
|
|
Ray = 0x08,
|
|
Faces = 0x09,
|
|
Axes = 0x0A,
|
|
BrickColor = 0x0B,
|
|
Color3 = 0x0C,
|
|
Vector2 = 0x0D,
|
|
Vector3 = 0x0E,
|
|
Vector2int16 = 0x0F,
|
|
CFrame = 0x10,
|
|
Enum = 0x12,
|
|
Ref = 0x13,
|
|
Vector3int16 = 0x14,
|
|
NumberSequence = 0x15,
|
|
ColorSequence = 0x16,
|
|
NumberRange = 0x17,
|
|
Rect = 0x18,
|
|
PhysicalProperties = 0x19,
|
|
Color3uint8 = 0x1A,
|
|
Int64 = 0x1B,
|
|
SharedString = 0x1C,
|
|
Bytecode = 0x1D,
|
|
OptionalCFrame = 0x1E,
|
|
UniqueId = 0x1F,
|
|
Font = 0x20,
|
|
SecurityCapabilities = 0x21,
|
|
Content = 0x22,
|
|
} :: { [string]: number })
|
|
local NAMES: { [number]: string } = {}
|
|
for name, id in Rbx.TYPE_IDS do
|
|
if NAMES[id] == nil or name == "String" then
|
|
NAMES[id] = name
|
|
end
|
|
end
|
|
Rbx.TYPE_NAMES = table.freeze(NAMES)
|
|
|
|
-- the value a binary writer uses for an instance without the property (zero / empty / identity; BrickColor 194, the
|
|
-- Medium stone grey Roblox defaults to; sequences of two keypoints at times 0 and 1)
|
|
local IDENTITY_CF = { 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1 }
|
|
function Rbx.default(typ: string): any
|
|
if typ == "String" or typ == "BinaryString" or typ == "ProtectedString" or typ == "ContentId" or typ == "Bytecode" or typ == "SharedString" then
|
|
return ""
|
|
elseif typ == "Bool" then
|
|
return false
|
|
elseif typ == "BrickColor" then
|
|
return 194
|
|
elseif typ == "UDim" or typ == "NumberRange" or typ == "Vector2" or typ == "Vector2int16" then
|
|
return { 0, 0 }
|
|
elseif typ == "UDim2" or typ == "Rect" then
|
|
return { 0, 0, 0, 0 }
|
|
elseif typ == "Vector3" or typ == "Vector3int16" or typ == "Color3" or typ == "Color3uint8" then
|
|
return { 0, 0, 0 }
|
|
elseif typ == "Ray" then
|
|
return { 0, 0, 0, 0, 0, 0 }
|
|
elseif typ == "CFrame" then
|
|
return table.clone(IDENTITY_CF)
|
|
elseif typ == "NumberSequence" then
|
|
return { { 0, 0, 0 }, { 1, 0, 0 } }
|
|
elseif typ == "ColorSequence" then
|
|
return { { 0, 0, 0, 0, 0 }, { 1, 0, 0, 0, 0 } }
|
|
elseif typ == "UniqueId" then
|
|
return string.rep("0", 32)
|
|
elseif typ == "Font" then
|
|
return { family = "rbxasset://fonts/families/SourceSansPro.json", weight = 400, style = "Normal" }
|
|
elseif typ == "PhysicalProperties" or typ == "Ref" or typ == "OptionalCFrame" or typ == "Content" then
|
|
return nil
|
|
end
|
|
return 0
|
|
end
|
|
|
|
-- STRINGS: the string-valued types (one binary type, several XML tags)
|
|
Rbx.STRINGS = table.freeze({ String = true, BinaryString = true, ProtectedString = true, ContentId = true, Bytecode = true } :: { [string]: boolean })
|
|
|
|
return Rbx
|