161 lines
5.7 KiB
Luau
161 lines
5.7 KiB
Luau
--!strict
|
|
-- codecs/model/Vrml: the VRML97 / X3D scene-graph engine shared by model/vrml (.wrl) and model/x3d (.x3d XML,
|
|
-- .x3dv classic): one parser for the classic encoding (codecs/model/Vrml/Parse), one for the XML encoding
|
|
-- (codecs/model/Vrml/Xml over codecs/text/Xml), both into the same node graph, and one builder from the graph to a
|
|
-- Scene (codecs/model/Vrml/Build, codecs/model/Vrml/Geometry). Gzipped files (.wrz / .x3dz / .x3dvz) are inflated
|
|
-- first. New code, written from ISO/IEC 14772-1 (VRML97), ISO/IEC 19775-1 (X3D) and its encodings 19776-1 / -2.
|
|
--
|
|
-- local graph, err = Vrml.graph(bytes) -- classic or XML, by content
|
|
-- local scene, err2 = Vrml.decode(bytes, { format = "vrml", files = { ["tex.png"] = png } })
|
|
local Err = require("@formats/core/Err")
|
|
local Types = require("@formats/Types")
|
|
local Deflate = require("@formats/codecs/compress/Deflate")
|
|
local XmlCodec = require("@formats/codecs/text/Xml")
|
|
local Parse = require("@formats/codecs/model/Vrml/Parse")
|
|
local VXml = require("@formats/codecs/model/Vrml/Xml")
|
|
local Build = require("@formats/codecs/model/Vrml/Build")
|
|
local Write = require("@formats/codecs/model/Vrml/Write")
|
|
|
|
local Vrml = {}
|
|
|
|
export type Graph = Parse.Graph
|
|
export type Node = Parse.Node
|
|
export type Options = Build.Options & {
|
|
files: { [string]: any }?, -- textures / Inline files by uri or base name (buffer or string)
|
|
resolve: ((uri: string) -> any)?, -- or: reads them (the api sets it for paths)
|
|
maxBytes: number?, -- the inflated size of a gzipped file (default 2^28)
|
|
}
|
|
|
|
-- the bytes as text: gzip inflated, UTF-8 BOM dropped
|
|
local function text(data: buffer | string, maxBytes: number?): (string?, Err.Err?)
|
|
local b = if type(data) == "string" then buffer.fromstring(data) else data
|
|
if buffer.len(b) >= 2 and buffer.readu8(b, 0) == 0x1F and buffer.readu8(b, 1) == 0x8B then
|
|
local out, err = Deflate.gunzip(b)
|
|
if not out then
|
|
return nil, err
|
|
end
|
|
if buffer.len(out) > (maxBytes or 2 ^ 28) then
|
|
return nil, Err.new(Err.LIMIT, "inflated file over maxBytes")
|
|
end
|
|
b = out
|
|
end
|
|
local s = buffer.tostring(b)
|
|
if string.sub(s, 1, 3) == "\239\187\191" then
|
|
s = string.sub(s, 4)
|
|
end
|
|
return s, nil
|
|
end
|
|
|
|
local function code(msg: string): string
|
|
if string.find(msg, "unsupported", 1, true) then
|
|
return Err.UNSUPPORTED
|
|
elseif string.find(msg, "limit", 1, true) then
|
|
return Err.LIMIT
|
|
elseif string.find(msg, "unterminated", 1, true) then
|
|
return Err.TRUNCATED
|
|
end
|
|
return Err.CORRUPT
|
|
end
|
|
|
|
-- parse(text) -> the graph of a classic-encoding file (VRML97 / .x3dv)
|
|
function Vrml.parse(s: string): (Graph?, Err.Err?)
|
|
local g, msg = Parse.parse(s)
|
|
if not g then
|
|
return nil, Err.new(code(msg or "parse error"), msg or "parse error")
|
|
end
|
|
return g, nil
|
|
end
|
|
|
|
-- parseXml(text | XML document) -> the graph of an X3D XML file
|
|
function Vrml.parseXml(src: any): (Graph?, Err.Err?)
|
|
local doc = src
|
|
if type(src) == "string" then
|
|
local d, err = XmlCodec.decode(src)
|
|
if not d and err and err.code == Err.TRUNCATED and not string.find(src, "</X3D>", 1, true) then
|
|
-- VTK's X3D exporter leaves the root element open
|
|
d = XmlCodec.decode(src .. "\n</X3D>\n")
|
|
end
|
|
if not d then
|
|
return nil, err
|
|
end
|
|
doc = d
|
|
end
|
|
local g, msg = VXml.graph(doc)
|
|
if not g then
|
|
return nil, Err.new(code(msg or "bad X3D"), msg or "bad X3D")
|
|
end
|
|
return g, nil
|
|
end
|
|
|
|
-- graph(bytes, opts?) -> the graph of either encoding (XML when the text starts with "<"); binary X3D is UNSUPPORTED
|
|
function Vrml.graph(data: buffer | string, opts: Options?): (Graph?, Err.Err?)
|
|
local s, err = text(data, opts and opts.maxBytes)
|
|
if not s then
|
|
return nil, err
|
|
end
|
|
local head = string.match(s, "^%s*(.?)")
|
|
if head == "<" then
|
|
return Vrml.parseXml(s)
|
|
end
|
|
if string.sub(s, 1, 4) == "\224\0\0\1" or string.sub(s, 1, 4) == "$EXI" then
|
|
return nil, Err.new(Err.UNSUPPORTED, "binary X3D (Fast Infoset / EXI encoding)")
|
|
end
|
|
return Vrml.parse(s)
|
|
end
|
|
|
|
-- fetcher(opts) -> fetch(uri) -> buffer?: opts.files (uri or base name) / opts.resolve
|
|
function Vrml.fetcher(opts: Options?): (uri: string) -> buffer?
|
|
return function(uri: string): buffer?
|
|
if not opts then
|
|
return nil
|
|
end
|
|
local v: any = nil
|
|
if type(opts.files) == "table" then
|
|
v = opts.files[uri] or opts.files[string.match(uri, "([^/\\]+)$") or uri]
|
|
end
|
|
if v == nil and type(opts.resolve) == "function" then
|
|
local ok, res = pcall(opts.resolve, uri)
|
|
v = if ok then res else nil
|
|
end
|
|
return if type(v) == "string" then buffer.fromstring(v) elseif type(v) == "buffer" then v else nil
|
|
end
|
|
end
|
|
|
|
-- build(graph, opts?) -> Scene
|
|
function Vrml.build(graph: Graph, opts: Options?): (Types.Scene?, Err.Err?)
|
|
local o: any = table.clone(opts or {})
|
|
o.fetch = o.fetch or Vrml.fetcher(opts)
|
|
o.load = o.load or function(data: buffer): (any, string?)
|
|
local g, err = Vrml.graph(data, opts)
|
|
return g, if err then err.message else nil
|
|
end
|
|
local scene, msg = Build.scene(graph, o)
|
|
if not scene then
|
|
return nil, Err.new(code(msg or "build failed"), msg or "build failed")
|
|
end
|
|
return scene, nil
|
|
end
|
|
|
|
-- decode(bytes, opts?) -> Scene: graph + build
|
|
function Vrml.decode(data: buffer | string, opts: Options?): (Types.Scene?, Err.Err?)
|
|
local g, err = Vrml.graph(data, opts)
|
|
if not g then
|
|
return nil, err
|
|
end
|
|
return Vrml.build(g, opts)
|
|
end
|
|
|
|
-- encode(scene, opts?) -> text: opts.encoding "vrml97" (default) / "x3dv" / "xml"; texture bytes go into opts.files
|
|
function Vrml.encode(scene: Types.Scene, opts: Write.Options?): (string?, 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 s, msg = Write.text(scene, opts or {})
|
|
if not s then
|
|
return nil, Err.new(Err.ARG, msg or "the scene could not be written")
|
|
end
|
|
return s, nil
|
|
end
|
|
|
|
return Vrml
|