model/blend: Blender 1.6x-2.0x files (big-endian, 32-bit, TFace / MCol, IPO curves, additive dsize), easing curves, owner-scoped address lookup; containers/Blend tests; docs section; bench line

This commit is contained in:
4DBug committed 2026-11-01 19:23:28 -06:00
1 parent 2e82b7d0af
commit 3c8d5e99d7
14 files changed
+766 -110

No files matched your search

+31 -24
View File
@@ -184,7 +184,7 @@ local function parseDna(f: File, at: number, size: number): string?
if n < 0 or n > e - p then
return nil, p
end
local out = table.create(n)
local out = table.create(n) :: { string }
for k = 1, n do
local q = p
while q < e and readu8(b, q) ~= 0 do
@@ -215,7 +215,7 @@ local function parseDna(f: File, at: number, size: number): string?
if p + 4 + nt * 2 > e or readstring(b, p, 4) ~= "TLEN" then
return "bad SDNA TLEN"
end
local lengths = table.create(nt)
local lengths = table.create(nt) :: { number }
for k = 1, nt do
lengths[k] = f.u16(b, p + 4 + (k - 1) * 2)
end
@@ -230,7 +230,7 @@ local function parseDna(f: File, at: number, size: number): string?
end
local i16 = f.i16
local ps = f.pointerSize
local structs, byName = table.create(ns), {}
local structs, byName = table.create(ns) :: { Struct }, {} :: { [string]: Struct }
for k = 1, ns do
if p + 4 > e then
return "truncated SDNA structs"
@@ -240,7 +240,8 @@ local function parseDna(f: File, at: number, size: number): string?
if t < 0 or t >= nt or nf < 0 or p + nf * 4 > e then
return "bad SDNA struct " .. k
end
local s: Struct = { name = types[t + 1], index = k - 1, size = lengths[t + 1], fields = table.create(nf), byName = {} }
local tnames = types :: { string }
local s: Struct = { name = tnames[t + 1], index = k - 1, size = lengths[t + 1], fields = table.create(nf) :: { Field }, byName = {} }
local off = 0
for j = 1, nf do
local ft, fn = i16(b, p), i16(b, p + 2)
@@ -248,9 +249,9 @@ local function parseDna(f: File, at: number, size: number): string?
if ft < 0 or ft >= nt or fn < 0 or fn >= #names then
return "bad SDNA field in " .. s.name
end
local decl = names[fn + 1]
local decl: string = (names :: { string })[fn + 1]
local pointer = string.sub(decl, 1, 1) == "*" or string.sub(decl, 1, 2) == "(*"
local dims, count = {}, 1
local dims: { number }, count = {}, 1
for d in string.gmatch(decl, "%[(%d+)%]") do
local v = tonumber(d) or 1
table.insert(dims, v)
@@ -258,9 +259,9 @@ local function parseDna(f: File, at: number, size: number): string?
end
local base = string.gsub(decl, "%[.*$", "")
base = string.gsub(base, "[%*%(%)]", "")
local elem = if pointer then ps else lengths[ft + 1]
local elem: number = if pointer then ps else lengths[ft + 1]
local fld: Field = {
name = base, decl = decl, type = types[ft + 1], offset = off, size = elem * count,
name = base, decl = decl, type = tnames[ft + 1], offset = off, size = elem * count,
pointer = pointer, dims = dims, count = count, elem = elem,
}
s.fields[j] = fld
@@ -272,7 +273,7 @@ local function parseDna(f: File, at: number, size: number): string?
structs[k] = s
byName[s.name] = s
end
f.dna = { names = names, types = types, lengths = lengths, structs = structs, byName = byName }
f.dna = { names = names :: { string }, types = types :: { string }, lengths = lengths, structs = structs, byName = byName }
return nil
end
@@ -298,7 +299,8 @@ function Blend.open(data: buffer, offset: number?, length: number?, opts: Option
end
local hs, ps, little, version, fmt, herr = header(b, n)
if not hs then
return nil, Err.new(if n < 12 then Err.TRUNCATED else Err.CORRUPT, herr or "bad header", 0)
local code = if n < 12 then Err.TRUNCATED elseif herr and string.sub(herr, 1, 12) == "unsupported:" then Err.UNSUPPORTED else Err.CORRUPT
return nil, Err.new(code, herr or "bad header", 0)
end
local f: File = {
buf = b, version = version, subversion = nil, pointerSize = ps, little = little, format = fmt,
@@ -341,7 +343,8 @@ function Blend.open(data: buffer, offset: number?, length: number?, opts: Option
count = i32(b, p + 12 + ps)
end
p += bh
code = string.gsub(code, "%z+$", "")
-- two-letter ID codes are a 16-bit value in a 32-bit field: "\0\0SC" in big-endian files
code = string.gsub(string.gsub(code, "%z+$", ""), "^%z+", "")
if code == "ENDB" then
break
end
@@ -389,29 +392,33 @@ function Blend.open(data: buffer, offset: number?, length: number?, opts: Option
f.byAddr[if ps == 8 then string.rep("\0", 8) else 0] = nil
local glob = f.byCode.GLOB
local fg = f.dna.byName.FileGlobal
if glob and fg and fg.byName.subversion then
local fld = fg.byName.subversion
if fld.offset + 2 <= glob[1].size then
f.subversion = f.i16(b, glob[1].offset + fld.offset)
local g1: Block? = if glob then glob[1] else nil
local fld = fg and fg.byName.subversion
if g1 and fld then
if fld.offset + 2 <= g1.size then
f.subversion = f.i16(b, g1.offset + fld.offset)
end
end
return f, nil
end
-- ------------------------------------------------------------------------------------------------ structs
export type View = {
file: File,
struct: Struct,
offset: number, -- the struct's first byte in file.buf
block: Block?,
}
local View = {}
View.__index = View
Blend.View = View
export type View = typeof(setmetatable(
{} :: {
file: File,
struct: Struct,
offset: number, -- the struct's first byte in file.buf
block: Block?,
},
View
))
local function view(f: File, s: Struct, offset: number, blk: Block?): View
return setmetatable({ file = f, struct = s, offset = offset, block = blk }, View) :: any
return setmetatable({ file = f, struct = s, offset = offset, block = blk }, View)
end
function Blend.structOf(f: File, blk: Block): Struct?
@@ -641,7 +648,7 @@ function View.array(self: View, name: string | { string }, as: string?): { View
local blk = first.block :: Block
local s = first.struct
local n = if s.size > 0 then blk.size // s.size else 0
local out = table.create(n)
local out = table.create(n) :: { View }
for k = 0, n - 1 do
out[k + 1] = view(self.file, s, blk.offset + k * s.size, blk)
end