pkgmgr.lua 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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 name = item
  70. local path = txtpath .. DIR_DELIM .. item .. DIR_DELIM
  71. if path == current_texture_path then
  72. name = fgettext("$1 (Enabled)", name)
  73. end
  74. local conf = Settings(path .. "texture_pack.conf")
  75. retval[#retval + 1] = {
  76. name = item,
  77. author = conf:get("author"),
  78. release = tonumber(conf:get("release")) or 0,
  79. list_name = name,
  80. type = "txp",
  81. path = path,
  82. enabled = path == current_texture_path,
  83. }
  84. end
  85. end
  86. end
  87. function get_mods(path,retval,modpack)
  88. local mods = core.get_dir_list(path, true)
  89. for _, name in ipairs(mods) do
  90. if name:sub(1, 1) ~= "." then
  91. local prefix = path .. DIR_DELIM .. name
  92. local toadd = {
  93. dir_name = name,
  94. parent_dir = path,
  95. }
  96. retval[#retval + 1] = toadd
  97. -- Get config file
  98. local mod_conf
  99. local modpack_conf = io.open(prefix .. DIR_DELIM .. "modpack.conf")
  100. if modpack_conf then
  101. toadd.is_modpack = true
  102. modpack_conf:close()
  103. mod_conf = Settings(prefix .. DIR_DELIM .. "modpack.conf"):to_table()
  104. if mod_conf.name then
  105. name = mod_conf.name
  106. toadd.is_name_explicit = true
  107. end
  108. else
  109. mod_conf = Settings(prefix .. DIR_DELIM .. "mod.conf"):to_table()
  110. if mod_conf.name then
  111. name = mod_conf.name
  112. toadd.is_name_explicit = true
  113. end
  114. end
  115. -- Read from config
  116. toadd.name = name
  117. toadd.author = mod_conf.author
  118. toadd.release = tonumber(mod_conf.release) or 0
  119. toadd.path = prefix
  120. toadd.type = "mod"
  121. -- Check modpack.txt
  122. -- Note: modpack.conf is already checked above
  123. local modpackfile = io.open(prefix .. DIR_DELIM .. "modpack.txt")
  124. if modpackfile then
  125. modpackfile:close()
  126. toadd.is_modpack = true
  127. end
  128. -- Deal with modpack contents
  129. if modpack and modpack ~= "" then
  130. toadd.modpack = modpack
  131. elseif toadd.is_modpack then
  132. toadd.type = "modpack"
  133. toadd.is_modpack = true
  134. get_mods(prefix, retval, name)
  135. end
  136. end
  137. end
  138. end
  139. --modmanager implementation
  140. pkgmgr = {}
  141. function pkgmgr.get_texture_packs()
  142. local txtpath = core.get_texturepath()
  143. local txtpath_system = core.get_texturepath_share()
  144. local retval = {}
  145. load_texture_packs(txtpath, retval)
  146. -- on portable versions these two paths coincide. It avoids loading the path twice
  147. if txtpath ~= txtpath_system then
  148. load_texture_packs(txtpath_system, retval)
  149. end
  150. table.sort(retval, function(a, b)
  151. return a.name > b.name
  152. end)
  153. return retval
  154. end
  155. --------------------------------------------------------------------------------
  156. function pkgmgr.extract(modfile)
  157. if modfile.type == "zip" then
  158. local tempfolder = os.tempfolder()
  159. if tempfolder ~= nil and
  160. tempfolder ~= "" then
  161. core.create_dir(tempfolder)
  162. if core.extract_zip(modfile.name,tempfolder) then
  163. return tempfolder
  164. end
  165. end
  166. end
  167. return nil
  168. end
  169. function pkgmgr.get_folder_type(path)
  170. local testfile = io.open(path .. DIR_DELIM .. "init.lua","r")
  171. if testfile ~= nil then
  172. testfile:close()
  173. return { type = "mod", path = path }
  174. end
  175. testfile = io.open(path .. DIR_DELIM .. "modpack.conf","r")
  176. if testfile ~= nil then
  177. testfile:close()
  178. return { type = "modpack", path = path }
  179. end
  180. testfile = io.open(path .. DIR_DELIM .. "modpack.txt","r")
  181. if testfile ~= nil then
  182. testfile:close()
  183. return { type = "modpack", path = path }
  184. end
  185. testfile = io.open(path .. DIR_DELIM .. "game.conf","r")
  186. if testfile ~= nil then
  187. testfile:close()
  188. return { type = "game", path = path }
  189. end
  190. testfile = io.open(path .. DIR_DELIM .. "texture_pack.conf","r")
  191. if testfile ~= nil then
  192. testfile:close()
  193. return { type = "txp", path = path }
  194. end
  195. return nil
  196. end
  197. -------------------------------------------------------------------------------
  198. function pkgmgr.get_base_folder(temppath)
  199. if temppath == nil then
  200. return { type = "invalid", path = "" }
  201. end
  202. local ret = pkgmgr.get_folder_type(temppath)
  203. if ret then
  204. return ret
  205. end
  206. local subdirs = core.get_dir_list(temppath, true)
  207. if #subdirs == 1 then
  208. ret = pkgmgr.get_folder_type(temppath .. DIR_DELIM .. subdirs[1])
  209. if ret then
  210. return ret
  211. else
  212. return { type = "invalid", path = temppath .. DIR_DELIM .. subdirs[1] }
  213. end
  214. end
  215. return nil
  216. end
  217. --------------------------------------------------------------------------------
  218. function pkgmgr.isValidModname(modpath)
  219. if modpath:find("-") ~= nil then
  220. return false
  221. end
  222. return true
  223. end
  224. --------------------------------------------------------------------------------
  225. function pkgmgr.parse_register_line(line)
  226. local pos1 = line:find("\"")
  227. local pos2 = nil
  228. if pos1 ~= nil then
  229. pos2 = line:find("\"",pos1+1)
  230. end
  231. if pos1 ~= nil and pos2 ~= nil then
  232. local item = line:sub(pos1+1,pos2-1)
  233. if item ~= nil and
  234. item ~= "" then
  235. local pos3 = item:find(":")
  236. if pos3 ~= nil then
  237. local retval = item:sub(1,pos3-1)
  238. if retval ~= nil and
  239. retval ~= "" then
  240. return retval
  241. end
  242. end
  243. end
  244. end
  245. return nil
  246. end
  247. --------------------------------------------------------------------------------
  248. function pkgmgr.parse_dofile_line(modpath,line)
  249. local pos1 = line:find("\"")
  250. local pos2 = nil
  251. if pos1 ~= nil then
  252. pos2 = line:find("\"",pos1+1)
  253. end
  254. if pos1 ~= nil and pos2 ~= nil then
  255. local filename = line:sub(pos1+1,pos2-1)
  256. if filename ~= nil and
  257. filename ~= "" and
  258. filename:find(".lua") then
  259. return pkgmgr.identify_modname(modpath,filename)
  260. end
  261. end
  262. return nil
  263. end
  264. --------------------------------------------------------------------------------
  265. function pkgmgr.identify_modname(modpath,filename)
  266. local testfile = io.open(modpath .. DIR_DELIM .. filename,"r")
  267. if testfile ~= nil then
  268. local line = testfile:read()
  269. while line~= nil do
  270. local modname = nil
  271. if line:find("minetest.register_tool") then
  272. modname = pkgmgr.parse_register_line(line)
  273. end
  274. if line:find("minetest.register_craftitem") then
  275. modname = pkgmgr.parse_register_line(line)
  276. end
  277. if line:find("minetest.register_node") then
  278. modname = pkgmgr.parse_register_line(line)
  279. end
  280. if line:find("dofile") then
  281. modname = pkgmgr.parse_dofile_line(modpath,line)
  282. end
  283. if modname ~= nil then
  284. testfile:close()
  285. return modname
  286. end
  287. line = testfile:read()
  288. end
  289. testfile:close()
  290. end
  291. return nil
  292. end
  293. --------------------------------------------------------------------------------
  294. function pkgmgr.render_packagelist(render_list)
  295. if not render_list then
  296. if not pkgmgr.global_mods then
  297. pkgmgr.refresh_globals()
  298. end
  299. render_list = pkgmgr.global_mods
  300. end
  301. local list = render_list:get_list()
  302. local retval = {}
  303. for i, v in ipairs(list) do
  304. local color = ""
  305. if v.is_modpack then
  306. local rawlist = render_list:get_raw_list()
  307. color = mt_color_dark_green
  308. for j = 1, #rawlist, 1 do
  309. if rawlist[j].modpack == list[i].name and
  310. not rawlist[j].enabled then
  311. -- Modpack not entirely enabled so showing as grey
  312. color = mt_color_grey
  313. break
  314. end
  315. end
  316. elseif v.is_game_content or v.type == "game" then
  317. color = mt_color_blue
  318. elseif v.enabled or v.type == "txp" then
  319. color = mt_color_green
  320. end
  321. retval[#retval + 1] = color
  322. if v.modpack ~= nil or v.loc == "game" then
  323. retval[#retval + 1] = "1"
  324. else
  325. retval[#retval + 1] = "0"
  326. end
  327. retval[#retval + 1] = core.formspec_escape(v.list_name or v.name)
  328. end
  329. return table.concat(retval, ",")
  330. end
  331. --------------------------------------------------------------------------------
  332. function pkgmgr.get_dependencies(path)
  333. if path == nil then
  334. return {}, {}
  335. end
  336. local info = core.get_content_info(path)
  337. return info.depends or {}, info.optional_depends or {}
  338. end
  339. ----------- tests whether all of the mods in the modpack are enabled -----------
  340. function pkgmgr.is_modpack_entirely_enabled(data, name)
  341. local rawlist = data.list:get_raw_list()
  342. for j = 1, #rawlist do
  343. if rawlist[j].modpack == name and not rawlist[j].enabled then
  344. return false
  345. end
  346. end
  347. return true
  348. end
  349. ---------- toggles or en/disables a mod or modpack and its dependencies --------
  350. local function toggle_mod_or_modpack(list, toggled_mods, enabled_mods, toset, mod)
  351. if not mod.is_modpack then
  352. -- Toggle or en/disable the mod
  353. if toset == nil then
  354. toset = not mod.enabled
  355. end
  356. if mod.enabled ~= toset then
  357. mod.enabled = toset
  358. toggled_mods[#toggled_mods+1] = mod.name
  359. end
  360. if toset then
  361. -- Mark this mod for recursive dependency traversal
  362. enabled_mods[mod.name] = true
  363. end
  364. else
  365. -- Toggle or en/disable every mod in the modpack,
  366. -- interleaved unsupported
  367. for i = 1, #list do
  368. if list[i].modpack == mod.name then
  369. toggle_mod_or_modpack(list, toggled_mods, enabled_mods, toset, list[i])
  370. end
  371. end
  372. end
  373. end
  374. function pkgmgr.enable_mod(this, toset)
  375. local list = this.data.list:get_list()
  376. local mod = list[this.data.selected_mod]
  377. -- Game mods can't be enabled or disabled
  378. if mod.is_game_content then
  379. return
  380. end
  381. local toggled_mods = {}
  382. local enabled_mods = {}
  383. toggle_mod_or_modpack(list, toggled_mods, enabled_mods, toset, mod)
  384. if not toset then
  385. -- Mod(s) were disabled, so no dependencies need to be enabled
  386. table.sort(toggled_mods)
  387. core.log("info", "Following mods were disabled: " ..
  388. table.concat(toggled_mods, ", "))
  389. return
  390. end
  391. -- Enable mods' depends after activation
  392. -- Make a list of mod ids indexed by their names
  393. local mod_ids = {}
  394. for id, mod2 in pairs(list) do
  395. if mod2.type == "mod" and not mod2.is_modpack then
  396. mod_ids[mod2.name] = id
  397. end
  398. end
  399. -- to_enable is used as a DFS stack with sp as stack pointer
  400. local to_enable = {}
  401. local sp = 0
  402. for name in pairs(enabled_mods) do
  403. local depends = pkgmgr.get_dependencies(list[mod_ids[name]].path)
  404. for i = 1, #depends do
  405. local dependency_name = depends[i]
  406. if not enabled_mods[dependency_name] then
  407. sp = sp+1
  408. to_enable[sp] = dependency_name
  409. end
  410. end
  411. end
  412. -- If sp is 0, every dependency is already activated
  413. while sp > 0 do
  414. local name = to_enable[sp]
  415. sp = sp-1
  416. if not enabled_mods[name] then
  417. enabled_mods[name] = true
  418. local mod_to_enable = list[mod_ids[name]]
  419. if not mod_to_enable then
  420. core.log("warning", "Mod dependency \"" .. name ..
  421. "\" not found!")
  422. else
  423. if mod_to_enable.enabled == false then
  424. mod_to_enable.enabled = true
  425. toggled_mods[#toggled_mods+1] = mod_to_enable.name
  426. end
  427. -- Push the dependencies of the dependency onto the stack
  428. local depends = pkgmgr.get_dependencies(mod_to_enable.path)
  429. for i = 1, #depends do
  430. if not enabled_mods[name] then
  431. sp = sp+1
  432. to_enable[sp] = depends[i]
  433. end
  434. end
  435. end
  436. end
  437. end
  438. -- Log the list of enabled mods
  439. table.sort(toggled_mods)
  440. core.log("info", "Following mods were enabled: " ..
  441. table.concat(toggled_mods, ", "))
  442. end
  443. --------------------------------------------------------------------------------
  444. function pkgmgr.get_worldconfig(worldpath)
  445. local filename = worldpath ..
  446. DIR_DELIM .. "world.mt"
  447. local worldfile = Settings(filename)
  448. local worldconfig = {}
  449. worldconfig.global_mods = {}
  450. worldconfig.game_mods = {}
  451. for key,value in pairs(worldfile:to_table()) do
  452. if key == "gameid" then
  453. worldconfig.id = value
  454. elseif key:sub(0, 9) == "load_mod_" then
  455. -- Compatibility: Check against "nil" which was erroneously used
  456. -- as value for fresh configured worlds
  457. worldconfig.global_mods[key] = value ~= "false" and value ~= "nil"
  458. and value
  459. else
  460. worldconfig[key] = value
  461. end
  462. end
  463. --read gamemods
  464. local gamespec = pkgmgr.find_by_gameid(worldconfig.id)
  465. pkgmgr.get_game_mods(gamespec, worldconfig.game_mods)
  466. return worldconfig
  467. end
  468. --------------------------------------------------------------------------------
  469. function pkgmgr.install_dir(type, path, basename, targetpath)
  470. local basefolder = pkgmgr.get_base_folder(path)
  471. -- There's no good way to detect a texture pack, so let's just assume
  472. -- it's correct for now.
  473. if type == "txp" then
  474. if basefolder and basefolder.type ~= "invalid" and basefolder.type ~= "txp" then
  475. return nil, fgettext("Unable to install a $1 as a texture pack", basefolder.type)
  476. end
  477. local from = basefolder and basefolder.path or path
  478. if targetpath then
  479. core.delete_dir(targetpath)
  480. core.create_dir(targetpath)
  481. else
  482. targetpath = core.get_texturepath() .. DIR_DELIM .. basename
  483. end
  484. if not core.copy_dir(from, targetpath) then
  485. return nil,
  486. fgettext("Failed to install $1 to $2", basename, targetpath)
  487. end
  488. return targetpath, nil
  489. elseif not basefolder then
  490. return nil, fgettext("Unable to find a valid mod or modpack")
  491. end
  492. --
  493. -- Get destination
  494. --
  495. if basefolder.type == "modpack" then
  496. if type ~= "mod" then
  497. return nil, fgettext("Unable to install a modpack as a $1", type)
  498. end
  499. -- Get destination name for modpack
  500. if targetpath then
  501. core.delete_dir(targetpath)
  502. core.create_dir(targetpath)
  503. else
  504. local clean_path = nil
  505. if basename ~= nil then
  506. clean_path = basename
  507. end
  508. if not clean_path then
  509. clean_path = get_last_folder(cleanup_path(basefolder.path))
  510. end
  511. if clean_path then
  512. targetpath = core.get_modpath() .. DIR_DELIM .. clean_path
  513. else
  514. return nil,
  515. fgettext("Install Mod: Unable to find suitable folder name for modpack $1",
  516. path)
  517. end
  518. end
  519. elseif basefolder.type == "mod" then
  520. if type ~= "mod" then
  521. return nil, fgettext("Unable to install a mod as a $1", type)
  522. end
  523. if targetpath then
  524. core.delete_dir(targetpath)
  525. core.create_dir(targetpath)
  526. else
  527. local targetfolder = basename
  528. if targetfolder == nil then
  529. targetfolder = pkgmgr.identify_modname(basefolder.path, "init.lua")
  530. end
  531. -- If heuristic failed try to use current foldername
  532. if targetfolder == nil then
  533. targetfolder = get_last_folder(basefolder.path)
  534. end
  535. if targetfolder ~= nil and pkgmgr.isValidModname(targetfolder) then
  536. targetpath = core.get_modpath() .. DIR_DELIM .. targetfolder
  537. else
  538. return nil, fgettext("Install Mod: Unable to find real mod name for: $1", path)
  539. end
  540. end
  541. elseif basefolder.type == "game" then
  542. if type ~= "game" then
  543. return nil, fgettext("Unable to install a game as a $1", type)
  544. end
  545. if targetpath then
  546. core.delete_dir(targetpath)
  547. core.create_dir(targetpath)
  548. else
  549. targetpath = core.get_gamepath() .. DIR_DELIM .. basename
  550. end
  551. end
  552. -- Copy it
  553. if not core.copy_dir(basefolder.path, targetpath) then
  554. return nil,
  555. fgettext("Failed to install $1 to $2", basename, targetpath)
  556. end
  557. if basefolder.type == "game" then
  558. pkgmgr.update_gamelist()
  559. else
  560. pkgmgr.refresh_globals()
  561. end
  562. return targetpath, nil
  563. end
  564. --------------------------------------------------------------------------------
  565. function pkgmgr.install(type, modfilename, basename, dest)
  566. local archive_info = pkgmgr.identify_filetype(modfilename)
  567. local path = pkgmgr.extract(archive_info)
  568. if path == nil then
  569. return nil,
  570. fgettext("Install: file: \"$1\"", archive_info.name) .. "\n" ..
  571. fgettext("Install: Unsupported file type \"$1\" or broken archive",
  572. archive_info.type)
  573. end
  574. local targetpath, msg = pkgmgr.install_dir(type, path, basename, dest)
  575. core.delete_dir(path)
  576. return targetpath, msg
  577. end
  578. --------------------------------------------------------------------------------
  579. function pkgmgr.preparemodlist(data)
  580. local retval = {}
  581. local global_mods = {}
  582. local game_mods = {}
  583. --read global mods
  584. local modpath = core.get_modpath()
  585. if modpath ~= nil and
  586. modpath ~= "" then
  587. get_mods(modpath,global_mods)
  588. end
  589. for i=1,#global_mods,1 do
  590. global_mods[i].type = "mod"
  591. global_mods[i].loc = "global"
  592. retval[#retval + 1] = global_mods[i]
  593. end
  594. --read game mods
  595. local gamespec = pkgmgr.find_by_gameid(data.gameid)
  596. pkgmgr.get_game_mods(gamespec, game_mods)
  597. if #game_mods > 0 then
  598. -- Add title
  599. retval[#retval + 1] = {
  600. type = "game",
  601. is_game_content = true,
  602. name = fgettext("$1 mods", gamespec.name),
  603. path = gamespec.path
  604. }
  605. end
  606. for i=1,#game_mods,1 do
  607. game_mods[i].type = "mod"
  608. game_mods[i].loc = "game"
  609. game_mods[i].is_game_content = true
  610. retval[#retval + 1] = game_mods[i]
  611. end
  612. if data.worldpath == nil then
  613. return retval
  614. end
  615. --read world mod configuration
  616. local filename = data.worldpath ..
  617. DIR_DELIM .. "world.mt"
  618. local worldfile = Settings(filename)
  619. for key,value in pairs(worldfile:to_table()) do
  620. if key:sub(1, 9) == "load_mod_" then
  621. key = key:sub(10)
  622. local element = nil
  623. for i=1,#retval,1 do
  624. if retval[i].name == key and
  625. not retval[i].is_modpack then
  626. element = retval[i]
  627. break
  628. end
  629. end
  630. if element ~= nil then
  631. element.enabled = value ~= "false" and value ~= "nil" and value
  632. else
  633. core.log("info", "Mod: " .. key .. " " .. dump(value) .. " but not found")
  634. end
  635. end
  636. end
  637. return retval
  638. end
  639. function pkgmgr.compare_package(a, b)
  640. return a and b and a.name == b.name and a.path == b.path
  641. end
  642. --------------------------------------------------------------------------------
  643. function pkgmgr.comparemod(elem1,elem2)
  644. if elem1 == nil or elem2 == nil then
  645. return false
  646. end
  647. if elem1.name ~= elem2.name then
  648. return false
  649. end
  650. if elem1.is_modpack ~= elem2.is_modpack then
  651. return false
  652. end
  653. if elem1.type ~= elem2.type then
  654. return false
  655. end
  656. if elem1.modpack ~= elem2.modpack then
  657. return false
  658. end
  659. if elem1.path ~= elem2.path then
  660. return false
  661. end
  662. return true
  663. end
  664. --------------------------------------------------------------------------------
  665. function pkgmgr.mod_exists(basename)
  666. if pkgmgr.global_mods == nil then
  667. pkgmgr.refresh_globals()
  668. end
  669. if pkgmgr.global_mods:raw_index_by_uid(basename) > 0 then
  670. return true
  671. end
  672. return false
  673. end
  674. --------------------------------------------------------------------------------
  675. function pkgmgr.get_global_mod(idx)
  676. if pkgmgr.global_mods == nil then
  677. return nil
  678. end
  679. if idx == nil or idx < 1 or
  680. idx > pkgmgr.global_mods:size() then
  681. return nil
  682. end
  683. return pkgmgr.global_mods:get_list()[idx]
  684. end
  685. --------------------------------------------------------------------------------
  686. function pkgmgr.refresh_globals()
  687. local function is_equal(element,uid) --uid match
  688. if element.name == uid then
  689. return true
  690. end
  691. end
  692. pkgmgr.global_mods = filterlist.create(pkgmgr.preparemodlist,
  693. pkgmgr.comparemod, is_equal, nil, {})
  694. pkgmgr.global_mods:add_sort_mechanism("alphabetic", sort_mod_list)
  695. pkgmgr.global_mods:set_sortmode("alphabetic")
  696. end
  697. --------------------------------------------------------------------------------
  698. function pkgmgr.identify_filetype(name)
  699. if name:sub(-3):lower() == "zip" then
  700. return {
  701. name = name,
  702. type = "zip"
  703. }
  704. end
  705. if name:sub(-6):lower() == "tar.gz" or
  706. name:sub(-3):lower() == "tgz"then
  707. return {
  708. name = name,
  709. type = "tgz"
  710. }
  711. end
  712. if name:sub(-6):lower() == "tar.bz2" then
  713. return {
  714. name = name,
  715. type = "tbz"
  716. }
  717. end
  718. if name:sub(-2):lower() == "7z" then
  719. return {
  720. name = name,
  721. type = "7z"
  722. }
  723. end
  724. return {
  725. name = name,
  726. type = "ukn"
  727. }
  728. end
  729. --------------------------------------------------------------------------------
  730. function pkgmgr.find_by_gameid(gameid)
  731. for i=1,#pkgmgr.games,1 do
  732. if pkgmgr.games[i].id == gameid then
  733. return pkgmgr.games[i], i
  734. end
  735. end
  736. return nil, nil
  737. end
  738. --------------------------------------------------------------------------------
  739. function pkgmgr.get_game_mods(gamespec, retval)
  740. if gamespec ~= nil and
  741. gamespec.gamemods_path ~= nil and
  742. gamespec.gamemods_path ~= "" then
  743. get_mods(gamespec.gamemods_path, retval)
  744. end
  745. end
  746. --------------------------------------------------------------------------------
  747. function pkgmgr.get_game_modlist(gamespec)
  748. local retval = ""
  749. local game_mods = {}
  750. pkgmgr.get_game_mods(gamespec, game_mods)
  751. for i=1,#game_mods,1 do
  752. if retval ~= "" then
  753. retval = retval..","
  754. end
  755. retval = retval .. game_mods[i].name
  756. end
  757. return retval
  758. end
  759. --------------------------------------------------------------------------------
  760. function pkgmgr.get_game(index)
  761. if index > 0 and index <= #pkgmgr.games then
  762. return pkgmgr.games[index]
  763. end
  764. return nil
  765. end
  766. --------------------------------------------------------------------------------
  767. function pkgmgr.update_gamelist()
  768. pkgmgr.games = core.get_games()
  769. end
  770. --------------------------------------------------------------------------------
  771. function pkgmgr.gamelist()
  772. local retval = ""
  773. if #pkgmgr.games > 0 then
  774. retval = retval .. core.formspec_escape(pkgmgr.games[1].name)
  775. for i=2,#pkgmgr.games,1 do
  776. retval = retval .. "," .. core.formspec_escape(pkgmgr.games[i].name)
  777. end
  778. end
  779. return retval
  780. end
  781. --------------------------------------------------------------------------------
  782. -- read initial data
  783. --------------------------------------------------------------------------------
  784. pkgmgr.update_gamelist()