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.get_folder_type(path)
185 local testfile = io.open(path .. DIR_DELIM .. "init.lua","r")
186 if testfile ~= nil then
188 return { type = "mod", path = path }
191 testfile = io.open(path .. DIR_DELIM .. "modpack.conf","r")
192 if testfile ~= nil then
194 return { type = "modpack", path = path }
197 testfile = io.open(path .. DIR_DELIM .. "modpack.txt","r")
198 if testfile ~= nil then
200 return { type = "modpack", path = path }
203 testfile = io.open(path .. DIR_DELIM .. "game.conf","r")
204 if testfile ~= nil then
206 return { type = "game", path = path }
209 testfile = io.open(path .. DIR_DELIM .. "texture_pack.conf","r")
210 if testfile ~= nil then
212 return { type = "txp", path = path }
218 -------------------------------------------------------------------------------
219 function pkgmgr.get_base_folder(temppath)
220 if temppath == nil then
221 return { type = "invalid", path = "" }
224 local ret = pkgmgr.get_folder_type(temppath)
229 local subdirs = core.get_dir_list(temppath, true)
230 if #subdirs == 1 then
231 ret = pkgmgr.get_folder_type(temppath .. DIR_DELIM .. subdirs[1])
235 return { type = "invalid", path = temppath .. DIR_DELIM .. subdirs[1] }
242 --------------------------------------------------------------------------------
243 function pkgmgr.isValidModname(modpath)
244 if modpath:find("-") ~= nil then
251 --------------------------------------------------------------------------------
252 function pkgmgr.parse_register_line(line)
253 local pos1 = line:find("\"")
256 pos2 = line:find("\"",pos1+1)
259 if pos1 ~= nil and pos2 ~= nil then
260 local item = line:sub(pos1+1,pos2-1)
264 local pos3 = item:find(":")
267 local retval = item:sub(1,pos3-1)
278 --------------------------------------------------------------------------------
279 function pkgmgr.parse_dofile_line(modpath,line)
280 local pos1 = line:find("\"")
283 pos2 = line:find("\"",pos1+1)
286 if pos1 ~= nil and pos2 ~= nil then
287 local filename = line:sub(pos1+1,pos2-1)
289 if filename ~= nil and
291 filename:find(".lua") then
292 return pkgmgr.identify_modname(modpath,filename)
298 --------------------------------------------------------------------------------
299 function pkgmgr.identify_modname(modpath,filename)
300 local testfile = io.open(modpath .. DIR_DELIM .. filename,"r")
301 if testfile ~= nil then
302 local line = testfile:read()
307 if line:find("minetest.register_tool") then
308 modname = pkgmgr.parse_register_line(line)
311 if line:find("minetest.register_craftitem") then
312 modname = pkgmgr.parse_register_line(line)
316 if line:find("minetest.register_node") then
317 modname = pkgmgr.parse_register_line(line)
320 if line:find("dofile") then
321 modname = pkgmgr.parse_dofile_line(modpath,line)
324 if modname ~= nil then
329 line = testfile:read()
336 --------------------------------------------------------------------------------
337 function pkgmgr.render_packagelist(render_list)
338 if not render_list then
339 if not pkgmgr.global_mods then
340 pkgmgr.refresh_globals()
342 render_list = pkgmgr.global_mods
345 local list = render_list:get_list()
347 for i, v in ipairs(list) do
350 local rawlist = render_list:get_raw_list()
351 color = mt_color_dark_green
353 for j = 1, #rawlist, 1 do
354 if rawlist[j].modpack == list[i].name and
355 not rawlist[j].enabled then
356 -- Modpack not entirely enabled so showing as grey
357 color = mt_color_grey
361 elseif v.is_game_content or v.type == "game" then
362 color = mt_color_blue
363 elseif v.enabled or v.type == "txp" then
364 color = mt_color_green
367 retval[#retval + 1] = color
368 if v.modpack ~= nil or v.loc == "game" then
369 retval[#retval + 1] = "1"
371 retval[#retval + 1] = "0"
373 retval[#retval + 1] = core.formspec_escape(v.list_name or v.name)
376 return table.concat(retval, ",")
379 --------------------------------------------------------------------------------
380 function pkgmgr.get_dependencies(path)
385 local info = core.get_content_info(path)
386 return info.depends or {}, info.optional_depends or {}
389 ----------- tests whether all of the mods in the modpack are enabled -----------
390 function pkgmgr.is_modpack_entirely_enabled(data, name)
391 local rawlist = data.list:get_raw_list()
392 for j = 1, #rawlist do
393 if rawlist[j].modpack == name and not rawlist[j].enabled then
400 ---------- toggles or en/disables a mod or modpack and its dependencies --------
401 local function toggle_mod_or_modpack(list, toggled_mods, enabled_mods, toset, mod)
402 if not mod.is_modpack then
403 -- Toggle or en/disable the mod
405 toset = not mod.enabled
407 if mod.enabled ~= toset then
409 toggled_mods[#toggled_mods+1] = mod.name
412 -- Mark this mod for recursive dependency traversal
413 enabled_mods[mod.name] = true
416 -- Toggle or en/disable every mod in the modpack,
417 -- interleaved unsupported
419 if list[i].modpack == mod.name then
420 toggle_mod_or_modpack(list, toggled_mods, enabled_mods, toset, list[i])
426 function pkgmgr.enable_mod(this, toset)
427 local list = this.data.list:get_list()
428 local mod = list[this.data.selected_mod]
430 -- Game mods can't be enabled or disabled
431 if mod.is_game_content then
435 local toggled_mods = {}
436 local enabled_mods = {}
437 toggle_mod_or_modpack(list, toggled_mods, enabled_mods, toset, mod)
440 -- Mod(s) were disabled, so no dependencies need to be enabled
441 table.sort(toggled_mods)
442 core.log("info", "Following mods were disabled: " ..
443 table.concat(toggled_mods, ", "))
447 -- Enable mods' depends after activation
449 -- Make a list of mod ids indexed by their names
451 for id, mod2 in pairs(list) do
452 if mod2.type == "mod" and not mod2.is_modpack then
453 mod_ids[mod2.name] = id
457 -- to_enable is used as a DFS stack with sp as stack pointer
460 for name in pairs(enabled_mods) do
461 local depends = pkgmgr.get_dependencies(list[mod_ids[name]].path)
462 for i = 1, #depends do
463 local dependency_name = depends[i]
464 if not enabled_mods[dependency_name] then
466 to_enable[sp] = dependency_name
470 -- If sp is 0, every dependency is already activated
472 local name = to_enable[sp]
475 if not enabled_mods[name] then
476 enabled_mods[name] = true
477 local mod_to_enable = list[mod_ids[name]]
478 if not mod_to_enable then
479 core.log("warning", "Mod dependency \"" .. name ..
482 if mod_to_enable.enabled == false then
483 mod_to_enable.enabled = true
484 toggled_mods[#toggled_mods+1] = mod_to_enable.name
486 -- Push the dependencies of the dependency onto the stack
487 local depends = pkgmgr.get_dependencies(mod_to_enable.path)
488 for i = 1, #depends do
489 if not enabled_mods[name] then
491 to_enable[sp] = depends[i]
498 -- Log the list of enabled mods
499 table.sort(toggled_mods)
500 core.log("info", "Following mods were enabled: " ..
501 table.concat(toggled_mods, ", "))
504 --------------------------------------------------------------------------------
505 function pkgmgr.get_worldconfig(worldpath)
506 local filename = worldpath ..
507 DIR_DELIM .. "world.mt"
509 local worldfile = Settings(filename)
511 local worldconfig = {}
512 worldconfig.global_mods = {}
513 worldconfig.game_mods = {}
515 for key,value in pairs(worldfile:to_table()) do
516 if key == "gameid" then
517 worldconfig.id = value
518 elseif key:sub(0, 9) == "load_mod_" then
519 -- Compatibility: Check against "nil" which was erroneously used
520 -- as value for fresh configured worlds
521 worldconfig.global_mods[key] = value ~= "false" and value ~= "nil"
524 worldconfig[key] = value
529 local gamespec = pkgmgr.find_by_gameid(worldconfig.id)
530 pkgmgr.get_game_mods(gamespec, worldconfig.game_mods)
535 --------------------------------------------------------------------------------
536 function pkgmgr.install_dir(type, path, basename, targetpath)
537 local basefolder = pkgmgr.get_base_folder(path)
539 -- There's no good way to detect a texture pack, so let's just assume
540 -- it's correct for now.
541 if type == "txp" then
542 if basefolder and basefolder.type ~= "invalid" and basefolder.type ~= "txp" then
543 return nil, fgettext("Unable to install a $1 as a texture pack", basefolder.type)
546 local from = basefolder and basefolder.path or path
548 core.delete_dir(targetpath)
550 targetpath = core.get_texturepath() .. DIR_DELIM .. basename
552 if not core.copy_dir(from, targetpath, false) then
554 fgettext("Failed to install $1 to $2", basename, targetpath)
556 return targetpath, nil
558 elseif not basefolder then
559 return nil, fgettext("Unable to find a valid mod or modpack")
565 if basefolder.type == "modpack" then
566 if type ~= "mod" then
567 return nil, fgettext("Unable to install a modpack as a $1", type)
570 -- Get destination name for modpack
572 core.delete_dir(targetpath)
574 local clean_path = nil
575 if basename ~= nil then
576 clean_path = basename
578 if not clean_path then
579 clean_path = get_last_folder(cleanup_path(basefolder.path))
582 targetpath = core.get_modpath() .. DIR_DELIM .. clean_path
585 fgettext("Install Mod: Unable to find suitable folder name for modpack $1",
589 elseif basefolder.type == "mod" then
590 if type ~= "mod" then
591 return nil, fgettext("Unable to install a mod as a $1", type)
595 core.delete_dir(targetpath)
597 local targetfolder = basename
598 if targetfolder == nil then
599 targetfolder = pkgmgr.identify_modname(basefolder.path, "init.lua")
602 -- If heuristic failed try to use current foldername
603 if targetfolder == nil then
604 targetfolder = get_last_folder(basefolder.path)
607 if targetfolder ~= nil and pkgmgr.isValidModname(targetfolder) then
608 targetpath = core.get_modpath() .. DIR_DELIM .. targetfolder
610 return nil, fgettext("Install Mod: Unable to find real mod name for: $1", path)
614 elseif basefolder.type == "game" then
615 if type ~= "game" then
616 return nil, fgettext("Unable to install a game as a $1", type)
620 core.delete_dir(targetpath)
622 targetpath = core.get_gamepath() .. DIR_DELIM .. basename
627 if not core.copy_dir(basefolder.path, targetpath, false) then
629 fgettext("Failed to install $1 to $2", basename, targetpath)
632 if basefolder.type == "game" then
633 pkgmgr.update_gamelist()
635 pkgmgr.refresh_globals()
638 return targetpath, nil
641 --------------------------------------------------------------------------------
642 function pkgmgr.preparemodlist(data)
645 local global_mods = {}
649 local modpaths = core.get_modpaths()
650 for _, modpath in ipairs(modpaths) do
651 get_mods(modpath, global_mods)
654 for i=1,#global_mods,1 do
655 global_mods[i].type = "mod"
656 global_mods[i].loc = "global"
657 retval[#retval + 1] = global_mods[i]
661 local gamespec = pkgmgr.find_by_gameid(data.gameid)
662 pkgmgr.get_game_mods(gamespec, game_mods)
664 if #game_mods > 0 then
666 retval[#retval + 1] = {
668 is_game_content = true,
669 name = fgettext("$1 mods", gamespec.name),
674 for i=1,#game_mods,1 do
675 game_mods[i].type = "mod"
676 game_mods[i].loc = "game"
677 game_mods[i].is_game_content = true
678 retval[#retval + 1] = game_mods[i]
681 if data.worldpath == nil then
685 --read world mod configuration
686 local filename = data.worldpath ..
687 DIR_DELIM .. "world.mt"
689 local worldfile = Settings(filename)
691 for key,value in pairs(worldfile:to_table()) do
692 if key:sub(1, 9) == "load_mod_" then
696 if retval[i].name == key and
697 not retval[i].is_modpack then
702 if element ~= nil then
703 element.enabled = value ~= "false" and value ~= "nil" and value
705 core.log("info", "Mod: " .. key .. " " .. dump(value) .. " but not found")
713 function pkgmgr.compare_package(a, b)
714 return a and b and a.name == b.name and a.path == b.path
717 --------------------------------------------------------------------------------
718 function pkgmgr.comparemod(elem1,elem2)
719 if elem1 == nil or elem2 == nil then
722 if elem1.name ~= elem2.name then
725 if elem1.is_modpack ~= elem2.is_modpack then
728 if elem1.type ~= elem2.type then
731 if elem1.modpack ~= elem2.modpack then
735 if elem1.path ~= elem2.path then
742 --------------------------------------------------------------------------------
743 function pkgmgr.mod_exists(basename)
745 if pkgmgr.global_mods == nil then
746 pkgmgr.refresh_globals()
749 if pkgmgr.global_mods:raw_index_by_uid(basename) > 0 then
756 --------------------------------------------------------------------------------
757 function pkgmgr.get_global_mod(idx)
759 if pkgmgr.global_mods == nil then
763 if idx == nil or idx < 1 or
764 idx > pkgmgr.global_mods:size() then
768 return pkgmgr.global_mods:get_list()[idx]
771 --------------------------------------------------------------------------------
772 function pkgmgr.refresh_globals()
773 local function is_equal(element,uid) --uid match
774 if element.name == uid then
778 pkgmgr.global_mods = filterlist.create(pkgmgr.preparemodlist,
779 pkgmgr.comparemod, is_equal, nil, {})
780 pkgmgr.global_mods:add_sort_mechanism("alphabetic", sort_mod_list)
781 pkgmgr.global_mods:set_sortmode("alphabetic")
784 --------------------------------------------------------------------------------
785 function pkgmgr.find_by_gameid(gameid)
786 for i=1,#pkgmgr.games,1 do
787 if pkgmgr.games[i].id == gameid then
788 return pkgmgr.games[i], i
794 --------------------------------------------------------------------------------
795 function pkgmgr.get_game_mods(gamespec, retval)
796 if gamespec ~= nil and
797 gamespec.gamemods_path ~= nil and
798 gamespec.gamemods_path ~= "" then
799 get_mods(gamespec.gamemods_path, retval)
803 --------------------------------------------------------------------------------
804 function pkgmgr.get_game_modlist(gamespec)
807 pkgmgr.get_game_mods(gamespec, game_mods)
808 for i=1,#game_mods,1 do
812 retval = retval .. game_mods[i].name
817 --------------------------------------------------------------------------------
818 function pkgmgr.get_game(index)
819 if index > 0 and index <= #pkgmgr.games then
820 return pkgmgr.games[index]
826 --------------------------------------------------------------------------------
827 function pkgmgr.update_gamelist()
828 pkgmgr.games = core.get_games()
831 --------------------------------------------------------------------------------
832 function pkgmgr.gamelist()
834 if #pkgmgr.games > 0 then
835 retval = retval .. core.formspec_escape(pkgmgr.games[1].name)
837 for i=2,#pkgmgr.games,1 do
838 retval = retval .. "," .. core.formspec_escape(pkgmgr.games[i].name)
844 --------------------------------------------------------------------------------
846 --------------------------------------------------------------------------------
847 pkgmgr.update_gamelist()