common.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 = modmgr.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. local randname = "MTTempModFolder_" .. math.random(0,10000)
  154. if DIR_DELIM == "\\" then
  155. local tempfolder = os.getenv("TEMP")
  156. return tempfolder .. filetocheck
  157. else
  158. local backstring = filetocheck:reverse()
  159. return filetocheck:sub(0,filetocheck:len()-backstring:find(DIR_DELIM)+1) ..randname
  160. end
  161. end
  162. --------------------------------------------------------------------------------
  163. function menu_render_worldlist()
  164. local retval = ""
  165. local current_worldlist = menudata.worldlist:get_list()
  166. for i, v in ipairs(current_worldlist) do
  167. if retval ~= "" then retval = retval .. "," end
  168. retval = retval .. core.formspec_escape(v.name) ..
  169. " \\[" .. core.formspec_escape(v.gameid) .. "\\]"
  170. end
  171. return retval
  172. end
  173. --------------------------------------------------------------------------------
  174. function menu_handle_key_up_down(fields, textlist, settingname)
  175. local oldidx, newidx = core.get_textlist_index(textlist), 1
  176. if fields.key_up or fields.key_down then
  177. if fields.key_up and oldidx and oldidx > 1 then
  178. newidx = oldidx - 1
  179. elseif fields.key_down and oldidx and
  180. oldidx < menudata.worldlist:size() then
  181. newidx = oldidx + 1
  182. end
  183. core.settings:set(settingname, menudata.worldlist:get_raw_index(newidx))
  184. configure_selected_world_params(newidx)
  185. return true
  186. end
  187. return false
  188. end
  189. --------------------------------------------------------------------------------
  190. function asyncOnlineFavourites()
  191. if not menudata.public_known then
  192. menudata.public_known = {{
  193. name = fgettext("Loading..."),
  194. description = fgettext_ne("Try reenabling public serverlist and check your internet connection.")
  195. }}
  196. end
  197. menudata.favorites = menudata.public_known
  198. menudata.favorites_is_public = true
  199. if not menudata.public_downloading then
  200. menudata.public_downloading = true
  201. else
  202. return
  203. end
  204. core.handle_async(
  205. function(param)
  206. return core.get_favorites("online")
  207. end,
  208. nil,
  209. function(result)
  210. menudata.public_downloading = nil
  211. local favs = order_favorite_list(result)
  212. if favs[1] then
  213. menudata.public_known = favs
  214. menudata.favorites = menudata.public_known
  215. menudata.favorites_is_public = true
  216. end
  217. core.event_handler("Refresh")
  218. end
  219. )
  220. end
  221. --------------------------------------------------------------------------------
  222. function text2textlist(xpos, ypos, width, height, tl_name, textlen, text, transparency)
  223. local textlines = core.wrap_text(text, textlen)
  224. local retval = "textlist[" .. xpos .. "," .. ypos .. ";" .. width ..
  225. "," .. height .. ";" .. tl_name .. ";"
  226. for i = 1, #textlines do
  227. textlines[i] = textlines[i]:gsub("\r", "")
  228. retval = retval .. core.formspec_escape(textlines[i]) .. ","
  229. end
  230. retval = retval .. ";0;"
  231. if transparency then retval = retval .. "true" end
  232. retval = retval .. "]"
  233. return retval
  234. end
  235. --------------------------------------------------------------------------------
  236. function is_server_protocol_compat(server_proto_min, server_proto_max)
  237. if (not server_proto_min) or (not server_proto_max) then
  238. -- There is no info. Assume the best and act as if we would be compatible.
  239. return true
  240. end
  241. return min_supp_proto <= server_proto_max and max_supp_proto >= server_proto_min
  242. end
  243. --------------------------------------------------------------------------------
  244. function is_server_protocol_compat_or_error(server_proto_min, server_proto_max)
  245. if not is_server_protocol_compat(server_proto_min, server_proto_max) then
  246. local server_prot_ver_info, client_prot_ver_info
  247. local s_p_min = server_proto_min
  248. local s_p_max = server_proto_max
  249. if s_p_min ~= s_p_max then
  250. server_prot_ver_info = fgettext_ne("Server supports protocol versions between $1 and $2. ",
  251. s_p_min, s_p_max)
  252. else
  253. server_prot_ver_info = fgettext_ne("Server enforces protocol version $1. ",
  254. s_p_min)
  255. end
  256. if min_supp_proto ~= max_supp_proto then
  257. client_prot_ver_info= fgettext_ne("We support protocol versions between version $1 and $2.",
  258. min_supp_proto, max_supp_proto)
  259. else
  260. client_prot_ver_info = fgettext_ne("We only support protocol version $1.", min_supp_proto)
  261. end
  262. gamedata.errormessage = fgettext_ne("Protocol version mismatch. ")
  263. .. server_prot_ver_info
  264. .. client_prot_ver_info
  265. return false
  266. end
  267. return true
  268. end
  269. --------------------------------------------------------------------------------
  270. function menu_worldmt(selected, setting, value)
  271. local world = menudata.worldlist:get_list()[selected]
  272. if world then
  273. local filename = world.path .. DIR_DELIM .. "world.mt"
  274. local world_conf = Settings(filename)
  275. if value then
  276. if not world_conf:write() then
  277. core.log("error", "Failed to write world config file")
  278. end
  279. world_conf:set(setting, value)
  280. world_conf:write()
  281. else
  282. return world_conf:get(setting)
  283. end
  284. else
  285. return nil
  286. end
  287. end
  288. function menu_worldmt_legacy(selected)
  289. local modes_names = {"creative_mode", "enable_damage", "server_announce"}
  290. for _, mode_name in pairs(modes_names) do
  291. local mode_val = menu_worldmt(selected, mode_name)
  292. if mode_val then
  293. core.settings:set(mode_name, mode_val)
  294. else
  295. menu_worldmt(selected, mode_name, core.settings:get(mode_name))
  296. end
  297. end
  298. end