Skip to content

Commit 6b2fe39

Browse files
committedMay 16, 2015
Use minetest.mkdir when available
1 parent 5c115e2 commit 6b2fe39

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed
 

Diff for: ‎worldedit_commands/init.lua

+38-19
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@ if minetest.place_schematic then
1111
end
1212

1313
dofile(minetest.get_modpath("worldedit_commands") .. "/mark.lua")
14-
dofile(minetest.get_modpath("worldedit_commands") .. "/safe.lua"); safe_region = safe_region or function(callback) return callback end
14+
dofile(minetest.get_modpath("worldedit_commands") .. "/safe.lua")
15+
safe_region = rawget(_G, "safe_region") or function(callback) return callback end
1516

16-
local get_position = function(name) --position 1 retrieval function for when not using `safe_region`
17+
local function get_position(name) --position 1 retrieval function for when not using `safe_region`
1718
local pos1 = worldedit.pos1[name]
1819
if pos1 == nil then
1920
worldedit.player_notify(name, "no position 1 selected")
2021
end
2122
return pos1
2223
end
2324

24-
local get_node = function(name, nodename)
25+
local function get_node(name, nodename)
2526
local node = worldedit.normalize_nodename(nodename)
2627
if not node then
2728
worldedit.player_notify(name, "invalid node name: " .. nodename)
@@ -30,7 +31,7 @@ local get_node = function(name, nodename)
3031
return node
3132
end
3233

33-
worldedit.player_notify = function(name, message)
34+
function worldedit.player_notify(name, message)
3435
minetest.chat_send_player(name, "WorldEdit -!- " .. message, false)
3536
end
3637

@@ -56,8 +57,8 @@ worldedit.normalize_nodename = function(nodename)
5657
return nil
5758
end
5859

59-
--determines the axis in which a player is facing, returning an axis ("x", "y", or "z") and the sign (1 or -1)
60-
worldedit.player_axis = function(name)
60+
-- Determines the axis in which a player is facing, returning an axis ("x", "y", or "z") and the sign (1 or -1)
61+
function worldedit.player_axis(name)
6162
local dir = minetest.get_player_by_name(name):get_look_dir()
6263
local x, y, z = math.abs(dir.x), math.abs(dir.y), math.abs(dir.z)
6364
if x > y then
@@ -70,6 +71,15 @@ worldedit.player_axis = function(name)
7071
return "z", dir.z > 0 and 1 or -1
7172
end
7273

74+
function worldedit.mkdir(path)
75+
if minetest.mkdir then
76+
minetest.mkdir(path)
77+
else
78+
os.execute('mkdir "' .. path .. '"')
79+
end
80+
end
81+
82+
7383
minetest.register_chatcommand("/about", {
7484
params = "",
7585
description = "Get information about the mod",
@@ -876,20 +886,22 @@ minetest.register_chatcommand("/save", {
876886
worldedit.player_notify(name, "invalid usage: " .. param)
877887
return
878888
end
879-
if not string.find(param, "^[%w \t.,+-_=!@#$%%^&*()%[%]{};'\"]+$") then
880-
worldedit.player_notify(name, "invalid file name: " .. param)
889+
if not param:find("^[a-zA-Z0-9_%-.]+$") then
890+
worldedit.player_notify(name, "Disallowed file name: " .. param)
881891
return
882892
end
883893

884-
local result, count = worldedit.serialize(worldedit.pos1[name], worldedit.pos2[name])
894+
local result, count = worldedit.serialize(worldedit.pos1[name],
895+
worldedit.pos2[name])
885896

886897
local path = minetest.get_worldpath() .. "/schems"
898+
-- Create directory if it does not already exist
899+
worldedit.mkdir(path)
900+
887901
local filename = path .. "/" .. param .. ".we"
888-
filename = filename:gsub("\"", "\\\""):gsub("\\", "\\\\") --escape any nasty characters
889-
os.execute("mkdir \"" .. path .. "\"") --create directory if it does not already exist
890902
local file, err = io.open(filename, "wb")
891903
if err ~= nil then
892-
worldedit.player_notify(name, "could not save file to \"" .. filename .. "\"")
904+
worldedit.player_notify(name, "Could not save file to \"" .. filename .. "\"")
893905
return
894906
end
895907
file:write(result)
@@ -1037,24 +1049,31 @@ minetest.register_chatcommand("/luatransform", {
10371049

10381050
minetest.register_chatcommand("/mtschemcreate", {
10391051
params = "<file>",
1040-
description = "Save the current WorldEdit region using the Minetest Schematic format to \"(world folder)/schems/<filename>.mts\"",
1052+
description = "Save the current WorldEdit region using the Minetest "..
1053+
"Schematic format to \"(world folder)/schems/<filename>.mts\"",
10411054
privs = {worldedit=true},
10421055
func = safe_region(function(name, param)
10431056
if param == nil then
10441057
worldedit.player_notify(name, "No filename specified")
10451058
return
10461059
end
1060+
if not param:find("^[a-zA-Z0-9_%-.]+$") then
1061+
worldedit.player_notify(name, "Disallowed file name: " .. param)
1062+
return
1063+
end
10471064

10481065
local path = minetest.get_worldpath() .. "/schems"
1049-
local filename = path .. "/" .. param .. ".mts"
1050-
filename = filename:gsub("\"", "\\\""):gsub("\\", "\\\\") --escape any nasty characters
1051-
os.execute("mkdir \"" .. path .. "\"") --create directory if it does not already exist
1066+
-- Create directory if it does not already exist
1067+
worldedit.mkdir(path)
10521068

1053-
local ret = minetest.create_schematic(worldedit.pos1[name], worldedit.pos2[name], worldedit.prob_list[name], filename)
1069+
local filename = path .. "/" .. param .. ".mts"
1070+
local ret = minetest.create_schematic(worldedit.pos1[name],
1071+
worldedit.pos2[name], worldedit.prob_list[name],
1072+
filename)
10541073
if ret == nil then
1055-
worldedit.player_notify(name, "failed to create Minetest schematic", false)
1074+
worldedit.player_notify(name, "Failed to create Minetest schematic", false)
10561075
else
1057-
worldedit.player_notify(name, "saved Minetest schematic to " .. param, false)
1076+
worldedit.player_notify(name, "Saved Minetest schematic to " .. param, false)
10581077
end
10591078
worldedit.prob_list[name] = {}
10601079
end),

0 commit comments

Comments
 (0)