]> git.lizzy.rs Git - worldedit.git/blobdiff - worldedit/serialization.lua
Fix //load with 0 nodes (#177)
[worldedit.git] / worldedit / serialization.lua
index e0d960d3ee7b2845aec3a5d6c7c4979211ef3bd9..e796fc1477653a8afa0d64047c0734747e6970a5 100644 (file)
@@ -1,48 +1,74 @@
-worldedit = worldedit or {}\r
-local minetest = minetest --local copy of global\r
-\r
---modifies positions `pos1` and `pos2` so that each component of `pos1` is less than or equal to its corresponding conent of `pos2`, returning two new positions\r
-worldedit.sort_pos = function(pos1, pos2)\r
-       pos1 = {x=pos1.x, y=pos1.y, z=pos1.z}\r
-       pos2 = {x=pos2.x, y=pos2.y, z=pos2.z}\r
-       if pos1.x > pos2.x then\r
-               pos2.x, pos1.x = pos1.x, pos2.x\r
-       end\r
-       if pos1.y > pos2.y then\r
-               pos2.y, pos1.y = pos1.y, pos2.y\r
+--- Schematic serialization and deserialiation.\r
+-- @module worldedit.serialization\r
+\r
+worldedit.LATEST_SERIALIZATION_VERSION = 5\r
+local LATEST_SERIALIZATION_HEADER = worldedit.LATEST_SERIALIZATION_VERSION .. ":"\r
+\r
+\r
+--[[\r
+Serialization version history:\r
+  1: Original format.  Serialized Lua table with a weird linked format...\r
+  2: Position and node seperated into sub-tables in fields `1` and `2`.\r
+  3: List of nodes, one per line, with fields seperated by spaces.\r
+      Format: <X> <Y> <Z> <Name> <Param1> <Param2>\r
+  4: Serialized Lua table containing a list of nodes with `x`, `y`, `z`,\r
+      `name`, `param1`, `param2`, and `meta` fields.\r
+  5: Added header and made `param1`, `param2`, and `meta` fields optional.\r
+      Header format: <Version>,<ExtraHeaderField1>,...:<Content>\r
+--]]\r
+\r
+\r
+--- Reads the header of serialized data.\r
+-- @param value Serialized WorldEdit data.\r
+-- @return The version as a positive natural number, or 0 for unknown versions.\r
+-- @return Extra header fields as a list of strings, or nil if not supported.\r
+-- @return Content (data after header).\r
+function worldedit.read_header(value)\r
+       if value:find("^[0-9]+[%-:]") then\r
+               local header_end = value:find(":", 1, true)\r
+               local header = value:sub(1, header_end - 1):split(",")\r
+               local version = tonumber(header[1])\r
+               table.remove(header, 1)\r
+               local content = value:sub(header_end + 1)\r
+               return version, header, content\r
        end\r
-       if pos1.z > pos2.z then\r
-               pos2.z, pos1.z = pos1.z, pos2.z\r
-       end\r
-       return pos1, pos2\r
-end\r
-\r
---determines the version of serialized data `value`, returning the version as a positive integer or 0 for unknown versions\r
-worldedit.valueversion = function(value)\r
-       if value:find("([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)") and not value:find("%{") then --previous list format\r
-               return 3\r
+       -- Old versions that didn't include a header with a version number\r
+       if value:find("([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)") and not value:find("%{") then -- List format\r
+               return 3, nil, value\r
        elseif value:find("^[^\"']+%{%d+%}") then\r
-               if value:find("%[\"meta\"%]") then --previous meta flat table format\r
-                       return 2\r
+               if value:find("%[\"meta\"%]") then -- Meta flat table format\r
+                       return 2, nil, value\r
                end\r
-               return 1 --original flat table format\r
-       elseif value:find("%{") then --current nested table format\r
-               return 4\r
+               return 1, nil, value -- Flat table format\r
+       elseif value:find("%{") then -- Raw nested table format\r
+               return 4, nil, value\r
        end\r
-       return 0 --unknown format\r
+       return nil\r
 end\r
 \r
---converts the region defined by positions `pos1` and `pos2` into a single string, returning the serialized data and the number of nodes serialized\r
-worldedit.serialize = function(pos1, pos2) --wip: check for ItemStacks and whether they can be serialized\r
-       --make area stay loaded\r
-       local manip = minetest.get_voxel_manip()\r
-       manip:read_from_map(pos1, pos2)\r
 \r
-       local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
+--- Converts the region defined by positions `pos1` and `pos2`\r
+-- into a single string.\r
+-- @return The serialized data.\r
+-- @return The number of nodes serialized.\r
+function worldedit.serialize(pos1, pos2)\r
+       pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
+\r
+       worldedit.keep_loaded(pos1, pos2)\r
+\r
+       local get_node, get_meta, hash_node_position =\r
+               minetest.get_node, minetest.get_meta, minetest.hash_node_position\r
+\r
+       -- Find the positions which have metadata\r
+       local has_meta = {}\r
+       local meta_positions = minetest.find_nodes_with_meta(pos1, pos2)\r
+       for i = 1, #meta_positions do\r
+               has_meta[hash_node_position(meta_positions[i])] = true\r
+       end\r
+\r
        local pos = {x=pos1.x, y=0, z=0}\r
        local count = 0\r
        local result = {}\r
-       local get_node, get_meta = minetest.get_node, minetest.get_meta\r
        while pos.x <= pos2.x do\r
                pos.y = pos1.y\r
                while pos.y <= pos2.y do\r
@@ -51,12 +77,19 @@ worldedit.serialize = function(pos1, pos2) --wip: check for ItemStacks and wheth
                                local node = get_node(pos)\r
                                if node.name ~= "air" and node.name ~= "ignore" then\r
                                        count = count + 1\r
-                                       local meta = get_meta(pos):to_table()\r
 \r
-                                       --convert metadata itemstacks to itemstrings\r
-                                       for name, inventory in pairs(meta.inventory) do\r
-                                               for index, stack in ipairs(inventory) do\r
-                                                       inventory[index] = stack.to_string and stack:to_string() or stack\r
+                                       local meta\r
+                                       if has_meta[hash_node_position(pos)] then\r
+                                               meta = get_meta(pos):to_table()\r
+\r
+                                               -- Convert metadata item stacks to item strings\r
+                                               for _, invlist in pairs(meta.inventory) do\r
+                                                       for index = 1, #invlist do\r
+                                                               local itemstack = invlist[index]\r
+                                                               if itemstack.to_string then\r
+                                                                       invlist[index] = itemstack:to_string()\r
+                                                               end\r
+                                                       end\r
                                                end\r
                                        end\r
 \r
@@ -65,8 +98,8 @@ worldedit.serialize = function(pos1, pos2) --wip: check for ItemStacks and wheth
                                                y = pos.y - pos1.y,\r
                                                z = pos.z - pos1.z,\r
                                                name = node.name,\r
-                                               param1 = node.param1,\r
-                                               param2 = node.param2,\r
+                                               param1 = node.param1 ~= 0 and node.param1 or nil,\r
+                                               param2 = node.param2 ~= 0 and node.param2 or nil,\r
                                                meta = meta,\r
                                        }\r
                                end\r
@@ -76,28 +109,24 @@ worldedit.serialize = function(pos1, pos2) --wip: check for ItemStacks and wheth
                end\r
                pos.x = pos.x + 1\r
        end\r
-       result = minetest.serialize(result) --convert entries to a string\r
-       return result, count\r
+       -- Serialize entries\r
+       result = minetest.serialize(result)\r
+       return LATEST_SERIALIZATION_HEADER .. result, count\r
 end\r
 \r
---determines the volume the nodes represented by string `value` would occupy if deserialized at `originpos`, returning the two corner positions and the number of nodes\r
---contains code based on [table.save/table.load](http://lua-users.org/wiki/SaveTableToFile) by ChillCode, available under the MIT license (GPL compatible)\r
-worldedit.allocate = function(originpos, value)\r
-       local huge = math.huge\r
-       local pos1x, pos1y, pos1z = huge, huge, huge\r
-       local pos2x, pos2y, pos2z = -huge, -huge, -huge\r
-       local originx, originy, originz = originpos.x, originpos.y, originpos.z\r
-       local count = 0\r
-       local version = worldedit.valueversion(value)\r
-       if version == 1 or version == 2 then --flat table format\r
-               --obtain the node table\r
-               local get_tables = loadstring(value)\r
-               if get_tables then --error loading value\r
-                       return originpos, originpos, count\r
-               end\r
-               local tables = get_tables()\r
 \r
-               --transform the node table into an array of nodes\r
+--- Loads the schematic in `value` into a node list in the latest format.\r
+-- Contains code based on [table.save/table.load](http://lua-users.org/wiki/SaveTableToFile)\r
+-- by ChillCode, available under the MIT license.\r
+-- @return A node list in the latest format, or nil on failure.\r
+local function load_schematic(value)\r
+       local version, header, content = worldedit.read_header(value)\r
+       local nodes = {}\r
+       if version == 1 or version == 2 then -- Original flat table format\r
+               local tables = minetest.deserialize(content)\r
+               if not tables then return nil end\r
+\r
+               -- Transform the node table into an array of nodes\r
                for i = 1, #tables do\r
                        for j, v in pairs(tables[i]) do\r
                                if type(v) == "table" then\r
@@ -105,164 +134,115 @@ worldedit.allocate = function(originpos, value)
                                end\r
                        end\r
                end\r
-               local nodes = tables[1]\r
+               nodes = tables[1]\r
 \r
-               --check the node array\r
-               count = #nodes\r
                if version == 1 then --original flat table format\r
-                       for index = 1, count do\r
-                               local entry = nodes[index]\r
+                       for i, entry in ipairs(nodes) do\r
                                local pos = entry[1]\r
-                               local x, y, z = originx - pos.x, originy - pos.y, originz - pos.z\r
-                               if x < pos1x then pos1x = x end\r
-                               if y < pos1y then pos1y = y end\r
-                               if z < pos1z then pos1z = z end\r
-                               if x > pos2x then pos2x = x end\r
-                               if y > pos2y then pos2y = y end\r
-                               if z > pos2z then pos2z = z end\r
-                       end\r
-               else --previous meta flat table format\r
-                       for index = 1, count do\r
-                               local entry = nodes[index]\r
-                               local x, y, z = originx - entry.x, originy - entry.y, originz - entry.z\r
-                               if x < pos1x then pos1x = x end\r
-                               if y < pos1y then pos1y = y end\r
-                               if z < pos1z then pos1z = z end\r
-                               if x > pos2x then pos2x = x end\r
-                               if y > pos2y then pos2y = y end\r
-                               if z > pos2z then pos2z = z end\r
+                               entry.x, entry.y, entry.z = pos.x, pos.y, pos.z\r
+                               entry[1] = nil\r
+                               local node = entry[2]\r
+                               entry.name, entry.param1, entry.param2 = node.name, node.param1, node.param2\r
+                               entry[2] = nil\r
                        end\r
                end\r
-       elseif version == 3 then --previous list format\r
-               for x, y, z, name, param1, param2 in value:gmatch("([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)%s+([^%s]+)%s+(%d+)%s+(%d+)[^\r\n]*[\r\n]*") do --match node entries\r
-                       x, y, z = originx + tonumber(x), originy + tonumber(y), originz + tonumber(z)\r
-                       if x < pos1x then pos1x = x end\r
-                       if y < pos1y then pos1y = y end\r
-                       if z < pos1z then pos1z = z end\r
-                       if x > pos2x then pos2x = x end\r
-                       if y > pos2y then pos2y = y end\r
-                       if z > pos2z then pos2z = z end\r
-                       count = count + 1\r
+       elseif version == 3 then -- List format\r
+               for x, y, z, name, param1, param2 in content:gmatch(\r
+                               "([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)%s+" ..\r
+                               "([^%s]+)%s+(%d+)%s+(%d+)[^\r\n]*[\r\n]*") do\r
+                       param1, param2 = tonumber(param1), tonumber(param2)\r
+                       table.insert(nodes, {\r
+                               x = tonumber(x),\r
+                               y = tonumber(y),\r
+                               z = tonumber(z),\r
+                               name = name,\r
+                               param1 = param1 ~= 0 and param1 or nil,\r
+                               param2 = param2 ~= 0 and param2 or nil,\r
+                       })\r
                end\r
-       elseif version == 4 then --current nested table format\r
-               --wip: this is a filthy hack that works surprisingly well\r
-               value = value:gsub("return%s*{", "", 1):gsub("}%s*$", "", 1)\r
-               local escaped = value:gsub("\\\\", "@@"):gsub("\\\"", "@@"):gsub("(\"[^\"]*\")", function(s) return string.rep("@", #s) end)\r
-               local startpos, startpos1, endpos = 1, 1\r
-               local nodes = {}\r
-               while true do\r
-                       startpos, endpos = escaped:find("},%s*{", startpos)\r
-                       if not startpos then\r
-                               break\r
+       elseif version == 4 or version == 5 then -- Nested table format\r
+               if not jit then\r
+                       -- This is broken for larger tables in the current version of LuaJIT\r
+                       nodes = minetest.deserialize(content)\r
+               else\r
+                       -- XXX: This is a filthy hack that works surprisingly well - in LuaJIT, `minetest.deserialize` will fail due to the register limit\r
+                       nodes = {}\r
+                       content = content:gsub("return%s*{", "", 1):gsub("}%s*$", "", 1) -- remove the starting and ending values to leave only the node data\r
+                       local escaped = content:gsub("\\\\", "@@"):gsub("\\\"", "@@"):gsub("(\"[^\"]*\")", function(s) return string.rep("@", #s) end)\r
+                       local startpos, startpos1, endpos = 1, 1\r
+                       while true do -- go through each individual node entry (except the last)\r
+                               startpos, endpos = escaped:find("},%s*{", startpos)\r
+                               if not startpos then\r
+                                       break\r
+                               end\r
+                               local current = content:sub(startpos1, startpos)\r
+                               local entry = minetest.deserialize("return " .. current)\r
+                               table.insert(nodes, entry)\r
+                               startpos, startpos1 = endpos, endpos\r
                        end\r
-                       local current = value:sub(startpos1, startpos)\r
-                       table.insert(nodes, minetest.deserialize("return " .. current))\r
-                       startpos, startpos1 = endpos, endpos\r
+                       local entry = minetest.deserialize("return " .. content:sub(startpos1)) -- process the last entry\r
+                       table.insert(nodes, entry)\r
                end\r
-               table.insert(nodes, minetest.deserialize("return " .. value:sub(startpos1)))\r
+       else\r
+               return nil\r
+       end\r
+       return nodes\r
+end\r
 \r
-               --local nodes = minetest.deserialize(value) --wip: this is broken for larger tables in the current version of LuaJIT\r
+--- Determines the volume the nodes represented by string `value` would occupy\r
+-- if deserialized at `origin_pos`.\r
+-- @return Low corner position.\r
+-- @return High corner position.\r
+-- @return The number of nodes.\r
+function worldedit.allocate(origin_pos, value)\r
+       local nodes = load_schematic(value)\r
+       if not nodes then return nil end\r
+       return worldedit.allocate_with_nodes(origin_pos, nodes)\r
+end\r
 \r
-               count = #nodes\r
-               for index = 1, count do\r
-                       local entry = nodes[index]\r
-                       x, y, z = originx + entry.x, originy + entry.y, originz + entry.z\r
-                       if x < pos1x then pos1x = x end\r
-                       if y < pos1y then pos1y = y end\r
-                       if z < pos1z then pos1z = z end\r
-                       if x > pos2x then pos2x = x end\r
-                       if y > pos2y then pos2y = y end\r
-                       if z > pos2z then pos2z = z end\r
-               end\r
+\r
+-- Internal\r
+function worldedit.allocate_with_nodes(origin_pos, nodes)\r
+       local huge = math.huge\r
+       local pos1x, pos1y, pos1z = huge, huge, huge\r
+       local pos2x, pos2y, pos2z = -huge, -huge, -huge\r
+       local origin_x, origin_y, origin_z = origin_pos.x, origin_pos.y, origin_pos.z\r
+       for i, entry in ipairs(nodes) do\r
+               local x, y, z = origin_x + entry.x, origin_y + entry.y, origin_z + entry.z\r
+               if x < pos1x then pos1x = x end\r
+               if y < pos1y then pos1y = y end\r
+               if z < pos1z then pos1z = z end\r
+               if x > pos2x then pos2x = x end\r
+               if y > pos2y then pos2y = y end\r
+               if z > pos2z then pos2z = z end\r
        end\r
        local pos1 = {x=pos1x, y=pos1y, z=pos1z}\r
        local pos2 = {x=pos2x, y=pos2y, z=pos2z}\r
-       return pos1, pos2, count\r
+       return pos1, pos2, #nodes\r
 end\r
 \r
---loads the nodes represented by string `value` at position `originpos`, returning the number of nodes deserialized\r
---contains code based on [table.save/table.load](http://lua-users.org/wiki/SaveTableToFile) by ChillCode, available under the MIT license (GPL compatible)\r
-worldedit.deserialize = function(originpos, value) --wip: use voxelmanip to make sure the blocks are loaded\r
-       local originx, originy, originz = originpos.x, originpos.y, originpos.z\r
-       local count = 0\r
-       local add_node, get_meta = minetest.add_node, minetest.get_meta\r
-       local version = worldedit.valueversion(value)\r
-       if version == 1 or version == 2 then --original flat table format\r
-               --obtain the node table\r
-               local get_tables = loadstring(value)\r
-               if not get_tables then --error loading value\r
-                       return count\r
-               end\r
-               local tables = get_tables()\r
 \r
-               --transform the node table into an array of nodes\r
-               for i = 1, #tables do\r
-                       for j, v in pairs(tables[i]) do\r
-                               if type(v) == "table" then\r
-                                       tables[i][j] = tables[v[1]]\r
-                               end\r
-                       end\r
-               end\r
-               local nodes = tables[1]\r
+--- Loads the nodes represented by string `value` at position `origin_pos`.\r
+-- @return The number of nodes deserialized.\r
+function worldedit.deserialize(origin_pos, value)\r
+       local nodes = load_schematic(value)\r
+       if not nodes then return nil end\r
+       if #nodes == 0 then return #nodes end\r
 \r
-               --load the node array\r
-               count = #nodes\r
-               if version == 1 then --original flat table format\r
-                       for index = 1, count do\r
-                               local entry = nodes[index]\r
-                               local pos = entry[1]\r
-                               pos.x, pos.y, pos.z = originx - pos.x, originy - pos.y, originz - pos.z\r
-                               add_node(pos, entry[2])\r
-                       end\r
-               else --previous meta flat table format\r
-                       for index = 1, #nodes do\r
-                               local entry = nodes[index]\r
-                               entry.x, entry.y, entry.z = originx + entry.x, originy + entry.y, originz + entry.z\r
-                               add_node(entry, entry) --entry acts both as position and as node\r
-                               get_meta(entry):from_table(entry.meta)\r
-                       end\r
-               end\r
-       elseif version == 3 then --previous list format\r
-               local pos = {x=0, y=0, z=0}\r
-               local node = {name="", param1=0, param2=0}\r
-               for x, y, z, name, param1, param2 in value:gmatch("([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)%s+([^%s]+)%s+(%d+)%s+(%d+)[^\r\n]*[\r\n]*") do --match node entries\r
-                       pos.x, pos.y, pos.z = originx + tonumber(x), originy + tonumber(y), originz + tonumber(z)\r
-                       node.name, node.param1, node.param2 = name, param1, param2\r
-                       add_node(pos, node)\r
-                       count = count + 1\r
-               end\r
-       elseif version == 4 then --current nested table format\r
-               --wip: this is a filthy hack that works surprisingly well\r
-               value = value:gsub("return%s*{", "", 1):gsub("}%s*$", "", 1)\r
-               local escaped = value:gsub("\\\\", "@@"):gsub("\\\"", "@@"):gsub("(\"[^\"]*\")", function(s) return string.rep("@", #s) end)\r
-               local startpos, startpos1, endpos = 1, 1\r
-               local nodes = {}\r
-               while true do\r
-                       startpos, endpos = escaped:find("},%s*{", startpos)\r
-                       if not startpos then\r
-                               break\r
-                       end\r
-                       local current = value:sub(startpos1, startpos)\r
-                       table.insert(nodes, minetest.deserialize("return " .. current))\r
-                       startpos, startpos1 = endpos, endpos\r
-               end\r
-               table.insert(nodes, minetest.deserialize("return " .. value:sub(startpos1)))\r
+       local pos1, pos2 = worldedit.allocate_with_nodes(origin_pos, nodes)\r
+       worldedit.keep_loaded(pos1, pos2)\r
 \r
-               --local nodes = minetest.deserialize(value) --wip: this is broken for larger tables in the current version of LuaJIT\r
-\r
-               --load the nodes\r
-               count = #nodes\r
-               for index = 1, count do\r
-                       local entry = nodes[index]\r
-                       entry.x, entry.y, entry.z = originx + entry.x, originy + entry.y, originz + entry.z\r
-                       add_node(entry, entry) --entry acts both as position and as node\r
-               end\r
-\r
-               --load the metadata\r
-               for index = 1, count do\r
-                       local entry = nodes[index]\r
+       local origin_x, origin_y, origin_z = origin_pos.x, origin_pos.y, origin_pos.z\r
+       local count = 0\r
+       local add_node, get_meta = minetest.add_node, minetest.get_meta\r
+       for i, entry in ipairs(nodes) do\r
+               entry.x, entry.y, entry.z = origin_x + entry.x, origin_y + entry.y, origin_z + entry.z\r
+               -- Entry acts as both position and node\r
+               add_node(entry, entry)\r
+               if entry.meta then\r
                        get_meta(entry):from_table(entry.meta)\r
                end\r
        end\r
-       return count\r
+       return #nodes\r
 end\r
+\r