Don't crash if level schematic is missing

This commit is contained in:
Wuzzy 2024-12-12 23:46:47 +01:00
parent 1434018c77
commit 060543625d
6 changed files with 28 additions and 19 deletions

View File

@ -348,19 +348,6 @@ minetest.register_chatcommand("editor_save", {
end, end,
}) })
-- Returns true if the given file exists, false otherwise.
-- * path: Path to file (without file name)
-- * filename: File name of file (without path)
local file_exists = function(path, filename)
local levels = minetest.get_dir_list(path, false)
for l=1, #levels do
if levels[l] == filename then
return true
end
end
return false
end
local load_level = function(level_name, player) local load_level = function(level_name, player)
if lzr_gamestate.get_state() ~= lzr_gamestate.EDITOR then if lzr_gamestate.get_state() ~= lzr_gamestate.EDITOR then
return false return false
@ -370,7 +357,7 @@ local load_level = function(level_name, player)
return false return false
end end
local filename = level_name..".mts" local filename = level_name..".mts"
local ok = file_exists(minetest.get_worldpath().."/levels", filename) local ok = lzr_util.file_exists(minetest.get_worldpath().."/levels", filename)
if not ok then if not ok then
return false return false
end end
@ -517,7 +504,7 @@ minetest.register_chatcommand("editor_load", {
if check_for_slash(level_name) then if check_for_slash(level_name) then
return false, S("Level name must not contain slash or backslash!") return false, S("Level name must not contain slash or backslash!")
end end
local ok = file_exists(minetest.get_worldpath().."/levels", level_name..".mts") local ok = lzr_util.file_exists(minetest.get_worldpath().."/levels", level_name..".mts")
if not ok then if not ok then
return false, S("Level file does not exist!") return false, S("Level file does not exist!")
end end

View File

@ -1,2 +1,2 @@
name = lzr_editor name = lzr_editor
depends = lzr_gamestate, lzr_levels, lzr_gui, lzr_player, lzr_node_drops, lzr_ambience, lzr_getitem, lzr_csv, lzr_globals, lzr_privs, lzr_weather, lzr_tools, lzr_hook, lzr_teleporter, lzr_treasure, lzr_mapgen, lzr_world, lzr_triggers depends = lzr_gamestate, lzr_levels, lzr_gui, lzr_player, lzr_node_drops, lzr_ambience, lzr_getitem, lzr_csv, lzr_globals, lzr_privs, lzr_weather, lzr_tools, lzr_hook, lzr_teleporter, lzr_treasure, lzr_mapgen, lzr_world, lzr_triggers, lzr_util

View File

@ -121,7 +121,9 @@ local load_custom_level = function(level, player)
local error_append = "" local error_append = ""
if error_detail then if error_detail then
local reason local reason
if error_detail == "no_teleporter" then if error_detail == "file_nonexistant" then
reason = S("The level schematic file chould not be found.")
elseif error_detail == "no_teleporter" then
reason = S("Theres no teleporter for the player to start on.") reason = S("Theres no teleporter for the player to start on.")
elseif error_detail == "too_many_teleporters" then elseif error_detail == "too_many_teleporters" then
reason = S("Theres more than one teleporter.") reason = S("Theres more than one teleporter.")
@ -133,6 +135,8 @@ local load_custom_level = function(level, player)
reason = S("Theres a bare gold block in the level.") reason = S("Theres a bare gold block in the level.")
elseif error_detail == "plant_on_ground" then elseif error_detail == "plant_on_ground" then
reason = S("Theres a rooted plant in the level.") reason = S("Theres a rooted plant in the level.")
elseif error_detail == "schematic_load_error" then
reason = S("Error while loading schematic file.")
else else
reason = error_detail reason = error_detail
end end

View File

@ -95,9 +95,14 @@ local flat_index_to_pos = function(index, size)
end end
local analyze_level_schematic = function(filename, levels_path, level_data_entry) local analyze_level_schematic = function(filename, levels_path, level_data_entry)
if not lzr_util.file_exists(levels_path, filename) then
return false, "file_nonexistant"
end
local filepath = levels_path .. "/" ..filename local filepath = levels_path .. "/" ..filename
local schem = minetest.read_schematic(filepath, {write_yslice_prob="none"}) local schem = minetest.read_schematic(filepath, {write_yslice_prob="none"})
assert(schem, "Could not load level file: "..filename) if not schem then
return false, "schematic_load_error"
end
level_data_entry.contains_rotatable_block = false level_data_entry.contains_rotatable_block = false
level_data_entry.treasures = 0 level_data_entry.treasures = 0
level_data_entry.size = schem.size level_data_entry.size = schem.size

View File

@ -1,2 +1,2 @@
name = lzr_levels name = lzr_levels
depends = lzr_core, lzr_mapgen, lzr_globals, lzr_hook, lzr_gamestate, lzr_ambience, lzr_csv, lzr_weather, lzr_laser, lzr_treasure, lzr_world, lzr_triggers, lzr_slowdown depends = lzr_core, lzr_mapgen, lzr_globals, lzr_hook, lzr_gamestate, lzr_ambience, lzr_csv, lzr_weather, lzr_laser, lzr_treasure, lzr_world, lzr_triggers, lzr_slowdown, lzr_util

View File

@ -83,3 +83,16 @@ lzr_util.hsv_to_rgb = function(h, s, v)
return math.floor(r * max_value), math.floor(g * max_value), math.floor(b * max_value) return math.floor(r * max_value), math.floor(g * max_value), math.floor(b * max_value)
end end
-- Returns true if the given file exists, false otherwise.
-- * path: Path to file (without file name)
-- * filename: File name of file (without path)
lzr_util.file_exists = function(path, filename)
local levels = minetest.get_dir_list(path, false)
for l=1, #levels do
if levels[l] == filename then
return true
end
end
return false
end