2 --Copyright (C) 2014 sapier
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.
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.
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 --------------------------------------------------------------------------------
19 --------------------------------------------------------------------------------
22 --------------------------------------------------------------------------------
23 -- Local cached values
24 --------------------------------------------------------------------------------
25 local min_supp_proto, max_supp_proto
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()
31 common_update_cached_supp_proto()
32 --------------------------------------------------------------------------------
33 -- Menu helper functions
34 --------------------------------------------------------------------------------
36 --------------------------------------------------------------------------------
37 local function render_client_count(n)
38 if n > 99 then return '99+'
39 elseif n >= 0 then return tostring(n)
43 local function configure_selected_world_params(idx)
44 local worldconfig = modmgr.get_worldconfig(menudata.worldlist:get_list()[idx].path)
45 if worldconfig.creative_mode then
46 core.setting_set("creative_mode", worldconfig.creative_mode)
48 if worldconfig.enable_damage then
49 core.setting_set("enable_damage", worldconfig.enable_damage)
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")
65 --------------------------------------------------------------------------------
66 function order_favorite_list(list)
68 --orders the favorite list after support
71 if is_server_protocol_compat(fav.proto_min, fav.proto_max) then
77 if not is_server_protocol_compat(fav.proto_min, fav.proto_max) then
84 --------------------------------------------------------------------------------
85 function render_serverlist_row(spec, is_favorite)
88 text = text .. core.formspec_escape(spec.name:trim())
89 elseif spec.address then
90 text = text .. spec.address:trim()
92 text = text .. ":" .. spec.port
97 local grey_out = not is_server_protocol_compat(spec.proto_min, spec.proto_max)
106 local ping = spec.ping * 1000
108 details = details .. "2,"
109 elseif ping <= 100 then
110 details = details .. "3,"
111 elseif ping <= 250 then
112 details = details .. "4,"
114 details = details .. "5,"
117 details = details .. "0,"
120 if spec.clients and spec.clients_max then
121 local clients_color = ''
122 local clients_percent = 100 * spec.clients / spec.clients_max
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
134 details = details .. clients_color .. ',' ..
135 render_client_count(spec.clients) .. ',/,' ..
136 render_client_count(spec.clients_max) .. ','
139 details = details .. '#aaaaaa,?,/,?,'
141 details = details .. ',?,/,?,'
144 if spec.creative then
145 details = details .. "1,"
147 details = details .. "0,"
151 details = details .. "1,"
153 details = details .. "0,"
157 details = details .. "1,"
159 details = details .. "0,"
162 return details .. (grey_out and '#aaaaaa,' or ',') .. text
165 --------------------------------------------------------------------------------
166 os.tempfolder = function()
167 if core.setting_get("TMPFolder") then
168 return core.setting_get("TMPFolder") .. DIR_DELIM .. "MT_" .. math.random(0,10000)
171 local filetocheck = os.tmpname()
172 os.remove(filetocheck)
174 local randname = "MTTempModFolder_" .. math.random(0,10000)
175 if DIR_DELIM == "\\" then
176 local tempfolder = os.getenv("TEMP")
177 return tempfolder .. filetocheck
179 local backstring = filetocheck:reverse()
180 return filetocheck:sub(0,filetocheck:len()-backstring:find(DIR_DELIM)+1) ..randname
185 --------------------------------------------------------------------------------
186 function menu_render_worldlist()
188 local current_worldlist = menudata.worldlist:get_list()
190 for i, v in ipairs(current_worldlist) do
191 if retval ~= "" then retval = retval .. "," end
192 retval = retval .. core.formspec_escape(v.name) ..
193 " \\[" .. core.formspec_escape(v.gameid) .. "\\]"
199 --------------------------------------------------------------------------------
200 function menu_handle_key_up_down(fields, textlist, settingname)
201 local oldidx, newidx = core.get_textlist_index(textlist), 1
202 if fields.key_up or fields.key_down then
203 if fields.key_up and oldidx and oldidx > 1 then
205 elseif fields.key_down and oldidx and
206 oldidx < menudata.worldlist:size() then
209 core.setting_set(settingname, menudata.worldlist:get_raw_index(newidx))
210 configure_selected_world_params(newidx)
216 --------------------------------------------------------------------------------
217 function asyncOnlineFavourites()
218 if not menudata.public_known then
219 menudata.public_known = {{
220 name = fgettext("Loading..."),
221 description = fgettext_ne("Try reenabling public serverlist and check your internet connection.")
224 menudata.favorites = menudata.public_known
225 menudata.favorites_is_public = true
227 if not menudata.public_downloading then
228 menudata.public_downloading = true
235 return core.get_favorites("online")
239 menudata.public_downloading = nil
240 local favs = order_favorite_list(result)
242 menudata.public_known = favs
243 menudata.favorites = menudata.public_known
244 menudata.favorites_is_public = true
246 core.event_handler("Refresh")
251 --------------------------------------------------------------------------------
252 function text2textlist(xpos, ypos, width, height, tl_name, textlen, text, transparency)
253 local textlines = core.splittext(text, textlen)
254 local retval = "textlist[" .. xpos .. "," .. ypos .. ";" .. width ..
255 "," .. height .. ";" .. tl_name .. ";"
257 for i = 1, #textlines do
258 textlines[i] = textlines[i]:gsub("\r", "")
259 retval = retval .. core.formspec_escape(textlines[i]) .. ","
262 retval = retval .. ";0;"
263 if transparency then retval = retval .. "true" end
264 retval = retval .. "]"
269 --------------------------------------------------------------------------------
270 function is_server_protocol_compat(server_proto_min, server_proto_max)
271 if (not server_proto_min) or (not server_proto_max) then
272 -- There is no info. Assume the best and act as if we would be compatible.
275 return min_supp_proto <= server_proto_max and max_supp_proto >= server_proto_min
277 --------------------------------------------------------------------------------
278 function is_server_protocol_compat_or_error(server_proto_min, server_proto_max)
279 if not is_server_protocol_compat(server_proto_min, server_proto_max) then
280 local server_prot_ver_info, client_prot_ver_info
281 local s_p_min = server_proto_min
282 local s_p_max = server_proto_max
284 if s_p_min ~= s_p_max then
285 server_prot_ver_info = fgettext_ne("Server supports protocol versions between $1 and $2. ",
288 server_prot_ver_info = fgettext_ne("Server enforces protocol version $1. ",
291 if min_supp_proto ~= max_supp_proto then
292 client_prot_ver_info= fgettext_ne("We support protocol versions between version $1 and $2.",
293 min_supp_proto, max_supp_proto)
295 client_prot_ver_info = fgettext_ne("We only support protocol version $1.", min_supp_proto)
297 gamedata.errormessage = fgettext_ne("Protocol version mismatch. ")
298 .. server_prot_ver_info
299 .. client_prot_ver_info
305 --------------------------------------------------------------------------------
306 function menu_worldmt(selected, setting, value)
307 local world = menudata.worldlist:get_list()[selected]
309 local filename = world.path .. DIR_DELIM .. "world.mt"
310 local world_conf = Settings(filename)
313 if not world_conf:write() then
314 core.log("error", "Failed to write world config file")
316 world_conf:set(setting, value)
319 return world_conf:get(setting)
326 function menu_worldmt_legacy(selected)
327 local modes_names = {"creative_mode", "enable_damage", "server_announce"}
328 for _, mode_name in pairs(modes_names) do
329 local mode_val = menu_worldmt(selected, mode_name)
331 core.setting_set(mode_name, mode_val)
333 menu_worldmt(selected, mode_name, core.setting_get(mode_name))