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, virtual_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 mod_path = path .. DIR_DELIM .. name
109 local mod_virtual_path = virtual_path .. "/" .. name
114 retval[#retval + 1] = toadd
118 local modpack_conf = io.open(mod_path .. DIR_DELIM .. "modpack.conf")
120 toadd.is_modpack = true
123 mod_conf = Settings(mod_path .. DIR_DELIM .. "modpack.conf"):to_table()
124 if mod_conf.name then
126 toadd.is_name_explicit = true
129 mod_conf = Settings(mod_path .. DIR_DELIM .. "mod.conf"):to_table()
130 if mod_conf.name then
132 toadd.is_name_explicit = true
138 toadd.author = mod_conf.author
139 toadd.release = tonumber(mod_conf.release) or 0
140 toadd.path = mod_path
141 toadd.virtual_path = mod_virtual_path
145 -- Note: modpack.conf is already checked above
146 local modpackfile = io.open(mod_path .. DIR_DELIM .. "modpack.txt")
149 toadd.is_modpack = true
152 -- Deal with modpack contents
153 if modpack and modpack ~= "" then
154 toadd.modpack = modpack
155 elseif toadd.is_modpack then
156 toadd.type = "modpack"
157 toadd.is_modpack = true
158 get_mods(mod_path, mod_virtual_path, retval, name)
164 --modmanager implementation
167 function pkgmgr.get_texture_packs()
168 local txtpath = core.get_texturepath()
169 local txtpath_system = core.get_texturepath_share()
172 load_texture_packs(txtpath, retval)
173 -- on portable versions these two paths coincide. It avoids loading the path twice
174 if txtpath ~= txtpath_system then
175 load_texture_packs(txtpath_system, retval)
178 table.sort(retval, function(a, b)
179 return a.name > b.name
185 --------------------------------------------------------------------------------
186 function pkgmgr.get_folder_type(path)
187 local testfile = io.open(path .. DIR_DELIM .. "init.lua","r")
188 if testfile ~= nil then
190 return { type = "mod", path = path }
193 testfile = io.open(path .. DIR_DELIM .. "modpack.conf","r")
194 if testfile ~= nil then
196 return { type = "modpack", path = path }
199 testfile = io.open(path .. DIR_DELIM .. "modpack.txt","r")
200 if testfile ~= nil then
202 return { type = "modpack", path = path }
205 testfile = io.open(path .. DIR_DELIM .. "game.conf","r")
206 if testfile ~= nil then
208 return { type = "game", path = path }
211 testfile = io.open(path .. DIR_DELIM .. "texture_pack.conf","r")
212 if testfile ~= nil then
214 return { type = "txp", path = path }
220 -------------------------------------------------------------------------------
221 function pkgmgr.get_base_folder(temppath)
222 if temppath == nil then
223 return { type = "invalid", path = "" }
226 local ret = pkgmgr.get_folder_type(temppath)
231 local subdirs = core.get_dir_list(temppath, true)
232 if #subdirs == 1 then
233 ret = pkgmgr.get_folder_type(temppath .. DIR_DELIM .. subdirs[1])
237 return { type = "invalid", path = temppath .. DIR_DELIM .. subdirs[1] }
244 --------------------------------------------------------------------------------
245 function pkgmgr.isValidModname(modpath)
246 if modpath:find("-") ~= nil then
253 --------------------------------------------------------------------------------
254 function pkgmgr.parse_register_line(line)
255 local pos1 = line:find("\"")
258 pos2 = line:find("\"",pos1+1)
261 if pos1 ~= nil and pos2 ~= nil then
262 local item = line:sub(pos1+1,pos2-1)
266 local pos3 = item:find(":")
269 local retval = item:sub(1,pos3-1)
280 --------------------------------------------------------------------------------
281 function pkgmgr.parse_dofile_line(modpath,line)
282 local pos1 = line:find("\"")
285 pos2 = line:find("\"",pos1+1)
288 if pos1 ~= nil and pos2 ~= nil then
289 local filename = line:sub(pos1+1,pos2-1)
291 if filename ~= nil and
293 filename:find(".lua") then
294 return pkgmgr.identify_modname(modpath,filename)
300 --------------------------------------------------------------------------------
301 function pkgmgr.identify_modname(modpath,filename)
302 local testfile = io.open(modpath .. DIR_DELIM .. filename,"r")
303 if testfile ~= nil then
304 local line = testfile:read()
309 if line:find("minetest.register_tool") then
310 modname = pkgmgr.parse_register_line(line)
313 if line:find("minetest.register_craftitem") then
314 modname = pkgmgr.parse_register_line(line)
318 if line:find("minetest.register_node") then
319 modname = pkgmgr.parse_register_line(line)
322 if line:find("dofile") then
323 modname = pkgmgr.parse_dofile_line(modpath,line)
326 if modname ~= nil then
331 line = testfile:read()
338 --------------------------------------------------------------------------------
339 function pkgmgr.render_packagelist(render_list)
340 if not render_list then
341 if not pkgmgr.global_mods then
342 pkgmgr.refresh_globals()
344 render_list = pkgmgr.global_mods
347 local list = render_list:get_list()
349 for i, v in ipairs(list) do
352 local rawlist = render_list:get_raw_list()
353 color = mt_color_dark_green
355 for j = 1, #rawlist, 1 do
356 if rawlist[j].modpack == list[i].name and
357 not rawlist[j].enabled then
358 -- Modpack not entirely enabled so showing as grey
359 color = mt_color_grey
363 elseif v.is_game_content or v.type == "game" then
364 color = mt_color_blue
365 elseif v.enabled or v.type == "txp" then
366 color = mt_color_green
369 retval[#retval + 1] = color
370 if v.modpack ~= nil or v.loc == "game" then
371 retval[#retval + 1] = "1"
373 retval[#retval + 1] = "0"
375 retval[#retval + 1] = core.formspec_escape(v.list_name or v.name)
378 return table.concat(retval, ",")
381 --------------------------------------------------------------------------------
382 function pkgmgr.get_dependencies(path)
387 local info = core.get_content_info(path)
388 return info.depends or {}, info.optional_depends or {}
391 ----------- tests whether all of the mods in the modpack are enabled -----------
392 function pkgmgr.is_modpack_entirely_enabled(data, name)
393 local rawlist = data.list:get_raw_list()
394 for j = 1, #rawlist do
395 if rawlist[j].modpack == name and not rawlist[j].enabled then
402 local function disable_all_by_name(list, name, except)
404 if list[i].name == name and list[i] ~= except then
405 list[i].enabled = false
410 ---------- toggles or en/disables a mod or modpack and its dependencies --------
411 local function toggle_mod_or_modpack(list, toggled_mods, enabled_mods, toset, mod)
412 if not mod.is_modpack then
413 -- Toggle or en/disable the mod
415 toset = not mod.enabled
417 if mod.enabled ~= toset then
418 toggled_mods[#toggled_mods+1] = mod.name
421 -- Mark this mod for recursive dependency traversal
422 enabled_mods[mod.name] = true
424 -- Disable other mods with the same name
425 disable_all_by_name(list, mod.name, mod)
429 -- Toggle or en/disable every mod in the modpack,
430 -- interleaved unsupported
432 if list[i].modpack == mod.name then
433 toggle_mod_or_modpack(list, toggled_mods, enabled_mods, toset, list[i])
439 function pkgmgr.enable_mod(this, toset)
440 local list = this.data.list:get_list()
441 local mod = list[this.data.selected_mod]
443 -- Game mods can't be enabled or disabled
444 if mod.is_game_content then
448 local toggled_mods = {}
449 local enabled_mods = {}
450 toggle_mod_or_modpack(list, toggled_mods, enabled_mods, toset, mod)
451 toset = mod.enabled -- Update if toggled
454 -- Mod(s) were disabled, so no dependencies need to be enabled
455 table.sort(toggled_mods)
456 core.log("info", "Following mods were disabled: " ..
457 table.concat(toggled_mods, ", "))
461 -- Enable mods' depends after activation
463 -- Make a list of mod ids indexed by their names
465 for id, mod2 in pairs(list) do
466 if mod2.type == "mod" and not mod2.is_modpack then
467 mod_ids[mod2.name] = id
471 -- to_enable is used as a DFS stack with sp as stack pointer
474 for name in pairs(enabled_mods) do
475 local depends = pkgmgr.get_dependencies(list[mod_ids[name]].path)
476 for i = 1, #depends do
477 local dependency_name = depends[i]
478 if not enabled_mods[dependency_name] then
480 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 not mod_to_enable.enabled 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)
565 targetpath = core.get_texturepath() .. DIR_DELIM .. basename
567 if not core.copy_dir(from, targetpath, false) then
569 fgettext("Failed to install $1 to $2", basename, targetpath)
571 return targetpath, nil
573 elseif not basefolder then
574 return nil, fgettext("Unable to find a valid mod or modpack")
580 if basefolder.type == "modpack" then
581 if type ~= "mod" then
582 return nil, fgettext("Unable to install a modpack as a $1", type)
585 -- Get destination name for modpack
587 core.delete_dir(targetpath)
589 local clean_path = nil
590 if basename ~= nil then
591 clean_path = basename
593 if not clean_path then
594 clean_path = get_last_folder(cleanup_path(basefolder.path))
597 targetpath = core.get_modpath() .. DIR_DELIM .. clean_path
600 fgettext("Install Mod: Unable to find suitable folder name for modpack $1",
604 elseif basefolder.type == "mod" then
605 if type ~= "mod" then
606 return nil, fgettext("Unable to install a mod as a $1", type)
610 core.delete_dir(targetpath)
612 local targetfolder = basename
613 if targetfolder == nil then
614 targetfolder = pkgmgr.identify_modname(basefolder.path, "init.lua")
617 -- If heuristic failed try to use current foldername
618 if targetfolder == nil then
619 targetfolder = get_last_folder(basefolder.path)
622 if targetfolder ~= nil and pkgmgr.isValidModname(targetfolder) then
623 targetpath = core.get_modpath() .. DIR_DELIM .. targetfolder
625 return nil, fgettext("Install Mod: Unable to find real mod name for: $1", path)
629 elseif basefolder.type == "game" then
630 if type ~= "game" then
631 return nil, fgettext("Unable to install a game as a $1", type)
635 core.delete_dir(targetpath)
637 targetpath = core.get_gamepath() .. DIR_DELIM .. basename
642 if not core.copy_dir(basefolder.path, targetpath, false) then
644 fgettext("Failed to install $1 to $2", basename, targetpath)
647 if basefolder.type == "game" then
648 pkgmgr.update_gamelist()
650 pkgmgr.refresh_globals()
653 return targetpath, nil
656 --------------------------------------------------------------------------------
657 function pkgmgr.preparemodlist(data)
660 local global_mods = {}
664 local modpaths = core.get_modpaths()
665 for key, modpath in pairs(modpaths) do
666 get_mods(modpath, key, global_mods)
669 for i=1,#global_mods,1 do
670 global_mods[i].type = "mod"
671 global_mods[i].loc = "global"
672 global_mods[i].enabled = false
673 retval[#retval + 1] = global_mods[i]
677 local gamespec = pkgmgr.find_by_gameid(data.gameid)
678 pkgmgr.get_game_mods(gamespec, game_mods)
680 if #game_mods > 0 then
682 retval[#retval + 1] = {
684 is_game_content = true,
685 name = fgettext("$1 mods", gamespec.name),
690 for i=1,#game_mods,1 do
691 game_mods[i].type = "mod"
692 game_mods[i].loc = "game"
693 game_mods[i].is_game_content = true
694 retval[#retval + 1] = game_mods[i]
697 if data.worldpath == nil then
701 --read world mod configuration
702 local filename = data.worldpath ..
703 DIR_DELIM .. "world.mt"
705 local worldfile = Settings(filename)
706 for key, value in pairs(worldfile:to_table()) do
707 if key:sub(1, 9) == "load_mod_" then
709 local mod_found = false
711 local fallback_found = false
712 local fallback_mod = nil
715 if retval[i].name == key and
716 not retval[i].is_modpack then
717 if core.is_yes(value) or retval[i].virtual_path == value then
718 retval[i].enabled = true
721 elseif fallback_found then
722 -- Only allow fallback if only one mod matches
725 fallback_found = true
726 fallback_mod = retval[i]
731 if not mod_found then
732 if fallback_mod and value:find("/") then
733 fallback_mod.enabled = true
735 core.log("info", "Mod: " .. key .. " " .. dump(value) .. " but not found")
744 function pkgmgr.compare_package(a, b)
745 return a and b and a.name == b.name and a.path == b.path
748 --------------------------------------------------------------------------------
749 function pkgmgr.comparemod(elem1,elem2)
750 if elem1 == nil or elem2 == nil then
753 if elem1.name ~= elem2.name then
756 if elem1.is_modpack ~= elem2.is_modpack then
759 if elem1.type ~= elem2.type then
762 if elem1.modpack ~= elem2.modpack then
766 if elem1.path ~= elem2.path then
773 --------------------------------------------------------------------------------
774 function pkgmgr.mod_exists(basename)
776 if pkgmgr.global_mods == nil then
777 pkgmgr.refresh_globals()
780 if pkgmgr.global_mods:raw_index_by_uid(basename) > 0 then
787 --------------------------------------------------------------------------------
788 function pkgmgr.get_global_mod(idx)
790 if pkgmgr.global_mods == nil then
794 if idx == nil or idx < 1 or
795 idx > pkgmgr.global_mods:size() then
799 return pkgmgr.global_mods:get_list()[idx]
802 --------------------------------------------------------------------------------
803 function pkgmgr.refresh_globals()
804 local function is_equal(element,uid) --uid match
805 if element.name == uid then
809 pkgmgr.global_mods = filterlist.create(pkgmgr.preparemodlist,
810 pkgmgr.comparemod, is_equal, nil, {})
811 pkgmgr.global_mods:add_sort_mechanism("alphabetic", sort_mod_list)
812 pkgmgr.global_mods:set_sortmode("alphabetic")
815 --------------------------------------------------------------------------------
816 function pkgmgr.find_by_gameid(gameid)
817 for i=1,#pkgmgr.games,1 do
818 if pkgmgr.games[i].id == gameid then
819 return pkgmgr.games[i], i
825 --------------------------------------------------------------------------------
826 function pkgmgr.get_game_mods(gamespec, retval)
827 if gamespec ~= nil and
828 gamespec.gamemods_path ~= nil and
829 gamespec.gamemods_path ~= "" then
830 get_mods(gamespec.gamemods_path, ("games/%s/mods"):format(gamespec.id), retval)
834 --------------------------------------------------------------------------------
835 function pkgmgr.get_game_modlist(gamespec)
838 pkgmgr.get_game_mods(gamespec, game_mods)
839 for i=1,#game_mods,1 do
843 retval = retval .. game_mods[i].name
848 --------------------------------------------------------------------------------
849 function pkgmgr.get_game(index)
850 if index > 0 and index <= #pkgmgr.games then
851 return pkgmgr.games[index]
857 --------------------------------------------------------------------------------
858 function pkgmgr.update_gamelist()
859 pkgmgr.games = core.get_games()
862 --------------------------------------------------------------------------------
863 function pkgmgr.gamelist()
865 if #pkgmgr.games > 0 then
866 retval = retval .. core.formspec_escape(pkgmgr.games[1].name)
868 for i=2,#pkgmgr.games,1 do
869 retval = retval .. "," .. core.formspec_escape(pkgmgr.games[i].name)
875 --------------------------------------------------------------------------------
877 --------------------------------------------------------------------------------
878 pkgmgr.update_gamelist()