ws-cleanup-types: image/pnm, model/*: type lookup tables, table.create results and literals; casts for known values

This commit is contained in:
4DBug committed 2026-10-31 10:59:11 -05:00
1 parent 7c15b02354
commit 139441a37b
23 files changed
+139 -137

No files matched your search

+6 -6
View File
@@ -63,17 +63,17 @@ function Ascii.parse(text: string, opts: { maxBytes: number? }?): (Node?, any, n
-- the values of one node (after the ':'), up to a new name, '{' or '}'
local function values(): ({ any }?, string?)
local props = {}
local props: { any } = {}
while true do
skip()
if i > n then
return props
return props, nil
end
local c = sbyte(text, i)
if c == 44 then -- ,
i += 1
elseif c == 123 or c == 125 then
return props
return props, nil
elseif c == 34 then -- "
local e = sfind(text, "\"", i + 1, true)
if not e then
@@ -117,7 +117,7 @@ function Ascii.parse(text: string, opts: { maxBytes: number? }?): (Node?, any, n
else
local nm = nameAt()
if nm then
return props
return props, nil
end
local s, e = sfind(text, "^[%w_]+", i)
if not s then
@@ -230,7 +230,7 @@ function Ascii.write(root: any, version: number?): string
table.insert(parts, string.sub(v, 2))
else
local nm, cls = string.match(v, "^(.-)%z\1(.*)$")
local sv = if nm then cls .. "::" .. nm else v
local sv = if nm then (cls :: string) .. "::" .. nm else v
table.insert(parts, "\"" .. string.gsub(sv, "\"", """) .. "\"")
end
elseif tv == "table" and v.array then
@@ -246,7 +246,7 @@ function Ascii.write(root: any, version: number?): string
end
end
if arr then
local vals = table.create(arr.count)
local vals = table.create(arr.count) :: { string }
for k = 0, arr.count - 1 do
vals[k + 1] = fmt(buffer.readf64(arr.data, k * 8))
end
+2 -2
View File
@@ -38,7 +38,7 @@ local function i64(b: buffer, p: number, asString: boolean): any
end
Binary.i64 = i64
local ELEM = { f = 4, d = 8, l = 8, i = 4, b = 1, c = 1 }
local ELEM: { [string]: number } = { f = 4, d = 8, l = 8, i = 4, b = 1, c = 1 }
-- the elements of a raw array (n elements of type t at p) as f64s
local function toF64(t: string, src: buffer, p: number, n: number): buffer
@@ -170,7 +170,7 @@ function Binary.parse(b: buffer, base: number, limit: number, opts: { maxBytes:
plen = readu32(b, p + 16) + readu32(b, p + 20) * 4294967296
nlen = readu8(b, p + 24)
else
endOff, nprops, plen, nlen = readu32(b, p), readu32(b, p + 4), readu32(b, p + 8), readu8(b, p + 12)
endOff, nprops, plen, nlen = readu32(b, p), readu32(b, p + 4), readu32(b, p + 8), (readu8(b, p + 12))
end
if endOff == 0 then
return false, p + head
+6 -6
View File
@@ -1058,7 +1058,7 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
local near, far = pnum(p, "NearPlane", 10), pnum(p, "FarPlane", 4000)
if ortho then
local zoom = pnum(p, "OrthoZoom", 1)
cam = { name = a.name ~= "" and a.name or o.name, type = "orthographic", xmag = zoom * aspect, ymag = zoom, znear = near, zfar = far }
cam = { name = a.name ~= "" and a.name or o.name, type = "orthographic", xmag = zoom * aspect, ymag = zoom, znear = near, zfar = far } :: Types.Camera
else
local focal = pnum(p, "FocalLength", 0)
local hfov
@@ -1068,7 +1068,7 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
hfov = pnum(p, "FieldOfView", 40) * math.pi / 180
end
local yfov = 2 * math.atan(math.tan(hfov / 2) / aspect)
cam = { name = a.name ~= "" and a.name or o.name, type = "perspective", yfov = yfov, aspect = aspect, znear = near, zfar = far }
cam = { name = a.name ~= "" and a.name or o.name, type = "perspective", yfov = yfov, aspect = aspect, znear = near, zfar = far } :: Types.Camera
end
table.insert(scene.cameras, cam)
table.insert(scene.nodes, { name = o.name .. "_camera", children = {}, camera = #scene.cameras, rotation = { 0, -orient, 0, orient }, extras = { fbx = { orientation = "camera" } } })
@@ -1317,13 +1317,13 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
return false
end
if tr.curves.T or varies(T, 3, nd.translation or { 0, 0, 0 }) then
table.insert(anim.channels, { node = ni, path = "translation", input = input, output = f32acc(T, 3), interpolation = interp })
table.insert(anim.channels, { node = ni, path = "translation", input = input, output = f32acc(T, 3), interpolation = interp } :: Types.Channel)
end
if tr.curves.R or varies(R, 4, nd.rotation or { 0, 0, 0, 1 }) then
table.insert(anim.channels, { node = ni, path = "rotation", input = input, output = f32acc(R, 4), interpolation = interp })
table.insert(anim.channels, { node = ni, path = "rotation", input = input, output = f32acc(R, 4), interpolation = interp } :: Types.Channel)
end
if tr.curves.S or varies(S, 3, nd.scale or { 1, 1, 1 }) then
table.insert(anim.channels, { node = ni, path = "scale", input = input, output = f32acc(S, 3), interpolation = interp })
table.insert(anim.channels, { node = ni, path = "scale", input = input, output = f32acc(S, 3), interpolation = interp } :: Types.Channel)
end
end
end
@@ -1348,7 +1348,7 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
end
end
local acc = f32acc(out, 1)
table.insert(anim.channels, { node = ni, path = "weights", input = f32acc(times, 1), output = acc, interpolation = if allConstant(cs) then "STEP" else "LINEAR" })
table.insert(anim.channels, { node = ni, path = "weights", input = f32acc(times, 1), output = acc, interpolation = if allConstant(cs) then "STEP" else "LINEAR" } :: Types.Channel)
end
end
end
+12 -12
View File
@@ -91,7 +91,7 @@ local function faces(prim: Types.Primitive): { { number } }?
if p + sz > count then
break
end
local poly = table.create(sz)
local poly = table.create(sz) :: { number }
for q = 0, sz - 1 do
poly[q + 1] = I(p + q)
end
@@ -148,9 +148,9 @@ function Write.tree(scene: Types.Scene, opts: any?): (WNode?, Err.Err?)
local nodes = scene.nodes or {}
-- ------------------------------------------------------------------------------------------ textures
local EXT = { ["image/png"] = "png", ["image/jpeg"] = "jpg", ["image/bmp"] = "bmp", ["image/webp"] = "webp", ["image/x-tga"] = "tga", ["image/vnd-ms.dds"] = "dds", ["image/tiff"] = "tif" }
local EXT: { [string]: string } = { ["image/png"] = "png", ["image/jpeg"] = "jpg", ["image/bmp"] = "bmp", ["image/webp"] = "webp", ["image/x-tga"] = "tga", ["image/vnd-ms.dds"] = "dds", ["image/tiff"] = "tif" }
local texIds: { [number]: number } = {}
local base = opts and opts.name and string.gsub(string.match(opts.name, "([^/\\]+)$") or opts.name, "%.[^.]*$", "") or "model"
local base: string = opts and opts.name and string.gsub(string.match(opts.name, "([^/\\]+)$") or opts.name, "%.[^.]*$", "") or "model"
for ti, tex in scene.textures or {} do
local file = tex.uri or (base .. "_" .. (tex.name or tostring(ti)) .. "." .. (EXT[tex.mime or ""] or "png"))
local tname = tex.name or ("Texture" .. ti)
@@ -295,14 +295,14 @@ function Write.tree(scene: Types.Scene, opts: any?): (WNode?, Err.Err?)
end
visiting[i] = true
local nd = nodes[i]
local l = if nd.matrix and #nd.matrix == 16 then nd.matrix else Xform.compose(nd.translation, nd.rotation, nd.scale)
local l = if nd.matrix and #(nd.matrix :: Xform.M4) == 16 then nd.matrix else Xform.compose(nd.translation, nd.rotation, nd.scale)
local p = parentOf[i]
local w = if p then Xform.mul(world(p), l) else l
worldM[i] = w
return w
end
local function trs(nd: Types.Node): ({ number }, { number }, { number })
if nd.matrix and #nd.matrix == 16 then
if nd.matrix and #(nd.matrix :: Xform.M4) == 16 then
return Xform.decompose(nd.matrix)
end
return nd.translation or { 0, 0, 0 }, nd.rotation or { 0, 0, 0, 1 }, nd.scale or { 1, 1, 1 }
@@ -505,7 +505,7 @@ function Write.tree(scene: Types.Scene, opts: any?): (WNode?, Err.Err?)
for v = 0, n - 1 do
local u, w = 0, 0
if T then
u, w = T(v, 0), T(v, 1)
u, w = T(v, 0), (T(v, 1))
end
table.insert(UV[k], u)
table.insert(UV[k], if flipV then 1 - w else w)
@@ -647,8 +647,8 @@ function Write.tree(scene: Types.Scene, opts: any?): (WNode?, Err.Err?)
end
for _, l in layout do
local tg = l.prim.targets and (l.prim.targets :: any)[t]
local TP = tg and accessorReader(tg.POSITION)
local TN = tg and accessorReader(tg.NORMAL)
local TP: ((i: number, c: number) -> number)? = tg and accessorReader(tg.POSITION)
local TN: ((i: number, c: number) -> number)? = tg and accessorReader(tg.NORMAL)
if TP then
for v = 0, l.prim.attributes.POSITION.count - 1 do
local x, y, z = TP(v, 0), TP(v, 1), TP(v, 2)
@@ -822,7 +822,7 @@ function Write.tree(scene: Types.Scene, opts: any?): (WNode?, Err.Err?)
local PATH = { translation = { "T", "Lcl Translation", 3 }, rotation = { "R", "Lcl Rotation", 4 }, scale = { "S", "Lcl Scaling", 3 } }
for ai, anim in scene.animations or {} do
local stid, lid = id(), id()
local astart, astop = math.huge, -math.huge
local astart: number, astop: number = math.huge, -math.huge
local layerNodes = {}
for _, ch in anim.channels do
local mid = modelIds[ch.node]
@@ -834,7 +834,7 @@ function Write.tree(scene: Types.Scene, opts: any?): (WNode?, Err.Err?)
local times = table.create(n, 0)
for k = 0, n - 1 do
times[k + 1] = IN(k, 0)
astart, astop = math.min(astart, times[k + 1]), math.max(astop, times[k + 1])
astart, astop = math.min(astart, times[k + 1]), (math.max(astop, times[k + 1]))
end
local cubic = ch.interpolation == "CUBICSPLINE"
local step = ch.interpolation == "STEP"
@@ -919,8 +919,8 @@ function Write.tree(scene: Types.Scene, opts: any?): (WNode?, Err.Err?)
end
-- ------------------------------------------------------------------------------------------ document
local meta = scene.meta or {}
local src = type(meta.source) == "table" and meta.source or {}
local meta: { [string]: any } = scene.meta or {}
local src: { [string]: any } = type(meta.source) == "table" and meta.source or {}
local up = if meta.up == "z" then 2 else 1
local upSign, front, frontSign, coord, coordSign = 1, 2, 1, 0, 1
if up == 2 then
+1 -1
View File
@@ -148,7 +148,7 @@ function Fbx.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
if not scene then
return nil, err
end
scene.meta.source.binary = binary
(scene.meta.source :: { [string]: any }).binary = binary
return scene, nil
end