]> git.lizzy.rs Git - minetest.git/blob - builtin/mainmenu/common.lua
Advanced settings noiseparams: No tailing comma for empty flags
[minetest.git] / builtin / mainmenu / common.lua
1 --Minetest
2 --Copyright (C) 2014 sapier
3 --
4 --This program is free software; you can redistribute it and/or modify
5 --it under the terms of the GNU Lesser General Public License as published by
6 --the Free Software Foundation; either version 2.1 of the License, or
7 --(at your option) any later version.
8 --
9 --This program is distributed in the hope that it will be useful,
10 --but WITHOUT ANY WARRANTY; without even the implied warranty of
11 --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 --GNU Lesser General Public License for more details.
13 --
14 --You should have received a copy of the GNU Lesser General Public License along
15 --with this program; if not, write to the Free Software Foundation, Inc.,
16 --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 --------------------------------------------------------------------------------
18 -- Global menu data
19 --------------------------------------------------------------------------------
20 menudata = {}
21
22 --------------------------------------------------------------------------------
23 -- Local cached values
24 --------------------------------------------------------------------------------
25 local min_supp_proto, max_supp_proto
26
27 function common_update_cached_supp_proto()
28         min_supp_proto = core.get_min_supp_proto()
29         max_supp_proto = core.get_max_supp_proto()
30 end
31 common_update_cached_supp_proto()
32 --------------------------------------------------------------------------------
33 -- Menu helper functions
34 --------------------------------------------------------------------------------
35
36 --------------------------------------------------------------------------------
37 local function render_client_count(n)
38         if     n > 99 then return '99+'
39         elseif n >= 0 then return tostring(n)
40         else return '?' end
41 end
42
43 local function configure_selected_world_params(idx)
44         local worldconfig = pkgmgr.get_worldconfig(menudata.worldlist:get_list()[idx].path)
45         if worldconfig.creative_mode then
46                 core.settings:set("creative_mode", worldconfig.creative_mode)
47         end
48         if worldconfig.enable_damage then
49                 core.settings:set("enable_damage", worldconfig.enable_damage)
50         end
51 end
52
53 --------------------------------------------------------------------------------
54 function image_column(tooltip, flagname)
55         return "image,tooltip=" .. core.formspec_escape(tooltip) .. "," ..
56                 "0=" .. core.formspec_escape(defaulttexturedir .. "blank.png") .. "," ..
57                 "1=" .. core.formspec_escape(defaulttexturedir ..
58                         (flagname and "server_flags_" .. flagname .. ".png" or "blank.png")) .. "," ..
59                 "2=" .. core.formspec_escape(defaulttexturedir .. "server_ping_4.png") .. "," ..
60                 "3=" .. core.formspec_escape(defaulttexturedir .. "server_ping_3.png") .. "," ..
61                 "4=" .. core.formspec_escape(defaulttexturedir .. "server_ping_2.png") .. "," ..
62                 "5=" .. core.formspec_escape(defaulttexturedir .. "server_ping_1.png")
63 end
64
65 --------------------------------------------------------------------------------
66 function order_favorite_list(list)
67         local res = {}
68         --orders the favorite list after support
69         for i = 1, #list do
70                 local fav = list[i]
71                 if is_server_protocol_compat(fav.proto_min, fav.proto_max) then
72                         res[#res + 1] = fav
73                 end
74         end
75         for i = 1, #list do
76                 local fav = list[i]
77                 if not is_server_protocol_compat(fav.proto_min, fav.proto_max) then
78                         res[#res + 1] = fav
79                 end
80         end
81         return res
82 end
83
84 --------------------------------------------------------------------------------
85 function render_serverlist_row(spec, is_favorite)
86         local text = ""
87         if spec.name then
88                 text = text .. core.formspec_escape(spec.name:trim())
89         elseif spec.address then
90                 text = text .. spec.address:trim()
91                 if spec.port then
92                         text = text .. ":" .. spec.port
93                 end
94         end
95
96         local details = ""
97         local grey_out = not is_server_protocol_compat(spec.proto_min, spec.proto_max)
98
99         if is_favorite then
100                 details = "1,"
101         else
102                 details = "0,"
103         end
104
105         if spec.ping then
106                 local ping = spec.ping * 1000
107                 if ping <= 50 then
108                         details = details .. "2,"
109                 elseif ping <= 100 then
110                         details = details .. "3,"
111                 elseif ping <= 250 then
112                         details = details .. "4,"
113                 else
114                         details = details .. "5,"
115                 end
116         else
117                 details = details .. "0,"
118         end
119
120         if spec.clients and spec.clients_max then
121                 local clients_color = ''
122                 local clients_percent = 100 * spec.clients / spec.clients_max
123
124                 -- Choose a color depending on how many clients are connected
125                 -- (relatively to clients_max)
126                 if     grey_out               then clients_color = '#aaaaaa'
127                 elseif spec.clients == 0      then clients_color = ''        -- 0 players: default/white
128                 elseif clients_percent <= 60  then clients_color = '#a1e587' -- 0-60%: green
129                 elseif clients_percent <= 90  then clients_color = '#ffdc97' -- 60-90%: yellow
130                 elseif clients_percent == 100 then clients_color = '#dd5b5b' -- full server: red (darker)
131                 else                               clients_color = '#ffba97' -- 90-100%: orange
132                 end
133
134                 details = details .. clients_color .. ',' ..
135                         render_client_count(spec.clients) .. ',/,' ..
136                         render_client_count(spec.clients_max) .. ','
137
138         elseif grey_out then
139                 details = details .. '#aaaaaa,?,/,?,'
140         else
141                 details = details .. ',?,/,?,'
142         end
143
144         if spec.creative then
145                 details = details .. "1,"
146         else
147                 details = details .. "0,"
148         end
149
150         if spec.damage then
151                 details = details .. "1,"
152         else
153                 details = details .. "0,"
154         end
155
156         if spec.pvp then
157                 details = details .. "1,"
158         else
159                 details = details .. "0,"
160         end
161
162         return details .. (grey_out and '#aaaaaa,' or ',') .. text
163 end
164
165 --------------------------------------------------------------------------------
166 os.tempfolder = function()
167         if core.settings:get("TMPFolder") then
168                 return core.settings:get("TMPFolder") .. DIR_DELIM .. "MT_" .. math.random(0,10000)
169         end
170
171         local filetocheck = os.tmpname()
172         os.remove(filetocheck)
173
174         -- https://blogs.msdn.microsoft.com/vcblog/2014/06/18/c-runtime-crt-features-fixes-and-breaking-changes-in-visual-studio-14-ctp1/
175         --   The C runtime (CRT) function called by os.tmpname is tmpnam.
176         --   Microsofts tmpnam implementation in older CRT / MSVC releases is defective.
177         --   tmpnam return values starting with a backslash characterize this behavior.
178         -- https://sourceforge.net/p/mingw-w64/bugs/555/
179         --   MinGW tmpnam implementation is forwarded to the CRT directly.
180         -- https://sourceforge.net/p/mingw-w64/discussion/723797/thread/55520785/
181         --   MinGW links to an older CRT release (msvcrt.dll).
182         --   Due to legal concerns MinGW will never use a newer CRT.
183         --
184         --   Make use of TEMP to compose the temporary filename if an old
185         --   style tmpnam return value is detected.
186         if filetocheck:sub(1, 1) == "\\" then
187                 local tempfolder = os.getenv("TEMP")
188                 return tempfolder .. filetocheck
189         end
190
191         local randname = "MTTempModFolder_" .. math.random(0,10000)
192         local backstring = filetocheck:reverse()
193         return filetocheck:sub(0, filetocheck:len() - backstring:find(DIR_DELIM) + 1) ..
194                 randname
195 end
196
197 --------------------------------------------------------------------------------
198 function menu_render_worldlist()
199         local retval = ""
200         local current_worldlist = menudata.worldlist:get_list()
201
202         for i, v in ipairs(current_worldlist) do
203                 if retval ~= "" then retval = retval .. "," end
204                 retval = retval .. core.formspec_escape(v.name) ..
205                                 " \\[" .. core.formspec_escape(v.gameid) .. "\\]"
206         end
207
208         return retval
209 end
210
211 --------------------------------------------------------------------------------
212 function menu_handle_key_up_down(fields, textlist, settingname)
213         local oldidx, newidx = core.get_textlist_index(textlist), 1
214         if fields.key_up or fields.key_down then
215                 if fields.key_up and oldidx and oldidx > 1 then
216                         newidx = oldidx - 1
217                 elseif fields.key_down and oldidx and
218                                 oldidx < menudata.worldlist:size() then
219                         newidx = oldidx + 1
220                 end
221                 core.settings:set(settingname, menudata.worldlist:get_raw_index(newidx))
222                 configure_selected_world_params(newidx)
223                 return true
224         end
225         return false
226 end
227
228 --------------------------------------------------------------------------------
229 function asyncOnlineFavourites()
230         if not menudata.public_known then
231                 menudata.public_known = {{
232                         name = fgettext("Loading..."),
233                         description = fgettext_ne("Try reenabling public serverlist and check your internet connection.")
234                 }}
235         end
236         menudata.favorites = menudata.public_known
237         menudata.favorites_is_public = true
238
239         if not menudata.public_downloading then
240                 menudata.public_downloading = true
241         else
242                 return
243         end
244
245         core.handle_async(
246                 function(param)
247                         return core.get_favorites("online")
248                 end,
249                 nil,
250                 function(result)
251                         menudata.public_downloading = nil
252                         local favs = order_favorite_list(result)
253                         if favs[1] then
254                                 menudata.public_known = favs
255                                 menudata.favorites = menudata.public_known
256                                 menudata.favorites_is_public = true
257                         end
258                         core.event_handler("Refresh")
259                 end
260         )
261 end
262
263 --------------------------------------------------------------------------------
264 function text2textlist(xpos, ypos, width, height, tl_name, textlen, text, transparency)
265         local textlines = core.wrap_text(text, textlen, true)
266         local retval = "textlist[" .. xpos .. "," .. ypos .. ";" .. width ..
267                         "," .. height .. ";" .. tl_name .. ";"
268
269         for i = 1, #textlines do
270                 textlines[i] = textlines[i]:gsub("\r", "")
271                 retval = retval .. core.formspec_escape(textlines[i]) .. ","
272         end
273
274         retval = retval .. ";0;"
275         if transparency then retval = retval .. "true" end
276         retval = retval .. "]"
277
278         return retval
279 end
280
281 --------------------------------------------------------------------------------
282 function is_server_protocol_compat(server_proto_min, server_proto_max)
283         if (not server_proto_min) or (not server_proto_max) then
284                 -- There is no info. Assume the best and act as if we would be compatible.
285                 return true
286         end
287         return min_supp_proto <= server_proto_max and max_supp_proto >= server_proto_min
288 end
289 --------------------------------------------------------------------------------
290 function is_server_protocol_compat_or_error(server_proto_min, server_proto_max)
291         if not is_server_protocol_compat(server_proto_min, server_proto_max) then
292                 local server_prot_ver_info, client_prot_ver_info
293                 local s_p_min = server_proto_min
294                 local s_p_max = server_proto_max
295
296                 if s_p_min ~= s_p_max then
297                         server_prot_ver_info = fgettext_ne("Server supports protocol versions between $1 and $2. ",
298                                 s_p_min, s_p_max)
299                 else
300                         server_prot_ver_info = fgettext_ne("Server enforces protocol version $1. ",
301                                 s_p_min)
302                 end
303                 if min_supp_proto ~= max_supp_proto then
304                         client_prot_ver_info= fgettext_ne("We support protocol versions between version $1 and $2.",
305                                 min_supp_proto, max_supp_proto)
306                 else
307                         client_prot_ver_info = fgettext_ne("We only support protocol version $1.", min_supp_proto)
308                 end
309                 gamedata.errormessage = fgettext_ne("Protocol version mismatch. ")
310                         .. server_prot_ver_info
311                         .. client_prot_ver_info
312                 return false
313         end
314
315         return true
316 end
317 --------------------------------------------------------------------------------
318 function menu_worldmt(selected, setting, value)
319         local world = menudata.worldlist:get_list()[selected]
320         if world then
321                 local filename = world.path .. DIR_DELIM .. "world.mt"
322                 local world_conf = Settings(filename)
323
324                 if value then
325                         if not world_conf:write() then
326                                 core.log("error", "Failed to write world config file")
327                         end
328                         world_conf:set(setting, value)
329                         world_conf:write()
330                 else
331                         return world_conf:get(setting)
332                 end
333         else
334                 return nil
335         end
336 end
337
338 function menu_worldmt_legacy(selected)
339         local modes_names = {"creative_mode", "enable_damage", "server_announce"}
340         for _, mode_name in pairs(modes_names) do
341                 local mode_val = menu_worldmt(selected, mode_name)
342                 if mode_val then
343                         core.settings:set(mode_name, mode_val)
344                 else
345                         menu_worldmt(selected, mode_name, core.settings:get(mode_name))
346                 end
347         end
348 end