1 --- Schematic serialization and deserialiation.
\r
2 -- @module worldedit.serialization
\r
4 worldedit.LATEST_SERIALIZATION_VERSION = 5
\r
5 local LATEST_SERIALIZATION_HEADER = worldedit.LATEST_SERIALIZATION_VERSION .. ":"
\r
9 Serialization version history:
\r
10 1: Original format. Serialized Lua table with a weird linked format...
\r
11 2: Position and node seperated into sub-tables in fields `1` and `2`.
\r
12 3: List of nodes, one per line, with fields seperated by spaces.
\r
13 Format: <X> <Y> <Z> <Name> <Param1> <Param2>
\r
14 4: Serialized Lua table containing a list of nodes with `x`, `y`, `z`,
\r
15 `name`, `param1`, `param2`, and `meta` fields.
\r
16 5: Added header and made `param1`, `param2`, and `meta` fields optional.
\r
17 Header format: <Version>,<ExtraHeaderField1>,...:<Content>
\r
21 --- Reads the header of serialized data.
\r
22 -- @param value Serialized WorldEdit data.
\r
23 -- @return The version as a positive natural number, or 0 for unknown versions.
\r
24 -- @return Extra header fields as a list of strings, or nil if not supported.
\r
25 -- @return Content (data after header).
\r
26 function worldedit.read_header(value)
\r
27 if value:find("^[0-9]+[%-:]") then
\r
28 local header_end = value:find(":", 1, true)
\r
29 local header = value:sub(1, header_end - 1):split(",")
\r
30 local version = tonumber(header[1])
\r
31 table.remove(header, 1)
\r
32 local content = value:sub(header_end + 1)
\r
33 return version, header, content
\r
35 -- Old versions that didn't include a header with a version number
\r
36 if value:find("([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)") and not value:find("%{") then -- List format
\r
37 return 3, nil, value
\r
38 elseif value:find("^[^\"']+%{%d+%}") then
\r
39 if value:find("%[\"meta\"%]") then -- Meta flat table format
\r
40 return 2, nil, value
\r
42 return 1, nil, value -- Flat table format
\r
43 elseif value:find("%{") then -- Raw nested table format
\r
44 return 4, nil, value
\r
50 --- Converts the region defined by positions `pos1` and `pos2`
\r
51 -- into a single string.
\r
52 -- @return The serialized data.
\r
53 -- @return The number of nodes serialized.
\r
54 function worldedit.serialize(pos1, pos2)
\r
55 pos1, pos2 = worldedit.sort_pos(pos1, pos2)
\r
57 worldedit.keep_loaded(pos1, pos2)
\r
59 local get_node, get_meta, hash_node_position =
\r
60 minetest.get_node, minetest.get_meta, minetest.hash_node_position
\r
62 -- Find the positions which have metadata
\r
64 local meta_positions = minetest.find_nodes_with_meta(pos1, pos2)
\r
65 for i = 1, #meta_positions do
\r
66 has_meta[hash_node_position(meta_positions[i])] = true
\r
69 local pos = {x=pos1.x, y=0, z=0}
\r
72 while pos.x <= pos2.x do
\r
74 while pos.y <= pos2.y do
\r
76 while pos.z <= pos2.z do
\r
77 local node = get_node(pos)
\r
78 if node.name ~= "air" and node.name ~= "ignore" then
\r
82 if has_meta[hash_node_position(pos)] then
\r
83 meta = get_meta(pos):to_table()
\r
85 -- Convert metadata item stacks to item strings
\r
86 for _, invlist in pairs(meta.inventory) do
\r
87 for index = 1, #invlist do
\r
88 local itemstack = invlist[index]
\r
89 if itemstack.to_string then
\r
90 invlist[index] = itemstack:to_string()
\r
101 param1 = node.param1 ~= 0 and node.param1 or nil,
\r
102 param2 = node.param2 ~= 0 and node.param2 or nil,
\r
112 -- Serialize entries
\r
113 result = minetest.serialize(result)
\r
114 return LATEST_SERIALIZATION_HEADER .. result, count
\r
118 --- Loads the schematic in `value` into a node list in the latest format.
\r
119 -- Contains code based on [table.save/table.load](http://lua-users.org/wiki/SaveTableToFile)
\r
120 -- by ChillCode, available under the MIT license.
\r
121 -- @return A node list in the latest format, or nil on failure.
\r
122 local function load_schematic(value)
\r
123 local version, header, content = worldedit.read_header(value)
\r
125 if version == 1 or version == 2 then -- Original flat table format
\r
126 local tables = minetest.deserialize(content)
\r
127 if not tables then return nil end
\r
129 -- Transform the node table into an array of nodes
\r
130 for i = 1, #tables do
\r
131 for j, v in pairs(tables[i]) do
\r
132 if type(v) == "table" then
\r
133 tables[i][j] = tables[v[1]]
\r
139 if version == 1 then --original flat table format
\r
140 for i, entry in ipairs(nodes) do
\r
141 local pos = entry[1]
\r
142 entry.x, entry.y, entry.z = pos.x, pos.y, pos.z
\r
144 local node = entry[2]
\r
145 entry.name, entry.param1, entry.param2 = node.name, node.param1, node.param2
\r
149 elseif version == 3 then -- List format
\r
150 for x, y, z, name, param1, param2 in content:gmatch(
\r
151 "([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)%s+" ..
\r
152 "([^%s]+)%s+(%d+)%s+(%d+)[^\r\n]*[\r\n]*") do
\r
153 param1, param2 = tonumber(param1), tonumber(param2)
\r
154 table.insert(nodes, {
\r
159 param1 = param1 ~= 0 and param1 or nil,
\r
160 param2 = param2 ~= 0 and param2 or nil,
\r
163 elseif version == 4 or version == 5 then -- Nested table format
\r
165 -- This is broken for larger tables in the current version of LuaJIT
\r
166 nodes = minetest.deserialize(content)
\r
168 -- XXX: This is a filthy hack that works surprisingly well - in LuaJIT, `minetest.deserialize` will fail due to the register limit
\r
170 content = content:gsub("return%s*{", "", 1):gsub("}%s*$", "", 1) -- remove the starting and ending values to leave only the node data
\r
171 local escaped = content:gsub("\\\\", "@@"):gsub("\\\"", "@@"):gsub("(\"[^\"]*\")", function(s) return string.rep("@", #s) end)
\r
172 local startpos, startpos1, endpos = 1, 1
\r
173 while true do -- go through each individual node entry (except the last)
\r
174 startpos, endpos = escaped:find("},%s*{", startpos)
\r
175 if not startpos then
\r
178 local current = content:sub(startpos1, startpos)
\r
179 local entry = minetest.deserialize("return " .. current)
\r
180 table.insert(nodes, entry)
\r
181 startpos, startpos1 = endpos, endpos
\r
183 local entry = minetest.deserialize("return " .. content:sub(startpos1)) -- process the last entry
\r
184 table.insert(nodes, entry)
\r
192 --- Determines the volume the nodes represented by string `value` would occupy
\r
193 -- if deserialized at `origin_pos`.
\r
194 -- @return Low corner position.
\r
195 -- @return High corner position.
\r
196 -- @return The number of nodes.
\r
197 function worldedit.allocate(origin_pos, value)
\r
198 local nodes = load_schematic(value)
\r
199 if not nodes then return nil end
\r
200 return worldedit.allocate_with_nodes(origin_pos, nodes)
\r
205 function worldedit.allocate_with_nodes(origin_pos, nodes)
\r
206 local huge = math.huge
\r
207 local pos1x, pos1y, pos1z = huge, huge, huge
\r
208 local pos2x, pos2y, pos2z = -huge, -huge, -huge
\r
209 local origin_x, origin_y, origin_z = origin_pos.x, origin_pos.y, origin_pos.z
\r
210 for i, entry in ipairs(nodes) do
\r
211 local x, y, z = origin_x + entry.x, origin_y + entry.y, origin_z + entry.z
\r
212 if x < pos1x then pos1x = x end
\r
213 if y < pos1y then pos1y = y end
\r
214 if z < pos1z then pos1z = z end
\r
215 if x > pos2x then pos2x = x end
\r
216 if y > pos2y then pos2y = y end
\r
217 if z > pos2z then pos2z = z end
\r
219 local pos1 = {x=pos1x, y=pos1y, z=pos1z}
\r
220 local pos2 = {x=pos2x, y=pos2y, z=pos2z}
\r
221 return pos1, pos2, #nodes
\r
225 --- Loads the nodes represented by string `value` at position `origin_pos`.
\r
226 -- @return The number of nodes deserialized.
\r
227 function worldedit.deserialize(origin_pos, value)
\r
228 local nodes = load_schematic(value)
\r
229 if not nodes then return nil end
\r
230 if #nodes == 0 then return #nodes end
\r
232 local pos1, pos2 = worldedit.allocate_with_nodes(origin_pos, nodes)
\r
233 worldedit.keep_loaded(pos1, pos2)
\r
235 local origin_x, origin_y, origin_z = origin_pos.x, origin_pos.y, origin_pos.z
\r
237 local add_node, get_meta = minetest.add_node, minetest.get_meta
\r
238 for i, entry in ipairs(nodes) do
\r
239 entry.x, entry.y, entry.z = origin_x + entry.x, origin_y + entry.y, origin_z + entry.z
\r
240 -- Entry acts as both position and node
\r
241 add_node(entry, entry)
\r
243 get_meta(entry):from_table(entry.meta)
\r