Fix marshal issues

This commit is contained in:
Joachim Stolberg 2022-01-30 11:24:48 +01:00
parent c60b242c5d
commit 3cb7b4f53e
5 changed files with 26 additions and 63 deletions

View File

@ -244,6 +244,17 @@ function techage.add_to_set(set, x)
end end
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) function techage.get_node_lvm(pos)
local node = minetest.get_node_or_nil(pos) local node = minetest.get_node_or_nil(pos)
if node then if node then

View File

@ -17,37 +17,8 @@ local M = minetest.get_meta
local storage = techage.storage local storage = techage.storage
------------------------------------------------------------------- local MP = minetest.get_modpath("techage")
-- Marshaling local serialize, deserialize = dofile(MP .. "/basis/marshal.lua")
-------------------------------------------------------------------
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
------------------------------------------------------------------- -------------------------------------------------------------------
-- API functions -- API functions
@ -86,15 +57,17 @@ function api.get_node_data(pos)
end end
-- Meta data can't be written reliable at shutdown, -- 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) 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 end
function api.restore_at_startup() function api.restore_at_startup()
local s = storage:get_string("shutdown_nodedata") local s = storage:get_string("shutdown_nodedata")
if s ~= "" then if s ~= "" then
return deserialize(s) or {} return minetest.deserialize(s) or {}
end end
return {} return {}
end end

View File

@ -18,23 +18,17 @@ local M = minetest.get_meta
------------------------------------------------------------------- -------------------------------------------------------------------
-- Database -- Database
------------------------------------------------------------------- -------------------------------------------------------------------
local MN = minetest.get_current_modname()
local WP = minetest.get_worldpath() local WP = minetest.get_worldpath()
local MAR_MAGIC = 0x8e
if not techage.IE then if not techage.IE then
error("Please add 'secure.trusted_mods = techage' to minetest.conf!") error("Please add 'secure.trusted_mods = techage' to minetest.conf!")
end end
local sqlite3 = techage.IE.require("lsqlite3") local sqlite3 = techage.IE.require("lsqlite3")
local marshal = techage.IE.require("marshal")
if not sqlite3 then if not sqlite3 then
error("Please install sqlite3 via 'luarocks install lsqlite3'") error("Please install sqlite3 via 'luarocks install lsqlite3'")
end end
if not marshal then
error("Please install marshal via 'luarocks install lua-marshal'")
end
local db = sqlite3.open(WP.."/techage_nodedata.sqlite") local db = sqlite3.open(WP.."/techage_nodedata.sqlite")
local ROW = sqlite3.ROW local ROW = sqlite3.ROW
@ -71,21 +65,18 @@ end
------------------------------------------------------------------- -------------------------------------------------------------------
local api = {} local api = {}
local MP = minetest.get_modpath("techage")
local serialize, deserialize = dofile(MP .. "/basis/marshal.lua")
function api.store_mapblock_data(key, mapblock_data) function api.store_mapblock_data(key, mapblock_data)
-- deactivated due to weird server crashes without error logs local s = serialize(mapblock_data)
--local s = marshal.encode(mapblock_data)
local s = minetest.serialize(mapblock_data)
return set_block(key, s) return set_block(key, s)
end end
function api.get_mapblock_data(key) function api.get_mapblock_data(key)
local s = get_block(key) local s = get_block(key)
if s then if s then
if s:byte(1) == MAR_MAGIC then return deserialize(s)
return marshal.decode(s)
else
return minetest.deserialize(s)
end
end end
api.store_mapblock_data(key, {}) api.store_mapblock_data(key, {})
return {} return {}
@ -96,11 +87,7 @@ function api.get_node_data(pos)
local s = M(pos):get_string("ta_data") local s = M(pos):get_string("ta_data")
if s ~= "" then if s ~= "" then
M(pos):set_string("ta_data", "") M(pos):set_string("ta_data", "")
if s:byte(1) == MAR_MAGIC then return deserialize(s)
return marshal.decode(s)
else
return minetest.deserialize(s)
end
end end
return {} return {}
end end

View File

@ -12,31 +12,22 @@
]]-- ]]--
-- for lazy programmers
local M = minetest.get_meta
local storage = techage.storage local storage = techage.storage
------------------------------------------------------------------- -------------------------------------------------------------------
-- Database -- Database
------------------------------------------------------------------- -------------------------------------------------------------------
local MN = minetest.get_current_modname()
local WP = minetest.get_worldpath() local WP = minetest.get_worldpath()
local MAR_MAGIC = 0x8e
if not techage.IE then if not techage.IE then
error("Please add 'secure.trusted_mods = techage' to minetest.conf!") error("Please add 'secure.trusted_mods = techage' to minetest.conf!")
end end
local sqlite3 = techage.IE.require("lsqlite3") local sqlite3 = techage.IE.require("lsqlite3")
local marshal = techage.IE.require("marshal")
if not sqlite3 then if not sqlite3 then
error("Please install sqlite3 via 'luarocks install lsqlite3'") error("Please install sqlite3 via 'luarocks install lsqlite3'")
end end
if not marshal then
error("Please install marshal via 'luarocks install lua-marshal'")
end
local db = sqlite3.open(WP.."/techage_numbers.sqlite") local db = sqlite3.open(WP.."/techage_numbers.sqlite")
local ROW = sqlite3.ROW local ROW = sqlite3.ROW
@ -129,7 +120,7 @@ end
-- delete invalid entries -- delete invalid entries
function api.delete_invalid_entries(node_def) function api.delete_invalid_entries(node_def)
minetest.log("info", "[TechAge] Data maintenance started") 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 pos = {x = x, y = y, z = z}
local name = techage.get_node_lvm(pos).name local name = techage.get_node_lvm(pos).name
if not node_def[name] then if not node_def[name] then

View File

@ -46,6 +46,7 @@ local function count_trues(t)
end end
local function nucleus(t) local function nucleus(t)
t = techage.tbl_filter(t, function(v, k, t) return type(v) == "table" end)
if #t == 4 then if #t == 4 then
if vector.equals(t[1], t[2]) and vector.equals(t[3], t[4]) then if vector.equals(t[1], t[2]) and vector.equals(t[3], t[4]) then
return true return true