techage/basis/numbers_storage.lua

99 lines
2.4 KiB
Lua
Raw Permalink Normal View History

2020-05-19 19:43:24 +03:00
--[[
TechAge
=======
Copyright (C) 2020 Joachim Stolberg
2020-10-19 20:09:17 +03:00
AGPL v3
2020-05-19 19:43:24 +03:00
See LICENSE.txt for more information
2022-01-03 23:40:31 +03:00
2020-05-23 15:11:35 +03:00
Storage backend for node number mapping via mod storage
2020-05-19 19:43:24 +03:00
]]--
local backend = {}
2020-05-23 15:11:35 +03:00
local storage = techage.storage
2020-05-19 19:43:24 +03:00
-- legacy method
local function deserialize(s)
local tbl = {}
for line in s:gmatch("[^;]+") do
local num, spos = unpack(string.split(line, "="))
tbl[num] = minetest.string_to_pos(spos)
end
return tbl
end
local Version = minetest.deserialize(storage:get_string("Version")) or 3
local NextNumber = 0
if Version == 1 then
Version = 3
local tbl = minetest.deserialize(storage:get_string("Number2Pos")) or {}
NextNumber = minetest.deserialize(storage:get_string("NextNumber")) or 1
for num, pos in pairs(tbl) do
storage:set_string(num, minetest.pos_to_string(pos))
end
storage:set_string("Number2Pos", "")
elseif Version == 2 then
Version = 3
NextNumber = minetest.deserialize(storage:get_string("NextNumber")) or 1
local tbl = deserialize(storage:get_string("Number2Pos"))
for num, pos in pairs(tbl) do
storage:set_string(num, minetest.pos_to_string(pos))
end
storage:set_string("Number2Pos", "")
else
Version = 3
NextNumber = storage:get_int("NextNumber")
end
storage:set_int("NextNumber", NextNumber)
storage:set_int("Version", Version)
-------------------------------------------------------------------
-- API functions
-------------------------------------------------------------------
function backend.get_nodepos(number)
return minetest.string_to_pos(storage:get_string(number))
2022-01-03 23:40:31 +03:00
end
2020-05-19 19:43:24 +03:00
function backend.set_nodepos(number, pos)
2020-05-19 23:24:50 +03:00
storage:set_string(number, minetest.pos_to_string(pos))
2022-01-03 23:40:31 +03:00
end
2020-05-19 19:43:24 +03:00
function backend.add_nodepos(pos)
local num = tostring(NextNumber)
NextNumber = NextNumber + 1
storage:set_int("NextNumber", NextNumber)
2020-05-19 23:24:50 +03:00
storage:set_string(num, minetest.pos_to_string(pos))
2020-05-19 19:43:24 +03:00
return num
2022-01-03 23:40:31 +03:00
end
2020-05-19 19:43:24 +03:00
function backend.del_nodepos(number)
2020-05-19 23:24:50 +03:00
storage:set_string(number, "")
2022-01-03 23:40:31 +03:00
end
2020-05-19 19:43:24 +03:00
-- delete invalid entries
function backend.delete_invalid_entries(node_def)
minetest.log("info", "[TechAge] Data maintenance started")
for i = 1, NextNumber do
local number = tostring(i)
if storage:contains(number) then
local pos = backend.get_nodepos(number)
local name = techage.get_node_lvm(pos).name
if not node_def[name] then
backend.del_nodepos(number)
2020-05-19 23:24:50 +03:00
else
2022-01-03 23:40:31 +03:00
minetest.get_meta(pos):set_string("node_number", number)
2020-05-19 19:43:24 +03:00
end
end
end
minetest.log("info", "[TechAge] Data maintenance finished")
2022-01-03 23:40:31 +03:00
end
2020-05-19 19:43:24 +03:00
return backend