serialization.lua 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. --- Schematic serialization and deserialiation.
  2. -- @module worldedit.serialization
  3. worldedit.LATEST_SERIALIZATION_VERSION = 5
  4. local LATEST_SERIALIZATION_HEADER = worldedit.LATEST_SERIALIZATION_VERSION .. ":"
  5. --[[
  6. Serialization version history:
  7. 1: Original format. Serialized Lua table with a weird linked format...
  8. 2: Position and node seperated into sub-tables in fields `1` and `2`.
  9. 3: List of nodes, one per line, with fields seperated by spaces.
  10. Format: <X> <Y> <Z> <Name> <Param1> <Param2>
  11. 4: Serialized Lua table containing a list of nodes with `x`, `y`, `z`,
  12. `name`, `param1`, `param2`, and `meta` fields.
  13. 5: Added header and made `param1`, `param2`, and `meta` fields optional.
  14. Header format: <Version>,<ExtraHeaderField1>,...:<Content>
  15. --]]
  16. --- Reads the header of serialized data.
  17. -- @param value Serialized WorldEdit data.
  18. -- @return The version as a positive natural number, or 0 for unknown versions.
  19. -- @return Extra header fields as a list of strings, or nil if not supported.
  20. -- @return Content (data after header).
  21. function worldedit.read_header(value)
  22. if value:find("^[0-9]+[,:]") then
  23. local header_end = value:find(":", 1, true)
  24. local header = value:sub(1, header_end - 1):split(",")
  25. local version = tonumber(header[1])
  26. table.remove(header, 1)
  27. local content = value:sub(header_end + 1)
  28. return version, header, content
  29. end
  30. -- Old versions that didn't include a header with a version number
  31. if value:find("([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)") and not value:find("%{") then -- List format
  32. return 3, nil, value
  33. elseif value:find("^[^\"']+%{%d+%}") then
  34. if value:find("%[\"meta\"%]") then -- Meta flat table format
  35. return 2, nil, value
  36. end
  37. return 1, nil, value -- Flat table format
  38. elseif value:find("%{") then -- Raw nested table format
  39. return 4, nil, value
  40. end
  41. return nil
  42. end
  43. --- Converts the region defined by positions `pos1` and `pos2`
  44. -- into a single string.
  45. -- @return The serialized data.
  46. -- @return The number of nodes serialized.
  47. function worldedit.serialize(pos1, pos2)
  48. pos1, pos2 = worldedit.sort_pos(pos1, pos2)
  49. worldedit.keep_loaded(pos1, pos2)
  50. local get_node, get_meta, hash_node_position =
  51. minetest.get_node, minetest.get_meta, minetest.hash_node_position
  52. -- Find the positions which have metadata
  53. local has_meta = {}
  54. local meta_positions = minetest.find_nodes_with_meta(pos1, pos2)
  55. for i = 1, #meta_positions do
  56. has_meta[hash_node_position(meta_positions[i])] = true
  57. end
  58. local pos = {x=pos1.x, y=0, z=0}
  59. local count = 0
  60. local result = {}
  61. while pos.x <= pos2.x do
  62. pos.y = pos1.y
  63. while pos.y <= pos2.y do
  64. pos.z = pos1.z
  65. while pos.z <= pos2.z do
  66. local node = get_node(pos)
  67. if node.name ~= "air" and node.name ~= "ignore" then
  68. count = count + 1
  69. local meta
  70. if has_meta[hash_node_position(pos)] then
  71. meta = get_meta(pos):to_table()
  72. -- Convert metadata item stacks to item strings
  73. for _, invlist in pairs(meta.inventory) do
  74. for index = 1, #invlist do
  75. local itemstack = invlist[index]
  76. if itemstack.to_string then
  77. invlist[index] = itemstack:to_string()
  78. end
  79. end
  80. end
  81. end
  82. result[count] = {
  83. x = pos.x - pos1.x,
  84. y = pos.y - pos1.y,
  85. z = pos.z - pos1.z,
  86. name = node.name,
  87. param1 = node.param1 ~= 0 and node.param1 or nil,
  88. param2 = node.param2 ~= 0 and node.param2 or nil,
  89. meta = meta,
  90. }
  91. end
  92. pos.z = pos.z + 1
  93. end
  94. pos.y = pos.y + 1
  95. end
  96. pos.x = pos.x + 1
  97. end
  98. -- Serialize entries
  99. result = minetest.serialize(result)
  100. return LATEST_SERIALIZATION_HEADER .. result, count
  101. end
  102. -- Contains code based on [table.save/table.load](http://lua-users.org/wiki/SaveTableToFile)
  103. -- by ChillCode, available under the MIT license.
  104. local function deserialize_workaround(content)
  105. local nodes
  106. if not jit then
  107. nodes = minetest.deserialize(content, true)
  108. else
  109. -- XXX: This is a filthy hack that works surprisingly well
  110. -- in LuaJIT, `minetest.deserialize` will fail due to the register limit
  111. nodes = {}
  112. content = content:gsub("^%s*return%s*{", "", 1):gsub("}%s*$", "", 1) -- remove the starting and ending values to leave only the node data
  113. -- remove string contents strings while preserving their length
  114. local escaped = content:gsub("\\\\", "@@"):gsub("\\\"", "@@"):gsub("(\"[^\"]*\")", function(s) return string.rep("@", #s) end)
  115. local startpos, startpos1 = 1, 1
  116. local endpos
  117. while true do -- go through each individual node entry (except the last)
  118. startpos, endpos = escaped:find("},%s*{", startpos)
  119. if not startpos then
  120. break
  121. end
  122. local current = content:sub(startpos1, startpos)
  123. local entry = minetest.deserialize("return " .. current, true)
  124. table.insert(nodes, entry)
  125. startpos, startpos1 = endpos, endpos
  126. end
  127. local entry = minetest.deserialize("return " .. content:sub(startpos1), true) -- process the last entry
  128. table.insert(nodes, entry)
  129. end
  130. return nodes
  131. end
  132. --- Loads the schematic in `value` into a node list in the latest format.
  133. -- @return A node list in the latest format, or nil on failure.
  134. local function load_schematic(value)
  135. local version, header, content = worldedit.read_header(value)
  136. local nodes = {}
  137. if version == 1 or version == 2 then -- Original flat table format
  138. local tables = minetest.deserialize(content, true)
  139. if not tables then return nil end
  140. -- Transform the node table into an array of nodes
  141. for i = 1, #tables do
  142. for j, v in pairs(tables[i]) do
  143. if type(v) == "table" then
  144. tables[i][j] = tables[v[1]]
  145. end
  146. end
  147. end
  148. nodes = tables[1]
  149. if version == 1 then --original flat table format
  150. for i, entry in ipairs(nodes) do
  151. local pos = entry[1]
  152. entry.x, entry.y, entry.z = pos.x, pos.y, pos.z
  153. entry[1] = nil
  154. local node = entry[2]
  155. entry.name, entry.param1, entry.param2 = node.name, node.param1, node.param2
  156. entry[2] = nil
  157. end
  158. end
  159. elseif version == 3 then -- List format
  160. for x, y, z, name, param1, param2 in content:gmatch(
  161. "([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)%s+" ..
  162. "([^%s]+)%s+(%d+)%s+(%d+)[^\r\n]*[\r\n]*") do
  163. param1, param2 = tonumber(param1), tonumber(param2)
  164. table.insert(nodes, {
  165. x = tonumber(x),
  166. y = tonumber(y),
  167. z = tonumber(z),
  168. name = name,
  169. param1 = param1 ~= 0 and param1 or nil,
  170. param2 = param2 ~= 0 and param2 or nil,
  171. })
  172. end
  173. elseif version == 4 or version == 5 then -- Nested table format
  174. nodes = deserialize_workaround(content)
  175. else
  176. return nil
  177. end
  178. return nodes
  179. end
  180. --- Determines the volume the nodes represented by string `value` would occupy
  181. -- if deserialized at `origin_pos`.
  182. -- @return Low corner position.
  183. -- @return High corner position.
  184. -- @return The number of nodes.
  185. function worldedit.allocate(origin_pos, value)
  186. local nodes = load_schematic(value)
  187. if not nodes or #nodes == 0 then return nil end
  188. return worldedit.allocate_with_nodes(origin_pos, nodes)
  189. end
  190. -- Internal
  191. function worldedit.allocate_with_nodes(origin_pos, nodes)
  192. local huge = math.huge
  193. local pos1x, pos1y, pos1z = huge, huge, huge
  194. local pos2x, pos2y, pos2z = -huge, -huge, -huge
  195. local origin_x, origin_y, origin_z = origin_pos.x, origin_pos.y, origin_pos.z
  196. for i, entry in ipairs(nodes) do
  197. local x, y, z = origin_x + entry.x, origin_y + entry.y, origin_z + entry.z
  198. if x < pos1x then pos1x = x end
  199. if y < pos1y then pos1y = y end
  200. if z < pos1z then pos1z = z end
  201. if x > pos2x then pos2x = x end
  202. if y > pos2y then pos2y = y end
  203. if z > pos2z then pos2z = z end
  204. end
  205. local pos1 = {x=pos1x, y=pos1y, z=pos1z}
  206. local pos2 = {x=pos2x, y=pos2y, z=pos2z}
  207. return pos1, pos2, #nodes
  208. end
  209. --- Loads the nodes represented by string `value` at position `origin_pos`.
  210. -- @return The number of nodes deserialized.
  211. function worldedit.deserialize(origin_pos, value)
  212. local nodes = load_schematic(value)
  213. if not nodes then return nil end
  214. if #nodes == 0 then return #nodes end
  215. local pos1, pos2 = worldedit.allocate_with_nodes(origin_pos, nodes)
  216. worldedit.keep_loaded(pos1, pos2)
  217. local origin_x, origin_y, origin_z = origin_pos.x, origin_pos.y, origin_pos.z
  218. local count = 0
  219. local add_node, get_meta = minetest.add_node, minetest.get_meta
  220. for i, entry in ipairs(nodes) do
  221. entry.x, entry.y, entry.z = origin_x + entry.x, origin_y + entry.y, origin_z + entry.z
  222. -- Entry acts as both position and node
  223. add_node(entry, entry)
  224. if entry.meta then
  225. get_meta(entry):from_table(entry.meta)
  226. end
  227. end
  228. return #nodes
  229. end