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,667 @@
|
||||
--!strict
|
||||
-- model/rbxmx: the Roblox XML model / place format (.rbxmx / .rbxlx, `<roblox version="4">`), read + write, to and from
|
||||
-- the instance tree of codecs/model/Rbx (an RbxTree, as model/rbxm: converting between the two is load + save).
|
||||
-- Written from the rbx-dom format notes (docs/xml.md); checked against rojo 7.7 and lune 0.11 (rbx-dom) both ways.
|
||||
-- Decode: <Meta>, <Item class referent> with <Properties> of every type codecs/model/Rbx lists (string, BinaryString
|
||||
-- (base64), ProtectedString, Content (url / uri / null), ContentId, bool, int, int64, float, double (INF / NAN),
|
||||
-- token, BrickColor (as rbx-dom: an <int>; read back as BrickColor for the BrickColor properties of a short
|
||||
-- built-in list), Color3 (components or packed ARGB), Color3uint8, Vector2 / 3 (int16), CoordinateFrame,
|
||||
-- OptionalCoordinateFrame, UDim / UDim2, Rect2D, Ray, Faces, Axes, NumberSequence, ColorSequence, NumberRange,
|
||||
-- PhysicalProperties, Ref, SharedString (+ <SharedStrings>), UniqueId, Font, SecurityCapabilities); unknown property
|
||||
-- tags are kept as "XmlRaw" values and written back as they are; unknown top-level elements go to tree.extra.
|
||||
-- tree.place is true when a root is a Workspace (rbxlx).
|
||||
-- Encode: rbx-dom's layout (version 4, referents "RBX" + 32 hex digits, properties sorted by name, two-space indent,
|
||||
-- floats as the shortest text that reads back to the same f32 / f64). A String holding bytes XML cannot carry (control
|
||||
-- characters, invalid UTF-8) is written as a BinaryString. "Raw" values (unknown binary types) are skipped.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Base64 = require("@rosetta/core/Base64")
|
||||
local Md5 = require("@rosetta/core/Md5")
|
||||
local Xml = require("@rosetta/codecs/text/Xml")
|
||||
local Rbx = require("@rosetta/codecs/model/Rbx")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Rbxmx = {
|
||||
name = "rbxmx",
|
||||
category = "model",
|
||||
extensions = { "rbxmx", "rbxlx" },
|
||||
mime = { "application/x-roblox-rbxmx", "application/x-roblox-rbxlx" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Base64", "core/Md5", "codecs/text/Xml", "codecs/model/Rbx" },
|
||||
caps = { decode = true, encode = true, tree = true },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
maxDepth: number?, -- XML nesting limit (default 10000)
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
indent: (number | string)?, -- default 2 spaces; 0 for none
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local XmlNode = Xml.element
|
||||
local find, format, match, gmatch = string.find, string.format, string.match, string.gmatch
|
||||
|
||||
-- tag name -> type name for the simple (text) types
|
||||
local SCALAR_TAGS: { [string]: string } = {
|
||||
string = "String",
|
||||
ProtectedString = "ProtectedString",
|
||||
bool = "Bool",
|
||||
int = "Int32",
|
||||
int64 = "Int64",
|
||||
float = "Float32",
|
||||
double = "Float64",
|
||||
token = "Enum",
|
||||
BrickColor = "BrickColor",
|
||||
SecurityCapabilities = "SecurityCapabilities",
|
||||
UniqueId = "UniqueId",
|
||||
}
|
||||
-- BrickColor properties: XML writes them as <int> (rbx-dom reads them through its reflection database); these known ones
|
||||
-- read back as BrickColor (the rest of an <int> stays Int32)
|
||||
local BRICKCOLOR: { [string]: boolean } = {
|
||||
["BrickColorValue.Value"] = true,
|
||||
["SpawnLocation.TeamColor"] = true,
|
||||
["Team.TeamColor"] = true,
|
||||
["FlagStand.TeamColor"] = true,
|
||||
["Skin.SkinColor"] = true,
|
||||
}
|
||||
-- the strings rbx-dom writes as BinaryString (the rest decides by content)
|
||||
local BINARY_NAMES: { [string]: boolean } = { AttributesSerialize = true, Tags = true, MaterialColors = true, SmoothGrid = true, PhysicsGrid = true }
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Rbxmx.sniff(r: Bytes.Reader): number
|
||||
local head = r:peek(256)
|
||||
head = string.gsub(head, "^\239\187\191", "")
|
||||
head = string.gsub(head, "^%s*<%?xml[^>]*%?>", "")
|
||||
if match(head, "^%s*<roblox[%s>]") then
|
||||
return if find(head, "version=", 1, true) then 1 else 0.8
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
-- variant(r) -> "rbxlx" for a place (a Workspace item), else "rbxmx" (F.detect)
|
||||
function Rbxmx.variant(r: Bytes.Reader): string?
|
||||
local text = buffer.readstring(r.buf, r.base, r.limit - r.base)
|
||||
return if find(text, '<Item class="Workspace"', 1, true) then "rbxlx" else "rbxmx"
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
local function num(s: string?): number
|
||||
if s == nil then
|
||||
return 0
|
||||
end
|
||||
local t = string.lower((string.gsub(s, "^%s+", "")))
|
||||
t = string.gsub(t, "%s+$", "")
|
||||
if t == "inf" or t == "+inf" or t == "infinity" then
|
||||
return math.huge
|
||||
elseif t == "-inf" or t == "-infinity" then
|
||||
return -math.huge
|
||||
elseif t == "nan" or t == "-nan" then
|
||||
return 0 / 0
|
||||
end
|
||||
return tonumber(t) or 0
|
||||
end
|
||||
|
||||
local function childText(node: Xml.Node, name: string): string?
|
||||
local c = Xml.child(node, name)
|
||||
return if c then Xml.text(c) else nil
|
||||
end
|
||||
local function comps(node: Xml.Node?, names: { string }): { number }
|
||||
local out = table.create(#names, 0)
|
||||
if node then
|
||||
for i, n in names do
|
||||
out[i] = num(childText(node, n))
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
local function numbers(s: string): { number }
|
||||
local out = {}
|
||||
for w in gmatch(s, "%S+") do
|
||||
table.insert(out, num(w))
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
local CF_NAMES = { "X", "Y", "Z", "R00", "R01", "R02", "R10", "R11", "R12", "R20", "R21", "R22" }
|
||||
local PHYS_NAMES = { "Density", "Friction", "Elasticity", "FrictionWeight", "ElasticityWeight" }
|
||||
|
||||
-- a property element -> type name, value | nil (unknown: XmlRaw). refs: referent -> instance (filled later: Refs
|
||||
-- come back as { pending = referent })
|
||||
local function readRaw(node: Xml.Node, shared: { [string]: string }, key: string): (string, any)
|
||||
local tag = node.name
|
||||
local scalar = SCALAR_TAGS[tag]
|
||||
if tag == "int" and BRICKCOLOR[key] then
|
||||
scalar = "BrickColor"
|
||||
end
|
||||
if scalar then
|
||||
local s = Xml.text(node)
|
||||
if scalar == "String" or scalar == "ProtectedString" then
|
||||
return scalar, s
|
||||
elseif scalar == "Bool" then
|
||||
return scalar, match(s, "^%s*true%s*$") ~= nil
|
||||
elseif scalar == "UniqueId" then
|
||||
return scalar, string.lower((string.gsub(s, "%s", "")))
|
||||
end
|
||||
return scalar, num(s)
|
||||
elseif tag == "BinaryString" then
|
||||
local data = Base64.decode((string.gsub(Xml.text(node), "%s", "")))
|
||||
return "BinaryString", if data then buffer.tostring(data) else ""
|
||||
elseif tag == "Content" or tag == "ContentId" then
|
||||
local url = Xml.child(node, "url")
|
||||
if url then
|
||||
return "ContentId", Xml.text(url)
|
||||
end
|
||||
local uri = Xml.child(node, "uri")
|
||||
if uri then
|
||||
return "Content", Xml.text(uri)
|
||||
end
|
||||
local obj = Xml.child(node, "Object") or Xml.child(node, "Ref")
|
||||
if obj then
|
||||
return "Content", { pending = Xml.text(obj) }
|
||||
end
|
||||
if Xml.child(node, "null") then
|
||||
return (if tag == "ContentId" then "ContentId" else "Content"), (if tag == "ContentId" then "" else nil)
|
||||
end
|
||||
return "ContentId", Xml.text(node)
|
||||
elseif tag == "Vector2" or tag == "Vector2int16" then
|
||||
return tag, comps(node, { "X", "Y" })
|
||||
elseif tag == "Vector3" or tag == "Vector3int16" then
|
||||
return tag, comps(node, { "X", "Y", "Z" })
|
||||
elseif tag == "Color3" then
|
||||
if Xml.child(node, "R") then
|
||||
return tag, comps(node, { "R", "G", "B" })
|
||||
end
|
||||
local v = num(Xml.text(node)) % 4294967296
|
||||
return tag, { (v // 65536) % 256 / 255, (v // 256) % 256 / 255, v % 256 / 255 }
|
||||
elseif tag == "Color3uint8" then
|
||||
local v = num(Xml.text(node)) % 4294967296
|
||||
return tag, { (v // 65536) % 256, (v // 256) % 256, v % 256 }
|
||||
elseif tag == "CoordinateFrame" or tag == "CFrame" then
|
||||
return "CFrame", comps(node, CF_NAMES)
|
||||
elseif tag == "OptionalCoordinateFrame" then
|
||||
local cf = Xml.child(node, "CFrame")
|
||||
return "OptionalCFrame", if cf then comps(cf, CF_NAMES) else nil
|
||||
elseif tag == "UDim" then
|
||||
return tag, comps(node, { "S", "O" })
|
||||
elseif tag == "UDim2" then
|
||||
return tag, comps(node, { "XS", "XO", "YS", "YO" })
|
||||
elseif tag == "Rect2D" then
|
||||
local a, b = comps(Xml.child(node, "min"), { "X", "Y" }), comps(Xml.child(node, "max"), { "X", "Y" })
|
||||
return "Rect", { a[1], a[2], b[1], b[2] }
|
||||
elseif tag == "Ray" then
|
||||
local a, b = comps(Xml.child(node, "origin"), { "X", "Y", "Z" }), comps(Xml.child(node, "direction"), { "X", "Y", "Z" })
|
||||
return tag, { a[1], a[2], a[3], b[1], b[2], b[3] }
|
||||
elseif tag == "Faces" or tag == "Axes" then
|
||||
local t = childText(node, string.lower(tag))
|
||||
return tag, num(t or Xml.text(node))
|
||||
elseif tag == "NumberRange" then
|
||||
local v = numbers(Xml.text(node))
|
||||
return tag, { v[1] or 0, v[2] or 0 }
|
||||
elseif tag == "NumberSequence" or tag == "ColorSequence" then
|
||||
local k = if tag == "NumberSequence" then 3 else 5
|
||||
local v = numbers(Xml.text(node))
|
||||
local seq = {}
|
||||
for i = 1, #v - k + 1, k do
|
||||
table.insert(seq, table.move(v, i, i + k - 1, 1, table.create(k, 0)))
|
||||
end
|
||||
return tag, seq
|
||||
elseif tag == "PhysicalProperties" then
|
||||
local custom = childText(node, "CustomPhysics")
|
||||
if not custom or not match(custom, "^%s*true%s*$") then
|
||||
return tag, nil
|
||||
end
|
||||
local v = comps(node, PHYS_NAMES)
|
||||
local a = childText(node, "AcousticAbsorption")
|
||||
v[6] = if a then num(a) else 1
|
||||
return tag, v
|
||||
elseif tag == "Ref" then
|
||||
local s = string.gsub(Xml.text(node), "%s", "")
|
||||
return tag, if s == "" or s == "null" then nil else { pending = s }
|
||||
elseif tag == "SharedString" then
|
||||
local key = string.gsub(Xml.text(node), "%s", "")
|
||||
return tag, shared[key] or ""
|
||||
elseif tag == "Font" then
|
||||
local fam = Xml.child(node, "Family")
|
||||
local face = Xml.child(node, "CachedFaceId")
|
||||
local f = {
|
||||
family = if fam then childText(fam, "url") or Xml.text(fam) else "",
|
||||
weight = num(childText(node, "Weight") or "400"),
|
||||
style = if match(childText(node, "Style") or "", "Italic") then "Italic" else "Normal",
|
||||
cachedFaceId = if face then childText(face, "url") or nil else nil,
|
||||
}
|
||||
if f.cachedFaceId == "" then
|
||||
f.cachedFaceId = nil
|
||||
end
|
||||
return tag, f
|
||||
end
|
||||
return "XmlRaw", node
|
||||
end
|
||||
|
||||
-- single-precision types: their components rounded to f32 (as a binary file stores them), so XML and binary
|
||||
-- decodes of the same data are equal. Which components are floats, by type (true = all)
|
||||
local F32: { [string]: any } = {
|
||||
Float32 = true,
|
||||
Vector2 = true,
|
||||
Vector3 = true,
|
||||
Color3 = true,
|
||||
CFrame = true,
|
||||
OptionalCFrame = true,
|
||||
Rect = true,
|
||||
Ray = true,
|
||||
NumberRange = true,
|
||||
NumberSequence = true,
|
||||
ColorSequence = true,
|
||||
PhysicalProperties = true,
|
||||
UDim = { true, false },
|
||||
UDim2 = { true, false, true, false },
|
||||
}
|
||||
local f32buf = buffer.create(4)
|
||||
local function f32(v: number): number
|
||||
buffer.writef32(f32buf, 0, v)
|
||||
return buffer.readf32(f32buf, 0)
|
||||
end
|
||||
local function round32(v: any, which: any): any
|
||||
if type(v) == "number" then
|
||||
return f32(v)
|
||||
elseif type(v) == "table" then
|
||||
for i, x in v do
|
||||
if type(x) == "table" then
|
||||
round32(x, true)
|
||||
elseif type(x) == "number" and (which == true or which[i]) then
|
||||
v[i] = f32(x)
|
||||
end
|
||||
end
|
||||
end
|
||||
return v
|
||||
end
|
||||
local function readValue(node: Xml.Node, shared: { [string]: string }, key: string): (string, any)
|
||||
local t, v = readRaw(node, shared, key)
|
||||
local which = F32[t]
|
||||
if which then
|
||||
v = round32(v, which)
|
||||
end
|
||||
return t, v
|
||||
end
|
||||
|
||||
function Rbxmx.decode(r: Bytes.Reader, opts: DecodeOptions?): (Rbx.Tree?, Err.Err?)
|
||||
local text = buffer.readstring(r.buf, r.base, r.limit - r.base)
|
||||
local doc, err = Xml.decode(text, { maxDepth = opts and opts.maxDepth } :: any)
|
||||
if not doc then
|
||||
return nil, err
|
||||
end
|
||||
local root = (doc :: any).root
|
||||
if not root or root.name ~= "roblox" then
|
||||
return nil, Err.new(Err.CORRUPT, "the root element is not <roblox>")
|
||||
end
|
||||
local tree = Rbx.tree()
|
||||
tree.meta.source = { format = "rbxmx", version = root.attrs.version }
|
||||
-- shared strings first (properties name them by key)
|
||||
local shared: { [string]: string } = {}
|
||||
for _, ss in Xml.children(root, "SharedStrings") do
|
||||
for _, s in Xml.children(ss, "SharedString") do
|
||||
local key = s.attrs.md5
|
||||
local data = Base64.decode((string.gsub(Xml.text(s), "%s", "")))
|
||||
if key and data then
|
||||
shared[key] = buffer.tostring(data)
|
||||
end
|
||||
end
|
||||
end
|
||||
local byRef: { [string]: Rbx.Instance } = {}
|
||||
local pending: { { any } } = {} -- { value table, field, referent }
|
||||
local function item(node: Xml.Node, parent: any): Err.Err?
|
||||
local cls = node.attrs.class
|
||||
if type(cls) ~= "string" then
|
||||
return Err.new(Err.CORRUPT, "an <Item> without a class")
|
||||
end
|
||||
local inst = Rbx.instance(cls)
|
||||
local ref = node.attrs.referent
|
||||
if type(ref) == "string" then
|
||||
inst.referent = ref
|
||||
byRef[ref] = inst
|
||||
end
|
||||
Rbx.add(parent, inst)
|
||||
for _, c in node.children do
|
||||
if type(c) == "table" then
|
||||
if c.name == "Properties" then
|
||||
for _, pnode in c.children do
|
||||
if type(pnode) == "table" and string.sub(pnode.name, 1, 1) ~= "#" then
|
||||
local pname = pnode.attrs.name
|
||||
if type(pname) == "string" then
|
||||
local t, v = readValue(pnode, shared, cls .. "." .. pname)
|
||||
if pname == "Name" and (t == "String" or t == "ProtectedString") then
|
||||
inst.name = v
|
||||
else
|
||||
local val = { type = t, value = v }
|
||||
inst.properties[pname] = val
|
||||
if type(v) == "table" and v.pending then
|
||||
table.insert(pending, { val, v.pending })
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
elseif c.name == "Item" then
|
||||
local e = item(c, inst)
|
||||
if e then
|
||||
return e
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
for _, c in root.children do
|
||||
if type(c) == "table" then
|
||||
if c.name == "Item" then
|
||||
local e = item(c, tree)
|
||||
if e then
|
||||
return nil, e
|
||||
end
|
||||
elseif c.name == "Meta" then
|
||||
if type(c.attrs.name) == "string" then
|
||||
tree.metadata[c.attrs.name] = Xml.text(c)
|
||||
end
|
||||
elseif c.name ~= "SharedStrings" and string.sub(c.name, 1, 1) ~= "#" then
|
||||
table.insert(tree.extra, c)
|
||||
end
|
||||
end
|
||||
end
|
||||
for _, p in pending do
|
||||
p[1].value = byRef[p[2]]
|
||||
end
|
||||
for _, rt in tree.roots do
|
||||
if rt.className == "Workspace" then
|
||||
tree.place = true
|
||||
end
|
||||
end
|
||||
return tree, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
-- the shortest text that reads back as the same float (single: f32 values; else f64)
|
||||
local function fmt(v: any, single: boolean?): string
|
||||
if type(v) ~= "number" then
|
||||
v = 0
|
||||
end
|
||||
if v ~= v then
|
||||
return "NAN"
|
||||
elseif v == math.huge then
|
||||
return "INF"
|
||||
elseif v == -math.huge then
|
||||
return "-INF"
|
||||
end
|
||||
if single then
|
||||
v = f32(v)
|
||||
end
|
||||
if v == math.floor(v) and math.abs(v) < 2 ^ 53 then
|
||||
return format("%.0f", v)
|
||||
end
|
||||
for d = if single then 6 else 15, if single then 9 else 17 do
|
||||
local s = format("%." .. d .. "g", v)
|
||||
local back = tonumber(s) or 0
|
||||
if (if single then f32(back) else back) == v then
|
||||
return s
|
||||
end
|
||||
end
|
||||
return format("%.17g", v)
|
||||
end
|
||||
local function int(v: any): string
|
||||
return format("%.0f", math.floor(if type(v) == "number" and v == v and math.abs(v) ~= math.huge then v else 0))
|
||||
end
|
||||
|
||||
local function textEl(tag: string, text: string): Xml.Node
|
||||
return XmlNode(tag, nil, { text })
|
||||
end
|
||||
local function compsEl(tag: string, attrs: { [string]: string }?, names: { string }, v: any, single: boolean?, ints: boolean?): Xml.Node
|
||||
local kids = {}
|
||||
for i, n in names do
|
||||
local x = if type(v) == "table" then v[i] else 0
|
||||
table.insert(kids, textEl(n, if ints then int(x) else fmt(x, single)))
|
||||
end
|
||||
return XmlNode(tag, attrs, kids)
|
||||
end
|
||||
|
||||
local function hexDigits(i: number): string
|
||||
return format("%032X", i)
|
||||
end
|
||||
|
||||
local function xmlSafe(s: string): boolean
|
||||
if find(s, "[%z\1-\8\11\12\14-\31\127]") then
|
||||
return false
|
||||
end
|
||||
return utf8.len(s) ~= nil
|
||||
end
|
||||
|
||||
-- a property -> its element, or nil to skip
|
||||
local function writeValue(name: string, v: any, refOf: (any) -> string, sharedKey: (string) -> string): Xml.Node?
|
||||
local t, x = v.type, v.value
|
||||
local a = { name = name }
|
||||
if t == "String" or t == "ProtectedString" or t == "BinaryString" or t == "Bytecode" then
|
||||
local s = if type(x) == "string" then x else ""
|
||||
if t == "BinaryString" or t == "Bytecode" or BINARY_NAMES[name] or not xmlSafe(s) then
|
||||
return XmlNode("BinaryString", a, { Base64.encode(s) })
|
||||
end
|
||||
return XmlNode(if t == "ProtectedString" then "ProtectedString" else "string", a, { s })
|
||||
elseif t == "ContentId" then
|
||||
local s = if type(x) == "string" then x else ""
|
||||
return XmlNode("ContentId", a, { if s == "" then XmlNode("null") else textEl("url", s) })
|
||||
elseif t == "Content" then
|
||||
if type(x) == "string" then
|
||||
return XmlNode("Content", a, { textEl("uri", x) })
|
||||
elseif type(x) == "table" then
|
||||
return XmlNode("Content", a, { textEl("Object", refOf(x)) })
|
||||
end
|
||||
return XmlNode("Content", a, { XmlNode("null") })
|
||||
elseif t == "Bool" then
|
||||
return XmlNode("bool", a, { if x then "true" else "false" })
|
||||
elseif t == "Int32" then
|
||||
return XmlNode("int", a, { int(x) })
|
||||
elseif t == "Int64" then
|
||||
return XmlNode("int64", a, { int(x) })
|
||||
elseif t == "Float32" then
|
||||
return XmlNode("float", a, { fmt(x, true) })
|
||||
elseif t == "Float64" then
|
||||
return XmlNode("double", a, { fmt(x) })
|
||||
elseif t == "Enum" then
|
||||
return XmlNode("token", a, { int(x) })
|
||||
elseif t == "BrickColor" then
|
||||
return XmlNode("int", a, { int(x) })
|
||||
elseif t == "SecurityCapabilities" then
|
||||
return XmlNode("SecurityCapabilities", a, { int(x) })
|
||||
elseif t == "UniqueId" then
|
||||
return XmlNode("UniqueId", a, { if type(x) == "string" then string.lower(x) else string.rep("0", 32) })
|
||||
elseif t == "Vector2" then
|
||||
return compsEl(t, a, { "X", "Y" }, x, true)
|
||||
elseif t == "Vector3" then
|
||||
return compsEl(t, a, { "X", "Y", "Z" }, x, true)
|
||||
elseif t == "Vector2int16" then
|
||||
return compsEl(t, a, { "X", "Y" }, x, false, true)
|
||||
elseif t == "Vector3int16" then
|
||||
return compsEl(t, a, { "X", "Y", "Z" }, x, false, true)
|
||||
elseif t == "Color3" then
|
||||
return compsEl(t, a, { "R", "G", "B" }, x, true)
|
||||
elseif t == "Color3uint8" then
|
||||
local c = if type(x) == "table" then x else { 0, 0, 0 }
|
||||
local function b(i: number): number
|
||||
return math.clamp(math.floor(tonumber(c[i]) or 0), 0, 255)
|
||||
end
|
||||
return XmlNode(t, a, { int(b(1) * 65536 + b(2) * 256 + b(3)) })
|
||||
elseif t == "CFrame" then
|
||||
return compsEl("CoordinateFrame", a, CF_NAMES, x, true)
|
||||
elseif t == "OptionalCFrame" then
|
||||
return XmlNode("OptionalCoordinateFrame", a, if type(x) == "table" then { compsEl("CFrame", nil, CF_NAMES, x, true) } else {})
|
||||
elseif t == "UDim" then
|
||||
local u = if type(x) == "table" then x else { 0, 0 }
|
||||
return XmlNode(t, a, { textEl("S", fmt(u[1], true)), textEl("O", int(u[2])) })
|
||||
elseif t == "UDim2" then
|
||||
local u = if type(x) == "table" then x else { 0, 0, 0, 0 }
|
||||
return XmlNode(t, a, { textEl("XS", fmt(u[1], true)), textEl("XO", int(u[2])), textEl("YS", fmt(u[3], true)), textEl("YO", int(u[4])) })
|
||||
elseif t == "Rect" then
|
||||
local u = if type(x) == "table" then x else { 0, 0, 0, 0 }
|
||||
return XmlNode("Rect2D", a, { compsEl("min", nil, { "X", "Y" }, { u[1], u[2] }, true), compsEl("max", nil, { "X", "Y" }, { u[3], u[4] }, true) })
|
||||
elseif t == "Ray" then
|
||||
local u = if type(x) == "table" then x else { 0, 0, 0, 0, 0, 0 }
|
||||
return XmlNode(t, a, { compsEl("origin", nil, { "X", "Y", "Z" }, { u[1], u[2], u[3] }, true), compsEl("direction", nil, { "X", "Y", "Z" }, { u[4], u[5], u[6] }, true) })
|
||||
elseif t == "Faces" or t == "Axes" then
|
||||
return XmlNode(t, a, { textEl(string.lower(t), int(x)) })
|
||||
elseif t == "NumberRange" or t == "NumberSequence" or t == "ColorSequence" then
|
||||
local parts = {}
|
||||
local list = if type(x) ~= "table" then {} elseif t == "NumberRange" then { x } else x
|
||||
for _, key in list do
|
||||
for _, c in key do
|
||||
table.insert(parts, fmt(c, true) .. " ")
|
||||
end
|
||||
end
|
||||
return XmlNode(t, a, { table.concat(parts) })
|
||||
elseif t == "PhysicalProperties" then
|
||||
if type(x) ~= "table" then
|
||||
return XmlNode(t, a, { textEl("CustomPhysics", "false") })
|
||||
end
|
||||
local kids = { textEl("CustomPhysics", "true") }
|
||||
for i, n in PHYS_NAMES do
|
||||
table.insert(kids, textEl(n, fmt(x[i], true)))
|
||||
end
|
||||
table.insert(kids, textEl("AcousticAbsorption", fmt(if x[6] ~= nil then x[6] else 1, true)))
|
||||
return XmlNode(t, a, kids)
|
||||
elseif t == "Ref" then
|
||||
return XmlNode("Ref", a, { if x ~= nil then refOf(x) else "null" })
|
||||
elseif t == "SharedString" then
|
||||
return XmlNode("SharedString", a, { sharedKey(if type(x) == "string" then x else "") })
|
||||
elseif t == "Font" then
|
||||
local f = if type(x) == "table" then x else Rbx.default("Font")
|
||||
local kids = {
|
||||
XmlNode("Family", nil, { textEl("url", if type(f.family) == "string" then f.family else "") }),
|
||||
textEl("Weight", int(f.weight or 400)),
|
||||
textEl("Style", if f.style == "Italic" then "Italic" else "Normal"),
|
||||
}
|
||||
if type(f.cachedFaceId) == "string" and f.cachedFaceId ~= "" then
|
||||
table.insert(kids, XmlNode("CachedFaceId", nil, { textEl("url", f.cachedFaceId) }))
|
||||
end
|
||||
return XmlNode("Font", a, kids)
|
||||
elseif t == "XmlRaw" then
|
||||
if type(x) == "table" and type(x.name) == "string" then
|
||||
local copy = XmlNode(x.name, nil, x.children)
|
||||
for k, val in x.attrs do
|
||||
Xml.setAttr(copy, k, val)
|
||||
end
|
||||
Xml.setAttr(copy, "name", name)
|
||||
return copy
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
function Rbxmx.encode(tree: Rbx.Tree, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
if type(tree) ~= "table" or tree.kind ~= "rbxtree" or type(tree.roots) ~= "table" then
|
||||
return nil, Err.new(Err.ARG, "expected an RbxTree (codecs/model/Rbx)")
|
||||
end
|
||||
local refs: { [any]: string } = {}
|
||||
local count = 0
|
||||
local function assign(inst: any): Err.Err?
|
||||
if type(inst) ~= "table" or type(inst.className) ~= "string" or type(inst.properties) ~= "table" then
|
||||
return Err.new(Err.ARG, "an instance needs className and properties")
|
||||
end
|
||||
if refs[inst] then
|
||||
return Err.new(Err.ARG, "an instance appears twice in the tree")
|
||||
end
|
||||
refs[inst] = "RBX" .. hexDigits(count)
|
||||
count += 1
|
||||
for _, c in inst.children or {} do
|
||||
local e = assign(c)
|
||||
if e then
|
||||
return e
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
for _, rt in tree.roots do
|
||||
local e = assign(rt)
|
||||
if e then
|
||||
return nil, e
|
||||
end
|
||||
end
|
||||
local function refOf(x: any): string
|
||||
return refs[x] or "null"
|
||||
end
|
||||
local sharedKeys: { [string]: string } = {}
|
||||
local sharedList: { string } = {}
|
||||
local function sharedKey(s: string): string
|
||||
local k = sharedKeys[s]
|
||||
if not k then
|
||||
local h = Md5.new()
|
||||
h:update(buffer.fromstring(s))
|
||||
k = Base64.encode(h:digest())
|
||||
sharedKeys[s] = k
|
||||
table.insert(sharedList, s)
|
||||
end
|
||||
return k
|
||||
end
|
||||
local function item(inst: Rbx.Instance): Xml.Node
|
||||
local props = { textEl("string", inst.name or inst.className) }
|
||||
props[1].attrs.name = "Name"
|
||||
local names: { string } = {}
|
||||
for k in inst.properties do
|
||||
if k ~= "Name" then
|
||||
table.insert(names, k)
|
||||
end
|
||||
end
|
||||
table.sort(names)
|
||||
for _, k in names do
|
||||
local v = inst.properties[k]
|
||||
if type(v) == "table" then
|
||||
local el = writeValue(k, v, refOf, sharedKey)
|
||||
if el then
|
||||
table.insert(props, el)
|
||||
end
|
||||
end
|
||||
end
|
||||
local kids = { XmlNode("Properties", nil, props) }
|
||||
for _, c in inst.children or {} do
|
||||
table.insert(kids, item(c))
|
||||
end
|
||||
local el = XmlNode("Item", nil, kids)
|
||||
Xml.setAttr(el, "class", inst.className)
|
||||
Xml.setAttr(el, "referent", refs[inst])
|
||||
return el
|
||||
end
|
||||
local root = XmlNode("roblox", nil, {})
|
||||
Xml.setAttr(root, "version", "4")
|
||||
local metaKeys = {}
|
||||
for k in tree.metadata or {} do
|
||||
table.insert(metaKeys, k)
|
||||
end
|
||||
table.sort(metaKeys)
|
||||
for _, k in metaKeys do
|
||||
local m = textEl("Meta", tostring((tree.metadata :: any)[k]))
|
||||
m.attrs.name = k
|
||||
Xml.append(root, m)
|
||||
end
|
||||
for _, rt in tree.roots do
|
||||
Xml.append(root, item(rt))
|
||||
end
|
||||
for _, x in tree.extra or {} do
|
||||
if type(x) == "table" and type(x.name) == "string" and type(x.children) == "table" then
|
||||
Xml.append(root, x)
|
||||
end
|
||||
end
|
||||
if #sharedList > 0 then
|
||||
local ss = XmlNode("SharedStrings", nil, {})
|
||||
for _, s in sharedList do
|
||||
local el = XmlNode("SharedString", nil, { Base64.encode(s) })
|
||||
Xml.setAttr(el, "md5", sharedKeys[s])
|
||||
Xml.append(ss, el)
|
||||
end
|
||||
Xml.append(root, ss)
|
||||
end
|
||||
local indent = if opts and opts.indent ~= nil then opts.indent else 2
|
||||
local s, err = Xml.encode(root, { indent = indent, declaration = false, selfClose = false })
|
||||
if not s then
|
||||
return nil, err
|
||||
end
|
||||
return buffer.fromstring(s), nil
|
||||
end
|
||||
|
||||
return Rbxmx
|
||||
Reference in new issue
Block a user