common.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. --Minetest
  2. --Copyright (C) 2014 sapier
  3. --
  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.
  8. --
  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.
  13. --
  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. --------------------------------------------------------------------------------
  18. -- Global menu data
  19. --------------------------------------------------------------------------------
  20. menudata = {}
  21. --------------------------------------------------------------------------------
  22. -- Local cached values
  23. --------------------------------------------------------------------------------
  24. local min_supp_proto, max_supp_proto
  25. function common_update_cached_supp_proto()
  26. min_supp_proto = core.get_min_supp_proto()
  27. max_supp_proto = core.get_max_supp_proto()
  28. end
  29. common_update_cached_supp_proto()
  30. --------------------------------------------------------------------------------
  31. -- Menu helper functions
  32. --------------------------------------------------------------------------------
  33. --------------------------------------------------------------------------------
  34. local function render_client_count(n)
  35. if n > 99 then return '99+'
  36. elseif n >= 0 then return tostring(n)
  37. else return '?' end
  38. end
  39. local function configure_selected_world_params(idx)
  40. local worldconfig = pkgmgr.get_worldconfig(menudata.worldlist:get_list()[idx].path)
  41. if worldconfig.creative_mode then
  42. core.settings:set("creative_mode", worldconfig.creative_mode)
  43. end
  44. if worldconfig.enable_damage then
  45. core.settings:set("enable_damage", worldconfig.enable_damage)
  46. end
  47. end
  48. --------------------------------------------------------------------------------
  49. function image_column(tooltip, flagname)
  50. return "image,tooltip=" .. core.formspec_escape(tooltip) .. "," ..
  51. "0=" .. core.formspec_escape(defaulttexturedir .. "blank.png") .. "," ..
  52. "1=" .. core.formspec_escape(defaulttexturedir ..
  53. (flagname and "server_flags_" .. flagname .. ".png" or "blank.png")) .. "," ..
  54. "2=" .. core.formspec_escape(defaulttexturedir .. "server_ping_4.png") .. "," ..
  55. "3=" .. core.formspec_escape(defaulttexturedir .. "server_ping_3.png") .. "," ..
  56. "4=" .. core.formspec_escape(defaulttexturedir .. "server_ping_2.png") .. "," ..
  57. "5=" .. core.formspec_escape(defaulttexturedir .. "server_ping_1.png")
  58. end
  59. --------------------------------------------------------------------------------
  60. function order_favorite_list(list)
  61. local res = {}
  62. --orders the favorite list after support
  63. for i = 1, #list do
  64. local fav = list[i]
  65. if is_server_protocol_compat(fav.proto_min, fav.proto_max) then
  66. res[#res + 1] = fav
  67. end
  68. end
  69. for i = 1, #list do
  70. local fav = list[i]
  71. if not is_server_protocol_compat(fav.proto_min, fav.proto_max) then
  72. res[#res + 1] = fav
  73. end
  74. end
  75. return res
  76. end
  77. --------------------------------------------------------------------------------
  78. function render_serverlist_row(spec, is_favorite)
  79. local text = ""
  80. if spec.name then
  81. text = text .. core.formspec_escape(spec.name:trim())
  82. elseif spec.address then
  83. text = text .. spec.address:trim()
  84. if spec.port then
  85. text = text .. ":" .. spec.port
  86. end
  87. end
  88. local grey_out = not is_server_protocol_compat(spec.proto_min, spec.proto_max)
  89. local details
  90. if is_favorite then
  91. details = "1,"
  92. else
  93. details = "0,"
  94. end
  95. if spec.ping then
  96. local ping = spec.ping * 1000
  97. if ping <= 50 then
  98. details = details .. "2,"
  99. elseif ping <= 100 then
  100. details = details .. "3,"
  101. elseif ping <= 250 then
  102. details = details .. "4,"
  103. else
  104. details = details .. "5,"
  105. end
  106. else
  107. details = details .. "0,"
  108. end
  109. if spec.clients and spec.clients_max then
  110. local clients_percent = 100 * spec.clients / spec.clients_max
  111. -- Choose a color depending on how many clients are connected
  112. -- (relatively to clients_max)
  113. local clients_color
  114. if grey_out then clients_color = '#aaaaaa'
  115. elseif spec.clients == 0 then clients_color = '' -- 0 players: default/white
  116. elseif clients_percent <= 60 then clients_color = '#a1e587' -- 0-60%: green
  117. elseif clients_percent <= 90 then clients_color = '#ffdc97' -- 60-90%: yellow
  118. elseif clients_percent == 100 then clients_color = '#dd5b5b' -- full server: red (darker)
  119. else clients_color = '#ffba97' -- 90-100%: orange
  120. end
  121. details = details .. clients_color .. ',' ..
  122. render_client_count(spec.clients) .. ',/,' ..
  123. render_client_count(spec.clients_max) .. ','
  124. elseif grey_out then
  125. details = details .. '#aaaaaa,?,/,?,'
  126. else
  127. details = details .. ',?,/,?,'
  128. end
  129. if spec.creative then
  130. details = details .. "1,"
  131. else
  132. details = details .. "0,"
  133. end
  134. if spec.damage then
  135. details = details .. "1,"
  136. else
  137. details = details .. "0,"
  138. end
  139. if spec.pvp then
  140. details = details .. "1,"
  141. else
  142. details = details .. "0,"
  143. end
  144. return details .. (grey_out and '#aaaaaa,' or ',') .. text
  145. end
  146. --------------------------------------------------------------------------------
  147. os.tempfolder = function()
  148. if core.settings:get("TMPFolder") then
  149. return core.settings:get("TMPFolder") .. DIR_DELIM .. "MT_" .. math.random(0,10000)
  150. end
  151. local filetocheck = os.tmpname()
  152. os.remove(filetocheck)
  153. -- luacheck: ignore
  154. -- https://blogs.msdn.microsoft.com/vcblog/2014/06/18/c-runtime-crt-features-fixes-and-breaking-changes-in-visual-studio-14-ctp1/
  155. -- The C runtime (CRT) function called by os.tmpname is tmpnam.
  156. -- Microsofts tmpnam implementation in older CRT / MSVC releases is defective.
  157. -- tmpnam return values starting with a backslash characterize this behavior.
  158. -- https://sourceforge.net/p/mingw-w64/bugs/555/
  159. -- MinGW tmpnam implementation is forwarded to the CRT directly.
  160. -- https://sourceforge.net/p/mingw-w64/discussion/723797/thread/55520785/
  161. -- MinGW links to an older CRT release (msvcrt.dll).
  162. -- Due to legal concerns MinGW will never use a newer CRT.
  163. --
  164. -- Make use of TEMP to compose the temporary filename if an old
  165. -- style tmpnam return value is detected.
  166. if filetocheck:sub(1, 1) == "\\" then
  167. local tempfolder = os.getenv("TEMP")
  168. return tempfolder .. filetocheck
  169. end
  170. local randname = "MTTempModFolder_" .. math.random(0,10000)
  171. local backstring = filetocheck:reverse()
  172. return filetocheck:sub(0, filetocheck:len() - backstring:find(DIR_DELIM) + 1) ..
  173. randname
  174. end
  175. --------------------------------------------------------------------------------
  176. function menu_render_worldlist()
  177. local retval = ""
  178. local current_worldlist = menudata.worldlist:get_list()
  179. for i, v in ipairs(current_worldlist) do
  180. if retval ~= "" then retval = retval .. "," end
  181. retval = retval .. core.formspec_escape(v.name) ..
  182. " \\[" .. core.formspec_escape(v.gameid) .. "\\]"
  183. end
  184. return retval
  185. end
  186. --------------------------------------------------------------------------------
  187. function menu_handle_key_up_down(fields, textlist, settingname)
  188. local oldidx, newidx = core.get_textlist_index(textlist), 1
  189. if fields.key_up or fields.key_down then
  190. if fields.key_up and oldidx and oldidx > 1 then
  191. newidx = oldidx - 1
  192. elseif fields.key_down and oldidx and
  193. oldidx < menudata.worldlist:size() then
  194. newidx = oldidx + 1
  195. end
  196. core.settings:set(settingname, menudata.worldlist:get_raw_index(newidx))
  197. configure_selected_world_params(newidx)
  198. return true
  199. end
  200. return false
  201. end
  202. --------------------------------------------------------------------------------
  203. function asyncOnlineFavourites()
  204. if not menudata.public_known then
  205. menudata.public_known = {{
  206. name = fgettext("Loading..."),
  207. description = fgettext_ne("Try reenabling public serverlist and check your internet connection.")
  208. }}
  209. end
  210. menudata.favorites = menudata.public_known
  211. menudata.favorites_is_public = true
  212. if not menudata.public_downloading then
  213. menudata.public_downloading = true
  214. else
  215. return
  216. end
  217. core.handle_async(
  218. function(param)
  219. return core.get_favorites("online")
  220. end,
  221. nil,
  222. function(result)
  223. menudata.public_downloading = nil
  224. local favs = order_favorite_list(result)
  225. if favs[1] then
  226. menudata.public_known = favs
  227. menudata.favorites = menudata.public_known
  228. menudata.favorites_is_public = true
  229. end
  230. core.event_handler("Refresh")
  231. end
  232. )
  233. end
  234. --------------------------------------------------------------------------------
  235. function text2textlist(xpos, ypos, width, height, tl_name, textlen, text, transparency)
  236. local textlines = core.wrap_text(text, textlen, true)
  237. local retval = "textlist[" .. xpos .. "," .. ypos .. ";" .. width ..
  238. "," .. height .. ";" .. tl_name .. ";"
  239. for i = 1, #textlines do
  240. textlines[i] = textlines[i]:gsub("\r", "")
  241. retval = retval .. core.formspec_escape(textlines[i]) .. ","
  242. end
  243. retval = retval .. ";0;"
  244. if transparency then retval = retval .. "true" end
  245. retval = retval .. "]"
  246. return retval
  247. end
  248. --------------------------------------------------------------------------------
  249. function is_server_protocol_compat(server_proto_min, server_proto_max)
  250. if (not server_proto_min) or (not server_proto_max) then
  251. -- There is no info. Assume the best and act as if we would be compatible.
  252. return true
  253. end
  254. return min_supp_proto <= server_proto_max and max_supp_proto >= server_proto_min
  255. end
  256. --------------------------------------------------------------------------------
  257. function is_server_protocol_compat_or_error(server_proto_min, server_proto_max)
  258. if not is_server_protocol_compat(server_proto_min, server_proto_max) then
  259. local server_prot_ver_info, client_prot_ver_info
  260. local s_p_min = server_proto_min
  261. local s_p_max = server_proto_max
  262. if s_p_min ~= s_p_max then
  263. server_prot_ver_info = fgettext_ne("Server supports protocol versions between $1 and $2. ",
  264. s_p_min, s_p_max)
  265. else
  266. server_prot_ver_info = fgettext_ne("Server enforces protocol version $1. ",
  267. s_p_min)
  268. end
  269. if min_supp_proto ~= max_supp_proto then
  270. client_prot_ver_info= fgettext_ne("We support protocol versions between version $1 and $2.",
  271. min_supp_proto, max_supp_proto)
  272. else
  273. client_prot_ver_info = fgettext_ne("We only support protocol version $1.", min_supp_proto)
  274. end
  275. gamedata.errormessage = fgettext_ne("Protocol version mismatch. ")
  276. .. server_prot_ver_info
  277. .. client_prot_ver_info
  278. return false
  279. end
  280. return true
  281. end
  282. --------------------------------------------------------------------------------
  283. function menu_worldmt(selected, setting, value)
  284. local world = menudata.worldlist:get_list()[selected]
  285. if world then
  286. local filename = world.path .. DIR_DELIM .. "world.mt"
  287. local world_conf = Settings(filename)
  288. if value then
  289. if not world_conf:write() then
  290. core.log("error", "Failed to write world config file")
  291. end
  292. world_conf:set(setting, value)
  293. world_conf:write()
  294. else
  295. return world_conf:get(setting)
  296. end
  297. else
  298. return nil
  299. end
  300. end
  301. function menu_worldmt_legacy(selected)
  302. local modes_names = {"creative_mode", "enable_damage", "server_announce"}
  303. for _, mode_name in pairs(modes_names) do
  304. local mode_val = menu_worldmt(selected, mode_name)
  305. if mode_val then
  306. core.settings:set(mode_name, mode_val)
  307. else
  308. menu_worldmt(selected, mode_name, core.settings:get(mode_name))
  309. end
  310. end
  311. end