Verify and compress all meta strings

This commit is contained in:
stujones11 2017-07-07 18:40:44 +01:00
parent f35f4815b8
commit cdc3b1f54f
1 changed files with 32 additions and 9 deletions

41
api.lua
View File

@ -108,19 +108,27 @@ local function restore_facedir(node, delta, yaw)
end end
end end
local function compress(str)
str = minetest.compress(str, "deflate", 9)
if str:len() > 0xffff then
minetest.log("error",
"String too long for serialization!")
str = ""
end
return str
end
local function decompress(str)
return minetest.decompress(str, "deflate", 9)
end
local function inventory_to_table(data) local function inventory_to_table(data)
for _, list in pairs(data) do for _, list in pairs(data) do
for i, stack in ipairs(list) do for i, stack in ipairs(list) do
local t = ItemStack(stack):to_table() or {} local t = ItemStack(stack):to_table() or {}
for k, v in pairs(t) do for k, v in pairs(t) do
if type(v) == "string" then if type(v) == "string" and v ~= "" then
local str = minetest.compress(v, "deflate", 9) t[k] = compress(v)
if str:len() > 0xffff then
minetest.log("error",
"String too long for serialization!")
str = ""
end
t[k] = str
end end
end end
list[i] = t list[i] = t
@ -133,7 +141,7 @@ local function inventory_from_table(data)
for i, stack in ipairs(list) do for i, stack in ipairs(list) do
for k, v in pairs(stack) do for k, v in pairs(stack) do
if type(v) == "string" and v ~= "" then if type(v) == "string" and v ~= "" then
stack[k] = minetest.decompress(v, "deflate", 9) stack[k] = decompress(v)
end end
end end
list[i] = ItemStack(stack) list[i] = ItemStack(stack)
@ -370,6 +378,11 @@ meshnode.create = function(pos, parent)
inventory_to_table(meta_tab.inventory) inventory_to_table(meta_tab.inventory)
end end
if next(meta_tab) then if next(meta_tab) then
for k, v in pairs(meta_tab) do
if type(v) == "string" and v ~= "" then
meta_tab[k] = compress(v)
end
end
meta_str = minetest.serialize(meta_tab) meta_str = minetest.serialize(meta_tab)
end end
local ref = { local ref = {
@ -407,6 +420,11 @@ meshnode.restore = function(ref, parent)
if meta_tab.inventory then if meta_tab.inventory then
inventory_from_table(meta_tab.inventory) inventory_from_table(meta_tab.inventory)
end end
for k, v in pairs(meta_tab) do
if type(v) == "string" and v ~= "" then
meta_tab[k] = decompress(v)
end
end
meta:from_table(meta_tab) meta:from_table(meta_tab)
end end
end end
@ -459,6 +477,11 @@ meshnode.restore_all = function(parent, name)
if meta_tab.inventory then if meta_tab.inventory then
inventory_from_table(meta_tab.inventory) inventory_from_table(meta_tab.inventory)
end end
for k, v in pairs(meta_tab) do
if type(v) == "string" and v ~= "" then
meta_tab[k] = decompress(v)
end
end
meta:from_table(meta_tab) meta:from_table(meta_tab)
end end
table.insert(positions, data.pos) table.insert(positions, data.pos)