diff --git a/minetest.lua b/minetest.lua index 055c332..4b2b8a4 100644 --- a/minetest.lua +++ b/minetest.lua @@ -9,4 +9,5 @@ load"raycast" load"wielditem_change" load"colorspec" load"schematic" +load"luon" return _ENV \ No newline at end of file diff --git a/minetest/luon.lua b/minetest/luon.lua new file mode 100644 index 0000000..e22a9d9 --- /dev/null +++ b/minetest/luon.lua @@ -0,0 +1,29 @@ +-- Localize globals +local getmetatable, AreaStore, ItemStack + = getmetatable, AreaStore, ItemStack + +-- Metatable lookup for classes specified in lua_api.txt, section "Class reference" +local AreaStoreMT = getmetatable(AreaStore()) +local ItemStackMT = getmetatable(ItemStack"") +local metatables = { + [AreaStoreMT] = {name = "AreaStore", method = AreaStoreMT.to_string}, + [ItemStackMT] = {name = "ItemStack", method = ItemStackMT.to_table}, + -- TODO expand +} + +(...).luon = modlib.luon.new{ + aux_write = function(_, value) + local type = metatables[getmetatable(value)] + if type then + return type.name, type.method(value) + end + end, + aux_read = { + AreaStore = function(...) + local store = AreaStore() + store:from_string(...) + return store + end, + ItemStack = ItemStack + } +} \ No newline at end of file diff --git a/test.lua b/test.lua index 9705a67..2ddabf2 100644 --- a/test.lua +++ b/test.lua @@ -238,6 +238,8 @@ end if not _G.minetest then return end +assert(minetest.luon:read_string(minetest.luon:write_string(ItemStack""))) + -- colorspec local colorspec = minetest.colorspec local function test_from_string(string, number)