model/godot + data/tres + codecs/text/GodotVariant: Godot text scenes (.tscn / .escn) read + write against Godot 4's loader, .tres resources as data
docs/STYLE.md: the layout block lists codecs/text/GodotVariant (DOCS lint).
This commit is contained in:
1 parent
9c02ebdee5
commit
8f26e32a71
24 files changed
+3649
-5
No files matched your search
@@ -0,0 +1,89 @@
|
||||
--!strict
|
||||
-- model/godot: Godot text scenes (.tscn, .escn; formats 2-4) as a Scene, read + write (format 3); checked against
|
||||
-- Godot 4's own loader. Supports: Node3D trees (transforms or position / rotation / scale), MeshInstance3D with
|
||||
-- ArrayMesh surfaces and primitive meshes (Box, Plane, Quad, Sphere, Cylinder), StandardMaterial3D / SpatialMaterial
|
||||
-- as PBR, surface material overrides, Camera3D, Omni / Spot / Directional lights, instanced scenes through
|
||||
-- opts.resolve, non-3D nodes in extras.godot. Not supported: compressed or Godot 3 binary surfaces, skeletons and
|
||||
-- animations (AnimationPlayer libraries stay sub-resources), blend shapes, other mesh / material resource types.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Sniff = require("@rosetta/core/Sniff")
|
||||
local Types = require("@rosetta/Types")
|
||||
local GodotVariant = require("@rosetta/codecs/text/GodotVariant")
|
||||
local Build = require("@rosetta/model/godot/Build")
|
||||
local Write = require("@rosetta/model/godot/Write")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Godot = {
|
||||
name = "godot",
|
||||
category = "model",
|
||||
extensions = { "tscn", "escn" },
|
||||
mime = { "application/x-godot-scene" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types", "codecs/text/GodotVariant", "codecs/model/Mat4" },
|
||||
caps = { decode = true, encode = true, text = true, materials = true, cameras = true, lights = true, instances = true },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- default 2^28
|
||||
maxVertices: number?, -- per surface, default 2^24
|
||||
maxDepth: number?, -- value nesting, default 256
|
||||
resolve: ((string) -> buffer?)?, -- res:// paths without the prefix: textures, instanced scenes
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Godot.sniff(r: Bytes.Reader): number
|
||||
local h = Sniff.text(r, 256)
|
||||
if string.match(h, "^%s*%[gd_scene[%s%]]") then
|
||||
return 1
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
function Godot.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
|
||||
local o: any = opts or {}
|
||||
if r.limit - r.base > (o.maxBytes or 2 ^ 28) then
|
||||
return nil, Err.new(Err.LIMIT, "file is over the byte limit")
|
||||
end
|
||||
local text = buffer.readstring(r.buf, r.base, r.limit - r.base)
|
||||
local ok, doc, perr = pcall(function(): (GodotVariant.Document?, string?)
|
||||
return GodotVariant.parse(text, { maxDepth = o.maxDepth })
|
||||
end)
|
||||
if not ok then
|
||||
return nil, Err.new(Err.CORRUPT, "bad data: " .. tostring(doc))
|
||||
end
|
||||
if not doc then
|
||||
return nil, Err.from(perr)
|
||||
end
|
||||
if #doc.sections == 0 then
|
||||
return nil, Err.new(Err.CORRUPT, "empty file")
|
||||
end
|
||||
local ok2, scene, msg = pcall(Build.scene, doc, o)
|
||||
if not ok2 then
|
||||
return nil, Err.from(tostring(scene))
|
||||
end
|
||||
if not scene then
|
||||
return nil, Err.from(msg)
|
||||
end
|
||||
return scene, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
function Godot.encode(scene: Types.Scene, opts: any?): (buffer?, Err.Err?)
|
||||
if type(scene) ~= "table" or type(scene.nodes) ~= "table" then
|
||||
return nil, Err.new(Err.ARG, "needs a Scene")
|
||||
end
|
||||
local ok, doc, msg = pcall(Write.document, scene)
|
||||
if not ok then
|
||||
return nil, Err.new(Err.ARG, "bad scene: " .. tostring(doc))
|
||||
end
|
||||
if not doc then
|
||||
return nil, Err.new(Err.ARG, msg or "bad scene")
|
||||
end
|
||||
return buffer.fromstring(GodotVariant.encode(doc)), nil
|
||||
end
|
||||
|
||||
return Godot
|
||||
Reference in new issue
Block a user