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
117 local function deserialize_workaround(content)
\r
119 if not minetest.global_exists("jit") then
\r
120 nodes = minetest.deserialize(content, true)
\r
121 elseif not content:find("return%s*{") then
\r
122 -- The data doesn't look like we expect it to so we can't apply the workaround.
\r
123 -- hope for the best
\r
124 minetest.log("warning", "WorldEdit: deserializing data but can't apply LuaJIT workaround")
\r
125 nodes = minetest.deserialize(content, true)
\r
127 -- XXX: This is a filthy hack that works surprisingly well
\r
128 -- in LuaJIT, `minetest.deserialize` will fail due to the register limit
\r
131 local startreturn, endreturn = content:find("return%s*{")
\r
132 local header = content:sub(1, startreturn-1)
\r
134 content = content:sub(endreturn+1):gsub("}%s*$", "", 1) -- remove the starting and ending values to leave only the node data
\r
135 -- remove string contents strings while preserving their length
\r
136 local escaped = content:gsub("\\\\", "@@"):gsub("\\\"", "@@"):gsub("(\"[^\"]*\")", function(s) return string.rep("@", #s) end)
\r
137 local startpos, startpos1 = 1, 1
\r
139 while true do -- go through each individual node entry (except the last)
\r
140 startpos, endpos = escaped:find("}%s*,%s*{", startpos)
\r
141 if not startpos then
\r
144 local current = content:sub(startpos1, startpos)
\r
145 local entry = minetest.deserialize(header .. " return " .. current, true)
\r
146 table.insert(nodes, entry)
\r
147 startpos, startpos1 = endpos, endpos
\r
149 local entry = minetest.deserialize("return " .. content:sub(startpos1), true) -- process the last entry
\r
150 table.insert(nodes, entry)
\r
155 --- Loads the schematic in `value` into a node list in the latest format.
\r
156 -- @return A node list in the latest format, or nil on failure.
\r
157 local function load_schematic(value)
\r
158 local version, header, content = worldedit.read_header(value)
\r
160 if version == 1 or version == 2 then -- Original flat table format
\r
161 local tables = minetest.deserialize(content, true)
\r
162 if not tables then return nil end
\r
164 -- Transform the node table into an array of nodes
\r
165 for i = 1, #tables do
\r
166 for j, v in pairs(tables[i]) do
\r
167 if type(v) == "table" then
\r
168 tables[i][j] = tables[v[1]]
\r
174 if version == 1 then --original flat table format
\r
175 for i, entry in ipairs(nodes) do
\r
176 local pos = entry[1]
\r
177 entry.x, entry.y, entry.z = pos.x, pos.y, pos.z
\r
179 local node = entry[2]
\r
180 entry.name, entry.param1, entry.param2 = node.name, node.param1, node.param2
\r
184 elseif version == 3 then -- List format
\r
185 for x, y, z, name, param1, param2 in content:gmatch(
\r
186 "([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)%s+" ..
\r
187 "([^%s]+)%s+(%d+)%s+(%d+)[^\r\n]*[\r\n]*") do
\r
188 param1, param2 = tonumber(param1), tonumber(param2)
\r
189 table.insert(nodes, {
\r
194 param1 = param1 ~= 0 and param1 or nil,
\r
195 param2 = param2 ~= 0 and param2 or nil,
\r
198 elseif version == 4 or version == 5 then -- Nested table format
\r
199 nodes = deserialize_workaround(content)
\r
206 --- Determines the volume the nodes represented by string `value` would occupy
\r
207 -- if deserialized at `origin_pos`.
\r
208 -- @return Low corner position.
\r
209 -- @return High corner position.
\r
210 -- @return The number of nodes.
\r
211 function worldedit.allocate(origin_pos, value)
\r
212 local nodes = load_schematic(value)
\r
213 if not nodes or #nodes == 0 then return nil end
\r
214 return worldedit.allocate_with_nodes(origin_pos, nodes)
\r
219 function worldedit.allocate_with_nodes(origin_pos, nodes)
\r
220 local huge = math.huge
\r
221 local pos1x, pos1y, pos1z = huge, huge, huge
\r
222 local pos2x, pos2y, pos2z = -huge, -huge, -huge
\r
223 local origin_x, origin_y, origin_z = origin_pos.x, origin_pos.y, origin_pos.z
\r
224 for i, entry in ipairs(nodes) do
\r
225 local x, y, z = origin_x + entry.x, origin_y + entry.y, origin_z + entry.z
\r
226 if x < pos1x then pos1x = x end
\r
227 if y < pos1y then pos1y = y end
\r
228 if z < pos1z then pos1z = z end
\r
229 if x > pos2x then pos2x = x end
\r
230 if y > pos2y then pos2y = y end
\r
231 if z > pos2z then pos2z = z end
\r
233 local pos1 = {x=pos1x, y=pos1y, z=pos1z}
\r
234 local pos2 = {x=pos2x, y=pos2y, z=pos2z}
\r
235 return pos1, pos2, #nodes
\r
239 --- Loads the nodes represented by string `value` at position `origin_pos`.
\r
240 -- @return The number of nodes deserialized.
\r
241 function worldedit.deserialize(origin_pos, value)
\r
242 local nodes = load_schematic(value)
\r
243 if not nodes then return nil end
\r
244 if #nodes == 0 then return #nodes end
\r
246 local pos1, pos2 = worldedit.allocate_with_nodes(origin_pos, nodes)
\r
247 worldedit.keep_loaded(pos1, pos2)
\r
249 local origin_x, origin_y, origin_z = origin_pos.x, origin_pos.y, origin_pos.z
\r
251 local add_node, get_meta = minetest.add_node, minetest.get_meta
\r
252 for i, entry in ipairs(nodes) do
\r
253 entry.x, entry.y, entry.z = origin_x + entry.x, origin_y + entry.y, origin_z + entry.z
\r
254 -- Entry acts as both position and node
\r
255 add_node(entry, entry)
\r
257 get_meta(entry):from_table(entry.meta)
\r