Compare commits

...

2 Commits

Author SHA1 Message Date
sfan5 5260f595c6 Log deserialization errors 2023-06-07 11:50:33 +02:00
sfan5 7a645eba05 Improve loading and error handling for schematics 2023-06-07 11:41:10 +02:00
2 changed files with 59 additions and 60 deletions

View File

@ -115,14 +115,14 @@ function worldedit.serialize(pos1, pos2)
end
local function deserialize_workaround(content)
local nodes
local nodes, err
if not minetest.global_exists("jit") then
nodes = minetest.deserialize(content, true)
nodes, err = minetest.deserialize(content, true)
elseif not content:match("^%s*return%s*{") then
-- The data doesn't look like we expect it to so we can't apply the workaround.
-- hope for the best
minetest.log("warning", "WorldEdit: deserializing data but can't apply LuaJIT workaround")
nodes = minetest.deserialize(content, true)
nodes, err = minetest.deserialize(content, true)
else
-- XXX: This is a filthy hack that works surprisingly well
-- in LuaJIT, `minetest.deserialize` will fail due to the register limit
@ -132,18 +132,27 @@ local function deserialize_workaround(content)
local escaped = content:gsub("\\\\", "@@"):gsub("\\\"", "@@"):gsub("(\"[^\"]*\")", function(s) return string.rep("@", #s) end)
local startpos, startpos1 = 1, 1
local endpos
local entry
while true do -- go through each individual node entry (except the last)
startpos, endpos = escaped:find("}%s*,%s*{", startpos)
if not startpos then
break
end
local current = content:sub(startpos1, startpos)
local entry = minetest.deserialize("return " .. current, true)
entry, err = minetest.deserialize("return " .. current, true)
if not entry then
break
end
table.insert(nodes, entry)
startpos, startpos1 = endpos, endpos
end
local entry = minetest.deserialize("return " .. content:sub(startpos1), true) -- process the last entry
table.insert(nodes, entry)
if not err then
entry = minetest.deserialize("return " .. content:sub(startpos1), true) -- process the last entry
table.insert(nodes, entry)
end
end
if err then
minetest.log("warning", "WorldEdit: deserialize: " .. err)
end
return nodes
end

View File

@ -226,6 +226,39 @@ local function check_filename(name)
return name:find("^[%w%s%^&'@{}%[%],%$=!%-#%(%)%%%.%+~_]+$") ~= nil
end
local function open_schematic(name, param)
-- find the file in the world path
local testpaths = {
minetest.get_worldpath() .. "/schems/" .. param,
minetest.get_worldpath() .. "/schems/" .. param .. ".we",
minetest.get_worldpath() .. "/schems/" .. param .. ".wem",
}
local file, err
for index, path in ipairs(testpaths) do
file, err = io.open(path, "rb")
if not err then
break
end
end
if err then
worldedit.player_notify(name, "Could not open file \"" .. param .. "\"")
return
end
local value = file:read("*a")
file:close()
local version = worldedit.read_header(value)
if version == nil or version == 0 then
worldedit.player_notify(name, "File is invalid!")
return
elseif version > worldedit.LATEST_SERIALIZATION_VERSION then
worldedit.player_notify(name, "Schematic was created with a newer version of WorldEdit.")
return
end
return value
end
worldedit.register_command("about", {
privs = {},
@ -1415,28 +1448,15 @@ worldedit.register_command("allocate", {
func = function(name, param)
local pos = worldedit.pos1[name]
local filename = minetest.get_worldpath() .. "/schems/" .. param .. ".we"
local file, err = io.open(filename, "rb")
if err ~= nil then
worldedit.player_notify(name, "could not open file \"" .. filename .. "\"")
return
local value = open_schematic(name, param)
if not value then
return false
end
local value = file:read("*a")
file:close()
local version = worldedit.read_header(value)
if version == nil or version == 0 then
worldedit.player_notify(name, "File is invalid!")
return
elseif version > worldedit.LATEST_SERIALIZATION_VERSION then
worldedit.player_notify(name, "File was created with newer version of WorldEdit!")
return
end
local nodepos1, nodepos2, count = worldedit.allocate(pos, value)
if not nodepos1 then
worldedit.player_notify(name, "Schematic empty, nothing allocated")
return
return false
end
worldedit.pos1[name] = nodepos1
@ -1464,46 +1484,16 @@ worldedit.register_command("load", {
func = function(name, param)
local pos = worldedit.pos1[name]
if param == "" then
worldedit.player_notify(name, "invalid usage: " .. param)
return
end
if not string.find(param, "^[%w \t.,+-_=!@#$%%^&*()%[%]{};'\"]+$") then
worldedit.player_notify(name, "invalid file name: " .. param)
return
end
--find the file in the world path
local testpaths = {
minetest.get_worldpath() .. "/schems/" .. param,
minetest.get_worldpath() .. "/schems/" .. param .. ".we",
minetest.get_worldpath() .. "/schems/" .. param .. ".wem",
}
local file, err
for index, path in ipairs(testpaths) do
file, err = io.open(path, "rb")
if not err then
break
end
end
if err then
worldedit.player_notify(name, "could not open file \"" .. param .. "\"")
return
end
local value = file:read("*a")
file:close()
local version = worldedit.read_header(value)
if version == nil or version == 0 then
worldedit.player_notify(name, "File is invalid!")
return
elseif version > worldedit.LATEST_SERIALIZATION_VERSION then
worldedit.player_notify(name, "File was created with newer version of WorldEdit!")
return
local value = open_schematic(name, param)
if not value then
return false
end
local count = worldedit.deserialize(pos, value)
if count == nil then
worldedit.player_notify(name, "Loading failed!")
return false
end
worldedit.player_notify(name, count .. " nodes loaded")
end,
})