pkgmgr.lua 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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. function get_mods(path, virtual_path, retval, modpack)
  89. local mods = core.get_dir_list(path, true)
  90. for _, name in ipairs(mods) do
  91. if name:sub(1, 1) ~= "." then
  92. local mod_path = path .. DIR_DELIM .. name
  93. local mod_virtual_path = virtual_path .. "/" .. name
  94. local toadd = {
  95. dir_name = name,
  96. parent_dir = path,
  97. }
  98. retval[#retval + 1] = toadd
  99. -- Get config file
  100. local mod_conf
  101. local modpack_conf = io.open(mod_path .. DIR_DELIM .. "modpack.conf")
  102. if modpack_conf then
  103. toadd.is_modpack = true
  104. modpack_conf:close()
  105. mod_conf = Settings(mod_path .. DIR_DELIM .. "modpack.conf"):to_table()
  106. if mod_conf.name then
  107. name = mod_conf.name
  108. toadd.is_name_explicit = true
  109. end
  110. else
  111. mod_conf = Settings(mod_path .. DIR_DELIM .. "mod.conf"):to_table()
  112. if mod_conf.name then
  113. name = mod_conf.name
  114. toadd.is_name_explicit = true
  115. end
  116. end
  117. -- Read from config
  118. toadd.name = name
  119. toadd.title = mod_conf.title
  120. toadd.author = mod_conf.author
  121. toadd.release = tonumber(mod_conf.release) or 0
  122. toadd.path = mod_path
  123. toadd.virtual_path = mod_virtual_path
  124. toadd.type = "mod"
  125. -- Check modpack.txt
  126. -- Note: modpack.conf is already checked above
  127. local modpackfile = io.open(mod_path .. DIR_DELIM .. "modpack.txt")
  128. if modpackfile then
  129. modpackfile:close()
  130. toadd.is_modpack = true
  131. end
  132. -- Deal with modpack contents
  133. if modpack and modpack ~= "" then
  134. toadd.modpack = modpack
  135. elseif toadd.is_modpack then
  136. toadd.type = "modpack"
  137. toadd.is_modpack = true
  138. get_mods(mod_path, mod_virtual_path, retval, name)
  139. end
  140. end
  141. end
  142. end
  143. --modmanager implementation
  144. pkgmgr = {}
  145. function pkgmgr.get_texture_packs()
  146. local txtpath = core.get_texturepath()
  147. local txtpath_system = core.get_texturepath_share()
  148. local retval = {}
  149. load_texture_packs(txtpath, retval)
  150. -- on portable versions these two paths coincide. It avoids loading the path twice
  151. if txtpath ~= txtpath_system then
  152. load_texture_packs(txtpath_system, retval)
  153. end
  154. table.sort(retval, function(a, b)
  155. return a.name > b.name
  156. end)
  157. return retval
  158. end
  159. --------------------------------------------------------------------------------
  160. function pkgmgr.get_folder_type(path)
  161. local testfile = io.open(path .. DIR_DELIM .. "init.lua","r")
  162. if testfile ~= nil then
  163. testfile:close()
  164. return { type = "mod", path = path }
  165. end
  166. testfile = io.open(path .. DIR_DELIM .. "modpack.conf","r")
  167. if testfile ~= nil then
  168. testfile:close()
  169. return { type = "modpack", path = path }
  170. end
  171. testfile = io.open(path .. DIR_DELIM .. "modpack.txt","r")
  172. if testfile ~= nil then
  173. testfile:close()
  174. return { type = "modpack", path = path }
  175. end
  176. testfile = io.open(path .. DIR_DELIM .. "game.conf","r")
  177. if testfile ~= nil then
  178. testfile:close()
  179. return { type = "game", path = path }
  180. end
  181. testfile = io.open(path .. DIR_DELIM .. "texture_pack.conf","r")
  182. if testfile ~= nil then
  183. testfile:close()
  184. return { type = "txp", path = path }
  185. end
  186. return nil
  187. end
  188. -------------------------------------------------------------------------------
  189. function pkgmgr.get_base_folder(temppath)
  190. if temppath == nil then
  191. return { type = "invalid", path = "" }
  192. end
  193. local ret = pkgmgr.get_folder_type(temppath)
  194. if ret then
  195. return ret
  196. end
  197. local subdirs = core.get_dir_list(temppath, true)
  198. if #subdirs == 1 then
  199. ret = pkgmgr.get_folder_type(temppath .. DIR_DELIM .. subdirs[1])
  200. if ret then
  201. return ret
  202. else
  203. return { type = "invalid", path = temppath .. DIR_DELIM .. subdirs[1] }
  204. end
  205. end
  206. return nil
  207. end
  208. --------------------------------------------------------------------------------
  209. function pkgmgr.isValidModname(modpath)
  210. if modpath:find("-") ~= nil then
  211. return false
  212. end
  213. return true
  214. end
  215. --------------------------------------------------------------------------------
  216. function pkgmgr.parse_register_line(line)
  217. local pos1 = line:find("\"")
  218. local pos2 = nil
  219. if pos1 ~= nil then
  220. pos2 = line:find("\"",pos1+1)
  221. end
  222. if pos1 ~= nil and pos2 ~= nil then
  223. local item = line:sub(pos1+1,pos2-1)
  224. if item ~= nil and
  225. item ~= "" then
  226. local pos3 = item:find(":")
  227. if pos3 ~= nil then
  228. local retval = item:sub(1,pos3-1)
  229. if retval ~= nil and
  230. retval ~= "" then
  231. return retval
  232. end
  233. end
  234. end
  235. end
  236. return nil
  237. end
  238. --------------------------------------------------------------------------------
  239. function pkgmgr.parse_dofile_line(modpath,line)
  240. local pos1 = line:find("\"")
  241. local pos2 = nil
  242. if pos1 ~= nil then
  243. pos2 = line:find("\"",pos1+1)
  244. end
  245. if pos1 ~= nil and pos2 ~= nil then
  246. local filename = line:sub(pos1+1,pos2-1)
  247. if filename ~= nil and
  248. filename ~= "" and
  249. filename:find(".lua") then
  250. return pkgmgr.identify_modname(modpath,filename)
  251. end
  252. end
  253. return nil
  254. end
  255. --------------------------------------------------------------------------------
  256. function pkgmgr.identify_modname(modpath,filename)
  257. local testfile = io.open(modpath .. DIR_DELIM .. filename,"r")
  258. if testfile ~= nil then
  259. local line = testfile:read()
  260. while line~= nil do
  261. local modname = nil
  262. if line:find("minetest.register_tool") then
  263. modname = pkgmgr.parse_register_line(line)
  264. end
  265. if line:find("minetest.register_craftitem") then
  266. modname = pkgmgr.parse_register_line(line)
  267. end
  268. if line:find("minetest.register_node") then
  269. modname = pkgmgr.parse_register_line(line)
  270. end
  271. if line:find("dofile") then
  272. modname = pkgmgr.parse_dofile_line(modpath,line)
  273. end
  274. if modname ~= nil then
  275. testfile:close()
  276. return modname
  277. end
  278. line = testfile:read()
  279. end
  280. testfile:close()
  281. end
  282. return nil
  283. end
  284. --------------------------------------------------------------------------------
  285. function pkgmgr.render_packagelist(render_list, use_technical_names)
  286. if not render_list then
  287. if not pkgmgr.global_mods then
  288. pkgmgr.refresh_globals()
  289. end
  290. render_list = pkgmgr.global_mods
  291. end
  292. local list = render_list:get_list()
  293. local retval = {}
  294. for i, v in ipairs(list) do
  295. local color = ""
  296. if v.is_modpack then
  297. local rawlist = render_list:get_raw_list()
  298. color = mt_color_dark_green
  299. for j = 1, #rawlist, 1 do
  300. if rawlist[j].modpack == list[i].name and
  301. not rawlist[j].enabled then
  302. -- Modpack not entirely enabled so showing as grey
  303. color = mt_color_grey
  304. break
  305. end
  306. end
  307. elseif v.is_game_content or v.type == "game" then
  308. color = mt_color_blue
  309. elseif v.enabled or v.type == "txp" then
  310. color = mt_color_green
  311. end
  312. retval[#retval + 1] = color
  313. if v.modpack ~= nil or v.loc == "game" then
  314. retval[#retval + 1] = "1"
  315. else
  316. retval[#retval + 1] = "0"
  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. else
  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. function pkgmgr.install_dir(type, path, basename, targetpath)
  479. local basefolder = pkgmgr.get_base_folder(path)
  480. -- There's no good way to detect a texture pack, so let's just assume
  481. -- it's correct for now.
  482. if type == "txp" then
  483. if basefolder and basefolder.type ~= "invalid" and basefolder.type ~= "txp" then
  484. return nil, fgettext("Unable to install a $1 as a texture pack", basefolder.type)
  485. end
  486. local from = basefolder and basefolder.path or path
  487. if targetpath then
  488. core.delete_dir(targetpath)
  489. else
  490. targetpath = core.get_texturepath() .. DIR_DELIM .. basename
  491. end
  492. if not core.copy_dir(from, targetpath, false) then
  493. return nil,
  494. fgettext("Failed to install $1 to $2", basename, targetpath)
  495. end
  496. return targetpath, nil
  497. elseif not basefolder then
  498. return nil, fgettext("Unable to find a valid mod or modpack")
  499. end
  500. --
  501. -- Get destination
  502. --
  503. if basefolder.type == "modpack" then
  504. if type ~= "mod" then
  505. return nil, fgettext("Unable to install a modpack as a $1", type)
  506. end
  507. -- Get destination name for modpack
  508. if targetpath then
  509. core.delete_dir(targetpath)
  510. else
  511. local clean_path = nil
  512. if basename ~= nil then
  513. clean_path = basename
  514. end
  515. if not clean_path then
  516. clean_path = get_last_folder(cleanup_path(basefolder.path))
  517. end
  518. if clean_path then
  519. targetpath = core.get_modpath() .. DIR_DELIM .. clean_path
  520. else
  521. return nil,
  522. fgettext("Install Mod: Unable to find suitable folder name for modpack $1",
  523. path)
  524. end
  525. end
  526. elseif basefolder.type == "mod" then
  527. if type ~= "mod" then
  528. return nil, fgettext("Unable to install a mod as a $1", type)
  529. end
  530. if targetpath then
  531. core.delete_dir(targetpath)
  532. else
  533. local targetfolder = basename
  534. if targetfolder == nil then
  535. targetfolder = pkgmgr.identify_modname(basefolder.path, "init.lua")
  536. end
  537. -- If heuristic failed try to use current foldername
  538. if targetfolder == nil then
  539. targetfolder = get_last_folder(basefolder.path)
  540. end
  541. if targetfolder ~= nil and pkgmgr.isValidModname(targetfolder) then
  542. targetpath = core.get_modpath() .. DIR_DELIM .. targetfolder
  543. else
  544. return nil, fgettext("Install Mod: Unable to find real mod name for: $1", path)
  545. end
  546. end
  547. elseif basefolder.type == "game" then
  548. if type ~= "game" then
  549. return nil, fgettext("Unable to install a game as a $1", type)
  550. end
  551. if targetpath then
  552. core.delete_dir(targetpath)
  553. else
  554. targetpath = core.get_gamepath() .. DIR_DELIM .. basename
  555. end
  556. else
  557. error("basefolder didn't return a recognised type, this shouldn't happen")
  558. end
  559. -- Copy it
  560. if not core.copy_dir(basefolder.path, targetpath, false) then
  561. return nil,
  562. fgettext("Failed to install $1 to $2", basename, targetpath)
  563. end
  564. if basefolder.type == "game" then
  565. pkgmgr.update_gamelist()
  566. else
  567. pkgmgr.refresh_globals()
  568. end
  569. return targetpath, nil
  570. end
  571. --------------------------------------------------------------------------------
  572. function pkgmgr.preparemodlist(data)
  573. local retval = {}
  574. local global_mods = {}
  575. local game_mods = {}
  576. --read global mods
  577. local modpaths = core.get_modpaths()
  578. for key, modpath in pairs(modpaths) do
  579. get_mods(modpath, key, global_mods)
  580. end
  581. for i=1,#global_mods,1 do
  582. global_mods[i].type = "mod"
  583. global_mods[i].loc = "global"
  584. global_mods[i].enabled = false
  585. retval[#retval + 1] = global_mods[i]
  586. end
  587. --read game mods
  588. local gamespec = pkgmgr.find_by_gameid(data.gameid)
  589. pkgmgr.get_game_mods(gamespec, game_mods)
  590. if #game_mods > 0 then
  591. -- Add title
  592. retval[#retval + 1] = {
  593. type = "game",
  594. is_game_content = true,
  595. name = fgettext("$1 mods", gamespec.title),
  596. path = gamespec.path
  597. }
  598. end
  599. for i=1,#game_mods,1 do
  600. game_mods[i].type = "mod"
  601. game_mods[i].loc = "game"
  602. game_mods[i].is_game_content = true
  603. retval[#retval + 1] = game_mods[i]
  604. end
  605. if data.worldpath == nil then
  606. return retval
  607. end
  608. --read world mod configuration
  609. local filename = data.worldpath ..
  610. DIR_DELIM .. "world.mt"
  611. local worldfile = Settings(filename)
  612. for key, value in pairs(worldfile:to_table()) do
  613. if key:sub(1, 9) == "load_mod_" then
  614. key = key:sub(10)
  615. local mod_found = false
  616. local fallback_found = false
  617. local fallback_mod = nil
  618. for i=1, #retval do
  619. if retval[i].name == key and
  620. not retval[i].is_modpack then
  621. if core.is_yes(value) or retval[i].virtual_path == value then
  622. retval[i].enabled = true
  623. mod_found = true
  624. break
  625. elseif fallback_found then
  626. -- Only allow fallback if only one mod matches
  627. fallback_mod = nil
  628. else
  629. fallback_found = true
  630. fallback_mod = retval[i]
  631. end
  632. end
  633. end
  634. if not mod_found then
  635. if fallback_mod and value:find("/") then
  636. fallback_mod.enabled = true
  637. else
  638. core.log("info", "Mod: " .. key .. " " .. dump(value) .. " but not found")
  639. end
  640. end
  641. end
  642. end
  643. return retval
  644. end
  645. function pkgmgr.compare_package(a, b)
  646. return a and b and a.name == b.name and a.path == b.path
  647. end
  648. --------------------------------------------------------------------------------
  649. function pkgmgr.comparemod(elem1,elem2)
  650. if elem1 == nil or elem2 == nil then
  651. return false
  652. end
  653. if elem1.name ~= elem2.name then
  654. return false
  655. end
  656. if elem1.is_modpack ~= elem2.is_modpack then
  657. return false
  658. end
  659. if elem1.type ~= elem2.type then
  660. return false
  661. end
  662. if elem1.modpack ~= elem2.modpack then
  663. return false
  664. end
  665. if elem1.path ~= elem2.path then
  666. return false
  667. end
  668. return true
  669. end
  670. --------------------------------------------------------------------------------
  671. function pkgmgr.mod_exists(basename)
  672. if pkgmgr.global_mods == nil then
  673. pkgmgr.refresh_globals()
  674. end
  675. if pkgmgr.global_mods:raw_index_by_uid(basename) > 0 then
  676. return true
  677. end
  678. return false
  679. end
  680. --------------------------------------------------------------------------------
  681. function pkgmgr.get_global_mod(idx)
  682. if pkgmgr.global_mods == nil then
  683. return nil
  684. end
  685. if idx == nil or idx < 1 or
  686. idx > pkgmgr.global_mods:size() then
  687. return nil
  688. end
  689. return pkgmgr.global_mods:get_list()[idx]
  690. end
  691. --------------------------------------------------------------------------------
  692. function pkgmgr.refresh_globals()
  693. local function is_equal(element,uid) --uid match
  694. if element.name == uid then
  695. return true
  696. end
  697. end
  698. pkgmgr.global_mods = filterlist.create(pkgmgr.preparemodlist,
  699. pkgmgr.comparemod, is_equal, nil, {})
  700. pkgmgr.global_mods:add_sort_mechanism("alphabetic", sort_mod_list)
  701. pkgmgr.global_mods:set_sortmode("alphabetic")
  702. end
  703. --------------------------------------------------------------------------------
  704. function pkgmgr.find_by_gameid(gameid)
  705. for i=1,#pkgmgr.games,1 do
  706. if pkgmgr.games[i].id == gameid then
  707. return pkgmgr.games[i], i
  708. end
  709. end
  710. return nil, nil
  711. end
  712. --------------------------------------------------------------------------------
  713. function pkgmgr.get_game_mods(gamespec, retval)
  714. if gamespec ~= nil and
  715. gamespec.gamemods_path ~= nil and
  716. gamespec.gamemods_path ~= "" then
  717. get_mods(gamespec.gamemods_path, ("games/%s/mods"):format(gamespec.id), retval)
  718. end
  719. end
  720. --------------------------------------------------------------------------------
  721. function pkgmgr.get_game_modlist(gamespec)
  722. local retval = ""
  723. local game_mods = {}
  724. pkgmgr.get_game_mods(gamespec, game_mods)
  725. for i=1,#game_mods,1 do
  726. if retval ~= "" then
  727. retval = retval..","
  728. end
  729. retval = retval .. game_mods[i].name
  730. end
  731. return retval
  732. end
  733. --------------------------------------------------------------------------------
  734. function pkgmgr.get_game(index)
  735. if index > 0 and index <= #pkgmgr.games then
  736. return pkgmgr.games[index]
  737. end
  738. return nil
  739. end
  740. --------------------------------------------------------------------------------
  741. function pkgmgr.update_gamelist()
  742. pkgmgr.games = core.get_games()
  743. end
  744. --------------------------------------------------------------------------------
  745. function pkgmgr.gamelist()
  746. local retval = ""
  747. if #pkgmgr.games > 0 then
  748. retval = retval .. core.formspec_escape(pkgmgr.games[1].title)
  749. for i=2,#pkgmgr.games,1 do
  750. retval = retval .. "," .. core.formspec_escape(pkgmgr.games[i].title)
  751. end
  752. end
  753. return retval
  754. end
  755. --------------------------------------------------------------------------------
  756. -- read initial data
  757. --------------------------------------------------------------------------------
  758. pkgmgr.update_gamelist()