serialization.lua 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. local function deserialize_workaround(content)
  103. local nodes
  104. if not minetest.global_exists("jit") then
  105. nodes = minetest.deserialize(content, true)
  106. elseif not content:match("^%s*return%s*{") then
  107. -- The data doesn't look like we expect it to so we can't apply the workaround.
  108. -- hope for the best
  109. minetest.log("warning", "WorldEdit: deserializing data but can't apply LuaJIT workaround")
  110. nodes = minetest.deserialize(content, true)
  111. else
  112. -- XXX: This is a filthy hack that works surprisingly well
  113. -- in LuaJIT, `minetest.deserialize` will fail due to the register limit
  114. nodes = {}
  115. content = content:gsub("^%s*return%s*{", "", 1):gsub("}%s*$", "", 1) -- remove the starting and ending values to leave only the node data
  116. -- remove string contents strings while preserving their length
  117. local escaped = content:gsub("\\\\", "@@"):gsub("\\\"", "@@"):gsub("(\"[^\"]*\")", function(s) return string.rep("@", #s) end)
  118. local startpos, startpos1 = 1, 1
  119. local endpos
  120. while true do -- go through each individual node entry (except the last)
  121. startpos, endpos = escaped:find("}%s*,%s*{", startpos)
  122. if not startpos then
  123. break
  124. end
  125. local current = content:sub(startpos1, startpos)
  126. local entry = minetest.deserialize("return " .. current, true)
  127. table.insert(nodes, entry)
  128. startpos, startpos1 = endpos, endpos
  129. end
  130. local entry = minetest.deserialize("return " .. content:sub(startpos1), true) -- process the last entry
  131. table.insert(nodes, entry)
  132. end
  133. return nodes
  134. end
  135. --- Loads the schematic in `value` into a node list in the latest format.
  136. -- @return A node list in the latest format, or nil on failure.
  137. local function load_schematic(value)
  138. local version, header, content = worldedit.read_header(value)
  139. local nodes = {}
  140. if version == 1 or version == 2 then -- Original flat table format
  141. local tables = minetest.deserialize(content, true)
  142. if not tables then return nil end
  143. -- Transform the node table into an array of nodes
  144. for i = 1, #tables do
  145. for j, v in pairs(tables[i]) do
  146. if type(v) == "table" then
  147. tables[i][j] = tables[v[1]]
  148. end
  149. end
  150. end
  151. nodes = tables[1]
  152. if version == 1 then --original flat table format
  153. for i, entry in ipairs(nodes) do
  154. local pos = entry[1]
  155. entry.x, entry.y, entry.z = pos.x, pos.y, pos.z
  156. entry[1] = nil
  157. local node = entry[2]
  158. entry.name, entry.param1, entry.param2 = node.name, node.param1, node.param2
  159. entry[2] = nil
  160. end
  161. end
  162. elseif version == 3 then -- List format
  163. for x, y, z, name, param1, param2 in content:gmatch(
  164. "([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)%s+" ..
  165. "([^%s]+)%s+(%d+)%s+(%d+)[^\r\n]*[\r\n]*") do
  166. param1, param2 = tonumber(param1), tonumber(param2)
  167. table.insert(nodes, {
  168. x = tonumber(x),
  169. y = tonumber(y),
  170. z = tonumber(z),
  171. name = name,
  172. param1 = param1 ~= 0 and param1 or nil,
  173. param2 = param2 ~= 0 and param2 or nil,
  174. })
  175. end
  176. elseif version == 4 or version == 5 then -- Nested table format
  177. nodes = deserialize_workaround(content)
  178. else
  179. return nil
  180. end
  181. return nodes
  182. end
  183. --- Determines the volume the nodes represented by string `value` would occupy
  184. -- if deserialized at `origin_pos`.
  185. -- @return Low corner position.
  186. -- @return High corner position.
  187. -- @return The number of nodes.
  188. function worldedit.allocate(origin_pos, value)
  189. local nodes = load_schematic(value)
  190. if not nodes or #nodes == 0 then return nil end
  191. return worldedit.allocate_with_nodes(origin_pos, nodes)
  192. end
  193. -- Internal
  194. function worldedit.allocate_with_nodes(origin_pos, nodes)
  195. local huge = math.huge
  196. local pos1x, pos1y, pos1z = huge, huge, huge
  197. local pos2x, pos2y, pos2z = -huge, -huge, -huge
  198. local origin_x, origin_y, origin_z = origin_pos.x, origin_pos.y, origin_pos.z
  199. for i, entry in ipairs(nodes) do
  200. local x, y, z = origin_x + entry.x, origin_y + entry.y, origin_z + entry.z
  201. if x < pos1x then pos1x = x end
  202. if y < pos1y then pos1y = y end
  203. if z < pos1z then pos1z = z end
  204. if x > pos2x then pos2x = x end
  205. if y > pos2y then pos2y = y end
  206. if z > pos2z then pos2z = z end
  207. end
  208. local pos1 = {x=pos1x, y=pos1y, z=pos1z}
  209. local pos2 = {x=pos2x, y=pos2y, z=pos2z}
  210. return pos1, pos2, #nodes
  211. end
  212. --- Loads the nodes represented by string `value` at position `origin_pos`.
  213. -- @return The number of nodes deserialized.
  214. function worldedit.deserialize(origin_pos, value)
  215. local nodes = load_schematic(value)
  216. if not nodes then return nil end
  217. if #nodes == 0 then return #nodes end
  218. local pos1, pos2 = worldedit.allocate_with_nodes(origin_pos, nodes)
  219. worldedit.keep_loaded(pos1, pos2)
  220. local origin_x, origin_y, origin_z = origin_pos.x, origin_pos.y, origin_pos.z
  221. local count = 0
  222. local add_node, get_meta = minetest.add_node, minetest.get_meta
  223. for i, entry in ipairs(nodes) do
  224. entry.x, entry.y, entry.z = origin_x + entry.x, origin_y + entry.y, origin_z + entry.z
  225. -- Entry acts as both position and node
  226. add_node(entry, entry)
  227. if entry.meta then
  228. get_meta(entry):from_table(entry.meta)
  229. end
  230. end
  231. return #nodes
  232. end