refactored storage system to use the overloadable unix-storage which does not rely on ocutil
This commit is contained in:
modpol_core
@ -55,7 +55,7 @@ dofile (topdir .. "/util/misc.lua")
|
||||
-- Select a storage method
|
||||
-- -- preferably, declare this in the init.lua that calls modpol.lua This is the default.
|
||||
-- Works with CLI:
|
||||
modpol.storage_file_path = modpol.storage_file_path or topdir .. "/storage/storage-local.lua"
|
||||
modpol.storage_file_path = modpol.storage_file_path or topdir .. "/storage/unix-storage.lua"
|
||||
-- Works with Minetest 5:
|
||||
--modpol.storage_file_path = modpol.storage_file_path or topdir .. "/storage/storage-mod_storage.lua")
|
||||
|
||||
|
107
modpol_core/storage/unix-storage.lua
Normal file
107
modpol_core/storage/unix-storage.lua
Normal file
@ -0,0 +1,107 @@
|
||||
-- ===================================================================
|
||||
-- /unix-storage.lua
|
||||
-- Persistent storage in /data using Serpent Serializer
|
||||
-- Unix systems only
|
||||
|
||||
modpol.datadir = modpol.topdir .. "/data"
|
||||
modpol.file_ledger = modpol.datadir .. "/ledger.dat"
|
||||
modpol.file_orgs = modpol.datadir .. "/orgs.dat"
|
||||
|
||||
os.execute ("mkdir -p " .. modpol.datadir)
|
||||
|
||||
modpol.ocutil.setlogdir (modpol.datadir)
|
||||
modpol.ocutil.setlogname ("modpol.log")
|
||||
|
||||
modpol.serpent = {}
|
||||
dofile (modpol.topdir .. "/util/serpent/serpent.lua")
|
||||
|
||||
-- ===================================================================
|
||||
|
||||
local write_file = function(path, data)
|
||||
local file, err
|
||||
file, err = io.open(path, "w")
|
||||
if err then print(err) os.exit(1) end
|
||||
file:write(data)
|
||||
file:close()
|
||||
end
|
||||
|
||||
local read_file = function(path)
|
||||
local file, err
|
||||
file, err = io.open(path, "r")
|
||||
if err then return nil end
|
||||
local data = file:read()
|
||||
file:close()
|
||||
return data
|
||||
end
|
||||
|
||||
-- ===================================================================
|
||||
|
||||
local store_ledger = function(verbose)
|
||||
local serialized_ledger = modpol.serpent.dump(modpol.ledger)
|
||||
write_file(modpol.file_ledger, serialized_ledger)
|
||||
|
||||
local count = 0
|
||||
for _ in pairs(modpol.ledger) do count = count + 1 end
|
||||
|
||||
if verbose then modpol.ocutil.log(count .. " ledger entries stored to disk")
|
||||
end
|
||||
|
||||
local store_orgs = function(verbose)
|
||||
local serialized_orgs = modpol.serpent.dump(modpol.orgs)
|
||||
write_file(modpol.file_orgs, serialized_orgs)
|
||||
|
||||
local count = 0
|
||||
for _ in pairs(modpol.orgs.array) do count = count + 1 end
|
||||
|
||||
if verbose then modpol.ocutil.log(count .. " orgs stored to disk")
|
||||
end
|
||||
|
||||
modpol.store_data = function(verbose)
|
||||
store_ledger(verbose)
|
||||
store_orgs(verbose)
|
||||
end
|
||||
|
||||
-- ===================================================================
|
||||
|
||||
local load_ledger = function()
|
||||
local obj = read_file(modpol.file_ledger)
|
||||
if obj ~= nil then
|
||||
local func, err = load(obj)
|
||||
if err then
|
||||
modpol.ocutil.log("Error loading ledger data")
|
||||
os.exit(1)
|
||||
end
|
||||
modpol.ledger = func()
|
||||
|
||||
local count = 0
|
||||
for _ in pairs(modpol.ledger) do count = count + 1 end
|
||||
|
||||
modpol.ocutil.log(count .. " ledger entries loaded from disk")
|
||||
else
|
||||
modpol.ocutil.log("No stored ledger data found")
|
||||
end
|
||||
end
|
||||
|
||||
local load_orgs = function()
|
||||
local obj = read_file(modpol.file_orgs)
|
||||
if obj ~= nil then
|
||||
local func, err = load(obj)
|
||||
if err then
|
||||
modpol.ocutil.log("Error loading org data")
|
||||
os.exit(1)
|
||||
end
|
||||
modpol.orgs = func()
|
||||
|
||||
local count = 0
|
||||
for _ in pairs(modpol.orgs.array) do count = count + 1 end
|
||||
|
||||
modpol.ocutil.log(count .. " orgs loaded from disk")
|
||||
else
|
||||
modpol.ocutil.log("No stored orgs found")
|
||||
end
|
||||
end
|
||||
|
||||
modpol.load_storage = function()
|
||||
load_ledger()
|
||||
load_orgs()
|
||||
end
|
Reference in New Issue
Block a user