forked from minetest-mods/areas
Move areas:save() into async
On newer Minetest servers, handles saving jobs in async environment. To prevent conflicts, the save file is locked whie saving, and if a code requests saving while the file is locked, data is saved again immediately after finishing the current save.
This commit is contained in:
48
internal.lua
48
internal.lua
@ -4,26 +4,38 @@ function areas:player_exists(name)
|
||||
return minetest.get_auth_handler().get_auth(name) ~= nil
|
||||
end
|
||||
|
||||
local safe_file_write = minetest.safe_file_write
|
||||
if safe_file_write == nil then
|
||||
function safe_file_write(path, content)
|
||||
local file, err = io.open(path, "w")
|
||||
if err then
|
||||
return err
|
||||
end
|
||||
file:write(content)
|
||||
file:close()
|
||||
end
|
||||
end
|
||||
-- When saving is done in an async thread, the function will not be present in this global namespace.
|
||||
if not areas._internal_do_save then
|
||||
local saving_requested = false
|
||||
local saving_locked = false
|
||||
|
||||
-- Save the areas table to a file
|
||||
function areas:save()
|
||||
local datastr = minetest.write_json(self.areas)
|
||||
if not datastr then
|
||||
minetest.log("error", "[areas] Failed to serialize area data!")
|
||||
return
|
||||
-- Required cuz we are referring to _G.areas._internal_do_save *inside*
|
||||
-- async env (it does not exist in the main thread)
|
||||
local function async_func(...)
|
||||
return areas._internal_do_save(...)
|
||||
end
|
||||
|
||||
local function done_callback()
|
||||
saving_locked = false
|
||||
if saving_requested == true then
|
||||
saving_requested = false
|
||||
return areas:save()
|
||||
end
|
||||
end
|
||||
|
||||
function areas:save()
|
||||
if saving_locked == true then
|
||||
saving_requested = true
|
||||
else
|
||||
saving_locked = true
|
||||
return core.handle_async(async_func, done_callback, self.areas, self.config.filename)
|
||||
end
|
||||
end
|
||||
else
|
||||
-- Save the areas table to a file
|
||||
function areas:save()
|
||||
return areas._internal_do_save(self.areas, self.config.filename)
|
||||
end
|
||||
return safe_file_write(self.config.filename, datastr)
|
||||
end
|
||||
|
||||
-- Load the areas table from the save file
|
||||
|
Reference in New Issue
Block a user