mirror of
https://github.com/minetest-technic/datastorage.git
synced 2024-08-20 12:34:08 +02:00
Rewrite
This commit is contained in:
parent
4bcd5938fe
commit
9b3e1254c9
151
init.lua
151
init.lua
@ -1,99 +1,94 @@
|
|||||||
datastorage={}
|
datastorage = {data = {}}
|
||||||
datastorage["registered_players"]={}
|
|
||||||
|
|
||||||
datastorage.save_data = function(table_pointer)
|
local DIR_DELIM = DIR_DELIM or "/"
|
||||||
local data = minetest.serialize( datastorage[table_pointer] )
|
local data_path = minetest.get_worldpath()..DIR_DELIM.."datastorage"..DIR_DELIM
|
||||||
local path = minetest.get_worldpath().."/datastorage_"..table_pointer..".data"
|
|
||||||
local file = io.open( path, "w" )
|
function datastorage.save(id)
|
||||||
if( file ) then
|
local data = datastorage.data[id]
|
||||||
file:write( data )
|
-- Check if the container is empty
|
||||||
|
if not data or not next(data) then return end
|
||||||
|
for _, sub_data in pairs(data) do
|
||||||
|
if not next(sub_data) then return end
|
||||||
|
end
|
||||||
|
|
||||||
|
local file = io.open(data_path..id, "w")
|
||||||
|
if not file then
|
||||||
|
-- Most likely the data directory doesn't exist, create it
|
||||||
|
-- and try again.
|
||||||
|
-- Using os.execute like this is not very platform independent
|
||||||
|
-- or safe; but most platforms name their directory
|
||||||
|
-- creation utility mkdir, and the data path is unlikely to
|
||||||
|
-- contain special characters and is only mutabable by the
|
||||||
|
-- admin.
|
||||||
|
os.execute('mkdir "'..data_path..'"')
|
||||||
|
file = io.open(data_path..id, "w")
|
||||||
|
if not file then return end
|
||||||
|
end
|
||||||
|
|
||||||
|
local datastr = minetest.serialize(data)
|
||||||
|
if not datastr then return end
|
||||||
|
|
||||||
|
file:write(datastr)
|
||||||
file:close()
|
file:close()
|
||||||
return true
|
return true
|
||||||
else return nil
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
datastorage.load_data = function(table_pointer)
|
function datastorage.load(id)
|
||||||
local path = minetest.get_worldpath().."/datastorage_"..table_pointer..".data"
|
local file = io.open(data_path..id, "r")
|
||||||
local file = io.open( path, "r" )
|
if not file then return end
|
||||||
if( file ) then
|
|
||||||
local data = file:read("*all")
|
local data = minetest.deserialize(file:read("*all"))
|
||||||
datastorage[table_pointer] = minetest.deserialize( data )
|
datastorage.data[id] = data
|
||||||
|
|
||||||
file:close()
|
file:close()
|
||||||
return true
|
return data
|
||||||
else return nil
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
datastorage.get_container = function (mod_name, player)
|
-- Compatability
|
||||||
local player_name = player:get_player_name()
|
function datastorage.get_container(player, id)
|
||||||
local container = datastorage[player_name]
|
return datastorage.get(player:get_player_name(), id)
|
||||||
if container[mod_name] == nil then
|
|
||||||
container[mod_name] = {}
|
|
||||||
end
|
|
||||||
datastorage.save_data(player_name)
|
|
||||||
return container[mod_name]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- forced save of all player's data
|
-- Retrieves a value from the data storage
|
||||||
datastorage.save_container = function (player)
|
function datastorage.get(id, ...)
|
||||||
local player_name = player:get_player_name()
|
local last = datastorage.data[id]
|
||||||
datastorage.save_data(player_name)
|
if last == nil then last = datastorage.load(id) end
|
||||||
|
if last == nil then
|
||||||
|
last = {}
|
||||||
|
datastorage.data[id] = last
|
||||||
|
end
|
||||||
|
local cur = last
|
||||||
|
for _, sub_id in ipairs({...}) do
|
||||||
|
last = cur
|
||||||
|
cur = cur[sub_id]
|
||||||
|
if cur == nil then
|
||||||
|
cur = {}
|
||||||
|
last[sub_id] = cur
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return cur
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Saves a container and reomves it from memory
|
||||||
-- Init
|
function datastorage.finish(id)
|
||||||
if datastorage.load_data("registered_players") == nil then
|
datastorage.save(id)
|
||||||
datastorage["registered_players"]={}
|
datastorage.data[id] = nil
|
||||||
datastorage.save_data("registered_players")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_on_joinplayer(function(player)
|
-- Compatability
|
||||||
local player_name = player:get_player_name()
|
function datastorage.save_container(player)
|
||||||
local registered = nil
|
return datastorage.save(player:get_player_name())
|
||||||
for __,tab in ipairs(datastorage["registered_players"]) do
|
|
||||||
if tab["player_name"] == player_name then registered = true break end
|
|
||||||
end
|
end
|
||||||
if registered == nil then
|
|
||||||
print("creating new one")
|
|
||||||
local new={}
|
|
||||||
new["player_name"]=player_name
|
|
||||||
table.insert(datastorage["registered_players"],new)
|
|
||||||
datastorage[player_name]={}
|
|
||||||
datastorage.save_data("registered_players")
|
|
||||||
datastorage.save_data(player_name)
|
|
||||||
else
|
|
||||||
print("loading containers")
|
|
||||||
datastorage.load_data(player_name)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- TEST AREA:
|
|
||||||
local test_container = datastorage.get_container("dupa",player)
|
|
||||||
test_container["var1"] = 1.23
|
|
||||||
test_container["table1"] = {}
|
|
||||||
test_container["table1"]["var2"] = "nowa"
|
|
||||||
test_container["table1"]["var3"] = "a string"
|
|
||||||
|
|
||||||
print("Testing:")
|
|
||||||
print(dump(test_container))
|
|
||||||
--datastorage.save_container(player)
|
|
||||||
-- END OF TEST AREA
|
|
||||||
|
|
||||||
end
|
|
||||||
)
|
|
||||||
|
|
||||||
minetest.register_on_leaveplayer(function(player)
|
minetest.register_on_leaveplayer(function(player)
|
||||||
local player_name = player:get_player_name()
|
local player_name = player:get_player_name()
|
||||||
datastorage.save_data(player_name)
|
datastorage.save(player_name)
|
||||||
datastorage[player_name] = nil
|
datastorage.data[player_name] = nil
|
||||||
end
|
end)
|
||||||
)
|
|
||||||
|
|
||||||
minetest.register_on_shutdown(function()
|
minetest.register_on_shutdown(function()
|
||||||
for __,tab in ipairs(datastorage["registered_players"]) do
|
for id in pairs(datastorage.data) do
|
||||||
if datastorage[tab["player_name"]] == nil then break end
|
datastorage.save(id)
|
||||||
datastorage.save_data(tab["player_name"])
|
|
||||||
end
|
end
|
||||||
end
|
end)
|
||||||
)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user