From 2ecdd6cb1dd05b5627cbee32b357cfc15594809b Mon Sep 17 00:00:00 2001 From: Anthony Zhang Date: Thu, 19 Jul 2012 22:54:26 -0400 Subject: [PATCH] Re-add support for the old WorldEdit save format on a load-only basis. Implemented as worldedit.deserialize_old(), and documented in README.md. //load will now transparently detect these files and load them correctly. --- README.md | 8 ++++++++ functions.lua | 31 +++++++++++++++++++++++++++++++ init.lua | 10 ++++++---- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ffe6852..8b17a37 100644 --- a/README.md +++ b/README.md @@ -179,6 +179,14 @@ Loads the nodes represented by string `value` at position `originpos`. Returns the number of nodes deserialized. +### worldedit.deserialize_old(originpos, value) + +Loads the nodes represented by string `value` at position `originpos`, using the older table-based WorldEdit format. + +This function is deprecated, and should not be used unless there is a need to support legacy WorldEdit save files. + +Returns the number of nodes deserialized. + License ------- Copyright 2012 sfan5 and Anthony Zhang (Temperest) diff --git a/functions.lua b/functions.lua index b752b06..1e727db 100644 --- a/functions.lua +++ b/functions.lua @@ -246,4 +246,35 @@ worldedit.deserialize = function(originpos, value) count = count + 1 end return count +end + +--loads the nodes represented by string `value` at position `originpos`, returning the number of nodes deserialized +--based on [table.save/table.load](http://lua-users.org/wiki/SaveTableToFile) by ChillCode, available under the MIT license (GPL compatible) +worldedit.deserialize_old = function(originpos, value) + --obtain the node table + local count = 0 + local get_tables = loadstring(value) + if get_tables == nil then --error loading value + return count + end + local tables = get_tables() + + --transform the node table into an array of nodes + for i = 1, #tables do + for j, v in pairs(tables[i]) do + if type(v) == "table" then + tables[i][j] = tables[v[1]] + end + end + end + + --load the node array + local env = minetest.env + for i, v in ipairs(tables[1]) do + local pos = v[1] + pos.x, pos.y, pos.z = originpos.x + pos.x, originpos.y + pos.y, originpos.z + pos.z + env:add_node(pos, v[2]) + count = count + 1 + end + return count end \ No newline at end of file diff --git a/init.lua b/init.lua index 9c72ac1..64e14b5 100644 --- a/init.lua +++ b/init.lua @@ -1,8 +1,5 @@ minetest.register_privilege("worldedit", "Can use WorldEdit commands") ---wip: check to make sure player positions are set before doing editing ---wip; fix meseconedit to export to new WorldEdit format - worldedit = {} worldedit.set_pos = {} @@ -312,7 +309,12 @@ minetest.register_chatcommand("/load", { local value = file:read("*a") file:close() - local count = worldedit.deserialize(pos1, value) + local count + if value:find("{") then --old WorldEdit format + count = worldedit.deserialize_old(pos1, value) + else --new WorldEdit format + count = worldedit.deserialize(pos1, value) + end minetest.chat_send_player(name, count .. " nodes loaded") end,