dlg_install.lua 7.5 KB

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