]> git.lizzy.rs Git - minetest.git/blobdiff - builtin/mainmenu/common.lua
Fix ContentDB packages timing out by using download_file instead (#7891)
[minetest.git] / builtin / mainmenu / common.lua
index 5bf39abd75338e51506c61e39cf55999f535c871..cc61fe0adfc1e68c40d13f2132d1be015ee3ccfd 100644 (file)
@@ -41,12 +41,12 @@ local function render_client_count(n)
 end
 
 local function configure_selected_world_params(idx)
-       local worldconfig = modmgr.get_worldconfig(menudata.worldlist:get_list()[idx].path)
+       local worldconfig = pkgmgr.get_worldconfig(menudata.worldlist:get_list()[idx].path)
        if worldconfig.creative_mode then
-               core.setting_set("creative_mode", worldconfig.creative_mode)
+               core.settings:set("creative_mode", worldconfig.creative_mode)
        end
        if worldconfig.enable_damage then
-               core.setting_set("enable_damage", worldconfig.enable_damage)
+               core.settings:set("enable_damage", worldconfig.enable_damage)
        end
 end
 
@@ -54,7 +54,12 @@ end
 function image_column(tooltip, flagname)
        return "image,tooltip=" .. core.formspec_escape(tooltip) .. "," ..
                "0=" .. core.formspec_escape(defaulttexturedir .. "blank.png") .. "," ..
-               "1=" .. core.formspec_escape(defaulttexturedir .. "server_flags_" .. flagname .. ".png")
+               "1=" .. core.formspec_escape(defaulttexturedir ..
+                       (flagname and "server_flags_" .. flagname .. ".png" or "blank.png")) .. "," ..
+               "2=" .. core.formspec_escape(defaulttexturedir .. "server_ping_4.png") .. "," ..
+               "3=" .. core.formspec_escape(defaulttexturedir .. "server_ping_3.png") .. "," ..
+               "4=" .. core.formspec_escape(defaulttexturedir .. "server_ping_2.png") .. "," ..
+               "5=" .. core.formspec_escape(defaulttexturedir .. "server_ping_1.png")
 end
 
 --------------------------------------------------------------------------------
@@ -77,7 +82,7 @@ function order_favorite_list(list)
 end
 
 --------------------------------------------------------------------------------
-function render_favorite(spec, is_favorite)
+function render_serverlist_row(spec, is_favorite)
        local text = ""
        if spec.name then
                text = text .. core.formspec_escape(spec.name:trim())
@@ -97,6 +102,21 @@ function render_favorite(spec, is_favorite)
                details = "0,"
        end
 
+       if spec.ping then
+               local ping = spec.ping * 1000
+               if ping <= 50 then
+                       details = details .. "2,"
+               elseif ping <= 100 then
+                       details = details .. "3,"
+               elseif ping <= 250 then
+                       details = details .. "4,"
+               else
+                       details = details .. "5,"
+               end
+       else
+               details = details .. "0,"
+       end
+
        if spec.clients and spec.clients_max then
                local clients_color = ''
                local clients_percent = 100 * spec.clients / spec.clients_max
@@ -144,22 +164,34 @@ end
 
 --------------------------------------------------------------------------------
 os.tempfolder = function()
-       if core.setting_get("TMPFolder") then
-               return core.setting_get("TMPFolder") .. DIR_DELIM .. "MT_" .. math.random(0,10000)
+       if core.settings:get("TMPFolder") then
+               return core.settings:get("TMPFolder") .. DIR_DELIM .. "MT_" .. math.random(0,10000)
        end
 
        local filetocheck = os.tmpname()
        os.remove(filetocheck)
 
-       local randname = "MTTempModFolder_" .. math.random(0,10000)
-       if DIR_DELIM == "\\" then
+       -- https://blogs.msdn.microsoft.com/vcblog/2014/06/18/c-runtime-crt-features-fixes-and-breaking-changes-in-visual-studio-14-ctp1/
+       --   The C runtime (CRT) function called by os.tmpname is tmpnam.
+       --   Microsofts tmpnam implementation in older CRT / MSVC releases is defective.
+       --   tmpnam return values starting with a backslash characterize this behavior.
+       -- https://sourceforge.net/p/mingw-w64/bugs/555/
+       --   MinGW tmpnam implementation is forwarded to the CRT directly.
+       -- https://sourceforge.net/p/mingw-w64/discussion/723797/thread/55520785/
+       --   MinGW links to an older CRT release (msvcrt.dll).
+       --   Due to legal concerns MinGW will never use a newer CRT.
+       --
+       --   Make use of TEMP to compose the temporary filename if an old
+       --   style tmpnam return value is detected.
+       if filetocheck:sub(1, 1) == "\\" then
                local tempfolder = os.getenv("TEMP")
                return tempfolder .. filetocheck
-       else
-               local backstring = filetocheck:reverse()
-               return filetocheck:sub(0,filetocheck:len()-backstring:find(DIR_DELIM)+1) ..randname
        end
 
+       local randname = "MTTempModFolder_" .. math.random(0,10000)
+       local backstring = filetocheck:reverse()
+       return filetocheck:sub(0, filetocheck:len() - backstring:find(DIR_DELIM) + 1) ..
+               randname
 end
 
 --------------------------------------------------------------------------------
@@ -178,7 +210,7 @@ end
 
 --------------------------------------------------------------------------------
 function menu_handle_key_up_down(fields, textlist, settingname)
-       local oldidx, newidx = core.get_textlist_index(textlist)
+       local oldidx, newidx = core.get_textlist_index(textlist), 1
        if fields.key_up or fields.key_down then
                if fields.key_up and oldidx and oldidx > 1 then
                        newidx = oldidx - 1
@@ -186,7 +218,7 @@ function menu_handle_key_up_down(fields, textlist, settingname)
                                oldidx < menudata.worldlist:size() then
                        newidx = oldidx + 1
                end
-               core.setting_set(settingname, menudata.worldlist:get_raw_index(newidx))
+               core.settings:set(settingname, menudata.worldlist:get_raw_index(newidx))
                configure_selected_world_params(newidx)
                return true
        end
@@ -203,12 +235,20 @@ function asyncOnlineFavourites()
        end
        menudata.favorites = menudata.public_known
        menudata.favorites_is_public = true
+
+       if not menudata.public_downloading then
+               menudata.public_downloading = true
+       else
+               return
+       end
+
        core.handle_async(
                function(param)
                        return core.get_favorites("online")
                end,
                nil,
                function(result)
+                       menudata.public_downloading = nil
                        local favs = order_favorite_list(result)
                        if favs[1] then
                                menudata.public_known = favs
@@ -222,7 +262,7 @@ end
 
 --------------------------------------------------------------------------------
 function text2textlist(xpos, ypos, width, height, tl_name, textlen, text, transparency)
-       local textlines = core.splittext(text, textlen)
+       local textlines = core.wrap_text(text, textlen, true)
        local retval = "textlist[" .. xpos .. "," .. ypos .. ";" .. width ..
                        "," .. height .. ";" .. tl_name .. ";"
 
@@ -240,14 +280,18 @@ end
 
 --------------------------------------------------------------------------------
 function is_server_protocol_compat(server_proto_min, server_proto_max)
-       return min_supp_proto <= (server_proto_max or 24) and max_supp_proto >= (server_proto_min or 13)
+       if (not server_proto_min) or (not server_proto_max) then
+               -- There is no info. Assume the best and act as if we would be compatible.
+               return true
+       end
+       return min_supp_proto <= server_proto_max and max_supp_proto >= server_proto_min
 end
 --------------------------------------------------------------------------------
 function is_server_protocol_compat_or_error(server_proto_min, server_proto_max)
        if not is_server_protocol_compat(server_proto_min, server_proto_max) then
                local server_prot_ver_info, client_prot_ver_info
-               local s_p_min = server_proto_min or 13
-               local s_p_max = server_proto_max or 24
+               local s_p_min = server_proto_min
+               local s_p_max = server_proto_max
 
                if s_p_min ~= s_p_max then
                        server_prot_ver_info = fgettext_ne("Server supports protocol versions between $1 and $2. ",
@@ -296,9 +340,9 @@ function menu_worldmt_legacy(selected)
        for _, mode_name in pairs(modes_names) do
                local mode_val = menu_worldmt(selected, mode_name)
                if mode_val then
-                       core.setting_set(mode_name, mode_val)
+                       core.settings:set(mode_name, mode_val)
                else
-                       menu_worldmt(selected, mode_name, core.setting_get(mode_name))
+                       menu_worldmt(selected, mode_name, core.settings:get(mode_name))
                end
        end
 end