Files

258 lines
12 KiB
Luau

-- model/rbxm tests: rojo 7.7 / lune 0.11 binary models and places against lune's reading and their XML twins.
-- Fixtures: tools/fixtures/rbxmx.py.
local T = require("@tests/T")
local Rbxm = require("@rosetta/model/rbxm")
local Rbxmx = require("@rosetta/model/rbxmx")
local Rbx = require("@rosetta/codecs/model/Rbx")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local H = require("@tests/model/_rbx")
local Fx = require("@tests/gen/rbx_files")
local Json = require("@rosetta/codecs/text/Json")
local Ours = require("@tests/gen/rbx_ours")
local t = T.suite("rbxm")
t:eq(Rbxm.name, "rbxm", "name")
t:eq(Rbxm.category, "model", "category")
t:check(table.find(Rbxm.extensions, "rbxl") ~= nil, "extensions")
t:check(Rbxm.caps.decode and Rbxm.caps.encode, "caps")
local function decode(data: any, opts: any?): (any, any)
return Rbxm.decode(Bytes.reader(if type(data) == "string" then buffer.fromstring(data) else data), opts)
end
local function file(name: string): any
for _, f in Fx.files do
if f.name == name then
return f
end
end
error("no fixture " .. name)
end
local nprops = 0
local trees: { [string]: any } = {}
for _, f in Fx.files do
if not f.xml then
local tree, err = decode(f.data)
t:check(tree ~= nil, f.name .. ": " .. tostring(err))
if tree then
trees[f.name] = tree
t:check(tree.kind == "rbxtree" and tree.meta.source.format == "rbxm", f.name .. ": an RbxTree")
t:eq(tree.place, f.place, f.name .. ": place")
nprops += H.compare(t, tree, Json.decode(f.dump), f.name)
end
end
end
t:note("%d binary files, %d property values match lune", #Fx.files - 3, nprops)
for _, pair in { { "model.rbxm", "model.rbxmx" }, { "place.rbxl", "place.rbxlx" }, { "lune.rbxm", "lune.rbxmx" } } do
local xt = Rbxmx.decode(Bytes.reader(buffer.fromstring(file(pair[2]).data)))
local n = H.sameTree(t, xt, trees[pair[1]], pair[1] .. " vs " .. pair[2], { loose = true })
t:check(n > 5, pair[1] .. ": instances compared")
end
-- the typed property folder (tools/fixtures/rbxmx.py TYPES_FOLDER), value by value
do
local tree = trees["model.rbxm"]
local f = Rbx.find(tree, "Kit/Types") :: any
t:check(f ~= nil, "Kit/Types")
local P = f.properties
local function v(name: string, typ: string): any
local x = P[name]
t:check(x ~= nil and x.type == typ, name .. " is " .. typ .. " (" .. tostring(x and x.type) .. ")")
return x and x.value
end
t:eq(v("PString", "String"), "hello <world> & \"quotes\" \u{e9}\u{65e5}", "String")
t:eq(v("PBinary", "String"), "\0\1\2\3\4\255", "BinaryString bytes")
t:eq(v("PBool", "Bool"), true, "Bool")
t:eq(v("PInt32", "Int32"), -123, "Int32")
t:eq(v("PInt64", "Int64"), -1234567890123, "Int64")
t:eq(v("PFloat32", "Float32"), 1.5, "Float32")
t:eq(v("PFloat64", "Float64"), 3.141592653589793, "Float64")
t:eq(table.concat(v("PUDim2", "UDim2"), ","), "0.5,10,0.25,-3", "UDim2")
t:eq(table.concat(v("PRay", "Ray"), ","), "1,2,3,4,5,6", "Ray")
t:eq(v("PFaces", "Faces"), 1 + 2 + 32, "Faces")
t:eq(v("PAxes", "Axes"), 1 + 4, "Axes")
t:eq(v("PBrickColor", "BrickColor"), 21, "BrickColor")
t:eq(table.concat(v("PVector3int16", "Vector3int16"), ","), "-1,2,300", "Vector3int16")
t:eq(table.concat(v("PCFrame", "CFrame"), ","), "1,2,3,0,0,1,0,1,0,-1,0,0", "CFrame (rotation id)")
t:check(v("POptCFrame", "OptionalCFrame")[1] == 1, "OptionalCFrame")
t:check(P.POptNone and P.POptNone.type == "OptionalCFrame" and P.POptNone.value == nil, "OptionalCFrame none")
t:eq(v("PEnum", "Enum"), 3, "Enum")
t:eq(#v("PNumberSequence", "NumberSequence"), 3, "NumberSequence keypoints")
t:eq(v("PNumberSequence", "NumberSequence")[2][2], -3, "NumberSequence value")
t:eq(v("PColorSequence", "ColorSequence")[2][4], 1, "ColorSequence")
t:eq(table.concat(v("PNumberRange", "NumberRange"), ","), "1,5", "NumberRange")
t:eq(table.concat(v("PRect", "Rect"), ","), "1,2,3,4", "Rect")
t:eq(table.concat(v("PPhys", "PhysicalProperties"), ","), "1,0.5,0.25,2,3,0.75", "PhysicalProperties")
t:check(P.PPhysDefault and P.PPhysDefault.value == nil, "PhysicalProperties default")
local font = v("PFont", "Font")
t:check(font.family == "rbxasset://fonts/families/Arial.json" and font.weight == 700 and font.style == "Italic", "Font")
t:eq(v("PUniqueId", "UniqueId"), "44b188dace632b4702e9c68d004815fc", "UniqueId")
t:eq(v("PUniqueId2", "UniqueId"), "7fb188dace632b4702e9c68d004815fd", "UniqueId (top bits)")
t:eq(v("PSecurity", "SecurityCapabilities"), 12345, "SecurityCapabilities")
t:eq(v("PContent", "Content"), "rbxassetid://1234", "Content uri")
t:check(P.PContentNone and P.PContentNone.value == nil, "Content none")
t:eq(v("PContentId", "String"), "rbxassetid://99", "ContentId")
t:eq(v("PShared", "SharedString"), "\0\1\2\3", "SharedString")
local types2 = Rbx.find(tree, "Kit/Types2") :: any
t:eq(types2.properties.PShared.value, "\0\1\2\3", "SharedString shared by two instances")
local ptr = Rbx.find(tree, "Kit/Pointer") :: any
t:check(ptr.properties.Value.type == "Ref" and ptr.properties.Value.value == Rbx.find(tree, "Kit/Base"), "Ref")
t:check(string.find(Rbx.get(Rbx.find(tree, "Kit/Code") :: any, "Source"), "]]>", 1, true) ~= nil, "Source")
t:eq(Rbx.fullName(Rbx.find(tree, "Kit/Gui/Frame/Label") :: any), "Kit.Gui.Frame.Label", "fullName")
t:eq(#Rbx.find(tree, "Kit/Parts").children, 120, "120 parts")
t:eq(#Rbx.list(tree), 1 + 12 + 2 + 120, "every instance")
end
do
local base = trees["model.rbxm"]
t:eq(table.concat(base.meta.source.compression, ","), "lz4,none", "rojo: lz4 chunks (END raw)")
for _, name in { "model-zstd.rbxm", "model-raw.rbxm" } do
local tree = trees[name]
H.sameTree(t, base, tree, name)
end
t:eq(table.concat(trees["model-zstd.rbxm"].meta.source.compression, ","), "none,zstd", "zstd chunks")
local u = trees["model-unknown.rbxm"]
H.sameTree(t, base, u, "model-unknown.rbxm", { skipRaw = true })
t:check(#u.extra == 1 and u.extra[1].name == "ZZZZ", "the unknown chunk is kept")
local raws = 0
for inst in Rbx.descendants(u) do
local m = (inst :: any).properties.Mystery
if m then
raws += 1
t:check(m.type == "Raw" and m.value.typeId == 0x7f, "unknown property type kept raw")
end
end
t:check(raws > 0, "instances with the raw property: " .. raws)
local again = decode(Rbxm.encode(u) :: buffer)
t:check(again and #again.extra == 1 and buffer.tostring(again.extra[1].data) == buffer.tostring(u.extra[1].data), "unknown chunk round trip")
H.sameTree(t, u, again, "unknown round trip")
-- an edited class (an instance more) drops the raw column instead of writing it wrong
u = decode(file("model-unknown.rbxm").data)
local cls
for inst in Rbx.descendants(u) do
if (inst :: any).properties.Mystery then
cls = (inst :: any).className
break
end
end
Rbx.add(u.roots[1], Rbx.instance(cls, "Added"))
local edited = decode(Rbxm.encode(u) :: buffer)
local still = false
for inst in Rbx.descendants(edited) do
if (inst :: any).properties.Mystery then
still = true
end
end
t:check(edited and not still, "a raw column is dropped when its class changes")
end
local nours = 0
for _, v in Ours do
if string.sub(v.name, -5) == "/rbxm" then
t:check(v.ok, v.name .. ": " .. v.detail)
nours += 1
end
end
t:note("%d of our rbxm files read back by lune", nours)
for name, tree in trees do
local out, err = Rbxm.encode(tree)
t:check(out ~= nil, name .. ": encode " .. tostring(err))
if out then
local back = decode(out)
H.sameTree(t, tree, back, name .. ": round trip")
t:eq(back.place, tree.place, name .. ": place round trip")
end
end
do
local tree = H.everything()
local out = Rbxm.encode(tree) :: buffer
local back = decode(out)
H.sameTree(t, tree, back, "every type", { loose = true })
local raw = Rbxm.encode(tree, { compression = "none" }) :: buffer
local back2 = decode(raw)
H.sameTree(t, tree, back2, "every type, raw chunks", { loose = true })
t:eq(table.concat(back2.meta.source.compression, ","), "none", "compression = none")
t:check(buffer.len(raw) > buffer.len(out), "lz4 chunks are smaller")
local viaXml = Rbxmx.decode(Bytes.reader(Rbxmx.encode(back) :: buffer))
H.sameTree(t, tree, decode(Rbxm.encode(viaXml) :: buffer), "every type via rbxmx", { loose = true })
-- a place: roots marked as services
local place = Rbx.tree(true)
Rbx.add(place, Rbx.instance("Workspace"))
Rbx.add(place, Rbx.instance("Lighting"))
local pb = decode(Rbxm.encode(place) :: buffer)
t:check(pb.place and pb.roots[1].service and pb.roots[2].service, "place services")
local pm = decode(Rbxm.encode(place, { place = false }) :: buffer)
t:check(not pm.place, "place = false")
tree.metadata.ExplicitAutoJoints = "true"
t:eq(decode(Rbxm.encode(tree) :: buffer).metadata.ExplicitAutoJoints, "true", "META")
end
do
local data = file("model.rbxm").data
t:eq(Rbxm.sniff(Bytes.reader(buffer.fromstring(data))), 1, "sniff")
t:eq(Rbxm.sniff(Bytes.reader(buffer.fromstring(file("model.rbxmx").data))), 0, "sniff XML")
local _, e1 = decode("<roblox!\137\255\13\10\26\10")
t:check(e1 and e1.code == Err.TRUNCATED, "header only: " .. tostring(e1))
local _, e2 = decode("<roblox?" .. string.rep("\0", 40))
t:check(e2 and e2.code == Err.CORRUPT, "bad magic: " .. tostring(e2))
local _, e3 = decode(string.sub(data, 1, #data - 25))
t:check(e3 and e3.code == Err.TRUNCATED, "no END: " .. tostring(e3))
local _, e4 = decode(data, { maxInstances = 10 })
t:check(e4 and e4.code == Err.LIMIT, "maxInstances: " .. tostring(e4))
local _, e5 = Rbxm.encode({} :: any)
t:check(e5 and e5.code == Err.ARG, "encode a non-tree")
local bad = Rbx.tree()
local x, y = Rbx.instance("Folder", "x"), Rbx.instance("Folder", "y")
Rbx.set(x, "P", "Int32", 1)
Rbx.set(y, "P", "Vector3", { 1, 2, 3 })
Rbx.add(bad, x)
Rbx.add(bad, y)
local _, e6 = Rbxm.encode(bad)
t:check(e6 and e6.code == Err.ARG, "mixed property types: " .. tostring(e6))
Rbx.set(y, "P", "Nope", 1)
local _, e7 = Rbxm.encode(bad)
t:check(e7 and e7.code == Err.ARG, "unknown type: " .. tostring(e7))
-- a PRNT cycle (A's parent is B, B's parent is A)
local cyc = Rbx.tree()
local a = Rbx.add(cyc, Rbx.instance("Folder", "A"))
Rbx.add(a, Rbx.instance("Folder", "B"))
local raw = Rbxm.encode(cyc, { compression = "none" }) :: buffer
-- the PRNT body: version, count 2, children [0, 1], parents [-1, 0] -> parents [1, 0]
local s = buffer.tostring(raw)
local at = string.find(s, "PRNT", 1, true) :: number
local body = at - 1 + 16
-- parents are the second referent array: zigzag deltas (1, -1) = (2, 1), interleaved big-endian
buffer.writeu8(raw, body + 5 + 8 + 6, 2)
buffer.writeu8(raw, body + 5 + 8 + 7, 1)
local _, e8 = decode(raw)
t:check(e8 and e8.code == Err.CORRUPT, "a cycle: " .. tostring(e8))
end
do
local F = require("@rosetta")
t:eq(F.detect(file("place.rbxl").data), "rbxl", "detect a place")
t:eq(F.detect(file("model.rbxm").data), "rbxm", "detect a model")
t:eq(F.detect(file("place.rbxlx").data), "rbxlx", "detect an XML place")
t:eq(F.detect(file("model.rbxmx").data), "rbxmx", "detect an XML model")
local tree = F.load(file("model.rbxm").data)
t:check(tree and tree.kind == "rbxtree", "F.load")
local again = F.save(tree)
t:check(type(again) == "buffer" and Rbxm.sniff(Bytes.reader(again)) == 1, "F.save keeps the source format")
local x = F.convert(file("model.rbxm").data, "rbxmx")
t:check(type(x) == "buffer" and Rbxmx.sniff(Bytes.reader(x)) == 1, "F.convert to rbxmx")
local l = F.save(tree, nil, { format = "rbxl" })
t:eq(F.detect(l), "rbxl", "save as rbxl marks services")
end
do
local samples = { file("model-raw.rbxm").data, Rbxm.encode(H.everything(), { compression = "none" }) :: buffer, Rbxm.encode(H.everything()) :: buffer }
t:fuzz("decode", function(b: buffer)
return Rbxm.decode(Bytes.reader(b))
end, samples, { mutations = 150, garbage = 300, keepHeader = 32 })
end
t:done()