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 = core.get_min_supp_proto()
26 local max_supp_proto = core.get_max_supp_proto()
28 --------------------------------------------------------------------------------
29 -- Menu helper functions
30 --------------------------------------------------------------------------------
32 --------------------------------------------------------------------------------
33 local function render_client_count(n)
43 local function configure_selected_world_params(idx)
44 local worldconfig = modmgr.get_worldconfig(
45 menudata.worldlist:get_list()[idx].path)
47 if worldconfig.creative_mode ~= nil then
48 core.setting_set("creative_mode", worldconfig.creative_mode)
50 if worldconfig.enable_damage ~= nil then
51 core.setting_set("enable_damage", worldconfig.enable_damage)
55 --------------------------------------------------------------------------------
56 function image_column(tooltip, flagname)
58 "tooltip=" .. core.formspec_escape(tooltip) .. "," ..
59 "0=" .. core.formspec_escape(defaulttexturedir .. "blank.png") .. "," ..
60 "1=" .. core.formspec_escape(defaulttexturedir .. "server_flags_" .. flagname .. ".png")
63 --------------------------------------------------------------------------------
64 function order_favorite_list(list)
66 --orders the favorite list after support
69 if is_server_protocol_compat(fav.proto_min, fav.proto_max) then
70 table.insert(res, fav)
75 if not is_server_protocol_compat(fav.proto_min, fav.proto_max) then
76 table.insert(res, fav)
82 --------------------------------------------------------------------------------
83 function render_favorite(spec,render_details)
86 if spec.name ~= nil then
87 text = text .. core.formspec_escape(spec.name:trim())
89 -- if spec.description ~= nil and
90 -- core.formspec_escape(spec.description):trim() ~= "" then
91 -- text = text .. " (" .. core.formspec_escape(spec.description) .. ")"
94 if spec.address ~= nil then
95 text = text .. spec.address:trim()
97 if spec.port ~= nil then
98 text = text .. ":" .. spec.port
103 if not render_details then
108 local grey_out = not is_server_protocol_compat(spec.proto_max, spec.proto_min)
110 if spec.clients ~= nil and spec.clients_max ~= nil then
111 local clients_color = ''
112 local clients_percent = 100 * spec.clients / spec.clients_max
114 -- Choose a color depending on how many clients are connected
115 -- (relatively to clients_max)
116 if spec.clients == 0 then
117 clients_color = '' -- 0 players: default/white
118 elseif spec.clients == spec.clients_max then
119 clients_color = '#dd5b5b' -- full server: red (darker)
120 elseif clients_percent <= 60 then
121 clients_color = '#a1e587' -- 0-60%: green
122 elseif clients_percent <= 90 then
123 clients_color = '#ffdc97' -- 60-90%: yellow
125 clients_color = '#ffba97' -- 90-100%: orange
129 clients_color = '#aaaaaa'
133 clients_color .. ',' ..
134 render_client_count(spec.clients) .. ',' ..
136 render_client_count(spec.clients_max) .. ','
138 details = details .. '#aaaaaa,?,/,?,'
140 details = details .. ',?,/,?,'
143 if spec.creative then
144 details = details .. "1,"
146 details = details .. "0,"
150 details = details .. "1,"
152 details = details .. "0,"
156 details = details .. "1,"
158 details = details .. "0,"
161 return details .. (grey_out and '#aaaaaa,' or ',') .. text
164 --------------------------------------------------------------------------------
165 os.tempfolder = function()
166 if core.setting_get("TMPFolder") then
167 return core.setting_get("TMPFolder") .. DIR_DELIM .. "MT_" .. math.random(0,10000)
170 local filetocheck = os.tmpname()
171 os.remove(filetocheck)
173 local randname = "MTTempModFolder_" .. math.random(0,10000)
174 if DIR_DELIM == "\\" then
175 local tempfolder = os.getenv("TEMP")
176 return tempfolder .. filetocheck
178 local backstring = filetocheck:reverse()
179 return filetocheck:sub(0,filetocheck:len()-backstring:find(DIR_DELIM)+1) ..randname
184 --------------------------------------------------------------------------------
185 function menu_render_worldlist()
188 local current_worldlist = menudata.worldlist:get_list()
190 for i,v in ipairs(current_worldlist) do
192 retval = retval ..","
195 retval = retval .. core.formspec_escape(v.name) ..
196 " \\[" .. core.formspec_escape(v.gameid) .. "\\]"
202 --------------------------------------------------------------------------------
203 function menu_handle_key_up_down(fields,textlist,settingname)
204 if fields["key_up"] then
205 local oldidx = core.get_textlist_index(textlist)
207 if oldidx ~= nil and oldidx > 1 then
208 local newidx = oldidx -1
209 core.setting_set(settingname,
210 menudata.worldlist:get_raw_index(newidx))
212 configure_selected_world_params(newidx)
217 if fields["key_down"] then
218 local oldidx = core.get_textlist_index(textlist)
220 if oldidx ~= nil and oldidx < menudata.worldlist:size() then
221 local newidx = oldidx + 1
222 core.setting_set(settingname,
223 menudata.worldlist:get_raw_index(newidx))
225 configure_selected_world_params(newidx)
234 --------------------------------------------------------------------------------
235 function asyncOnlineFavourites()
237 if not menudata.public_known then
238 menudata.public_known = {{
239 name = fgettext("Loading..."),
240 description = fgettext_ne("Try reenabling public serverlist and check your internet connection.")
243 menudata.favorites = menudata.public_known
246 return core.get_favorites("online")
250 if core.setting_getbool("public_serverlist") then
251 local favs = order_favorite_list(result)
253 menudata.public_known = favs
254 menudata.favorites = menudata.public_known
256 core.event_handler("Refresh")
262 --------------------------------------------------------------------------------
263 function text2textlist(xpos,ypos,width,height,tl_name,textlen,text,transparency)
264 local textlines = core.splittext(text,textlen)
266 local retval = "textlist[" .. xpos .. "," .. ypos .. ";"
267 .. width .. "," .. height .. ";"
270 for i=1, #textlines, 1 do
271 textlines[i] = textlines[i]:gsub("\r","")
272 retval = retval .. core.formspec_escape(textlines[i]) .. ","
275 retval = retval .. ";0;"
278 retval = retval .. "true"
281 retval = retval .. "]"
286 --------------------------------------------------------------------------------
287 function is_server_protocol_compat(server_proto_min, server_proto_max)
288 return not ((min_supp_proto > (server_proto_max or 24)) or (max_supp_proto < (server_proto_min or 13)))
290 --------------------------------------------------------------------------------
291 function is_server_protocol_compat_or_error(server_proto_min, server_proto_max)
292 if not is_server_protocol_compat(server_proto_min, server_proto_max) then
293 local server_prot_ver_info
294 local client_prot_ver_info
295 if server_proto_min ~= server_proto_max then
296 server_prot_ver_info = fgettext_ne("Server supports protocol versions between $1 and $2. ",
297 server_proto_min or 13, server_proto_max or 24)
299 server_prot_ver_info = fgettext_ne("Server enforces protocol version $1. ",
300 server_proto_min or 13)
302 if min_supp_proto ~= max_supp_proto then
303 client_prot_ver_info= fgettext_ne("We support protocol versions between version $1 and $2.",
304 min_supp_proto, max_supp_proto)
306 client_prot_ver_info = fgettext_ne("We only support protocol version $1.", min_supp_proto)
308 gamedata.errormessage = fgettext_ne("Protocol version mismatch. ")
309 .. server_prot_ver_info
310 .. client_prot_ver_info
316 --------------------------------------------------------------------------------
317 function menu_worldmt(selected, setting, value)
318 local world = menudata.worldlist:get_list()[selected]
320 local filename = world.path .. DIR_DELIM .. "world.mt"
321 local world_conf = Settings(filename)
324 if not world_conf:write() then
325 core.log("error", "Failed to write world config file")
327 world_conf:set(setting, value)
330 return world_conf:get(setting)
337 function menu_worldmt_legacy(selected)
338 local modes_names = {"creative_mode", "enable_damage", "server_announce"}
339 for _, mode_name in pairs(modes_names) do
340 local mode_val = menu_worldmt(selected, mode_name)
341 if mode_val ~= nil then
342 core.setting_set(mode_name, mode_val)
344 menu_worldmt(selected, mode_name, core.setting_get(mode_name))