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 local function get_last_folder(text,count)
20 local parts = text:split(DIR_DELIM)
28 retval = retval .. parts[#parts - (count-i)] .. DIR_DELIM
34 local function cleanup_path(temppath)
36 local parts = temppath:split("-")
39 if temppath ~= "" then
40 temppath = temppath .. "_"
42 temppath = temppath .. parts[i]
45 parts = temppath:split(".")
48 if temppath ~= "" then
49 temppath = temppath .. "_"
51 temppath = temppath .. parts[i]
54 parts = temppath:split("'")
57 if temppath ~= "" then
58 temppath = temppath .. ""
60 temppath = temppath .. parts[i]
63 parts = temppath:split(" ")
66 if temppath ~= "" then
69 temppath = temppath .. parts[i]
75 local function load_texture_packs(txtpath, retval)
76 local list = core.get_dir_list(txtpath, true)
77 local current_texture_path = core.settings:get("texture_path")
79 for _, item in ipairs(list) do
80 if item ~= "base" then
83 local path = txtpath .. DIR_DELIM .. item .. DIR_DELIM
84 if path == current_texture_path then
85 name = fgettext("$1 (Enabled)", name)
88 local conf = Settings(path .. "texture_pack.conf")
90 retval[#retval + 1] = {
92 author = conf:get("author"),
93 release = tonumber(conf:get("release")) or 0,
97 enabled = path == current_texture_path,
103 function get_mods(path,retval,modpack)
104 local mods = core.get_dir_list(path, true)
106 for _, name in ipairs(mods) do
107 if name:sub(1, 1) ~= "." then
108 local prefix = path .. DIR_DELIM .. name
113 retval[#retval + 1] = toadd
117 local modpack_conf = io.open(prefix .. DIR_DELIM .. "modpack.conf")
119 toadd.is_modpack = true
122 mod_conf = Settings(prefix .. DIR_DELIM .. "modpack.conf"):to_table()
123 if mod_conf.name then
125 toadd.is_name_explicit = true
128 mod_conf = Settings(prefix .. DIR_DELIM .. "mod.conf"):to_table()
129 if mod_conf.name then
131 toadd.is_name_explicit = true
137 toadd.author = mod_conf.author
138 toadd.release = tonumber(mod_conf.release) or 0
143 -- Note: modpack.conf is already checked above
144 local modpackfile = io.open(prefix .. DIR_DELIM .. "modpack.txt")
147 toadd.is_modpack = true
150 -- Deal with modpack contents
151 if modpack and modpack ~= "" then
152 toadd.modpack = modpack
153 elseif toadd.is_modpack then
154 toadd.type = "modpack"
155 toadd.is_modpack = true
156 get_mods(prefix, retval, name)
162 --modmanager implementation
165 function pkgmgr.get_texture_packs()
166 local txtpath = core.get_texturepath()
167 local txtpath_system = core.get_texturepath_share()
170 load_texture_packs(txtpath, retval)
171 -- on portable versions these two paths coincide. It avoids loading the path twice
172 if txtpath ~= txtpath_system then
173 load_texture_packs(txtpath_system, retval)
176 table.sort(retval, function(a, b)
177 return a.name > b.name
183 --------------------------------------------------------------------------------
184 function pkgmgr.extract(modfile)
185 if modfile.type == "zip" then
186 local tempfolder = os.tempfolder()
188 if tempfolder ~= nil and
189 tempfolder ~= "" then
190 core.create_dir(tempfolder)
191 if core.extract_zip(modfile.name,tempfolder) then
199 function pkgmgr.get_folder_type(path)
200 local testfile = io.open(path .. DIR_DELIM .. "init.lua","r")
201 if testfile ~= nil then
203 return { type = "mod", path = path }
206 testfile = io.open(path .. DIR_DELIM .. "modpack.conf","r")
207 if testfile ~= nil then
209 return { type = "modpack", path = path }
212 testfile = io.open(path .. DIR_DELIM .. "modpack.txt","r")
213 if testfile ~= nil then
215 return { type = "modpack", path = path }
218 testfile = io.open(path .. DIR_DELIM .. "game.conf","r")
219 if testfile ~= nil then
221 return { type = "game", path = path }
224 testfile = io.open(path .. DIR_DELIM .. "texture_pack.conf","r")
225 if testfile ~= nil then
227 return { type = "txp", path = path }
233 -------------------------------------------------------------------------------
234 function pkgmgr.get_base_folder(temppath)
235 if temppath == nil then
236 return { type = "invalid", path = "" }
239 local ret = pkgmgr.get_folder_type(temppath)
244 local subdirs = core.get_dir_list(temppath, true)
245 if #subdirs == 1 then
246 ret = pkgmgr.get_folder_type(temppath .. DIR_DELIM .. subdirs[1])
250 return { type = "invalid", path = temppath .. DIR_DELIM .. subdirs[1] }
257 --------------------------------------------------------------------------------
258 function pkgmgr.isValidModname(modpath)
259 if modpath:find("-") ~= nil then
266 --------------------------------------------------------------------------------
267 function pkgmgr.parse_register_line(line)
268 local pos1 = line:find("\"")
271 pos2 = line:find("\"",pos1+1)
274 if pos1 ~= nil and pos2 ~= nil then
275 local item = line:sub(pos1+1,pos2-1)
279 local pos3 = item:find(":")
282 local retval = item:sub(1,pos3-1)
293 --------------------------------------------------------------------------------
294 function pkgmgr.parse_dofile_line(modpath,line)
295 local pos1 = line:find("\"")
298 pos2 = line:find("\"",pos1+1)
301 if pos1 ~= nil and pos2 ~= nil then
302 local filename = line:sub(pos1+1,pos2-1)
304 if filename ~= nil and
306 filename:find(".lua") then
307 return pkgmgr.identify_modname(modpath,filename)
313 --------------------------------------------------------------------------------
314 function pkgmgr.identify_modname(modpath,filename)
315 local testfile = io.open(modpath .. DIR_DELIM .. filename,"r")
316 if testfile ~= nil then
317 local line = testfile:read()
322 if line:find("minetest.register_tool") then
323 modname = pkgmgr.parse_register_line(line)
326 if line:find("minetest.register_craftitem") then
327 modname = pkgmgr.parse_register_line(line)
331 if line:find("minetest.register_node") then
332 modname = pkgmgr.parse_register_line(line)
335 if line:find("dofile") then
336 modname = pkgmgr.parse_dofile_line(modpath,line)
339 if modname ~= nil then
344 line = testfile:read()
351 --------------------------------------------------------------------------------
352 function pkgmgr.render_packagelist(render_list)
353 if not render_list then
354 if not pkgmgr.global_mods then
355 pkgmgr.refresh_globals()
357 render_list = pkgmgr.global_mods
360 local list = render_list:get_list()
362 for i, v in ipairs(list) do
365 local rawlist = render_list:get_raw_list()
366 color = mt_color_dark_green
368 for j = 1, #rawlist, 1 do
369 if rawlist[j].modpack == list[i].name and
370 not rawlist[j].enabled then
371 -- Modpack not entirely enabled so showing as grey
372 color = mt_color_grey
376 elseif v.is_game_content or v.type == "game" then
377 color = mt_color_blue
378 elseif v.enabled or v.type == "txp" then
379 color = mt_color_green
382 retval[#retval + 1] = color
383 if v.modpack ~= nil or v.loc == "game" then
384 retval[#retval + 1] = "1"
386 retval[#retval + 1] = "0"
388 retval[#retval + 1] = core.formspec_escape(v.list_name or v.name)
391 return table.concat(retval, ",")
394 --------------------------------------------------------------------------------
395 function pkgmgr.get_dependencies(path)
400 local info = core.get_content_info(path)
401 return info.depends or {}, info.optional_depends or {}
404 ----------- tests whether all of the mods in the modpack are enabled -----------
405 function pkgmgr.is_modpack_entirely_enabled(data, name)
406 local rawlist = data.list:get_raw_list()
407 for j = 1, #rawlist do
408 if rawlist[j].modpack == name and not rawlist[j].enabled then
415 ---------- toggles or en/disables a mod or modpack and its dependencies --------
416 local function toggle_mod_or_modpack(list, toggled_mods, enabled_mods, toset, mod)
417 if not mod.is_modpack then
418 -- Toggle or en/disable the mod
420 toset = not mod.enabled
422 if mod.enabled ~= toset then
424 toggled_mods[#toggled_mods+1] = mod.name
427 -- Mark this mod for recursive dependency traversal
428 enabled_mods[mod.name] = true
431 -- Toggle or en/disable every mod in the modpack,
432 -- interleaved unsupported
434 if list[i].modpack == mod.name then
435 toggle_mod_or_modpack(list, toggled_mods, enabled_mods, toset, list[i])
441 function pkgmgr.enable_mod(this, toset)
442 local list = this.data.list:get_list()
443 local mod = list[this.data.selected_mod]
445 -- Game mods can't be enabled or disabled
446 if mod.is_game_content then
450 local toggled_mods = {}
451 local enabled_mods = {}
452 toggle_mod_or_modpack(list, toggled_mods, enabled_mods, toset, mod)
455 -- Mod(s) were disabled, so no dependencies need to be enabled
456 table.sort(toggled_mods)
457 core.log("info", "Following mods were disabled: " ..
458 table.concat(toggled_mods, ", "))
462 -- Enable mods' depends after activation
464 -- Make a list of mod ids indexed by their names
466 for id, mod2 in pairs(list) do
467 if mod2.type == "mod" and not mod2.is_modpack then
468 mod_ids[mod2.name] = id
472 -- to_enable is used as a DFS stack with sp as stack pointer
475 for name in pairs(enabled_mods) do
476 local depends = pkgmgr.get_dependencies(list[mod_ids[name]].path)
477 for i = 1, #depends do
478 local dependency_name = depends[i]
479 if not enabled_mods[dependency_name] then
481 to_enable[sp] = dependency_name
485 -- If sp is 0, every dependency is already activated
487 local name = to_enable[sp]
490 if not enabled_mods[name] then
491 enabled_mods[name] = true
492 local mod_to_enable = list[mod_ids[name]]
493 if not mod_to_enable then
494 core.log("warning", "Mod dependency \"" .. name ..
497 if mod_to_enable.enabled == false then
498 mod_to_enable.enabled = true
499 toggled_mods[#toggled_mods+1] = mod_to_enable.name
501 -- Push the dependencies of the dependency onto the stack
502 local depends = pkgmgr.get_dependencies(mod_to_enable.path)
503 for i = 1, #depends do
504 if not enabled_mods[name] then
506 to_enable[sp] = depends[i]
513 -- Log the list of enabled mods
514 table.sort(toggled_mods)
515 core.log("info", "Following mods were enabled: " ..
516 table.concat(toggled_mods, ", "))
519 --------------------------------------------------------------------------------
520 function pkgmgr.get_worldconfig(worldpath)
521 local filename = worldpath ..
522 DIR_DELIM .. "world.mt"
524 local worldfile = Settings(filename)
526 local worldconfig = {}
527 worldconfig.global_mods = {}
528 worldconfig.game_mods = {}
530 for key,value in pairs(worldfile:to_table()) do
531 if key == "gameid" then
532 worldconfig.id = value
533 elseif key:sub(0, 9) == "load_mod_" then
534 -- Compatibility: Check against "nil" which was erroneously used
535 -- as value for fresh configured worlds
536 worldconfig.global_mods[key] = value ~= "false" and value ~= "nil"
539 worldconfig[key] = value
544 local gamespec = pkgmgr.find_by_gameid(worldconfig.id)
545 pkgmgr.get_game_mods(gamespec, worldconfig.game_mods)
550 --------------------------------------------------------------------------------
551 function pkgmgr.install_dir(type, path, basename, targetpath)
552 local basefolder = pkgmgr.get_base_folder(path)
554 -- There's no good way to detect a texture pack, so let's just assume
555 -- it's correct for now.
556 if type == "txp" then
557 if basefolder and basefolder.type ~= "invalid" and basefolder.type ~= "txp" then
558 return nil, fgettext("Unable to install a $1 as a texture pack", basefolder.type)
561 local from = basefolder and basefolder.path or path
563 core.delete_dir(targetpath)
564 core.create_dir(targetpath)
566 targetpath = core.get_texturepath() .. DIR_DELIM .. basename
568 if not core.copy_dir(from, targetpath) then
570 fgettext("Failed to install $1 to $2", basename, targetpath)
572 return targetpath, nil
574 elseif not basefolder then
575 return nil, fgettext("Unable to find a valid mod or modpack")
581 if basefolder.type == "modpack" then
582 if type ~= "mod" then
583 return nil, fgettext("Unable to install a modpack as a $1", type)
586 -- Get destination name for modpack
588 core.delete_dir(targetpath)
589 core.create_dir(targetpath)
591 local clean_path = nil
592 if basename ~= nil then
593 clean_path = basename
595 if not clean_path then
596 clean_path = get_last_folder(cleanup_path(basefolder.path))
599 targetpath = core.get_clientmodpath() .. DIR_DELIM .. clean_path
602 fgettext("Install Mod: Unable to find suitable folder name for modpack $1",
606 elseif basefolder.type == "mod" then
607 if type ~= "mod" then
608 return nil, fgettext("Unable to install a mod as a $1", type)
612 core.delete_dir(targetpath)
613 core.create_dir(targetpath)
615 local targetfolder = basename
616 if targetfolder == nil then
617 targetfolder = pkgmgr.identify_modname(basefolder.path, "init.lua")
620 -- If heuristic failed try to use current foldername
621 if targetfolder == nil then
622 targetfolder = get_last_folder(basefolder.path)
625 if targetfolder ~= nil and pkgmgr.isValidModname(targetfolder) then
626 targetpath = core.get_clientmodpath() .. DIR_DELIM .. targetfolder
628 return nil, fgettext("Install Mod: Unable to find real mod name for: $1", path)
632 elseif basefolder.type == "game" then
633 if type ~= "game" then
634 return nil, fgettext("Unable to install a game as a $1", type)
638 core.delete_dir(targetpath)
639 core.create_dir(targetpath)
641 targetpath = core.get_gamepath() .. DIR_DELIM .. basename
646 if not core.copy_dir(basefolder.path, targetpath) then
648 fgettext("Failed to install $1 to $2", basename, targetpath)
651 if basefolder.type == "game" then
652 pkgmgr.update_gamelist()
654 pkgmgr.refresh_globals()
657 return targetpath, nil
660 --------------------------------------------------------------------------------
661 function pkgmgr.install(type, modfilename, basename, dest)
662 local archive_info = pkgmgr.identify_filetype(modfilename)
663 local path = pkgmgr.extract(archive_info)
667 fgettext("Install: file: \"$1\"", archive_info.name) .. "\n" ..
668 fgettext("Install: Unsupported file type \"$1\" or broken archive",
672 local targetpath, msg = pkgmgr.install_dir(type, path, basename, dest)
673 core.delete_dir(path)
674 return targetpath, msg
677 --------------------------------------------------------------------------------
678 function pkgmgr.prepareclientmodlist(data)
681 local clientmods = {}
684 local modpath = core.get_clientmodpath()
686 if modpath ~= nil and
688 get_mods(modpath,clientmods)
691 for i=1,#clientmods,1 do
692 clientmods[i].type = "mod"
693 clientmods[i].loc = "global"
694 clientmods[i].is_clientside = true
695 retval[#retval + 1] = clientmods[i]
698 --read mods configuration
699 local filename = modpath ..
700 DIR_DELIM .. "mods.conf"
702 local conffile = Settings(filename)
704 for key,value in pairs(conffile:to_table()) do
705 if key:sub(1, 9) == "load_mod_" then
709 if retval[i].name == key and
710 not retval[i].is_modpack then
715 if element ~= nil then
716 element.enabled = value ~= "false" and value ~= "nil" and value
718 core.log("info", "Clientmod: " .. key .. " " .. dump(value) .. " but not found")
726 function pkgmgr.preparemodlist(data)
729 local global_mods = {}
733 local modpath = core.get_modpath()
735 if modpath ~= nil and
737 get_mods(modpath,global_mods)
740 for i=1,#global_mods,1 do
741 global_mods[i].type = "mod"
742 global_mods[i].loc = "global"
743 retval[#retval + 1] = global_mods[i]
747 local gamespec = pkgmgr.find_by_gameid(data.gameid)
748 pkgmgr.get_game_mods(gamespec, game_mods)
750 if #game_mods > 0 then
752 retval[#retval + 1] = {
754 is_game_content = true,
755 name = fgettext("$1 mods", gamespec.name),
760 for i=1,#game_mods,1 do
761 game_mods[i].type = "mod"
762 game_mods[i].loc = "game"
763 game_mods[i].is_game_content = true
764 retval[#retval + 1] = game_mods[i]
767 if data.worldpath == nil then
771 --read world mod configuration
772 local filename = data.worldpath ..
773 DIR_DELIM .. "world.mt"
775 local worldfile = Settings(filename)
777 for key,value in pairs(worldfile:to_table()) do
778 if key:sub(1, 9) == "load_mod_" then
782 if retval[i].name == key and
783 not retval[i].is_modpack then
788 if element ~= nil then
789 element.enabled = value ~= "false" and value ~= "nil" and value
791 core.log("info", "Mod: " .. key .. " " .. dump(value) .. " but not found")
799 function pkgmgr.compare_package(a, b)
800 return a and b and a.name == b.name and a.path == b.path
803 --------------------------------------------------------------------------------
804 function pkgmgr.comparemod(elem1,elem2)
805 if elem1 == nil or elem2 == nil then
808 if elem1.name ~= elem2.name then
811 if elem1.is_modpack ~= elem2.is_modpack then
814 if elem1.type ~= elem2.type then
817 if elem1.modpack ~= elem2.modpack then
821 if elem1.path ~= elem2.path then
828 --------------------------------------------------------------------------------
829 function pkgmgr.mod_exists(basename)
831 if pkgmgr.global_mods == nil then
832 pkgmgr.refresh_globals()
835 if pkgmgr.global_mods:raw_index_by_uid(basename) > 0 then
842 --------------------------------------------------------------------------------
843 function pkgmgr.get_global_mod(idx)
845 if pkgmgr.global_mods == nil then
849 if idx == nil or idx < 1 or
850 idx > pkgmgr.global_mods:size() then
854 return pkgmgr.global_mods:get_list()[idx]
857 --------------------------------------------------------------------------------
858 function pkgmgr.refresh_globals()
859 local function is_equal(element,uid) --uid match
860 if element.name == uid then
864 pkgmgr.global_mods = filterlist.create(pkgmgr.preparemodlist,
865 pkgmgr.comparemod, is_equal, nil, {})
866 pkgmgr.global_mods:add_sort_mechanism("alphabetic", sort_mod_list)
867 pkgmgr.global_mods:set_sortmode("alphabetic")
868 pkgmgr.clientmods = filterlist.create(pkgmgr.prepareclientmodlist,
869 pkgmgr.comparemod, is_equal, nil, {})
870 pkgmgr.clientmods:add_sort_mechanism("alphabetic", sort_mod_list)
871 pkgmgr.clientmods:set_sortmode("alphabetic")
874 --------------------------------------------------------------------------------
875 function pkgmgr.identify_filetype(name)
877 if name:sub(-3):lower() == "zip" then
884 if name:sub(-6):lower() == "tar.gz" or
885 name:sub(-3):lower() == "tgz"then
892 if name:sub(-6):lower() == "tar.bz2" then
899 if name:sub(-2):lower() == "7z" then
913 --------------------------------------------------------------------------------
914 function pkgmgr.find_by_gameid(gameid)
915 for i=1,#pkgmgr.games,1 do
916 if pkgmgr.games[i].id == gameid then
917 return pkgmgr.games[i], i
923 --------------------------------------------------------------------------------
924 function pkgmgr.get_game_mods(gamespec, retval)
925 if gamespec ~= nil and
926 gamespec.gamemods_path ~= nil and
927 gamespec.gamemods_path ~= "" then
928 get_mods(gamespec.gamemods_path, retval)
932 --------------------------------------------------------------------------------
933 function pkgmgr.get_game_modlist(gamespec)
936 pkgmgr.get_game_mods(gamespec, game_mods)
937 for i=1,#game_mods,1 do
941 retval = retval .. game_mods[i].name
946 --------------------------------------------------------------------------------
947 function pkgmgr.get_game(index)
948 if index > 0 and index <= #pkgmgr.games then
949 return pkgmgr.games[index]
955 --------------------------------------------------------------------------------
956 function pkgmgr.update_gamelist()
957 pkgmgr.games = core.get_games()
960 --------------------------------------------------------------------------------
961 function pkgmgr.gamelist()
963 if #pkgmgr.games > 0 then
964 retval = retval .. core.formspec_escape(pkgmgr.games[1].name)
966 for i=2,#pkgmgr.games,1 do
967 retval = retval .. "," .. core.formspec_escape(pkgmgr.games[i].name)
973 --------------------------------------------------------------------------------
975 --------------------------------------------------------------------------------
976 pkgmgr.update_gamelist()