]> git.lizzy.rs Git - minetest.git/blob - builtin/misc_helpers.lua
Add ability to activate mods with doubleclick (remove old_style_mod_selection)
[minetest.git] / builtin / misc_helpers.lua
1 -- Minetest: builtin/misc_helpers.lua
2
3 --------------------------------------------------------------------------------
4 function basic_dump2(o)
5         if type(o) == "number" then
6                 return tostring(o)
7         elseif type(o) == "string" then
8                 return string.format("%q", o)
9         elseif type(o) == "boolean" then
10                 return tostring(o)
11         elseif type(o) == "function" then
12                 return "<function>"
13         elseif type(o) == "userdata" then
14                 return "<userdata>"
15         elseif type(o) == "nil" then
16                 return "nil"
17         else
18                 error("cannot dump a " .. type(o))
19                 return nil
20         end
21 end
22
23 --------------------------------------------------------------------------------
24 function dump2(o, name, dumped)
25         name = name or "_"
26         dumped = dumped or {}
27         io.write(name, " = ")
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
33                 if dumped[o] then
34                         io.write(dumped[o], "\n")
35                 else
36                         dumped[o] = name
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)
41                         end
42                 end
43         else
44                 error("cannot dump a " .. type(o))
45                 return nil
46         end
47 end
48
49 --------------------------------------------------------------------------------
50 function dump(o, dumped)
51         dumped = dumped or {}
52         if type(o) == "number" then
53                 return tostring(o)
54         elseif type(o) == "string" then
55                 return string.format("%q", o)
56         elseif type(o) == "table" then
57                 if dumped[o] then
58                         return "<circular reference>"
59                 end
60                 dumped[o] = true
61                 local t = {}
62                 for k,v in pairs(o) do
63                         t[#t+1] = "[" .. dump(k, dumped) .. "] = " .. dump(v, dumped)
64                 end
65                 return "{" .. table.concat(t, ", ") .. "}"
66         elseif type(o) == "boolean" then
67                 return tostring(o)
68         elseif type(o) == "function" then
69                 return "<function>"
70         elseif type(o) == "userdata" then
71                 return "<userdata>"
72         elseif type(o) == "nil" then
73                 return "nil"
74         else
75                 error("cannot dump a " .. type(o))
76                 return nil
77         end
78 end
79
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)
85         return fields
86 end
87
88 --------------------------------------------------------------------------------
89 function string:trim()
90         return (self:gsub("^%s*(.-)%s*$", "%1"))
91 end
92
93 assert(string.trim("\n \t\tfoo bar\t ") == "foo bar")
94
95
96
97 --------------------------------------------------------------------------------
98 function math.hypot(x, y)
99         local t
100         x = math.abs(x)
101         y = math.abs(y)
102         t = math.min(x, y)
103         x = math.max(x, y)
104         if x == 0 then return 0 end
105         t = t / x
106         return x * math.sqrt(1 + t * t)
107 end
108
109 --------------------------------------------------------------------------------
110 function explode_textlist_event(text)
111         
112         local retval = {}
113         retval.typ = "INV"
114         
115         local parts = text:split(":")
116                                 
117         if #parts == 2 then
118                 retval.typ = parts[1]:trim()
119                 retval.index= tonumber(parts[2]:trim())
120                 
121                 if type(retval.index) ~= "number" then
122                         retval.typ = "INV"
123                 end
124         end
125         
126         return retval
127 end
128
129 --------------------------------------------------------------------------------
130 function get_last_folder(text,count)
131         local parts = text:split(DIR_DELIM)
132         
133         if count == nil then
134                 return parts[#parts]
135         end
136         
137         local retval = ""
138         for i=1,count,1 do
139                 retval = retval .. parts[#parts - (count-i)] .. DIR_DELIM
140         end
141         
142         return retval
143 end
144
145 --------------------------------------------------------------------------------
146 function cleanup_path(temppath)
147         
148         local parts = temppath:split("-")
149         temppath = ""   
150         for i=1,#parts,1 do
151                 if temppath ~= "" then
152                         temppath = temppath .. "_"
153                 end
154                 temppath = temppath .. parts[i]
155         end
156         
157         parts = temppath:split(".")
158         temppath = ""   
159         for i=1,#parts,1 do
160                 if temppath ~= "" then
161                         temppath = temppath .. "_"
162                 end
163                 temppath = temppath .. parts[i]
164         end
165         
166         parts = temppath:split("'")
167         temppath = ""   
168         for i=1,#parts,1 do
169                 if temppath ~= "" then
170                         temppath = temppath .. ""
171                 end
172                 temppath = temppath .. parts[i]
173         end
174         
175         parts = temppath:split(" ")
176         temppath = ""   
177         for i=1,#parts,1 do
178                 if temppath ~= "" then
179                         temppath = temppath
180                 end
181                 temppath = temppath .. parts[i]
182         end
183         
184         return temppath
185 end
186
187 local tbl = engine or minetest
188 function tbl.formspec_escape(text)
189         if text ~= nil then
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,",","\\,")
195         end
196         return text
197 end
198
199 --------------------------------------------------------------------------------
200 -- mainmenu only functions
201 --------------------------------------------------------------------------------
202 if engine ~= nil then
203         engine.get_game = function(index)
204                 local games = game.get_games()
205                 
206                 if index > 0 and index <= #games then
207                         return games[index]
208                 end
209                 
210                 return nil
211         end
212 end
213 --------------------------------------------------------------------------------
214 -- core only fct
215 --------------------------------------------------------------------------------
216 if minetest ~= nil then
217         --------------------------------------------------------------------------------
218         function minetest.pos_to_string(pos)
219                 return "(" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ")"
220         end
221 end
222