From e29183f57429628eac457714c1010197c664cc44 Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Sun, 11 Jul 2021 16:07:54 +0200 Subject: [PATCH] Add Minetest Luon: ItemStack and AreaStore support --- minetest.lua | 1 + minetest/luon.lua | 29 +++++++++++++++++++++++++++++ test.lua | 2 ++ 3 files changed, 32 insertions(+) create mode 100644 minetest/luon.lua 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)