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 details = ""
  89. local grey_out = not is_server_protocol_compat(spec.proto_min, spec.proto_max)
  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_color = ''
  111. local clients_percent = 100 * spec.clients / spec.clients_max
  112. -- Choose a color depending on how many clients are connected
  113. -- (relatively to clients_max)
  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. -- https://blogs.msdn.microsoft.com/vcblog/2014/06/18/c-runtime-crt-features-fixes-and-breaking-changes-in-visual-studio-14-ctp1/
  154. -- The C runtime (CRT) function called by os.tmpname is tmpnam.
  155. -- Microsofts tmpnam implementation in older CRT / MSVC releases is defective.
  156. -- tmpnam return values starting with a backslash characterize this behavior.
  157. -- https://sourceforge.net/p/mingw-w64/bugs/555/
  158. -- MinGW tmpnam implementation is forwarded to the CRT directly.
  159. -- https://sourceforge.net/p/mingw-w64/discussion/723797/thread/55520785/
  160. -- MinGW links to an older CRT release (msvcrt.dll).
  161. -- Due to legal concerns MinGW will never use a newer CRT.
  162. --
  163. -- Make use of TEMP to compose the temporary filename if an old
  164. -- style tmpnam return value is detected.
  165. if filetocheck:sub(1, 1) == "\\" then
  166. local tempfolder = os.getenv("TEMP")
  167. return tempfolder .. filetocheck
  168. end
  169. local randname = "MTTempModFolder_" .. math.random(0,10000)
  170. local backstring = filetocheck:reverse()
  171. return filetocheck:sub(0, filetocheck:len() - backstring:find(DIR_DELIM) + 1) ..
  172. randname
  173. end
  174. --------------------------------------------------------------------------------
  175. function menu_render_worldlist()
  176. local retval = ""
  177. local current_worldlist = menudata.worldlist:get_list()
  178. for i, v in ipairs(current_worldlist) do
  179. if retval ~= "" then retval = retval .. "," end
  180. retval = retval .. core.formspec_escape(v.name) ..
  181. " \\[" .. core.formspec_escape(v.gameid) .. "\\]"
  182. end
  183. return retval
  184. end
  185. --------------------------------------------------------------------------------
  186. function menu_handle_key_up_down(fields, textlist, settingname)
  187. local oldidx, newidx = core.get_textlist_index(textlist), 1
  188. if fields.key_up or fields.key_down then
  189. if fields.key_up and oldidx and oldidx > 1 then
  190. newidx = oldidx - 1
  191. elseif fields.key_down and oldidx and
  192. oldidx < menudata.worldlist:size() then
  193. newidx = oldidx + 1
  194. end
  195. core.settings:set(settingname, menudata.worldlist:get_raw_index(newidx))
  196. configure_selected_world_params(newidx)
  197. return true
  198. end
  199. return false
  200. end
  201. --------------------------------------------------------------------------------
  202. function asyncOnlineFavourites()
  203. if not menudata.public_known then
  204. menudata.public_known = {{
  205. name = fgettext("Loading..."),
  206. description = fgettext_ne("Try reenabling public serverlist and check your internet connection.")
  207. }}
  208. end
  209. menudata.favorites = menudata.public_known
  210. menudata.favorites_is_public = true
  211. if not menudata.public_downloading then
  212. menudata.public_downloading = true
  213. else
  214. return
  215. end
  216. core.handle_async(
  217. function(param)
  218. return core.get_favorites("online")
  219. end,
  220. nil,
  221. function(result)
  222. menudata.public_downloading = nil
  223. local favs = order_favorite_list(result)
  224. if favs[1] then
  225. menudata.public_known = favs
  226. menudata.favorites = menudata.public_known
  227. menudata.favorites_is_public = true
  228. end
  229. core.event_handler("Refresh")
  230. end
  231. )
  232. end
  233. --------------------------------------------------------------------------------
  234. function text2textlist(xpos, ypos, width, height, tl_name, textlen, text, transparency)
  235. local textlines = core.wrap_text(text, textlen, true)
  236. local retval = "textlist[" .. xpos .. "," .. ypos .. ";" .. width ..
  237. "," .. height .. ";" .. tl_name .. ";"
  238. for i = 1, #textlines do
  239. textlines[i] = textlines[i]:gsub("\r", "")
  240. retval = retval .. core.formspec_escape(textlines[i]) .. ","
  241. end
  242. retval = retval .. ";0;"
  243. if transparency then retval = retval .. "true" end
  244. retval = retval .. "]"
  245. return retval
  246. end
  247. --------------------------------------------------------------------------------
  248. function is_server_protocol_compat(server_proto_min, server_proto_max)
  249. if (not server_proto_min) or (not server_proto_max) then
  250. -- There is no info. Assume the best and act as if we would be compatible.
  251. return true
  252. end
  253. return min_supp_proto <= server_proto_max and max_supp_proto >= server_proto_min
  254. end
  255. --------------------------------------------------------------------------------
  256. function is_server_protocol_compat_or_error(server_proto_min, server_proto_max)
  257. if not is_server_protocol_compat(server_proto_min, server_proto_max) then
  258. local server_prot_ver_info, client_prot_ver_info
  259. local s_p_min = server_proto_min
  260. local s_p_max = server_proto_max
  261. if s_p_min ~= s_p_max then
  262. server_prot_ver_info = fgettext_ne("Server supports protocol versions between $1 and $2. ",
  263. s_p_min, s_p_max)
  264. else
  265. server_prot_ver_info = fgettext_ne("Server enforces protocol version $1. ",
  266. s_p_min)
  267. end
  268. if min_supp_proto ~= max_supp_proto then
  269. client_prot_ver_info= fgettext_ne("We support protocol versions between version $1 and $2.",
  270. min_supp_proto, max_supp_proto)
  271. else
  272. client_prot_ver_info = fgettext_ne("We only support protocol version $1.", min_supp_proto)
  273. end
  274. gamedata.errormessage = fgettext_ne("Protocol version mismatch. ")
  275. .. server_prot_ver_info
  276. .. client_prot_ver_info
  277. return false
  278. end
  279. return true
  280. end
  281. --------------------------------------------------------------------------------
  282. function menu_worldmt(selected, setting, value)
  283. local world = menudata.worldlist:get_list()[selected]
  284. if world then
  285. local filename = world.path .. DIR_DELIM .. "world.mt"
  286. local world_conf = Settings(filename)
  287. if value then
  288. if not world_conf:write() then
  289. core.log("error", "Failed to write world config file")
  290. end
  291. world_conf:set(setting, value)
  292. world_conf:write()
  293. else
  294. return world_conf:get(setting)
  295. end
  296. else
  297. return nil
  298. end
  299. end
  300. function menu_worldmt_legacy(selected)
  301. local modes_names = {"creative_mode", "enable_damage", "server_announce"}
  302. for _, mode_name in pairs(modes_names) do
  303. local mode_val = menu_worldmt(selected, mode_name)
  304. if mode_val then
  305. core.settings:set(mode_name, mode_val)
  306. else
  307. menu_worldmt(selected, mode_name, core.settings:get(mode_name))
  308. end
  309. end
  310. end