]> git.lizzy.rs Git - elidragon_v2.git/commitdiff
Fix DB system crashes and implement custom directory support
authorElias Fleckenstein <eliasfleckenstein@web.de>
Sun, 28 Feb 2021 10:32:46 +0000 (11:32 +0100)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Sun, 28 Feb 2021 10:32:46 +0000 (11:32 +0100)
mods/elidragon_db/init.lua

index f4f03e8212197576313c9dc992a4442cbc2d6888..c4329214a74a83336fe827ae06cec37dddb3b9da 100644 (file)
@@ -2,16 +2,17 @@ local class = elidragon.class
 
 local db = {}
 
-local paths = {}
+local private = {}
 local worldpath = minetest.get_worldpath()
 
-function db:constructor(name, initial_data, dir)
-       paths[self] = dir or worldpath .. "/" .. name .. ".json"
+function db:constructor(name, initial_data, dir, env)
+       private[self] = {env = env or _G, path = (dir or worldpath) .. "/" .. name .. ".json"}
        self:load(initial_data or {})
 end
 
 function db:load(initial_data)
-       local file = io.open(paths[self], "r")
+       local _self = private[self]
+       local file = _self.env.io.open(_self.path, "r")
        local data = file and minetest.parse_json(file:read()) or {}
        if file then
                file:close()
@@ -27,14 +28,15 @@ function db:load(initial_data)
 end
 
 function db:save()
-       local file = assert(io.open(paths[self], "w"))
+       local _self = private[self]
+       local file = assert(_self.env.io.open(_self.path, "w"))
        file:write(minetest.write_json(self))
        file:close()
 end
 
 function db:close()
        self:save()
-       paths[self] = nil
+       private[self] = nil
 end
 
 minetest.register_on_shutdown(function()