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

+8 -7
View File
@@ -192,6 +192,7 @@ function Obj.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
if key == nil or string.sub(key, 1, 1) == "#" then
continue
end
local rest: string = rest :: string -- both captures match together
local hash = string.find(rest, "#", 1, true)
if hash then
rest = string.gsub(string.sub(rest, 1, hash - 1), "%s+$", "")
@@ -513,14 +514,14 @@ local function nameOk(s: string): string
return t
end
local EXT = { ["image/png"] = "png", ["image/jpeg"] = "jpg", ["image/webp"] = "webp", ["image/gif"] = "gif", ["image/bmp"] = "bmp", ["image/ktx2"] = "ktx2" }
local EXT: { [string]: string } = { ["image/png"] = "png", ["image/jpeg"] = "jpg", ["image/webp"] = "webp", ["image/gif"] = "gif", ["image/bmp"] = "bmp", ["image/ktx2"] = "ktx2" }
function Obj.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err?)
if type(scene) ~= "table" or scene.kind ~= "scene" or type(scene.nodes) ~= "table" or type(scene.meshes) ~= "table" then
return nil, Err.new(Err.ARG, "needs a Scene")
end
local flipV = not (opts and opts.flipV == false)
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"
local mtlName = opts and opts.mtlName or (base .. ".mtl")
local files = opts and opts.files
local placed = Types.Scene.flatten(scene)
@@ -586,9 +587,9 @@ function Obj.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
table.insert(out, "o " .. nameOk(node.name or ("node_" .. pl.node)))
end
local n = pos.count
local Cc = if attrs.COLOR_0 and not (opts and opts.colors == false) then Types.Scene.reader(attrs.COLOR_0) else nil
local T = if attrs.TEXCOORD_0 and not (opts and opts.texcoords == false) then Types.Scene.reader(attrs.TEXCOORD_0) else nil
local Nr = if attrs.NORMAL and not (opts and opts.normals == false) then Types.Scene.reader(attrs.NORMAL) else nil
local Cc = if attrs.COLOR_0 and not (opts and opts.colors == false) then Types.Scene.reader(attrs.COLOR_0 :: Types.Accessor) else nil
local T = if attrs.TEXCOORD_0 and not (opts and opts.texcoords == false) then Types.Scene.reader(attrs.TEXCOORD_0 :: Types.Accessor) else nil
local Nr = if attrs.NORMAL and not (opts and opts.normals == false) then Types.Scene.reader(attrs.NORMAL :: Types.Accessor) else nil
for _, a in { attrs.COLOR_0, attrs.TEXCOORD_0, attrs.NORMAL } do
if a and (a.count < n or buffer.len(a.data) < a.count * a.components * (Types.COMPONENT_SIZE[a.type] or 4)) then
return nil, Err.new(Err.ARG, "an attribute is shorter than POSITION")
@@ -692,7 +693,7 @@ function Obj.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
if o + k > count then
return nil, Err.new(Err.ARG, "faces run past the indices")
end
local list = table.create(k)
local list = table.create(k) :: { number }
for j = 0, k - 1 do
list[j + 1] = at(o + j)
end
@@ -747,7 +748,7 @@ function Obj.encodeMtl(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.
if type(scene) ~= "table" or type(scene.materials) ~= "table" then
return nil, Err.new(Err.ARG, "needs a Scene")
end
local files = {}
local files: { [string]: string } = {}
local o: any = table.clone(opts or {})
o.files = files
o.mtlName = "x.mtl"