merge ws/sourcemdl: Source MDL v44-49 (+vvd/vtx/phy/ani, flexes, animations), VMT materials, codecs/model/SourceAnim
This commit is contained in:
commit
c19a68dfae
33 files changed
+14429
-1224
No files matched your search
@@ -0,0 +1,166 @@
|
||||
-- codecs/text/KeyValues tests: text against srctools' Keyvalues.parse, trees written by the python vdf module, binary
|
||||
-- KeyValues by vdf.binary_dumps; write round trips, conditions, #include / #base, the entity lump, errors, fuzz.
|
||||
-- Fixtures: tools/fixtures/keyvalues.py.
|
||||
local T = require("@tests/T")
|
||||
local KeyValues = require("@rosetta/codecs/text/KeyValues")
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Fx = require("@tests/gen/keyvalues")
|
||||
|
||||
local t = T.suite("keyvalues")
|
||||
|
||||
local function canon(b: KeyValues.Block, typed: boolean?): string
|
||||
local out = { "{" }
|
||||
for _, p in b do
|
||||
table.insert(out, #p.key .. ":" .. p.key .. "=")
|
||||
local v = p.value
|
||||
if type(v) == "table" then
|
||||
table.insert(out, canon(v, typed))
|
||||
elseif typed and p.type then
|
||||
local tag = ({ int32 = "i32", float32 = "f32", pointer = "ptr", color = "col", uint64 = "u64", int64 = "i64", wstring = "wstr" } :: any)[p.type]
|
||||
if p.type == "float32" then
|
||||
table.insert(out, tag .. "f" .. string.format("%.17g", v))
|
||||
elseif type(v) == "string" and p.type ~= "wstring" then
|
||||
table.insert(out, tag .. "i" .. v)
|
||||
else
|
||||
table.insert(out, tag .. "i" .. string.format("%d", v))
|
||||
end
|
||||
else
|
||||
table.insert(out, "s" .. #v .. ":" .. v)
|
||||
end
|
||||
end
|
||||
table.insert(out, "}")
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
for i, c in Fx.text do
|
||||
local b, err = KeyValues.parse(c.data)
|
||||
t:check(b ~= nil, "text " .. i .. ": " .. tostring(err))
|
||||
if b then
|
||||
t:eq(canon(b), c.canon, "text " .. i .. ": same pairs as srctools")
|
||||
local s = KeyValues.write(b) :: string
|
||||
local b2 = KeyValues.parse(s)
|
||||
t:eq(b2 and canon(b2), c.canon, "text " .. i .. ": write round trip")
|
||||
end
|
||||
end
|
||||
for i, c in Fx.written do
|
||||
local b, err = KeyValues.parse(c.data)
|
||||
t:check(b ~= nil, "vdf.dumps " .. i .. ": " .. tostring(err))
|
||||
t:eq(b and canon(b), c.canon, "vdf.dumps " .. i .. ": pairs")
|
||||
if b then
|
||||
local b2 = KeyValues.parse(KeyValues.write(b) :: string)
|
||||
t:eq(b2 and canon(b2), c.canon, "vdf.dumps " .. i .. ": round trip")
|
||||
end
|
||||
end
|
||||
for i, c in Fx.binary do
|
||||
local b, err = KeyValues.readBinary(c.data)
|
||||
t:check(b ~= nil, "binary " .. i .. ": " .. tostring(err))
|
||||
if b then
|
||||
t:eq(canon(b, true), c.canon, "binary " .. i .. ": values as vdf.binary_loads")
|
||||
local w = KeyValues.writeBinary(b) :: buffer
|
||||
t:eq(buffer.tostring(w), c.data, "binary " .. i .. ": identical bytes")
|
||||
end
|
||||
t:check(KeyValues.isBinary(buffer.fromstring(c.data), 0, #c.data), "binary " .. i .. ": sniffed")
|
||||
end
|
||||
|
||||
do
|
||||
-- conditions
|
||||
t:check(KeyValues.condition("$WIN32"), "WIN32 by default")
|
||||
t:check(not KeyValues.condition("$X360"), "X360 off")
|
||||
t:check(KeyValues.condition("!$X360"), "negation")
|
||||
t:check(KeyValues.condition("$X360 || $WIN32"), "or")
|
||||
t:check(not KeyValues.condition("$WIN32 && $OSX"), "and")
|
||||
t:check(KeyValues.condition("$OSX", { OSX = true }), "defines")
|
||||
local b = KeyValues.parse('"r" { "a" "1" [$X360] "a" "2" [$X360||$WIN32] }') :: KeyValues.Block
|
||||
t:eq(KeyValues.get(b, "r/a"), "2", "|| in a condition")
|
||||
local keep = KeyValues.parse('"r" { "a" "1" [$X360] "a" "2" [$WIN32] }', { defines = false }) :: KeyValues.Block
|
||||
local r = KeyValues.find(keep, "r") :: KeyValues.Pair
|
||||
t:eq(#r.value, 2, "defines = false keeps both")
|
||||
t:eq(r.value[1].cond, "$X360", "cond kept")
|
||||
t:check(string.find(KeyValues.write(keep) :: string, '"a"\t"1" [$X360]', 1, true) ~= nil, "cond written")
|
||||
|
||||
-- #include / #base
|
||||
local files = {
|
||||
["base.res"] = '"r" { "a" "base" "b" "base" "sub" { "x" "1" "y" "1" } }',
|
||||
["inc.res"] = '"extra" { "k" "v" }',
|
||||
}
|
||||
local main = '#base "base.res"\n#include "inc.res"\n"r" { "a" "main" "sub" { "x" "2" } }'
|
||||
local m = KeyValues.parse(main, { resolve = function(n: string): string?
|
||||
return files[n]
|
||||
end }) :: KeyValues.Block
|
||||
t:eq(KeyValues.get(m, "r/a"), "main", "#base: main wins")
|
||||
t:eq(KeyValues.get(m, "r/b"), "base", "#base: missing key added")
|
||||
t:eq(KeyValues.get(m, "r/sub/x"), "2", "#base: nested merge keeps main")
|
||||
t:eq(KeyValues.get(m, "r/sub/y"), "1", "#base: nested merge adds")
|
||||
t:eq(KeyValues.get(m, "extra/k"), "v", "#include appended")
|
||||
t:eq(m[#m].key, "extra", "#include after the file's keys")
|
||||
local raw = KeyValues.parse(main) :: KeyValues.Block
|
||||
t:eq(raw[1].key, "#base", "no resolve: directive kept as a pair")
|
||||
local _, e1 = KeyValues.parse(main, { resolve = function()
|
||||
return nil
|
||||
end })
|
||||
t:eq(e1 and e1.code, Err.IO, "missing include")
|
||||
local _, e2 = KeyValues.parse('#include "self"', { resolve = function()
|
||||
return '#include "self"'
|
||||
end })
|
||||
t:eq(e2 and e2.code, Err.LIMIT, "include loop")
|
||||
|
||||
-- objects
|
||||
local o = KeyValues.toObject(KeyValues.parse('"r" { "d" "1" "d" "2" "x" "y" }') :: KeyValues.Block)
|
||||
t:eq(#o.r.d, 2, "duplicates -> array")
|
||||
local last = KeyValues.toObject(KeyValues.parse('"r" { "d" "1" "d" "2" }') :: KeyValues.Block, { duplicates = "last" })
|
||||
t:eq(last.r.d, "2", "duplicates last")
|
||||
local back = KeyValues.fromObject(o) :: KeyValues.Block
|
||||
t:eq(canon(back), "{1:r={1:d=s1:11:d=s1:21:x=s1:y}}", "fromObject repeats array keys in order")
|
||||
|
||||
-- escapes off
|
||||
local ne = KeyValues.parse('"p" "C:\\maps\\x"', { escapes = false }) :: KeyValues.Block
|
||||
t:eq(ne[1].value, "C:\\maps\\x", "escapes off")
|
||||
|
||||
-- entity lump
|
||||
local ents = KeyValues.entities('{\n"classname" "worldspawn"\n"mapversion" "1"\n}\n{\n"classname" "logic_relay"\n"OnTrigger" "a,b,,0,-1"\n"OnTrigger" "c,d,,0,-1"\n}\n\0') :: { KeyValues.Block }
|
||||
t:eq(#ents, 2, "entities")
|
||||
t:eq(#KeyValues.all(ents[2], "ontrigger"), 2, "repeated outputs kept")
|
||||
local again = KeyValues.entities(KeyValues.writeEntities(ents) :: string) :: { KeyValues.Block }
|
||||
t:eq(canon(again[2]), canon(ents[2]), "entity round trip")
|
||||
|
||||
-- 64-bit binary values
|
||||
local big = KeyValues.writeBinary({ { key = "id", value = "76561197960287930", type = "uint64" }, { key = "n", value = -5, type = "int64" }, { key = "w", value = "\u{e9}\u{1F600}", type = "wstring" } }) :: buffer
|
||||
local rb = KeyValues.readBinary(big) :: KeyValues.Block
|
||||
t:eq(rb[1].value, "76561197960287930", "uint64 above 2^53 as digits")
|
||||
t:eq(rb[2].value, -5, "negative int64")
|
||||
t:eq(rb[3].value, "\u{e9}\u{1F600}", "wide string")
|
||||
|
||||
-- errors
|
||||
local function code(s: string): string?
|
||||
local _, e = KeyValues.parse(s)
|
||||
return e and e.code
|
||||
end
|
||||
t:eq(code('"r" { "a" "1"'), Err.TRUNCATED, "unclosed block")
|
||||
t:eq(code('"r" "unterminated'), Err.TRUNCATED, "unterminated string")
|
||||
t:eq(code('"r" { } }'), Err.CORRUPT, "extra brace")
|
||||
t:eq(code("{ }"), Err.CORRUPT, "block without a key")
|
||||
t:eq(code(string.rep('"a" {', 300) .. string.rep("}", 300)), Err.LIMIT, "depth limit")
|
||||
local _, be = KeyValues.readBinary(buffer.fromstring("\1key\0no end"))
|
||||
t:eq(be and be.code, Err.TRUNCATED, "binary truncated")
|
||||
local _, we = KeyValues.write({ { key = "k", value = function() end } } :: any)
|
||||
t:eq(we and we.code, Err.ARG, "unwritable value")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx.text do
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
t:fuzz("parse", function(b: buffer)
|
||||
return KeyValues.parse(b)
|
||||
end, samples)
|
||||
local bsamples = {}
|
||||
for _, c in Fx.binary do
|
||||
table.insert(bsamples, c.data)
|
||||
end
|
||||
t:fuzz("readBinary", function(b: buffer)
|
||||
return KeyValues.readBinary(b)
|
||||
end, bsamples)
|
||||
t:fuzz("entities", function(b: buffer)
|
||||
return KeyValues.entities(buffer.tostring(b))
|
||||
end, { '{\n"classname" "worldspawn"\n}\n{ "a" "b" }' })
|
||||
t:done()
|
||||
@@ -0,0 +1,104 @@
|
||||
-- codecs/model/SourceAnim tests: run-length anim values (repeat of value[valid], valid = 0), Quaternion48 / 64,
|
||||
-- Vector48 and a bone record chain, against values built here from the studio.h bit layouts.
|
||||
local T = require("@tests/T")
|
||||
local SourceAnim = require("@rosetta/codecs/model/SourceAnim")
|
||||
local Half = require("@rosetta/codecs/image/Half")
|
||||
|
||||
local t = T.suite("sourceanim")
|
||||
|
||||
local function shorts(list: { number }): buffer
|
||||
local b = buffer.create(#list * 2)
|
||||
for i, v in list do
|
||||
buffer.writei16(b, (i - 1) * 2, v)
|
||||
end
|
||||
return b
|
||||
end
|
||||
|
||||
-- runs: { valid 2, total 4 } 10 20 (then 20 20); { valid 1, total 2 } -5 (then -5)
|
||||
local runs = buffer.create(12)
|
||||
buffer.writeu8(runs, 0, 2)
|
||||
buffer.writeu8(runs, 1, 4)
|
||||
buffer.writei16(runs, 2, 10)
|
||||
buffer.writei16(runs, 4, 20)
|
||||
buffer.writeu8(runs, 6, 1)
|
||||
buffer.writeu8(runs, 7, 2)
|
||||
buffer.writei16(runs, 8, -5)
|
||||
local v = SourceAnim.values(runs, 0, 10, 6)
|
||||
t:eq(table.concat(v :: { number }, ","), "10,20,20,20,-5,-5", "values: runs and repeats")
|
||||
for k = 0, 5 do
|
||||
t:eq(SourceAnim.value(runs, 0, 10, k), (v :: { number })[k + 1], "value " .. k)
|
||||
end
|
||||
t:eq(SourceAnim.values(runs, 0, 10, 7), nil, "values past the data")
|
||||
t:eq(SourceAnim.value(runs, 0, 10, 6), nil, "value past the data")
|
||||
local zero = shorts({ 0x0300 })
|
||||
t:eq(table.concat(SourceAnim.values(zero, 0, 2, 3) :: { number }, ","), "768,768,768", "valid = 0 repeats the run header (as the engine)")
|
||||
|
||||
-- Quaternion48: x = 0.5, y = -0.25, z = 0.125, w < 0
|
||||
local q48 = buffer.create(6)
|
||||
buffer.writeu16(q48, 0, 32768 + 16384)
|
||||
buffer.writeu16(q48, 2, 32768 - 8192)
|
||||
buffer.writeu16(q48, 4, (16384 + 2048) + 32768)
|
||||
local x, y, z, w = SourceAnim.quat48(q48, 0)
|
||||
t:check(x == 0.5 and y == -0.25 and z == 0.125 and math.abs(w + math.sqrt(1 - 0.25 - 0.0625 - 0.015625)) < 1e-12, "quat48")
|
||||
|
||||
-- Quaternion64: 21-bit fields
|
||||
local xs, ys, zs = 1048576 + 524288, 1048576 - 262144, 1048576 + 1
|
||||
local lo = xs + (ys % 2048) * 2097152
|
||||
local hi = ys // 2048 + zs * 1024
|
||||
local q64 = buffer.create(8)
|
||||
buffer.writeu32(q64, 0, lo)
|
||||
buffer.writeu32(q64, 4, hi)
|
||||
x, y, z, w = SourceAnim.quat64(q64, 0)
|
||||
t:check(math.abs(x - 524288 / 1048576.5) < 1e-12 and math.abs(y + 262144 / 1048576.5) < 1e-12 and math.abs(z - 1 / 1048576.5) < 1e-12 and w > 0, "quat64")
|
||||
buffer.writeu32(q64, 4, hi + 0x80000000)
|
||||
local _, _, _, w2 = SourceAnim.quat64(q64, 0)
|
||||
t:check(w2 < 0 and math.abs(w2 + w) < 1e-12, "quat64 w sign")
|
||||
|
||||
local v48 = buffer.create(6)
|
||||
buffer.writeu16(v48, 0, Half.fromFloat(1.5))
|
||||
buffer.writeu16(v48, 2, Half.fromFloat(-2))
|
||||
buffer.writeu16(v48, 4, Half.fromFloat(0.25))
|
||||
local a, b, c = SourceAnim.vec48(v48, 0)
|
||||
t:check(a == 1.5 and b == -2 and c == 0.25, "vec48")
|
||||
|
||||
-- a chain: bone 1 animated rotation x + raw position; bone 0 raw rotation (delta flag off)
|
||||
local bones = {
|
||||
{ pos = { 1, 2, 3 }, quat = { 0, 0, 0, 1 }, rot = { 0, 0, 0 }, posScale = { 1, 1, 1 }, rotScale = { 1, 1, 1 } },
|
||||
{ pos = { 4, 5, 6 }, quat = { 0, 0, 0, 1 }, rot = { 0.1, 0.2, 0.3 }, posScale = { 0.5, 0.5, 0.5 }, rotScale = { 0.01, 0.01, 0.01 } },
|
||||
}
|
||||
local chain = buffer.create(64)
|
||||
-- record 0: bone 1, ANIMROT | RAWPOS, next = 4 + 6 + 6 + 6
|
||||
buffer.writeu8(chain, 0, 1)
|
||||
buffer.writeu8(chain, 1, 0x08 + 0x01)
|
||||
buffer.writei16(chain, 2, 22)
|
||||
buffer.writei16(chain, 4, 12) -- x values at 4 + 12 (after the valueptr and Vector48)
|
||||
buffer.writeu16(chain, 10, Half.fromFloat(7))
|
||||
buffer.writeu16(chain, 12, Half.fromFloat(8))
|
||||
buffer.writeu16(chain, 14, Half.fromFloat(9))
|
||||
buffer.writeu8(chain, 16, 2)
|
||||
buffer.writeu8(chain, 17, 2)
|
||||
buffer.writei16(chain, 18, 10)
|
||||
buffer.writei16(chain, 20, -10)
|
||||
-- record 1 at 22: bone 0, RAWROT, last
|
||||
buffer.writeu8(chain, 22, 0)
|
||||
buffer.writeu8(chain, 23, 0x02)
|
||||
buffer.writei16(chain, 24, 0)
|
||||
buffer.writeu16(chain, 26, 32768)
|
||||
buffer.writeu16(chain, 28, 32768)
|
||||
buffer.writeu16(chain, 30, 16384)
|
||||
local tracks, err = SourceAnim.decode(chain, 0, 32, bones, 2)
|
||||
t:check(tracks, "decode: " .. tostring(err))
|
||||
if tracks then
|
||||
local t1, t0 = tracks[2], tracks[1]
|
||||
t:eq(table.concat(t1.pos, ","), "7,8,9,7,8,9", "raw position")
|
||||
local qx, qy, qz, qw = SourceAnim.angles(0.1 - 0.1, 0.2, 0.3)
|
||||
t:check(math.abs(t1.rot[5] - qx) < 1e-12 and math.abs(t1.rot[6] - qy) < 1e-12 and math.abs(t1.rot[7] - qz) < 1e-12 and math.abs(t1.rot[8] - qw) < 1e-12, "animated rotation, frame 1")
|
||||
t:eq(table.concat(t0.rot, ","), "0,0,0,1,0,0,0,1", "raw rotation")
|
||||
t:eq(table.concat(t0.pos, ","), "1,2,3,1,2,3", "rest position without position data")
|
||||
end
|
||||
t:eq(SourceAnim.decode(chain, 0, 20, bones, 2), nil, "truncated chain")
|
||||
local junk = { buffer.tostring(chain), buffer.tostring(runs) }
|
||||
t:fuzz("decode", function(bb: buffer)
|
||||
return SourceAnim.decode(bb, 0, buffer.len(bb), bones, 4)
|
||||
end, junk)
|
||||
t:done()
|
||||
@@ -0,0 +1,62 @@
|
||||
-- data/vdf tests: KeyValues text written by the python vdf module decodes to what vdf.loads gives, encodes back;
|
||||
-- binary KeyValues by sniffing; pair lists; sniff; fuzz. Fixtures: tools/fixtures/keyvalues.py.
|
||||
local T = require("@tests/T")
|
||||
local Vdf = require("@rosetta/data/vdf")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Canon = require("@tests/data/Canon")
|
||||
local Fx = require("@tests/gen/keyvalues")
|
||||
|
||||
local t = T.suite("data/vdf")
|
||||
|
||||
t:eq(Vdf.name, "vdf", "name")
|
||||
t:check(Vdf.caps.decode and Vdf.caps.encode, "caps")
|
||||
|
||||
local function dec(s: string, opts: any?): (any, any)
|
||||
return Vdf.decode(Bytes.reader(buffer.fromstring(s)), opts)
|
||||
end
|
||||
|
||||
for i, c in Fx.objects do
|
||||
local v, err = dec(c.data)
|
||||
t:check(v ~= nil, "vdf.dumps " .. i .. ": " .. tostring(err))
|
||||
t:eq(Canon.of(v), c.canon, "vdf.dumps " .. i .. ": as vdf.loads")
|
||||
local back = dec(buffer.tostring(Vdf.encode(v) :: buffer))
|
||||
t:eq(Canon.of(back), c.canon, "vdf.dumps " .. i .. ": encode round trip")
|
||||
t:check(Vdf.sniff(Bytes.reader(buffer.fromstring(c.data))) > 0, "vdf.dumps " .. i .. ": sniff")
|
||||
end
|
||||
for i, c in Fx.binary do
|
||||
local v, err = dec(c.data)
|
||||
t:check(type(v) == "table" and v.shortcuts ~= nil, "binary " .. i .. ": " .. tostring(err))
|
||||
local p = dec(c.data, { pairs = true })
|
||||
t:eq(buffer.tostring(Vdf.encode(p, { binary = true }) :: buffer), c.data, "binary " .. i .. ": pairs round trip")
|
||||
end
|
||||
|
||||
do
|
||||
local v = dec('"AppState"\n{\n\t"appid"\t"440"\n\t"InstalledDepots"\n\t{\n\t\t"441" { "manifest" "1" }\n\t}\n\t"x" "1"\n\t"x" "2"\n}\n')
|
||||
t:eq(v.AppState.appid, "440", "acf value")
|
||||
t:eq(v.AppState.InstalledDepots["441"].manifest, "1", "nested")
|
||||
t:eq(#v.AppState.x, 2, "duplicates as array")
|
||||
t:eq(table.concat(Types.keys(v.AppState), ","), "appid,InstalledDepots,x", "key order")
|
||||
local s = buffer.tostring(Vdf.encode(v) :: buffer)
|
||||
t:check(string.find(s, '\t"x"\t"1"\n\t"x"\t"2"\n', 1, true) ~= nil, "array written as repeated keys")
|
||||
local o = Types.object()
|
||||
Types.set(o, "b", true)
|
||||
Types.set(o, "n", 3)
|
||||
t:eq(buffer.tostring(Vdf.encode({ r = o }) :: buffer), '"r"\n{\n\t"b"\t"1"\n\t"n"\t"3"\n}\n', "encode typed values")
|
||||
local _, e = dec('"r" {')
|
||||
t:eq(e and e.code, Err.TRUNCATED, "truncated")
|
||||
local _, ee = Vdf.encode({ r = { function() end } })
|
||||
t:eq(ee and ee.code, Err.ARG, "unwritable")
|
||||
t:eq(Vdf.sniff(Bytes.reader(buffer.fromstring("{}"))), 0, "json is not vdf")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for i = 1, 10 do
|
||||
table.insert(samples, Fx.objects[i].data)
|
||||
table.insert(samples, Fx.binary[i].data)
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Vdf.decode(Bytes.reader(b))
|
||||
end, samples)
|
||||
t:done()
|
||||
@@ -0,0 +1,319 @@
|
||||
-- model/sourcemdl tests: generated MDL v44 / v48 / v49 + VVD / VTX / PHY / ANI against the writer's exact values and
|
||||
-- srctools' reading of the same files. Fixtures: tools/fixtures/sourcemdl.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@rosetta")
|
||||
local SourceMdl = require("@rosetta/model/sourcemdl")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Mat4 = require("@rosetta/codecs/model/Mat4")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Fx = require("@tests/gen/sourcemdl")
|
||||
|
||||
local t = T.suite("sourcemdl")
|
||||
t:eq(SourceMdl.name, "sourcemdl", "name")
|
||||
t:eq(SourceMdl.category, "model", "category")
|
||||
|
||||
local function floats(acc: any): { number }
|
||||
local rd = Types.Scene.reader(acc) :: any
|
||||
local out = {}
|
||||
for i = 0, acc.count - 1 do
|
||||
for c = 0, acc.components - 1 do
|
||||
table.insert(out, rd(i, c))
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
local function near(a: { number }, b: { number }, tol: number): boolean
|
||||
if #a ~= #b then
|
||||
return false
|
||||
end
|
||||
for i = 1, #b do
|
||||
if math.abs(a[i] - b[i]) > tol then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local function decode(c: any, extra: { [string]: any }?): (any, any)
|
||||
local o: { [string]: any } = { files = c.files }
|
||||
for k, v in extra or {} do
|
||||
o[k] = v
|
||||
end
|
||||
return SourceMdl.decode(Bytes.reader(buffer.fromstring(c.mdl)), o :: any)
|
||||
end
|
||||
|
||||
local function normDir(s: string): string
|
||||
local d = string.gsub(s, "\\", "/")
|
||||
d = string.gsub(d, "^/+", "")
|
||||
if d ~= "" and string.sub(d, -1) ~= "/" then
|
||||
d ..= "/"
|
||||
end
|
||||
return d
|
||||
end
|
||||
|
||||
for _, c in Fx.cases do
|
||||
local tag = "v" .. c.version .. ": "
|
||||
local e = c.expect
|
||||
local st = c.srctools
|
||||
local s, err = decode(c)
|
||||
t:check(s, tag .. tostring(err))
|
||||
if not s then
|
||||
continue
|
||||
end
|
||||
local src = s.meta.source
|
||||
t:eq(F.detect(c.mdl), "sourcemdl", tag .. "F.detect")
|
||||
t:eq(#src.warnings, 0, tag .. "no warnings (" .. table.concat(src.warnings, "; ") .. ")")
|
||||
t:eq(src.vtx, "model.dx90.vtx", tag .. "the dx90 strips")
|
||||
|
||||
-- header against srctools
|
||||
t:eq(src.name, st.name, tag .. "name (srctools)")
|
||||
t:eq(src.version, st.version, tag .. "version (srctools)")
|
||||
t:eq(src.surfaceProp, st.surfaceprop, tag .. "surfaceprop (srctools)")
|
||||
t:eq(src.keyValues, st.keyvalues, tag .. "keyvalues (srctools)")
|
||||
t:check(math.abs(src.mass - st.mass) < 1e-6, tag .. "mass (srctools)")
|
||||
t:check(near(src.eye, st.eye, 1e-6) and near(src.hull[1], st.hull[1], 1e-6) and near(src.hull[2], st.hull[2], 1e-6), tag .. "eye / hull (srctools)")
|
||||
for i, d in src.cdmaterials do
|
||||
t:eq(normDir(d), st.cdmaterials[i], tag .. "cdmaterials " .. i .. " (srctools)")
|
||||
end
|
||||
t:eq(#src.includeModels, #st.included, tag .. "include models (srctools)")
|
||||
t:eq(src.includeModels[1].file, st.included[1][2], tag .. "include model file (srctools)")
|
||||
t:eq(#src.skins, #st.skins, tag .. "skin families (srctools)")
|
||||
for f, fam in src.skins do
|
||||
local names = {}
|
||||
for r, mi in fam do
|
||||
names[r] = s.materials[mi].name
|
||||
end
|
||||
t:eq(table.concat(names, ","), table.concat(st.skins[f], ","), tag .. "skin family " .. f .. " (srctools)")
|
||||
end
|
||||
t:eq(#src.sequences, #st.sequences, tag .. "sequences (srctools)")
|
||||
for i, q in st.sequences do
|
||||
local ours = src.sequences[i]
|
||||
t:eq(ours.name, q.label, tag .. "sequence " .. i .. " label (srctools)")
|
||||
t:eq(ours.activity, q.act, tag .. "sequence " .. i .. " activity (srctools)")
|
||||
t:eq(ours.flags, q.flags, tag .. "sequence " .. i .. " flags (srctools)")
|
||||
t:eq(ours.weight, q.weight, tag .. "sequence " .. i .. " weight (srctools)")
|
||||
t:eq(ours.keyValues, q.keyvalues, tag .. "sequence " .. i .. " keyvalues (srctools)")
|
||||
t:check(near(ours.bboxMin, q.bbox[1], 1e-6) and near(ours.bboxMax, q.bbox[2], 1e-6), tag .. "sequence " .. i .. " bbox (srctools)")
|
||||
t:eq(#ours.events, #q.events, tag .. "sequence " .. i .. " events (srctools)")
|
||||
for k, ev in q.events do
|
||||
t:check(math.abs(ours.events[k].cycle - ev[1]) < 1e-6 and ours.events[k].options == ev[2], tag .. "sequence " .. i .. " event " .. k .. " (srctools)")
|
||||
end
|
||||
end
|
||||
local total = 0
|
||||
for _, p in src.bodyParts do
|
||||
for _, m in p.models do
|
||||
total += m.vertices
|
||||
end
|
||||
end
|
||||
t:eq(total, st.total_verts, tag .. "total vertices (srctools)")
|
||||
t:check(string.find(src.physics.text, '"mass" "12.5', 1, true) ~= nil and st.phys_mass == 12.5, tag .. "phy keyvalues (srctools)")
|
||||
|
||||
-- materials: the .vmt srctools finds through cdmaterials, its $basetexture / $bumpmap
|
||||
local seen = 0
|
||||
for _, vmt in st.textures do
|
||||
local mat
|
||||
for _, m in s.materials do
|
||||
if m.extras and m.extras.vmt and m.extras.vmt.path == vmt then
|
||||
mat = m
|
||||
end
|
||||
end
|
||||
t:check(mat, tag .. vmt .. ": resolved as srctools")
|
||||
if mat then
|
||||
seen += 1
|
||||
local want = st.materials[vmt]
|
||||
t:eq(mat.extras.vmt.shader, want.shader, tag .. vmt .. ": shader (srctools)")
|
||||
local base = string.gsub(want.basetexture, "\\", "/")
|
||||
t:eq(s.textures[mat.baseColorTexture.texture].uri, "materials/" .. base .. ".vtf", tag .. vmt .. ": $basetexture (srctools)")
|
||||
if want.bumpmap ~= "" then
|
||||
t:eq(s.textures[mat.normalTexture.texture].uri, "materials/" .. want.bumpmap .. ".vtf", tag .. vmt .. ": $bumpmap (srctools)")
|
||||
else
|
||||
t:check(mat.normalTexture == nil, tag .. vmt .. ": no bumpmap")
|
||||
end
|
||||
t:eq(mat.alphaMode, if want.translucent == "1" then "blend" elseif string.find(vmt, "metal") then "mask" else "opaque", tag .. vmt .. ": alpha mode")
|
||||
t:eq(mat.doubleSided, string.find(vmt, "metal") ~= nil, tag .. vmt .. ": $nocull")
|
||||
end
|
||||
end
|
||||
t:eq(seen, #s.materials, tag .. "every texture has its .vmt")
|
||||
if c.version == 49 then
|
||||
local Vtf = require("@tests/gen/vtf")
|
||||
local loaded, errs = F.model.loadTextures(s, { resolve = function(uri: string): string?
|
||||
return if string.sub(uri, -4) == ".vtf" then Vtf[1].data else nil
|
||||
end })
|
||||
t:check(next(errs) == nil and loaded.textures[1].image and loaded.textures[1].image.width > 0, tag .. "F.model.loadTextures decodes the .vtf references")
|
||||
end
|
||||
|
||||
-- skeleton
|
||||
local skin = s.skins[1]
|
||||
t:eq(#skin.joints, #e.bones, tag .. "joints")
|
||||
local ibm = floats(skin.inverseBindMatrices)
|
||||
for i, b in e.bones do
|
||||
local node = s.nodes[skin.joints[i]]
|
||||
t:eq(node.name, b.name, tag .. "bone " .. i .. " name")
|
||||
t:check(near(node.translation, b.pos, 0) and near(node.rotation, b.quat, 0), tag .. "bone " .. i .. " rest pose")
|
||||
if b.parent >= 0 then
|
||||
t:check(table.find(s.nodes[skin.joints[b.parent + 1]].children, skin.joints[i]) ~= nil, tag .. "bone " .. i .. " parent")
|
||||
end
|
||||
local m = Mat4.fromRows(b.poseToBone)
|
||||
local got = table.move(ibm, (i - 1) * 16 + 1, i * 16, 1, {})
|
||||
t:check(near(got, m, 1e-6), tag .. "bone " .. i .. " poseToBone as the inverse bind matrix")
|
||||
end
|
||||
t:eq(s.nodes[skin.joints[5]].extras.procedural.type, "jiggle", tag .. "jiggle bone")
|
||||
t:eq(s.nodes[skin.joints[6]].extras.procedural.type, "aimatbone", tag .. "aim-at bone")
|
||||
t:check(near(src.lodVertices, e.lodCounts, 0) or (src.lodVertices[1] == e.lodCounts[1] and src.lodVertices[2] == e.lodCounts[2]), tag .. "vvd LOD vertex counts")
|
||||
|
||||
-- meshes
|
||||
for p, part in e.parts do
|
||||
for m, model in part.models do
|
||||
local info = src.bodyParts[p].models[m]
|
||||
t:eq(info.name, model.name, tag .. "model " .. model.name)
|
||||
if #model.meshes == 0 then
|
||||
t:check(info.mesh == nil, tag .. model.name .. ": no mesh")
|
||||
continue
|
||||
end
|
||||
local mesh = s.meshes[info.mesh]
|
||||
t:eq(#mesh.primitives, #model.meshes, tag .. model.name .. ": primitives")
|
||||
for k, x in model.meshes do
|
||||
local prim = mesh.primitives[k]
|
||||
local lab = tag .. model.name .. " mesh " .. k .. ": "
|
||||
t:check(near(floats(prim.attributes.POSITION), x.positions, 0), lab .. "positions")
|
||||
t:check(near(floats(prim.attributes.NORMAL), x.normals, 0), lab .. "normals")
|
||||
t:check(near(floats(prim.attributes.TEXCOORD_0), x.uvs, 0), lab .. "uvs")
|
||||
t:check(near(floats(prim.attributes.TANGENT), x.tangents, 0), lab .. "tangents")
|
||||
t:check(near(floats(prim.attributes.WEIGHTS_0), x.weights, 0), lab .. "weights")
|
||||
t:check(near(floats(prim.attributes.JOINTS_0), x.joints, 0), lab .. "joints")
|
||||
t:check(near(floats(prim.indices), x.lods[1], 0), lab .. "LOD 0 triangles (strips, groups, winding)")
|
||||
t:check(near(floats(prim.extras.lods[2].indices), x.lods[2], 0), lab .. "LOD 1 triangles")
|
||||
t:eq(prim.extras.lods[2].switchPoint, 40, lab .. "LOD 1 switch point")
|
||||
t:eq(prim.material, x.material, lab .. "material through skin family 1")
|
||||
t:eq(prim.extras.skinRef, x.skinRef, lab .. "skin reference")
|
||||
local names = mesh.extras.targetNames
|
||||
local want = 0
|
||||
for d, verts in x.targets do
|
||||
local slot = table.find(names, e.flexdescs[tonumber(d) :: number + 1])
|
||||
t:check(slot ~= nil, lab .. "flex target " .. d)
|
||||
if slot then
|
||||
local pos, nrm = floats(prim.targets[slot].POSITION), floats(prim.targets[slot].NORMAL)
|
||||
local ok = true
|
||||
for v, dd in verts do
|
||||
local vi = tonumber(v) :: number
|
||||
for cc = 1, 3 do
|
||||
ok = ok and math.abs(pos[vi * 3 + cc] - dd[cc]) < 1e-6 and math.abs(nrm[vi * 3 + cc] - dd[cc + 3]) < 1e-6
|
||||
end
|
||||
want += 1
|
||||
end
|
||||
t:check(ok, lab .. "flex " .. names[slot] .. " deltas")
|
||||
end
|
||||
end
|
||||
if names then
|
||||
t:eq(#prim.targets, #names, lab .. "a target per flex")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(s.nodes[s.roots[1]].name, src.name, tag .. "root node")
|
||||
|
||||
-- attachments
|
||||
for i, a in e.attachments do
|
||||
local at = src.attachments[i]
|
||||
local node = s.nodes[at.node]
|
||||
t:eq(node.name, a.name, tag .. "attachment " .. i .. " name")
|
||||
t:check(table.find(s.nodes[skin.joints[a.bone + 1]].children, at.node) ~= nil, tag .. "attachment " .. i .. " parent bone")
|
||||
local m = Mat4.compose(node.translation, node.rotation, node.scale)
|
||||
t:check(near(m, Mat4.fromRows(a.matrix), 1e-5), tag .. "attachment " .. i .. " matrix")
|
||||
end
|
||||
t:eq(#src.hitboxSets[1].boxes, 2, tag .. "hitboxes")
|
||||
t:eq(src.hitboxSets[1].boxes[2].name, "head_box", tag .. "hitbox name")
|
||||
|
||||
-- animations
|
||||
for _, a in e.anims do
|
||||
local anim
|
||||
for _, x in s.animations do
|
||||
if x.name == a.name then
|
||||
anim = x
|
||||
end
|
||||
end
|
||||
t:check(anim, tag .. "animation " .. a.name)
|
||||
if not anim then
|
||||
continue
|
||||
end
|
||||
for bone, want in a.bones do
|
||||
local node = skin.joints[tonumber(bone) :: number]
|
||||
local tr, rot
|
||||
for _, ch in anim.channels do
|
||||
if ch.node == node then
|
||||
if ch.path == "translation" then
|
||||
tr = ch
|
||||
elseif ch.path == "rotation" then
|
||||
rot = ch
|
||||
end
|
||||
end
|
||||
end
|
||||
t:check(tr and rot, tag .. a.name .. " bone " .. bone .. ": channels")
|
||||
if tr and rot then
|
||||
t:eq(tr.input.count, a.frames, tag .. a.name .. ": frames")
|
||||
t:check(math.abs(floats(tr.input)[2] - 1 / a.fps) < 1e-6, tag .. a.name .. ": fps")
|
||||
t:check(near(floats(tr.output), want.pos, 1e-4), tag .. a.name .. " bone " .. bone .. ": positions per frame")
|
||||
t:check(near(floats(rot.output), want.rot, 1e-5), tag .. a.name .. " bone " .. bone .. ": rotations per frame")
|
||||
end
|
||||
end
|
||||
end
|
||||
if c.version == 49 then
|
||||
t:check(c.files["model.ani"] ~= nil, tag .. "an .ani block")
|
||||
end
|
||||
|
||||
-- physics
|
||||
local soup = {}
|
||||
for _, sol in src.physics.solids do
|
||||
for _, h in sol.hulls do
|
||||
for _, v in h.indices do
|
||||
for cc = 1, 3 do
|
||||
table.insert(soup, h.positions[v * 3 + cc])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
t:check(near(soup, e.physics, 1e-3), tag .. "phy convex hulls")
|
||||
|
||||
-- options
|
||||
local s2 = decode(c, { bodies = { 2, 2 }, lod = 2 }) :: any
|
||||
local names = {}
|
||||
for _, ni in s2.nodes[s2.roots[1]].children do
|
||||
if s2.nodes[ni].mesh then
|
||||
table.insert(names, s2.meshes[s2.nodes[ni].mesh].name)
|
||||
end
|
||||
end
|
||||
t:eq(table.concat(names, ","), "body_lo,hat", tag .. "opts.bodies")
|
||||
t:check(near(floats(s2.meshes[1].primitives[1].indices), e.parts[1].models[1].meshes[1].lods[2], 0), tag .. "opts.lod")
|
||||
local s3 = SourceMdl.decode(Bytes.reader(buffer.fromstring(c.mdl))) :: any
|
||||
t:check(s3 and #s3.meshes == 0 and #s3.meta.source.warnings > 0 and #s3.skins == 1, tag .. "no siblings: skeleton only, with warnings")
|
||||
local s4 = decode(c, { name = "renamed.mdl", files = { ["renamed.vvd"] = c.files["model.vvd"], ["renamed.sw.vtx"] = c.files["model.dx90.vtx"] } }) :: any
|
||||
t:check(s4 and #s4.meshes == 3 and s4.meta.source.vtx == "renamed.sw.vtx", tag .. "siblings named after opts.name")
|
||||
|
||||
-- fuzz: the mdl, and each sibling file
|
||||
local samples = { c.mdl }
|
||||
t:fuzz(tag .. "mdl", function(b: buffer)
|
||||
return SourceMdl.decode(Bytes.reader(b), { files = c.files })
|
||||
end, samples, { mutations = 40 })
|
||||
for _, name in { "model.vvd", "model.dx90.vtx", "model.phy", "materials/models/test/skin_c.vmt" } do
|
||||
t:fuzz(tag .. name, function(b: buffer)
|
||||
local files = table.clone(c.files)
|
||||
files[name] = buffer.tostring(b)
|
||||
return SourceMdl.decode(Bytes.reader(buffer.fromstring(c.mdl)), { files = files })
|
||||
end, { c.files[name] }, { mutations = 30, garbage = 20 })
|
||||
end
|
||||
end
|
||||
|
||||
-- errors
|
||||
local bad = buffer.fromstring(Fx.cases[1].mdl)
|
||||
buffer.writei32(bad, 4, 52)
|
||||
local _, e1 = SourceMdl.decode(Bytes.reader(bad))
|
||||
t:check(e1 and e1.code == Err.UNSUPPORTED, "version 52: UNSUPPORTED")
|
||||
local _, e2 = SourceMdl.decode(Bytes.reader(buffer.fromstring(string.sub(Fx.cases[1].mdl, 1, 100))))
|
||||
t:check(e2 and (e2.code == Err.TRUNCATED or e2.code == Err.CORRUPT), "truncated header")
|
||||
local s5 = SourceMdl.decode(Bytes.reader(buffer.fromstring(Fx.cases[1].mdl)), { files = { ["model.vvd"] = "IDSV" } })
|
||||
t:check(s5 == nil, "a broken .vvd is an error")
|
||||
t:eq(SourceMdl.sniff(Bytes.reader(buffer.fromstring(Fx.cases[1].mdl))), 1, "sniff")
|
||||
t:eq(SourceMdl.sniff(Bytes.reader(buffer.fromstring("IDST\10\0\0\0"))), 0, "sniff: GoldSrc is model/mdl")
|
||||
t:done()
|
||||
Reference in new issue
Block a user