worldedit = worldedit or {}\r
\r
-dofile(minetest.get_modpath("worldedit") .. "/table_save.lua") --wip: remove dependency\r
-\r
--modifies positions `pos1` and `pos2` so that each component of `pos1` is less than or equal to its corresponding conent of `pos2`, returning two new positions\r
worldedit.sort_pos = function(pos1, pos2)\r
pos1 = {x=pos1.x, y=pos1.y, z=pos1.z}\r
return pos1, pos2\r
end\r
\r
+--determines the version of serialized data `value`, returning the version as a positive integer or 0 for unknown versions\r
+worldedit.valueversion = function(value)\r
+ if value:find("([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)") and not value:find("%{") then --previous list format\r
+ return 3\r
+ elseif value:find("^[^\"']+%{%d+%}") then\r
+ if value:find("%[\"meta\"%]") then --previous meta flat table format\r
+ return 2\r
+ end\r
+ return 1 --original flat table format\r
+ elseif value:find("%{") then --current nested table format\r
+ return 4\r
+ end\r
+ return 0 --unknown format\r
+end\r
+\r
--converts the region defined by positions `pos1` and `pos2` into a single string, returning the serialized data and the number of nodes serialized\r
-worldedit.serialize = function(pos1, pos2)\r
+worldedit.serialize = function(pos1, pos2) --wip: check for ItemStacks and whether they can be serialized\r
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
local pos = {x=pos1.x, y=0, z=0}\r
local count = 0\r
local node = env:get_node(pos)\r
if node.name ~= "air" and node.name ~= "ignore" then\r
count = count + 1\r
- result[count] = pos.x - pos1.x .. " " .. pos.y - pos1.y .. " " .. pos.z - pos1.z .. " " .. node.name .. " " .. node.param1 .. " " .. node.param2\r
+ local meta = env:get_meta(pos):to_table()\r
+ result[count] = {\r
+ x = pos.x - pos1.x,\r
+ y = pos.y - pos1.y,\r
+ z = pos.z - pos1.z,\r
+ name = node.name,\r
+ param1 = node.param1,\r
+ param2 = node.param2,\r
+ meta = meta,\r
+ }\r
end\r
pos.z = pos.z + 1\r
end\r
end\r
pos.x = pos.x + 1\r
end\r
- result = table.concat(result, "\n") --join all node entries into single string\r
+ result = minetest.serialize(result) --convert entries to a string\r
return result, count\r
end\r
\r
--determines the volume the nodes represented by string `value` would occupy if deserialized at `originpos`, returning the two corner positions and the number of nodes\r
+--contains code based on [table.save/table.load](http://lua-users.org/wiki/SaveTableToFile) by ChillCode, available under the MIT license (GPL compatible)\r
worldedit.allocate = function(originpos, value)\r
local huge = math.huge\r
- local pos1 = {x=huge, y=huge, z=huge}\r
- local pos2 = {x=-huge, y=-huge, z=-huge}\r
+ local pos1x, pos1y, pos1z = huge, huge, huge\r
+ local pos2x, pos2y, pos2z = -huge, -huge, -huge\r
local originx, originy, originz = originpos.x, originpos.y, originpos.z\r
local count = 0\r
- for x, y, z, name, param1, param2 in value:gmatch("([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)%s+([^%s]+)%s+(%d+)%s+(%d+)[^\r\n]*[\r\n]*") do --match node entries\r
- x, y, z = originx + tonumber(x), originy + tonumber(y), originz + tonumber(z)\r
- if x < pos1.x then\r
- pos1.x = x\r
- end\r
- if y < pos1.y then\r
- pos1.y = y\r
- end\r
- if z < pos1.z then\r
- pos1.z = z\r
- end\r
- if x > pos2.x then\r
- pos2.x = x\r
+ local version = worldedit.valueversion(value)\r
+ if version == 1 or version == 2 then --flat table format\r
+ --obtain the node table\r
+ local get_tables = loadstring(value)\r
+ if get_tables then --error loading value\r
+ return originpos, originpos, count\r
+ end\r
+ local tables = get_tables()\r
+\r
+ --transform the node table into an array of nodes\r
+ for i = 1, #tables do\r
+ for j, v in pairs(tables[i]) do\r
+ if type(v) == "table" then\r
+ tables[i][j] = tables[v[1]]\r
+ end\r
+ end\r
end\r
- if y > pos2.y then\r
- pos2.y = y\r
+ local nodes = tables[1]\r
+\r
+ --check the node array\r
+ count = #nodes\r
+ if version == 1 then --original flat table format\r
+ for index = 1, count do\r
+ local entry = nodes[index]\r
+ local pos = entry[1]\r
+ local x, y, z = originx - pos.x, originy - pos.y, originz - pos.z\r
+ if x < pos1x then pos1x = x end\r
+ if y < pos1y then pos1y = y end\r
+ if z < pos1z then pos1z = z end\r
+ if x > pos2x then pos2x = x end\r
+ if y > pos2y then pos2y = y end\r
+ if z > pos2z then pos2z = z end\r
+ end\r
+ else --previous meta flat table format\r
+ for index = 1, count do\r
+ local entry = nodes[index]\r
+ local x, y, z = originx - entry.x, originy - entry.y, originz - entry.z\r
+ if x < pos1x then pos1x = x end\r
+ if y < pos1y then pos1y = y end\r
+ if z < pos1z then pos1z = z end\r
+ if x > pos2x then pos2x = x end\r
+ if y > pos2y then pos2y = y end\r
+ if z > pos2z then pos2z = z end\r
+ end\r
end\r
- if z > pos2.z then\r
- pos2.z = z\r
+ elseif version == 3 then --previous list format\r
+ for x, y, z, name, param1, param2 in value:gmatch("([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)%s+([^%s]+)%s+(%d+)%s+(%d+)[^\r\n]*[\r\n]*") do --match node entries\r
+ x, y, z = originx + tonumber(x), originy + tonumber(y), originz + tonumber(z)\r
+ if x < pos1x then pos1x = x end\r
+ if y < pos1y then pos1y = y end\r
+ if z < pos1z then pos1z = z end\r
+ if x > pos2x then pos2x = x end\r
+ if y > pos2y then pos2y = y end\r
+ if z > pos2z then pos2z = z end\r
+ count = count + 1\r
+ end\r
+ elseif version == 4 then --current nested table format\r
+ local nodes = minetest.deserialize(value)\r
+ count = #nodes\r
+ for index = 1, count do\r
+ local entry = nodes[index]\r
+ x, y, z = originx + entry.x, originy + entry.y, originz + entry.z\r
+ if x < pos1x then pos1x = x end\r
+ if y < pos1y then pos1y = y end\r
+ if z < pos1z then pos1z = z end\r
+ if x > pos2x then pos2x = x end\r
+ if y > pos2y then pos2y = y end\r
+ if z > pos2z then pos2z = z end\r
end\r
- count = count + 1\r
end\r
+ local pos1 = {x=pos1x, y=pos1y, z=pos1z}\r
+ local pos2 = {x=pos2x, y=pos2y, z=pos2z}\r
return pos1, pos2, count\r
end\r
\r
--loads the nodes represented by string `value` at position `originpos`, returning the number of nodes deserialized\r
+--contains code based on [table.save/table.load](http://lua-users.org/wiki/SaveTableToFile) by ChillCode, available under the MIT license (GPL compatible)\r
worldedit.deserialize = function(originpos, value)\r
- local pos = {x=0, y=0, z=0}\r
- local node = {name="", param1=0, param2=0}\r
local originx, originy, originz = originpos.x, originpos.y, originpos.z\r
local count = 0\r
local env = minetest.env\r
- for x, y, z, name, param1, param2 in value:gmatch("([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)%s+([^%s]+)%s+(%d+)%s+(%d+)[^\r\n]*[\r\n]*") do --match node entries\r
- pos.x = originx + tonumber(x)\r
- pos.y = originy + tonumber(y)\r
- pos.z = originz + tonumber(z)\r
- node.name = name\r
- node.param1 = param1\r
- node.param2 = param2\r
- env:add_node(pos, node)\r
- count = count + 1\r
- end\r
- return count\r
-end\r
-\r
---determines the volume the nodes represented by string `value` would occupy if deserialized at `originpos`, returning the two corner positions and the number of nodes\r
---based on [table.save/table.load](http://lua-users.org/wiki/SaveTableToFile) by ChillCode, available under the MIT license (GPL compatible)\r
-worldedit.allocate_old = function(originpos, value)\r
- --obtain the node table\r
- local count = 0\r
- local get_tables = loadstring(value)\r
- if get_tables == nil then --error loading value\r
- return count\r
- end\r
- local tables = get_tables()\r
-\r
- --transform the node table into an array of nodes\r
- for i = 1, #tables do\r
- for j, v in pairs(tables[i]) do\r
- if type(v) == "table" then\r
- tables[i][j] = tables[v[1]]\r
+ local version = worldedit.valueversion(value)\r
+ if version == 1 or version == 2 then --original flat table format\r
+ --obtain the node table\r
+ local get_tables = loadstring(value)\r
+ if not get_tables then --error loading value\r
+ return count\r
+ end\r
+ local tables = get_tables()\r
+\r
+ --transform the node table into an array of nodes\r
+ for i = 1, #tables do\r
+ for j, v in pairs(tables[i]) do\r
+ if type(v) == "table" then\r
+ tables[i][j] = tables[v[1]]\r
+ end\r
end\r
end\r
- end\r
+ local nodes = tables[1]\r
\r
- local huge = math.huge\r
- local pos1 = {x=huge, y=huge, z=huge}\r
- local pos2 = {x=-huge, y=-huge, z=-huge}\r
- local originx, originy, originz = originpos.x, originpos.y, originpos.z\r
-\r
- --load the node array\r
- for i, v in ipairs(tables[1]) do\r
- local pos = v[1]\r
- local x, y, z = originx - pos.x, originy - pos.y, originz - pos.z\r
- if x < pos1.x then\r
- pos1.x = x\r
- end\r
- if y < pos1.y then\r
- pos1.y = y\r
- end\r
- if z < pos1.z then\r
- pos1.z = z\r
- end\r
- if x > pos2.x then\r
- pos2.x = x\r
- end\r
- if y > pos2.y then\r
- pos2.y = y\r
- end\r
- if z > pos2.z then\r
- pos2.z = z\r
- end\r
- count = count + 1\r
- end\r
- return pos1, pos2, count\r
-end\r
-\r
---loads the nodes represented by string `value` at position `originpos`, returning the number of nodes deserialized\r
---based on [table.save/table.load](http://lua-users.org/wiki/SaveTableToFile) by ChillCode, available under the MIT license (GPL compatible)\r
-worldedit.deserialize_old = function(originpos, value)\r
- --obtain the node table\r
- local count = 0\r
- local get_tables = loadstring(value)\r
- if get_tables == nil then --error loading value\r
- return count\r
- end\r
- local tables = get_tables()\r
-\r
- --transform the node table into an array of nodes\r
- for i = 1, #tables do\r
- for j, v in pairs(tables[i]) do\r
- if type(v) == "table" then\r
- tables[i][j] = tables[v[1]]\r
+ --load the node array\r
+ count = #nodes\r
+ if version == 1 then --original flat table format\r
+ for index = 1, count do\r
+ local entry = nodes[index]\r
+ local pos = entry[1]\r
+ pos.x, pos.y, pos.z = originx - pos.x, originy - pos.y, originz - pos.z\r
+ env:add_node(pos, entry[2])\r
end\r
- end\r
- end\r
-\r
- --load the node array\r
- local env = minetest.env\r
- local originx, originy, originz = originpos.x, originpos.y, originpos.z\r
- for i, v in ipairs(tables[1]) do\r
- local pos = v[1]\r
- pos.x, pos.y, pos.z = originx - pos.x, originy - pos.y, originz - pos.z\r
- env:add_node(pos, v[2])\r
- count = count + 1\r
- end\r
- return count\r
-end\r
-\r
---saves the nodes and meta defined by positions `pos1` and `pos2` into a file, returning the number of nodes saved\r
-worldedit.metasave = function(pos1, pos2, file) --wip: simply work with strings instead of doing IO\r
- local path = minetest.get_worldpath() .. "/schems"\r
- local filename = path .. "/" .. file .. ".wem"\r
- os.execute("mkdir \"" .. path .. "\"") --create directory if it does not already exist\r
- local rows = {}\r
- local pos1, pos2 = worldedit.sort_pos(pos1, pos2)\r
- local pos = {x=pos1.x, y=0, z=0}\r
- local count = 0\r
- local result = {}\r
- local env = minetest.env\r
- while pos.x <= pos2.x do\r
- pos.y = pos1.y\r
- while pos.y <= pos2.y do\r
- pos.z = pos1.z\r
- while pos.z <= pos2.z do\r
- local node = env:get_node(pos)\r
- if node.name ~= "air" and node.name ~= "ignore" then\r
- count = count + 1\r
- local meta = env:get_meta(pos):to_table()\r
-\r
- --convert metadata itemstacks to itemstrings\r
- for i, v in pairs(meta.inventory) do\r
- for index, items in ipairs(v) do\r
- v[index] = items:to_string()\r
- end\r
- end\r
-\r
- table.insert(rows, {\r
- x = pos.x-pos1.x,\r
- y = pos.y-pos1.y,\r
- z = pos.z-pos1.z,\r
- name = node.name,\r
- param1 = node.param1,\r
- param2 = node.param2,\r
- meta = meta,\r
- })\r
- end\r
- pos.z = pos.z + 1\r
+ else --previous meta flat table format\r
+ for index = 1, #nodes do\r
+ local entry = nodes[index]\r
+ entry.x, entry.y, entry.z = originx + entry.x, originy + entry.y, originz + entry.z\r
+ env:add_node(entry, entry) --entry acts both as position and as node\r
+ env:get_meta(entry):from_table(entry.meta)\r
end\r
- pos.y = pos.y + 1\r
end\r
- pos.x = pos.x + 1\r
- end\r
- local err = table.save(rows, filename)\r
- if err then return _, err end\r
- return count\r
-end\r
-\r
---loads the nodes and meta from `file` to position `pos1`, returning the number of nodes loaded\r
-worldedit.metaload = function(pos1, file) --wip: simply work with strings instead of doing IO\r
- local filename = minetest.get_worldpath() .. "/schems/" .. file .. ".wem"\r
- local rows, err = table.load(filename)\r
- if err then return _,err end\r
- local pos = {x=0, y=0, z=0}\r
- local node = {name="", param1=0, param2=0}\r
- local count = 0\r
- local env = minetest.env\r
- for i,row in pairs(rows) do\r
- pos.x = pos1.x + tonumber(row.x)\r
- pos.y = pos1.y + tonumber(row.y)\r
- pos.z = pos1.z + tonumber(row.z)\r
- node.name = row.name\r
- node.param1 = row.param1\r
- node.param2 = row.param2\r
- env:add_node(pos, node)\r
- env:get_meta(pos):from_table(row.meta)\r
- count = count + 1\r
+ elseif version == 3 then --previous list format\r
+ local pos = {x=0, y=0, z=0}\r
+ local node = {name="", param1=0, param2=0}\r
+ for x, y, z, name, param1, param2 in value:gmatch("([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)%s+([^%s]+)%s+(%d+)%s+(%d+)[^\r\n]*[\r\n]*") do --match node entries\r
+ pos.x = originx + tonumber(x)\r
+ pos.y = originy + tonumber(y)\r
+ pos.z = originz + tonumber(z)\r
+ node.name = name\r
+ node.param1 = param1\r
+ node.param2 = param2\r
+ env:add_node(pos, node)\r
+ count = count + 1\r
+ end\r
+ elseif version == 4 then --current nested table format\r
+ local nodes = minetest.deserialize(value)\r
+ count = #nodes\r
+ for index = 1, count do\r
+ local entry = nodes[index]\r
+ entry.x, entry.y, entry.z = originx + entry.x, originy + entry.y, originz + entry.z\r
+ env:add_node(entry, entry) --entry acts both as position and as node\r
+ env:get_meta(entry):from_table(entry.meta)\r
+ end\r
end\r
return count\r
end
\ No newline at end of file
+++ /dev/null
---[[
- Save Table to File
- Load Table from File
- v 1.0
-
- Lua 5.2 compatible
-
- Only Saves Tables, Numbers and Strings
- Insides Table References are saved
- Does not save Userdata, Metatables, Functions and indices of these
- ----------------------------------------------------
- table.save( table , filename )
-
- on failure: returns an error msg
-
- ----------------------------------------------------
- table.load( filename or stringtable )
-
- Loads a table that has been saved via the table.save function
-
- on success: returns a previously saved table
- on failure: returns as second argument an error msg
- ----------------------------------------------------
-
- Licensed under the same terms as Lua itself.
-]]--
-do
- -- declare local variables
- --// exportstring( string )
- --// returns a "Lua" portable version of the string
- local function exportstring( s )
- return string.format("%q", s)
- end
-
- --// The Save Function
- function table.save( tbl,filename )
- local charS,charE = " ","\n"
- local file,err = io.open( filename, "wb" )
- if err then return err end
-
- -- initiate variables for save procedure
- local tables,lookup = { tbl },{ [tbl] = 1 }
- file:write( "return {"..charE )
-
- for idx,t in ipairs( tables ) do
- file:write( "-- Table: {"..idx.."}"..charE )
- file:write( "{"..charE )
- local thandled = {}
-
- for i,v in ipairs( t ) do
- thandled[i] = true
- local stype = type( v )
- -- only handle value
- if stype == "table" then
- if not lookup[v] then
- table.insert( tables, v )
- lookup[v] = #tables
- end
- file:write( charS.."{"..lookup[v].."},"..charE )
- elseif stype == "string" then
- file:write( charS..exportstring( v )..","..charE )
- elseif stype == "number" then
- file:write( charS..tostring( v )..","..charE )
- end
- end
-
- for i,v in pairs( t ) do
- -- escape handled values
- if (not thandled[i]) then
-
- local str = ""
- local stype = type( i )
- -- handle index
- if stype == "table" then
- if not lookup[i] then
- table.insert( tables,i )
- lookup[i] = #tables
- end
- str = charS.."[{"..lookup[i].."}]="
- elseif stype == "string" then
- str = charS.."["..exportstring( i ).."]="
- elseif stype == "number" then
- str = charS.."["..tostring( i ).."]="
- end
-
- if str ~= "" then
- stype = type( v )
- -- handle value
- if stype == "table" then
- if not lookup[v] then
- table.insert( tables,v )
- lookup[v] = #tables
- end
- file:write( str.."{"..lookup[v].."},"..charE )
- elseif stype == "string" then
- file:write( str..exportstring( v )..","..charE )
- elseif stype == "number" then
- file:write( str..tostring( v )..","..charE )
- end
- end
- end
- end
- file:write( "},"..charE )
- end
- file:write( "}" )
- file:close()
- end
-
- --// The Load Function
- function table.load( sfile )
- local ftables,err = loadfile( sfile )
- if err then return _,err end
- local tables = ftables()
- for idx = 1,#tables do
- local tolinki = {}
- for i,v in pairs( tables[idx] ) do
- if type( v ) == "table" then
- tables[idx][i] = tables[v[1]]
- end
- if type( i ) == "table" and tables[i[1]] then
- table.insert( tolinki,{ i,tables[i[1]] } )
- end
- end
- -- link indices
- for _,v in ipairs( tolinki ) do
- tables[idx][v[2]],tables[idx][v[1]] = tables[idx][v[1]],nil
- end
- end
- return tables[1]
- end
--- close do
-end
--- ChillCode
\ No newline at end of file
local value = file:read("*a")\r
file:close()\r
\r
- local nodepos1, nodepos2, count\r
- if value:find("{") then --old WorldEdit format\r
- nodepos1, nodepos2, count = worldedit.allocate_old(pos1, value)\r
- else --new WorldEdit format\r
- nodepos1, nodepos2, count = worldedit.allocate(pos1, value)\r
+ if worldedit.valueversion(value) == 0 then --unknown version\r
+ minetest.chat_send_player(name, "Invalid file: file is invalid or created with newer version of WorldEdit")\r
+ return\r
end\r
+ local nodepos1, nodepos2, count = worldedit.allocate(pos1, value)\r
\r
worldedit.pos1[name] = nodepos1\r
worldedit.mark_pos1(name)\r
\r
minetest.register_chatcommand("/load", {\r
params = "<file>",\r
- description = "Load nodes from \"(world folder)/schems/<file>.we\" with position 1 of the current WorldEdit region as the origin",\r
+ description = "Load nodes from \"(world folder)/schems/<file>[.we[m]]\" with position 1 of the current WorldEdit region as the origin",\r
privs = {worldedit=true},\r
func = function(name, param)\r
local pos1 = worldedit.pos1[name]\r
return\r
end\r
\r
- local filename = minetest.get_worldpath() .. "/schems/" .. param .. ".we"\r
- local file, err = io.open(filename, "rb")\r
- if err ~= nil then\r
- minetest.chat_send_player(name, "Could not open file \"" .. filename .. "\"")\r
+ --find the file in the world path\r
+ local testpaths = {\r
+ minetest.get_worldpath() .. "/schems/" .. param,\r
+ minetest.get_worldpath() .. "/schems/" .. param .. ".we",\r
+ minetest.get_worldpath() .. "/schems/" .. param .. ".wem",\r
+ }\r
+ local file, err\r
+ for index, path in ipairs(testpaths) do\r
+ file, err = io.open(path, "rb")\r
+ if not err then\r
+ break\r
+ end\r
+ end\r
+ if err then\r
+ minetest.chat_send_player(name, "Could not open file \"" .. param .. "\"")\r
return\r
end\r
local value = file:read("*a")\r
file:close()\r
\r
- local count\r
- if value:find("{") then --old WorldEdit format\r
- count = worldedit.deserialize_old(pos1, value)\r
- else --new WorldEdit format\r
- count = worldedit.deserialize(pos1, value)\r
- end\r
-\r
- minetest.chat_send_player(name, count .. " nodes loaded")\r
- end,\r
-})\r
-\r
-minetest.register_chatcommand("/metasave", {\r
- params = "<file>",\r
- description = "Save the current WorldEdit region to \"(world folder)/schems/<file>.wem\"",\r
- privs = {worldedit=true},\r
- func = function(name, param)\r
- local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]\r
- if pos1 == nil or pos2 == nil then\r
- minetest.chat_send_player(name, "No WorldEdit region selected")\r
+ if worldedit.valueversion(value) == 0 then --unknown version\r
+ minetest.chat_send_player(name, "Invalid file: file is invalid or created with newer version of WorldEdit")\r
return\r
end\r
- if param == "" then\r
- minetest.chat_send_player(name, "Invalid usage: " .. param)\r
- return\r
- end\r
- local count, err = worldedit.metasave(pos1, pos2, param)\r
- if err then\r
- minetest.chat_send_player(name, "error loading file: " .. err)\r
- else\r
- minetest.chat_send_player(name, count .. " nodes saved")\r
- end\r
- end,\r
-})\r
+ local count = worldedit.deserialize(pos1, value)\r
\r
-minetest.register_chatcommand("/metaload", {\r
- params = "<file>",\r
- description = "Load nodes from \"(world folder)/schems/<file>.wem\" with position 1 of the current WorldEdit region as the origin",\r
- privs = {worldedit=true},\r
- func = function(name, param)\r
- local pos1 = worldedit.pos1[name]\r
- if pos1 == nil then\r
- minetest.chat_send_player(name, "No WorldEdit region selected")\r
- return\r
- end\r
- if param == "" then\r
- minetest.chat_send_player(name, "Invalid usage: " .. param)\r
- return\r
- end\r
- local count, err = worldedit.metaload(pos1, param)\r
- if err then\r
- minetest.chat_send_player(name, "Error loading file: " .. err)\r
- else\r
- minetest.chat_send_player(name, count .. " nodes loaded")\r
- end\r
+ minetest.chat_send_player(name, count .. " nodes loaded")\r
end,\r
})\r
\r