pkgmgr.lua 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. --Luanti
  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 function get_last_folder(text,count)
  19. local parts = text:split(DIR_DELIM)
  20. if count == nil then
  21. return parts[#parts]
  22. end
  23. local retval = ""
  24. for i=1,count,1 do
  25. retval = retval .. parts[#parts - (count-i)] .. DIR_DELIM
  26. end
  27. return retval
  28. end
  29. local function cleanup_path(temppath)
  30. local parts = temppath:split("-")
  31. temppath = ""
  32. for i=1,#parts,1 do
  33. if temppath ~= "" then
  34. temppath = temppath .. "_"
  35. end
  36. temppath = temppath .. parts[i]
  37. end
  38. parts = temppath:split(".")
  39. temppath = ""
  40. for i=1,#parts,1 do
  41. if temppath ~= "" then
  42. temppath = temppath .. "_"
  43. end
  44. temppath = temppath .. parts[i]
  45. end
  46. parts = temppath:split("'")
  47. temppath = ""
  48. for i=1,#parts,1 do
  49. if temppath ~= "" then
  50. temppath = temppath .. ""
  51. end
  52. temppath = temppath .. parts[i]
  53. end
  54. parts = temppath:split(" ")
  55. temppath = ""
  56. for i=1,#parts,1 do
  57. if temppath ~= "" then
  58. temppath = temppath
  59. end
  60. temppath = temppath .. parts[i]
  61. end
  62. return temppath
  63. end
  64. local function load_texture_packs(txtpath, retval)
  65. local list = core.get_dir_list(txtpath, true)
  66. local current_texture_path = core.settings:get("texture_path")
  67. for _, item in ipairs(list) do
  68. if item ~= "base" then
  69. local path = txtpath .. DIR_DELIM .. item .. DIR_DELIM
  70. local conf = Settings(path .. "texture_pack.conf")
  71. local enabled = path == current_texture_path
  72. local title = conf:get("title") or item
  73. -- list_* is only used if non-nil, else the regular versions are used.
  74. retval[#retval + 1] = {
  75. name = item,
  76. title = title,
  77. list_name = enabled and fgettext("$1 (Enabled)", item) or nil,
  78. list_title = enabled and fgettext("$1 (Enabled)", title) or nil,
  79. author = conf:get("author"),
  80. release = tonumber(conf:get("release")) or 0,
  81. type = "txp",
  82. path = path,
  83. enabled = enabled,
  84. }
  85. end
  86. end
  87. end
  88. --modmanager implementation
  89. pkgmgr = {}
  90. --- Scans a directory recursively for mods and adds them to `listing`
  91. -- @param path Absolute directory path to scan recursively
  92. -- @param virtual_path Prettified unique path (e.g. "mods", "mods/mt_modpack")
  93. -- @param listing Input. Flat array to insert located mods and modpacks
  94. -- @param modpack Currently processing modpack or nil/"" if none (recursion)
  95. function pkgmgr.get_mods(path, virtual_path, listing, modpack)
  96. local mods = core.get_dir_list(path, true)
  97. local added = {}
  98. for _, name in ipairs(mods) do
  99. if name:sub(1, 1) ~= "." then
  100. local mod_path = path .. DIR_DELIM .. name
  101. local mod_virtual_path = virtual_path .. "/" .. name
  102. local toadd = {
  103. dir_name = name,
  104. parent_dir = path,
  105. }
  106. listing[#listing + 1] = toadd
  107. added[#added + 1] = toadd
  108. -- Get config file
  109. local mod_conf
  110. local modpack_conf = io.open(mod_path .. DIR_DELIM .. "modpack.conf")
  111. if modpack_conf then
  112. toadd.is_modpack = true
  113. modpack_conf:close()
  114. mod_conf = Settings(mod_path .. DIR_DELIM .. "modpack.conf"):to_table()
  115. if mod_conf.name then
  116. name = mod_conf.name
  117. toadd.is_name_explicit = true
  118. end
  119. else
  120. mod_conf = Settings(mod_path .. DIR_DELIM .. "mod.conf"):to_table()
  121. if mod_conf.name then
  122. name = mod_conf.name
  123. toadd.is_name_explicit = true
  124. end
  125. end
  126. -- Read from config
  127. toadd.name = name
  128. toadd.title = mod_conf.title
  129. toadd.author = mod_conf.author
  130. toadd.release = tonumber(mod_conf.release) or 0
  131. toadd.path = mod_path
  132. toadd.virtual_path = mod_virtual_path
  133. toadd.type = "mod"
  134. -- Check modpack.txt
  135. -- Note: modpack.conf is already checked above
  136. local modpackfile = io.open(mod_path .. DIR_DELIM .. "modpack.txt")
  137. if modpackfile then
  138. modpackfile:close()
  139. toadd.is_modpack = true
  140. end
  141. -- Deal with modpack contents
  142. if modpack and modpack ~= "" then
  143. toadd.modpack = modpack
  144. elseif toadd.is_modpack then
  145. toadd.type = "modpack"
  146. toadd.is_modpack = true
  147. pkgmgr.get_mods(mod_path, mod_virtual_path, listing, name)
  148. end
  149. end
  150. end
  151. pkgmgr.update_translations(added)
  152. if not modpack then
  153. -- Sort all when the recursion is done
  154. table.sort(listing, function(a, b)
  155. return a.virtual_path:lower() < b.virtual_path:lower()
  156. end)
  157. end
  158. end
  159. --------------------------------------------------------------------------------
  160. function pkgmgr.reload_texture_packs()
  161. local txtpath = core.get_texturepath()
  162. local txtpath_system = core.get_texturepath_share()
  163. local retval = {}
  164. load_texture_packs(txtpath, retval)
  165. -- on portable versions these two paths coincide. It avoids loading the path twice
  166. if txtpath ~= txtpath_system then
  167. load_texture_packs(txtpath_system, retval)
  168. end
  169. pkgmgr.update_translations(retval)
  170. table.sort(retval, function(a, b)
  171. return a.title:lower() < b.title:lower()
  172. end)
  173. pkgmgr.texture_packs = retval
  174. end
  175. --------------------------------------------------------------------------------
  176. function pkgmgr.get_all()
  177. pkgmgr.load_all()
  178. local result = {}
  179. for _, mod in pairs(pkgmgr.global_mods:get_list()) do
  180. result[#result + 1] = mod
  181. end
  182. for _, game in pairs(pkgmgr.games) do
  183. result[#result + 1] = game
  184. end
  185. for _, txp in pairs(pkgmgr.texture_packs) do
  186. result[#result + 1] = txp
  187. end
  188. return result
  189. end
  190. --------------------------------------------------------------------------------
  191. function pkgmgr.get_folder_type(path)
  192. local testfile = io.open(path .. DIR_DELIM .. "init.lua","r")
  193. if testfile ~= nil then
  194. testfile:close()
  195. return { type = "mod", path = path }
  196. end
  197. testfile = io.open(path .. DIR_DELIM .. "modpack.conf","r")
  198. if testfile ~= nil then
  199. testfile:close()
  200. return { type = "modpack", path = path }
  201. end
  202. testfile = io.open(path .. DIR_DELIM .. "modpack.txt","r")
  203. if testfile ~= nil then
  204. testfile:close()
  205. return { type = "modpack", path = path }
  206. end
  207. testfile = io.open(path .. DIR_DELIM .. "game.conf","r")
  208. if testfile ~= nil then
  209. testfile:close()
  210. return { type = "game", path = path }
  211. end
  212. testfile = io.open(path .. DIR_DELIM .. "texture_pack.conf","r")
  213. if testfile ~= nil then
  214. testfile:close()
  215. return { type = "txp", path = path }
  216. end
  217. return nil
  218. end
  219. -------------------------------------------------------------------------------
  220. function pkgmgr.get_base_folder(temppath)
  221. if temppath == nil then
  222. return { type = "invalid", path = "" }
  223. end
  224. local ret = pkgmgr.get_folder_type(temppath)
  225. if ret then
  226. return ret
  227. end
  228. local subdirs = core.get_dir_list(temppath, true)
  229. if #subdirs == 1 then
  230. ret = pkgmgr.get_folder_type(temppath .. DIR_DELIM .. subdirs[1])
  231. if ret then
  232. return ret
  233. else
  234. return { type = "invalid", path = temppath .. DIR_DELIM .. subdirs[1] }
  235. end
  236. end
  237. return nil
  238. end
  239. --------------------------------------------------------------------------------
  240. function pkgmgr.is_valid_modname(modpath)
  241. return modpath:match("[^a-z0-9_]") == nil
  242. end
  243. --------------------------------------------------------------------------------
  244. --- @param render_list filterlist
  245. --- @param use_technical_names boolean to show technical names instead of human-readable titles
  246. --- @param with_icon table or nil, from virtual path to icon object
  247. function pkgmgr.render_packagelist(render_list, use_technical_names, with_icon)
  248. if not render_list then
  249. if not pkgmgr.global_mods then
  250. pkgmgr.reload_global_mods()
  251. end
  252. render_list = pkgmgr.global_mods
  253. end
  254. local list = render_list:get_list()
  255. local retval = {}
  256. for i, v in ipairs(list) do
  257. local color = ""
  258. local icon = 0
  259. local icon_info = with_icon and with_icon[v.virtual_path or v.path]
  260. local function update_icon_info(val)
  261. if val and (not icon_info or (icon_info.type == "warning" and val.type == "error")) then
  262. icon_info = val
  263. end
  264. end
  265. if v.is_modpack then
  266. local rawlist = render_list:get_raw_list()
  267. color = mt_color_dark_green
  268. for j = 1, #rawlist do
  269. if rawlist[j].modpack == list[i].name then
  270. if with_icon then
  271. update_icon_info(with_icon[rawlist[j].virtual_path or rawlist[j].path])
  272. end
  273. if rawlist[j].enabled then
  274. icon = 1
  275. else
  276. -- Modpack not entirely enabled so showing as grey
  277. color = mt_color_grey
  278. end
  279. end
  280. end
  281. elseif v.is_game_content or v.type == "game" then
  282. icon = 1
  283. color = mt_color_blue
  284. local rawlist = render_list:get_raw_list()
  285. if v.type == "game" and with_icon then
  286. for j = 1, #rawlist do
  287. if rawlist[j].is_game_content then
  288. update_icon_info(with_icon[rawlist[j].virtual_path or rawlist[j].path])
  289. end
  290. end
  291. end
  292. elseif v.enabled or v.type == "txp" then
  293. icon = 1
  294. color = mt_color_green
  295. end
  296. if icon_info then
  297. if icon_info.type == "warning" then
  298. color = mt_color_orange
  299. icon = 2
  300. elseif icon_info.type == "error" then
  301. color = mt_color_red
  302. icon = 3
  303. elseif icon_info.type == "update" then
  304. icon = 4
  305. else
  306. error("Unknown icon type " .. icon_info.type)
  307. end
  308. end
  309. retval[#retval + 1] = color
  310. if v.modpack ~= nil or v.loc == "game" then
  311. retval[#retval + 1] = "1"
  312. else
  313. retval[#retval + 1] = "0"
  314. end
  315. if with_icon then
  316. retval[#retval + 1] = icon
  317. end
  318. if use_technical_names then
  319. retval[#retval + 1] = core.formspec_escape(v.list_name or v.name)
  320. else
  321. retval[#retval + 1] = core.formspec_escape(v.list_title or v.list_name or v.title or v.name)
  322. end
  323. end
  324. return table.concat(retval, ",")
  325. end
  326. --------------------------------------------------------------------------------
  327. function pkgmgr.get_dependencies(path)
  328. if path == nil then
  329. return {}, {}
  330. end
  331. local info = core.get_content_info(path)
  332. return info.depends or {}, info.optional_depends or {}
  333. end
  334. ----------- tests whether all of the mods in the modpack are enabled -----------
  335. function pkgmgr.is_modpack_entirely_enabled(data, name)
  336. local rawlist = data.list:get_raw_list()
  337. for j = 1, #rawlist do
  338. if rawlist[j].modpack == name and not rawlist[j].enabled then
  339. return false
  340. end
  341. end
  342. return true
  343. end
  344. local function disable_all_by_name(list, name, except)
  345. for i=1, #list do
  346. if list[i].name == name and list[i] ~= except then
  347. list[i].enabled = false
  348. end
  349. end
  350. end
  351. ---------- toggles or en/disables a mod or modpack and its dependencies --------
  352. local function toggle_mod_or_modpack(list, toggled_mods, enabled_mods, toset, mod)
  353. if not mod.is_modpack then
  354. -- Toggle or en/disable the mod
  355. if toset == nil then
  356. toset = not mod.enabled
  357. end
  358. if mod.enabled ~= toset then
  359. toggled_mods[#toggled_mods+1] = mod.name
  360. end
  361. if toset then
  362. -- Mark this mod for recursive dependency traversal
  363. enabled_mods[mod.name] = true
  364. -- Disable other mods with the same name
  365. disable_all_by_name(list, mod.name, mod)
  366. end
  367. mod.enabled = toset
  368. else
  369. -- Toggle or en/disable every mod in the modpack,
  370. -- interleaved unsupported
  371. for i = 1, #list do
  372. if list[i].modpack == mod.name then
  373. toggle_mod_or_modpack(list, toggled_mods, enabled_mods, toset, list[i])
  374. end
  375. end
  376. end
  377. end
  378. function pkgmgr.enable_mod(this, toset)
  379. local list = this.data.list:get_list()
  380. local mod = list[this.data.selected_mod]
  381. -- Game mods can't be enabled or disabled
  382. if mod.is_game_content then
  383. return
  384. end
  385. local toggled_mods = {}
  386. local enabled_mods = {}
  387. toggle_mod_or_modpack(list, toggled_mods, enabled_mods, toset, mod)
  388. if next(enabled_mods) == nil then
  389. -- Mod(s) were disabled, so no dependencies need to be enabled
  390. table.sort(toggled_mods)
  391. core.log("info", "Following mods were disabled: " ..
  392. table.concat(toggled_mods, ", "))
  393. return
  394. end
  395. -- Enable mods' depends after activation
  396. -- Make a list of mod ids indexed by their names. Among mods with the
  397. -- same name, enabled mods take precedence, after which game mods take
  398. -- precedence, being last in the mod list.
  399. local mod_ids = {}
  400. for id, mod2 in pairs(list) do
  401. if mod2.type == "mod" and not mod2.is_modpack then
  402. local prev_id = mod_ids[mod2.name]
  403. if not prev_id or not list[prev_id].enabled then
  404. mod_ids[mod2.name] = id
  405. end
  406. end
  407. end
  408. -- to_enable is used as a DFS stack with sp as stack pointer
  409. local to_enable = {}
  410. local sp = 0
  411. for name in pairs(enabled_mods) do
  412. local depends = pkgmgr.get_dependencies(list[mod_ids[name]].path)
  413. for i = 1, #depends do
  414. local dependency_name = depends[i]
  415. if not enabled_mods[dependency_name] then
  416. sp = sp+1
  417. to_enable[sp] = dependency_name
  418. end
  419. end
  420. end
  421. -- If sp is 0, every dependency is already activated
  422. while sp > 0 do
  423. local name = to_enable[sp]
  424. sp = sp-1
  425. if not enabled_mods[name] then
  426. enabled_mods[name] = true
  427. local mod_to_enable = list[mod_ids[name]]
  428. if not mod_to_enable then
  429. core.log("warning", "Mod dependency \"" .. name ..
  430. "\" not found!")
  431. elseif not mod_to_enable.is_game_content then
  432. if not mod_to_enable.enabled then
  433. mod_to_enable.enabled = true
  434. toggled_mods[#toggled_mods+1] = mod_to_enable.name
  435. end
  436. -- Push the dependencies of the dependency onto the stack
  437. local depends = pkgmgr.get_dependencies(mod_to_enable.path)
  438. for i = 1, #depends do
  439. if not enabled_mods[depends[i]] then
  440. sp = sp+1
  441. to_enable[sp] = depends[i]
  442. end
  443. end
  444. end
  445. end
  446. end
  447. -- Log the list of enabled mods
  448. table.sort(toggled_mods)
  449. core.log("info", "Following mods were enabled: " ..
  450. table.concat(toggled_mods, ", "))
  451. end
  452. --------------------------------------------------------------------------------
  453. function pkgmgr.get_worldconfig(worldpath)
  454. local filename = worldpath ..
  455. DIR_DELIM .. "world.mt"
  456. local worldfile = Settings(filename)
  457. local worldconfig = {}
  458. worldconfig.global_mods = {}
  459. worldconfig.game_mods = {}
  460. for key,value in pairs(worldfile:to_table()) do
  461. if key == "gameid" then
  462. worldconfig.id = value
  463. elseif key:sub(0, 9) == "load_mod_" then
  464. -- Compatibility: Check against "nil" which was erroneously used
  465. -- as value for fresh configured worlds
  466. worldconfig.global_mods[key] = value ~= "false" and value ~= "nil"
  467. and value
  468. else
  469. worldconfig[key] = value
  470. end
  471. end
  472. --read gamemods
  473. local gamespec = pkgmgr.find_by_gameid(worldconfig.id)
  474. pkgmgr.get_game_mods(gamespec, worldconfig.game_mods)
  475. return worldconfig
  476. end
  477. --------------------------------------------------------------------------------
  478. -- Caller is responsible for reloading content types (see reload_by_type)
  479. function pkgmgr.install_dir(expected_type, path, basename, targetpath)
  480. assert(type(expected_type) == "string")
  481. assert(type(path) == "string")
  482. assert(basename == nil or type(basename) == "string")
  483. assert(targetpath == nil or type(targetpath) == "string")
  484. local basefolder = pkgmgr.get_base_folder(path)
  485. if expected_type == "txp" then
  486. assert(basename)
  487. -- There's no good way to detect a texture pack, so let's just assume
  488. -- it's correct for now.
  489. if basefolder and basefolder.type ~= "invalid" and basefolder.type ~= "txp" then
  490. return nil, fgettext_ne("Unable to install a $1 as a texture pack", basefolder.type)
  491. end
  492. local from = basefolder and basefolder.path or path
  493. if not targetpath then
  494. targetpath = core.get_texturepath() .. DIR_DELIM .. basename
  495. end
  496. core.delete_dir(targetpath)
  497. if not core.copy_dir(from, targetpath, false) then
  498. return nil,
  499. fgettext_ne("Failed to install $1 to $2", basename, targetpath)
  500. end
  501. return targetpath, nil
  502. elseif not basefolder then
  503. return nil, fgettext_ne("Unable to find a valid mod, modpack, or game")
  504. end
  505. -- Check type
  506. if basefolder.type ~= expected_type and (basefolder.type ~= "modpack" or expected_type ~= "mod") then
  507. return nil, fgettext_ne("Unable to install a $1 as a $2", basefolder.type, expected_type)
  508. end
  509. -- Set targetpath if not predetermined
  510. if not targetpath then
  511. local content_path
  512. if basefolder.type == "modpack" or basefolder.type == "mod" then
  513. if not basename then
  514. basename = get_last_folder(cleanup_path(basefolder.path))
  515. end
  516. content_path = core.get_modpath()
  517. elseif basefolder.type == "game" then
  518. content_path = core.get_gamepath()
  519. else
  520. error("Unknown content type")
  521. end
  522. if basename and (basefolder.type ~= "mod" or pkgmgr.is_valid_modname(basename)) then
  523. targetpath = content_path .. DIR_DELIM .. basename
  524. else
  525. return nil,
  526. fgettext_ne("Install: Unable to find suitable folder name for $1", path)
  527. end
  528. end
  529. -- Copy it
  530. core.delete_dir(targetpath)
  531. if not core.copy_dir(basefolder.path, targetpath, false) then
  532. return nil,
  533. fgettext_ne("Failed to install $1 to $2", basename, targetpath)
  534. end
  535. return targetpath, nil
  536. end
  537. --------------------------------------------------------------------------------
  538. function pkgmgr.preparemodlist(data)
  539. local retval = {}
  540. local global_mods = {}
  541. local game_mods = {}
  542. --read global mods
  543. local modpaths = core.get_modpaths()
  544. for key, modpath in pairs(modpaths) do
  545. pkgmgr.get_mods(modpath, key, global_mods)
  546. end
  547. for i=1,#global_mods,1 do
  548. global_mods[i].type = "mod"
  549. global_mods[i].loc = "global"
  550. global_mods[i].enabled = false
  551. retval[#retval + 1] = global_mods[i]
  552. end
  553. --read game mods
  554. local gamespec = pkgmgr.find_by_gameid(data.gameid)
  555. pkgmgr.get_game_mods(gamespec, game_mods)
  556. if #game_mods > 0 then
  557. -- Add title
  558. retval[#retval + 1] = {
  559. type = "game",
  560. is_game_content = true,
  561. name = fgettext("$1 mods", gamespec.title),
  562. path = gamespec.path
  563. }
  564. end
  565. for i=1,#game_mods,1 do
  566. game_mods[i].type = "mod"
  567. game_mods[i].loc = "game"
  568. game_mods[i].is_game_content = true
  569. retval[#retval + 1] = game_mods[i]
  570. end
  571. if data.worldpath == nil then
  572. return retval
  573. end
  574. --read world mod configuration
  575. local filename = data.worldpath ..
  576. DIR_DELIM .. "world.mt"
  577. local worldfile = Settings(filename)
  578. for key, value in pairs(worldfile:to_table()) do
  579. if key:sub(1, 9) == "load_mod_" then
  580. key = key:sub(10)
  581. local mod_found = false
  582. local fallback_found = false
  583. local fallback_mod = nil
  584. for i=1, #retval do
  585. if retval[i].name == key and
  586. not retval[i].is_modpack then
  587. if core.is_yes(value) or retval[i].virtual_path == value then
  588. retval[i].enabled = true
  589. mod_found = true
  590. break
  591. elseif fallback_found then
  592. -- Only allow fallback if only one mod matches
  593. fallback_mod = nil
  594. else
  595. fallback_found = true
  596. fallback_mod = retval[i]
  597. end
  598. end
  599. end
  600. if not mod_found then
  601. if fallback_mod and value:find("/") then
  602. fallback_mod.enabled = true
  603. else
  604. core.log("info", "Mod: " .. key .. " " .. dump(value) .. " but not found")
  605. end
  606. end
  607. end
  608. end
  609. return retval
  610. end
  611. function pkgmgr.compare_package(a, b)
  612. return a and b and a.name == b.name and a.path == b.path
  613. end
  614. --------------------------------------------------------------------------------
  615. function pkgmgr.comparemod(elem1,elem2)
  616. if elem1 == nil or elem2 == nil then
  617. return false
  618. end
  619. if elem1.name ~= elem2.name then
  620. return false
  621. end
  622. if elem1.is_modpack ~= elem2.is_modpack then
  623. return false
  624. end
  625. if elem1.type ~= elem2.type then
  626. return false
  627. end
  628. if elem1.modpack ~= elem2.modpack then
  629. return false
  630. end
  631. if elem1.path ~= elem2.path then
  632. return false
  633. end
  634. return true
  635. end
  636. --------------------------------------------------------------------------------
  637. function pkgmgr.reload_global_mods()
  638. local function is_equal(element,uid) --uid match
  639. if element.name == uid then
  640. return true
  641. end
  642. end
  643. pkgmgr.global_mods = filterlist.create(pkgmgr.preparemodlist,
  644. pkgmgr.comparemod, is_equal, nil, {})
  645. pkgmgr.global_mods:add_sort_mechanism("alphabetic", sort_mod_list)
  646. pkgmgr.global_mods:set_sortmode("alphabetic")
  647. end
  648. --------------------------------------------------------------------------------
  649. function pkgmgr.find_by_gameid(gameid)
  650. for i, game in ipairs(pkgmgr.games) do
  651. if game.id == gameid then
  652. return game, i
  653. end
  654. end
  655. return nil, nil
  656. end
  657. --------------------------------------------------------------------------------
  658. function pkgmgr.get_game_mods(gamespec, retval)
  659. if gamespec ~= nil and
  660. gamespec.gamemods_path ~= nil and
  661. gamespec.gamemods_path ~= "" then
  662. pkgmgr.get_mods(gamespec.gamemods_path, ("games/%s/mods"):format(gamespec.id), retval)
  663. end
  664. end
  665. --------------------------------------------------------------------------------
  666. function pkgmgr.reload_games()
  667. pkgmgr.games = core.get_games()
  668. table.sort(pkgmgr.games, function(a, b)
  669. return a.title:lower() < b.title:lower()
  670. end)
  671. pkgmgr.update_translations(pkgmgr.games)
  672. end
  673. --------------------------------------------------------------------------------
  674. function pkgmgr.reload_by_type(type)
  675. if type == "game" then
  676. pkgmgr.reload_games()
  677. elseif type == "txp" then
  678. pkgmgr.reload_texture_packs()
  679. elseif type == "mod" or type == "modpack" then
  680. pkgmgr.reload_global_mods()
  681. else
  682. error("Unknown package type: " .. type)
  683. end
  684. end
  685. --------------------------------------------------------------------------------
  686. function pkgmgr.load_all()
  687. if not pkgmgr.global_mods then
  688. pkgmgr.reload_global_mods()
  689. end
  690. if not pkgmgr.games then
  691. pkgmgr.reload_games()
  692. end
  693. if not pkgmgr.texture_packs then
  694. pkgmgr.reload_texture_packs()
  695. end
  696. end
  697. --------------------------------------------------------------------------------
  698. function pkgmgr.update_translations(list)
  699. for _, item in ipairs(list) do
  700. local info = core.get_content_info(item.path)
  701. assert(info.path)
  702. assert(info.textdomain)
  703. assert(not item.is_translated)
  704. item.is_translated = true
  705. if info.title and info.title ~= "" then
  706. item.title = core.get_content_translation(info.path, info.textdomain,
  707. core.translate(info.textdomain, info.title))
  708. end
  709. if info.description and info.description ~= "" then
  710. item.description = core.get_content_translation(info.path, info.textdomain,
  711. core.translate(info.textdomain, info.description))
  712. end
  713. end
  714. end
  715. --------------------------------------------------------------------------------
  716. -- Returns the ContentDB ID for an installed piece of content.
  717. function pkgmgr.get_contentdb_id(content)
  718. -- core.get_games() will return "" instead of nil if there is no "author" field.
  719. if content.author and content.author ~= "" and content.release > 0 then
  720. if content.type == "game" then
  721. return content.author:lower() .. "/" .. content.id
  722. end
  723. return content.author:lower() .. "/" .. content.name
  724. end
  725. -- Until version 5.8.0, Minetest Game was bundled with the engine.
  726. -- Unfortunately, the bundled MTG was not versioned (missing "release"
  727. -- field in game.conf).
  728. -- Therefore, we consider any installation of MTG that is not versioned,
  729. -- has not been cloned from Git, and is not system-wide to be updatable.
  730. if content.type == "game" and content.id == "minetest" and content.release == 0 and
  731. not core.is_dir(content.path .. "/.git") and core.may_modify_path(content.path) then
  732. return "minetest/minetest"
  733. end
  734. return nil
  735. end
  736. --------------------------------------------------------------------------------
  737. -- read initial data
  738. --------------------------------------------------------------------------------
  739. pkgmgr.reload_games()