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
81 local path = txtpath .. DIR_DELIM .. item .. DIR_DELIM
82 local conf = Settings(path .. "texture_pack.conf")
83 local enabled = path == current_texture_path
85 local title = conf:get("title") or item
87 -- list_* is only used if non-nil, else the regular versions are used.
88 retval[#retval + 1] = {
91 list_name = enabled and fgettext("$1 (Enabled)", item) or nil,
92 list_title = enabled and fgettext("$1 (Enabled)", title) or nil,
93 author = conf:get("author"),
94 release = tonumber(conf:get("release")) or 0,
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.title = mod_conf.title
139 toadd.author = mod_conf.author
140 toadd.release = tonumber(mod_conf.release) or 0
141 toadd.path = mod_path
142 toadd.virtual_path = mod_virtual_path
146 -- Note: modpack.conf is already checked above
147 local modpackfile = io.open(mod_path .. DIR_DELIM .. "modpack.txt")
150 toadd.is_modpack = true
153 -- Deal with modpack contents
154 if modpack and modpack ~= "" then
155 toadd.modpack = modpack
156 elseif toadd.is_modpack then
157 toadd.type = "modpack"
158 toadd.is_modpack = true
159 get_mods(mod_path, mod_virtual_path, retval, name)
165 --modmanager implementation
168 function pkgmgr.get_texture_packs()
169 local txtpath = core.get_texturepath()
170 local txtpath_system = core.get_texturepath_share()
173 load_texture_packs(txtpath, retval)
174 -- on portable versions these two paths coincide. It avoids loading the path twice
175 if txtpath ~= txtpath_system then
176 load_texture_packs(txtpath_system, retval)
179 table.sort(retval, function(a, b)
180 return a.name > b.name
186 --------------------------------------------------------------------------------
187 function pkgmgr.get_folder_type(path)
188 local testfile = io.open(path .. DIR_DELIM .. "init.lua","r")
189 if testfile ~= nil then
191 return { type = "mod", path = path }
194 testfile = io.open(path .. DIR_DELIM .. "modpack.conf","r")
195 if testfile ~= nil then
197 return { type = "modpack", path = path }
200 testfile = io.open(path .. DIR_DELIM .. "modpack.txt","r")
201 if testfile ~= nil then
203 return { type = "modpack", path = path }
206 testfile = io.open(path .. DIR_DELIM .. "game.conf","r")
207 if testfile ~= nil then
209 return { type = "game", path = path }
212 testfile = io.open(path .. DIR_DELIM .. "texture_pack.conf","r")
213 if testfile ~= nil then
215 return { type = "txp", path = path }
221 -------------------------------------------------------------------------------
222 function pkgmgr.get_base_folder(temppath)
223 if temppath == nil then
224 return { type = "invalid", path = "" }
227 local ret = pkgmgr.get_folder_type(temppath)
232 local subdirs = core.get_dir_list(temppath, true)
233 if #subdirs == 1 then
234 ret = pkgmgr.get_folder_type(temppath .. DIR_DELIM .. subdirs[1])
238 return { type = "invalid", path = temppath .. DIR_DELIM .. subdirs[1] }
245 --------------------------------------------------------------------------------
246 function pkgmgr.isValidModname(modpath)
247 if modpath:find("-") ~= nil then
254 --------------------------------------------------------------------------------
255 function pkgmgr.parse_register_line(line)
256 local pos1 = line:find("\"")
259 pos2 = line:find("\"",pos1+1)
262 if pos1 ~= nil and pos2 ~= nil then
263 local item = line:sub(pos1+1,pos2-1)
267 local pos3 = item:find(":")
270 local retval = item:sub(1,pos3-1)
281 --------------------------------------------------------------------------------
282 function pkgmgr.parse_dofile_line(modpath,line)
283 local pos1 = line:find("\"")
286 pos2 = line:find("\"",pos1+1)
289 if pos1 ~= nil and pos2 ~= nil then
290 local filename = line:sub(pos1+1,pos2-1)
292 if filename ~= nil and
294 filename:find(".lua") then
295 return pkgmgr.identify_modname(modpath,filename)
301 --------------------------------------------------------------------------------
302 function pkgmgr.identify_modname(modpath,filename)
303 local testfile = io.open(modpath .. DIR_DELIM .. filename,"r")
304 if testfile ~= nil then
305 local line = testfile:read()
310 if line:find("minetest.register_tool") then
311 modname = pkgmgr.parse_register_line(line)
314 if line:find("minetest.register_craftitem") then
315 modname = pkgmgr.parse_register_line(line)
319 if line:find("minetest.register_node") then
320 modname = pkgmgr.parse_register_line(line)
323 if line:find("dofile") then
324 modname = pkgmgr.parse_dofile_line(modpath,line)
327 if modname ~= nil then
332 line = testfile:read()
339 --------------------------------------------------------------------------------
340 function pkgmgr.render_packagelist(render_list, use_technical_names)
341 if not render_list then
342 if not pkgmgr.global_mods then
343 pkgmgr.refresh_globals()
345 render_list = pkgmgr.global_mods
348 local list = render_list:get_list()
350 for i, v in ipairs(list) do
353 local rawlist = render_list:get_raw_list()
354 color = mt_color_dark_green
356 for j = 1, #rawlist, 1 do
357 if rawlist[j].modpack == list[i].name and
358 not rawlist[j].enabled then
359 -- Modpack not entirely enabled so showing as grey
360 color = mt_color_grey
364 elseif v.is_game_content or v.type == "game" then
365 color = mt_color_blue
366 elseif v.enabled or v.type == "txp" then
367 color = mt_color_green
370 retval[#retval + 1] = color
371 if v.modpack ~= nil or v.loc == "game" then
372 retval[#retval + 1] = "1"
374 retval[#retval + 1] = "0"
377 if use_technical_names then
378 retval[#retval + 1] = core.formspec_escape(v.list_name or v.name)
380 retval[#retval + 1] = core.formspec_escape(v.list_title or v.list_name or v.title or v.name)
384 return table.concat(retval, ",")
387 --------------------------------------------------------------------------------
388 function pkgmgr.get_dependencies(path)
393 local info = core.get_content_info(path)
394 return info.depends or {}, info.optional_depends or {}
397 ----------- tests whether all of the mods in the modpack are enabled -----------
398 function pkgmgr.is_modpack_entirely_enabled(data, name)
399 local rawlist = data.list:get_raw_list()
400 for j = 1, #rawlist do
401 if rawlist[j].modpack == name and not rawlist[j].enabled then
408 local function disable_all_by_name(list, name, except)
410 if list[i].name == name and list[i] ~= except then
411 list[i].enabled = false
416 ---------- toggles or en/disables a mod or modpack and its dependencies --------
417 local function toggle_mod_or_modpack(list, toggled_mods, enabled_mods, toset, mod)
418 if not mod.is_modpack then
419 -- Toggle or en/disable the mod
421 toset = not mod.enabled
423 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
430 -- Disable other mods with the same name
431 disable_all_by_name(list, mod.name, mod)
435 -- Toggle or en/disable every mod in the modpack,
436 -- interleaved unsupported
438 if list[i].modpack == mod.name then
439 toggle_mod_or_modpack(list, toggled_mods, enabled_mods, toset, list[i])
445 function pkgmgr.enable_mod(this, toset)
446 local list = this.data.list:get_list()
447 local mod = list[this.data.selected_mod]
449 -- Game mods can't be enabled or disabled
450 if mod.is_game_content then
454 local toggled_mods = {}
455 local enabled_mods = {}
456 toggle_mod_or_modpack(list, toggled_mods, enabled_mods, toset, mod)
458 if next(enabled_mods) == nil then
459 -- Mod(s) were disabled, so no dependencies need to be enabled
460 table.sort(toggled_mods)
461 core.log("info", "Following mods were disabled: " ..
462 table.concat(toggled_mods, ", "))
466 -- Enable mods' depends after activation
468 -- Make a list of mod ids indexed by their names. Among mods with the
469 -- same name, enabled mods take precedence, after which game mods take
470 -- precedence, being last in the mod list.
472 for id, mod2 in pairs(list) do
473 if mod2.type == "mod" and not mod2.is_modpack then
474 local prev_id = mod_ids[mod2.name]
475 if not prev_id or not list[prev_id].enabled then
476 mod_ids[mod2.name] = id
481 -- to_enable is used as a DFS stack with sp as stack pointer
484 for name in pairs(enabled_mods) do
485 local depends = pkgmgr.get_dependencies(list[mod_ids[name]].path)
486 for i = 1, #depends do
487 local dependency_name = depends[i]
488 if not enabled_mods[dependency_name] then
490 to_enable[sp] = dependency_name
495 -- If sp is 0, every dependency is already activated
497 local name = to_enable[sp]
500 if not enabled_mods[name] then
501 enabled_mods[name] = true
502 local mod_to_enable = list[mod_ids[name]]
503 if not mod_to_enable then
504 core.log("warning", "Mod dependency \"" .. name ..
507 if not mod_to_enable.enabled then
508 mod_to_enable.enabled = true
509 toggled_mods[#toggled_mods+1] = mod_to_enable.name
511 -- Push the dependencies of the dependency onto the stack
512 local depends = pkgmgr.get_dependencies(mod_to_enable.path)
513 for i = 1, #depends do
514 if not enabled_mods[depends[i]] then
516 to_enable[sp] = depends[i]
523 -- Log the list of enabled mods
524 table.sort(toggled_mods)
525 core.log("info", "Following mods were enabled: " ..
526 table.concat(toggled_mods, ", "))
529 --------------------------------------------------------------------------------
530 function pkgmgr.get_worldconfig(worldpath)
531 local filename = worldpath ..
532 DIR_DELIM .. "world.mt"
534 local worldfile = Settings(filename)
536 local worldconfig = {}
537 worldconfig.global_mods = {}
538 worldconfig.game_mods = {}
540 for key,value in pairs(worldfile:to_table()) do
541 if key == "gameid" then
542 worldconfig.id = value
543 elseif key:sub(0, 9) == "load_mod_" then
544 -- Compatibility: Check against "nil" which was erroneously used
545 -- as value for fresh configured worlds
546 worldconfig.global_mods[key] = value ~= "false" and value ~= "nil"
549 worldconfig[key] = value
554 local gamespec = pkgmgr.find_by_gameid(worldconfig.id)
555 pkgmgr.get_game_mods(gamespec, worldconfig.game_mods)
560 --------------------------------------------------------------------------------
561 function pkgmgr.install_dir(type, path, basename, targetpath)
562 local basefolder = pkgmgr.get_base_folder(path)
564 -- There's no good way to detect a texture pack, so let's just assume
565 -- it's correct for now.
566 if type == "txp" then
567 if basefolder and basefolder.type ~= "invalid" and basefolder.type ~= "txp" then
568 return nil, fgettext("Unable to install a $1 as a texture pack", basefolder.type)
571 local from = basefolder and basefolder.path or path
573 core.delete_dir(targetpath)
575 targetpath = core.get_texturepath() .. DIR_DELIM .. basename
577 if not core.copy_dir(from, targetpath, false) then
579 fgettext("Failed to install $1 to $2", basename, targetpath)
581 return targetpath, nil
583 elseif not basefolder then
584 return nil, fgettext("Unable to find a valid mod or modpack")
590 if basefolder.type == "modpack" then
591 if type ~= "mod" then
592 return nil, fgettext("Unable to install a modpack as a $1", type)
595 -- Get destination name for modpack
597 core.delete_dir(targetpath)
599 local clean_path = nil
600 if basename ~= nil then
601 clean_path = basename
603 if not clean_path then
604 clean_path = get_last_folder(cleanup_path(basefolder.path))
607 targetpath = core.get_modpath() .. DIR_DELIM .. clean_path
610 fgettext("Install Mod: Unable to find suitable folder name for modpack $1",
614 elseif basefolder.type == "mod" then
615 if type ~= "mod" then
616 return nil, fgettext("Unable to install a mod as a $1", type)
620 core.delete_dir(targetpath)
622 local targetfolder = basename
623 if targetfolder == nil then
624 targetfolder = pkgmgr.identify_modname(basefolder.path, "init.lua")
627 -- If heuristic failed try to use current foldername
628 if targetfolder == nil then
629 targetfolder = get_last_folder(basefolder.path)
632 if targetfolder ~= nil and pkgmgr.isValidModname(targetfolder) then
633 targetpath = core.get_modpath() .. DIR_DELIM .. targetfolder
635 return nil, fgettext("Install Mod: Unable to find real mod name for: $1", path)
639 elseif basefolder.type == "game" then
640 if type ~= "game" then
641 return nil, fgettext("Unable to install a game as a $1", type)
645 core.delete_dir(targetpath)
647 targetpath = core.get_gamepath() .. DIR_DELIM .. basename
650 error("basefolder didn't return a recognised type, this shouldn't happen")
654 if not core.copy_dir(basefolder.path, targetpath, false) then
656 fgettext("Failed to install $1 to $2", basename, targetpath)
659 if basefolder.type == "game" then
660 pkgmgr.update_gamelist()
662 pkgmgr.refresh_globals()
665 return targetpath, nil
668 --------------------------------------------------------------------------------
669 function pkgmgr.preparemodlist(data)
672 local global_mods = {}
676 local modpaths = core.get_modpaths()
677 for key, modpath in pairs(modpaths) do
678 get_mods(modpath, key, global_mods)
681 for i=1,#global_mods,1 do
682 global_mods[i].type = "mod"
683 global_mods[i].loc = "global"
684 global_mods[i].enabled = false
685 retval[#retval + 1] = global_mods[i]
689 local gamespec = pkgmgr.find_by_gameid(data.gameid)
690 pkgmgr.get_game_mods(gamespec, game_mods)
692 if #game_mods > 0 then
694 retval[#retval + 1] = {
696 is_game_content = true,
697 name = fgettext("$1 mods", gamespec.title),
702 for i=1,#game_mods,1 do
703 game_mods[i].type = "mod"
704 game_mods[i].loc = "game"
705 game_mods[i].is_game_content = true
706 retval[#retval + 1] = game_mods[i]
709 if data.worldpath == nil then
713 --read world mod configuration
714 local filename = data.worldpath ..
715 DIR_DELIM .. "world.mt"
717 local worldfile = Settings(filename)
718 for key, value in pairs(worldfile:to_table()) do
719 if key:sub(1, 9) == "load_mod_" then
721 local mod_found = false
723 local fallback_found = false
724 local fallback_mod = nil
727 if retval[i].name == key and
728 not retval[i].is_modpack then
729 if core.is_yes(value) or retval[i].virtual_path == value then
730 retval[i].enabled = true
733 elseif fallback_found then
734 -- Only allow fallback if only one mod matches
737 fallback_found = true
738 fallback_mod = retval[i]
743 if not mod_found then
744 if fallback_mod and value:find("/") then
745 fallback_mod.enabled = true
747 core.log("info", "Mod: " .. key .. " " .. dump(value) .. " but not found")
756 function pkgmgr.compare_package(a, b)
757 return a and b and a.name == b.name and a.path == b.path
760 --------------------------------------------------------------------------------
761 function pkgmgr.comparemod(elem1,elem2)
762 if elem1 == nil or elem2 == nil then
765 if elem1.name ~= elem2.name then
768 if elem1.is_modpack ~= elem2.is_modpack then
771 if elem1.type ~= elem2.type then
774 if elem1.modpack ~= elem2.modpack then
778 if elem1.path ~= elem2.path then
785 --------------------------------------------------------------------------------
786 function pkgmgr.mod_exists(basename)
788 if pkgmgr.global_mods == nil then
789 pkgmgr.refresh_globals()
792 if pkgmgr.global_mods:raw_index_by_uid(basename) > 0 then
799 --------------------------------------------------------------------------------
800 function pkgmgr.get_global_mod(idx)
802 if pkgmgr.global_mods == nil then
806 if idx == nil or idx < 1 or
807 idx > pkgmgr.global_mods:size() then
811 return pkgmgr.global_mods:get_list()[idx]
814 --------------------------------------------------------------------------------
815 function pkgmgr.refresh_globals()
816 local function is_equal(element,uid) --uid match
817 if element.name == uid then
821 pkgmgr.global_mods = filterlist.create(pkgmgr.preparemodlist,
822 pkgmgr.comparemod, is_equal, nil, {})
823 pkgmgr.global_mods:add_sort_mechanism("alphabetic", sort_mod_list)
824 pkgmgr.global_mods:set_sortmode("alphabetic")
827 --------------------------------------------------------------------------------
828 function pkgmgr.find_by_gameid(gameid)
829 for i=1,#pkgmgr.games,1 do
830 if pkgmgr.games[i].id == gameid then
831 return pkgmgr.games[i], i
837 --------------------------------------------------------------------------------
838 function pkgmgr.get_game_mods(gamespec, retval)
839 if gamespec ~= nil and
840 gamespec.gamemods_path ~= nil and
841 gamespec.gamemods_path ~= "" then
842 get_mods(gamespec.gamemods_path, ("games/%s/mods"):format(gamespec.id), retval)
846 --------------------------------------------------------------------------------
847 function pkgmgr.get_game_modlist(gamespec)
850 pkgmgr.get_game_mods(gamespec, game_mods)
851 for i=1,#game_mods,1 do
855 retval = retval .. game_mods[i].name
860 --------------------------------------------------------------------------------
861 function pkgmgr.get_game(index)
862 if index > 0 and index <= #pkgmgr.games then
863 return pkgmgr.games[index]
869 --------------------------------------------------------------------------------
870 function pkgmgr.update_gamelist()
871 pkgmgr.games = core.get_games()
874 --------------------------------------------------------------------------------
875 function pkgmgr.gamelist()
877 if #pkgmgr.games > 0 then
878 retval = retval .. core.formspec_escape(pkgmgr.games[1].title)
880 for i=2,#pkgmgr.games,1 do
881 retval = retval .. "," .. core.formspec_escape(pkgmgr.games[i].title)
887 --------------------------------------------------------------------------------
889 --------------------------------------------------------------------------------
890 pkgmgr.update_gamelist()