pkgmgr.lua 23 KB

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