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
26 retval[#retval + 1] = toadd
30 local modpack_conf = io.open(prefix .. DIR_DELIM .. "modpack.conf")
32 toadd.is_modpack = true
35 mod_conf = Settings(prefix .. DIR_DELIM .. "modpack.conf"):to_table()
40 mod_conf = Settings(prefix .. DIR_DELIM .. "mod.conf"):to_table()
48 toadd.author = mod_conf.author
49 toadd.release = tonumber(mod_conf.release or "0")
54 -- Note: modpack.conf is already checked above
55 local modpackfile = io.open(prefix .. DIR_DELIM .. "modpack.txt")
58 toadd.is_modpack = true
61 -- Deal with modpack contents
62 if modpack and modpack ~= "" then
63 toadd.modpack = modpack
64 elseif toadd.is_modpack then
65 toadd.type = "modpack"
66 toadd.is_modpack = true
67 get_mods(prefix, retval, name)
73 --modmanager implementation
76 function pkgmgr.get_texture_packs()
77 local txtpath = core.get_texturepath()
78 local list = core.get_dir_list(txtpath, true)
81 local current_texture_path = core.settings:get("texture_path")
83 for _, item in ipairs(list) do
84 if item ~= "base" then
87 local path = txtpath .. DIR_DELIM .. item .. DIR_DELIM
88 if path == current_texture_path then
89 name = fgettext("$1 (Enabled)", name)
92 local conf = Settings(path .. "texture_pack.conf")
94 retval[#retval + 1] = {
96 author = conf:get("author"),
97 release = tonumber(conf:get("release") or "0"),
101 enabled = path == current_texture_path,
106 table.sort(retval, function(a, b)
107 return a.name > b.name
113 --------------------------------------------------------------------------------
114 function pkgmgr.extract(modfile)
115 if modfile.type == "zip" then
116 local tempfolder = os.tempfolder()
118 if tempfolder ~= nil and
119 tempfolder ~= "" then
120 core.create_dir(tempfolder)
121 if core.extract_zip(modfile.name,tempfolder) then
129 function pkgmgr.get_folder_type(path)
130 local testfile = io.open(path .. DIR_DELIM .. "init.lua","r")
131 if testfile ~= nil then
133 return { type = "mod", path = path }
136 testfile = io.open(path .. DIR_DELIM .. "modpack.conf","r")
137 if testfile ~= nil then
139 return { type = "modpack", path = path }
142 testfile = io.open(path .. DIR_DELIM .. "modpack.txt","r")
143 if testfile ~= nil then
145 return { type = "modpack", path = path }
148 testfile = io.open(path .. DIR_DELIM .. "game.conf","r")
149 if testfile ~= nil then
151 return { type = "game", path = path }
154 testfile = io.open(path .. DIR_DELIM .. "texture_pack.conf","r")
155 if testfile ~= nil then
157 return { type = "txp", path = path }
163 -------------------------------------------------------------------------------
164 function pkgmgr.get_base_folder(temppath)
165 if temppath == nil then
166 return { type = "invalid", path = "" }
169 local ret = pkgmgr.get_folder_type(temppath)
174 local subdirs = core.get_dir_list(temppath, true)
175 if #subdirs == 1 then
176 ret = pkgmgr.get_folder_type(temppath .. DIR_DELIM .. subdirs[1])
180 return { type = "invalid", path = temppath .. DIR_DELIM .. subdirs[1] }
187 --------------------------------------------------------------------------------
188 function pkgmgr.isValidModname(modpath)
189 if modpath:find("-") ~= nil then
196 --------------------------------------------------------------------------------
197 function pkgmgr.parse_register_line(line)
198 local pos1 = line:find("\"")
201 pos2 = line:find("\"",pos1+1)
204 if pos1 ~= nil and pos2 ~= nil then
205 local item = line:sub(pos1+1,pos2-1)
209 local pos3 = item:find(":")
212 local retval = item:sub(1,pos3-1)
223 --------------------------------------------------------------------------------
224 function pkgmgr.parse_dofile_line(modpath,line)
225 local pos1 = line:find("\"")
228 pos2 = line:find("\"",pos1+1)
231 if pos1 ~= nil and pos2 ~= nil then
232 local filename = line:sub(pos1+1,pos2-1)
234 if filename ~= nil and
236 filename:find(".lua") then
237 return pkgmgr.identify_modname(modpath,filename)
243 --------------------------------------------------------------------------------
244 function pkgmgr.identify_modname(modpath,filename)
245 local testfile = io.open(modpath .. DIR_DELIM .. filename,"r")
246 if testfile ~= nil then
247 local line = testfile:read()
252 if line:find("minetest.register_tool") then
253 modname = pkgmgr.parse_register_line(line)
256 if line:find("minetest.register_craftitem") then
257 modname = pkgmgr.parse_register_line(line)
261 if line:find("minetest.register_node") then
262 modname = pkgmgr.parse_register_line(line)
265 if line:find("dofile") then
266 modname = pkgmgr.parse_dofile_line(modpath,line)
269 if modname ~= nil then
274 line = testfile:read()
281 --------------------------------------------------------------------------------
282 function pkgmgr.render_packagelist(render_list)
285 if render_list == nil then
286 if pkgmgr.global_mods == nil then
287 pkgmgr.refresh_globals()
289 render_list = pkgmgr.global_mods
292 local list = render_list:get_list()
293 local last_modpack = nil
295 for i, v in ipairs(list) do
298 local rawlist = render_list:get_raw_list()
299 color = mt_color_dark_green
301 for j = 1, #rawlist, 1 do
302 if rawlist[j].modpack == list[i].name and
303 rawlist[j].enabled ~= true then
304 -- Modpack not entirely enabled so showing as grey
305 color = mt_color_grey
309 elseif v.is_game_content or v.type == "game" then
310 color = mt_color_blue
311 elseif v.enabled or v.type == "txp" then
312 color = mt_color_green
315 retval[#retval + 1] = color
316 if v.modpack ~= nil or v.loc == "game" then
317 retval[#retval + 1] = "1"
319 retval[#retval + 1] = "0"
321 retval[#retval + 1] = core.formspec_escape(v.list_name or v.name)
324 return table.concat(retval, ",")
327 --------------------------------------------------------------------------------
328 function pkgmgr.get_dependencies(path)
333 local info = core.get_content_info(path)
334 return table.concat(info.depends or {}, ","), table.concat(info.optional_depends or {}, ",")
337 ----------- tests whether all of the mods in the modpack are enabled -----------
338 function pkgmgr.is_modpack_entirely_enabled(data, name)
339 local rawlist = data.list:get_raw_list()
340 for j = 1, #rawlist do
341 if rawlist[j].modpack == name and not rawlist[j].enabled then
348 ---------- toggles or en/disables a mod or modpack -----------------------------
349 function pkgmgr.enable_mod(this, toset)
350 local mod = this.data.list:get_list()[this.data.selected_mod]
352 -- game mods can't be enabled or disabled
353 if mod.is_game_content then
357 -- toggle or en/disable the mod
358 if not mod.is_modpack then
360 mod.enabled = not mod.enabled
367 -- toggle or en/disable every mod in the modpack, interleaved unsupported
368 local list = this.data.list:get_raw_list()
370 if list[i].modpack == mod.name then
372 toset = not list[i].enabled
374 list[i].enabled = toset
379 --------------------------------------------------------------------------------
380 function pkgmgr.get_worldconfig(worldpath)
381 local filename = worldpath ..
382 DIR_DELIM .. "world.mt"
384 local worldfile = Settings(filename)
386 local worldconfig = {}
387 worldconfig.global_mods = {}
388 worldconfig.game_mods = {}
390 for key,value in pairs(worldfile:to_table()) do
391 if key == "gameid" then
392 worldconfig.id = value
393 elseif key:sub(0, 9) == "load_mod_" then
394 -- Compatibility: Check against "nil" which was erroneously used
395 -- as value for fresh configured worlds
396 worldconfig.global_mods[key] = value ~= "false" and value ~= "nil"
399 worldconfig[key] = value
404 local gamespec = pkgmgr.find_by_gameid(worldconfig.id)
405 pkgmgr.get_game_mods(gamespec, worldconfig.game_mods)
410 --------------------------------------------------------------------------------
411 function pkgmgr.install_dir(type, path, basename, targetpath)
412 local basefolder = pkgmgr.get_base_folder(path)
414 -- There's no good way to detect a texture pack, so let's just assume
415 -- it's correct for now.
416 if type == "txp" then
417 if basefolder and basefolder.type ~= "invalid" and basefolder.type ~= "txp" then
418 return nil, fgettext("Unable to install a $1 as a texture pack", basefolder.type)
421 local from = basefolder and basefolder.path or path
423 core.delete_dir(targetpath)
424 core.create_dir(targetpath)
426 targetpath = core.get_texturepath() .. DIR_DELIM .. basename
428 if not core.copy_dir(from, targetpath) then
430 fgettext("Failed to install $1 to $2", basename, targetpath)
432 return targetpath, nil
434 elseif not basefolder then
435 return nil, fgettext("Unable to find a valid mod or modpack")
441 if basefolder.type == "modpack" then
442 if type ~= "mod" then
443 return nil, fgettext("Unable to install a modpack as a $1", type)
446 -- Get destination name for modpack
448 core.delete_dir(targetpath)
449 core.create_dir(targetpath)
451 local clean_path = nil
452 if basename ~= nil then
453 clean_path = basename
455 if not clean_path then
456 clean_path = get_last_folder(cleanup_path(basefolder.path))
459 targetpath = core.get_modpath() .. DIR_DELIM .. clean_path
462 fgettext("Install Mod: Unable to find suitable folder name for modpack $1",
466 elseif basefolder.type == "mod" then
467 if type ~= "mod" then
468 return nil, fgettext("Unable to install a mod as a $1", type)
472 core.delete_dir(targetpath)
473 core.create_dir(targetpath)
475 local targetfolder = basename
476 if targetfolder == nil then
477 targetfolder = pkgmgr.identify_modname(basefolder.path, "init.lua")
480 -- If heuristic failed try to use current foldername
481 if targetfolder == nil then
482 targetfolder = get_last_folder(basefolder.path)
485 if targetfolder ~= nil and pkgmgr.isValidModname(targetfolder) then
486 targetpath = core.get_modpath() .. DIR_DELIM .. targetfolder
488 return nil, fgettext("Install Mod: Unable to find real mod name for: $1", modfilename)
492 elseif basefolder.type == "game" then
493 if type ~= "game" then
494 return nil, fgettext("Unable to install a game as a $1", type)
498 core.delete_dir(targetpath)
499 core.create_dir(targetpath)
501 targetpath = core.get_gamepath() .. DIR_DELIM .. basename
506 if not core.copy_dir(basefolder.path, targetpath) then
508 fgettext("Failed to install $1 to $2", basename, targetpath)
511 if basefolder.type == "game" then
512 pkgmgr.update_gamelist()
514 pkgmgr.refresh_globals()
517 return targetpath, nil
520 --------------------------------------------------------------------------------
521 function pkgmgr.install(type, modfilename, basename, dest)
522 local archive_info = pkgmgr.identify_filetype(modfilename)
523 local path = pkgmgr.extract(archive_info)
527 fgettext("Install: file: \"$1\"", archive_info.name) .. "\n" ..
528 fgettext("Install: Unsupported file type \"$1\" or broken archive",
532 local targetpath, msg = pkgmgr.install_dir(type, path, basename, dest)
533 core.delete_dir(path)
534 return targetpath, msg
537 --------------------------------------------------------------------------------
538 function pkgmgr.preparemodlist(data)
541 local global_mods = {}
545 local modpath = core.get_modpath()
547 if modpath ~= nil and
549 get_mods(modpath,global_mods)
552 for i=1,#global_mods,1 do
553 global_mods[i].type = "mod"
554 global_mods[i].loc = "global"
555 retval[#retval + 1] = global_mods[i]
559 local gamespec = pkgmgr.find_by_gameid(data.gameid)
560 pkgmgr.get_game_mods(gamespec, game_mods)
562 if #game_mods > 0 then
564 retval[#retval + 1] = {
566 is_game_content = true,
567 name = fgettext(gamespec.name .. " mods"),
572 for i=1,#game_mods,1 do
573 game_mods[i].type = "mod"
574 game_mods[i].loc = "game"
575 game_mods[i].is_game_content = true
576 retval[#retval + 1] = game_mods[i]
579 if data.worldpath == nil then
583 --read world mod configuration
584 local filename = data.worldpath ..
585 DIR_DELIM .. "world.mt"
587 local worldfile = Settings(filename)
589 for key,value in pairs(worldfile:to_table()) do
590 if key:sub(1, 9) == "load_mod_" then
594 if retval[i].name == key and
595 not retval[i].is_modpack then
600 if element ~= nil then
601 element.enabled = value ~= "false" and value ~= "nil" and value
603 core.log("info", "Mod: " .. key .. " " .. dump(value) .. " but not found")
611 function pkgmgr.compare_package(a, b)
612 return a and b and a.name == b.name and a.path == b.path
615 --------------------------------------------------------------------------------
616 function pkgmgr.comparemod(elem1,elem2)
617 if elem1 == nil or elem2 == nil then
620 if elem1.name ~= elem2.name then
623 if elem1.is_modpack ~= elem2.is_modpack then
626 if elem1.type ~= elem2.type then
629 if elem1.modpack ~= elem2.modpack then
633 if elem1.path ~= elem2.path then
640 --------------------------------------------------------------------------------
641 function pkgmgr.mod_exists(basename)
643 if pkgmgr.global_mods == nil then
644 pkgmgr.refresh_globals()
647 if pkgmgr.global_mods:raw_index_by_uid(basename) > 0 then
654 --------------------------------------------------------------------------------
655 function pkgmgr.get_global_mod(idx)
657 if pkgmgr.global_mods == nil then
661 if idx == nil or idx < 1 or
662 idx > pkgmgr.global_mods:size() then
666 return pkgmgr.global_mods:get_list()[idx]
669 --------------------------------------------------------------------------------
670 function pkgmgr.refresh_globals()
671 local function is_equal(element,uid) --uid match
672 if element.name == uid then
676 pkgmgr.global_mods = filterlist.create(pkgmgr.preparemodlist,
677 pkgmgr.comparemod, is_equal, nil, {})
678 pkgmgr.global_mods:add_sort_mechanism("alphabetic", sort_mod_list)
679 pkgmgr.global_mods:set_sortmode("alphabetic")
682 --------------------------------------------------------------------------------
683 function pkgmgr.identify_filetype(name)
685 if name:sub(-3):lower() == "zip" then
692 if name:sub(-6):lower() == "tar.gz" or
693 name:sub(-3):lower() == "tgz"then
700 if name:sub(-6):lower() == "tar.bz2" then
707 if name:sub(-2):lower() == "7z" then
721 --------------------------------------------------------------------------------
722 function pkgmgr.find_by_gameid(gameid)
723 for i=1,#pkgmgr.games,1 do
724 if pkgmgr.games[i].id == gameid then
725 return pkgmgr.games[i], i
731 --------------------------------------------------------------------------------
732 function pkgmgr.get_game_mods(gamespec, retval)
733 if gamespec ~= nil and
734 gamespec.gamemods_path ~= nil and
735 gamespec.gamemods_path ~= "" then
736 get_mods(gamespec.gamemods_path, retval)
740 --------------------------------------------------------------------------------
741 function pkgmgr.get_game_modlist(gamespec)
744 pkgmgr.get_game_mods(gamespec, game_mods)
745 for i=1,#game_mods,1 do
749 retval = retval .. game_mods[i].name
754 --------------------------------------------------------------------------------
755 function pkgmgr.get_game(index)
756 if index > 0 and index <= #pkgmgr.games then
757 return pkgmgr.games[index]
763 --------------------------------------------------------------------------------
764 function pkgmgr.update_gamelist()
765 pkgmgr.games = core.get_games()
768 --------------------------------------------------------------------------------
769 function pkgmgr.gamelist()
771 if #pkgmgr.games > 0 then
772 retval = retval .. core.formspec_escape(pkgmgr.games[1].name)
774 for i=2,#pkgmgr.games,1 do
775 retval = retval .. "," .. core.formspec_escape(pkgmgr.games[i].name)
781 --------------------------------------------------------------------------------
783 --------------------------------------------------------------------------------
784 pkgmgr.update_gamelist()