1 -- Minetest: builtin/misc_helpers.lua
3 --------------------------------------------------------------------------------
4 function basic_dump2(o)
5 if type(o) == "number" then
7 elseif type(o) == "string" then
8 return string.format("%q", o)
9 elseif type(o) == "boolean" then
11 elseif type(o) == "function" then
13 elseif type(o) == "userdata" then
15 elseif type(o) == "nil" then
18 error("cannot dump a " .. type(o))
23 --------------------------------------------------------------------------------
24 function dump2(o, name, dumped)
28 if type(o) == "number" or type(o) == "string" or type(o) == "boolean"
29 or type(o) == "function" or type(o) == "nil"
30 or type(o) == "userdata" then
31 io.write(basic_dump2(o), "\n")
32 elseif type(o) == "table" then
34 io.write(dumped[o], "\n")
37 io.write("{}\n") -- new table
38 for k,v in pairs(o) do
39 local fieldname = string.format("%s[%s]", name, basic_dump2(k))
40 dump2(v, fieldname, dumped)
44 error("cannot dump a " .. type(o))
49 --------------------------------------------------------------------------------
50 function dump(o, dumped)
52 if type(o) == "number" then
54 elseif type(o) == "string" then
55 return string.format("%q", o)
56 elseif type(o) == "table" then
58 return "<circular reference>"
62 for k,v in pairs(o) do
63 t[#t+1] = "[" .. dump(k, dumped) .. "] = " .. dump(v, dumped)
65 return "{" .. table.concat(t, ", ") .. "}"
66 elseif type(o) == "boolean" then
68 elseif type(o) == "function" then
70 elseif type(o) == "userdata" then
72 elseif type(o) == "nil" then
75 error("cannot dump a " .. type(o))
80 --------------------------------------------------------------------------------
81 function string:split(sep)
82 local sep, fields = sep or ",", {}
83 local pattern = string.format("([^%s]+)", sep)
84 self:gsub(pattern, function(c) fields[#fields+1] = c end)
88 --------------------------------------------------------------------------------
89 function string:trim()
90 return (self:gsub("^%s*(.-)%s*$", "%1"))
93 assert(string.trim("\n \t\tfoo bar\t ") == "foo bar")
97 --------------------------------------------------------------------------------
98 function math.hypot(x, y)
104 if x == 0 then return 0 end
106 return x * math.sqrt(1 + t * t)
109 --------------------------------------------------------------------------------
110 function explode_textlist_event(text)
115 local parts = text:split(":")
118 retval.typ = parts[1]:trim()
119 retval.index= tonumber(parts[2]:trim())
121 if type(retval.index) ~= "number" then
129 --------------------------------------------------------------------------------
130 function get_last_folder(text,count)
131 local parts = text:split(DIR_DELIM)
139 retval = retval .. parts[#parts - (count-i)] .. DIR_DELIM
145 --------------------------------------------------------------------------------
146 function cleanup_path(temppath)
148 local parts = temppath:split("-")
151 if temppath ~= "" then
152 temppath = temppath .. "_"
154 temppath = temppath .. parts[i]
157 parts = temppath:split(".")
160 if temppath ~= "" then
161 temppath = temppath .. "_"
163 temppath = temppath .. parts[i]
166 parts = temppath:split("'")
169 if temppath ~= "" then
170 temppath = temppath .. ""
172 temppath = temppath .. parts[i]
175 parts = temppath:split(" ")
178 if temppath ~= "" then
181 temppath = temppath .. parts[i]
187 local tbl = engine or minetest
188 function tbl.formspec_escape(text)
190 text = string.gsub(text,"\\","\\\\")
191 text = string.gsub(text,"%]","\\]")
192 text = string.gsub(text,"%[","\\[")
193 text = string.gsub(text,";","\\;")
194 text = string.gsub(text,",","\\,")
199 --------------------------------------------------------------------------------
200 -- mainmenu only functions
201 --------------------------------------------------------------------------------
202 if engine ~= nil then
203 engine.get_game = function(index)
204 local games = game.get_games()
206 if index > 0 and index <= #games then
213 --------------------------------------------------------------------------------
215 --------------------------------------------------------------------------------
216 if minetest ~= nil then
217 --------------------------------------------------------------------------------
218 function minetest.pos_to_string(pos)
219 return "(" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ")"