]> git.lizzy.rs Git - worldedit.git/blob - worldedit/serialization.lua
4140e171f496e5dec2d262dc5d5fdd04e9ff64f8
[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 local function deserialize_workaround(content)\r
118         local nodes\r
119         if not minetest.global_exists("jit") then\r
120                 nodes = minetest.deserialize(content, true)\r
121         elseif not content:match("^%s*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
126         else\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
129                 nodes = {}\r
130                 content = content:gsub("^%s*return%s*{", "", 1):gsub("}%s*$", "", 1) -- remove the starting and ending values to leave only the node data\r
131                 -- remove string contents strings while preserving their length\r
132                 local escaped = content:gsub("\\\\", "@@"):gsub("\\\"", "@@"):gsub("(\"[^\"]*\")", function(s) return string.rep("@", #s) end)\r
133                 local startpos, startpos1 = 1, 1\r
134                 local endpos\r
135                 while true do -- go through each individual node entry (except the last)\r
136                         startpos, endpos = escaped:find("}%s*,%s*{", startpos)\r
137                         if not startpos then\r
138                                 break\r
139                         end\r
140                         local current = content:sub(startpos1, startpos)\r
141                         local entry = minetest.deserialize("return " .. current, true)\r
142                         table.insert(nodes, entry)\r
143                         startpos, startpos1 = endpos, endpos\r
144                 end\r
145                 local entry = minetest.deserialize("return " .. content:sub(startpos1), true) -- process the last entry\r
146                 table.insert(nodes, entry)\r
147         end\r
148         return nodes\r
149 end\r
150 \r
151 --- Loads the schematic in `value` into a node list in the latest format.\r
152 -- @return A node list in the latest format, or nil on failure.\r
153 local function load_schematic(value)\r
154         local version, header, content = worldedit.read_header(value)\r
155         local nodes = {}\r
156         if version == 1 or version == 2 then -- Original flat table format\r
157                 local tables = minetest.deserialize(content, true)\r
158                 if not tables then return nil end\r
159 \r
160                 -- Transform the node table into an array of nodes\r
161                 for i = 1, #tables do\r
162                         for j, v in pairs(tables[i]) do\r
163                                 if type(v) == "table" then\r
164                                         tables[i][j] = tables[v[1]]\r
165                                 end\r
166                         end\r
167                 end\r
168                 nodes = tables[1]\r
169 \r
170                 if version == 1 then --original flat table format\r
171                         for i, entry in ipairs(nodes) do\r
172                                 local pos = entry[1]\r
173                                 entry.x, entry.y, entry.z = pos.x, pos.y, pos.z\r
174                                 entry[1] = nil\r
175                                 local node = entry[2]\r
176                                 entry.name, entry.param1, entry.param2 = node.name, node.param1, node.param2\r
177                                 entry[2] = nil\r
178                         end\r
179                 end\r
180         elseif version == 3 then -- List format\r
181                 for x, y, z, name, param1, param2 in content:gmatch(\r
182                                 "([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)%s+" ..\r
183                                 "([^%s]+)%s+(%d+)%s+(%d+)[^\r\n]*[\r\n]*") do\r
184                         param1, param2 = tonumber(param1), tonumber(param2)\r
185                         table.insert(nodes, {\r
186                                 x = tonumber(x),\r
187                                 y = tonumber(y),\r
188                                 z = tonumber(z),\r
189                                 name = name,\r
190                                 param1 = param1 ~= 0 and param1 or nil,\r
191                                 param2 = param2 ~= 0 and param2 or nil,\r
192                         })\r
193                 end\r
194         elseif version == 4 or version == 5 then -- Nested table format\r
195                 nodes = deserialize_workaround(content)\r
196         else\r
197                 return nil\r
198         end\r
199         return nodes\r
200 end\r
201 \r
202 --- Determines the volume the nodes represented by string `value` would occupy\r
203 -- if deserialized at `origin_pos`.\r
204 -- @return Low corner position.\r
205 -- @return High corner position.\r
206 -- @return The number of nodes.\r
207 function worldedit.allocate(origin_pos, value)\r
208         local nodes = load_schematic(value)\r
209         if not nodes or #nodes == 0 then return nil end\r
210         return worldedit.allocate_with_nodes(origin_pos, nodes)\r
211 end\r
212 \r
213 \r
214 -- Internal\r
215 function worldedit.allocate_with_nodes(origin_pos, nodes)\r
216         local huge = math.huge\r
217         local pos1x, pos1y, pos1z = huge, huge, huge\r
218         local pos2x, pos2y, pos2z = -huge, -huge, -huge\r
219         local origin_x, origin_y, origin_z = origin_pos.x, origin_pos.y, origin_pos.z\r
220         for i, entry in ipairs(nodes) do\r
221                 local x, y, z = origin_x + entry.x, origin_y + entry.y, origin_z + entry.z\r
222                 if x < pos1x then pos1x = x end\r
223                 if y < pos1y then pos1y = y end\r
224                 if z < pos1z then pos1z = z end\r
225                 if x > pos2x then pos2x = x end\r
226                 if y > pos2y then pos2y = y end\r
227                 if z > pos2z then pos2z = z end\r
228         end\r
229         local pos1 = {x=pos1x, y=pos1y, z=pos1z}\r
230         local pos2 = {x=pos2x, y=pos2y, z=pos2z}\r
231         return pos1, pos2, #nodes\r
232 end\r
233 \r
234 \r
235 --- Loads the nodes represented by string `value` at position `origin_pos`.\r
236 -- @return The number of nodes deserialized.\r
237 function worldedit.deserialize(origin_pos, value)\r
238         local nodes = load_schematic(value)\r
239         if not nodes then return nil end\r
240         if #nodes == 0 then return #nodes end\r
241 \r
242         local pos1, pos2 = worldedit.allocate_with_nodes(origin_pos, nodes)\r
243         worldedit.keep_loaded(pos1, pos2)\r
244 \r
245         local origin_x, origin_y, origin_z = origin_pos.x, origin_pos.y, origin_pos.z\r
246         local count = 0\r
247         local add_node, get_meta = minetest.add_node, minetest.get_meta\r
248         for i, entry in ipairs(nodes) do\r
249                 entry.x, entry.y, entry.z = origin_x + entry.x, origin_y + entry.y, origin_z + entry.z\r
250                 -- Entry acts as both position and node\r
251                 add_node(entry, entry)\r
252                 if entry.meta then\r
253                         get_meta(entry):from_table(entry.meta)\r
254                 end\r
255         end\r
256         return #nodes\r
257 end\r
258 \r