2 --Copyright (C) 2013 sapier
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.
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.
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.
18 --------------------------------------------------------------------------------
19 function get_mods(path,retval,modpack)
20 local mods = core.get_dir_list(path, true)
22 for _, name in ipairs(mods) do
23 if name:sub(1, 1) ~= "." then
24 local prefix = path .. DIR_DELIM .. name
29 retval[#retval + 1] = toadd
33 local modpack_conf = io.open(prefix .. DIR_DELIM .. "modpack.conf")
35 toadd.is_modpack = true
38 mod_conf = Settings(prefix .. DIR_DELIM .. "modpack.conf"):to_table()
41 toadd.is_name_explicit = true
44 mod_conf = Settings(prefix .. DIR_DELIM .. "mod.conf"):to_table()
47 toadd.is_name_explicit = true
53 toadd.author = mod_conf.author
54 toadd.release = tonumber(mod_conf.release or "0")
59 -- Note: modpack.conf is already checked above
60 local modpackfile = io.open(prefix .. DIR_DELIM .. "modpack.txt")
63 toadd.is_modpack = true
66 -- Deal with modpack contents
67 if modpack and modpack ~= "" then
68 toadd.modpack = modpack
69 elseif toadd.is_modpack then
70 toadd.type = "modpack"
71 toadd.is_modpack = true
72 get_mods(prefix, retval, name)
78 --modmanager implementation
81 function pkgmgr.get_texture_packs()
82 local txtpath = core.get_texturepath()
83 local list = core.get_dir_list(txtpath, true)
86 local current_texture_path = core.settings:get("texture_path")
88 for _, item in ipairs(list) do
89 if item ~= "base" then
92 local path = txtpath .. DIR_DELIM .. item .. DIR_DELIM
93 if path == current_texture_path then
94 name = fgettext("$1 (Enabled)", name)
97 local conf = Settings(path .. "texture_pack.conf")
99 retval[#retval + 1] = {
101 author = conf:get("author"),
102 release = tonumber(conf:get("release") or "0"),
106 enabled = path == current_texture_path,
111 table.sort(retval, function(a, b)
112 return a.name > b.name
118 --------------------------------------------------------------------------------
119 function pkgmgr.extract(modfile)
120 if modfile.type == "zip" then
121 local tempfolder = os.tempfolder()
123 if tempfolder ~= nil and
124 tempfolder ~= "" then
125 core.create_dir(tempfolder)
126 if core.extract_zip(modfile.name,tempfolder) then
134 function pkgmgr.get_folder_type(path)
135 local testfile = io.open(path .. DIR_DELIM .. "init.lua","r")
136 if testfile ~= nil then
138 return { type = "mod", path = path }
141 testfile = io.open(path .. DIR_DELIM .. "modpack.conf","r")
142 if testfile ~= nil then
144 return { type = "modpack", path = path }
147 testfile = io.open(path .. DIR_DELIM .. "modpack.txt","r")
148 if testfile ~= nil then
150 return { type = "modpack", path = path }
153 testfile = io.open(path .. DIR_DELIM .. "game.conf","r")
154 if testfile ~= nil then
156 return { type = "game", path = path }
159 testfile = io.open(path .. DIR_DELIM .. "texture_pack.conf","r")
160 if testfile ~= nil then
162 return { type = "txp", path = path }
168 -------------------------------------------------------------------------------
169 function pkgmgr.get_base_folder(temppath)
170 if temppath == nil then
171 return { type = "invalid", path = "" }
174 local ret = pkgmgr.get_folder_type(temppath)
179 local subdirs = core.get_dir_list(temppath, true)
180 if #subdirs == 1 then
181 ret = pkgmgr.get_folder_type(temppath .. DIR_DELIM .. subdirs[1])
185 return { type = "invalid", path = temppath .. DIR_DELIM .. subdirs[1] }
192 --------------------------------------------------------------------------------
193 function pkgmgr.isValidModname(modpath)
194 if modpath:find("-") ~= nil then
201 --------------------------------------------------------------------------------
202 function pkgmgr.parse_register_line(line)
203 local pos1 = line:find("\"")
206 pos2 = line:find("\"",pos1+1)
209 if pos1 ~= nil and pos2 ~= nil then
210 local item = line:sub(pos1+1,pos2-1)
214 local pos3 = item:find(":")
217 local retval = item:sub(1,pos3-1)
228 --------------------------------------------------------------------------------
229 function pkgmgr.parse_dofile_line(modpath,line)
230 local pos1 = line:find("\"")
233 pos2 = line:find("\"",pos1+1)
236 if pos1 ~= nil and pos2 ~= nil then
237 local filename = line:sub(pos1+1,pos2-1)
239 if filename ~= nil and
241 filename:find(".lua") then
242 return pkgmgr.identify_modname(modpath,filename)
248 --------------------------------------------------------------------------------
249 function pkgmgr.identify_modname(modpath,filename)
250 local testfile = io.open(modpath .. DIR_DELIM .. filename,"r")
251 if testfile ~= nil then
252 local line = testfile:read()
257 if line:find("minetest.register_tool") then
258 modname = pkgmgr.parse_register_line(line)
261 if line:find("minetest.register_craftitem") then
262 modname = pkgmgr.parse_register_line(line)
266 if line:find("minetest.register_node") then
267 modname = pkgmgr.parse_register_line(line)
270 if line:find("dofile") then
271 modname = pkgmgr.parse_dofile_line(modpath,line)
274 if modname ~= nil then
279 line = testfile:read()
286 --------------------------------------------------------------------------------
287 function pkgmgr.render_packagelist(render_list)
290 if render_list == nil then
291 if pkgmgr.global_mods == nil then
292 pkgmgr.refresh_globals()
294 render_list = pkgmgr.global_mods
297 local list = render_list:get_list()
298 local last_modpack = nil
300 for i, v in ipairs(list) do
303 local rawlist = render_list:get_raw_list()
304 color = mt_color_dark_green
306 for j = 1, #rawlist, 1 do
307 if rawlist[j].modpack == list[i].name and
308 not rawlist[j].enabled then
309 -- Modpack not entirely enabled so showing as grey
310 color = mt_color_grey
314 elseif v.is_game_content or v.type == "game" then
315 color = mt_color_blue
316 elseif v.enabled or v.type == "txp" then
317 color = mt_color_green
320 retval[#retval + 1] = color
321 if v.modpack ~= nil or v.loc == "game" then
322 retval[#retval + 1] = "1"
324 retval[#retval + 1] = "0"
326 retval[#retval + 1] = core.formspec_escape(v.list_name or v.name)
329 return table.concat(retval, ",")
332 --------------------------------------------------------------------------------
333 function pkgmgr.get_dependencies(path)
338 local info = core.get_content_info(path)
339 return table.concat(info.depends or {}, ","), table.concat(info.optional_depends or {}, ",")
342 ----------- tests whether all of the mods in the modpack are enabled -----------
343 function pkgmgr.is_modpack_entirely_enabled(data, name)
344 local rawlist = data.list:get_raw_list()
345 for j = 1, #rawlist do
346 if rawlist[j].modpack == name and not rawlist[j].enabled then
353 ---------- toggles or en/disables a mod or modpack -----------------------------
354 function pkgmgr.enable_mod(this, toset)
355 local mod = this.data.list:get_list()[this.data.selected_mod]
357 -- game mods can't be enabled or disabled
358 if mod.is_game_content then
362 -- toggle or en/disable the mod
363 if not mod.is_modpack then
365 mod.enabled = not mod.enabled
372 -- toggle or en/disable every mod in the modpack, interleaved unsupported
373 local list = this.data.list:get_raw_list()
375 if list[i].modpack == mod.name then
377 toset = not list[i].enabled
379 list[i].enabled = toset
384 --------------------------------------------------------------------------------
385 function pkgmgr.get_worldconfig(worldpath)
386 local filename = worldpath ..
387 DIR_DELIM .. "world.mt"
389 local worldfile = Settings(filename)
391 local worldconfig = {}
392 worldconfig.global_mods = {}
393 worldconfig.game_mods = {}
395 for key,value in pairs(worldfile:to_table()) do
396 if key == "gameid" then
397 worldconfig.id = value
398 elseif key:sub(0, 9) == "load_mod_" then
399 -- Compatibility: Check against "nil" which was erroneously used
400 -- as value for fresh configured worlds
401 worldconfig.global_mods[key] = value ~= "false" and value ~= "nil"
404 worldconfig[key] = value
409 local gamespec = pkgmgr.find_by_gameid(worldconfig.id)
410 pkgmgr.get_game_mods(gamespec, worldconfig.game_mods)
415 --------------------------------------------------------------------------------
416 function pkgmgr.install_dir(type, path, basename, targetpath)
417 local basefolder = pkgmgr.get_base_folder(path)
419 -- There's no good way to detect a texture pack, so let's just assume
420 -- it's correct for now.
421 if type == "txp" then
422 if basefolder and basefolder.type ~= "invalid" and basefolder.type ~= "txp" then
423 return nil, fgettext("Unable to install a $1 as a texture pack", basefolder.type)
426 local from = basefolder and basefolder.path or path
428 core.delete_dir(targetpath)
429 core.create_dir(targetpath)
431 targetpath = core.get_texturepath() .. DIR_DELIM .. basename
433 if not core.copy_dir(from, targetpath) then
435 fgettext("Failed to install $1 to $2", basename, targetpath)
437 return targetpath, nil
439 elseif not basefolder then
440 return nil, fgettext("Unable to find a valid mod or modpack")
446 if basefolder.type == "modpack" then
447 if type ~= "mod" then
448 return nil, fgettext("Unable to install a modpack as a $1", type)
451 -- Get destination name for modpack
453 core.delete_dir(targetpath)
454 core.create_dir(targetpath)
456 local clean_path = nil
457 if basename ~= nil then
458 clean_path = basename
460 if not clean_path then
461 clean_path = get_last_folder(cleanup_path(basefolder.path))
464 targetpath = core.get_modpath() .. DIR_DELIM .. clean_path
467 fgettext("Install Mod: Unable to find suitable folder name for modpack $1",
471 elseif basefolder.type == "mod" then
472 if type ~= "mod" then
473 return nil, fgettext("Unable to install a mod as a $1", type)
477 core.delete_dir(targetpath)
478 core.create_dir(targetpath)
480 local targetfolder = basename
481 if targetfolder == nil then
482 targetfolder = pkgmgr.identify_modname(basefolder.path, "init.lua")
485 -- If heuristic failed try to use current foldername
486 if targetfolder == nil then
487 targetfolder = get_last_folder(basefolder.path)
490 if targetfolder ~= nil and pkgmgr.isValidModname(targetfolder) then
491 targetpath = core.get_modpath() .. DIR_DELIM .. targetfolder
493 return nil, fgettext("Install Mod: Unable to find real mod name for: $1", modfilename)
497 elseif basefolder.type == "game" then
498 if type ~= "game" then
499 return nil, fgettext("Unable to install a game as a $1", type)
503 core.delete_dir(targetpath)
504 core.create_dir(targetpath)
506 targetpath = core.get_gamepath() .. DIR_DELIM .. basename
511 if not core.copy_dir(basefolder.path, targetpath) then
513 fgettext("Failed to install $1 to $2", basename, targetpath)
516 if basefolder.type == "game" then
517 pkgmgr.update_gamelist()
519 pkgmgr.refresh_globals()
522 return targetpath, nil
525 --------------------------------------------------------------------------------
526 function pkgmgr.install(type, modfilename, basename, dest)
527 local archive_info = pkgmgr.identify_filetype(modfilename)
528 local path = pkgmgr.extract(archive_info)
532 fgettext("Install: file: \"$1\"", archive_info.name) .. "\n" ..
533 fgettext("Install: Unsupported file type \"$1\" or broken archive",
537 local targetpath, msg = pkgmgr.install_dir(type, path, basename, dest)
538 core.delete_dir(path)
539 return targetpath, msg
542 --------------------------------------------------------------------------------
543 function pkgmgr.preparemodlist(data)
546 local global_mods = {}
550 local modpath = core.get_modpath()
552 if modpath ~= nil and
554 get_mods(modpath,global_mods)
557 for i=1,#global_mods,1 do
558 global_mods[i].type = "mod"
559 global_mods[i].loc = "global"
560 retval[#retval + 1] = global_mods[i]
564 local gamespec = pkgmgr.find_by_gameid(data.gameid)
565 pkgmgr.get_game_mods(gamespec, game_mods)
567 if #game_mods > 0 then
569 retval[#retval + 1] = {
571 is_game_content = true,
572 name = fgettext("$1 mods", gamespec.name),
577 for i=1,#game_mods,1 do
578 game_mods[i].type = "mod"
579 game_mods[i].loc = "game"
580 game_mods[i].is_game_content = true
581 retval[#retval + 1] = game_mods[i]
584 if data.worldpath == nil then
588 --read world mod configuration
589 local filename = data.worldpath ..
590 DIR_DELIM .. "world.mt"
592 local worldfile = Settings(filename)
594 for key,value in pairs(worldfile:to_table()) do
595 if key:sub(1, 9) == "load_mod_" then
599 if retval[i].name == key and
600 not retval[i].is_modpack then
605 if element ~= nil then
606 element.enabled = value ~= "false" and value ~= "nil" and value
608 core.log("info", "Mod: " .. key .. " " .. dump(value) .. " but not found")
616 function pkgmgr.compare_package(a, b)
617 return a and b and a.name == b.name and a.path == b.path
620 --------------------------------------------------------------------------------
621 function pkgmgr.comparemod(elem1,elem2)
622 if elem1 == nil or elem2 == nil then
625 if elem1.name ~= elem2.name then
628 if elem1.is_modpack ~= elem2.is_modpack then
631 if elem1.type ~= elem2.type then
634 if elem1.modpack ~= elem2.modpack then
638 if elem1.path ~= elem2.path then
645 --------------------------------------------------------------------------------
646 function pkgmgr.mod_exists(basename)
648 if pkgmgr.global_mods == nil then
649 pkgmgr.refresh_globals()
652 if pkgmgr.global_mods:raw_index_by_uid(basename) > 0 then
659 --------------------------------------------------------------------------------
660 function pkgmgr.get_global_mod(idx)
662 if pkgmgr.global_mods == nil then
666 if idx == nil or idx < 1 or
667 idx > pkgmgr.global_mods:size() then
671 return pkgmgr.global_mods:get_list()[idx]
674 --------------------------------------------------------------------------------
675 function pkgmgr.refresh_globals()
676 local function is_equal(element,uid) --uid match
677 if element.name == uid then
681 pkgmgr.global_mods = filterlist.create(pkgmgr.preparemodlist,
682 pkgmgr.comparemod, is_equal, nil, {})
683 pkgmgr.global_mods:add_sort_mechanism("alphabetic", sort_mod_list)
684 pkgmgr.global_mods:set_sortmode("alphabetic")
687 --------------------------------------------------------------------------------
688 function pkgmgr.identify_filetype(name)
690 if name:sub(-3):lower() == "zip" then
697 if name:sub(-6):lower() == "tar.gz" or
698 name:sub(-3):lower() == "tgz"then
705 if name:sub(-6):lower() == "tar.bz2" then
712 if name:sub(-2):lower() == "7z" then
726 --------------------------------------------------------------------------------
727 function pkgmgr.find_by_gameid(gameid)
728 for i=1,#pkgmgr.games,1 do
729 if pkgmgr.games[i].id == gameid then
730 return pkgmgr.games[i], i
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)
745 --------------------------------------------------------------------------------
746 function pkgmgr.get_game_modlist(gamespec)
749 pkgmgr.get_game_mods(gamespec, game_mods)
750 for i=1,#game_mods,1 do
754 retval = retval .. game_mods[i].name
759 --------------------------------------------------------------------------------
760 function pkgmgr.get_game(index)
761 if index > 0 and index <= #pkgmgr.games then
762 return pkgmgr.games[index]
768 --------------------------------------------------------------------------------
769 function pkgmgr.update_gamelist()
770 pkgmgr.games = core.get_games()
773 --------------------------------------------------------------------------------
774 function pkgmgr.gamelist()
776 if #pkgmgr.games > 0 then
777 retval = retval .. core.formspec_escape(pkgmgr.games[1].name)
779 for i=2,#pkgmgr.games,1 do
780 retval = retval .. "," .. core.formspec_escape(pkgmgr.games[i].name)
786 --------------------------------------------------------------------------------
788 --------------------------------------------------------------------------------
789 pkgmgr.update_gamelist()