modstore.lua 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. --Minetest
  2. --Copyright (C) 2013 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. --modstore implementation
  19. modstore = {}
  20. --------------------------------------------------------------------------------
  21. function modstore.init()
  22. modstore.tabnames = {}
  23. table.insert(modstore.tabnames,"dialog_modstore_unsorted")
  24. table.insert(modstore.tabnames,"dialog_modstore_search")
  25. modstore.modsperpage = 5
  26. modstore.basetexturedir = engine.get_texturepath() .. DIR_DELIM .. "base" ..
  27. DIR_DELIM .. "pack" .. DIR_DELIM
  28. modstore.current_list = nil
  29. modstore.details_cache = {}
  30. end
  31. --------------------------------------------------------------------------------
  32. function modstore.nametoindex(name)
  33. for i=1,#modstore.tabnames,1 do
  34. if modstore.tabnames[i] == name then
  35. return i
  36. end
  37. end
  38. return 1
  39. end
  40. --------------------------------------------------------------------------------
  41. function modstore.gettab(tabname)
  42. local retval = ""
  43. local is_modstore_tab = false
  44. if tabname == "dialog_modstore_unsorted" then
  45. retval = modstore.getmodlist(modstore.modlist_unsorted)
  46. is_modstore_tab = true
  47. end
  48. if tabname == "dialog_modstore_search" then
  49. is_modstore_tab = true
  50. end
  51. if is_modstore_tab then
  52. return modstore.tabheader(tabname) .. retval
  53. end
  54. if tabname == "modstore_mod_installed" then
  55. return "size[6,2]label[0.25,0.25;Mod: " .. modstore.lastmodtitle ..
  56. " installed successfully]" ..
  57. "button[2.5,1.5;1,0.5;btn_confirm_mod_successfull;ok]"
  58. end
  59. return ""
  60. end
  61. --------------------------------------------------------------------------------
  62. function modstore.tabheader(tabname)
  63. local retval = "size[12,9.25]"
  64. retval = retval .. "tabheader[-0.3,-0.99;modstore_tab;" ..
  65. "Unsorted,Search;" ..
  66. modstore.nametoindex(tabname) .. ";true;false]"
  67. return retval
  68. end
  69. --------------------------------------------------------------------------------
  70. function modstore.handle_buttons(current_tab,fields)
  71. modstore.lastmodtitle = ""
  72. if fields["modstore_tab"] then
  73. local index = tonumber(fields["modstore_tab"])
  74. if index > 0 and
  75. index <= #modstore.tabnames then
  76. return {
  77. current_tab = modstore.tabnames[index],
  78. is_dialog = true,
  79. show_buttons = false
  80. }
  81. end
  82. modstore.modlist_page = 0
  83. end
  84. if fields["btn_modstore_page_up"] then
  85. if modstore.current_list ~= nil and modstore.current_list.page > 0 then
  86. modstore.current_list.page = modstore.current_list.page - 1
  87. end
  88. end
  89. if fields["btn_modstore_page_down"] then
  90. if modstore.current_list ~= nil and
  91. modstore.current_list.page <modstore.current_list.pagecount-1 then
  92. modstore.current_list.page = modstore.current_list.page +1
  93. end
  94. end
  95. if fields["btn_confirm_mod_successfull"] then
  96. return {
  97. current_tab = modstore.tabnames[1],
  98. is_dialog = true,
  99. show_buttons = false
  100. }
  101. end
  102. for i=1, modstore.modsperpage, 1 do
  103. local installbtn = "btn_install_mod_" .. i
  104. if fields[installbtn] then
  105. local modlistentry =
  106. modstore.current_list.page * modstore.modsperpage + i
  107. local moddetails = modstore.get_details(modstore.current_list.data[modlistentry].id)
  108. local fullurl = engine.setting_get("modstore_download_url") ..
  109. moddetails.download_url
  110. local modfilename = os.tempfolder() .. ".zip"
  111. if engine.download_file(fullurl,modfilename) then
  112. modmgr.installmod(modfilename,moddetails.basename)
  113. os.remove(modfilename)
  114. modstore.lastmodtitle = modstore.current_list.data[modlistentry].title
  115. return {
  116. current_tab = "modstore_mod_installed",
  117. is_dialog = true,
  118. show_buttons = false
  119. }
  120. else
  121. gamedata.errormessage = "Unable to download " ..
  122. moddetails.download_url .. " (internet connection?)"
  123. end
  124. end
  125. end
  126. end
  127. --------------------------------------------------------------------------------
  128. function modstore.update_modlist()
  129. modstore.modlist_unsorted = {}
  130. modstore.modlist_unsorted.data = engine.get_modstore_list()
  131. if modstore.modlist_unsorted.data ~= nil then
  132. modstore.modlist_unsorted.pagecount =
  133. math.ceil((#modstore.modlist_unsorted.data / modstore.modsperpage))
  134. else
  135. modstore.modlist_unsorted.data = {}
  136. modstore.modlist_unsorted.pagecount = 1
  137. end
  138. modstore.modlist_unsorted.page = 0
  139. end
  140. --------------------------------------------------------------------------------
  141. function modstore.getmodlist(list)
  142. local retval = ""
  143. retval = retval .. "label[10,-0.4;" .. fgettext("Page $1 of $2", list.page+1, list.pagecount) .. "]"
  144. retval = retval .. "button[11.6,-0.1;0.5,0.5;btn_modstore_page_up;^]"
  145. retval = retval .. "box[11.6,0.35;0.28,8.6;000000]"
  146. local scrollbarpos = 0.35 + (8.1/(list.pagecount-1)) * list.page
  147. retval = retval .. "box[11.6," ..scrollbarpos .. ";0.28,0.5;32CD32]"
  148. retval = retval .. "button[11.6,9.0;0.5,0.5;btn_modstore_page_down;v]"
  149. if #list.data < (list.page * modstore.modsperpage) then
  150. return retval
  151. end
  152. local endmod = (list.page * modstore.modsperpage) + modstore.modsperpage
  153. if (endmod > #list.data) then
  154. endmod = #list.data
  155. end
  156. for i=(list.page * modstore.modsperpage) +1, endmod, 1 do
  157. --getmoddetails
  158. local details = modstore.get_details(list.data[i].id)
  159. if details ~= nil then
  160. local screenshot_ypos = (i-1 - (list.page * modstore.modsperpage))*1.9 +0.2
  161. retval = retval .. "box[0," .. screenshot_ypos .. ";11.4,1.75;FFFFFF]"
  162. --screenshot
  163. if details.screenshot_url ~= nil and
  164. details.screenshot_url ~= "" then
  165. if list.data[i].texturename == nil then
  166. local fullurl = engine.setting_get("modstore_download_url") ..
  167. details.screenshot_url
  168. local filename = os.tempfolder()
  169. if engine.download_file(fullurl,filename) then
  170. list.data[i].texturename = filename
  171. end
  172. end
  173. end
  174. if list.data[i].texturename == nil then
  175. list.data[i].texturename = modstore.basetexturedir .. "no_screenshot.png"
  176. end
  177. retval = retval .. "image[0,".. screenshot_ypos .. ";3,2;" ..
  178. engine.formspec_escape(list.data[i].texturename) .. "]"
  179. --title + author
  180. retval = retval .."label[2.75," .. screenshot_ypos .. ";" ..
  181. engine.formspec_escape(details.title) .. " (" .. details.author .. ")]"
  182. --description
  183. local descriptiony = screenshot_ypos + 0.5
  184. retval = retval .. "textarea[3," .. descriptiony .. ";6.5,1.55;;" ..
  185. engine.formspec_escape(details.description) .. ";]"
  186. --rating
  187. local ratingy = screenshot_ypos + 0.6
  188. retval = retval .."label[10.1," .. ratingy .. ";" ..
  189. fgettext("Rating") .. ": " .. details.rating .."]"
  190. --install button
  191. local buttony = screenshot_ypos + 1.2
  192. local buttonnumber = (i - (list.page * modstore.modsperpage))
  193. retval = retval .."button[9.6," .. buttony .. ";2,0.5;btn_install_mod_" .. buttonnumber .. ";"
  194. if modmgr.mod_exists(details.basename) then
  195. retval = retval .. fgettext("re-Install") .."]"
  196. else
  197. retval = retval .. fgettext("Install") .."]"
  198. end
  199. end
  200. end
  201. modstore.current_list = list
  202. return retval
  203. end
  204. --------------------------------------------------------------------------------
  205. function modstore.get_details(modid)
  206. if modstore.details_cache[modid] ~= nil then
  207. return modstore.details_cache[modid]
  208. end
  209. local retval = engine.get_modstore_details(tostring(modid))
  210. modstore.details_cache[modid] = retval
  211. return retval
  212. end