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 --------------------------------------------------------------------------------
28 function common_update_cached_supp_proto()
29 min_supp_proto = core.get_min_supp_proto()
30 max_supp_proto = core.get_max_supp_proto()
32 common_update_cached_supp_proto()
33 --------------------------------------------------------------------------------
34 -- Menu helper functions
35 --------------------------------------------------------------------------------
37 --------------------------------------------------------------------------------
38 local function render_client_count(n)
48 local function configure_selected_world_params(idx)
49 local worldconfig = modmgr.get_worldconfig(
50 menudata.worldlist:get_list()[idx].path)
52 if worldconfig.creative_mode ~= nil then
53 core.setting_set("creative_mode", worldconfig.creative_mode)
55 if worldconfig.enable_damage ~= nil then
56 core.setting_set("enable_damage", worldconfig.enable_damage)
60 --------------------------------------------------------------------------------
61 function image_column(tooltip, flagname)
63 "tooltip=" .. core.formspec_escape(tooltip) .. "," ..
64 "0=" .. core.formspec_escape(defaulttexturedir .. "blank.png") .. "," ..
65 "1=" .. core.formspec_escape(defaulttexturedir .. "server_flags_" .. flagname .. ".png")
68 --------------------------------------------------------------------------------
69 function order_favorite_list(list)
71 --orders the favorite list after support
74 if is_server_protocol_compat(fav.proto_min, fav.proto_max) then
80 if not is_server_protocol_compat(fav.proto_min, fav.proto_max) then
87 --------------------------------------------------------------------------------
88 function render_favorite(spec,render_details)
91 if spec.name ~= nil then
92 text = text .. core.formspec_escape(spec.name:trim())
94 -- if spec.description ~= nil and
95 -- core.formspec_escape(spec.description):trim() ~= "" then
96 -- text = text .. " (" .. core.formspec_escape(spec.description) .. ")"
99 if spec.address ~= nil then
100 text = text .. spec.address:trim()
102 if spec.port ~= nil then
103 text = text .. ":" .. spec.port
108 if not render_details then
113 local grey_out = not is_server_protocol_compat(spec.proto_min, spec.proto_max)
115 if spec.clients ~= nil and spec.clients_max ~= nil then
116 local clients_color = ''
117 local clients_percent = 100 * spec.clients / spec.clients_max
119 -- Choose a color depending on how many clients are connected
120 -- (relatively to clients_max)
121 if spec.clients == 0 then
122 clients_color = '' -- 0 players: default/white
123 elseif spec.clients == spec.clients_max then
124 clients_color = '#dd5b5b' -- full server: red (darker)
125 elseif clients_percent <= 60 then
126 clients_color = '#a1e587' -- 0-60%: green
127 elseif clients_percent <= 90 then
128 clients_color = '#ffdc97' -- 60-90%: yellow
130 clients_color = '#ffba97' -- 90-100%: orange
134 clients_color = '#aaaaaa'
138 clients_color .. ',' ..
139 render_client_count(spec.clients) .. ',' ..
141 render_client_count(spec.clients_max) .. ','
143 details = details .. '#aaaaaa,?,/,?,'
145 details = details .. ',?,/,?,'
148 if spec.creative then
149 details = details .. "1,"
151 details = details .. "0,"
155 details = details .. "1,"
157 details = details .. "0,"
161 details = details .. "1,"
163 details = details .. "0,"
166 return details .. (grey_out and '#aaaaaa,' or ',') .. text
169 --------------------------------------------------------------------------------
170 os.tempfolder = function()
171 if core.setting_get("TMPFolder") then
172 return core.setting_get("TMPFolder") .. DIR_DELIM .. "MT_" .. math.random(0,10000)
175 local filetocheck = os.tmpname()
176 os.remove(filetocheck)
178 local randname = "MTTempModFolder_" .. math.random(0,10000)
179 if DIR_DELIM == "\\" then
180 local tempfolder = os.getenv("TEMP")
181 return tempfolder .. filetocheck
183 local backstring = filetocheck:reverse()
184 return filetocheck:sub(0,filetocheck:len()-backstring:find(DIR_DELIM)+1) ..randname
189 --------------------------------------------------------------------------------
190 function menu_render_worldlist()
193 local current_worldlist = menudata.worldlist:get_list()
195 for i,v in ipairs(current_worldlist) do
197 retval = retval ..","
200 retval = retval .. core.formspec_escape(v.name) ..
201 " \\[" .. core.formspec_escape(v.gameid) .. "\\]"
207 --------------------------------------------------------------------------------
208 function menu_handle_key_up_down(fields,textlist,settingname)
209 if fields["key_up"] then
210 local oldidx = core.get_textlist_index(textlist)
212 if oldidx ~= nil and oldidx > 1 then
213 local newidx = oldidx -1
214 core.setting_set(settingname,
215 menudata.worldlist:get_raw_index(newidx))
217 configure_selected_world_params(newidx)
222 if fields["key_down"] then
223 local oldidx = core.get_textlist_index(textlist)
225 if oldidx ~= nil and oldidx < menudata.worldlist:size() then
226 local newidx = oldidx + 1
227 core.setting_set(settingname,
228 menudata.worldlist:get_raw_index(newidx))
230 configure_selected_world_params(newidx)
239 --------------------------------------------------------------------------------
240 function asyncOnlineFavourites()
242 if not menudata.public_known then
243 menudata.public_known = {{
244 name = fgettext("Loading..."),
245 description = fgettext_ne("Try reenabling public serverlist and check your internet connection.")
248 menudata.favorites = menudata.public_known
251 return core.get_favorites("online")
255 if core.setting_getbool("public_serverlist") then
256 local favs = order_favorite_list(result)
258 menudata.public_known = favs
259 menudata.favorites = menudata.public_known
261 core.event_handler("Refresh")
267 --------------------------------------------------------------------------------
268 function text2textlist(xpos,ypos,width,height,tl_name,textlen,text,transparency)
269 local textlines = core.splittext(text,textlen)
271 local retval = "textlist[" .. xpos .. "," .. ypos .. ";"
272 .. width .. "," .. height .. ";"
275 for i=1, #textlines, 1 do
276 textlines[i] = textlines[i]:gsub("\r","")
277 retval = retval .. core.formspec_escape(textlines[i]) .. ","
280 retval = retval .. ";0;"
283 retval = retval .. "true"
286 retval = retval .. "]"
291 --------------------------------------------------------------------------------
292 function is_server_protocol_compat(server_proto_min, server_proto_max)
293 return not ((min_supp_proto > (server_proto_max or 24)) or (max_supp_proto < (server_proto_min or 13)))
295 --------------------------------------------------------------------------------
296 function is_server_protocol_compat_or_error(server_proto_min, server_proto_max)
297 if not is_server_protocol_compat(server_proto_min, server_proto_max) then
298 local server_prot_ver_info
299 local client_prot_ver_info
300 if server_proto_min ~= server_proto_max then
301 server_prot_ver_info = fgettext_ne("Server supports protocol versions between $1 and $2. ",
302 server_proto_min or 13, server_proto_max or 24)
304 server_prot_ver_info = fgettext_ne("Server enforces protocol version $1. ",
305 server_proto_min or 13)
307 if min_supp_proto ~= max_supp_proto then
308 client_prot_ver_info= fgettext_ne("We support protocol versions between version $1 and $2.",
309 min_supp_proto, max_supp_proto)
311 client_prot_ver_info = fgettext_ne("We only support protocol version $1.", min_supp_proto)
313 gamedata.errormessage = fgettext_ne("Protocol version mismatch. ")
314 .. server_prot_ver_info
315 .. client_prot_ver_info
321 --------------------------------------------------------------------------------
322 function menu_worldmt(selected, setting, value)
323 local world = menudata.worldlist:get_list()[selected]
325 local filename = world.path .. DIR_DELIM .. "world.mt"
326 local world_conf = Settings(filename)
329 if not world_conf:write() then
330 core.log("error", "Failed to write world config file")
332 world_conf:set(setting, value)
335 return world_conf:get(setting)
342 function menu_worldmt_legacy(selected)
343 local modes_names = {"creative_mode", "enable_damage", "server_announce"}
344 for _, mode_name in pairs(modes_names) do
345 local mode_val = menu_worldmt(selected, mode_name)
346 if mode_val ~= nil then
347 core.setting_set(mode_name, mode_val)
349 menu_worldmt(selected, mode_name, core.setting_get(mode_name))