merge ws/roblox (codecs/compress/Lz4 for Anvil LZ4 chunks); registry regenerated
This commit is contained in:
commit
09354cbd12
23 files changed
+5959
-1
No files matched your search
@@ -0,0 +1,286 @@
|
||||
-- tests/model/_rbx: helpers shared by the model/rbxm and model/rbxmx tests: instance paths (the scheme of the lune dump
|
||||
-- in tools/fixtures/rbxmx.py), our property values in lune's canonical form, a dump comparison, a deep tree comparison,
|
||||
-- and a tree with a value of every type built from scratch.
|
||||
local Rbx = require("@formats/codecs/model/Rbx")
|
||||
|
||||
local H = {}
|
||||
|
||||
local f32buf = buffer.create(4)
|
||||
local function f32(v: number): number
|
||||
buffer.writef32(f32buf, 0, v)
|
||||
return buffer.readf32(f32buf, 0)
|
||||
end
|
||||
|
||||
local function hex(s: string): string
|
||||
return (string.gsub(s, ".", function(c: string): string
|
||||
return string.format("%02x", string.byte(c))
|
||||
end))
|
||||
end
|
||||
|
||||
-- paths(tree) -> { [path] = instance }, { [instance] = path }: names joined with "/", "#k" for the k-th sibling of a name
|
||||
function H.paths(tree: any): ({ [string]: any }, { [any]: string })
|
||||
local byPath, ofInst = {}, {}
|
||||
local function walk(list: { any }, prefix: string)
|
||||
local seen: { [string]: number } = {}
|
||||
for _, inst in list do
|
||||
local n = inst.name
|
||||
seen[n] = (seen[n] or 0) + 1
|
||||
local p = prefix .. (if prefix == "" then "" else "/") .. n .. (if seen[n] > 1 then "#" .. seen[n] else "")
|
||||
byPath[p] = inst
|
||||
ofInst[inst] = p
|
||||
walk(inst.children, p)
|
||||
end
|
||||
end
|
||||
walk(tree.roots, "")
|
||||
return byPath, ofInst
|
||||
end
|
||||
|
||||
local function numbers(t: string, v: any): { number }?
|
||||
if type(v) == "number" then
|
||||
return { v }
|
||||
elseif type(v) ~= "table" then
|
||||
return nil
|
||||
end
|
||||
if t == "NumberSequence" or t == "ColorSequence" then
|
||||
local out = {}
|
||||
for _, k in v do
|
||||
local n = if t == "ColorSequence" then 4 else 3 -- lune has no ColorSequence envelope
|
||||
for c = 1, n do
|
||||
table.insert(out, k[c])
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
return v
|
||||
end
|
||||
|
||||
-- same(ours: Value, want: lune canon, pathOf, propName) -> ok, detail
|
||||
function H.same(v: any, want: any, pathOf: { [any]: string }, pname: string): (boolean, string)
|
||||
local t = if v then v.type else "nil"
|
||||
local x = if v then v.value else nil
|
||||
local wt = want.t
|
||||
if wt == "nil" then
|
||||
return x == nil, "want nil, got " .. t
|
||||
elseif wt == "string" then
|
||||
return type(x) == "string" and hex(x) == want.s, "string differs (" .. t .. ")"
|
||||
elseif wt == "bool" then
|
||||
return x == want.b, "bool differs"
|
||||
elseif wt == "Instance" then
|
||||
return type(x) == "table" and pathOf[x] == want.s, "ref differs: " .. tostring(x and pathOf[x]) .. " vs " .. tostring(want.s)
|
||||
elseif wt == "Content" then
|
||||
if want.kind == "None" then
|
||||
return x == nil, "content not none"
|
||||
elseif want.kind == "Uri" then
|
||||
return x == want.s, "content uri " .. tostring(x)
|
||||
end
|
||||
return type(x) == "table" and pathOf[x] == want.s, "content object"
|
||||
elseif wt == "UniqueId" then
|
||||
return x == want.s, "uniqueid " .. tostring(x) .. " vs " .. tostring(want.s)
|
||||
elseif wt == "Font" then
|
||||
return type(x) == "table" and x.family == want.s and x.weight == want.n[1] and x.style == want.style, "font differs"
|
||||
end
|
||||
local got = numbers(t, x)
|
||||
if t == "Color3uint8" and got then
|
||||
got = { f32(got[1] / 255), f32(got[2] / 255), f32(got[3] / 255) }
|
||||
end
|
||||
local n = want.n
|
||||
if not got or not n then
|
||||
return false, "no numbers: " .. t .. " vs " .. tostring(wt)
|
||||
end
|
||||
local count = #n
|
||||
if t == "PhysicalProperties" then
|
||||
count = math.min(#got, #n)
|
||||
elseif #got ~= #n then
|
||||
return false, string.format("%d numbers, want %d (%s / %s)", #got, #n, t, wt)
|
||||
end
|
||||
for i = 1, count do
|
||||
if got[i] ~= n[i] then
|
||||
return false, string.format("%s[%d] = %.9g, want %.9g", t, i, got[i], n[i])
|
||||
end
|
||||
end
|
||||
return true, ""
|
||||
end
|
||||
|
||||
-- compare(t, tree, dump, label) -> properties compared: every instance / property of a lune dump against our tree
|
||||
function H.compare(t: any, tree: any, dump: any, label: string): number
|
||||
local byPath, pathOf = H.paths(tree)
|
||||
local n = 0
|
||||
for path, e in dump do
|
||||
local inst = byPath[path]
|
||||
t:check(inst ~= nil, label .. ": instance " .. path)
|
||||
if inst then
|
||||
t:check(inst.className == e.class, label .. ": " .. path .. " class " .. inst.className .. ", want " .. e.class)
|
||||
for pname, want in e.props do
|
||||
local v = if pname == "Name" then { type = "String", value = inst.name } else inst.properties[pname]
|
||||
local ok, detail = H.same(v, want, pathOf, pname)
|
||||
t:check(ok, label .. ": " .. path .. "." .. pname .. ": " .. detail)
|
||||
n += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
return n
|
||||
end
|
||||
|
||||
-- the type family a value compares under (a binary file has one string type; XML writes BrickColor as an int)
|
||||
local FAMILY: { [string]: string } = {
|
||||
String = "string",
|
||||
BinaryString = "string",
|
||||
ProtectedString = "string",
|
||||
ContentId = "string",
|
||||
Bytecode = "string",
|
||||
BrickColor = "int",
|
||||
Int32 = "int",
|
||||
}
|
||||
local function family(t: string): string
|
||||
return FAMILY[t] or t
|
||||
end
|
||||
|
||||
-- equal(a, b, pathA, pathB) -> ok, where: values compared deeply (Refs / Content objects by path)
|
||||
local function equal(a: any, b: any, pa: { [any]: string }, pb: { [any]: string }): boolean
|
||||
if type(a) ~= type(b) then
|
||||
return false
|
||||
end
|
||||
if type(a) == "buffer" then
|
||||
return buffer.tostring(a) == buffer.tostring(b)
|
||||
elseif type(a) ~= "table" then
|
||||
return a == b or (a ~= a and b ~= b)
|
||||
end
|
||||
if a.className and b.className then
|
||||
return pa[a] == pb[b]
|
||||
end
|
||||
for k, v in a do
|
||||
if not equal(v, b[k], pa, pb) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
for k in b do
|
||||
if a[k] == nil then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
-- sameTree(t, a, b, label, opts?) -> instances compared: class, name, children and every property (opts.loose:
|
||||
-- string / int families compare as one type, properties only b has are allowed when another instance of the class has them in a; opts.skipRaw: Raw values are not compared)
|
||||
function H.sameTree(t: any, a: any, b: any, label: string, opts: any?): number
|
||||
local o = opts or {}
|
||||
local pa = select(2, H.paths(a))
|
||||
local pb = select(2, H.paths(b))
|
||||
local n = 0
|
||||
local bad = 0
|
||||
local classProps: { [string]: { [string]: boolean } } = {}
|
||||
for inst in Rbx.descendants(a) do
|
||||
local i = inst :: any
|
||||
classProps[i.className] = classProps[i.className] or {}
|
||||
for k in i.properties do
|
||||
classProps[i.className][k] = true
|
||||
end
|
||||
end
|
||||
local function walk(la: { any }, lb: { any }, where: string)
|
||||
if #la ~= #lb then
|
||||
t:check(false, string.format("%s: %s has %d children, want %d", label, where, #lb, #la))
|
||||
return
|
||||
end
|
||||
for i, x in la do
|
||||
local y = lb[i]
|
||||
n += 1
|
||||
local w = where .. "/" .. x.name
|
||||
if x.className ~= y.className or x.name ~= y.name then
|
||||
t:check(false, label .. ": " .. w .. ": " .. y.className .. " " .. y.name)
|
||||
else
|
||||
for k, v in x.properties do
|
||||
local u = y.properties[k]
|
||||
local skip = o.skipRaw and v.type == "Raw"
|
||||
if not skip then
|
||||
local tt = if o.loose then family(v.type) == family(u and u.type or "") else u and u.type == v.type
|
||||
if not tt or not equal(v.value, u.value, pa, pb) then
|
||||
bad += 1
|
||||
if bad < 10 then
|
||||
t:check(false, label .. ": " .. w .. "." .. k .. " (" .. v.type .. " / " .. tostring(u and u.type) .. ")")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
for k, u in y.properties do
|
||||
-- a binary file stores a value for every instance of a class: the writer fills in the ones the XML
|
||||
-- twin leaves out (when another instance of the class has the property)
|
||||
local filled = o.loose and classProps[y.className] and classProps[y.className][k]
|
||||
if x.properties[k] == nil and not (o.skipRaw and u.type == "Raw") and not filled then
|
||||
bad += 1
|
||||
if bad < 10 then
|
||||
t:check(false, label .. ": " .. w .. "." .. k .. " is extra")
|
||||
end
|
||||
end
|
||||
end
|
||||
walk(x.children, y.children, w)
|
||||
end
|
||||
end
|
||||
end
|
||||
walk(a.roots, b.roots, "")
|
||||
t:check(bad == 0, label .. ": " .. bad .. " property differences")
|
||||
return n
|
||||
end
|
||||
|
||||
-- everything() -> a model with one value of every type (and several instances per class)
|
||||
function H.everything(): any
|
||||
local tree = Rbx.tree()
|
||||
local root = Rbx.add(tree, Rbx.instance("Model", "Everything"))
|
||||
local a = Rbx.add(root, Rbx.instance("Folder", "A"))
|
||||
local b = Rbx.add(root, Rbx.instance("Folder", "B"))
|
||||
local c = Rbx.add(a, Rbx.instance("Part", "C"))
|
||||
local S = Rbx.set
|
||||
S(a, "Str", "String", "plain")
|
||||
S(a, "Bin", "BinaryString", "\0\1\2\255")
|
||||
S(a, "Prot", "ProtectedString", "print('x')")
|
||||
S(a, "Cid", "ContentId", "rbxassetid://1")
|
||||
S(a, "Flag", "Bool", true)
|
||||
S(a, "I32", "Int32", -2147483648)
|
||||
S(a, "I64", "Int64", 2 ^ 52 + 1)
|
||||
S(a, "F32", "Float32", f32(0.1))
|
||||
S(a, "F64", "Float64", 0.1)
|
||||
S(a, "En", "Enum", 7)
|
||||
S(a, "Brick", "BrickColor", 1004)
|
||||
S(a, "Sec", "SecurityCapabilities", 2 ^ 40)
|
||||
S(a, "Fc", "Faces", 63)
|
||||
S(a, "Ax", "Axes", 7)
|
||||
S(a, "Ud", "UDim", { 0.5, -7 })
|
||||
S(a, "Ud2", "UDim2", { 0.25, 1, -0.5, 1000 })
|
||||
S(a, "Nr", "NumberRange", { -1, 1 })
|
||||
S(a, "V2", "Vector2", { 1.5, -2.5 })
|
||||
S(a, "V2i", "Vector2int16", { -32768, 32767 })
|
||||
S(a, "V3", "Vector3", { 1, 2, 3 })
|
||||
S(a, "V3i", "Vector3int16", { -5, 0, 5 })
|
||||
S(a, "C3", "Color3", { 0.5, 0.25, 1 })
|
||||
S(a, "C3u", "Color3uint8", { 0, 128, 255 })
|
||||
S(a, "Rc", "Rect", { 1, 2, 3, 4 })
|
||||
S(a, "Ry", "Ray", { 1, 2, 3, 0, 0, -1 })
|
||||
S(a, "Cf", "CFrame", { 1, 2, 3, 0, -1, 0, 1, 0, 0, 0, 0, 1 })
|
||||
S(a, "Cf2", "CFrame", { 1, 2, 3, f32(0.36), f32(0.48), f32(-0.8), f32(-0.8), f32(0.6), 0, f32(0.48), f32(0.64), f32(0.6) })
|
||||
S(a, "Ocf", "OptionalCFrame", { 4, 5, 6, 1, 0, 0, 0, 1, 0, 0, 0, 1 })
|
||||
S(b, "Ocf", "OptionalCFrame", nil)
|
||||
S(a, "Ns", "NumberSequence", { { 0, 1, 0 }, { 0.5, 2, 0.25 }, { 1, 3, 0 } })
|
||||
S(a, "Cs", "ColorSequence", { { 0, 1, 0, 0, 0 }, { 1, 0, 0, 1, 0 } })
|
||||
S(a, "Phys", "PhysicalProperties", { 1, 0.5, 0.25, 2, 3, 1 })
|
||||
S(b, "Phys", "PhysicalProperties", { 1, 0.5, 0.25, 2, 3, 0.75 })
|
||||
S(c, "Phys", "PhysicalProperties", nil)
|
||||
S(a, "Ref", "Ref", c)
|
||||
S(b, "Ref", "Ref", nil)
|
||||
S(a, "Sh", "SharedString", "shared\0bytes")
|
||||
S(b, "Sh", "SharedString", "shared\0bytes")
|
||||
S(a, "Uid", "UniqueId", "7fb188dace632b4702e9c68d004815fd")
|
||||
S(b, "Uid", "UniqueId", "00000000000000010000000200000003")
|
||||
S(a, "Font", "Font", { family = "rbxasset://fonts/families/Arial.json", weight = 700, style = "Italic", cachedFaceId = "rbxasset://fonts/arialbd.ttf" })
|
||||
S(a, "Ct", "Content", "rbxassetid://5")
|
||||
S(b, "Ct", "Content", c)
|
||||
S(c, "Ct", "Content", nil)
|
||||
-- the same properties on B (columns of two values), so every type is also written for a second instance
|
||||
for k, v in a.properties do
|
||||
if b.properties[k] == nil then
|
||||
b.properties[k] = v
|
||||
end
|
||||
end
|
||||
return tree
|
||||
end
|
||||
|
||||
return H
|
||||
@@ -0,0 +1,268 @@
|
||||
-- model/rbxm tests: binary models and places built by rojo 7.7 and lune 0.11 (rbx-dom) decode to the values lune reads
|
||||
-- from them (every property the files hold); the binary decode equals the decode of the XML twin; Zstandard and raw
|
||||
-- chunk variants; an unknown chunk and an unknown property type round trip raw; our rbxm output read back by lune
|
||||
-- equals the original (fixture verdicts); round trips of a tree with every type; options, errors and the fuzz pass.
|
||||
-- Fixtures: tools/fixtures/rbxmx.py.
|
||||
local T = require("@tests/T")
|
||||
local Rbxm = require("@formats/model/rbxm")
|
||||
local Rbxmx = require("@formats/model/rbxmx")
|
||||
local Rbx = require("@formats/codecs/model/Rbx")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local H = require("@tests/model/_rbx")
|
||||
local Fx = require("@tests/gen/rbx_files")
|
||||
local Json = require("@formats/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
|
||||
|
||||
-- every binary reference file against lune's reading of it, and against the decode of its XML twin
|
||||
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
|
||||
|
||||
-- chunk variants: Zstandard, raw, an unknown chunk and an unknown property type
|
||||
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
|
||||
|
||||
-- our encoder: lune reads our rbxm back to the same values (tools/fixtures/rbxmx.py)
|
||||
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)
|
||||
|
||||
-- round trips: every reference file, and a tree with every type
|
||||
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")
|
||||
-- through XML and back
|
||||
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
|
||||
|
||||
-- sniff, errors
|
||||
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
|
||||
|
||||
-- through the api: detection (variant: rbxl for places), load, save, convert
|
||||
do
|
||||
local F = require("@formats")
|
||||
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
|
||||
|
||||
-- fuzz
|
||||
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()
|
||||
@@ -0,0 +1,188 @@
|
||||
-- model/rbxmx tests: XML models and places built by rojo 7.7 and lune 0.11 (rbx-dom) decode to the values lune reads
|
||||
-- from them; our rbxmx output read back by lune equals the original (fixture verdicts); round trips of every reference
|
||||
-- file and of a tree with every type; XML specifics (BinaryString, CDATA, packed Color3, INF / NAN, unknown property
|
||||
-- tags kept raw, referents, SharedStrings, floats written exactly); errors and the fuzz pass.
|
||||
-- Fixtures: tools/fixtures/rbxmx.py.
|
||||
local T = require("@tests/T")
|
||||
local Rbxmx = require("@formats/model/rbxmx")
|
||||
local Rbx = require("@formats/codecs/model/Rbx")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local H = require("@tests/model/_rbx")
|
||||
local Fx = require("@tests/gen/rbx_files")
|
||||
local Json = require("@formats/codecs/text/Json")
|
||||
local Ours = require("@tests/gen/rbx_ours")
|
||||
|
||||
local t = T.suite("rbxmx")
|
||||
|
||||
t:eq(Rbxmx.name, "rbxmx", "name")
|
||||
t:eq(Rbxmx.category, "model", "category")
|
||||
t:check(table.find(Rbxmx.extensions, "rbxlx") ~= nil, "extensions")
|
||||
t:check(Rbxmx.caps.decode and Rbxmx.caps.encode, "caps")
|
||||
|
||||
local function decode(data: any, opts: any?): (any, any)
|
||||
return Rbxmx.decode(Bytes.reader(if type(data) == "string" then buffer.fromstring(data) else data), opts)
|
||||
end
|
||||
local function encode(tree: any, opts: any?): string
|
||||
return buffer.tostring(Rbxmx.encode(tree, opts) :: buffer)
|
||||
end
|
||||
|
||||
local nprops = 0
|
||||
local trees: { [string]: any } = {}
|
||||
for _, f in Fx.files do
|
||||
if 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 == "rbxmx", 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("3 XML files, %d property values match lune", nprops)
|
||||
|
||||
do
|
||||
local tree = trees["model.rbxmx"]
|
||||
local P = (Rbx.find(tree, "Kit/Types") :: any).properties
|
||||
t:eq(P.PBinary.type, "BinaryString", "BinaryString (CDATA)")
|
||||
t:eq(P.PBinary.value, "\0\1\2\3\4\255", "BinaryString bytes")
|
||||
t:eq(P.PContentId.type, "ContentId", "ContentId")
|
||||
t:eq(P.PFloat32.value, 1.5, "float")
|
||||
t:eq(P.PUniqueId.value, "44b188dace632b4702e9c68d004815fc", "UniqueId")
|
||||
t:eq(P.PShared.value, "\0\1\2\3", "SharedString")
|
||||
t:eq(P.PBrickColor.type, "Int32", "a BrickColor of an unknown property reads as its <int>")
|
||||
local base = (Rbx.find(tree, "Kit/Base") :: any).properties
|
||||
t:eq(base.Tags.value, "Enemy\0Boss", "Tags")
|
||||
t:check(#base.AttributesSerialize.value > 10, "AttributesSerialize")
|
||||
local lune = trees["lune.rbxmx"]
|
||||
t:eq((Rbx.find(lune, "Kit/Brick") :: any).properties.Value.type, "BrickColor", "BrickColorValue.Value is a BrickColor")
|
||||
end
|
||||
|
||||
local nours = 0
|
||||
for _, v in Ours do
|
||||
if string.sub(v.name, -6) == "/rbxmx" then
|
||||
t:check(v.ok, v.name .. ": " .. v.detail)
|
||||
nours += 1
|
||||
end
|
||||
end
|
||||
t:note("%d of our rbxmx files read back by lune", nours)
|
||||
|
||||
for name, tree in trees do
|
||||
local back = decode(encode(tree))
|
||||
H.sameTree(t, tree, back, name .. ": round trip")
|
||||
t:eq(back.place, tree.place, name .. ": place round trip")
|
||||
end
|
||||
do
|
||||
local tree = H.everything()
|
||||
local s = encode(tree)
|
||||
H.sameTree(t, tree, decode(s), "every type", { loose = true })
|
||||
t:check(string.find(s, '<roblox version="4">', 1, true) == 1, "rbx-dom's root element")
|
||||
t:check(string.find(s, "<SharedStrings>", 1, true) ~= nil, "SharedStrings")
|
||||
local again = encode(decode(s))
|
||||
t:eq(again, s, "a second write is byte-identical")
|
||||
local flat = encode(tree, { indent = 0 })
|
||||
t:check(not string.find(flat, "\n <Item", 1, true) and decode(flat) ~= nil, "indent = 0")
|
||||
end
|
||||
|
||||
-- XML specifics
|
||||
do
|
||||
local doc = [==[<?xml version="1.0" encoding="utf-8"?>
|
||||
<roblox xmlns:xmime="http://www.w3.org/2005/05/xmlmime" version="4">
|
||||
<Meta name="ExplicitAutoJoints">true</Meta>
|
||||
<External>null</External>
|
||||
<Item class="Part" referent="RBXA">
|
||||
<Properties>
|
||||
<string name="Name"><![CDATA[Odd <name>]]></string>
|
||||
<Color3 name="Color">4294934528</Color3>
|
||||
<float name="Inf">INF</float>
|
||||
<double name="NegInf">-INF</double>
|
||||
<float name="Nan">NAN</float>
|
||||
<Ref name="Self">RBXA</Ref>
|
||||
<Ref name="None">null</Ref>
|
||||
<BinaryString name="Data">
|
||||
AAEC
|
||||
AwQ=
|
||||
</BinaryString>
|
||||
<Content name="Old"><url>rbxasset://textures/a.png</url></Content>
|
||||
<Content name="OldNull"><null></null></Content>
|
||||
<FutureType name="Mystery"><A>1</A><B x="2"/></FutureType>
|
||||
<PhysicalProperties name="Phys"><CustomPhysics>true</CustomPhysics><Density>2</Density><Friction>0.3</Friction><Elasticity>0.5</Elasticity><FrictionWeight>1</FrictionWeight><ElasticityWeight>1</ElasticityWeight></PhysicalProperties>
|
||||
</Properties>
|
||||
</Item>
|
||||
</roblox>]==]
|
||||
local tree, err = decode(doc)
|
||||
t:check(tree ~= nil, "a hand-written file: " .. tostring(err))
|
||||
local p = tree.roots[1]
|
||||
t:eq(p.name, "Odd <name>", "CDATA name")
|
||||
local c = p.properties.Color.value
|
||||
t:check(c[1] == 1 and math.abs(c[2] - 0x80 / 255) < 1e-6 and c[3] == 0, "packed ARGB Color3")
|
||||
t:eq(p.properties.Inf.value, math.huge, "INF")
|
||||
t:eq(p.properties.NegInf.value, -math.huge, "-INF")
|
||||
t:check(p.properties.Nan.value ~= p.properties.Nan.value, "NAN")
|
||||
t:check(p.properties.Self.value == p, "Ref to itself")
|
||||
t:check(p.properties.None.type == "Ref" and p.properties.None.value == nil, "Ref null")
|
||||
t:eq(p.properties.Data.value, "\0\1\2\3\4", "BinaryString with whitespace")
|
||||
t:eq(p.properties.Old.type, "ContentId", "legacy <Content><url>")
|
||||
t:eq(p.properties.Old.value, "rbxasset://textures/a.png", "url")
|
||||
t:eq(p.properties.Mystery.type, "XmlRaw", "unknown tag kept")
|
||||
t:eq(p.properties.Phys.value[6], 1, "acoustic absorption defaults to 1")
|
||||
t:eq(tree.metadata.ExplicitAutoJoints, "true", "Meta")
|
||||
t:eq(#tree.extra, 1, "<External> kept")
|
||||
local out = encode(tree)
|
||||
t:check(string.find(out, '<FutureType name="Mystery"><A>1</A><B x="2"></B></FutureType>', 1, true) ~= nil
|
||||
or string.find(out, "<FutureType", 1, true) ~= nil, "unknown tag written back")
|
||||
local back = decode(out)
|
||||
t:eq(back.roots[1].properties.Mystery.type, "XmlRaw", "unknown tag round trip")
|
||||
t:eq(back.roots[1].properties.Inf.value, math.huge, "INF round trip")
|
||||
t:check(back.roots[1].properties.Self.value == back.roots[1], "Ref round trip")
|
||||
t:check(string.find(out, '<Meta name="ExplicitAutoJoints">true</Meta>', 1, true) ~= nil, "Meta written")
|
||||
-- a String with bytes XML cannot hold is written as a BinaryString
|
||||
local raw = Rbx.tree()
|
||||
local f = Rbx.add(raw, Rbx.instance("Folder"))
|
||||
Rbx.set(f, "Blob", "String", "a\0b\255")
|
||||
local rs = encode(raw)
|
||||
t:check(string.find(rs, '<BinaryString name="Blob">', 1, true) ~= nil, "binary bytes as BinaryString")
|
||||
t:eq(decode(rs).roots[1].properties.Blob.value, "a\0b\255", "binary bytes round trip")
|
||||
-- floats: the shortest text that reads back exactly
|
||||
local fl = Rbx.tree()
|
||||
local g = Rbx.add(fl, Rbx.instance("Folder"))
|
||||
Rbx.set(g, "A", "Float64", 0.1)
|
||||
Rbx.set(g, "B", "Float32", 0.1)
|
||||
Rbx.set(g, "C", "Vector3", { 1 / 3, 1e-30, -2 ^ 100 })
|
||||
local fs = encode(fl)
|
||||
t:check(string.find(fs, '<double name="A">0.1</double>', 1, true) ~= nil, "double 0.1")
|
||||
t:check(string.find(fs, '<float name="B">0.1</float>', 1, true) ~= nil, "float 0.1")
|
||||
local fb = decode(fs).roots[1].properties
|
||||
t:eq(fb.A.value, 0.1, "double exact")
|
||||
local v = buffer.create(4)
|
||||
buffer.writef32(v, 0, 1 / 3)
|
||||
t:eq(fb.C.value[1], buffer.readf32(v, 0), "f32 exact")
|
||||
end
|
||||
|
||||
-- sniff, errors
|
||||
do
|
||||
local data = Fx.files[2].data
|
||||
t:eq(Rbxmx.sniff(Bytes.reader(buffer.fromstring(data))), 1, "sniff")
|
||||
t:eq(Rbxmx.sniff(Bytes.reader(buffer.fromstring("<?xml version='1.0'?>\n<roblox version=\"4\">"))), 1, "sniff with a declaration")
|
||||
t:eq(Rbxmx.sniff(Bytes.reader(buffer.fromstring(Fx.files[1].data))), 0, "sniff binary")
|
||||
t:eq(Rbxmx.sniff(Bytes.reader(buffer.fromstring("<svg/>"))), 0, "sniff other XML")
|
||||
local _, e1 = decode("<svg></svg>")
|
||||
t:check(e1 and e1.code == Err.CORRUPT, "not <roblox>: " .. tostring(e1))
|
||||
local _, e2 = decode("<roblox version=\"4\"><Item class=\"Part\">")
|
||||
t:check(e2 and e2.code == Err.TRUNCATED, "truncated: " .. tostring(e2))
|
||||
local _, e3 = decode("<roblox><Item><Properties/></Item></roblox>")
|
||||
t:check(e3 and e3.code == Err.CORRUPT, "an Item without a class: " .. tostring(e3))
|
||||
local _, e4 = Rbxmx.encode({ kind = "scene" } :: any)
|
||||
t:check(e4 and e4.code == Err.ARG, "encode a non-tree")
|
||||
end
|
||||
|
||||
do
|
||||
local samples = { Fx.files[2].data, encode(H.everything()) }
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Rbxmx.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 80, garbage = 300 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,202 @@
|
||||
-- model/robloxmesh tests: meshes of every version written by the fixture's spec writer (1.00 / 1.01 ASCII, 2.00 with
|
||||
-- 36- and 40-byte vertices, 3.00 / 3.01 LODs, 4.00 / 4.01 / 5.00 skinned with one and several subsets, 5.00 FACS,
|
||||
-- 6.00 / 7.00 chunked) decode to the values they were written from (attribute CRCs, LOD index CRCs, global joints and
|
||||
-- weights, bone names / parents / world transforms, FACS, raw chunks); a Draco COREMESH is UNSUPPORTED; our 2.00 /
|
||||
-- 4.00 output read back by the fixture's reader (verdicts); round trips; errors and the fuzz pass.
|
||||
-- Fixtures: tools/fixtures/robloxmesh.py.
|
||||
local T = require("@tests/T")
|
||||
local RobloxMesh = require("@formats/model/robloxmesh")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Mat4 = require("@formats/codecs/model/Mat4")
|
||||
local Fx = require("@tests/gen/robloxmesh")
|
||||
|
||||
local t = T.suite("robloxmesh")
|
||||
|
||||
t:eq(RobloxMesh.name, "robloxmesh", "name")
|
||||
t:eq(RobloxMesh.category, "model", "category")
|
||||
t:check(table.find(RobloxMesh.extensions, "mesh") ~= nil, "extensions")
|
||||
t:check(RobloxMesh.caps.decode and RobloxMesh.caps.encode, "caps")
|
||||
|
||||
local function decode(data: any, opts: any?): (any, any)
|
||||
return RobloxMesh.decode(Bytes.reader(if type(data) == "string" then buffer.fromstring(data) else data), opts)
|
||||
end
|
||||
|
||||
-- a world matrix of node i (parents through the children lists)
|
||||
local function world(scene: any, i: number): { number }
|
||||
local parent = {}
|
||||
for k, n in scene.nodes do
|
||||
for _, c in n.children do
|
||||
parent[c] = k
|
||||
end
|
||||
end
|
||||
local m = Types.Scene.nodeMatrix(scene.nodes[i])
|
||||
local p = parent[i]
|
||||
while p do
|
||||
m = Mat4.mul(Types.Scene.nodeMatrix(scene.nodes[p]), m)
|
||||
p = parent[p]
|
||||
end
|
||||
return m
|
||||
end
|
||||
|
||||
local ncases = 0
|
||||
for _, c in Fx.cases do
|
||||
local scene, err = decode(c.data)
|
||||
if c.unsupported then
|
||||
t:check(scene == nil and err and err.code == Err.UNSUPPORTED, c.name .. ": unsupported (" .. tostring(err) .. ")")
|
||||
continue
|
||||
end
|
||||
t:check(scene ~= nil, c.name .. ": " .. tostring(err))
|
||||
if not scene then
|
||||
continue
|
||||
end
|
||||
ncases += 1
|
||||
local prim = scene.meshes[1].primitives[1]
|
||||
local a = prim.attributes
|
||||
t:eq(scene.meta.source.version, c.version, c.name .. ": version")
|
||||
t:eq(a.POSITION.count, c.vertices, c.name .. ": vertices")
|
||||
t:eq(Crc32.of(a.POSITION.data), c.pos, c.name .. ": POSITION")
|
||||
t:eq(Crc32.of(a.NORMAL.data), c.nrm, c.name .. ": NORMAL")
|
||||
t:eq(Crc32.of(a.TEXCOORD_0.data), c.uv, c.name .. ": TEXCOORD_0")
|
||||
if c.tan then
|
||||
t:check(a.TANGENT and Crc32.of(a.TANGENT.data) == c.tan, c.name .. ": TANGENT")
|
||||
end
|
||||
if c.col then
|
||||
t:check(a.COLOR_0 and a.COLOR_0.type == "unorm8" and Crc32.of(a.COLOR_0.data) == c.col, c.name .. ": COLOR_0")
|
||||
else
|
||||
t:check(a.COLOR_0 == nil, c.name .. ": no COLOR_0")
|
||||
end
|
||||
local lods = { prim }
|
||||
for _, l in (scene.meshes[1].extras or {}).lods or {} do
|
||||
table.insert(lods, l)
|
||||
end
|
||||
t:eq(#lods, #c.lods, c.name .. ": LODs")
|
||||
for k, want in c.lods do
|
||||
t:check(lods[k] and Crc32.of(lods[k].indices.data) == want, c.name .. ": LOD " .. (k - 1) .. " indices")
|
||||
end
|
||||
if c.joints then
|
||||
t:check(a.JOINTS_0 and Crc32.of(a.JOINTS_0.data) == c.joints, c.name .. ": JOINTS_0")
|
||||
t:check(a.WEIGHTS_0 and Crc32.of(a.WEIGHTS_0.data) == c.weights, c.name .. ": WEIGHTS_0")
|
||||
local skin = scene.skins[1]
|
||||
t:eq(#skin.joints, #c.bones, c.name .. ": joints")
|
||||
t:eq(skin.inverseBindMatrices.count, #c.bones, c.name .. ": inverse bind matrices")
|
||||
t:eq(scene.nodes[1].skin, 1, c.name .. ": the mesh node is skinned")
|
||||
local worst = 0
|
||||
for i, b in c.bones do
|
||||
local j = skin.joints[i]
|
||||
local node = scene.nodes[j]
|
||||
t:eq(node.name, b.name, c.name .. ": bone name")
|
||||
t:eq(node.extras.culling, b.culling, c.name .. ": culling")
|
||||
local isRoot = table.find(scene.roots, j) ~= nil
|
||||
t:eq(isRoot, b.parent == nil, c.name .. ": " .. b.name .. " root")
|
||||
if b.parent then
|
||||
t:check(table.find(scene.nodes[skin.joints[b.parent + 1]].children, j) ~= nil, c.name .. ": " .. b.name .. " parent")
|
||||
end
|
||||
local m = world(scene, j)
|
||||
local cf = b.cf
|
||||
local want = { cf[1], cf[4], cf[7], 0, cf[2], cf[5], cf[8], 0, cf[3], cf[6], cf[9], 0, cf[10], cf[11], cf[12], 1 }
|
||||
for k = 1, 16 do
|
||||
worst = math.max(worst, math.abs(m[k] - want[k]))
|
||||
end
|
||||
end
|
||||
t:check(worst < 1e-4, c.name .. ": bone world transforms (" .. worst .. ")")
|
||||
end
|
||||
if c.facs then
|
||||
t:check(scene.meta.source.facs and Crc32.of(scene.meta.source.facs) == c.facs, c.name .. ": FACS kept raw")
|
||||
end
|
||||
if c.chunks then
|
||||
t:eq(scene.meta.source.chunks[1].name, c.chunks[1], c.name .. ": raw chunk")
|
||||
end
|
||||
t:eq(scene.meta.up, "y", c.name .. ": up")
|
||||
end
|
||||
t:note("%d meshes match", ncases)
|
||||
|
||||
for _, v in Fx.ours do
|
||||
t:check(v.ok, v.name .. ": " .. v.detail)
|
||||
end
|
||||
t:note("%d of our files read back by the fixture's reader", #Fx.ours)
|
||||
|
||||
-- round trips through our encoder
|
||||
do
|
||||
for _, name in { "v2.00-40", "v4.00-lods", "v4.00-40bones", "v6.00" } do
|
||||
local c
|
||||
for _, x in Fx.cases do
|
||||
if x.name == name then
|
||||
c = x
|
||||
end
|
||||
end
|
||||
local s = decode(c.data)
|
||||
local out = RobloxMesh.encode(s, { version = "4.00" }) :: buffer
|
||||
local back = decode(out)
|
||||
t:check(back ~= nil, name .. ": re-read")
|
||||
local p0, p1 = s.meshes[1].primitives[1], back.meshes[1].primitives[1]
|
||||
t:eq(p1.indices.count, p0.indices.count, name .. ": triangles")
|
||||
if not c.joints then
|
||||
t:eq(Crc32.of(p1.attributes.POSITION.data), Crc32.of(p0.attributes.POSITION.data), name .. ": positions")
|
||||
t:eq(Crc32.of(p1.indices.data), Crc32.of(p0.indices.data), name .. ": indices")
|
||||
t:eq(Crc32.of(p1.attributes.TANGENT.data), Crc32.of(p0.attributes.TANGENT.data), name .. ": tangents")
|
||||
else
|
||||
t:eq(#back.skins[1].joints, #s.skins[1].joints, name .. ": joints")
|
||||
end
|
||||
local v2 = decode(RobloxMesh.encode(s) :: buffer)
|
||||
t:eq(v2.meta.source.version, "2.00", name .. ": 2.00 by default")
|
||||
end
|
||||
-- a glTF-style scene: a quad under a transformed node, no normals / colours
|
||||
local s = Types.Scene.new()
|
||||
local pos = buffer.create(48)
|
||||
for i, v in { 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0 } do
|
||||
buffer.writef32(pos, (i - 1) * 4, v)
|
||||
end
|
||||
local idx = buffer.create(24)
|
||||
for i, v in { 0, 1, 2, 0, 2, 3 } do
|
||||
buffer.writeu32(idx, (i - 1) * 4, v)
|
||||
end
|
||||
s.meshes[1] = { primitives = { { attributes = { POSITION = Types.Scene.accessor(pos, "f32", 3, 4) }, indices = Types.Scene.accessor(idx, "u32", 1, 6), mode = "triangles" } } }
|
||||
s.nodes[1] = { children = {}, mesh = 1, translation = { 10, 0, 0 } }
|
||||
s.roots[1] = 1
|
||||
local q = decode(RobloxMesh.encode(s) :: buffer)
|
||||
local qa = q.meshes[1].primitives[1].attributes
|
||||
t:eq(buffer.readf32(qa.POSITION.data, 12), 11, "node transform applied")
|
||||
t:eq(buffer.readf32(qa.NORMAL.data, 8), 1, "computed normal +Z")
|
||||
t:check(qa.COLOR_0 == nil, "white / no colours: 36-byte vertices")
|
||||
end
|
||||
|
||||
-- sniff, errors
|
||||
do
|
||||
local c = Fx.cases[3]
|
||||
t:eq(RobloxMesh.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, "sniff")
|
||||
t:eq(RobloxMesh.sniff(Bytes.reader(buffer.fromstring("version 1.0"))), 0, "sniff short")
|
||||
t:eq(RobloxMesh.sniff(Bytes.reader(buffer.fromstring("solid cube\n"))), 0, "sniff STL")
|
||||
local _, e1 = decode("version 9.00\n")
|
||||
t:check(e1 and e1.code == Err.UNSUPPORTED, "version 9: " .. tostring(e1))
|
||||
local _, e2 = decode(string.sub(c.data, 1, 60))
|
||||
t:check(e2 and e2.code == Err.TRUNCATED, "truncated: " .. tostring(e2))
|
||||
local _, e3 = decode(c.data, { maxVertices = 10 })
|
||||
t:check(e3 and e3.code == Err.LIMIT, "maxVertices: " .. tostring(e3))
|
||||
local bad = buffer.fromstring(c.data)
|
||||
buffer.writeu32(bad, buffer.len(bad) - 4, 99999)
|
||||
local _, e4 = decode(bad)
|
||||
t:check(e4 and e4.code == Err.CORRUPT, "face index out of range: " .. tostring(e4))
|
||||
local _, e5 = decode("hello there, a mesh")
|
||||
t:check(e5 and e5.code == Err.CORRUPT, "no version line: " .. tostring(e5))
|
||||
local _, e6 = RobloxMesh.encode(Types.Scene.new())
|
||||
t:check(e6 and e6.code == Err.ARG, "an empty scene: " .. tostring(e6))
|
||||
local _, e7 = RobloxMesh.encode(decode(c.data), { version = "3.00" } :: any)
|
||||
t:check(e7 and e7.code == Err.ARG, "version 3.00 is not written")
|
||||
end
|
||||
|
||||
do
|
||||
local samples = {}
|
||||
for _, c in Fx.cases do
|
||||
if #c.data < 40000 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return RobloxMesh.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 30, garbage = 300, keepHeader = 13 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
Reference in new issue
Block a user