1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-01 17:38:41 +00:00

Add API for custom (de)serializers

This commit is contained in:
y5nw 2025-02-22 22:40:34 +01:00
parent 738b3cfadc
commit 2a1a2b1f9d
2 changed files with 61 additions and 24 deletions

View file

@ -1,14 +1,28 @@
-- Registered metatables, used by the C++ packer
local serializable_metatables = {}
local known_metatables = {}
function core.register_portable_metatable(name, mt)
local function dummy_serializer(x)
return x
end
function core.register_portable_metatable(name, mt, serializer, deserializer)
serializer = serializer or dummy_serializer
deserializer = deserializer or function(x) return setmetatable(x, mt) end
assert(type(name) == "string", ("attempt to use %s value as metatable name"):format(type(name)))
assert(type(mt) == "table", ("attempt to register a %s value as metatable"):format(type(mt)))
assert(type(serializer), ("attempt to use a %s value as serializer"):format(type(serializer)))
assert(type(deserializer), ("attempt to use a %s value as serialier"):format(type(deserializer)))
assert(known_metatables[name] == nil or known_metatables[name] == mt,
("attempt to override metatable %s"):format(name))
known_metatables[name] = mt
known_metatables[mt] = name
serializable_metatables[mt] = serializer
serializable_metatables[name] = deserializer
end
core.known_metatables = known_metatables
core.serializable_metatables = serializable_metatables
function core.register_async_metatable(...)
core.log("deprecated", "core.register_async_metatable is deprecated. " ..