]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/misc_helpers.lua
Add translation for main menu
[dragonfireclient.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 file_exists(filename)
90         local f = io.open(filename, "r")
91         if f==nil then
92                 return false
93         else
94                 f:close()
95                 return true
96         end
97 end
98
99 --------------------------------------------------------------------------------
100 function string:trim()
101         return (self:gsub("^%s*(.-)%s*$", "%1"))
102 end
103
104 assert(string.trim("\n \t\tfoo bar\t ") == "foo bar")
105
106 --------------------------------------------------------------------------------
107 function math.hypot(x, y)
108         local t
109         x = math.abs(x)
110         y = math.abs(y)
111         t = math.min(x, y)
112         x = math.max(x, y)
113         if x == 0 then return 0 end
114         t = t / x
115         return x * math.sqrt(1 + t * t)
116 end
117
118 --------------------------------------------------------------------------------
119 function explode_textlist_event(text)
120         
121         local retval = {}
122         retval.typ = "INV"
123         
124         local parts = text:split(":")
125                                 
126         if #parts == 2 then
127                 retval.typ = parts[1]:trim()
128                 retval.index= tonumber(parts[2]:trim())
129                 
130                 if type(retval.index) ~= "number" then
131                         retval.typ = "INV"
132                 end
133         end
134         
135         return retval
136 end
137
138 --------------------------------------------------------------------------------
139 function get_last_folder(text,count)
140         local parts = text:split(DIR_DELIM)
141         
142         if count == nil then
143                 return parts[#parts]
144         end
145         
146         local retval = ""
147         for i=1,count,1 do
148                 retval = retval .. parts[#parts - (count-i)] .. DIR_DELIM
149         end
150         
151         return retval
152 end
153
154 --------------------------------------------------------------------------------
155 function cleanup_path(temppath)
156         
157         local 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         parts = temppath:split(" ")
185         temppath = ""   
186         for i=1,#parts,1 do
187                 if temppath ~= "" then
188                         temppath = temppath
189                 end
190                 temppath = temppath .. parts[i]
191         end
192         
193         return temppath
194 end
195
196 local tbl = engine or minetest
197 function tbl.formspec_escape(text)
198         if text ~= nil then
199                 text = string.gsub(text,"\\","\\\\")
200                 text = string.gsub(text,"%]","\\]")
201                 text = string.gsub(text,"%[","\\[")
202                 text = string.gsub(text,";","\\;")
203                 text = string.gsub(text,",","\\,")
204         end
205         return text
206 end
207
208 --------------------------------------------------------------------------------
209 -- mainmenu only functions
210 --------------------------------------------------------------------------------
211 if engine ~= nil then
212         engine.get_game = function(index)
213                 local games = game.get_games()
214                 
215                 if index > 0 and index <= #games then
216                         return games[index]
217                 end
218                 
219                 return nil
220         end
221         
222         function fgettext(text, ...)
223                 text = engine.gettext(text)
224                 local arg = {n=select('#', ...), ...}
225                 if arg.n >= 1 then
226                         -- Insert positional parameters ($1, $2, ...)
227                         result = ''
228                         pos = 1
229                         while pos <= text:len() do
230                                 newpos = text:find('[$]', pos)
231                                 if newpos == nil then
232                                         result = result .. text:sub(pos)
233                                         pos = text:len() + 1
234                                 else
235                                         paramindex = tonumber(text:sub(newpos+1, newpos+1))
236                                         result = result .. text:sub(pos, newpos-1) .. tostring(arg[paramindex])
237                                         pos = newpos + 2
238                                 end
239                         end
240                         text = result
241                 end
242                 return engine.formspec_escape(text)
243         end
244 end
245 --------------------------------------------------------------------------------
246 -- core only fct
247 --------------------------------------------------------------------------------
248 if minetest ~= nil then
249         --------------------------------------------------------------------------------
250         function minetest.pos_to_string(pos)
251                 return "(" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ")"
252         end
253 end
254