2
0

dlg_install.lua 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. --Luanti
  2. --Copyright (C) 2018-24 rubenwardy
  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. local function is_still_visible(dlg)
  18. local this = ui.find_by_name("install_dialog")
  19. return this == dlg and not dlg.hidden
  20. end
  21. local function get_loading_formspec()
  22. local TOUCH_GUI = core.settings:get_bool("touch_gui")
  23. local w = TOUCH_GUI and 14 or 7
  24. local formspec = {
  25. "formspec_version[3]",
  26. "size[", w, ",9.05]",
  27. TOUCH_GUI and "padding[0.01,0.01]" or "position[0.5,0.55]",
  28. "label[3,4.525;", fgettext("Loading..."), "]",
  29. }
  30. return table.concat(formspec)
  31. end
  32. local function get_formspec(data)
  33. if not data.has_hard_deps_ready then
  34. return get_loading_formspec()
  35. end
  36. local selected_game, selected_game_idx = pkgmgr.find_by_gameid(core.settings:get("menu_last_game"))
  37. if not selected_game_idx then
  38. selected_game_idx = 1
  39. selected_game = pkgmgr.games[1]
  40. end
  41. local game_list = {}
  42. for i, game in ipairs(pkgmgr.games) do
  43. game_list[i] = core.formspec_escape(game.title)
  44. end
  45. if not data.deps_ready[selected_game_idx] and
  46. not data.deps_loading[selected_game_idx] then
  47. data.deps_loading[selected_game_idx] = true
  48. contentdb.resolve_dependencies(data.package, selected_game, function(deps)
  49. if not is_still_visible(data.dlg) then
  50. return
  51. end
  52. data.deps_ready[selected_game_idx] = deps
  53. ui.update()
  54. end)
  55. end
  56. -- The value of `data.deps_ready[selected_game_idx]` may have changed
  57. -- since the last if statement since `contentdb.resolve_dependencies`
  58. -- calls the callback immediately if the dependencies are already cached.
  59. if not data.deps_ready[selected_game_idx] then
  60. return get_loading_formspec()
  61. end
  62. local package = data.package
  63. local will_install_deps = data.will_install_deps
  64. local deps_to_install = 0
  65. local deps_not_found = 0
  66. data.deps_chosen = data.deps_ready[selected_game_idx]
  67. local formatted_deps = {}
  68. for _, dep in pairs(data.deps_chosen) do
  69. formatted_deps[#formatted_deps + 1] = "#fff"
  70. formatted_deps[#formatted_deps + 1] = core.formspec_escape(dep.name)
  71. if dep.installed then
  72. formatted_deps[#formatted_deps + 1] = "#ccf"
  73. formatted_deps[#formatted_deps + 1] = fgettext("Already installed")
  74. elseif dep.package then
  75. formatted_deps[#formatted_deps + 1] = "#cfc"
  76. formatted_deps[#formatted_deps + 1] = fgettext("$1 by $2", dep.package.title, dep.package.author)
  77. deps_to_install = deps_to_install + 1
  78. else
  79. formatted_deps[#formatted_deps + 1] = "#f00"
  80. formatted_deps[#formatted_deps + 1] = fgettext("Not found")
  81. deps_not_found = deps_not_found + 1
  82. end
  83. end
  84. local message_bg = "#3333"
  85. local message
  86. if will_install_deps then
  87. message = fgettext("$1 and $2 dependencies will be installed.", package.title, deps_to_install)
  88. else
  89. message = fgettext("$1 will be installed, and $2 dependencies will be skipped.", package.title, deps_to_install)
  90. end
  91. if deps_not_found > 0 then
  92. message = fgettext("$1 required dependencies could not be found.", deps_not_found) ..
  93. " " .. fgettext("Please check that the base game is correct.", deps_not_found) ..
  94. "\n" .. message
  95. message_bg = mt_color_orange
  96. end
  97. local TOUCH_GUI = core.settings:get_bool("touch_gui")
  98. local w = TOUCH_GUI and 14 or 7
  99. local padded_w = w - 2*0.375
  100. local dropdown_w = TOUCH_GUI and 10.2 or 4.25
  101. local button_w = (padded_w - 0.25) / 3
  102. local button_pad = button_w / 2
  103. local formspec = {
  104. "formspec_version[3]",
  105. "size[", w, ",9.05]",
  106. TOUCH_GUI and "padding[0.01,0.01]" or "position[0.5,0.55]",
  107. "style[title;border=false]",
  108. "box[0,0;", w, ",0.8;#3333]",
  109. "button[0,0;", w, ",0.8;title;", fgettext("Install $1", package.title) , "]",
  110. "container[0.375,1]",
  111. "label[0,0.4;", fgettext("Base Game:"), "]",
  112. "dropdown[", padded_w - dropdown_w, ",0;", dropdown_w, ",0.8;selected_game;",
  113. table.concat(game_list, ","), ";", selected_game_idx, "]",
  114. "label[0,1.1;", fgettext("Dependencies:"), "]",
  115. "tablecolumns[color;text;color;text]",
  116. "table[0,1.4;", padded_w, ",3;packages;", table.concat(formatted_deps, ","), "]",
  117. "container_end[]",
  118. "checkbox[0.375,5.7;will_install_deps;",
  119. fgettext("Install missing dependencies"), ";",
  120. will_install_deps and "true" or "false", "]",
  121. "box[0,6;", w, ",1.8;", message_bg, "]",
  122. "textarea[0.375,6.1;", padded_w, ",1.6;;;", message, "]",
  123. "container[", 0.375 + button_pad, ",8.05]",
  124. "button[0,0;", button_w, ",0.8;install_all;", fgettext("Install"), "]",
  125. "button[", 0.25 + button_w, ",0;", button_w, ",0.8;cancel;", fgettext("Cancel"), "]",
  126. "container_end[]",
  127. }
  128. return table.concat(formspec)
  129. end
  130. local function handle_submit(this, fields)
  131. local data = this.data
  132. if fields.cancel then
  133. this:delete()
  134. return true
  135. end
  136. if fields.will_install_deps ~= nil then
  137. data.will_install_deps = core.is_yes(fields.will_install_deps)
  138. return true
  139. end
  140. if fields.install_all then
  141. contentdb.queue_download(data.package, contentdb.REASON_NEW)
  142. if data.will_install_deps then
  143. for _, dep in pairs(data.deps_chosen) do
  144. if not dep.is_optional and not dep.installed and dep.package then
  145. contentdb.queue_download(dep.package, contentdb.REASON_DEPENDENCY)
  146. end
  147. end
  148. end
  149. this:delete()
  150. return true
  151. end
  152. if fields.selected_game then
  153. for _, game in pairs(pkgmgr.games) do
  154. if game.title == fields.selected_game then
  155. core.settings:set("menu_last_game", game.id)
  156. break
  157. end
  158. end
  159. return true
  160. end
  161. return false
  162. end
  163. local function load_deps(dlg)
  164. local package = dlg.data.package
  165. contentdb.has_hard_deps(package, function(result)
  166. if not is_still_visible(dlg) then
  167. return
  168. end
  169. if result == nil then
  170. local parent = dlg.parent
  171. dlg:delete()
  172. local dlg2 = messagebox("error_checking_deps",
  173. fgettext("Error getting dependencies for package $1", package.url_part))
  174. dlg2:set_parent(parent)
  175. parent:hide()
  176. dlg2:show()
  177. elseif result == false then
  178. contentdb.queue_download(package, package.path and contentdb.REASON_UPDATE or contentdb.REASON_NEW)
  179. dlg:delete()
  180. else
  181. assert(result == true)
  182. dlg.data.has_hard_deps_ready = true
  183. end
  184. ui.update()
  185. end)
  186. end
  187. function create_install_dialog(package)
  188. local dlg = dialog_create("install_dialog", get_formspec, handle_submit, nil)
  189. dlg.data.deps_chosen = nil
  190. dlg.data.package = package
  191. dlg.data.will_install_deps = true
  192. dlg.data.has_hard_deps_ready = false
  193. dlg.data.deps_ready = {}
  194. dlg.data.deps_loading = {}
  195. dlg.load_deps = load_deps
  196. -- `get_formspec` needs to access `dlg` to check whether it's still open.
  197. -- It doesn't suffice to check that any "install_dialog" instance is open
  198. -- via `ui.find_by_name`, it's necessary to check for this exact instance.
  199. dlg.data.dlg = dlg
  200. return dlg
  201. end
  202. function install_or_update_package(parent, package)
  203. local install_parent
  204. if package.type == "mod" then
  205. install_parent = core.get_modpath()
  206. elseif package.type == "game" then
  207. install_parent = core.get_gamepath()
  208. elseif package.type == "txp" then
  209. install_parent = core.get_texturepath()
  210. else
  211. error("Unknown package type: " .. package.type)
  212. end
  213. if package.queued or package.downloading then
  214. return
  215. end
  216. local function on_confirm()
  217. local dlg = create_install_dialog(package)
  218. dlg:set_parent(parent)
  219. parent:hide()
  220. dlg:show()
  221. dlg:load_deps()
  222. end
  223. if package.type == "mod" and #pkgmgr.games == 0 then
  224. local dlg = messagebox("install_game",
  225. fgettext("You need to install a game before you can install a mod"))
  226. dlg:set_parent(parent)
  227. parent:hide()
  228. dlg:show()
  229. elseif not package.path and core.is_dir(install_parent .. DIR_DELIM .. package.name) then
  230. local dlg = create_confirm_overwrite(package, on_confirm)
  231. dlg:set_parent(parent)
  232. parent:hide()
  233. dlg:show()
  234. else
  235. on_confirm()
  236. end
  237. end