dlg_config_world.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. local enabled_all = false
  19. local function modname_valid(name)
  20. return not name:find("[^a-z0-9_]")
  21. end
  22. local function init_data(data)
  23. data.list = filterlist.create(
  24. pkgmgr.preparemodlist,
  25. pkgmgr.comparemod,
  26. function(element, uid)
  27. if element.name == uid then
  28. return true
  29. end
  30. end,
  31. function(element, criteria)
  32. if criteria.hide_game and
  33. element.is_game_content then
  34. return false
  35. end
  36. if criteria.hide_modpackcontents and
  37. element.modpack ~= nil then
  38. return false
  39. end
  40. return true
  41. end,
  42. {
  43. worldpath = data.worldspec.path,
  44. gameid = data.worldspec.gameid
  45. })
  46. if data.selected_mod > data.list:size() then
  47. data.selected_mod = 0
  48. end
  49. data.list:set_filtercriteria({
  50. hide_game = data.hide_gamemods,
  51. hide_modpackcontents = data.hide_modpackcontents
  52. })
  53. -- Sorting is already done by pgkmgr.get_mods
  54. end
  55. -- Returns errors errors and a list of all enabled mods (inc. game and world mods)
  56. --
  57. -- `with_errors` is a table from mod virtual path to `{ type = "error" | "warning" }`.
  58. -- `enabled_mods_by_name` is a table from mod virtual path to `true`.
  59. --
  60. -- @param world_path Path to the world
  61. -- @param all_mods List of mods, with `enabled` property.
  62. -- @returns with_errors, enabled_mods_by_name
  63. local function check_mod_configuration(world_path, all_mods)
  64. -- Build up lookup tables for enabled mods and all mods by vpath
  65. local enabled_mod_paths = {}
  66. local all_mods_by_vpath = {}
  67. for _, mod in ipairs(all_mods) do
  68. if mod.type == "mod" then
  69. all_mods_by_vpath[mod.virtual_path] = mod
  70. end
  71. if mod.enabled then
  72. enabled_mod_paths[mod.virtual_path] = mod.path
  73. end
  74. end
  75. -- Use the engine's mod configuration code to resolve dependencies and return any errors
  76. local config_status = core.check_mod_configuration(world_path, enabled_mod_paths)
  77. -- Build the list of enabled mod virtual paths
  78. local enabled_mods_by_name = {}
  79. for _, mod in ipairs(config_status.satisfied_mods) do
  80. assert(mod.virtual_path ~= "")
  81. enabled_mods_by_name[mod.name] = all_mods_by_vpath[mod.virtual_path] or mod
  82. end
  83. for _, mod in ipairs(config_status.unsatisfied_mods) do
  84. assert(mod.virtual_path ~= "")
  85. enabled_mods_by_name[mod.name] = all_mods_by_vpath[mod.virtual_path] or mod
  86. end
  87. -- Build the table of errors
  88. local with_error = {}
  89. for _, mod in ipairs(config_status.unsatisfied_mods) do
  90. local error = { type = "warning" }
  91. with_error[mod.virtual_path] = error
  92. for _, depname in ipairs(mod.unsatisfied_depends) do
  93. if not enabled_mods_by_name[depname] then
  94. error.type = "error"
  95. break
  96. end
  97. end
  98. end
  99. return with_error, enabled_mods_by_name
  100. end
  101. local function get_formspec(data)
  102. if not data.list then
  103. init_data(data)
  104. end
  105. local all_mods = data.list:get_list()
  106. local with_error, enabled_mods_by_name = check_mod_configuration(data.worldspec.path, all_mods)
  107. local mod = all_mods[data.selected_mod] or {name = ""}
  108. local retval =
  109. "size[11.5,7.5,true]" ..
  110. "label[0.5,0;" .. fgettext("World:") .. "]" ..
  111. "label[1.75,0;" .. data.worldspec.name .. "]"
  112. if mod.is_modpack or mod.type == "game" then
  113. local info = core.formspec_escape(
  114. core.get_content_info(mod.path).description)
  115. if info == "" then
  116. if mod.is_modpack then
  117. info = fgettext("No modpack description provided.")
  118. else
  119. info = fgettext("No game description provided.")
  120. end
  121. end
  122. retval = retval ..
  123. "textarea[0.25,0.7;5.75,7.2;;" .. info .. ";]"
  124. else
  125. local hard_deps, soft_deps = pkgmgr.get_dependencies(mod.path)
  126. -- Add error messages to dep lists
  127. if mod.enabled or mod.is_game_content then
  128. for i, dep_name in ipairs(hard_deps) do
  129. local dep = enabled_mods_by_name[dep_name]
  130. if not dep then
  131. hard_deps[i] = mt_color_red .. dep_name .. " " .. fgettext("(Unsatisfied)")
  132. elseif with_error[dep.virtual_path] then
  133. hard_deps[i] = mt_color_orange .. dep_name .. " " .. fgettext("(Enabled, has error)")
  134. else
  135. hard_deps[i] = mt_color_green .. dep_name
  136. end
  137. end
  138. for i, dep_name in ipairs(soft_deps) do
  139. local dep = enabled_mods_by_name[dep_name]
  140. if dep and with_error[dep.virtual_path] then
  141. soft_deps[i] = mt_color_orange .. dep_name .. " " .. fgettext("(Enabled, has error)")
  142. elseif dep then
  143. soft_deps[i] = mt_color_green .. dep_name
  144. end
  145. end
  146. end
  147. local hard_deps_str = table.concat(hard_deps, ",")
  148. local soft_deps_str = table.concat(soft_deps, ",")
  149. retval = retval ..
  150. "label[0,0.7;" .. fgettext("Mod:") .. "]" ..
  151. "label[0.75,0.7;" .. mod.name .. "]"
  152. if hard_deps_str == "" then
  153. if soft_deps_str == "" then
  154. retval = retval ..
  155. "label[0,1.25;" ..
  156. fgettext("No (optional) dependencies") .. "]"
  157. else
  158. retval = retval ..
  159. "label[0,1.25;" .. fgettext("No hard dependencies") ..
  160. "]" ..
  161. "label[0,1.75;" .. fgettext("Optional dependencies:") ..
  162. "]" ..
  163. "textlist[0,2.25;5,4;world_config_optdepends;" ..
  164. soft_deps_str .. ";0]"
  165. end
  166. else
  167. if soft_deps_str == "" then
  168. retval = retval ..
  169. "label[0,1.25;" .. fgettext("Dependencies:") .. "]" ..
  170. "textlist[0,1.75;5,4;world_config_depends;" ..
  171. hard_deps_str .. ";0]" ..
  172. "label[0,6;" .. fgettext("No optional dependencies") .. "]"
  173. else
  174. retval = retval ..
  175. "label[0,1.25;" .. fgettext("Dependencies:") .. "]" ..
  176. "textlist[0,1.75;5,2.125;world_config_depends;" ..
  177. hard_deps_str .. ";0]" ..
  178. "label[0,3.9;" .. fgettext("Optional dependencies:") ..
  179. "]" ..
  180. "textlist[0,4.375;5,1.8;world_config_optdepends;" ..
  181. soft_deps_str .. ";0]"
  182. end
  183. end
  184. end
  185. retval = retval ..
  186. "button[3.25,7;2.5,0.5;btn_config_world_save;" ..
  187. fgettext("Save") .. "]" ..
  188. "button[5.75,7;2.5,0.5;btn_config_world_cancel;" ..
  189. fgettext("Cancel") .. "]" ..
  190. "button[9,7;2.5,0.5;btn_config_world_cdb;" ..
  191. fgettext("Find More Mods") .. "]"
  192. if mod.name ~= "" and not mod.is_game_content then
  193. if mod.is_modpack then
  194. if pkgmgr.is_modpack_entirely_enabled(data, mod.name) then
  195. retval = retval ..
  196. "button[5.5,0.125;3,0.5;btn_mp_disable;" ..
  197. fgettext("Disable modpack") .. "]"
  198. else
  199. retval = retval ..
  200. "button[5.5,0.125;3,0.5;btn_mp_enable;" ..
  201. fgettext("Enable modpack") .. "]"
  202. end
  203. else
  204. retval = retval ..
  205. "checkbox[5.5,-0.125;cb_mod_enable;" .. fgettext("enabled") ..
  206. ";" .. tostring(mod.enabled) .. "]"
  207. end
  208. end
  209. if enabled_all then
  210. retval = retval ..
  211. "button[8.95,0.125;2.5,0.5;btn_disable_all_mods;" ..
  212. fgettext("Disable all") .. "]"
  213. else
  214. retval = retval ..
  215. "button[8.95,0.125;2.5,0.5;btn_enable_all_mods;" ..
  216. fgettext("Enable all") .. "]"
  217. end
  218. local use_technical_names = core.settings:get_bool("show_technical_names")
  219. return retval ..
  220. "tablecolumns[color;tree;image,align=inline,width=1.5,0=" .. core.formspec_escape(defaulttexturedir .. "blank.png") ..
  221. ",1=" .. core.formspec_escape(defaulttexturedir .. "checkbox_16.png") ..
  222. ",2=" .. core.formspec_escape(defaulttexturedir .. "error_icon_orange.png") ..
  223. ",3=" .. core.formspec_escape(defaulttexturedir .. "error_icon_red.png") .. ";text]" ..
  224. "table[5.5,0.75;5.75,6;world_config_modlist;" ..
  225. pkgmgr.render_packagelist(data.list, use_technical_names, with_error) .. ";" .. data.selected_mod .."]"
  226. end
  227. local function handle_buttons(this, fields)
  228. if fields.world_config_modlist then
  229. local event = core.explode_table_event(fields.world_config_modlist)
  230. this.data.selected_mod = event.row
  231. core.settings:set("world_config_selected_mod", event.row)
  232. if event.type == "DCL" then
  233. pkgmgr.enable_mod(this)
  234. end
  235. return true
  236. end
  237. if fields.key_enter then
  238. pkgmgr.enable_mod(this)
  239. return true
  240. end
  241. if fields.cb_mod_enable ~= nil then
  242. pkgmgr.enable_mod(this, core.is_yes(fields.cb_mod_enable))
  243. return true
  244. end
  245. if fields.btn_mp_enable ~= nil or
  246. fields.btn_mp_disable then
  247. pkgmgr.enable_mod(this, fields.btn_mp_enable ~= nil)
  248. return true
  249. end
  250. if fields.btn_config_world_save then
  251. local filename = this.data.worldspec.path .. DIR_DELIM .. "world.mt"
  252. local worldfile = Settings(filename)
  253. local mods = worldfile:to_table()
  254. local rawlist = this.data.list:get_raw_list()
  255. local was_set = {}
  256. for i = 1, #rawlist do
  257. local mod = rawlist[i]
  258. if not mod.is_modpack and
  259. not mod.is_game_content then
  260. if modname_valid(mod.name) then
  261. if mod.enabled then
  262. worldfile:set("load_mod_" .. mod.name, mod.virtual_path)
  263. was_set[mod.name] = true
  264. elseif not was_set[mod.name] then
  265. worldfile:set("load_mod_" .. mod.name, "false")
  266. end
  267. elseif mod.enabled then
  268. gamedata.errormessage = fgettext_ne("Failed to enable mo" ..
  269. "d \"$1\" as it contains disallowed characters. " ..
  270. "Only characters [a-z0-9_] are allowed.",
  271. mod.name)
  272. end
  273. mods["load_mod_" .. mod.name] = nil
  274. end
  275. end
  276. -- Remove mods that are not present anymore
  277. for key in pairs(mods) do
  278. if key:sub(1, 9) == "load_mod_" then
  279. worldfile:remove(key)
  280. end
  281. end
  282. if not worldfile:write() then
  283. core.log("error", "Failed to write world config file")
  284. end
  285. this:delete()
  286. return true
  287. end
  288. if fields.btn_config_world_cancel then
  289. this:delete()
  290. return true
  291. end
  292. if fields.btn_config_world_cdb then
  293. this.data.list = nil
  294. local dlg = create_contentdb_dlg("mod")
  295. dlg:set_parent(this)
  296. this:hide()
  297. dlg:show()
  298. return true
  299. end
  300. if fields.btn_enable_all_mods then
  301. local list = this.data.list:get_raw_list()
  302. -- When multiple copies of a mod are installed, we need to avoid enabling multiple of them
  303. -- at a time. So lets first collect all the enabled mods, and then use this to exclude
  304. -- multiple enables.
  305. local was_enabled = {}
  306. for i = 1, #list do
  307. if not list[i].is_game_content
  308. and not list[i].is_modpack and list[i].enabled then
  309. was_enabled[list[i].name] = true
  310. end
  311. end
  312. for i = 1, #list do
  313. if not list[i].is_game_content and not list[i].is_modpack and
  314. not was_enabled[list[i].name] then
  315. list[i].enabled = true
  316. was_enabled[list[i].name] = true
  317. end
  318. end
  319. enabled_all = true
  320. return true
  321. end
  322. if fields.btn_disable_all_mods then
  323. local list = this.data.list:get_raw_list()
  324. for i = 1, #list do
  325. if not list[i].is_game_content
  326. and not list[i].is_modpack then
  327. list[i].enabled = false
  328. end
  329. end
  330. enabled_all = false
  331. return true
  332. end
  333. return false
  334. end
  335. function create_configure_world_dlg(worldidx)
  336. local dlg = dialog_create("sp_config_world", get_formspec, handle_buttons)
  337. dlg.data.selected_mod = tonumber(
  338. core.settings:get("world_config_selected_mod")) or 0
  339. dlg.data.worldspec = core.get_worlds()[worldidx]
  340. if not dlg.data.worldspec then
  341. dlg:delete()
  342. return
  343. end
  344. dlg.data.worldconfig = pkgmgr.get_worldconfig(dlg.data.worldspec.path)
  345. if not dlg.data.worldconfig or not dlg.data.worldconfig.id or
  346. dlg.data.worldconfig.id == "" then
  347. dlg:delete()
  348. return
  349. end
  350. return dlg
  351. end