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,143 @@
|
||||
--!strict
|
||||
-- data/tres: Godot text resources (.tres, formats 2-4) as data, read + write (codecs/text/GodotVariant). The value:
|
||||
-- { type, format, uid?, scriptClass?, loadSteps?, external = { { id, type, path, uid? } }, subresources = { { id,
|
||||
-- type, properties } }, properties } with Variant values (Vector3, Color, SubResource, ... as { type, args } tables;
|
||||
-- dictionaries and property tables keep their order). Not supported: binary .res files.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ 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")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Tres = {
|
||||
name = "tres",
|
||||
category = "data",
|
||||
extensions = { "tres" },
|
||||
mime = { "application/x-godot-resource" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types", "codecs/text/GodotVariant" },
|
||||
caps = { decode = true, encode = true, ordered = true },
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- default 2^28
|
||||
maxDepth: number?, -- value nesting, default 256
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Tres.sniff(r: Bytes.Reader): number
|
||||
local h = Sniff.text(r, 256)
|
||||
if string.match(h, "^%s*%[gd_resource[%s%]]") then
|
||||
return 1
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
local function props(sec: GodotVariant.Section): { [string]: any }
|
||||
local out = {}
|
||||
for _, k in sec.order do
|
||||
out[k] = sec.props[k]
|
||||
end
|
||||
GodotVariant.copyFloats(sec.props, out)
|
||||
return Types.object(out, table.clone(sec.order) :: { any })
|
||||
end
|
||||
|
||||
function Tres.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
local o: DecodeOptions = 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
|
||||
local head = doc.sections[1]
|
||||
if not head or head.tag ~= "gd_resource" then
|
||||
return nil, Err.new(if head and head.tag == "gd_scene" then Err.UNSUPPORTED else Err.CORRUPT, if head and head.tag == "gd_scene" then "a scene (model/godot), not a resource" else "no [gd_resource] header")
|
||||
end
|
||||
local a = head.attrs
|
||||
local value: { [string]: any } = {
|
||||
type = a.type,
|
||||
format = a.format,
|
||||
uid = a.uid,
|
||||
scriptClass = a.script_class,
|
||||
loadSteps = a.load_steps,
|
||||
external = {},
|
||||
subresources = {},
|
||||
properties = Types.object({}),
|
||||
}
|
||||
for i = 2, #doc.sections do
|
||||
local sec = doc.sections[i]
|
||||
local sa = sec.attrs
|
||||
if sec.tag == "ext_resource" then
|
||||
table.insert(value.external, { id = sa.id, type = sa.type, path = sa.path, uid = sa.uid })
|
||||
elseif sec.tag == "sub_resource" then
|
||||
table.insert(value.subresources, { id = sa.id, type = sa.type, properties = props(sec) })
|
||||
elseif sec.tag == "resource" then
|
||||
value.properties = props(sec)
|
||||
end
|
||||
end
|
||||
return value, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
local function section(tag: string, pairs_: { any }, properties: any?): GodotVariant.Section
|
||||
local attrs, order = {}, {}
|
||||
local i = 1
|
||||
while pairs_[i] ~= nil do
|
||||
if pairs_[i + 1] ~= nil then
|
||||
attrs[pairs_[i]] = pairs_[i + 1]
|
||||
table.insert(order, pairs_[i])
|
||||
end
|
||||
i += 2
|
||||
end
|
||||
local p, po = {}, {}
|
||||
if type(properties) == "table" then
|
||||
for _, k in Types.keys(properties) do
|
||||
if type(k) == "string" then
|
||||
p[k] = properties[k]
|
||||
table.insert(po, k)
|
||||
end
|
||||
end
|
||||
GodotVariant.copyFloats(properties, p)
|
||||
end
|
||||
return { tag = tag, attrs = attrs, attrOrder = order, props = p, order = po }
|
||||
end
|
||||
|
||||
function Tres.encode(value: any, opts: any?): (buffer?, Err.Err?)
|
||||
if type(value) ~= "table" or type(value.type) ~= "string" then
|
||||
return nil, Err.new(Err.ARG, "needs a resource table with a type")
|
||||
end
|
||||
local ext = if type(value.external) == "table" then value.external else {}
|
||||
local sub = if type(value.subresources) == "table" then value.subresources else {}
|
||||
local format = if type(value.format) == "number" then value.format else 3
|
||||
local steps = if type(value.loadSteps) == "number" then value.loadSteps elseif #ext + #sub > 0 then #ext + #sub + 1 else nil
|
||||
local sections = { section("gd_resource", { "type", value.type, "script_class", value.scriptClass, "load_steps", steps, "format", format, "uid", value.uid }) }
|
||||
for _, e in ext do
|
||||
if type(e) == "table" then
|
||||
table.insert(sections, section("ext_resource", { "type", e.type, "uid", e.uid, "path", e.path, "id", e.id }))
|
||||
end
|
||||
end
|
||||
for _, s in sub do
|
||||
if type(s) == "table" then
|
||||
table.insert(sections, section("sub_resource", { "type", s.type, "id", s.id }, s.properties))
|
||||
end
|
||||
end
|
||||
table.insert(sections, section("resource", {}, value.properties))
|
||||
local ok, s = pcall(GodotVariant.encode, { sections = sections })
|
||||
if not ok then
|
||||
return nil, Err.new(Err.ARG, "cannot encode: " .. tostring(s))
|
||||
end
|
||||
return buffer.fromstring(s), nil
|
||||
end
|
||||
|
||||
return Tres
|
||||
Reference in new issue
Block a user