]> git.lizzy.rs Git - worldedit.git/blobdiff - worldedit/serialization.lua
Fix //load with 0 nodes (#177)
[worldedit.git] / worldedit / serialization.lua
index 8e4923170219c5055b59d726be69a1c1dd3e046b..e796fc1477653a8afa0d64047c0734747e6970a5 100644 (file)
-worldedit = worldedit or {}\r
+--- Schematic serialization and deserialiation.\r
+-- @module worldedit.serialization\r
 \r
-dofile(minetest.get_modpath("worldedit") .. "/table_save.lua") --wip: remove dependency\r
+worldedit.LATEST_SERIALIZATION_VERSION = 5\r
+local LATEST_SERIALIZATION_HEADER = worldedit.LATEST_SERIALIZATION_VERSION .. ":"\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
+\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
+       -- 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 -- Meta flat table format\r
+                       return 2, nil, value\r
+               end\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 pos1, pos2\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)\r
-       local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
+\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 env = minetest.env\r
        while pos.x <= pos2.x do\r
                pos.y = pos1.y\r
                while pos.y <= pos2.y do\r
                        pos.z = pos1.z\r
                        while pos.z <= pos2.z do\r
-                               local node = env:get_node(pos)\r
+                               local node = get_node(pos)\r
                                if node.name ~= "air" and node.name ~= "ignore" then\r
                                        count = count + 1\r
-                                       result[count] = pos.x - pos1.x .. " " .. pos.y - pos1.y .. " " .. pos.z - pos1.z .. " " .. node.name .. " " .. node.param1 .. " " .. node.param2\r
+\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
+                                       result[count] = {\r
+                                               x = pos.x - pos1.x,\r
+                                               y = pos.y - pos1.y,\r
+                                               z = pos.z - pos1.z,\r
+                                               name = node.name,\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
                                pos.z = pos.z + 1\r
                        end\r
@@ -41,206 +109,140 @@ worldedit.serialize = function(pos1, pos2)
                end\r
                pos.x = pos.x + 1\r
        end\r
-       result = table.concat(result, "\n") --join all node entries into single 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
-worldedit.allocate = function(originpos, value)\r
-       local huge = math.huge\r
-       local pos1 = {x=huge, y=huge, z=huge}\r
-       local pos2 = {x=-huge, y=-huge, z=-huge}\r
-       local originx, originy, originz = originpos.x, originpos.y, originpos.z\r
-       local count = 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
-               x, y, z = originx + tonumber(x), originy + tonumber(y), originz + tonumber(z)\r
-               if x < pos1.x then\r
-                       pos1.x = x\r
-               end\r
-               if y < pos1.y then\r
-                       pos1.y = y\r
-               end\r
-               if z < pos1.z then\r
-                       pos1.z = z\r
-               end\r
-               if x > pos2.x then\r
-                       pos2.x = x\r
+\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
+                                       tables[i][j] = tables[v[1]]\r
+                               end\r
+                       end\r
                end\r
-               if y > pos2.y then\r
-                       pos2.y = y\r
+               nodes = tables[1]\r
+\r
+               if version == 1 then --original flat table format\r
+                       for i, entry in ipairs(nodes) do\r
+                               local pos = entry[1]\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
-               if z > pos2.z then\r
-                       pos2.z = z\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 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 entry = minetest.deserialize("return " .. content:sub(startpos1)) -- process the last entry\r
+                       table.insert(nodes, entry)\r
                end\r
-               count = count + 1\r
+       else\r
+               return nil\r
        end\r
-       return pos1, pos2, count\r
+       return nodes\r
 end\r
 \r
---loads the nodes represented by string `value` at position `originpos`, returning the number of nodes deserialized\r
-worldedit.deserialize = function(originpos, value)\r
-       local pos = {x=0, y=0, z=0}\r
-       local node = {name="", param1=0, param2=0}\r
-       local originx, originy, originz = originpos.x, originpos.y, originpos.z\r
-       local count = 0\r
-       local env = minetest.env\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 = originx + tonumber(x)\r
-               pos.y = originy + tonumber(y)\r
-               pos.z = originz + tonumber(z)\r
-               node.name = name\r
-               node.param1 = param1\r
-               node.param2 = param2\r
-               env:add_node(pos, node)\r
-               count = count + 1\r
-       end\r
-       return count\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
---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
---based on [table.save/table.load](http://lua-users.org/wiki/SaveTableToFile) by ChillCode, available under the MIT license (GPL compatible)\r
-worldedit.allocate_old = function(originpos, value)\r
-       --obtain the node table\r
-       local count = 0\r
-       local get_tables = loadstring(value)\r
-       if get_tables == nil 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
 \r
+-- Internal\r
+function worldedit.allocate_with_nodes(origin_pos, nodes)\r
        local huge = math.huge\r
-       local pos1 = {x=huge, y=huge, z=huge}\r
-       local pos2 = {x=-huge, y=-huge, z=-huge}\r
-       local originx, originy, originz = originpos.x, originpos.y, originpos.z\r
-\r
-       --load the node array\r
-       for i, v in ipairs(tables[1]) do\r
-               local pos = v[1]\r
-               local x, y, z = originx - pos.x, originy - pos.y, originz - pos.z\r
-               if x < pos1.x then\r
-                       pos1.x = x\r
-               end\r
-               if y < pos1.y then\r
-                       pos1.y = y\r
-               end\r
-               if z < pos1.z then\r
-                       pos1.z = z\r
-               end\r
-               if x > pos2.x then\r
-                       pos2.x = x\r
-               end\r
-               if y > pos2.y then\r
-                       pos2.y = y\r
-               end\r
-               if z > pos2.z then\r
-                       pos2.z = z\r
-               end\r
-               count = count + 1\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
-       return pos1, pos2, count\r
+       local pos1 = {x=pos1x, y=pos1y, z=pos1z}\r
+       local pos2 = {x=pos2x, y=pos2y, z=pos2z}\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
---based on [table.save/table.load](http://lua-users.org/wiki/SaveTableToFile) by ChillCode, available under the MIT license (GPL compatible)\r
-worldedit.deserialize_old = function(originpos, value)\r
-       --obtain the node table\r
-       local count = 0\r
-       local get_tables = loadstring(value)\r
-       if get_tables == nil 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
+--- 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
-       local env = minetest.env\r
-       local originx, originy, originz = originpos.x, originpos.y, originpos.z\r
-       for i, v in ipairs(tables[1]) do\r
-               local pos = v[1]\r
-               pos.x, pos.y, pos.z = originx - pos.x, originy - pos.y, originz - pos.z\r
-               env:add_node(pos, v[2])\r
-               count = count + 1\r
-       end\r
-       return count\r
-end\r
+       local pos1, pos2 = worldedit.allocate_with_nodes(origin_pos, nodes)\r
+       worldedit.keep_loaded(pos1, pos2)\r
 \r
---saves the nodes and meta defined by positions `pos1` and `pos2` into a file, returning the number of nodes saved\r
-worldedit.metasave = function(pos1, pos2, file) --wip: simply work with strings instead of doing IO\r
-       local path = minetest.get_worldpath() .. "/schems"\r
-       local filename = path .. "/" .. file .. ".wem"\r
-       os.execute("mkdir \"" .. path .. "\"") --create directory if it does not already exist\r
-       local rows = {}\r
-       local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
-       local pos = {x=pos1.x, y=0, z=0}\r
+       local origin_x, origin_y, origin_z = origin_pos.x, origin_pos.y, origin_pos.z\r
        local count = 0\r
-       local result = {}\r
-       local env = minetest.env\r
-       while pos.x <= pos2.x do\r
-               pos.y = pos1.y\r
-               while pos.y <= pos2.y do\r
-                       pos.z = pos1.z\r
-                       while pos.z <= pos2.z do\r
-                               local node = env:get_node(pos)\r
-                               if node.name ~= "air" and node.name ~= "ignore" then\r
-                                       count = count + 1\r
-                                       local row = {\r
-                                               x = pos.x-pos1.x,\r
-                                               y = pos.y-pos1.y,\r
-                                               z = pos.z-pos1.z,\r
-                                               name = node.name,\r
-                                               param1 = node.param1,\r
-                                               param2 = node.param2,\r
-                                               meta = env:get_meta(pos):to_table(),\r
-                                       }\r
-                                       table.insert(rows, row)\r
-                               end\r
-                               pos.z = pos.z + 1\r
-                       end\r
-                       pos.y = pos.y + 1\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
-               pos.x = pos.x + 1\r
        end\r
-       local err = table.save(rows,filename)\r
-       if err then return _,err end\r
-       return count\r
+       return #nodes\r
 end\r
 \r
---loads the nodes and meta from `file` to position `pos1`, returning the number of nodes loaded\r
-worldedit.metaload = function(pos1, file) --wip: simply work with strings instead of doing IO\r
-       local filename = minetest.get_worldpath() .. "/schems/" .. file .. ".wem"\r
-       local rows, err = table.load(filename)\r
-       if err then return _,err end\r
-       local pos = {x=0, y=0, z=0}\r
-       local node = {name="", param1=0, param2=0}\r
-       local count = 0\r
-       local env = minetest.env\r
-       for i,row in pairs(rows) do\r
-               pos.x = pos1.x + tonumber(row.x)\r
-               pos.y = pos1.y + tonumber(row.y)\r
-               pos.z = pos1.z + tonumber(row.z)\r
-               node.name = row.name\r
-               node.param1 = row.param1\r
-               node.param2 = row.param2\r
-               env:add_node(pos, node)\r
-               env:get_meta(pos):from_table(row.meta)\r
-               count = count + 1\r
-       end\r
-       return count\r
-end
\ No newline at end of file