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 worldconfig.global_mods[key] = core.is_yes(value)
396 worldconfig[key] = value
401 local gamespec = pkgmgr.find_by_gameid(worldconfig.id)
402 pkgmgr.get_game_mods(gamespec, worldconfig.game_mods)
407 --------------------------------------------------------------------------------
408 function pkgmgr.install_dir(type, path, basename, targetpath)
409 local basefolder = pkgmgr.get_base_folder(path)
411 -- There's no good way to detect a texture pack, so let's just assume
412 -- it's correct for now.
413 if type == "txp" then
414 if basefolder and basefolder.type ~= "invalid" and basefolder.type ~= "txp" then
415 return nil, fgettext("Unable to install a $1 as a texture pack", basefolder.type)
418 local from = basefolder and basefolder.path or path
420 core.delete_dir(targetpath)
421 core.create_dir(targetpath)
423 targetpath = core.get_texturepath() .. DIR_DELIM .. basename
425 if not core.copy_dir(from, targetpath) then
427 fgettext("Failed to install $1 to $2", basename, targetpath)
429 return targetpath, nil
431 elseif not basefolder then
432 return nil, fgettext("Unable to find a valid mod or modpack")
438 if basefolder.type == "modpack" then
439 if type ~= "mod" then
440 return nil, fgettext("Unable to install a modpack as a $1", type)
443 -- Get destination name for modpack
445 core.delete_dir(targetpath)
446 core.create_dir(targetpath)
448 local clean_path = nil
449 if basename ~= nil then
450 clean_path = basename
452 if not clean_path then
453 clean_path = get_last_folder(cleanup_path(basefolder.path))
456 targetpath = core.get_modpath() .. DIR_DELIM .. clean_path
459 fgettext("Install Mod: Unable to find suitable folder name for modpack $1",
463 elseif basefolder.type == "mod" then
464 if type ~= "mod" then
465 return nil, fgettext("Unable to install a mod as a $1", type)
469 core.delete_dir(targetpath)
470 core.create_dir(targetpath)
472 local targetfolder = basename
473 if targetfolder == nil then
474 targetfolder = pkgmgr.identify_modname(basefolder.path, "init.lua")
477 -- If heuristic failed try to use current foldername
478 if targetfolder == nil then
479 targetfolder = get_last_folder(basefolder.path)
482 if targetfolder ~= nil and pkgmgr.isValidModname(targetfolder) then
483 targetpath = core.get_modpath() .. DIR_DELIM .. targetfolder
485 return nil, fgettext("Install Mod: Unable to find real mod name for: $1", modfilename)
489 elseif basefolder.type == "game" then
490 if type ~= "game" then
491 return nil, fgettext("Unable to install a game as a $1", type)
495 core.delete_dir(targetpath)
496 core.create_dir(targetpath)
498 targetpath = core.get_gamepath() .. DIR_DELIM .. basename
503 if not core.copy_dir(basefolder.path, targetpath) then
505 fgettext("Failed to install $1 to $2", basename, targetpath)
508 if basefolder.type == "game" then
509 pkgmgr.update_gamelist()
511 pkgmgr.refresh_globals()
514 return targetpath, nil
517 --------------------------------------------------------------------------------
518 function pkgmgr.install(type, modfilename, basename, dest)
519 local archive_info = pkgmgr.identify_filetype(modfilename)
520 local path = pkgmgr.extract(archive_info)
524 fgettext("Install: file: \"$1\"", archive_info.name) .. "\n" ..
525 fgettext("Install: Unsupported file type \"$1\" or broken archive",
529 local targetpath, msg = pkgmgr.install_dir(type, path, basename, dest)
530 core.delete_dir(path)
531 return targetpath, msg
534 --------------------------------------------------------------------------------
535 function pkgmgr.preparemodlist(data)
538 local global_mods = {}
542 local modpath = core.get_modpath()
544 if modpath ~= nil and
546 get_mods(modpath,global_mods)
549 for i=1,#global_mods,1 do
550 global_mods[i].type = "mod"
551 global_mods[i].loc = "global"
552 retval[#retval + 1] = global_mods[i]
556 local gamespec = pkgmgr.find_by_gameid(data.gameid)
557 pkgmgr.get_game_mods(gamespec, game_mods)
559 if #game_mods > 0 then
561 retval[#retval + 1] = {
563 is_game_content = true,
564 name = fgettext(gamespec.name .. " mods"),
569 for i=1,#game_mods,1 do
570 game_mods[i].type = "mod"
571 game_mods[i].loc = "game"
572 game_mods[i].is_game_content = true
573 retval[#retval + 1] = game_mods[i]
576 if data.worldpath == nil then
580 --read world mod configuration
581 local filename = data.worldpath ..
582 DIR_DELIM .. "world.mt"
584 local worldfile = Settings(filename)
586 for key,value in pairs(worldfile:to_table()) do
587 if key:sub(1, 9) == "load_mod_" then
591 if retval[i].name == key and
592 not retval[i].is_modpack then
597 if element ~= nil then
598 element.enabled = core.is_yes(value)
600 core.log("info", "Mod: " .. key .. " " .. dump(value) .. " but not found")
608 function pkgmgr.compare_package(a, b)
609 return a and b and a.name == b.name and a.path == b.path
612 --------------------------------------------------------------------------------
613 function pkgmgr.comparemod(elem1,elem2)
614 if elem1 == nil or elem2 == nil then
617 if elem1.name ~= elem2.name then
620 if elem1.is_modpack ~= elem2.is_modpack then
623 if elem1.type ~= elem2.type then
626 if elem1.modpack ~= elem2.modpack then
630 if elem1.path ~= elem2.path then
637 --------------------------------------------------------------------------------
638 function pkgmgr.mod_exists(basename)
640 if pkgmgr.global_mods == nil then
641 pkgmgr.refresh_globals()
644 if pkgmgr.global_mods:raw_index_by_uid(basename) > 0 then
651 --------------------------------------------------------------------------------
652 function pkgmgr.get_global_mod(idx)
654 if pkgmgr.global_mods == nil then
658 if idx == nil or idx < 1 or
659 idx > pkgmgr.global_mods:size() then
663 return pkgmgr.global_mods:get_list()[idx]
666 --------------------------------------------------------------------------------
667 function pkgmgr.refresh_globals()
668 local function is_equal(element,uid) --uid match
669 if element.name == uid then
673 pkgmgr.global_mods = filterlist.create(pkgmgr.preparemodlist,
674 pkgmgr.comparemod, is_equal, nil, {})
675 pkgmgr.global_mods:add_sort_mechanism("alphabetic", sort_mod_list)
676 pkgmgr.global_mods:set_sortmode("alphabetic")
679 --------------------------------------------------------------------------------
680 function pkgmgr.identify_filetype(name)
682 if name:sub(-3):lower() == "zip" then
689 if name:sub(-6):lower() == "tar.gz" or
690 name:sub(-3):lower() == "tgz"then
697 if name:sub(-6):lower() == "tar.bz2" then
704 if name:sub(-2):lower() == "7z" then
718 --------------------------------------------------------------------------------
719 function pkgmgr.find_by_gameid(gameid)
720 for i=1,#pkgmgr.games,1 do
721 if pkgmgr.games[i].id == gameid then
722 return pkgmgr.games[i], i
728 --------------------------------------------------------------------------------
729 function pkgmgr.get_game_mods(gamespec, retval)
730 if gamespec ~= nil and
731 gamespec.gamemods_path ~= nil and
732 gamespec.gamemods_path ~= "" then
733 get_mods(gamespec.gamemods_path, retval)
737 --------------------------------------------------------------------------------
738 function pkgmgr.get_game_modlist(gamespec)
741 pkgmgr.get_game_mods(gamespec, game_mods)
742 for i=1,#game_mods,1 do
746 retval = retval .. game_mods[i].name
751 --------------------------------------------------------------------------------
752 function pkgmgr.get_game(index)
753 if index > 0 and index <= #pkgmgr.games then
754 return pkgmgr.games[index]
760 --------------------------------------------------------------------------------
761 function pkgmgr.update_gamelist()
762 pkgmgr.games = core.get_games()
765 --------------------------------------------------------------------------------
766 function pkgmgr.gamelist()
768 if #pkgmgr.games > 0 then
769 retval = retval .. core.formspec_escape(pkgmgr.games[1].name)
771 for i=2,#pkgmgr.games,1 do
772 retval = retval .. "," .. core.formspec_escape(pkgmgr.games[i].name)
778 --------------------------------------------------------------------------------
780 --------------------------------------------------------------------------------
781 pkgmgr.update_gamelist()