269 lines
7.3 KiB
Luau
269 lines
7.3 KiB
Luau
--!strict
|
|
-- model/fbx/Ascii: the ASCII FBX 7.x node tree ("Name: value, ... { children }", ';' comments, "*N { a: ... }" arrays,
|
|
-- " in strings); integers longer than 15 digits stay strings so ids are exact.
|
|
local Base64 = require("@rosetta/core/Base64")
|
|
|
|
local Ascii = {}
|
|
|
|
type Node = { name: string, props: { any }, children: { Node } }
|
|
|
|
local sbyte, sfind, ssub = string.byte, string.find, string.sub
|
|
local MAX_DEPTH = 128
|
|
|
|
local function isNameStart(c: number): boolean
|
|
return (c >= 65 and c <= 90) or (c >= 97 and c <= 122) or c == 95
|
|
end
|
|
|
|
function Ascii.parse(text: string, opts: { maxBytes: number? }?): (Node?, any, number?)
|
|
local maxBytes = opts and opts.maxBytes or 2 ^ 30
|
|
local n = #text
|
|
local i = 1
|
|
local total = 0
|
|
|
|
local function skip()
|
|
while i <= n do
|
|
local c = sbyte(text, i)
|
|
if c == 59 then -- ; comment
|
|
local e = sfind(text, "\n", i, true)
|
|
i = if e then e + 1 else n + 1
|
|
elseif c == 32 or c == 9 or c == 10 or c == 13 then
|
|
i += 1
|
|
else
|
|
return
|
|
end
|
|
end
|
|
end
|
|
|
|
local function nameAt(): (string?, number)
|
|
local c = sbyte(text, i)
|
|
if not c or not isNameStart(c) then
|
|
return nil, i
|
|
end
|
|
local s, e = sfind(text, "^[%w_|%-]+", i)
|
|
if not s then
|
|
return nil, i
|
|
end
|
|
local j = (e :: number) + 1
|
|
while j <= n do
|
|
local d = sbyte(text, j)
|
|
if d == 32 or d == 9 then
|
|
j += 1
|
|
else
|
|
break
|
|
end
|
|
end
|
|
if sbyte(text, j) == 58 then
|
|
return ssub(text, i, e :: number), j + 1
|
|
end
|
|
return nil, i
|
|
end
|
|
|
|
local function values(): ({ any }?, string?)
|
|
local props: { any } = {}
|
|
while true do
|
|
skip()
|
|
if i > n then
|
|
return props, nil
|
|
end
|
|
local c = sbyte(text, i)
|
|
if c == 44 then -- ,
|
|
i += 1
|
|
elseif c == 123 or c == 125 then
|
|
return props, nil
|
|
elseif c == 34 then -- "
|
|
local e = sfind(text, "\"", i + 1, true)
|
|
if not e then
|
|
return nil, "unterminated string"
|
|
end
|
|
local s = ssub(text, i + 1, e - 1)
|
|
if sfind(s, "&", 1, true) then
|
|
s = string.gsub(s, """, "\"")
|
|
end
|
|
table.insert(props, s)
|
|
i = e + 1
|
|
elseif c == 42 then -- *N
|
|
local s, e = sfind(text, "^%*%d+", i)
|
|
if not s then
|
|
return nil, "bad array length"
|
|
end
|
|
table.insert(props, { star = tonumber(ssub(text, i + 1, e :: number)) })
|
|
i = (e :: number) + 1
|
|
elseif (c >= 48 and c <= 57) or c == 45 or c == 43 or c == 46 then
|
|
local s, e = sfind(text, "^[-+%deE.]+", i)
|
|
local tok = ssub(text, i, e :: number)
|
|
local v = tonumber(tok)
|
|
if v == nil then
|
|
-- "1.#INF", "-1.#IND", "nan": special floats
|
|
local s2, e2 = sfind(text, "^[-+%w.#]+", i)
|
|
tok = ssub(text, i, e2 :: number)
|
|
local low = string.lower(tok)
|
|
if sfind(low, "inf", 1, true) then
|
|
v = if sbyte(tok, 1) == 45 then -math.huge else math.huge
|
|
else
|
|
v = 0 / 0
|
|
end
|
|
e = e2
|
|
local _ = s2
|
|
elseif #tok > 15 and not sfind(tok, "[.eE]") then
|
|
v = (if sbyte(tok, 1) == 45 then "L-" else "L") .. string.gsub(tok, "^[-+]", "") :: any
|
|
end
|
|
local _ = s
|
|
table.insert(props, v)
|
|
i = (e :: number) + 1
|
|
else
|
|
local nm = nameAt()
|
|
if nm then
|
|
return props, nil
|
|
end
|
|
local s, e = sfind(text, "^[%w_]+", i)
|
|
if not s then
|
|
return nil, string.format("unexpected character %q", ssub(text, i, i))
|
|
end
|
|
table.insert(props, ssub(text, i, e :: number))
|
|
i = (e :: number) + 1
|
|
end
|
|
end
|
|
end
|
|
|
|
local function nodes(depth: number, into: { Node }): string?
|
|
while true do
|
|
skip()
|
|
if i > n then
|
|
return if depth > 0 then "unterminated {" else nil
|
|
end
|
|
if sbyte(text, i) == 125 then
|
|
if depth == 0 then
|
|
return "unbalanced }"
|
|
end
|
|
i += 1
|
|
return nil
|
|
end
|
|
local name, j = nameAt()
|
|
if not name then
|
|
return string.format("expected a node name at %q", ssub(text, i, i + 10))
|
|
end
|
|
i = j
|
|
local props, err = values()
|
|
if not props then
|
|
return err
|
|
end
|
|
local node: Node = { name = name, props = props, children = {} }
|
|
skip()
|
|
if sbyte(text, i) == 123 then
|
|
if depth >= MAX_DEPTH then
|
|
return "nodes nested too deep"
|
|
end
|
|
i += 1
|
|
local e = nodes(depth + 1, node.children)
|
|
if e then
|
|
return e
|
|
end
|
|
end
|
|
local p1 = props[1]
|
|
if #props == 1 and type(p1) == "table" and p1.star then
|
|
local a = node.children[1]
|
|
local count = if a and a.name == "a" then #a.props else 0
|
|
total += count * 8
|
|
if total > maxBytes then
|
|
return "limit:arrays over " .. maxBytes .. " bytes"
|
|
end
|
|
local data = buffer.create(count * 8)
|
|
for k = 1, count do
|
|
local v = (a :: Node).props[k]
|
|
buffer.writef64(data, (k - 1) * 8, if type(v) == "number" then v else (tonumber(v) or 0))
|
|
end
|
|
node.props = { { array = true, type = "d", count = count, data = data } }
|
|
node.children = {}
|
|
end
|
|
table.insert(into, node)
|
|
end
|
|
end
|
|
|
|
local root: Node = { name = "", props = {}, children = {} }
|
|
local err = nodes(0, root.children)
|
|
if err then
|
|
return nil, err, i - 1
|
|
end
|
|
local version = nil
|
|
for _, c in root.children do
|
|
if c.name == "FBXHeaderExtension" then
|
|
for _, d in c.children do
|
|
if d.name == "FBXVersion" then
|
|
version = d.props[1]
|
|
end
|
|
end
|
|
end
|
|
end
|
|
return root, version, nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ writer
|
|
local function fmt(v: number): string
|
|
if v == math.floor(v) and math.abs(v) < 1e15 then
|
|
return string.format("%d", v)
|
|
end
|
|
if v ~= v then
|
|
return "0"
|
|
end
|
|
return string.format("%.17g", v)
|
|
end
|
|
|
|
function Ascii.write(root: any, version: number?): string
|
|
local out = { string.format("; FBX %d.%d.0 project file\n; Created by rosetta\n\n", (version or 7400) // 1000, (version or 7400) % 1000 // 100) }
|
|
local function node(nd: any, indent: string)
|
|
local parts = {}
|
|
local arr = nil
|
|
for _, v in nd.props do
|
|
local tv = type(v)
|
|
if tv == "number" then
|
|
table.insert(parts, fmt(v))
|
|
elseif tv == "boolean" then
|
|
table.insert(parts, if v then "T" else "F")
|
|
elseif tv == "string" then
|
|
if string.sub(v, 1, 1) == "L" and string.find(v, "^L%-?%d+$") then
|
|
table.insert(parts, string.sub(v, 2))
|
|
else
|
|
local nm, cls = string.match(v, "^(.-)%z\1(.*)$")
|
|
local sv = if nm then (cls :: string) .. "::" .. nm else v
|
|
table.insert(parts, "\"" .. string.gsub(sv, "\"", """) .. "\"")
|
|
end
|
|
elseif tv == "table" and v.array then
|
|
arr = v
|
|
elseif tv == "buffer" then
|
|
-- raw bytes (Video Content): base64 split into lines as the FBX SDK writes them
|
|
local b64 = Base64.encode(v)
|
|
local chunks = {}
|
|
for k = 1, #b64, 2048 do
|
|
table.insert(chunks, "\"" .. string.sub(b64, k, k + 2047) .. "\"")
|
|
end
|
|
table.insert(parts, if #chunks > 0 then table.concat(chunks, ",\n") else "\"\"")
|
|
end
|
|
end
|
|
if arr then
|
|
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
|
|
table.insert(out, string.format("%s%s: *%d {\n%s\ta: %s\n%s}\n", indent, nd.name, arr.count, indent, table.concat(vals, ","), indent))
|
|
return
|
|
end
|
|
table.insert(out, indent .. nd.name .. ": " .. table.concat(parts, ", "))
|
|
local ch = nd.children
|
|
if ch and #ch > 0 then
|
|
table.insert(out, " {\n")
|
|
for _, c in ch do
|
|
node(c, indent .. "\t")
|
|
end
|
|
table.insert(out, indent .. "}\n")
|
|
else
|
|
table.insert(out, "\n")
|
|
end
|
|
end
|
|
for _, c in root.children do
|
|
node(c, "")
|
|
end
|
|
return table.concat(out)
|
|
end
|
|
|
|
return Ascii
|