pkgmgr.lua 22 KB

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