merge ws/game
This commit is contained in:
commit
3cebe7d4e7
72 files changed
+10473
-6
No files matched your search
@@ -0,0 +1,64 @@
|
||||
-- archive/grp tests: Build GRP archives written by tools/fixtures/grp.py list the entries an independent Python
|
||||
-- reader sees (names, sizes, data CRCs); our writer produces the same bytes as the Python writer and round-trips;
|
||||
-- conversion from a zip; errors; the fuzz pass. Fixtures: tools/fixtures/grp.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Grp = require("@formats/archive/grp")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/grp")
|
||||
|
||||
local t = T.suite("grp")
|
||||
t:eq(Grp.name, "grp", "name")
|
||||
|
||||
for _, c in Fx do
|
||||
local a, err = Grp.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(a, c.name .. ": " .. tostring(err))
|
||||
local list = a:list()
|
||||
t:eq(#list, #c.entries, c.name .. ": entry count")
|
||||
for i, w in c.entries do
|
||||
t:eq(list[i].name, w.name, c.name .. ": name " .. i)
|
||||
t:eq(list[i].size, w.size, c.name .. ": size " .. w.name)
|
||||
t:eq(Crc32.of((a:read(w.name))), w.crc, c.name .. ": data " .. w.name)
|
||||
end
|
||||
t:eq(F.detect(c.data), "grp", c.name .. ": F.detect")
|
||||
local n = Grp.new()
|
||||
for _, f in c.files do
|
||||
n:add(f.name, f.data)
|
||||
end
|
||||
t:check(buffer.tostring(n:write() :: buffer) == c.data, c.name .. ": bytes identical to the Python writer")
|
||||
t:check(buffer.tostring(F.save(a) :: buffer) == c.data, c.name .. ": decode + F.save is lossless")
|
||||
end
|
||||
|
||||
do
|
||||
local zip = F.format("zip").create()
|
||||
zip:add("readme.txt", "hi")
|
||||
local g = F.load(F.save(zip, nil, { format = "grp", upper = true }) :: buffer)
|
||||
t:check(g.format == "grp" and g:list()[1].name == "README.TXT", "zip -> grp, upper-cased")
|
||||
local c = Fx[1]
|
||||
local _, e1 = Grp.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, #c.data - 1))))
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1))
|
||||
local _, e2 = Grp.decode(Bytes.reader(buffer.fromstring("KenSilverman\255\255\255\255")))
|
||||
t:check(e2 and e2.code == Err.LIMIT, "count limit: " .. tostring(e2))
|
||||
local long = Grp.new()
|
||||
long:add("THIRTEEN.CHAR", "x")
|
||||
local _, e3 = long:write()
|
||||
t:check(e3 and e3.code == Err.ARG, "a 13-byte name does not fit")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx do
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
local a = Grp.decode(Bytes.reader(b))
|
||||
if a then
|
||||
for _, e in a:list() do
|
||||
a:read(e.name)
|
||||
end
|
||||
end
|
||||
return a
|
||||
end, samples, { mutations = 40 })
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,127 @@
|
||||
-- archive/mpq tests: archives written by StormLib (tools/fixtures/mpq.py; header versions 1-4) list every file and
|
||||
-- read back its exact bytes: zlib, PKWARE implode, bzip2, LZMA and sparse sectors, imploded files, single-unit,
|
||||
-- stored, encrypted (plain and fix-key) and sector-CRC files; a prefixed archive; an archive without a listfile
|
||||
-- (names by block index, opts.names); Huffman sectors are UNSUPPORTED; the hash helper; errors; the fuzz pass.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Mpq = require("@formats/archive/mpq")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/mpq")
|
||||
|
||||
local t = T.suite("mpq")
|
||||
t:eq(Mpq.name, "mpq", "name")
|
||||
-- the classic test vector: the hash table key
|
||||
t:eq(Mpq.hash("(hash table)", 3), 0xC3AF3770, "hash of (hash table)")
|
||||
t:eq(Mpq.hash("(block table)", 3), 0xEC83B3A3, "hash of (block table)")
|
||||
|
||||
for _, c in Fx.archives do
|
||||
local a, err = Mpq.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(a, c.name .. ": " .. tostring(err))
|
||||
t:eq(F.detect(c.data), "mpq", c.name .. ": F.detect")
|
||||
local by = {}
|
||||
for _, e in a:list() do
|
||||
by[e.name] = e
|
||||
end
|
||||
local ok = 0
|
||||
for _, w in c.files do
|
||||
if c.unnamed then
|
||||
continue
|
||||
end
|
||||
local e = by[w.name]
|
||||
t:check(e, c.name .. ": has " .. w.name)
|
||||
if w.size > 0 then
|
||||
local b, rerr = a:read(w.name)
|
||||
if w.huffman then
|
||||
t:check(rerr and rerr.code == Err.UNSUPPORTED, c.name .. ": " .. w.name .. ": Huffman is unsupported: " .. tostring(rerr))
|
||||
else
|
||||
t:check(b, c.name .. ": read " .. w.name .. ": " .. tostring(rerr))
|
||||
t:eq(e.size, w.size, c.name .. ": size " .. w.name)
|
||||
t:eq(Crc32.of(b), w.crc, c.name .. ": data " .. w.name)
|
||||
ok += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
if c.unnamed then
|
||||
-- no listfile: unnamed entries; unencrypted ones read by block index, names through opts.names
|
||||
local n = 0
|
||||
for _, e in a:list() do
|
||||
if string.match(e.name, "^File%d+%.xxx$") then
|
||||
n += 1
|
||||
local b, rerr = a:read(e.name)
|
||||
if (e :: any).encrypted then
|
||||
t:check(rerr and rerr.code == Err.UNSUPPORTED, c.name .. ": encrypted without a name")
|
||||
else
|
||||
t:check(b, c.name .. ": read " .. e.name .. ": " .. tostring(rerr))
|
||||
end
|
||||
end
|
||||
end
|
||||
t:check(n > 0, c.name .. ": entries named by block index")
|
||||
local names = {}
|
||||
for _, w in c.files do
|
||||
table.insert(names, w.name)
|
||||
end
|
||||
local b = Mpq.decode(Bytes.reader(buffer.fromstring(c.data)), { names = names }) :: any
|
||||
for _, w in c.files do
|
||||
if w.size > 0 then
|
||||
t:eq(Crc32.of((b:read(w.name))), w.crc, c.name .. ": opts.names " .. w.name)
|
||||
end
|
||||
end
|
||||
end
|
||||
t:note("%-9s v%d, %2d entries, %2d files checked", c.name, a.meta.source.version, #a:list(), ok)
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local c = Fx.archives[1]
|
||||
local _, e1 = Mpq.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, 200))))
|
||||
t:check(e1 and (e1.code == Err.TRUNCATED or e1.code == Err.CORRUPT), "truncated: " .. tostring(e1))
|
||||
local _, e2 = Mpq.decode(Bytes.reader(buffer.fromstring(string.rep("x", 64))))
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "no header")
|
||||
local a = Mpq.decode(Bytes.reader(buffer.fromstring(c.data))) :: any
|
||||
local _, e3 = a:write()
|
||||
t:check(e3 and e3.code == Err.UNSUPPORTED, "read-only")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx.archives do
|
||||
if c.name == "v1" or c.name == "nolist" or c.name == "v4" then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
local a = Mpq.decode(Bytes.reader(b))
|
||||
if a then
|
||||
for _, e in a:list() do
|
||||
a:read(e.name)
|
||||
end
|
||||
end
|
||||
return a
|
||||
end, samples, { mutations = 60 })
|
||||
do
|
||||
-- mutations inside the file data (the tables intact)
|
||||
local c = Fx.archives[1]
|
||||
local rand = T.rng(5)
|
||||
local threw = 0
|
||||
for _ = 1, 80 do
|
||||
local b = buffer.fromstring(c.data)
|
||||
for _ = 1, 1 + rand(8) do
|
||||
buffer.writeu8(b, 32 + rand(#c.data // 2), rand(256))
|
||||
end
|
||||
local ok = pcall(function()
|
||||
local a = Mpq.decode(Bytes.reader(b))
|
||||
if a then
|
||||
for _, e in a:list() do
|
||||
a:read(e.name)
|
||||
end
|
||||
end
|
||||
end)
|
||||
if not ok then
|
||||
threw += 1
|
||||
end
|
||||
end
|
||||
t:eq(threw, 0, "mutated file data never throws")
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,132 @@
|
||||
-- archive/vpk tests: version 1 and 2 directory files written by Python's vpk package list the entries it reads back
|
||||
-- (names, sizes, CRC-32s); a multi-part archive (part files through opts.files and opts.resolve, preload bytes, an
|
||||
-- embedded entry); our writer's version 1 / 2 output read back by the vpk package (MD5 verify for version 2) and by
|
||||
-- us; CRC checks; errors; the fuzz pass. Fixtures: tools/fixtures/vpk.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Vpk = require("@formats/archive/vpk")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/vpk")
|
||||
local Src = require("@tests/gen/vpk_src")
|
||||
|
||||
local t = T.suite("vpk")
|
||||
t:eq(Vpk.name, "vpk", "name")
|
||||
|
||||
local function check(a: any, want: { any }, label: string)
|
||||
local by = {}
|
||||
for _, e in a:list() do
|
||||
by[e.name] = e
|
||||
end
|
||||
t:eq(#a:list(), #want, label .. ": entry count")
|
||||
for _, w in want do
|
||||
local e = by[w.name]
|
||||
t:check(e, label .. ": has " .. w.name)
|
||||
if e then
|
||||
t:eq(e.size, w.size, label .. ": size " .. w.name)
|
||||
local b, err = a:read(w.name)
|
||||
t:check(b, label .. ": read " .. w.name .. ": " .. tostring(err))
|
||||
t:eq(Crc32.of(b), w.crc, label .. ": data " .. w.name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- 1. Python's vpk package
|
||||
for _, c in Fx.cases do
|
||||
local a, err = Vpk.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(a, c.name .. ": " .. tostring(err))
|
||||
check(a, c.entries, c.name)
|
||||
t:eq(a.meta.source.version, c.version, c.name .. ": version")
|
||||
t:eq(F.detect(c.data), "vpk", c.name .. ": F.detect")
|
||||
if c.version == 2 then
|
||||
t:check(a.meta.source.v2.fileMd5 and #a.meta.source.v2.fileMd5 == 16, c.name .. ": MD5 section")
|
||||
end
|
||||
end
|
||||
|
||||
-- 2. multi-part
|
||||
do
|
||||
local m = Fx.multi
|
||||
local a = Vpk.decode(Bytes.reader(buffer.fromstring(m.dir)), { files = m.parts, name = "maps/pak01_dir.vpk" })
|
||||
check(a, m.entries, "multi (opts.files)")
|
||||
t:eq(#a.meta.source.parts, 2, "two part files")
|
||||
local asked = {}
|
||||
local b = Vpk.decode(Bytes.reader(buffer.fromstring(m.dir)), {
|
||||
resolve = function(name: string): string?
|
||||
table.insert(asked, name)
|
||||
return m.parts[name]
|
||||
end,
|
||||
})
|
||||
check(b, m.entries, "multi (opts.resolve, default pak01 name)")
|
||||
t:check(table.find(asked, "pak01_001.vpk") ~= nil, "resolve gets the part file name")
|
||||
local c = Vpk.decode(Bytes.reader(buffer.fromstring(m.dir)))
|
||||
local _, e = c:read("materials/a.vmt")
|
||||
t:check(e and e.code == Err.UNSUPPORTED, "missing part: " .. tostring(e))
|
||||
t:eq(buffer.tostring((c:read("root.txt"))), "embedded", "embedded entry without the parts")
|
||||
-- multi-part -> single file
|
||||
local one = F.load(F.save(b) :: buffer)
|
||||
check(one, m.entries, "multi -> one directory file")
|
||||
end
|
||||
|
||||
-- 3. our writer, read back by the vpk package and by us
|
||||
for k, v in Fx.ours do
|
||||
t:check(v.ok, k .. ": the vpk package verifies our MD5s")
|
||||
t:eq(#v.entries, #Fx.want, k .. ": the vpk package lists every entry")
|
||||
for i, w in Fx.want do
|
||||
local g = v.entries[i]
|
||||
t:check(g and g.name == w.name and g.size == w.size and g.crc == w.crc, k .. ": " .. w.name)
|
||||
end
|
||||
local a = Vpk.new(v.version)
|
||||
for _, f in Src do
|
||||
a:add(f.name, f.data)
|
||||
end
|
||||
check(Vpk.decode(Bytes.reader(a:write() :: buffer)), Fx.want, k .. ": round trip")
|
||||
end
|
||||
|
||||
-- 4. errors
|
||||
do
|
||||
local c = Fx.cases[2]
|
||||
local bad = buffer.fromstring(c.data)
|
||||
-- flip a byte in the last entry's data (before the 48-byte MD5 section)
|
||||
local at = #c.data - 49
|
||||
buffer.writeu8(bad, at, bit32.bxor(buffer.readu8(bad, at), 0xFF))
|
||||
local a = Vpk.decode(Bytes.reader(bad))
|
||||
local failed = 0
|
||||
for _, e in a:list() do
|
||||
local _, err = a:read(e.name)
|
||||
if err and err.code == Err.CHECKSUM then
|
||||
failed += 1
|
||||
end
|
||||
end
|
||||
t:eq(failed, 1, "a damaged entry fails its CRC")
|
||||
local nv = Vpk.decode(Bytes.reader(bad), { verify = false })
|
||||
local ok = true
|
||||
for _, e in nv:list() do
|
||||
ok = ok and nv:read(e.name) ~= nil
|
||||
end
|
||||
t:check(ok, "verify = false")
|
||||
local _, e1 = Vpk.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, 40))))
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "truncated tree: " .. tostring(e1))
|
||||
local v3 = buffer.fromstring(c.data)
|
||||
buffer.writeu32(v3, 4, 3)
|
||||
local _, e2 = Vpk.decode(Bytes.reader(v3))
|
||||
t:check(e2 and e2.code == Err.UNSUPPORTED, "version 3")
|
||||
local _, e3 = Vpk.new(3):write()
|
||||
t:check(e3 and e3.code == Err.ARG, "write version 3")
|
||||
end
|
||||
|
||||
local samples = { Fx.multi.dir }
|
||||
for _, c in Fx.cases do
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
local a = Vpk.decode(Bytes.reader(b), { files = Fx.multi.parts })
|
||||
if a then
|
||||
for _, e in a:list() do
|
||||
a:read(e.name)
|
||||
end
|
||||
end
|
||||
return a
|
||||
end, samples, { mutations = 60 })
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,247 @@
|
||||
-- archive/wad tests: a cut-down Freedoom IWAD (tools/fixtures/wad.py) lists its lumps (names, sizes, data CRCs,
|
||||
-- namespaces, map lumps as "MAP01/THINGS"); every flat, patch, sprite and graphic decodes to the pixels DeuTex
|
||||
-- extracts (RGBA CRC, offsets); composed wall textures; MAP01 parses to the counts of an independent parse and its
|
||||
-- Scene's floor area matches Green's theorem over the sectors' sides; a hand-made room with a pillar (exact areas,
|
||||
-- walls, things on floors), a UDMF stub, duplicate names, a tall patch; the writer round-trips byte for byte;
|
||||
-- errors; the fuzz pass. Fixtures: tools/fixtures/wad.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Wad = require("@formats/archive/wad")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Fx = require("@tests/gen/wad")
|
||||
|
||||
local t = T.suite("wad")
|
||||
t:eq(Wad.name, "wad", "name")
|
||||
|
||||
local fd = Fx.freedoom
|
||||
local a, err = Wad.decode(Bytes.reader(buffer.fromstring(fd.data)))
|
||||
t:check(a, "freedoom: " .. tostring(err))
|
||||
t:eq(F.detect(fd.data), "wad", "F.detect")
|
||||
t:eq(a.meta.source.type, "IWAD", "IWAD")
|
||||
|
||||
-- 1. lumps
|
||||
do
|
||||
local lumps = Wad.lumps(a) :: { Wad.Lump }
|
||||
t:eq(#lumps, #fd.lumps, "lump count")
|
||||
local list = a:list()
|
||||
for i, w in fd.lumps do
|
||||
t:eq(lumps[i].name, w.name, "lump " .. i)
|
||||
t:eq(lumps[i].size, w.size, "size " .. w.name)
|
||||
t:eq(Crc32.of((a:read(list[i].name))), w.crc, "data " .. w.name)
|
||||
end
|
||||
t:eq(list[2].name, "MAP01/THINGS", "map lumps are named after their map")
|
||||
t:eq(lumps[2].map, "MAP01", "map membership")
|
||||
t:check(lumps[1].marker, "map marker")
|
||||
local byName = {}
|
||||
for _, l in lumps do
|
||||
byName[l.name] = l
|
||||
end
|
||||
t:eq(byName.BLOOD1.namespace, "flats", "flats namespace")
|
||||
t:eq(byName.BOS2A1.namespace, "sprites", "sprites namespace")
|
||||
t:eq(byName.AG128_1.namespace, "patches", "patches namespace")
|
||||
t:eq(byName.PLAYPAL.namespace, nil, "no namespace")
|
||||
t:eq(#Wad.maps(a), 1, "one map")
|
||||
end
|
||||
|
||||
-- 2. images against DeuTex
|
||||
do
|
||||
local n = 0
|
||||
for _, w in fd.images do
|
||||
local img, e = Wad.image(a, w.name)
|
||||
t:check(img, w.name .. ": " .. tostring(e))
|
||||
t:eq(img.width, w.width, w.name .. ": width")
|
||||
t:eq(img.height, w.height, w.name .. ": height")
|
||||
if w.kind == "flats" then
|
||||
t:eq(Crc32.of(img.pixels), w.crc, w.name .. ": flat pixels against DeuTex")
|
||||
else
|
||||
t:eq(Crc32.of(img.pixels), w.crc, w.name .. ": picture pixels against DeuTex")
|
||||
if w.left then
|
||||
t:eq(img.meta.offset[1], w.left, w.name .. ": left offset")
|
||||
t:eq(img.meta.offset[2], w.top, w.name .. ": top offset")
|
||||
end
|
||||
end
|
||||
n += 1
|
||||
end
|
||||
t:note("%d graphics match DeuTex", n)
|
||||
for _, tx in fd.textures do
|
||||
local img, e = Wad.texture(a, tx.name)
|
||||
t:check(img, tx.name .. ": " .. tostring(e))
|
||||
t:check(img.width == tx.width and img.height == tx.height, tx.name .. ": texture size")
|
||||
t:eq(img.meta.source.patches, #tx.patches, tx.name .. ": patch count")
|
||||
end
|
||||
end
|
||||
|
||||
-- 3. MAP01
|
||||
do
|
||||
local m = Wad.mapData(a, "MAP01") :: any
|
||||
local r = fd.map
|
||||
t:eq(#m.things, r.things, "things")
|
||||
t:eq(#m.linedefs, r.linedefs, "linedefs")
|
||||
t:eq(#m.sidedefs, r.sidedefs, "sidedefs")
|
||||
t:eq(#m.vertices // 2, r.vertices, "vertices")
|
||||
t:eq(#m.sectors, r.sectors, "sectors")
|
||||
for i, th in r.thingList do
|
||||
local g = m.things[i]
|
||||
t:check(g.x == th.x and g.y == th.y and g.type == th.type and g.angle == th.angle, "thing " .. i)
|
||||
end
|
||||
local t0 = T.clock()
|
||||
local s, e = Wad.map(a, "MAP01")
|
||||
t:check(s, "map scene: " .. tostring(e))
|
||||
local floor, nprim = 0, 0
|
||||
for _, p in s.meshes[1].primitives do
|
||||
nprim += 1
|
||||
local pos = Types.Scene.reader(p.attributes.POSITION) :: any
|
||||
local nrm = Types.Scene.reader(p.attributes.NORMAL) :: any
|
||||
for k = 0, p.attributes.POSITION.count - 1, 3 do
|
||||
if nrm(k, 2) > 0.5 then
|
||||
local ax, ay, bx, by, cx, cy = pos(k, 0), pos(k, 1), pos(k + 1, 0), pos(k + 1, 1), pos(k + 2, 0), pos(k + 2, 1)
|
||||
floor += ((bx - ax) * (cy - ay) - (by - ay) * (cx - ax)) / 2
|
||||
end
|
||||
end
|
||||
end
|
||||
t:check(math.abs(floor - r.floorArea) <= r.floorArea * 0.01, string.format("floor area %.0f vs %.0f", floor, r.floorArea))
|
||||
t:check(#s.nodes == 1 + r.things, "a node per thing")
|
||||
t:eq(s.meta.up, "z", "z up")
|
||||
local withImage = 0
|
||||
for _, tex in s.textures do
|
||||
if tex.image then
|
||||
withImage += 1
|
||||
end
|
||||
end
|
||||
t:note("MAP01: %d primitives, %d textures (%d with images in the cut-down WAD), floor area %.0f of %.0f, %.2f s", nprim, #s.textures, withImage, floor, r.floorArea, T.clock() - t0)
|
||||
local lean = Wad.map(a, "MAP01", { textures = false }) :: any
|
||||
t:check(lean.textures[1].image == nil and lean.textures[1].uri ~= nil, "textures = false")
|
||||
end
|
||||
|
||||
-- 4. the hand-made room
|
||||
do
|
||||
local rm = Wad.decode(Bytes.reader(buffer.fromstring(Fx.room.data)))
|
||||
t:check(rm, "room decodes")
|
||||
local names = {}
|
||||
for _, e in rm:list() do
|
||||
table.insert(names, e.name)
|
||||
end
|
||||
t:eq(names[2], "E1M1/THINGS", "E1M1 lumps")
|
||||
t:eq(names[16], "E1M2/TEXTMAP", "UDMF lumps belong to their map")
|
||||
t:eq(names[17], "E1M2/ENDMAP", "up to ENDMAP")
|
||||
t:eq(names[18], "DEMO1", "after the map")
|
||||
t:eq(names[19], "DEMO1~2", "duplicate names get ~n")
|
||||
local _, ue = Wad.map(rm, "E1M2")
|
||||
t:check(ue and ue.code == Err.UNSUPPORTED, "UDMF unsupported")
|
||||
local s = Wad.map(rm, "E1M1") :: any
|
||||
local area = { floor = 0, ceil = 0, wall = 0 }
|
||||
for _, p in s.meshes[1].primitives do
|
||||
local pos = Types.Scene.reader(p.attributes.POSITION) :: any
|
||||
local nrm = Types.Scene.reader(p.attributes.NORMAL) :: any
|
||||
for k = 0, p.attributes.POSITION.count - 1, 3 do
|
||||
local ux, uy, uz = pos(k + 1, 0) - pos(k, 0), pos(k + 1, 1) - pos(k, 1), pos(k + 1, 2) - pos(k, 2)
|
||||
local vx, vy, vz = pos(k + 2, 0) - pos(k, 0), pos(k + 2, 1) - pos(k, 1), pos(k + 2, 2) - pos(k, 2)
|
||||
local cx, cy, cz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
|
||||
local ar = math.sqrt(cx * cx + cy * cy + cz * cz) / 2
|
||||
-- the winding agrees with the normal
|
||||
t:check(cx * nrm(k, 0) + cy * nrm(k, 1) + cz * nrm(k, 2) > 0, "winding faces the normal")
|
||||
if nrm(k, 2) > 0.5 then
|
||||
area.floor += ar
|
||||
elseif nrm(k, 2) < -0.5 then
|
||||
area.ceil += ar
|
||||
else
|
||||
area.wall += ar
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(area.floor, 256 * 256, "floors: the room around the pillar + the pillar top")
|
||||
t:eq(area.ceil, 256 * 256, "ceilings")
|
||||
t:eq(area.wall, 4 * 256 * 128 + 4 * 64 * 32, "outer walls + the pillar's lower walls")
|
||||
t:eq(s.nodes[2].translation[3], 0, "thing on the room floor")
|
||||
t:eq(s.nodes[3].translation[3], 32, "thing on the pillar")
|
||||
t:eq(s.nodes[2].extras.thing.type, 1, "thing type")
|
||||
local tall = Wad.image(rm, "TALL") :: any
|
||||
t:eq(tall.height, 300, "tall patch height")
|
||||
t:eq(buffer.readu32(tall.pixels, 0), 0, "tall patch: transparent above the first post")
|
||||
t:eq(buffer.readu32(tall.pixels, 100 * 4), buffer.readu32((Wad.palette(rm) :: buffer), 7 * 4), "tall patch: first post")
|
||||
t:eq(buffer.readu32(tall.pixels, 249 * 4), buffer.readu32((Wad.palette(rm) :: buffer), 9 * 4), "tall patch: relative topdelta 100 + 50")
|
||||
t:eq(buffer.readu32(tall.pixels, 260 * 4), 0, "tall patch: transparent below")
|
||||
local flat = Wad.image(rm, "FLOOR") :: any
|
||||
t:check(flat.width == 64 and flat.height == 64, "flat 64 x 64")
|
||||
-- writer: lossless for decoded WADs (map lumps and ~n names stripped back)
|
||||
t:check(buffer.tostring(F.save(rm) :: buffer) == Fx.room.data, "room: decode + save is byte-identical")
|
||||
end
|
||||
|
||||
-- 5. writer
|
||||
do
|
||||
t:check(buffer.tostring(F.save(a) :: buffer) == fd.data, "freedoom: decode + save is byte-identical")
|
||||
local p = Wad.new()
|
||||
p:add("dir/demo1", "abc")
|
||||
p:add("MAP02/THINGS", "")
|
||||
local back = Wad.decode(Bytes.reader(p:write() :: buffer))
|
||||
t:eq(Wad.lumps(back)[1].name, "DEMO1", "names upper-cased, paths stripped")
|
||||
t:eq(back.meta.source.type, "PWAD", "PWAD by default")
|
||||
local long = Wad.new()
|
||||
long:add("NINECHARS", "x")
|
||||
local _, e = long:write()
|
||||
t:check(e and e.code == Err.ARG, "9-byte lump name")
|
||||
local zip = F.format("zip").create()
|
||||
zip:add("x.lmp", "1")
|
||||
t:eq(Wad.lumps(F.load(F.save(zip, nil, { format = "wad" }) :: buffer))[1].name, "X.LMP", "zip -> wad")
|
||||
end
|
||||
|
||||
-- 6. errors
|
||||
do
|
||||
local _, e1 = Wad.decode(Bytes.reader(buffer.fromstring(string.sub(fd.data, 1, 100))))
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1))
|
||||
local _, e2 = Wad.image(a, "NOPE")
|
||||
t:check(e2 and e2.code == Err.ARG, "missing lump")
|
||||
local _, e3 = Wad.image({}, "X")
|
||||
t:check(e3 and e3.code == Err.ARG, "not a WAD")
|
||||
local _, e4 = Wad.map(a, "MAP99")
|
||||
t:check(e4 and e4.code == Err.ARG, "missing map")
|
||||
end
|
||||
|
||||
-- 7. fuzz: the directory, then the lumps of the room (maps, pictures)
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
local w = Wad.decode(Bytes.reader(b))
|
||||
if w then
|
||||
for _, l in Wad.lumps(w) :: any do
|
||||
if not l.map and l.size > 0 then
|
||||
Wad.image(w, l.name)
|
||||
end
|
||||
end
|
||||
for _, m in Wad.maps(w) do
|
||||
Wad.map(w, m)
|
||||
end
|
||||
Wad.texture(w, "X")
|
||||
end
|
||||
return w
|
||||
end, { Fx.room.data }, { mutations = 300 })
|
||||
do
|
||||
-- mutate inside the lumps of the real map and pictures (the directory kept intact)
|
||||
local rand = T.rng(9)
|
||||
local threw = 0
|
||||
for i = 1, 60 do
|
||||
local b = buffer.fromstring(fd.data)
|
||||
for _ = 1, 1 + rand(20) do
|
||||
local at = 12 + rand(#fd.data - 12 - (#fd.lumps * 16))
|
||||
buffer.writeu8(b, at, rand(256))
|
||||
end
|
||||
local ok = pcall(function()
|
||||
local w = Wad.decode(Bytes.reader(b))
|
||||
if w then
|
||||
if i % 6 == 0 then
|
||||
Wad.map(w, "MAP01")
|
||||
end
|
||||
for _, im in fd.images do
|
||||
Wad.image(w, im.name)
|
||||
end
|
||||
end
|
||||
end)
|
||||
if not ok then
|
||||
threw += 1
|
||||
end
|
||||
end
|
||||
t:eq(threw, 0, "mutated lumps never throw")
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,33 @@
|
||||
-- codecs/compress/Explode tests: PKWARE DCL streams written by StormLib's SCompImplode (tools/fixtures/mpq.py:
|
||||
-- text, noise, a short repeat, zeros) decompress to the source bytes; maxOut stops early; hand-made streams with
|
||||
-- coded literals; bad headers and truncations; the fuzz pass.
|
||||
local T = require("@tests/T")
|
||||
local Explode = require("@formats/codecs/compress/Explode")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/mpq")
|
||||
|
||||
local t = T.suite("codecs/explode")
|
||||
local samples = {}
|
||||
for _, c in Fx.implode do
|
||||
local out, err = Explode.decompress(c.data)
|
||||
t:check(out, c.name .. ": " .. tostring(err))
|
||||
t:eq(buffer.len(out), c.size, c.name .. ": size")
|
||||
t:eq(Crc32.of(out), c.crc, c.name .. ": data")
|
||||
t:note("%-6s %6d -> %6d bytes (header %d %d)", c.name, #c.data, c.size, string.byte(c.data, 1), string.byte(c.data, 2))
|
||||
local part = Explode.decompress(c.data, nil, nil, 7)
|
||||
t:eq(buffer.len(part), math.min(7, c.size), c.name .. ": maxOut")
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
-- a stream with coded literals: header 1 (Huffman literals), 4 (1 KB); then only the end code would need the tables:
|
||||
-- check the header checks instead
|
||||
local _, e1 = Explode.decompress("\2\4\0")
|
||||
t:check(e1 and e1.code == Err.CORRUPT, "literal mode 2")
|
||||
local _, e2 = Explode.decompress("\0\7\0")
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "dictionary 7")
|
||||
local _, e3 = Explode.decompress(string.sub(Fx.implode[1].data, 1, 20))
|
||||
t:check(e3 and e3.code == Err.TRUNCATED, "truncated")
|
||||
t:fuzz("decompress", function(b: buffer)
|
||||
return Explode.decompress(b)
|
||||
end, samples, { mutations = 40 })
|
||||
t:done()
|
||||
@@ -0,0 +1,69 @@
|
||||
-- codecs/model/Polygon tests: convex / concave / holed / nested polygons triangulate to the exact area in
|
||||
-- counter-clockwise triangles, either input orientation; degenerate loops; a fuzz of random loops never throws.
|
||||
local T = require("@tests/T")
|
||||
local Polygon = require("@formats/codecs/model/Polygon")
|
||||
|
||||
local t = T.suite("codecs/polygon")
|
||||
|
||||
local function area(loops: { { number } }, tris: { number }): (number, boolean)
|
||||
local a, ccw = 0, true
|
||||
for k = 1, #tris, 6 do
|
||||
local p = {}
|
||||
for c = 0, 2 do
|
||||
local l = loops[tris[k + c * 2]]
|
||||
local i = tris[k + c * 2 + 1]
|
||||
p[c * 2 + 1], p[c * 2 + 2] = l[i * 2 - 1], l[i * 2]
|
||||
end
|
||||
local s = ((p[3] - p[1]) * (p[6] - p[2]) - (p[4] - p[2]) * (p[5] - p[1])) / 2
|
||||
if s <= 0 then
|
||||
ccw = false
|
||||
end
|
||||
a += s
|
||||
end
|
||||
return a, ccw
|
||||
end
|
||||
|
||||
local cases = {
|
||||
{ "square", { { 0, 0, 10, 0, 10, 10, 0, 10 } }, 100 },
|
||||
{ "clockwise square", { { 0, 0, 0, 10, 10, 10, 10, 0 } }, 100 },
|
||||
{ "L shape", { { 0, 0, 20, 0, 20, 10, 10, 10, 10, 20, 0, 20 } }, 300 },
|
||||
{ "comb", { { 0, 0, 50, 0, 50, 10, 40, 10, 40, 2, 30, 2, 30, 10, 20, 10, 20, 2, 10, 2, 10, 10, 0, 10 } }, 50 * 2 + 3 * 10 * 8 },
|
||||
{ "square with a hole", { { 0, 0, 10, 0, 10, 10, 0, 10 }, { 2, 2, 2, 4, 4, 4, 4, 2 } }, 96 },
|
||||
{ "two holes", { { 0, 0, 30, 0, 30, 10, 0, 10 }, { 2, 2, 4, 2, 4, 4, 2, 4 }, { 20, 2, 28, 2, 28, 8, 20, 8 } }, 300 - 4 - 48 },
|
||||
{ "island in a hole", { { 0, 0, 30, 0, 30, 30, 0, 30 }, { 5, 5, 25, 5, 25, 25, 5, 25 }, { 10, 10, 20, 10, 20, 20, 10, 20 } }, 900 - 400 + 100 },
|
||||
{ "degenerate loops ignored", { { 0, 0, 1, 1 }, { 0, 0, 5, 5, 10, 10 }, { 0, 0, 4, 0, 4, 4, 0, 4 } }, 16 },
|
||||
}
|
||||
for _, c in cases do
|
||||
local tris = Polygon.triangulate(c[2])
|
||||
local a, ccw = area(c[2], tris)
|
||||
t:check(math.abs(a - c[3]) < 1e-9, c[1] .. ": area " .. a .. " want " .. c[3])
|
||||
t:check(ccw, c[1] .. ": counter-clockwise triangles")
|
||||
end
|
||||
|
||||
-- random star polygons with a random hole: area preserved, never throws
|
||||
local rand = T.rng(3)
|
||||
local threw = 0
|
||||
for i = 1, 200 do
|
||||
local n = 3 + rand(30)
|
||||
local outer = {}
|
||||
for k = 0, n - 1 do
|
||||
local ang = k / n * math.pi * 2
|
||||
local r = 50 + rand(50)
|
||||
table.insert(outer, math.floor(math.cos(ang) * r))
|
||||
table.insert(outer, math.floor(math.sin(ang) * r))
|
||||
end
|
||||
local hole = { -5, -5, 5, -5, 5, 5, -5, 5 }
|
||||
local ok = pcall(Polygon.triangulate, if i % 2 == 0 then { outer, hole } else { outer })
|
||||
if not ok then
|
||||
threw += 1
|
||||
end
|
||||
local garbage = {}
|
||||
for _ = 1, rand(40) do
|
||||
table.insert(garbage, rand(20) - 10)
|
||||
end
|
||||
if not pcall(Polygon.triangulate, { garbage, { 0 / 0, 1, 2 } }) then
|
||||
threw += 1
|
||||
end
|
||||
end
|
||||
t:eq(threw, 0, "random and garbage loops never throw")
|
||||
t:done()
|
||||
@@ -0,0 +1,89 @@
|
||||
-- image/blp tests: BLP1 / BLP2 palette files written by Pillow and DXT1 / DXT3 / DXT5 / palette-with-alpha files
|
||||
-- written by tools/fixtures/blp.py decode to Pillow's RGBA; BLP1 JPEG (3 components) to Pillow's, 4 components (BGRA)
|
||||
-- within a PSNR of the source; BLP2 BGRA and BLP1 palette + alpha against the writer; our BLP2 writer round-trips
|
||||
-- (with mips); errors; the fuzz pass. Fixtures: tools/fixtures/blp.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Blp = require("@formats/image/blp")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Fx = require("@tests/gen/blp")
|
||||
|
||||
local t = T.suite("blp")
|
||||
t:eq(Blp.name, "blp", "name")
|
||||
|
||||
local function psnr(a: buffer, b: string): number
|
||||
local se = 0
|
||||
for i = 0, #b - 1 do
|
||||
local d = buffer.readu8(a, i) - string.byte(b, i + 1)
|
||||
se += d * d
|
||||
end
|
||||
if se == 0 then
|
||||
return math.huge
|
||||
end
|
||||
return 10 * math.log10(255 * 255 / (se / #b))
|
||||
end
|
||||
|
||||
for _, c in Fx do
|
||||
local img, err = Blp.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(img, c.name .. ": " .. tostring(err))
|
||||
t:eq(F.detect(c.data), "blp", c.name .. ": F.detect")
|
||||
t:check(img.width == c.width and img.height == c.height, c.name .. ": size")
|
||||
if c.crc then
|
||||
t:eq(Crc32.of(img.pixels), c.crc, c.name .. ": pixels against Pillow")
|
||||
elseif c.ref then
|
||||
local worst, alpha = 0, true
|
||||
for i = 0, #c.ref - 1 do
|
||||
local d = math.abs(buffer.readu8(img.pixels, i) - string.byte(c.ref, i + 1))
|
||||
if i % 4 == 3 then
|
||||
alpha = alpha and d == 0
|
||||
else
|
||||
worst = math.max(worst, d)
|
||||
end
|
||||
end
|
||||
t:check(worst <= c.tol, c.name .. ": colours within " .. c.tol .. " of Pillow (worst " .. worst .. ")")
|
||||
t:check(alpha, c.name .. ": alpha equal to Pillow's")
|
||||
elseif c.rgbref then
|
||||
local ok = true
|
||||
for i = 0, img.width * img.height - 1 do
|
||||
for k = 0, 2 do
|
||||
ok = ok and buffer.readu8(img.pixels, i * 4 + k) == string.byte(c.rgbref, i * 4 + k + 1)
|
||||
end
|
||||
ok = ok and buffer.readu8(img.pixels, i * 4 + 3) == string.byte(c.alpha, i + 1)
|
||||
end
|
||||
t:check(ok, c.name .. ": RGB against Pillow, alpha plane against the writer")
|
||||
elseif c.psnr then
|
||||
local p = psnr(img.pixels, c.rgba)
|
||||
t:check(p >= c.psnr, string.format("%s: PSNR %.1f dB", c.name, p))
|
||||
t:note("%s: PSNR %.1f dB against the source", c.name, p)
|
||||
else
|
||||
t:check(buffer.tostring(img.pixels) == c.rgba, c.name .. ": pixels against the writer")
|
||||
end
|
||||
-- our writer: BLP2 BGRA, lossless
|
||||
local enc = Blp.encode(img, { mips = true }) :: buffer
|
||||
local back = Blp.decode(Bytes.reader(enc)) :: any
|
||||
t:eq(Crc32.of(back.pixels), Crc32.of(img.pixels), c.name .. ": BLP2 BGRA round trip")
|
||||
t:check(back.meta.source.mips > 1 or (img.width == 1 and img.height == 1), c.name .. ": mips written")
|
||||
end
|
||||
|
||||
do
|
||||
local _, e1 = Blp.decode(Bytes.reader(buffer.fromstring(string.sub(Fx[3].data, 1, 1200))))
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1))
|
||||
local _, e2 = Blp.decode(Bytes.reader(buffer.fromstring("BLP0" .. string.rep("\0", 200))))
|
||||
t:check(e2 and e2.code == Err.UNSUPPORTED, "BLP0")
|
||||
local _, e3 = Blp.encode({ format = "rgba16" } :: any)
|
||||
t:check(e3 and e3.code == Err.ARG, "encode needs rgba8")
|
||||
local one = Types.Image.new(1, 1)
|
||||
t:check(Blp.decode(Bytes.reader(Blp.encode(one) :: buffer)) ~= nil, "1 x 1")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx do
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Blp.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 40 })
|
||||
t:done()
|
||||
@@ -0,0 +1,95 @@
|
||||
-- image/tim tests: PlayStation TIM files written by tools/fixtures/tim.py (4 / 8-bit CLUT, 15-bit, 24-bit) decode to
|
||||
-- ImageMagick's RGB, colour 0x0000 is transparent, CLUT rows, the VRAM position; the encoder round-trips 16 / 24 /
|
||||
-- 8 / 4-bit; errors; the fuzz pass.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Tim = require("@formats/image/tim")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Fx = require("@tests/gen/tim")
|
||||
|
||||
local t = T.suite("tim")
|
||||
t:eq(Tim.name, "tim", "name")
|
||||
|
||||
local function rgb(img: any): buffer
|
||||
local n = img.width * img.height
|
||||
local out = buffer.create(n * 3)
|
||||
for i = 0, n - 1 do
|
||||
buffer.copy(out, i * 3, img.pixels, i * 4, 3)
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
for _, c in Fx do
|
||||
local img, err = Tim.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(img, c.name .. ": " .. tostring(err))
|
||||
t:eq(F.detect(c.data), "tim", c.name .. ": F.detect")
|
||||
t:check(img.width == c.width and img.height == c.height, c.name .. ": size")
|
||||
-- transparent pixels: compare RGB with ImageMagick where opaque (it reads 0x0000 as black)
|
||||
t:eq(Crc32.of(rgb(img)), c.rgb[1], c.name .. ": RGB against ImageMagick")
|
||||
local tr = 0
|
||||
for i = 0, img.width * img.height - 1 do
|
||||
if buffer.readu8(img.pixels, i * 4 + 3) == 0 then
|
||||
tr += 1
|
||||
end
|
||||
end
|
||||
t:eq(tr, c.transparent, c.name .. ": transparent pixels")
|
||||
t:eq(img.meta.source.bpp, c.bpp, c.name .. ": bpp")
|
||||
t:eq(img.meta.source.x, 320, c.name .. ": VRAM x")
|
||||
if c.cluts > 1 then
|
||||
local other = Tim.decode(Bytes.reader(buffer.fromstring(c.data)), { clut = 1 }) :: any
|
||||
t:check(Crc32.of(other.pixels) ~= Crc32.of(img.pixels) and other.meta.source.clut.row == 1, c.name .. ": second CLUT row")
|
||||
end
|
||||
-- round trips: decoded 4 / 8 / 15-bit pixels are PSX colours, so every depth that fits is lossless (24-bit only
|
||||
-- round-trips as 24-bit)
|
||||
for _, bpp in (if c.bpp == 24 then { 24 } else { 16, 24, 8, 4 }) do
|
||||
local enc, eerr = Tim.encode(img, { bpp = bpp })
|
||||
if bpp == 24 then
|
||||
-- 24-bit drops alpha: compare RGB
|
||||
local back = Tim.decode(Bytes.reader(enc :: buffer)) :: any
|
||||
t:eq(Crc32.of(rgb(back)), Crc32.of(rgb(img)), c.name .. ": 24-bit round trip (RGB)")
|
||||
elseif enc then
|
||||
-- 4 / 8-bit rows hold whole 16-bit units: odd widths come back padded on the right
|
||||
local back = Tim.decode(Bytes.reader(enc)) :: any
|
||||
local same = back.height == img.height and back.width >= img.width
|
||||
for y = 0, img.height - 1 do
|
||||
for x = 0, img.width - 1 do
|
||||
same = same and buffer.readu32(back.pixels, (y * back.width + x) * 4) == buffer.readu32(img.pixels, (y * img.width + x) * 4)
|
||||
end
|
||||
end
|
||||
t:check(same, c.name .. ": " .. bpp .. "-bit round trip")
|
||||
else
|
||||
t:check(eerr and eerr.code == Err.ARG, c.name .. ": " .. bpp .. "-bit refused (too many colours)")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
do
|
||||
local img = Types.Image.new(3, 1)
|
||||
buffer.writeu32(img.pixels, 0, 0xFF000000) -- opaque black
|
||||
buffer.writeu32(img.pixels, 4, 0x00FFFFFF) -- transparent
|
||||
buffer.writeu32(img.pixels, 8, 0xFF0000FF) -- red
|
||||
local back = Tim.decode(Bytes.reader(Tim.encode(img) :: buffer)) :: any
|
||||
t:eq(buffer.readu32(back.pixels, 0), 0xFF000000, "opaque black stays opaque (STP)")
|
||||
t:eq(buffer.readu32(back.pixels, 4), 0, "transparent")
|
||||
t:eq(buffer.readu32(back.pixels, 8), 0xFF0000FF, "red")
|
||||
local _, e1 = Tim.encode(img, { bpp = 12 })
|
||||
t:check(e1 and e1.code == Err.ARG, "bpp 12")
|
||||
local _, e2 = Tim.decode(Bytes.reader(buffer.fromstring(string.sub(Fx[1].data, 1, 40))))
|
||||
t:check(e2 and e2.code == Err.TRUNCATED, "truncated: " .. tostring(e2))
|
||||
local mixed = buffer.fromstring(Fx[3].data)
|
||||
buffer.writeu8(mixed, 4, 4)
|
||||
local _, e3 = Tim.decode(Bytes.reader(mixed))
|
||||
t:check(e3 and e3.code == Err.UNSUPPORTED, "mixed mode")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx do
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Tim.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 60 })
|
||||
t:done()
|
||||
@@ -0,0 +1,74 @@
|
||||
-- image/vtf tests: VTFs written by srctools (tools/fixtures/vtf.py: every texel format it writes, versions 7.2 / 7.4
|
||||
-- / 7.5, mips, two-frame animations) decode to srctools' own RGBA (DXT within its libsquish rounding), frames, the
|
||||
-- mip / thumbnail layout; the half / float / 16-bit formats srctools cannot write against the fixture writer; errors;
|
||||
-- the fuzz pass.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Vtf = require("@formats/image/vtf")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/vtf")
|
||||
|
||||
local t = T.suite("vtf")
|
||||
t:eq(Vtf.name, "vtf", "name")
|
||||
|
||||
local function compare(px: buffer, want: string, tol: number): (number, number)
|
||||
local worst, at = 0, -1
|
||||
for i = 0, #want - 1 do
|
||||
local d = math.abs(buffer.readu8(px, i) - string.byte(want, i + 1))
|
||||
if d > worst then
|
||||
worst, at = d, i
|
||||
end
|
||||
end
|
||||
return worst, at
|
||||
end
|
||||
|
||||
local exact = 0
|
||||
for _, c in Fx do
|
||||
local img, err = Vtf.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(img, c.name .. ": " .. tostring(err))
|
||||
t:eq(F.detect(c.data), "vtf", c.name .. ": F.detect")
|
||||
t:check(img.width == c.width and img.height == c.height, c.name .. ": size")
|
||||
local worst, at = compare(img.pixels, c.rgba, c.tol)
|
||||
t:check(worst <= c.tol, string.format("%s (%s): within %d of srctools (worst %d at byte %d)", c.name, c.version, c.tol, worst, at))
|
||||
if worst == 0 then
|
||||
exact += 1
|
||||
end
|
||||
t:eq(img.meta.source.texel, c.name, c.name .. ": texel format")
|
||||
t:check(img.meta.source.mips > 1 or c.mips == 1, c.name .. ": mips counted")
|
||||
if c.frames > 1 then
|
||||
t:eq(#img.frames, c.frames, c.name .. ": frames")
|
||||
local lw = compare(img.frames[c.frames].image.pixels, c.last, c.tol)
|
||||
t:check(lw <= c.tol, c.name .. ": last frame")
|
||||
end
|
||||
if c.mips ~= 1 then
|
||||
local small = Vtf.decode(Bytes.reader(buffer.fromstring(c.data)), { mip = 1 }) :: any
|
||||
t:check(small and small.width == math.max(1, c.width // 2), c.name .. ": mip 1")
|
||||
end
|
||||
end
|
||||
t:note("%d of %d files exactly equal to their reference (srctools or the fixture writer)", exact, #Fx)
|
||||
|
||||
do
|
||||
local c = Fx[1]
|
||||
local _, e1 = Vtf.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, #c.data - 20))))
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1))
|
||||
local bad = buffer.fromstring(c.data)
|
||||
buffer.writeu32(bad, 8, 9)
|
||||
local _, e2 = Vtf.decode(Bytes.reader(bad))
|
||||
t:check(e2 and e2.code == Err.UNSUPPORTED, "version 7.9")
|
||||
local p8 = buffer.fromstring(c.data)
|
||||
buffer.writei32(p8, 52, 7)
|
||||
local _, e3 = Vtf.decode(Bytes.reader(p8))
|
||||
t:check(e3 and e3.code == Err.UNSUPPORTED, "P8")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for i, c in Fx do
|
||||
if i % 3 == 0 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Vtf.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 30 })
|
||||
t:done()
|
||||
@@ -0,0 +1,99 @@
|
||||
-- tests/model/_soup: helpers for the game model tests: a primitive's triangle soup (positions, morph target frames,
|
||||
-- texture coordinates in index order) and a tolerant comparison against assimp's soup.
|
||||
local Types = require("@formats/Types")
|
||||
|
||||
local Soup = {}
|
||||
|
||||
-- positions(prim, target?) -> f32 x3 buffer of the corners in index order (target k adds morph target k's deltas)
|
||||
function Soup.positions(prim: any, target: number?): buffer
|
||||
local pos = Types.Scene.reader(prim.attributes.POSITION) :: any
|
||||
local d = if target then Types.Scene.reader(prim.targets[target].POSITION) :: any else nil
|
||||
local idx = prim.indices
|
||||
local n = idx.count
|
||||
local out = buffer.create(n * 12)
|
||||
for k = 0, n - 1 do
|
||||
local v = buffer.readu32(idx.data, k * 4)
|
||||
for c = 0, 2 do
|
||||
buffer.writef32(out, k * 12 + c * 4, pos(v, c) + (if d then d(v, c) else 0))
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
-- uvs(prim, set?, flip?) -> { u, v, ... } of the corners in index order (flip: v -> 1 - v, assimp's convention;
|
||||
-- "neg": v -> -v, assimp's Half-Life loader)
|
||||
function Soup.uvs(prim: any, set: number?, flip: (boolean | string)?): { number }
|
||||
local tc = Types.Scene.reader(prim.attributes["TEXCOORD_" .. (set or 0)]) :: any
|
||||
local idx = prim.indices
|
||||
local out = {}
|
||||
for k = 0, idx.count - 1 do
|
||||
local v = buffer.readu32(idx.data, k * 4)
|
||||
table.insert(out, tc(v, 0))
|
||||
table.insert(out, if flip == "neg" then -tc(v, 1) elseif flip then 1 - tc(v, 1) else tc(v, 1))
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
-- close(a: buffer f32 | {number}, b: {number}, tol) -> true | false, first index that differs
|
||||
function Soup.close(a: any, b: { number }, tol: number): (boolean, number?)
|
||||
local n = if type(a) == "buffer" then buffer.len(a) // 4 else #a
|
||||
if n ~= #b then
|
||||
return false, -1
|
||||
end
|
||||
for i = 1, n do
|
||||
local x = if type(a) == "buffer" then buffer.readf32(a, (i - 1) * 4) else a[i]
|
||||
if math.abs(x - b[i]) > tol * math.max(1, math.abs(b[i])) then
|
||||
return false, i
|
||||
end
|
||||
end
|
||||
return true, nil
|
||||
end
|
||||
|
||||
-- triangles(a, b, tol, stride?) -> true when the triangle lists a and b (flat numbers, stride per corner: 3 for
|
||||
-- positions, 2 for texture coordinates) hold the same triangles in the same order, each up to a cyclic rotation of
|
||||
-- its corners (assimp rotates them); false, the first differing triangle otherwise
|
||||
function Soup.triangles(a: { number }, b: { number }, tol: number, stride: number?): (boolean, number?)
|
||||
local s = stride or 3
|
||||
if #a ~= #b then
|
||||
return false, -1
|
||||
end
|
||||
local per = 3 * s
|
||||
for t = 0, #a // per - 1 do
|
||||
local found = false
|
||||
for rot = 0, 2 do
|
||||
local same = true
|
||||
for c = 0, 2 do
|
||||
local ca, cb = t * per + c * s, t * per + ((c + rot) % 3) * s
|
||||
for k = 1, s do
|
||||
if math.abs(a[ca + k] - b[cb + k]) > tol * math.max(1, math.abs(b[cb + k])) then
|
||||
same = false
|
||||
end
|
||||
end
|
||||
end
|
||||
if same then
|
||||
found = true
|
||||
break
|
||||
end
|
||||
end
|
||||
if not found then
|
||||
return false, t
|
||||
end
|
||||
end
|
||||
return true, nil
|
||||
end
|
||||
|
||||
-- list(buf) -> the f32 values of a buffer as numbers
|
||||
function Soup.list(b: buffer): { number }
|
||||
local out = table.create(buffer.len(b) // 4)
|
||||
for i = 0, buffer.len(b) // 4 - 1 do
|
||||
out[i + 1] = buffer.readf32(b, i * 4)
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
-- slice(list, from, n) -> n values from index from (1-based)
|
||||
function Soup.slice(list: { number }, from: number, n: number): { number }
|
||||
return table.move(list, from, from + n - 1, 1, table.create(n))
|
||||
end
|
||||
|
||||
return Soup
|
||||
@@ -0,0 +1,226 @@
|
||||
-- model/bsp tests: Quake 1, Half-Life, Quake II and Quake III maps written by tools/fixtures/bsp.py: every model's
|
||||
-- triangles per texture (positions, normals, texture coordinates against the documented rules), counter-clockwise
|
||||
-- about the face normals, embedded textures (Quake through the palette, Half-Life through its own), WAD / .wal
|
||||
-- textures as URIs, entities; Quake III: polygon / mesh faces against assimp's soup (of a .pk3) and the writer,
|
||||
-- lightmaps, vertex colours, a tessellated patch; errors; the fuzz pass.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Bsp = require("@formats/model/bsp")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Soup = require("@tests/model/_soup")
|
||||
local Fx = require("@tests/gen/bsp")
|
||||
|
||||
local t = T.suite("bsp")
|
||||
t:eq(Bsp.name, "bsp", "name")
|
||||
|
||||
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 * math.max(1, math.abs(b[i])) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local function meshOf(s: any, model: number): any
|
||||
for _, n in s.nodes do
|
||||
if n.mesh and n.extras and n.extras.bsp and n.extras.bsp.model == model then
|
||||
return s.meshes[n.mesh]
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
-- winding: every triangle's geometric normal agrees with its NORMAL
|
||||
local function windingOk(p: any): boolean
|
||||
local pos = Types.Scene.reader(p.attributes.POSITION) :: any
|
||||
local nrm = Types.Scene.reader(p.attributes.NORMAL) :: any
|
||||
local idx = p.indices
|
||||
for k = 0, idx.count - 1, 3 do
|
||||
local a, b, c = buffer.readu32(idx.data, k * 4), buffer.readu32(idx.data, k * 4 + 4), buffer.readu32(idx.data, k * 4 + 8)
|
||||
local ux, uy, uz = pos(b, 0) - pos(a, 0), pos(b, 1) - pos(a, 1), pos(b, 2) - pos(a, 2)
|
||||
local vx, vy, vz = pos(c, 0) - pos(a, 0), pos(c, 1) - pos(a, 1), pos(c, 2) - pos(a, 2)
|
||||
local cx, cy, cz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
|
||||
if cx * nrm(a, 0) + cy * nrm(a, 1) + cz * nrm(a, 2) <= 0 then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local function brush(label: string, c: any, opts: any)
|
||||
local s, err = Bsp.decode(Bytes.reader(buffer.fromstring(c.data)), opts)
|
||||
t:check(s, label .. ": " .. tostring(err))
|
||||
t:eq(F.detect(c.data), "bsp", label .. ": F.detect")
|
||||
for model, texs in c.models do
|
||||
local mesh = meshOf(s, tonumber(model) :: number)
|
||||
t:check(mesh, label .. ": model " .. model)
|
||||
for tname, want in texs do
|
||||
local prim
|
||||
for _, p in mesh.primitives do
|
||||
if p.material and s.materials[p.material].name == tname then
|
||||
prim = p
|
||||
end
|
||||
end
|
||||
t:check(prim, label .. ": model " .. model .. " texture " .. tname)
|
||||
t:check(near(Soup.list(Soup.positions(prim)), want.pos, 1e-6), label .. ": " .. tname .. " positions")
|
||||
t:check(near(Soup.uvs(prim, 0), want.uv, 1e-5), label .. ": " .. tname .. " texture coordinates")
|
||||
t:check(near(Soup.list(prim.attributes.NORMAL.data), want.nrm, 1e-6), label .. ": " .. tname .. " normals")
|
||||
t:check(windingOk(prim), label .. ": " .. tname .. " counter-clockwise")
|
||||
end
|
||||
end
|
||||
t:eq(#s.meta.entities, 3, label .. ": entities")
|
||||
t:eq(s.meta.entities[2].classname, "info_player_start", label .. ": entity classname")
|
||||
t:eq(s.meta.entities[3].model, "*1", label .. ": brush entity model")
|
||||
return s
|
||||
end
|
||||
|
||||
-- 1. Quake 1, Half-Life
|
||||
do
|
||||
local q1 = brush("q1", Fx.q1, { palette = Fx.q1.palette })
|
||||
t:eq(Crc32.of(q1.textures[1].image.pixels), Fx.q1.textures[1], "q1: embedded texture through the palette")
|
||||
t:eq(q1.textures[2].uri, "+0door", "q1: WAD texture as a URI")
|
||||
t:eq(q1.meta.source.variant, "quake", "q1: variant")
|
||||
local hl = brush("hl", Fx.hl, nil)
|
||||
t:eq(Crc32.of(hl.textures[1].image.pixels), Fx.hl.textures[1], "hl: embedded texture through its own palette")
|
||||
t:eq(hl.meta.source.variant, "halflife", "hl: variant")
|
||||
local grey = Bsp.decode(Bytes.reader(buffer.fromstring(Fx.q1.data))) :: any
|
||||
t:eq(grey.meta.source.palette, false, "q1 without a palette: grey")
|
||||
end
|
||||
|
||||
-- 2. Quake II
|
||||
do
|
||||
local q2 = brush("q2", Fx.q2, { files = { ["textures/brick.wal"] = Fx.q2.wal } })
|
||||
t:eq(q2.textures[1].uri, "textures/brick.wal", "q2: .wal URI")
|
||||
t:eq(q2.materials[1].extras.bsp.width, 16, "q2: size from the .wal")
|
||||
t:eq(q2.materials[2].extras.bsp.texels, true, "q2: texel coordinates without the .wal")
|
||||
end
|
||||
|
||||
-- 3. Quake III
|
||||
do
|
||||
local q = Fx.q3
|
||||
local s, err = Bsp.decode(Bytes.reader(buffer.fromstring(q.data)))
|
||||
t:check(s, "q3: " .. tostring(err))
|
||||
t:eq(F.detect(q.data), "bsp", "q3: F.detect")
|
||||
for model, keys in q.models do
|
||||
local mesh = meshOf(s, tonumber(model) :: number)
|
||||
for key, want in keys do
|
||||
local sh, lm = string.match(key, "^(%-?%d+):(%-?%d+)$")
|
||||
local prim
|
||||
for _, p in mesh.primitives do
|
||||
local mat = s.materials[p.material]
|
||||
local lt = mat.extras.bsp.lightmap
|
||||
local tex = mat.baseColorTexture and mat.baseColorTexture.texture
|
||||
if tex == tonumber(sh) :: number + 1 and ((lt and s.textures[lt].image.meta.source.lightmap) or -1) == tonumber(lm) then
|
||||
prim = p
|
||||
end
|
||||
end
|
||||
t:check(prim, "q3: model " .. model .. " batch " .. key)
|
||||
t:check(near(Soup.list(Soup.positions(prim)), want.pos, 1e-6), "q3: " .. key .. " positions")
|
||||
t:check(near(Soup.uvs(prim, 0), want.uv, 1e-6), "q3: " .. key .. " texture coordinates")
|
||||
t:check(near(Soup.uvs(prim, 1), want.lm, 1e-6), "q3: " .. key .. " lightmap coordinates")
|
||||
local col = {}
|
||||
local cr = Types.Scene.reader(prim.attributes.COLOR_0) :: any
|
||||
for k = 0, prim.indices.count - 1 do
|
||||
local v = buffer.readu32(prim.indices.data, k * 4)
|
||||
for c = 0, 3 do
|
||||
table.insert(col, math.floor(cr(v, c) * 255 + 0.5))
|
||||
end
|
||||
end
|
||||
t:check(near(col, want.col, 0), "q3: " .. key .. " vertex colours")
|
||||
t:check(windingOk(prim), "q3: " .. key .. " counter-clockwise about the normals")
|
||||
end
|
||||
end
|
||||
-- assimp: the polygon / mesh faces
|
||||
local all = {}
|
||||
local apos = q.assimp.pos
|
||||
for _, mesh in s.meshes do
|
||||
for _, p in mesh.primitives do
|
||||
local mat = s.materials[p.material]
|
||||
if mat.name ~= "textures/base/floor" or p.indices.count == 6 then
|
||||
if p.indices.count == 6 then
|
||||
for _, x in Soup.list(Soup.positions(p)) do
|
||||
table.insert(all, x)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(q.assimp.faces, 6, "q3: assimp reads the 3 quads")
|
||||
-- compare triangle sets: every assimp triangle is one of ours (up to corner rotation); assimp keeps the stored
|
||||
-- clockwise order, we reverse it
|
||||
local found = 0
|
||||
for k = 0, #apos // 9 - 1 do
|
||||
local tri = Soup.slice(apos, k * 9 + 1, 9)
|
||||
tri = { tri[7], tri[8], tri[9], tri[4], tri[5], tri[6], tri[1], tri[2], tri[3] }
|
||||
for j = 0, #all // 9 - 1 do
|
||||
if Soup.triangles(Soup.slice(all, j * 9 + 1, 9), tri, 1e-5) then
|
||||
found += 1
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(found, 6, "q3: every assimp triangle matches one of ours")
|
||||
t:eq(#q.lightmaps, 2, "q3: lightmaps")
|
||||
for i, crc in q.lightmaps do
|
||||
local tex
|
||||
for _, tx in s.textures do
|
||||
if tx.image and tx.image.meta.source.lightmap == i - 1 then
|
||||
tex = tx
|
||||
end
|
||||
end
|
||||
t:eq(Crc32.of(tex.image.pixels), crc, "q3: lightmap " .. i)
|
||||
end
|
||||
-- the patch: a flat 32 x 32 square at z = 100, tessellated
|
||||
local area = 0
|
||||
for _, mesh in s.meshes do
|
||||
for _, p in mesh.primitives do
|
||||
local pos = Types.Scene.reader(p.attributes.POSITION) :: any
|
||||
if pos(0, 2) == 100 then
|
||||
t:check(windingOk(p), "q3: patch counter-clockwise")
|
||||
for k = 0, p.indices.count - 1, 3 do
|
||||
local ux, uy = pos(k + 1, 0) - pos(k, 0), pos(k + 1, 1) - pos(k, 1)
|
||||
local vx, vy = pos(k + 2, 0) - pos(k, 0), pos(k + 2, 1) - pos(k, 1)
|
||||
area += (ux * vy - uy * vx) / 2
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
t:check(math.abs(math.abs(area) - q.patchArea) < 1e-3, "q3: patch area " .. area)
|
||||
t:eq(s.meta.faces.patches, 1, "q3: one patch")
|
||||
local lean = Bsp.decode(Bytes.reader(buffer.fromstring(q.data)), { patchLevel = 1 }) :: any
|
||||
t:check(lean ~= nil, "q3: patchLevel 1")
|
||||
end
|
||||
|
||||
-- 4. errors, fuzz
|
||||
do
|
||||
local _, e1 = Bsp.decode(Bytes.reader(buffer.fromstring(string.sub(Fx.q1.data, 1, 100))))
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1))
|
||||
local _, e2 = Bsp.decode(Bytes.reader(buffer.fromstring("VBSP" .. string.rep("\0", 100))))
|
||||
t:check(e2 and e2.code == Err.UNSUPPORTED, "VBSP")
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Bsp.decode(Bytes.reader(b), { palette = Fx.q1.palette })
|
||||
end, { Fx.q1.data, Fx.hl.data, Fx.q2.data }, { mutations = 80 })
|
||||
do
|
||||
local rand = T.rng(13)
|
||||
local threw = 0
|
||||
local q = Fx.q3.data
|
||||
for _ = 1, 60 do
|
||||
local b = buffer.fromstring(q)
|
||||
for _ = 1, 1 + rand(10) do
|
||||
buffer.writeu8(b, 8 + 17 * 8 + rand(#q - 8 - 17 * 8 - 98304), rand(256))
|
||||
end
|
||||
if not pcall(Bsp.decode, Bytes.reader(b)) then
|
||||
threw += 1
|
||||
end
|
||||
end
|
||||
t:eq(threw, 0, "q3: mutated lumps never throw")
|
||||
end
|
||||
t:done()
|
||||
@@ -0,0 +1,83 @@
|
||||
-- model/md2 tests: MD2 files written by tools/fixtures/md2.py decode to assimp's frame-0 triangle soup (positions,
|
||||
-- texture coordinates) and to the writer's exact positions for every frame (base + morph target deltas), frame
|
||||
-- groups as animations, skins as texture URIs, opts.frame / morph = false, errors, the fuzz pass.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Md2 = require("@formats/model/md2")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Soup = require("@tests/model/_soup")
|
||||
local Fx = require("@tests/gen/md2")
|
||||
|
||||
local t = T.suite("md2")
|
||||
t:eq(Md2.name, "md2", "name")
|
||||
|
||||
for _, c in Fx do
|
||||
local s, err = Md2.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(s, c.name .. ": " .. tostring(err))
|
||||
t:eq(F.detect(c.data), "md2", c.name .. ": F.detect")
|
||||
local prim = s.meshes[1].primitives[1]
|
||||
t:eq(prim.indices.count, c.tris * 3, c.name .. ": triangles")
|
||||
-- assimp, frame 0
|
||||
local ok, at = Soup.close(Soup.positions(prim), c.assimp.pos, 1e-5)
|
||||
t:check(ok, c.name .. ": positions against assimp (" .. tostring(at) .. ")")
|
||||
local uok, uat = Soup.close(Soup.uvs(prim, 0, true), c.assimp.uv, 1e-6)
|
||||
t:check(uok, c.name .. ": texture coordinates against assimp (" .. tostring(uat) .. ")")
|
||||
-- every frame, exact
|
||||
t:eq(Crc32.of(Soup.positions(prim)), c.crcs[1], c.name .. ": frame 1 exact")
|
||||
for k = 1, c.frames do
|
||||
local f = Soup.positions(prim, k)
|
||||
-- base + delta in f32 can differ from the frame by an ulp; compare to the decoded frame directly
|
||||
local frame = Md2.decode(Bytes.reader(buffer.fromstring(c.data)), { frame = k, morph = false }) :: any
|
||||
t:eq(Crc32.of(Soup.positions(frame.meshes[1].primitives[1])), c.crcs[k], c.name .. ": frame " .. k .. " exact (opts.frame)")
|
||||
local fok = Soup.close(f, (function()
|
||||
local list = {}
|
||||
local fp = Soup.positions(frame.meshes[1].primitives[1])
|
||||
for i = 0, buffer.len(fp) // 4 - 1 do
|
||||
list[i + 1] = buffer.readf32(fp, i * 4)
|
||||
end
|
||||
return list
|
||||
end)(), 1e-5)
|
||||
t:check(fok, c.name .. ": morph target " .. k .. " = frame " .. k)
|
||||
end
|
||||
-- animations from the frame names
|
||||
local groups = {}
|
||||
for _, a in s.animations do
|
||||
table.insert(groups, a.name)
|
||||
end
|
||||
t:eq(#s.meshes[1].weights, c.frames, c.name .. ": one weight per frame")
|
||||
t:eq(s.meta.source.frameNames[1], c.names[1], c.name .. ": frame names")
|
||||
if c.name == "monster" then
|
||||
t:eq(table.concat(groups, ","), "stand,run,attack,pain", c.name .. ": animation groups")
|
||||
t:eq(s.animations[2].channels[1].input.count, 4, c.name .. ": run has 4 keys")
|
||||
end
|
||||
-- skins
|
||||
t:eq(#s.textures, #c.skins, c.name .. ": skins")
|
||||
if #c.skins > 0 then
|
||||
t:eq(s.textures[1].uri, c.skins[1], c.name .. ": skin uri")
|
||||
t:eq(s.materials[prim.material].baseColorTexture.texture, 1, c.name .. ": material")
|
||||
end
|
||||
t:eq(s.meta.up, "z", c.name .. ": z up")
|
||||
end
|
||||
|
||||
do
|
||||
local c = Fx[1]
|
||||
local lean = Md2.decode(Bytes.reader(buffer.fromstring(c.data)), { morph = false }) :: any
|
||||
t:check(lean.meshes[1].primitives[1].targets == nil and #lean.animations == 0, "morph = false")
|
||||
local _, e1 = Md2.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, 300))))
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "truncated: " .. tostring(e1))
|
||||
local bad = buffer.fromstring(c.data)
|
||||
buffer.writei32(bad, 4, 7)
|
||||
local _, e2 = Md2.decode(Bytes.reader(bad))
|
||||
t:check(e2 and e2.code == Err.UNSUPPORTED, "version 7")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx do
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Md2.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 80 })
|
||||
t:done()
|
||||
@@ -0,0 +1,109 @@
|
||||
-- model/md3 tests: MD3 files written by tools/fixtures/md3.py (several surfaces, frames, tags) decode to assimp's
|
||||
-- frame-0 triangle soup (every surface: positions, texture coordinates) and to the writer's exact positions and
|
||||
-- normals for every frame, shaders as texture URIs, tags as nodes with their per-frame channels, errors, the fuzz
|
||||
-- pass.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Md3 = require("@formats/model/md3")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Soup = require("@tests/model/_soup")
|
||||
local Fx = require("@tests/gen/md3")
|
||||
|
||||
local t = T.suite("md3")
|
||||
t:eq(Md3.name, "md3", "name")
|
||||
|
||||
local function normals(prim: any): buffer
|
||||
local n = Types.Scene.reader(prim.attributes.NORMAL) :: any
|
||||
local idx = prim.indices
|
||||
local out = buffer.create(idx.count * 12)
|
||||
for k = 0, idx.count - 1 do
|
||||
local v = buffer.readu32(idx.data, k * 4)
|
||||
for c = 0, 2 do
|
||||
buffer.writef32(out, k * 12 + c * 4, n(v, c))
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
for _, c in Fx do
|
||||
local s, err = Md3.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(s, c.name .. ": " .. tostring(err))
|
||||
t:eq(F.detect(c.data), "md3", c.name .. ": F.detect")
|
||||
local prims = s.meshes[1].primitives
|
||||
t:eq(#prims, #c.surfaces, c.name .. ": a primitive per surface")
|
||||
-- assimp: every surface's frame 0 (assimp orders the meshes differently and rotates triangle corners: match each
|
||||
-- assimp mesh to the surface whose triangles it holds)
|
||||
t:eq(c.assimp.meshes, #c.surfaces, c.name .. ": assimp meshes")
|
||||
local matched = 0
|
||||
for mi, m in c.assimp.per do
|
||||
local nextStart = if c.assimp.per[mi + 1] then c.assimp.per[mi + 1].start else c.assimp.faces
|
||||
local nf = nextStart - m.start
|
||||
local apos = Soup.slice(c.assimp.pos, m.start * 9 + 1, nf * 9)
|
||||
local auv = Soup.slice(c.assimp.uv, m.start * 6 + 1, nf * 6)
|
||||
for _, p in prims do
|
||||
if p.indices.count == nf * 3 and Soup.triangles(Soup.list(Soup.positions(p)), apos, 1e-5) then
|
||||
t:check(Soup.triangles(Soup.uvs(p, 0, true), auv, 1e-5, 2), c.name .. ": texture coordinates against assimp")
|
||||
matched += 1
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(matched, #c.surfaces, c.name .. ": every surface matches assimp's positions")
|
||||
for si, w in c.surfaces do
|
||||
t:eq(Crc32.of(Soup.positions(prims[si])), w.crcs[1], c.name .. ": " .. w.name .. " frame 1")
|
||||
t:eq(s.textures[s.materials[prims[si].material].baseColorTexture.texture].uri, w.shader, c.name .. ": shader")
|
||||
for k = 1, c.frames do
|
||||
local fs = Md3.decode(Bytes.reader(buffer.fromstring(c.data)), { frame = k, morph = false }) :: any
|
||||
local fp = fs.meshes[1].primitives[si]
|
||||
t:eq(Crc32.of(Soup.positions(fp)), w.crcs[k], c.name .. ": " .. w.name .. " frame " .. k)
|
||||
local nb = normals(fp)
|
||||
-- normals are computed in double then stored as f32: compare by CRC of the fixture's f32 values
|
||||
t:eq(Crc32.of(nb), w.ncrcs[k], c.name .. ": " .. w.name .. " normals frame " .. k)
|
||||
end
|
||||
if c.frames > 1 then
|
||||
t:eq(#prims[si].targets, c.frames, c.name .. ": morph targets")
|
||||
end
|
||||
end
|
||||
-- tags
|
||||
local ntags = #c.tags // c.frames
|
||||
t:eq(#s.meta.source.tags, ntags, c.name .. ": tags")
|
||||
for _, tg in c.tags do
|
||||
if tg.frame == 1 then
|
||||
local node
|
||||
for _, n in s.nodes do
|
||||
if n.name == tg.name then
|
||||
node = n
|
||||
end
|
||||
end
|
||||
t:check(node and math.abs(node.translation[1] - tg.origin[1]) < 1e-6, c.name .. ": tag " .. tg.name .. " origin")
|
||||
-- the rotation about z by angle
|
||||
t:check(node and math.abs(node.rotation[3] - math.sin(tg.angle / 2)) < 1e-5, c.name .. ": tag " .. tg.name .. " rotation")
|
||||
end
|
||||
end
|
||||
if c.frames > 1 then
|
||||
t:eq(#s.animations, 1, c.name .. ": one animation")
|
||||
t:eq(#s.animations[1].channels, 1 + 2 * ntags, c.name .. ": weights + tag channels")
|
||||
end
|
||||
end
|
||||
|
||||
do
|
||||
local c = Fx[1]
|
||||
local _, e1 = Md3.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, 500))))
|
||||
t:check(e1 and (e1.code == Err.TRUNCATED or e1.code == Err.CORRUPT), "truncated: " .. tostring(e1))
|
||||
local bad = buffer.fromstring(c.data)
|
||||
buffer.writei32(bad, 4, 14)
|
||||
local _, e2 = Md3.decode(Bytes.reader(bad))
|
||||
t:check(e2 and e2.code == Err.UNSUPPORTED, "version 14")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx do
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Md3.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 80 })
|
||||
t:done()
|
||||
@@ -0,0 +1,153 @@
|
||||
-- model/md5 tests: an .md5mesh and two .md5anim files written by tools/fixtures/md5.py: the bind pose against
|
||||
-- assimp's triangle soup (positions, texture coordinates) and the writer's exact weighted positions, the joints'
|
||||
-- model-space pose through the node tree, weights, animations through opts.files / opts.resolve (every frame's
|
||||
-- parent-relative pose), a standalone .md5anim, missing animations, errors, the fuzz pass.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Md5 = require("@formats/model/md5")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Soup = require("@tests/model/_soup")
|
||||
local Fx = require("@tests/gen/md5")
|
||||
|
||||
local t = T.suite("md5")
|
||||
t:eq(Md5.name, "md5", "name")
|
||||
|
||||
local function near(a: { number }, b: { number }, tol: number): boolean
|
||||
for i = 1, #b do
|
||||
if math.abs(a[i] - b[i]) > tol * math.max(1, math.abs(b[i])) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
local function sameQ(a: { number }, b: { number }): boolean
|
||||
return near(a, b, 1e-5) or near(a, { -b[1], -b[2], -b[3], -b[4] }, 1e-5)
|
||||
end
|
||||
|
||||
local M = Fx.mesh
|
||||
local files = {}
|
||||
for _, a in Fx.anims do
|
||||
files[a.name .. ".md5anim"] = a.data
|
||||
end
|
||||
local s, err = Md5.decode(Bytes.reader(buffer.fromstring(M.data)), { anims = { "idle.md5anim", "walk.md5anim" }, files = files })
|
||||
t:check(s, "md5mesh: " .. tostring(err))
|
||||
t:eq(F.detect(M.data), "md5", "F.detect")
|
||||
|
||||
-- 1. geometry
|
||||
do
|
||||
local prims = s.meshes[1].primitives
|
||||
t:eq(#prims, #M.positions, "a primitive per mesh")
|
||||
for i, p in prims do
|
||||
local pos = Soup.list(p.attributes.POSITION.data)
|
||||
t:check(near(pos, M.positions[i], 1e-5), "mesh " .. i .. ": weighted positions exact")
|
||||
t:eq(p.indices.count, M.tris[i] * 3, "mesh " .. i .. ": triangles")
|
||||
t:check(near(Soup.list(p.attributes.TEXCOORD_0.data), M.uvs[i], 1e-6), "mesh " .. i .. ": texture coordinates")
|
||||
t:eq(s.textures[s.materials[p.material].baseColorTexture.texture].uri, "models/test/skin" .. (i - 1), "mesh " .. i .. ": shader")
|
||||
end
|
||||
local matched = 0
|
||||
for mi, m in M.assimp.per do
|
||||
local nextStart = if M.assimp.per[mi + 1] then M.assimp.per[mi + 1].start else M.assimp.faces
|
||||
local nf = nextStart - m.start
|
||||
local apos = Soup.slice(M.assimp.pos, m.start * 9 + 1, nf * 9)
|
||||
local auv = Soup.slice(M.assimp.uv, m.start * 6 + 1, nf * 6)
|
||||
for _, p in prims do
|
||||
if p.indices.count == nf * 3 and Soup.triangles(Soup.list(Soup.positions(p)), apos, 1e-4) then
|
||||
t:check(Soup.triangles(Soup.uvs(p, 0, true), auv, 1e-5, 2), "texture coordinates against assimp")
|
||||
matched += 1
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(matched, #M.positions, "every mesh matches assimp's bind pose")
|
||||
end
|
||||
|
||||
-- 2. skeleton: the model-space joint poses through the node tree
|
||||
do
|
||||
local skin = s.skins[1]
|
||||
t:eq(#skin.joints, #M.joints, "joints")
|
||||
local parent = {}
|
||||
for ni, nd in s.nodes do
|
||||
for _, c in nd.children do
|
||||
parent[c] = ni
|
||||
end
|
||||
end
|
||||
local function world(ni: number): { number }
|
||||
local m = Types.Scene.nodeMatrix(s.nodes[ni])
|
||||
local p = parent[ni]
|
||||
return if p then Types.Scene.mul(world(p), m) else m
|
||||
end
|
||||
for i, j in M.joints do
|
||||
local nd = skin.joints[i]
|
||||
t:eq(s.nodes[nd].name, j.name, "joint name " .. i)
|
||||
local w = world(nd)
|
||||
t:check(near({ w[13], w[14], w[15] }, j.pos, 1e-4), "joint " .. j.name .. " model-space position")
|
||||
end
|
||||
t:check(s.meshes[1].primitives[1].attributes.JOINTS_0 ~= nil, "skinned")
|
||||
end
|
||||
|
||||
-- 3. animations
|
||||
local function checkAnim(scene: any, a: any, anim: any, label: string)
|
||||
t:eq(anim.name, a.name, label .. ": name")
|
||||
local byNode = {}
|
||||
for _, ch in anim.channels do
|
||||
byNode[ch.node .. ch.path] = ch
|
||||
end
|
||||
local skin = scene.skins[1]
|
||||
local ok = true
|
||||
for f, pose in a.poses do
|
||||
for ji, jp in pose do
|
||||
local nd = skin.joints[ji]
|
||||
local tr, rot = byNode[nd .. "translation"], byNode[nd .. "rotation"]
|
||||
local tv, qv = {}, {}
|
||||
for k = 1, 3 do
|
||||
tv[k] = buffer.readf32(tr.output.data, ((f - 1) * 3 + k - 1) * 4)
|
||||
end
|
||||
for k = 1, 4 do
|
||||
qv[k] = buffer.readf32(rot.output.data, ((f - 1) * 4 + k - 1) * 4)
|
||||
end
|
||||
ok = ok and near(tv, jp.t, 1e-5) and sameQ(qv, jp.q)
|
||||
end
|
||||
end
|
||||
t:check(ok, label .. ": every frame's pose")
|
||||
t:eq(anim.channels[1].input.count, a.frames, label .. ": keys")
|
||||
t:check(math.abs(buffer.readf32(anim.channels[1].input.data, 4) - 1 / a.rate) < 1e-6, label .. ": frame rate")
|
||||
end
|
||||
t:eq(#s.animations, 2, "two animations")
|
||||
for i, a in Fx.anims do
|
||||
checkAnim(s, a, s.animations[i], "mesh + " .. a.name)
|
||||
end
|
||||
do
|
||||
local asked = {}
|
||||
local r = Md5.decode(Bytes.reader(buffer.fromstring(M.data)), {
|
||||
anims = { "walk.md5anim" },
|
||||
resolve = function(name: string): string?
|
||||
table.insert(asked, name)
|
||||
return files[name]
|
||||
end,
|
||||
}) :: any
|
||||
t:check(r and #r.animations == 1 and asked[1] == "walk.md5anim", "opts.resolve")
|
||||
local _, e = Md5.decode(Bytes.reader(buffer.fromstring(M.data)), { anims = { "run.md5anim" } })
|
||||
t:check(e and e.code == Err.UNSUPPORTED, "missing animation")
|
||||
local skip = Md5.decode(Bytes.reader(buffer.fromstring(M.data)), { anims = { "run.md5anim" }, skipMissing = true }) :: any
|
||||
t:check(skip and #skip.animations == 0, "skipMissing")
|
||||
-- a standalone .md5anim
|
||||
local a = Fx.anims[2]
|
||||
local alone = Md5.decode(Bytes.reader(buffer.fromstring(a.data)), { name = "models/walk.md5anim" }) :: any
|
||||
t:check(alone and #alone.skins == 1, "md5anim alone: skeleton")
|
||||
checkAnim(alone, a, alone.animations[1], "md5anim alone")
|
||||
t:eq(alone.meta.source.kind, "anim", "md5anim alone: kind")
|
||||
end
|
||||
|
||||
-- 4. errors, fuzz
|
||||
do
|
||||
local _, e1 = Md5.decode(Bytes.reader(buffer.fromstring("MD5Version 11\n")))
|
||||
t:check(e1 and e1.code == Err.UNSUPPORTED, "version 11")
|
||||
local _, e2 = Md5.decode(Bytes.reader(buffer.fromstring(string.sub(M.data, 1, 900))))
|
||||
t:check(e2 ~= nil, "truncated: " .. tostring(e2))
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Md5.decode(Bytes.reader(b), { anims = { "idle.md5anim" }, files = files })
|
||||
end, { M.data, Fx.anims[1].data }, { mutations = 80 })
|
||||
t:done()
|
||||
@@ -0,0 +1,155 @@
|
||||
-- model/mdl tests: a Quake 1 and a Half-Life 1 model written by tools/fixtures/mdl.py. Quake: assimp's frame-0
|
||||
-- triangle soup (positions, texture coordinates), every frame exact, skins through opts.palette / opts.resolve /
|
||||
-- the grey ramp, frame-name animations. Half-Life: assimp's bind-pose soup per mesh (positions, texture
|
||||
-- coordinates), the bones' TRS, the skin, every sequence's animation values against the writer's, textures with
|
||||
-- their palettes, the skin family. Errors and the fuzz pass.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Mdl = require("@formats/model/mdl")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Soup = require("@tests/model/_soup")
|
||||
local Fx = require("@tests/gen/mdl")
|
||||
|
||||
local t = T.suite("mdl")
|
||||
t:eq(Mdl.name, "mdl", "name")
|
||||
|
||||
local function near(a: { number }, b: { number }, tol: number): boolean
|
||||
for i = 1, #b do
|
||||
if math.abs(a[i] - b[i]) > tol then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
-- assimp mesh mi of a soup -> positions, uvs
|
||||
local function assimpMesh(a: any, mi: number): ({ number }, { number }, number)
|
||||
local m = a.per[mi]
|
||||
local nextStart = if a.per[mi + 1] then a.per[mi + 1].start else a.faces
|
||||
local nf = nextStart - m.start
|
||||
return Soup.slice(a.pos, m.start * 9 + 1, nf * 9), Soup.slice(a.uv, m.start * 6 + 1, nf * 6), nf
|
||||
end
|
||||
|
||||
-- 1. Quake
|
||||
do
|
||||
local q = Fx.quake
|
||||
local s, err = Mdl.decode(Bytes.reader(buffer.fromstring(q.data)), { palette = q.palette })
|
||||
t:check(s, "quake: " .. tostring(err))
|
||||
t:eq(F.detect(q.data), "mdl", "quake: F.detect")
|
||||
local prim = s.meshes[1].primitives[1]
|
||||
local apos, auv = assimpMesh(q.assimp, 1)
|
||||
t:check(Soup.triangles(Soup.list(Soup.positions(prim)), apos, 1e-5), "quake: positions against assimp")
|
||||
t:check(Soup.triangles(Soup.uvs(prim, 0, true), auv, 1e-5, 2), "quake: texture coordinates against assimp")
|
||||
t:check(Soup.close(Soup.uvs(prim, 0), q.uv, 1e-6), "quake: texture coordinates (seam) against the writer")
|
||||
t:eq(#q.crcs, s.meta.source.frames, "quake: frames (groups unrolled)")
|
||||
for k, crc in q.crcs do
|
||||
local f = Mdl.decode(Bytes.reader(buffer.fromstring(q.data)), { frame = k, morph = false, palette = q.palette }) :: any
|
||||
t:eq(Crc32.of(Soup.positions(f.meshes[1].primitives[1])), crc, "quake: frame " .. k)
|
||||
end
|
||||
t:eq(#prim.targets, #q.crcs, "quake: morph targets")
|
||||
local groups = {}
|
||||
for _, a in s.animations do
|
||||
table.insert(groups, a.name)
|
||||
end
|
||||
t:eq(table.concat(groups, ","), "stand,walk,death", "quake: animation groups")
|
||||
t:eq(#s.textures, #q.skins, "quake: skins")
|
||||
for i, crc in q.skins do
|
||||
t:eq(Crc32.of(s.textures[i].image.pixels), crc, "quake: skin " .. i .. " through the palette")
|
||||
end
|
||||
local viaResolve = Mdl.decode(Bytes.reader(buffer.fromstring(q.data)), {
|
||||
resolve = function(uri: string): string?
|
||||
return if uri == "gfx/palette.lmp" then q.palette else nil
|
||||
end,
|
||||
}) :: any
|
||||
t:eq(Crc32.of(viaResolve.textures[1].image.pixels), q.skins[1], "quake: palette through opts.resolve")
|
||||
local grey = Mdl.decode(Bytes.reader(buffer.fromstring(q.data))) :: any
|
||||
t:eq(Crc32.of(grey.textures[1].image.pixels), q.grey, "quake: grey ramp without a palette")
|
||||
t:eq(grey.meta.source.palette, false, "quake: meta.source.palette")
|
||||
end
|
||||
|
||||
-- 2. Half-Life
|
||||
do
|
||||
local h = Fx.halflife
|
||||
local s, err = Mdl.decode(Bytes.reader(buffer.fromstring(h.data)))
|
||||
t:check(s, "halflife: " .. tostring(err))
|
||||
t:eq(F.detect(h.data), "mdl", "halflife: F.detect")
|
||||
local mesh = s.meshes[1]
|
||||
t:eq(#mesh.primitives, #h.tris, "halflife: a primitive per mesh")
|
||||
t:eq(h.assimp.meshes, #h.tris, "halflife: assimp meshes")
|
||||
local matched = 0
|
||||
for mi = 1, h.assimp.meshes do
|
||||
local apos, auv, nf = assimpMesh(h.assimp, mi)
|
||||
for _, p in mesh.primitives do
|
||||
if p.indices.count == nf * 3 and Soup.triangles(Soup.list(Soup.positions(p)), apos, 1e-4) then
|
||||
-- assimp's Half-Life loader stores v = -t / height
|
||||
t:check(Soup.triangles(Soup.uvs(p, 0, "neg"), auv, 1e-5, 2), "halflife: texture coordinates against assimp")
|
||||
matched += 1
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(matched, #h.tris, "halflife: every mesh matches assimp's bind-pose positions")
|
||||
for i, p in mesh.primitives do
|
||||
t:eq(p.indices.count, h.tris[i] * 3, "halflife: mesh " .. i .. " triangles")
|
||||
t:check(p.attributes.JOINTS_0 and p.attributes.WEIGHTS_0, "halflife: skinned")
|
||||
end
|
||||
-- bones
|
||||
local skin = s.skins[1]
|
||||
t:eq(#skin.joints, #h.bones, "halflife: joints")
|
||||
for i, bn in h.bones do
|
||||
local node = s.nodes[skin.joints[i]]
|
||||
t:eq(node.name, bn.name, "halflife: bone name " .. i)
|
||||
t:check(near(node.translation, bn.t, 1e-6) and near(node.rotation, bn.q, 1e-6), "halflife: bone " .. bn.name .. " TRS")
|
||||
end
|
||||
-- animations
|
||||
t:eq(#s.animations, #h.sequences, "halflife: sequences")
|
||||
for si, sq in h.sequences do
|
||||
local a = s.animations[si]
|
||||
t:eq(a.name, sq.name, "halflife: sequence name")
|
||||
local byNode = {}
|
||||
for _, ch in a.channels do
|
||||
byNode[ch.node .. ch.path] = ch
|
||||
end
|
||||
local ok = true
|
||||
for bi, frames in sq.bones do
|
||||
local node = skin.joints[bi]
|
||||
local tch, rch = byNode[node .. "translation"], byNode[node .. "rotation"]
|
||||
for f, fr in frames do
|
||||
for k = 1, 3 do
|
||||
ok = ok and math.abs(buffer.readf32(tch.output.data, ((f - 1) * 3 + k - 1) * 4) - fr.t[k]) < 1e-4
|
||||
end
|
||||
for k = 1, 4 do
|
||||
ok = ok and math.abs(buffer.readf32(rch.output.data, ((f - 1) * 4 + k - 1) * 4) - fr.q[k]) < 1e-5
|
||||
end
|
||||
end
|
||||
t:eq(tch.input.count, sq.frames, "halflife: " .. sq.name .. " keys")
|
||||
t:check(math.abs(buffer.readf32(tch.input.data, 4) - 1 / sq.fps) < 1e-6, "halflife: " .. sq.name .. " fps")
|
||||
end
|
||||
t:check(ok, "halflife: " .. sq.name .. ": every value against the writer")
|
||||
end
|
||||
-- textures, skin family 0
|
||||
for i, tx in h.textures do
|
||||
t:eq(s.textures[i].name, tx.name, "halflife: texture name")
|
||||
t:eq(Crc32.of(s.textures[i].image.pixels), tx.crc, "halflife: texture " .. i .. " through its palette")
|
||||
end
|
||||
t:eq(mesh.primitives[1].material, 1, "halflife: mesh 1 -> skin ref 0 -> texture 1")
|
||||
t:eq(mesh.primitives[2].material, 2, "halflife: mesh 2 -> skin ref 1 -> texture 2")
|
||||
end
|
||||
|
||||
-- 3. errors, fuzz
|
||||
do
|
||||
local _, e1 = Mdl.decode(Bytes.reader(buffer.fromstring(string.sub(Fx.quake.data, 1, 200))))
|
||||
t:check(e1 and (e1.code == Err.TRUNCATED or e1.code == Err.CORRUPT), "quake truncated: " .. tostring(e1))
|
||||
local _, e2 = Mdl.decode(Bytes.reader(buffer.fromstring(string.sub(Fx.halflife.data, 1, 300))))
|
||||
t:check(e2 ~= nil, "halflife truncated: " .. tostring(e2))
|
||||
local src = buffer.fromstring(Fx.halflife.data)
|
||||
buffer.writei32(src, 4, 48)
|
||||
local _, e3 = Mdl.decode(Bytes.reader(src))
|
||||
t:check(e3 and e3.code == Err.UNSUPPORTED, "Source engine MDL")
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Mdl.decode(Bytes.reader(b), { palette = Fx.quake.palette })
|
||||
end, { Fx.quake.data, Fx.halflife.data }, { mutations = 200 })
|
||||
t:done()
|
||||
@@ -0,0 +1,176 @@
|
||||
-- model/smd tests: SMD files written by tools/fixtures/smd.py decode to assimp's triangle soup (positions, texture
|
||||
-- coordinates per material) and to the writer's exact values (bind pose TRS, per-vertex weights and joints, every
|
||||
-- skeleton frame of the animation); an animation-only SMD; our writer's output re-read by us (same soup, weights,
|
||||
-- poses) and by assimp (faces, bones); errors; the fuzz pass. Fixtures: tools/fixtures/smd.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Smd = require("@formats/model/smd")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Soup = require("@tests/model/_soup")
|
||||
local Fx = require("@tests/gen/smd")
|
||||
|
||||
local t = T.suite("smd")
|
||||
t:eq(Smd.name, "smd", "name")
|
||||
|
||||
local function near(a: { number }, b: { number }, tol: number): boolean
|
||||
for i = 1, #b do
|
||||
if math.abs(a[i] - b[i]) > tol then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
-- same rotation (q or -q)
|
||||
local function sameQ(a: { number }, b: { number }): boolean
|
||||
return near(a, b, 1e-5) or near(a, { -b[1], -b[2], -b[3], -b[4] }, 1e-5)
|
||||
end
|
||||
|
||||
local function check(s: any, c: any, label: string)
|
||||
local skin = s.skins[1]
|
||||
t:eq(#skin.joints, #c.bones, label .. ": joints")
|
||||
for i, bn in c.bones do
|
||||
local node = s.nodes[skin.joints[i]]
|
||||
t:eq(node.name, bn.name, label .. ": bone name " .. i)
|
||||
local pose = c.frames[1][i]
|
||||
t:check(near(node.translation, pose.t, 1e-5) and sameQ(node.rotation, pose.q), label .. ": bind pose " .. bn.name)
|
||||
end
|
||||
if #c.tris > 0 then
|
||||
local prims = s.meshes[1].primitives
|
||||
t:eq(#prims, #c.materials, label .. ": a primitive per material")
|
||||
-- every triangle of the writer, in file order per material
|
||||
for pi, p in prims do
|
||||
local mat = s.materials[p.material].name
|
||||
local pos = Types.Scene.reader(p.attributes.POSITION) :: any
|
||||
local uv = Types.Scene.reader(p.attributes.TEXCOORD_0) :: any
|
||||
local jr = Types.Scene.reader(p.attributes.JOINTS_0) :: any
|
||||
local wr = Types.Scene.reader(p.attributes.WEIGHTS_0) :: any
|
||||
local k = 0
|
||||
local ok, wok = true, true
|
||||
for _, tri in c.tris do
|
||||
if tri.material == mat then
|
||||
for ci, cn in tri.corners do
|
||||
local v = buffer.readu32(p.indices.data, (k * 3 + ci - 1) * 4)
|
||||
ok = ok and near({ pos(v, 0), pos(v, 1), pos(v, 2) }, cn.p, 1e-5) and near({ uv(v, 0), 1 - uv(v, 1) }, cn.uv, 1e-6)
|
||||
local got = {}
|
||||
for wi = 0, 3 do
|
||||
if wr(v, wi) > 0 then
|
||||
got[jr(v, wi)] = wr(v, wi)
|
||||
end
|
||||
end
|
||||
for _, w in cn.w do
|
||||
wok = wok and got[w[1]] ~= nil and math.abs(got[w[1]] - w[2]) < 1e-5
|
||||
end
|
||||
end
|
||||
k += 1
|
||||
end
|
||||
end
|
||||
t:check(ok, label .. ": " .. mat .. " positions / texture coordinates")
|
||||
t:check(wok, label .. ": " .. mat .. " joints / weights")
|
||||
t:eq(p.indices.count, k * 3, label .. ": " .. mat .. " triangles")
|
||||
local _ = pi
|
||||
end
|
||||
end
|
||||
if #c.frames > 1 then
|
||||
local a = s.animations[1]
|
||||
local byNode = {}
|
||||
for _, ch in a.channels do
|
||||
byNode[ch.node .. ch.path] = ch
|
||||
end
|
||||
local ok = true
|
||||
for f, pose in c.frames do
|
||||
for i, bp in pose do
|
||||
local node = skin.joints[i]
|
||||
local tr, rot = byNode[node .. "translation"], byNode[node .. "rotation"]
|
||||
local tv, qv = {}, {}
|
||||
for k = 1, 3 do
|
||||
tv[k] = buffer.readf32(tr.output.data, ((f - 1) * 3 + k - 1) * 4)
|
||||
end
|
||||
for k = 1, 4 do
|
||||
qv[k] = buffer.readf32(rot.output.data, ((f - 1) * 4 + k - 1) * 4)
|
||||
end
|
||||
ok = ok and near(tv, bp.t, 1e-5) and sameQ(qv, bp.q)
|
||||
end
|
||||
end
|
||||
t:check(ok, label .. ": every skeleton frame")
|
||||
t:eq(a.channels[1].input.count, #c.frames, label .. ": keys")
|
||||
end
|
||||
end
|
||||
|
||||
for _, c in Fx.cases do
|
||||
local s, err = Smd.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
t:check(s, c.name .. ": " .. tostring(err))
|
||||
t:eq(F.detect(c.data), "smd", c.name .. ": F.detect")
|
||||
check(s, c, c.name)
|
||||
if not c.assimp.failed then
|
||||
local matched = 0
|
||||
for mi, m in c.assimp.per do
|
||||
local nextStart = if c.assimp.per[mi + 1] then c.assimp.per[mi + 1].start else c.assimp.faces
|
||||
local nf = nextStart - m.start
|
||||
local apos = Soup.slice(c.assimp.pos, m.start * 9 + 1, nf * 9)
|
||||
local auv = Soup.slice(c.assimp.uv, m.start * 6 + 1, nf * 6)
|
||||
for _, p in s.meshes[1].primitives do
|
||||
if p.indices.count == nf * 3 and Soup.triangles(Soup.list(Soup.positions(p)), apos, 1e-5) then
|
||||
t:check(Soup.triangles(Soup.uvs(p, 0, true), auv, 1e-5, 2), c.name .. ": texture coordinates against assimp")
|
||||
matched += 1
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(matched, #c.materials, c.name .. ": every material mesh matches assimp's positions")
|
||||
end
|
||||
-- our writer, read back by us
|
||||
local out = Smd.encode(s) :: buffer
|
||||
local back, berr = Smd.decode(Bytes.reader(out))
|
||||
t:check(back, c.name .. ": re-read our SMD: " .. tostring(berr))
|
||||
local again = table.clone(c)
|
||||
again.frames = { c.frames[1] }
|
||||
check(back, again, c.name .. " (written)")
|
||||
if #c.frames > 1 then
|
||||
local anim = Smd.decode(Bytes.reader(Smd.encode(s, { animation = 1 }) :: buffer)) :: any
|
||||
local only = table.clone(c)
|
||||
only.tris = {}
|
||||
check(anim, only, c.name .. " (written animation)")
|
||||
end
|
||||
-- assimp reads our output
|
||||
local ours = Fx.ours[c.name]
|
||||
if ours and #c.tris > 0 then
|
||||
t:eq(ours.faces, #c.tris, c.name .. ": assimp reads our triangles")
|
||||
local soupOk = ours.soup and not ours.soup.failed and ours.soup.faces == #c.tris
|
||||
t:check(soupOk, c.name .. ": assimp soup of our file")
|
||||
end
|
||||
end
|
||||
|
||||
-- a scene without a skin writes one root bone
|
||||
do
|
||||
local prim: any = {
|
||||
attributes = { POSITION = Types.Scene.accessor(buffer.create(36), "f32", 3, 3) },
|
||||
mode = "triangles",
|
||||
}
|
||||
buffer.writef32(prim.attributes.POSITION.data, 12, 1)
|
||||
buffer.writef32(prim.attributes.POSITION.data, 28, 1)
|
||||
local text = buffer.tostring(Smd.encode(Types.Scene.fromPrimitive(prim)) :: buffer)
|
||||
t:check(string.find(text, '0 "root" -1', 1, true) ~= nil, "static scene: a root bone")
|
||||
local back = Smd.decode(Bytes.reader(buffer.fromstring(text))) :: any
|
||||
t:eq(back.meshes[1].primitives[1].indices.count, 3, "static scene round trip")
|
||||
local _, e1 = Smd.encode({ kind = "image" } :: any)
|
||||
t:check(e1 and e1.code == Err.ARG, "encode a non-scene")
|
||||
local _, e2 = Smd.decode(Bytes.reader(buffer.fromstring("version 1\nnodes\n0 \"a\" -1\n")))
|
||||
t:check(e2 and e2.code == Err.TRUNCATED, "nodes without end: " .. tostring(e2))
|
||||
local _, e3 = Smd.decode(Bytes.reader(buffer.fromstring("version 2\nnodes\nend\n")))
|
||||
t:check(e3 and e3.code == Err.UNSUPPORTED, "version 2")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx.cases do
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
local s = Smd.decode(Bytes.reader(b))
|
||||
if s then
|
||||
Smd.encode(s)
|
||||
end
|
||||
return s
|
||||
end, samples, { mutations = 60 })
|
||||
t:done()
|
||||
Reference in new issue
Block a user