]> git.lizzy.rs Git - worldedit.git/blob - worldedit/serialization.lua
Fix schematics with extra headers to parse properly
[worldedit.git] / worldedit / serialization.lua
1 --- Schematic serialization and deserialiation.\r
2 -- @module worldedit.serialization\r
3 \r
4 worldedit.LATEST_SERIALIZATION_VERSION = 5\r
5 local LATEST_SERIALIZATION_HEADER = worldedit.LATEST_SERIALIZATION_VERSION .. ":"\r
6 \r
7 \r
8 --[[\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
18 --]]\r
19 \r
20 \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
34         end\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
41                 end\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
45         end\r
46         return nil\r
47 end\r
48 \r
49 \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
56 \r
57         worldedit.keep_loaded(pos1, pos2)\r
58 \r
59         local get_node, get_meta, hash_node_position =\r
60                 minetest.get_node, minetest.get_meta, minetest.hash_node_position\r
61 \r
62         -- Find the positions which have metadata\r
63         local has_meta = {}\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
67         end\r
68 \r
69         local pos = {x=pos1.x, y=0, z=0}\r
70         local count = 0\r
71         local result = {}\r
72         while pos.x <= pos2.x do\r
73                 pos.y = pos1.y\r
74                 while pos.y <= pos2.y do\r
75                         pos.z = pos1.z\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
79                                         count = count + 1\r
80 \r
81                                         local meta\r
82                                         if has_meta[hash_node_position(pos)] then\r
83                                                 meta = get_meta(pos):to_table()\r
84 \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
91                                                                 end\r
92                                                         end\r
93                                                 end\r
94                                         end\r
95 \r
96                                         result[count] = {\r
97                                                 x = pos.x - pos1.x,\r
98                                                 y = pos.y - pos1.y,\r
99                                                 z = pos.z - pos1.z,\r
100                                                 name = node.name,\r
101                                                 param1 = node.param1 ~= 0 and node.param1 or nil,\r
102                                                 param2 = node.param2 ~= 0 and node.param2 or nil,\r
103                                                 meta = meta,\r
104                                         }\r
105                                 end\r
106                                 pos.z = pos.z + 1\r
107                         end\r
108                         pos.y = pos.y + 1\r
109                 end\r
110                 pos.x = pos.x + 1\r
111         end\r
112         -- Serialize entries\r
113         result = minetest.serialize(result)\r
114         return LATEST_SERIALIZATION_HEADER .. result, count\r
115 end\r
116 \r
117 \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
124         local nodes = {}\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
128 \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
134                                 end\r
135                         end\r
136                 end\r
137                 nodes = tables[1]\r
138 \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
143                                 entry[1] = nil\r
144                                 local node = entry[2]\r
145                                 entry.name, entry.param1, entry.param2 = node.name, node.param1, node.param2\r
146                                 entry[2] = nil\r
147                         end\r
148                 end\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
155                                 x = tonumber(x),\r
156                                 y = tonumber(y),\r
157                                 z = tonumber(z),\r
158                                 name = name,\r
159                                 param1 = param1 ~= 0 and param1 or nil,\r
160                                 param2 = param2 ~= 0 and param2 or nil,\r
161                         })\r
162                 end\r
163         elseif version == 4 or version == 5 then -- Nested table format\r
164                 if not jit then\r
165                         -- This is broken for larger tables in the current version of LuaJIT\r
166                         nodes = minetest.deserialize(content)\r
167                 else\r
168                         -- XXX: This is a filthy hack that works surprisingly well - in LuaJIT, `minetest.deserialize` will fail due to the register limit\r
169                         nodes = {}\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
176                                         break\r
177                                 end\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
182                         end\r
183                         local entry = minetest.deserialize("return " .. content:sub(startpos1)) -- process the last entry\r
184                         table.insert(nodes, entry)\r
185                 end\r
186         else\r
187                 return nil\r
188         end\r
189         return nodes\r
190 end\r
191 \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 or #nodes == 0 then return nil end\r
200         return worldedit.allocate_with_nodes(origin_pos, nodes)\r
201 end\r
202 \r
203 \r
204 -- Internal\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
218         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
222 end\r
223 \r
224 \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
231 \r
232         local pos1, pos2 = worldedit.allocate_with_nodes(origin_pos, nodes)\r
233         worldedit.keep_loaded(pos1, pos2)\r
234 \r
235         local origin_x, origin_y, origin_z = origin_pos.x, origin_pos.y, origin_pos.z\r
236         local count = 0\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
242                 if entry.meta then\r
243                         get_meta(entry):from_table(entry.meta)\r
244                 end\r
245         end\r
246         return #nodes\r
247 end\r
248 \r