From 3cb7b4f53e7eb1c79f38b4ff5cc4834ad93d931b Mon Sep 17 00:00:00 2001 From: Joachim Stolberg Date: Sun, 30 Jan 2022 11:24:48 +0100 Subject: [PATCH] Fix marshal issues --- basis/lib.lua | 11 ++++++++++ basis/nodedata_meta.lua | 41 ++++++----------------------------- basis/nodedata_sqlite.lua | 25 +++++---------------- basis/numbers_sqlite.lua | 11 +--------- fusion_reactor/controller.lua | 1 + 5 files changed, 26 insertions(+), 63 deletions(-) diff --git a/basis/lib.lua b/basis/lib.lua index d47f5ac..6bf49dc 100644 --- a/basis/lib.lua +++ b/basis/lib.lua @@ -244,6 +244,17 @@ function techage.add_to_set(set, x) end end +-- techage.tbl_filter({"a", "b", "c", "d"}, function(v, k, t) return v >= "c" end) --> {"c","d"} +techage.tbl_filter = function(t, filterIter) + local out = {} + + for k, v in pairs(t) do + if filterIter(v, k, t) then out[k] = v end + end + + return out +end + function techage.get_node_lvm(pos) local node = minetest.get_node_or_nil(pos) if node then diff --git a/basis/nodedata_meta.lua b/basis/nodedata_meta.lua index 875ce6d..5128080 100644 --- a/basis/nodedata_meta.lua +++ b/basis/nodedata_meta.lua @@ -17,37 +17,8 @@ local M = minetest.get_meta local storage = techage.storage -------------------------------------------------------------------- --- Marshaling -------------------------------------------------------------------- -local use_marshal = minetest.settings:get_bool('techage_use_marshal', false) -local MAR_MAGIC = 0x8e - --- default functions -local serialize = minetest.serialize -local deserialize = minetest.deserialize - -if use_marshal then - if not techage.IE then - error("Please add 'secure.trusted_mods = techage' to minetest.conf!") - end - local marshal = techage.IE.require("marshal") - if not marshal then - error("Please install marshal via 'luarocks install lua-marshal'") - end - - serialize = marshal.encode - - deserialize = function(s) - if s ~= "" then - if s:byte(1) == MAR_MAGIC then - return marshal.decode(s) - else - return minetest.deserialize(s) - end - end - end -end +local MP = minetest.get_modpath("techage") +local serialize, deserialize = dofile(MP .. "/basis/marshal.lua") ------------------------------------------------------------------- -- API functions @@ -86,15 +57,17 @@ function api.get_node_data(pos) end -- Meta data can't be written reliable at shutdown, --- so we have to store/restore the data differently +-- so we have to store/restore the data differently. function api.freeze_at_shutdown(data) - storage:set_string("shutdown_nodedata", serialize(data)) + -- We use the minetest serialize function, because marshal.encode + -- generates a binary string, which can't be stored in storage. + storage:set_string("shutdown_nodedata", minetest.serialize(data)) end function api.restore_at_startup() local s = storage:get_string("shutdown_nodedata") if s ~= "" then - return deserialize(s) or {} + return minetest.deserialize(s) or {} end return {} end diff --git a/basis/nodedata_sqlite.lua b/basis/nodedata_sqlite.lua index e9e3f8c..fae7aaf 100644 --- a/basis/nodedata_sqlite.lua +++ b/basis/nodedata_sqlite.lua @@ -18,23 +18,17 @@ local M = minetest.get_meta ------------------------------------------------------------------- -- Database ------------------------------------------------------------------- -local MN = minetest.get_current_modname() local WP = minetest.get_worldpath() -local MAR_MAGIC = 0x8e if not techage.IE then error("Please add 'secure.trusted_mods = techage' to minetest.conf!") end local sqlite3 = techage.IE.require("lsqlite3") -local marshal = techage.IE.require("marshal") if not sqlite3 then error("Please install sqlite3 via 'luarocks install lsqlite3'") end -if not marshal then - error("Please install marshal via 'luarocks install lua-marshal'") -end local db = sqlite3.open(WP.."/techage_nodedata.sqlite") local ROW = sqlite3.ROW @@ -71,21 +65,18 @@ end ------------------------------------------------------------------- local api = {} +local MP = minetest.get_modpath("techage") +local serialize, deserialize = dofile(MP .. "/basis/marshal.lua") + function api.store_mapblock_data(key, mapblock_data) - -- deactivated due to weird server crashes without error logs - --local s = marshal.encode(mapblock_data) - local s = minetest.serialize(mapblock_data) + local s = serialize(mapblock_data) return set_block(key, s) end function api.get_mapblock_data(key) local s = get_block(key) if s then - if s:byte(1) == MAR_MAGIC then - return marshal.decode(s) - else - return minetest.deserialize(s) - end + return deserialize(s) end api.store_mapblock_data(key, {}) return {} @@ -96,11 +87,7 @@ function api.get_node_data(pos) local s = M(pos):get_string("ta_data") if s ~= "" then M(pos):set_string("ta_data", "") - if s:byte(1) == MAR_MAGIC then - return marshal.decode(s) - else - return minetest.deserialize(s) - end + return deserialize(s) end return {} end diff --git a/basis/numbers_sqlite.lua b/basis/numbers_sqlite.lua index f2f8be8..58b0291 100644 --- a/basis/numbers_sqlite.lua +++ b/basis/numbers_sqlite.lua @@ -12,31 +12,22 @@ ]]-- --- for lazy programmers -local M = minetest.get_meta - local storage = techage.storage ------------------------------------------------------------------- -- Database ------------------------------------------------------------------- -local MN = minetest.get_current_modname() local WP = minetest.get_worldpath() -local MAR_MAGIC = 0x8e if not techage.IE then error("Please add 'secure.trusted_mods = techage' to minetest.conf!") end local sqlite3 = techage.IE.require("lsqlite3") -local marshal = techage.IE.require("marshal") if not sqlite3 then error("Please install sqlite3 via 'luarocks install lsqlite3'") end -if not marshal then - error("Please install marshal via 'luarocks install lua-marshal'") -end local db = sqlite3.open(WP.."/techage_numbers.sqlite") local ROW = sqlite3.ROW @@ -129,7 +120,7 @@ end -- delete invalid entries function api.delete_invalid_entries(node_def) minetest.log("info", "[TechAge] Data maintenance started") - for id, num, x, y, z in db:urows('SELECT * FROM numbers') do + for _, num, x, y, z in db:urows('SELECT * FROM numbers') do local pos = {x = x, y = y, z = z} local name = techage.get_node_lvm(pos).name if not node_def[name] then diff --git a/fusion_reactor/controller.lua b/fusion_reactor/controller.lua index 848cdaf..8af0bd8 100644 --- a/fusion_reactor/controller.lua +++ b/fusion_reactor/controller.lua @@ -46,6 +46,7 @@ local function count_trues(t) end local function nucleus(t) + t = techage.tbl_filter(t, function(v, k, t) return type(v) == "table" end) if #t == 4 then if vector.equals(t[1], t[2]) and vector.equals(t[3], t[4]) then return true