aboutsummaryrefslogtreecommitdiff
path: root/serialize.lua
diff options
context:
space:
mode:
authorluk3yx <luk3yx@users.noreply.github.com>2023-09-08 03:38:26 +1200
committerGitHub <noreply@github.com>2023-09-07 17:38:26 +0200
commitc850d11a3ccaf21183a137c0ecaf0ebaee58f514 (patch)
tree9ff79a5082897f77821751d8139903bcf55100fb /serialize.lua
parentd2cda4f73a3a5372b70ffa63e2a16bf39d734e40 (diff)
downloadxban2-c850d11a3ccaf21183a137c0ecaf0ebaee58f514.tar.gz
xban2-c850d11a3ccaf21183a137c0ecaf0ebaee58f514.tar.bz2
xban2-c850d11a3ccaf21183a137c0ecaf0ebaee58f514.zip
Use JSON in xban.db (#26)
This should prevent bans database from resetting with a "function has more than 65536 constants" error. Older databases should still be loaded correctly. Also makes use of minetest.safe_file_write to avoid data corruption.
Diffstat (limited to 'serialize.lua')
-rw-r--r--serialize.lua39
1 files changed, 39 insertions, 0 deletions
diff --git a/serialize.lua b/serialize.lua
index c159141..0a54212 100644
--- a/serialize.lua
+++ b/serialize.lua
@@ -27,5 +27,44 @@ local function my_serialize_2(t, level)
end
function xban.serialize(t)
+ minetest.log("warning", "[xban2] xban.serialize() is deprecated")
return "return {\n"..my_serialize_2(t, 1).."\n}"
end
+
+-- JSON doesn't allow combined string+number keys, this function moves any
+-- number keys into an "entries" table
+function xban.serialize_db(t)
+ local res = {}
+ local entries = {}
+ for k, v in pairs(t) do
+ if type(k) == "number" then
+ entries[k] = v
+ else
+ res[k] = v
+ end
+ end
+ res.entries = entries
+ return minetest.write_json(res, true)
+end
+
+function xban.deserialize_db(s)
+ if s:sub(1, 1) ~= "{" then
+ -- Load legacy databases
+ return minetest.deserialize(s)
+ end
+
+ local res, err = minetest.parse_json(s)
+ if not res then
+ return nil, err
+ end
+
+ -- Remove all "null"s added by empty tables
+ for i, entry in ipairs(res.entries or {}) do
+ entry.names = entry.names or {}
+ entry.record = entry.record or {}
+ res[i] = entry
+ end
+ res.entries = nil
+
+ return res
+end