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 .. "server_flags_" .. flagname .. ".png")
60 --------------------------------------------------------------------------------
61 function order_favorite_list(list)
63 --orders the favorite list after support
66 if is_server_protocol_compat(fav.proto_min, fav.proto_max) then
72 if not is_server_protocol_compat(fav.proto_min, fav.proto_max) then
79 --------------------------------------------------------------------------------
80 function render_favorite(spec, is_favorite)
83 text = text .. core.formspec_escape(spec.name:trim())
84 elseif spec.address then
85 text = text .. spec.address:trim()
87 text = text .. ":" .. spec.port
92 local grey_out = not is_server_protocol_compat(spec.proto_min, spec.proto_max)
100 if spec.clients and spec.clients_max then
101 local clients_color = ''
102 local clients_percent = 100 * spec.clients / spec.clients_max
104 -- Choose a color depending on how many clients are connected
105 -- (relatively to clients_max)
106 if grey_out then clients_color = '#aaaaaa'
107 elseif spec.clients == 0 then clients_color = '' -- 0 players: default/white
108 elseif clients_percent <= 60 then clients_color = '#a1e587' -- 0-60%: green
109 elseif clients_percent <= 90 then clients_color = '#ffdc97' -- 60-90%: yellow
110 elseif clients_percent == 100 then clients_color = '#dd5b5b' -- full server: red (darker)
111 else clients_color = '#ffba97' -- 90-100%: orange
114 details = details .. clients_color .. ',' ..
115 render_client_count(spec.clients) .. ',/,' ..
116 render_client_count(spec.clients_max) .. ','
119 details = details .. '#aaaaaa,?,/,?,'
121 details = details .. ',?,/,?,'
124 if spec.creative then
125 details = details .. "1,"
127 details = details .. "0,"
131 details = details .. "1,"
133 details = details .. "0,"
137 details = details .. "1,"
139 details = details .. "0,"
142 return details .. (grey_out and '#aaaaaa,' or ',') .. text
145 --------------------------------------------------------------------------------
146 os.tempfolder = function()
147 if core.setting_get("TMPFolder") then
148 return core.setting_get("TMPFolder") .. DIR_DELIM .. "MT_" .. math.random(0,10000)
151 local filetocheck = os.tmpname()
152 os.remove(filetocheck)
154 local randname = "MTTempModFolder_" .. math.random(0,10000)
155 if DIR_DELIM == "\\" then
156 local tempfolder = os.getenv("TEMP")
157 return tempfolder .. filetocheck
159 local backstring = filetocheck:reverse()
160 return filetocheck:sub(0,filetocheck:len()-backstring:find(DIR_DELIM)+1) ..randname
165 --------------------------------------------------------------------------------
166 function menu_render_worldlist()
168 local current_worldlist = menudata.worldlist:get_list()
170 for i, v in ipairs(current_worldlist) do
171 if retval ~= "" then retval = retval .. "," end
172 retval = retval .. core.formspec_escape(v.name) ..
173 " \\[" .. core.formspec_escape(v.gameid) .. "\\]"
179 --------------------------------------------------------------------------------
180 function menu_handle_key_up_down(fields, textlist, settingname)
181 local oldidx, newidx = core.get_textlist_index(textlist), 1
182 if fields.key_up or fields.key_down then
183 if fields.key_up and oldidx and oldidx > 1 then
185 elseif fields.key_down and oldidx and
186 oldidx < menudata.worldlist:size() then
189 core.setting_set(settingname, menudata.worldlist:get_raw_index(newidx))
190 configure_selected_world_params(newidx)
196 --------------------------------------------------------------------------------
197 function asyncOnlineFavourites()
198 if not menudata.public_known then
199 menudata.public_known = {{
200 name = fgettext("Loading..."),
201 description = fgettext_ne("Try reenabling public serverlist and check your internet connection.")
204 menudata.favorites = menudata.public_known
205 menudata.favorites_is_public = true
207 if not menudata.public_downloading then
208 menudata.public_downloading = true
215 return core.get_favorites("online")
219 menudata.public_downloading = nil
220 local favs = order_favorite_list(result)
222 menudata.public_known = favs
223 menudata.favorites = menudata.public_known
224 menudata.favorites_is_public = true
226 core.event_handler("Refresh")
231 --------------------------------------------------------------------------------
232 function text2textlist(xpos, ypos, width, height, tl_name, textlen, text, transparency)
233 local textlines = core.splittext(text, textlen)
234 local retval = "textlist[" .. xpos .. "," .. ypos .. ";" .. width ..
235 "," .. height .. ";" .. tl_name .. ";"
237 for i = 1, #textlines do
238 textlines[i] = textlines[i]:gsub("\r", "")
239 retval = retval .. core.formspec_escape(textlines[i]) .. ","
242 retval = retval .. ";0;"
243 if transparency then retval = retval .. "true" end
244 retval = retval .. "]"
249 --------------------------------------------------------------------------------
250 function is_server_protocol_compat(server_proto_min, server_proto_max)
251 if (not server_proto_min) or (not server_proto_max) then
252 -- There is no info. Assume the best and act as if we would be compatible.
255 return min_supp_proto <= server_proto_max and max_supp_proto >= server_proto_min
257 --------------------------------------------------------------------------------
258 function is_server_protocol_compat_or_error(server_proto_min, server_proto_max)
259 if not is_server_protocol_compat(server_proto_min, server_proto_max) then
260 local server_prot_ver_info, client_prot_ver_info
261 local s_p_min = server_proto_min
262 local s_p_max = server_proto_max
264 if s_p_min ~= s_p_max then
265 server_prot_ver_info = fgettext_ne("Server supports protocol versions between $1 and $2. ",
268 server_prot_ver_info = fgettext_ne("Server enforces protocol version $1. ",
271 if min_supp_proto ~= max_supp_proto then
272 client_prot_ver_info= fgettext_ne("We support protocol versions between version $1 and $2.",
273 min_supp_proto, max_supp_proto)
275 client_prot_ver_info = fgettext_ne("We only support protocol version $1.", min_supp_proto)
277 gamedata.errormessage = fgettext_ne("Protocol version mismatch. ")
278 .. server_prot_ver_info
279 .. client_prot_ver_info
285 --------------------------------------------------------------------------------
286 function menu_worldmt(selected, setting, value)
287 local world = menudata.worldlist:get_list()[selected]
289 local filename = world.path .. DIR_DELIM .. "world.mt"
290 local world_conf = Settings(filename)
293 if not world_conf:write() then
294 core.log("error", "Failed to write world config file")
296 world_conf:set(setting, value)
299 return world_conf:get(setting)
306 function menu_worldmt_legacy(selected)
307 local modes_names = {"creative_mode", "enable_damage", "server_announce"}
308 for _, mode_name in pairs(modes_names) do
309 local mode_val = menu_worldmt(selected, mode_name)
311 core.setting_set(mode_name, mode_val)
313 menu_worldmt(selected, mode_name, core.setting_get(mode_name))