pkgmgr.lua 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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. function pkgmgr.enable_mod(this, toset)
  351. local list = this.data.list:get_list()
  352. local mod = list[this.data.selected_mod]
  353. -- Game mods can't be enabled or disabled
  354. if mod.is_game_content then
  355. return
  356. end
  357. local toggled_mods = {}
  358. local enabled_mods = {}
  359. if not mod.is_modpack then
  360. -- Toggle or en/disable the mod
  361. if toset == nil then
  362. toset = not mod.enabled
  363. end
  364. if mod.enabled ~= toset then
  365. mod.enabled = toset
  366. toggled_mods[#toggled_mods+1] = mod.name
  367. end
  368. if toset then
  369. -- Mark this mod for recursive dependency traversal
  370. enabled_mods[mod.name] = true
  371. end
  372. else
  373. -- Toggle or en/disable every mod in the modpack,
  374. -- interleaved unsupported
  375. for i = 1, #list do
  376. if list[i].modpack == mod.name then
  377. if toset == nil then
  378. toset = not list[i].enabled
  379. end
  380. if list[i].enabled ~= toset then
  381. list[i].enabled = toset
  382. toggled_mods[#toggled_mods+1] = list[i].name
  383. end
  384. if toset then
  385. enabled_mods[list[i].name] = true
  386. end
  387. end
  388. end
  389. end
  390. if not toset then
  391. -- Mod(s) were disabled, so no dependencies need to be enabled
  392. table.sort(toggled_mods)
  393. core.log("info", "Following mods were disabled: " ..
  394. table.concat(toggled_mods, ", "))
  395. return
  396. end
  397. -- Enable mods' depends after activation
  398. -- Make a list of mod ids indexed by their names
  399. local mod_ids = {}
  400. for id, mod2 in pairs(list) do
  401. if mod2.type == "mod" and not mod2.is_modpack then
  402. mod_ids[mod2.name] = id
  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. else
  429. if mod_to_enable.enabled == false 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[name] 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(type, path, basename, targetpath)
  476. local basefolder = pkgmgr.get_base_folder(path)
  477. -- There's no good way to detect a texture pack, so let's just assume
  478. -- it's correct for now.
  479. if type == "txp" then
  480. if basefolder and basefolder.type ~= "invalid" and basefolder.type ~= "txp" then
  481. return nil, fgettext("Unable to install a $1 as a texture pack", basefolder.type)
  482. end
  483. local from = basefolder and basefolder.path or path
  484. if targetpath then
  485. core.delete_dir(targetpath)
  486. core.create_dir(targetpath)
  487. else
  488. targetpath = core.get_texturepath() .. DIR_DELIM .. basename
  489. end
  490. if not core.copy_dir(from, targetpath) then
  491. return nil,
  492. fgettext("Failed to install $1 to $2", basename, targetpath)
  493. end
  494. return targetpath, nil
  495. elseif not basefolder then
  496. return nil, fgettext("Unable to find a valid mod or modpack")
  497. end
  498. --
  499. -- Get destination
  500. --
  501. if basefolder.type == "modpack" then
  502. if type ~= "mod" then
  503. return nil, fgettext("Unable to install a modpack as a $1", type)
  504. end
  505. -- Get destination name for modpack
  506. if targetpath then
  507. core.delete_dir(targetpath)
  508. core.create_dir(targetpath)
  509. else
  510. local clean_path = nil
  511. if basename ~= nil then
  512. clean_path = basename
  513. end
  514. if not clean_path then
  515. clean_path = get_last_folder(cleanup_path(basefolder.path))
  516. end
  517. if clean_path then
  518. targetpath = core.get_modpath() .. DIR_DELIM .. clean_path
  519. else
  520. return nil,
  521. fgettext("Install Mod: Unable to find suitable folder name for modpack $1",
  522. path)
  523. end
  524. end
  525. elseif basefolder.type == "mod" then
  526. if type ~= "mod" then
  527. return nil, fgettext("Unable to install a mod as a $1", type)
  528. end
  529. if targetpath then
  530. core.delete_dir(targetpath)
  531. core.create_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. core.create_dir(targetpath)
  554. else
  555. targetpath = core.get_gamepath() .. DIR_DELIM .. basename
  556. end
  557. end
  558. -- Copy it
  559. if not core.copy_dir(basefolder.path, targetpath) then
  560. return nil,
  561. fgettext("Failed to install $1 to $2", basename, targetpath)
  562. end
  563. if basefolder.type == "game" then
  564. pkgmgr.update_gamelist()
  565. else
  566. pkgmgr.refresh_globals()
  567. end
  568. return targetpath, nil
  569. end
  570. --------------------------------------------------------------------------------
  571. function pkgmgr.install(type, modfilename, basename, dest)
  572. local archive_info = pkgmgr.identify_filetype(modfilename)
  573. local path = pkgmgr.extract(archive_info)
  574. if path == nil then
  575. return nil,
  576. fgettext("Install: file: \"$1\"", archive_info.name) .. "\n" ..
  577. fgettext("Install: Unsupported file type \"$1\" or broken archive",
  578. archive_info.type)
  579. end
  580. local targetpath, msg = pkgmgr.install_dir(type, path, basename, dest)
  581. core.delete_dir(path)
  582. return targetpath, msg
  583. end
  584. --------------------------------------------------------------------------------
  585. function pkgmgr.preparemodlist(data)
  586. local retval = {}
  587. local global_mods = {}
  588. local game_mods = {}
  589. --read global mods
  590. local modpath = core.get_modpath()
  591. if modpath ~= nil and
  592. modpath ~= "" then
  593. get_mods(modpath,global_mods)
  594. end
  595. for i=1,#global_mods,1 do
  596. global_mods[i].type = "mod"
  597. global_mods[i].loc = "global"
  598. retval[#retval + 1] = global_mods[i]
  599. end
  600. --read game mods
  601. local gamespec = pkgmgr.find_by_gameid(data.gameid)
  602. pkgmgr.get_game_mods(gamespec, game_mods)
  603. if #game_mods > 0 then
  604. -- Add title
  605. retval[#retval + 1] = {
  606. type = "game",
  607. is_game_content = true,
  608. name = fgettext("$1 mods", gamespec.name),
  609. path = gamespec.path
  610. }
  611. end
  612. for i=1,#game_mods,1 do
  613. game_mods[i].type = "mod"
  614. game_mods[i].loc = "game"
  615. game_mods[i].is_game_content = true
  616. retval[#retval + 1] = game_mods[i]
  617. end
  618. if data.worldpath == nil then
  619. return retval
  620. end
  621. --read world mod configuration
  622. local filename = data.worldpath ..
  623. DIR_DELIM .. "world.mt"
  624. local worldfile = Settings(filename)
  625. for key,value in pairs(worldfile:to_table()) do
  626. if key:sub(1, 9) == "load_mod_" then
  627. key = key:sub(10)
  628. local element = nil
  629. for i=1,#retval,1 do
  630. if retval[i].name == key and
  631. not retval[i].is_modpack then
  632. element = retval[i]
  633. break
  634. end
  635. end
  636. if element ~= nil then
  637. element.enabled = value ~= "false" and value ~= "nil" and value
  638. else
  639. core.log("info", "Mod: " .. key .. " " .. dump(value) .. " but not found")
  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.identify_filetype(name)
  705. if name:sub(-3):lower() == "zip" then
  706. return {
  707. name = name,
  708. type = "zip"
  709. }
  710. end
  711. if name:sub(-6):lower() == "tar.gz" or
  712. name:sub(-3):lower() == "tgz"then
  713. return {
  714. name = name,
  715. type = "tgz"
  716. }
  717. end
  718. if name:sub(-6):lower() == "tar.bz2" then
  719. return {
  720. name = name,
  721. type = "tbz"
  722. }
  723. end
  724. if name:sub(-2):lower() == "7z" then
  725. return {
  726. name = name,
  727. type = "7z"
  728. }
  729. end
  730. return {
  731. name = name,
  732. type = "ukn"
  733. }
  734. end
  735. --------------------------------------------------------------------------------
  736. function pkgmgr.find_by_gameid(gameid)
  737. for i=1,#pkgmgr.games,1 do
  738. if pkgmgr.games[i].id == gameid then
  739. return pkgmgr.games[i], i
  740. end
  741. end
  742. return nil, nil
  743. end
  744. --------------------------------------------------------------------------------
  745. function pkgmgr.get_game_mods(gamespec, retval)
  746. if gamespec ~= nil and
  747. gamespec.gamemods_path ~= nil and
  748. gamespec.gamemods_path ~= "" then
  749. get_mods(gamespec.gamemods_path, retval)
  750. end
  751. end
  752. --------------------------------------------------------------------------------
  753. function pkgmgr.get_game_modlist(gamespec)
  754. local retval = ""
  755. local game_mods = {}
  756. pkgmgr.get_game_mods(gamespec, game_mods)
  757. for i=1,#game_mods,1 do
  758. if retval ~= "" then
  759. retval = retval..","
  760. end
  761. retval = retval .. game_mods[i].name
  762. end
  763. return retval
  764. end
  765. --------------------------------------------------------------------------------
  766. function pkgmgr.get_game(index)
  767. if index > 0 and index <= #pkgmgr.games then
  768. return pkgmgr.games[index]
  769. end
  770. return nil
  771. end
  772. --------------------------------------------------------------------------------
  773. function pkgmgr.update_gamelist()
  774. pkgmgr.games = core.get_games()
  775. end
  776. --------------------------------------------------------------------------------
  777. function pkgmgr.gamelist()
  778. local retval = ""
  779. if #pkgmgr.games > 0 then
  780. retval = retval .. core.formspec_escape(pkgmgr.games[1].name)
  781. for i=2,#pkgmgr.games,1 do
  782. retval = retval .. "," .. core.formspec_escape(pkgmgr.games[i].name)
  783. end
  784. end
  785. return retval
  786. end
  787. --------------------------------------------------------------------------------
  788. -- read initial data
  789. --------------------------------------------------------------------------------
  790. pkgmgr.update_gamelist()