forked from mtcontrib/pipeworks
Store teleport tube DB in mod storage (#40)
* Store teleport tube DB in mod storage * Prevent overwriting of tube DB backup file * Remove backup code * Slightly improve storage reading code * Log DB migration to mod storage * Add migration note in README * Improve pipeworks.logger
This commit is contained in:
parent
75894355d4
commit
1823690dad
4
README
4
README
@ -20,3 +20,7 @@ This mod is a work in progress.
|
|||||||
|
|
||||||
Please note that owing to the nature of this mod, I have opted to use 64px
|
Please note that owing to the nature of this mod, I have opted to use 64px
|
||||||
textures. Anything less just looks terrible.
|
textures. Anything less just looks terrible.
|
||||||
|
|
||||||
|
The teleport tube database used to kept in a file named 'teleport_tubes'.
|
||||||
|
The database is now kept in mod storage. The migration from 'teleport_tubes' is
|
||||||
|
automatic. The old file is then kept around but is not used at all.
|
||||||
|
2
init.lua
2
init.lua
@ -128,7 +128,7 @@ function pipeworks.replace_name(tbl,tr,name)
|
|||||||
end
|
end
|
||||||
|
|
||||||
pipeworks.logger = function(msg)
|
pipeworks.logger = function(msg)
|
||||||
print("[pipeworks] "..msg)
|
minetest.log("action", "[pipeworks] "..msg)
|
||||||
end
|
end
|
||||||
|
|
||||||
-------------------------------------------
|
-------------------------------------------
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
local S = minetest.get_translator("pipeworks")
|
local S = minetest.get_translator("pipeworks")
|
||||||
local filename=minetest.get_worldpath() .. "/teleport_tubes"
|
local filename=minetest.get_worldpath() .. "/teleport_tubes" -- Only used for backward-compat
|
||||||
|
local storage=minetest.get_mod_storage()
|
||||||
|
|
||||||
local tp_tube_db = nil -- nil forces a read
|
local tp_tube_db = nil -- nil forces a read
|
||||||
local tp_tube_db_version = 2.0
|
local tp_tube_db_version = 3.0
|
||||||
|
|
||||||
-- cached rceiver list: hash(pos) => {receivers}
|
-- cached rceiver list: hash(pos) => {receivers}
|
||||||
local cache = {}
|
local cache = {}
|
||||||
@ -12,47 +13,78 @@ local function hash(pos)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function save_tube_db()
|
local function save_tube_db()
|
||||||
local file, err = io.open(filename, "w")
|
|
||||||
if file then
|
|
||||||
tp_tube_db.version = tp_tube_db_version
|
|
||||||
file:write(minetest.serialize(tp_tube_db))
|
|
||||||
tp_tube_db.version = nil
|
|
||||||
io.close(file)
|
|
||||||
else
|
|
||||||
error(err)
|
|
||||||
end
|
|
||||||
-- reset tp-tube cache
|
-- reset tp-tube cache
|
||||||
cache = {}
|
cache = {}
|
||||||
|
|
||||||
|
local fields = {version = tp_tube_db_version}
|
||||||
|
for key, val in pairs(tp_tube_db) do
|
||||||
|
fields[key] = minetest.serialize(val)
|
||||||
|
end
|
||||||
|
storage:from_table({fields = fields})
|
||||||
|
end
|
||||||
|
|
||||||
|
local function save_tube_db_entry(hash)
|
||||||
|
-- reset tp-tube cache
|
||||||
|
cache = {}
|
||||||
|
|
||||||
|
local val = tp_tube_db[hash]
|
||||||
|
storage:set_string(hash, val and minetest.serialize(val) or "")
|
||||||
end
|
end
|
||||||
|
|
||||||
local function migrate_tube_db()
|
local function migrate_tube_db()
|
||||||
local tmp_db = {}
|
local old_version = tp_tube_db.version or 0
|
||||||
tp_tube_db.version = nil
|
tp_tube_db.version = nil
|
||||||
|
if old_version < 2.0 then
|
||||||
|
local tmp_db = {}
|
||||||
for _, val in pairs(tp_tube_db) do
|
for _, val in pairs(tp_tube_db) do
|
||||||
if(val.channel ~= "") then -- skip unconfigured tubes
|
if(val.channel ~= "") then -- skip unconfigured tubes
|
||||||
tmp_db[hash(val)] = val
|
tmp_db[hash(val)] = val
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
tp_tube_db = tmp_db
|
tp_tube_db = tmp_db
|
||||||
|
end
|
||||||
save_tube_db()
|
save_tube_db()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function read_tube_db()
|
local function read_tube_db()
|
||||||
local file = io.open(filename, "r")
|
local file = not storage:contains("version") and io.open(filename, "r")
|
||||||
if file ~= nil then
|
if not file then
|
||||||
|
tp_tube_db = {}
|
||||||
|
|
||||||
|
for key, val in pairs(storage:to_table().fields) do
|
||||||
|
if tonumber(key) then
|
||||||
|
tp_tube_db[key] = minetest.deserialize(val)
|
||||||
|
elseif key == "version" then
|
||||||
|
tp_tube_db.version = tonumber(val)
|
||||||
|
else
|
||||||
|
error("Unknown field in teleport tube DB: " .. key)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if tp_tube_db.version == nil then
|
||||||
|
tp_tube_db.version = tp_tube_db_version
|
||||||
|
storage:set_string("version", tp_tube_db.version)
|
||||||
|
elseif tp_tube_db.version > tp_tube_db_version then
|
||||||
|
error("Cannot read teleport tube DB of version " .. tp_tube_db.version)
|
||||||
|
end
|
||||||
|
else
|
||||||
local file_content = file:read("*all")
|
local file_content = file:read("*all")
|
||||||
io.close(file)
|
io.close(file)
|
||||||
|
|
||||||
|
pipeworks.logger("Moving teleport tube DB into mod storage from " .. filename)
|
||||||
|
|
||||||
if file_content and file_content ~= "" then
|
if file_content and file_content ~= "" then
|
||||||
tp_tube_db = minetest.deserialize(file_content)
|
tp_tube_db = minetest.deserialize(file_content)
|
||||||
|
else
|
||||||
|
tp_tube_db = {version = 2.0}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if(not tp_tube_db.version or tonumber(tp_tube_db.version) < tp_tube_db_version) then
|
if(not tp_tube_db.version or tonumber(tp_tube_db.version) < tp_tube_db_version) then
|
||||||
migrate_tube_db()
|
migrate_tube_db()
|
||||||
end
|
end
|
||||||
tp_tube_db.version = nil -- we add it back when saving
|
tp_tube_db.version = nil
|
||||||
return tp_tube_db -- we read sucessfully
|
|
||||||
end
|
|
||||||
end
|
|
||||||
tp_tube_db = {}
|
|
||||||
return tp_tube_db
|
return tp_tube_db
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -69,7 +101,7 @@ local function set_tube(pos, channel, can_receive)
|
|||||||
if tube then
|
if tube then
|
||||||
tube.channel = channel
|
tube.channel = channel
|
||||||
tube.cr = can_receive
|
tube.cr = can_receive
|
||||||
save_tube_db()
|
save_tube_db_entry(hash)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -88,13 +120,14 @@ local function set_tube(pos, channel, can_receive)
|
|||||||
end
|
end
|
||||||
|
|
||||||
tp_tube_db[hash] = {x=pos.x,y=pos.y,z=pos.z,channel=channel,cr=can_receive}
|
tp_tube_db[hash] = {x=pos.x,y=pos.y,z=pos.z,channel=channel,cr=can_receive}
|
||||||
save_tube_db()
|
save_tube_db_entry(hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function remove_tube(pos)
|
local function remove_tube(pos)
|
||||||
local tubes = tp_tube_db or read_tube_db()
|
local tubes = tp_tube_db or read_tube_db()
|
||||||
tubes[hash(pos)] = nil
|
local hash = hash(pos)
|
||||||
save_tube_db()
|
tubes[hash] = nil
|
||||||
|
save_tube_db_entry(hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function read_node_with_vm(pos)
|
local function read_node_with_vm(pos)
|
||||||
@ -114,7 +147,6 @@ local function get_receivers(pos, channel)
|
|||||||
|
|
||||||
local tubes = tp_tube_db or read_tube_db()
|
local tubes = tp_tube_db or read_tube_db()
|
||||||
local receivers = {}
|
local receivers = {}
|
||||||
local dirty = false
|
|
||||||
for key, val in pairs(tubes) do
|
for key, val in pairs(tubes) do
|
||||||
-- skip all non-receivers and the tube that it came from as early as possible, as this is called often
|
-- skip all non-receivers and the tube that it came from as early as possible, as this is called often
|
||||||
if (val.cr == 1 and val.channel == channel and (val.x ~= pos.x or val.y ~= pos.y or val.z ~= pos.z)) then
|
if (val.cr == 1 and val.channel == channel and (val.x ~= pos.x or val.y ~= pos.y or val.z ~= pos.z)) then
|
||||||
@ -125,13 +157,10 @@ local function get_receivers(pos, channel)
|
|||||||
table.insert(receivers, val)
|
table.insert(receivers, val)
|
||||||
else
|
else
|
||||||
tp_tube_db[key] = nil
|
tp_tube_db[key] = nil
|
||||||
dirty = true
|
save_tube_db_entry(key)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if dirty then
|
|
||||||
save_tube_db()
|
|
||||||
end
|
|
||||||
-- cache the result for next time
|
-- cache the result for next time
|
||||||
cache[hash] = receivers
|
cache[hash] = receivers
|
||||||
return receivers
|
return receivers
|
||||||
@ -290,6 +319,7 @@ end
|
|||||||
pipeworks.tptube = {
|
pipeworks.tptube = {
|
||||||
hash = hash,
|
hash = hash,
|
||||||
save_tube_db = save_tube_db,
|
save_tube_db = save_tube_db,
|
||||||
|
save_tube_db_entry = save_tube_db_entry,
|
||||||
get_db = function() return tp_tube_db or read_tube_db() end,
|
get_db = function() return tp_tube_db or read_tube_db() end,
|
||||||
set_tube = set_tube,
|
set_tube = set_tube,
|
||||||
update_meta = update_meta,
|
update_meta = update_meta,
|
||||||
|
Loading…
Reference in New Issue
Block a user