common.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. -- Menu helper functions
  23. --------------------------------------------------------------------------------
  24. --------------------------------------------------------------------------------
  25. function render_favorite(spec,render_details)
  26. local text = ""
  27. if spec.name ~= nil then
  28. text = text .. core.formspec_escape(spec.name:trim())
  29. -- if spec.description ~= nil and
  30. -- core.formspec_escape(spec.description):trim() ~= "" then
  31. -- text = text .. " (" .. core.formspec_escape(spec.description) .. ")"
  32. -- end
  33. else
  34. if spec.address ~= nil then
  35. text = text .. spec.address:trim()
  36. if spec.port ~= nil then
  37. text = text .. ":" .. spec.port
  38. end
  39. end
  40. end
  41. if not render_details then
  42. return text
  43. end
  44. local details = ""
  45. if spec.password == true then
  46. details = details .. "*"
  47. else
  48. details = details .. "_"
  49. end
  50. if spec.creative then
  51. details = details .. "C"
  52. else
  53. details = details .. "_"
  54. end
  55. if spec.damage then
  56. details = details .. "D"
  57. else
  58. details = details .. "_"
  59. end
  60. if spec.pvp then
  61. details = details .. "P"
  62. else
  63. details = details .. "_"
  64. end
  65. details = details .. " "
  66. local playercount = ""
  67. if spec.clients ~= nil and
  68. spec.clients_max ~= nil then
  69. playercount = string.format("%03d",spec.clients) .. "/" ..
  70. string.format("%03d",spec.clients_max) .. " "
  71. end
  72. return playercount .. core.formspec_escape(details) .. text
  73. end
  74. --------------------------------------------------------------------------------
  75. os.tempfolder = function()
  76. if core.setting_get("TMPFolder") then
  77. return core.setting_get("TMPFolder") .. DIR_DELIM .. "MT_" .. math.random(0,10000)
  78. end
  79. local filetocheck = os.tmpname()
  80. os.remove(filetocheck)
  81. local randname = "MTTempModFolder_" .. math.random(0,10000)
  82. if DIR_DELIM == "\\" then
  83. local tempfolder = os.getenv("TEMP")
  84. return tempfolder .. filetocheck
  85. else
  86. local backstring = filetocheck:reverse()
  87. return filetocheck:sub(0,filetocheck:len()-backstring:find(DIR_DELIM)+1) ..randname
  88. end
  89. end
  90. --------------------------------------------------------------------------------
  91. function menu_render_worldlist()
  92. local retval = ""
  93. local current_worldlist = menudata.worldlist:get_list()
  94. for i,v in ipairs(current_worldlist) do
  95. if retval ~= "" then
  96. retval = retval ..","
  97. end
  98. retval = retval .. core.formspec_escape(v.name) ..
  99. " \\[" .. core.formspec_escape(v.gameid) .. "\\]"
  100. end
  101. return retval
  102. end
  103. --------------------------------------------------------------------------------
  104. function menu_handle_key_up_down(fields,textlist,settingname)
  105. if fields["key_up"] then
  106. local oldidx = core.get_textlist_index(textlist)
  107. if oldidx ~= nil and oldidx > 1 then
  108. local newidx = oldidx -1
  109. core.setting_set(settingname,
  110. menudata.worldlist:get_raw_index(newidx))
  111. end
  112. return true
  113. end
  114. if fields["key_down"] then
  115. local oldidx = core.get_textlist_index(textlist)
  116. if oldidx ~= nil and oldidx < menudata.worldlist:size() then
  117. local newidx = oldidx + 1
  118. core.setting_set(settingname,
  119. menudata.worldlist:get_raw_index(newidx))
  120. end
  121. return true
  122. end
  123. return false
  124. end
  125. --------------------------------------------------------------------------------
  126. function asyncOnlineFavourites()
  127. menudata.favorites = {}
  128. core.handle_async(
  129. function(param)
  130. return core.get_favorites("online")
  131. end,
  132. nil,
  133. function(result)
  134. menudata.favorites = result
  135. core.event_handler("Refresh")
  136. end
  137. )
  138. end
  139. --------------------------------------------------------------------------------
  140. function text2textlist(xpos,ypos,width,height,tl_name,textlen,text,transparency)
  141. local textlines = core.splittext(text,textlen)
  142. local retval = "textlist[" .. xpos .. "," .. ypos .. ";"
  143. .. width .. "," .. height .. ";"
  144. .. tl_name .. ";"
  145. for i=1, #textlines, 1 do
  146. textlines[i] = textlines[i]:gsub("\r","")
  147. retval = retval .. core.formspec_escape(textlines[i]) .. ","
  148. end
  149. retval = retval .. ";0;"
  150. if transparency then
  151. retval = retval .. "true"
  152. end
  153. retval = retval .. "]"
  154. return retval
  155. end