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)
21 local mods = engine.get_dirlist(path,true)
24 local modpackfile = nil
27 toadd.path = path .. DIR_DELIM .. mods[i] .. DIR_DELIM
30 toadd.modpack = modpack
32 local filename = path .. DIR_DELIM .. mods[i] .. DIR_DELIM .. "modpack.txt"
34 modpackfile,error = io.open(filename,"r")
37 if modpackfile ~= nil then
39 toadd.is_modpack = true
40 table.insert(retval,toadd)
41 get_mods(path .. DIR_DELIM .. mods[i],retval,mods[i])
43 table.insert(retval,toadd)
48 --modmanager implementation
51 --------------------------------------------------------------------------------
52 function modmgr.extract(modfile)
53 if modfile.type == "zip" then
54 local tempfolder = os.tempfolder()
56 if tempfolder ~= nil and
58 engine.create_dir(tempfolder)
59 engine.extract_zip(modfile.name,tempfolder)
65 -------------------------------------------------------------------------------
66 function modmgr.getbasefolder(temppath)
68 if temppath == nil then
75 local testfile = io.open(temppath .. DIR_DELIM .. "init.lua","r")
76 if testfile ~= nil then
84 testfile = io.open(temppath .. DIR_DELIM .. "modpack.txt","r")
85 if testfile ~= nil then
93 local subdirs = engine.get_dirlist(temppath,true)
95 --only single mod or modpack allowed
104 io.open(temppath .. DIR_DELIM .. subdirs[1] ..DIR_DELIM .."init.lua","r")
105 if testfile ~= nil then
109 path= temppath .. DIR_DELIM .. subdirs[1]
114 io.open(temppath .. DIR_DELIM .. subdirs[1] ..DIR_DELIM .."modpack.txt","r")
115 if testfile ~= nil then
119 path=temppath .. DIR_DELIM .. subdirs[1]
129 --------------------------------------------------------------------------------
130 function modmgr.isValidModname(modpath)
131 if modpath:find("-") ~= nil then
138 --------------------------------------------------------------------------------
139 function modmgr.parse_register_line(line)
140 local pos1 = line:find("\"")
143 pos2 = line:find("\"",pos1+1)
146 if pos1 ~= nil and pos2 ~= nil then
147 local item = line:sub(pos1+1,pos2-1)
151 local pos3 = item:find(":")
154 return item:sub(1,pos3-1)
161 --------------------------------------------------------------------------------
162 function modmgr.parse_dofile_line(modpath,line)
163 local pos1 = line:find("\"")
166 pos2 = line:find("\"",pos1+1)
169 if pos1 ~= nil and pos2 ~= nil then
170 local filename = line:sub(pos1+1,pos2-1)
172 if filename ~= nil and
174 filename:find(".lua") then
175 return modmgr.identify_modname(modpath,filename)
181 --------------------------------------------------------------------------------
182 function modmgr.identify_modname(modpath,filename)
183 local testfile = io.open(modpath .. DIR_DELIM .. filename,"r")
184 if testfile ~= nil then
185 local line = testfile:read()
190 if line:find("minetest.register_tool") then
191 modname = modmgr.parse_register_line(line)
194 if line:find("minetest.register_craftitem") then
195 modname = modmgr.parse_register_line(line)
199 if line:find("minetest.register_node") then
200 modname = modmgr.parse_register_line(line)
203 if line:find("dofile") then
204 modname = modmgr.parse_dofile_line(modpath,line)
207 if modname ~= nil then
212 line = testfile:read()
220 --------------------------------------------------------------------------------
221 function modmgr.tab()
223 if modmgr.global_mods == nil then
224 modmgr.refresh_globals()
227 if modmgr.selected_mod == nil then
228 modmgr.selected_mod = 1
232 "vertlabel[0,-0.25;MODS]" ..
233 "label[0.8,-0.25;Installed Mods:]" ..
234 "textlist[0.75,0.25;4.5,4.3;modlist;" ..
235 modmgr.render_modlist(modmgr.global_mods) ..
236 ";" .. modmgr.selected_mod .. "]"
239 "button[1,4.85;2,0.5;btn_mod_mgr_install_local;Install]" ..
240 "button[3,4.85;2,0.5;btn_mod_mgr_download;Download]"
242 local selected_mod = nil
244 if filterlist.size(modmgr.global_mods) >= modmgr.selected_mod then
245 selected_mod = filterlist.get_list(modmgr.global_mods)[modmgr.selected_mod]
248 if selected_mod ~= nil then
249 if selected_mod.is_modpack then
250 retval = retval .. "button[10,4.85;2,0.5;btn_mod_mgr_rename_modpack;Rename]"
254 "label[6,1.9;Depends:]" ..
255 "textlist[6,2.4;5.7,2;deplist;"
257 toadd = modmgr.get_dependencies(selected_mod.path)
259 retval = retval .. toadd .. ";0;true,false]"
264 retval = retval .. "button[8,4.85;2,0.5;btn_mod_mgr_delete_mod;Delete]"
269 --------------------------------------------------------------------------------
270 function modmgr.dialog_rename_modpack()
272 local mod = filterlist.get_list(modmgr.modlist)[modmgr.selected_mod]
275 "label[1.75,1;Rename Modpack:]"..
276 "field[4.5,1.4;6,0.5;te_modpack_name;;" ..
279 "button[5,4.2;2.6,0.5;dlg_rename_modpack_confirm;Accept]" ..
280 "button[7.5,4.2;2.8,0.5;dlg_rename_modpack_cancel;Cancel]"
285 --------------------------------------------------------------------------------
286 function modmgr.precheck()
288 if modmgr.world_config_selected_world == nil then
289 modmgr.world_config_selected_world = 1
292 if modmgr.world_config_selected_mod == nil then
293 modmgr.world_config_selected_mod = 1
296 if modmgr.hide_gamemods == nil then
297 modmgr.hide_gamemods = true
300 if modmgr.hide_modpackcontents == nil then
301 modmgr.hide_modpackcontents = true
305 --------------------------------------------------------------------------------
306 function modmgr.render_modlist(render_list)
309 if render_list == nil then
310 if modmgr.global_mods == nil then
311 modmgr.refresh_globals()
313 render_list = modmgr.global_mods
316 local list = filterlist.get_list(render_list)
317 local last_modpack = nil
319 for i,v in ipairs(list) do
321 retval = retval ..","
325 local rawlist = filterlist.get_raw_list(render_list)
327 local all_enabled = true
328 for j=1,#rawlist,1 do
329 if rawlist[j].modpack == list[i].name and
330 rawlist[j].enabled ~= true then
336 if all_enabled == false then
337 retval = retval .. mt_color_grey
339 retval = retval .. mt_color_dark_green
343 if v.typ == "game_mod" then
344 retval = retval .. mt_color_blue
347 retval = retval .. mt_color_green
350 if v.modpack ~= nil then
351 retval = retval .. " "
353 retval = retval .. v.name
359 --------------------------------------------------------------------------------
360 function modmgr.dialog_configure_world()
363 local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
364 local mod = filterlist.get_list(modmgr.modlist)[modmgr.world_config_selected_mod]
368 "label[1.5,-0.25;World: " .. worldspec.name .. "]"
370 if modmgr.hide_gamemods then
371 retval = retval .. "checkbox[0,5.75;cb_hide_gamemods;Hide Game;true]"
373 retval = retval .. "checkbox[0,5.75;cb_hide_gamemods;Hide Game;false]"
376 if modmgr.hide_modpackcontents then
377 retval = retval .. "checkbox[2,5.75;cb_hide_mpcontent;Hide mp content;true]"
379 retval = retval .. "checkbox[2,5.75;cb_hide_mpcontent;Hide mp content;false]"
386 "label[0,0.45;Mod:]" ..
387 "label[0.75,0.45;" .. mod.name .. "]" ..
388 "label[0,1;Depends:]" ..
389 "textlist[0,1.5;5,4.25;world_config_depends;" ..
390 modmgr.get_dependencies(mod.path) .. ";0]" ..
391 "button[9.25,6.35;2,0.5;btn_config_world_save;Save]" ..
392 "button[7.4,6.35;2,0.5;btn_config_world_cancel;Cancel]" ..
393 "button[5.5,-0.125;5.75,0.5;btn_all_mods;Enable all Mods]" ..
394 "textlist[5.5,0.5;5.5,5.75;world_config_modlist;"
397 retval = retval .. modmgr.render_modlist(modmgr.modlist)
399 retval = retval .. ";" .. modmgr.world_config_selected_mod .."]"
404 --------------------------------------------------------------------------------
405 function modmgr.handle_buttons(tab,fields)
409 if tab == "mod_mgr" then
410 retval = modmgr.handle_modmgr_buttons(fields)
413 if tab == "dialog_rename_modpack" then
414 retval = modmgr.handle_rename_modpack_buttons(fields)
417 if tab == "dialog_delete_mod" then
418 retval = modmgr.handle_delete_mod_buttons(fields)
421 if tab == "dialog_configure_world" then
422 retval = modmgr.handle_configure_world_buttons(fields)
428 --------------------------------------------------------------------------------
429 function modmgr.get_dependencies(modfolder)
431 if modfolder ~= nil then
432 local filename = modfolder ..
433 DIR_DELIM .. "depends.txt"
435 local dependencyfile = io.open(filename,"r")
437 if dependencyfile then
438 local dependency = dependencyfile:read("*l")
443 toadd = toadd .. dependency
444 dependency = dependencyfile:read()
446 dependencyfile:close()
454 --------------------------------------------------------------------------------
455 function modmgr.get_worldconfig(worldpath)
456 local filename = worldpath ..
457 DIR_DELIM .. "world.mt"
459 local worldfile = io.open(filename,"r")
461 local worldconfig = {}
462 worldconfig.global_mods = {}
463 worldconfig.game_mods = {}
466 local dependency = worldfile:read("*l")
468 local parts = dependency:split("=")
470 local key = parts[1]:trim()
472 if key == "gameid" then
473 worldconfig.id = parts[2]:trim()
475 local key = parts[1]:trim():sub(10)
476 if parts[2]:trim() == "true" then
477 worldconfig.global_mods[key] = true
479 worldconfig.global_mods[key] = false
482 dependency = worldfile:read("*l")
486 print("Modmgr: " .. filename .. " not found")
490 local gamemodpath = engine.get_gamepath() .. DIR_DELIM .. worldconfig.id .. DIR_DELIM .. "mods"
492 get_mods(gamemodpath,worldconfig.game_mods)
496 --------------------------------------------------------------------------------
497 function modmgr.handle_modmgr_buttons(fields)
504 if fields["modlist"] ~= nil then
505 local event = explode_textlist_event(fields["modlist"])
506 modmgr.selected_mod = event.index
509 if fields["btn_mod_mgr_install_local"] ~= nil then
510 engine.show_file_open_dialog("mod_mgt_open_dlg","Select Mod File:")
513 if fields["btn_mod_mgr_download"] ~= nil then
514 retval.current_tab = "dialog_modstore_unsorted"
515 retval.is_dialog = true
516 retval.show_buttons = false
520 if fields["btn_mod_mgr_rename_modpack"] ~= nil then
521 retval.current_tab = "dialog_rename_modpack"
522 retval.is_dialog = true
523 retval.show_buttons = false
527 if fields["btn_mod_mgr_delete_mod"] ~= nil then
528 retval.current_tab = "dialog_delete_mod"
529 retval.is_dialog = true
530 retval.show_buttons = false
534 if fields["mod_mgt_open_dlg_accepted"] ~= nil and
535 fields["mod_mgt_open_dlg_accepted"] ~= "" then
536 modmgr.installmod(fields["mod_mgt_open_dlg_accepted"],nil)
542 --------------------------------------------------------------------------------
543 function modmgr.installmod(modfilename,basename)
544 local modfile = modmgr.identify_filetype(modfilename)
545 local modpath = modmgr.extract(modfile)
547 if modpath == nil then
548 gamedata.errormessage = "Install Mod: file: " .. modfile.name ..
549 "\nInstall Mod: unsupported filetype \"" .. modfile.type .. "\""
554 local basefolder = modmgr.getbasefolder(modpath)
556 if basefolder.type == "modpack" then
557 local clean_path = nil
559 if basename ~= nil then
560 clean_path = "mp_" .. basename
563 if clean_path == nil then
564 clean_path = get_last_folder(cleanup_path(basefolder.path))
567 if clean_path ~= nil then
568 local targetpath = engine.get_modpath() .. DIR_DELIM .. clean_path
569 if not engine.copy_dir(basefolder.path,targetpath) then
570 gamedata.errormessage = "Failed to install " .. basename .. " to " .. targetpath
573 gamedata.errormessage = "Install Mod: unable to find suitable foldername for modpack "
578 if basefolder.type == "mod" then
579 local targetfolder = basename
581 if targetfolder == nil then
582 targetfolder = modmgr.identify_modname(basefolder.path,"init.lua")
585 --if heuristic failed try to use current foldername
586 if targetfolder == nil then
587 targetfolder = get_last_folder(basefolder.path)
590 if targetfolder ~= nil and modmgr.isValidModname(targetfolder) then
591 local targetpath = engine.get_modpath() .. DIR_DELIM .. targetfolder
592 engine.copy_dir(basefolder.path,targetpath)
594 gamedata.errormessage = "Install Mod: unable to find real modname for: "
599 engine.delete_dir(modpath)
601 modmgr.refresh_globals()
605 --------------------------------------------------------------------------------
606 function modmgr.handle_rename_modpack_buttons(fields)
608 if fields["dlg_rename_modpack_confirm"] ~= nil then
609 local mod = filterlist.get_list(modmgr.modlist)[modmgr.selected_mod]
610 local oldpath = engine.get_modpath() .. DIR_DELIM .. mod.name
611 local targetpath = engine.get_modpath() .. DIR_DELIM .. fields["te_modpack_name"]
612 engine.copy_dir(oldpath,targetpath,false)
618 current_tab = engine.setting_get("main_menu_tab")
621 --------------------------------------------------------------------------------
622 function modmgr.handle_configure_world_buttons(fields)
623 if fields["world_config_modlist"] ~= nil then
624 local event = explode_textlist_event(fields["world_config_modlist"])
625 modmgr.world_config_selected_mod = event.index
627 if event.typ == "DCL" then
628 local mod = filterlist.get_list(modmgr.modlist)[event.index]
630 if mod.typ == "game_mod" then
634 if not mod.is_modpack then
635 mod.enabled = not mod.enabled
637 local list = filterlist.get_raw_list(modmgr.modlist)
641 if list[i].modpack == mod.name then
643 toset = not list[i].enabled
646 list[i].enabled = toset
653 if fields["cb_hide_gamemods"] ~= nil then
654 local current = filterlist.get_filtercriteria(modmgr.modlist)
656 if current == nil then
660 if fields["cb_hide_gamemods"] == "true" then
661 current.hide_game = true
662 modmgr.hide_gamemods = true
664 current.hide_game = false
665 modmgr.hide_gamemods = false
668 filterlist.set_filtercriteria(modmgr.modlist,current)
671 if fields["cb_hide_mpcontent"] ~= nil then
672 local current = filterlist.get_filtercriteria(modmgr.modlist)
674 if current == nil then
678 if fields["cb_hide_mpcontent"] == "true" then
679 current.hide_modpackcontents = true
680 modmgr.hide_modpackcontents = true
682 current.hide_modpackcontents = false
683 modmgr.hide_modpackcontents = false
686 filterlist.set_filtercriteria(modmgr.modlist,current)
689 if fields["btn_config_world_save"] then
690 local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
692 local filename = worldspec.path ..
693 DIR_DELIM .. "world.mt"
695 local worldfile = io.open(filename,"w")
698 worldfile:write("gameid = " .. modmgr.worldconfig.id .. "\n")
700 local rawlist = filterlist.get_raw_list(modmgr.modlist)
702 for i=1,#rawlist,1 do
704 if not rawlist[i].is_modpack and
705 rawlist[i].typ ~= "game_mod" then
706 if rawlist[i].enabled then
707 worldfile:write("load_mod_" .. rawlist[i].name .. " = true" .. "\n")
709 worldfile:write("load_mod_" .. rawlist[i].name .. " = false" .. "\n")
716 print("failed to open world config file")
720 modmgr.worldconfig = nil
725 current_tab = engine.setting_get("main_menu_tab")
729 if fields["btn_config_world_cancel"] then
731 modmgr.worldconfig = nil
736 current_tab = engine.setting_get("main_menu_tab")
740 if fields["btn_all_mods"] then
741 local list = filterlist.get_raw_list(modmgr.modlist)
744 if list[i].typ ~= "game_mod" and
745 not list[i].is_modpack then
746 list[i].enabled = true
755 --------------------------------------------------------------------------------
756 function modmgr.handle_delete_mod_buttons(fields)
757 local mod = filterlist.get_list(modmgr.global_mods)[modmgr.selected_mod]
759 if fields["dlg_delete_mod_confirm"] ~= nil then
761 if mod.path ~= nil and
763 mod.path ~= engine.get_modpath() then
764 if not engine.delete_dir(mod.path) then
765 gamedata.errormessage ="Modmgr: failed to delete >" .. mod.path .. "<"
767 modmgr.refresh_globals()
769 gamedata.errormessage ="Modmgr: invalid modpath >" .. mod.path .. "<"
776 current_tab = engine.setting_get("main_menu_tab")
780 --------------------------------------------------------------------------------
781 function modmgr.dialog_delete_mod()
783 local mod = filterlist.get_list(modmgr.global_mods)[modmgr.selected_mod]
786 "field[1.75,1;10,3;;Are you sure you want to delete ".. mod.name .. "?;]"..
787 "button[4,4.2;1,0.5;dlg_delete_mod_confirm;Yes]" ..
788 "button[6.5,4.2;3,0.5;dlg_delete_mod_cancel;No of course not!]"
793 --------------------------------------------------------------------------------
794 function modmgr.preparemodlist(data)
797 local global_mods = {}
801 local modpath = engine.get_modpath()
803 if modpath ~= nil and
805 get_mods(modpath,global_mods)
808 for i=1,#global_mods,1 do
809 global_mods[i].typ = "global_mod"
810 table.insert(retval,global_mods[i])
814 if data.gameid ~= nil and
815 data.gameid ~= "" then
816 local gamemodpath = engine.get_gamepath() .. DIR_DELIM .. data.gameid .. DIR_DELIM .. "mods"
818 get_mods(gamemodpath,game_mods)
821 for i=1,#game_mods,1 do
822 game_mods[i].typ = "game_mod"
823 table.insert(retval,game_mods[i])
826 if data.worldpath == nil then
830 --read world mod configuration
831 local filename = data.worldpath ..
832 DIR_DELIM .. "world.mt"
834 local worldfile = io.open(filename,"r")
836 local dependency = worldfile:read("*l")
838 local parts = dependency:split("=")
840 local key = parts[1]:trim()
842 if key ~= "gameid" then
843 local key = parts[1]:trim():sub(10)
846 if retval[i].name == key then
851 if element ~= nil then
852 if parts[2]:trim() == "true" then
853 element.enabled = true
855 element.enabled = false
858 print("Mod: " .. key .. " " .. dump(parts[2]) .. " but not found")
861 dependency = worldfile:read("*l")
870 --------------------------------------------------------------------------------
871 function modmgr.init_worldconfig()
873 local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
875 if worldspec ~= nil then
877 modmgr.worldconfig = modmgr.get_worldconfig(worldspec.path)
879 if modmgr.worldconfig.id == nil or
880 modmgr.worldconfig.id == "" then
881 modmgr.worldconfig = nil
885 modmgr.modlist = filterlist.create(
886 modmgr.preparemodlist, --refresh
887 modmgr.comparemod, --compare
888 function(element,uid) --uid match
889 if element.name == uid then
893 function(element,criteria)
894 if criteria.hide_game and
895 element.typ == "game_mod" then
899 if criteria.hide_modpackcontents and
900 element.modpack ~= nil then
905 { worldpath= worldspec.path,
906 gameid = worldspec.gameid }
909 filterlist.set_filtercriteria(modmgr.modlist, {
910 hide_game=modmgr.hide_gamemods,
911 hide_modpackcontents= modmgr.hide_modpackcontents
920 --------------------------------------------------------------------------------
921 function modmgr.comparemod(elem1,elem2)
922 if elem1 == nil or elem2 == nil then
925 if elem1.name ~= elem2.name then
928 if elem1.is_modpack ~= elem2.is_modpack then
931 if elem1.typ ~= elem2.typ then
934 if elem1.modpack ~= elem2.modpack then
938 if elem1.path ~= elem2.path then
945 --------------------------------------------------------------------------------
946 function modmgr.gettab(name)
949 if name == "mod_mgr" then
950 retval = retval .. modmgr.tab()
953 if name == "dialog_rename_modpack" then
954 retval = retval .. modmgr.dialog_rename_modpack()
957 if name == "dialog_delete_mod" then
958 retval = retval .. modmgr.dialog_delete_mod()
961 if name == "dialog_configure_world" then
962 retval = retval .. modmgr.dialog_configure_world()
968 --------------------------------------------------------------------------------
969 function modmgr.mod_exists(basename)
971 if modmgr.global_mods == nil then
972 modmgr.refresh_globals()
975 if filterlist.raw_index_by_uid(modmgr.global_mods,basename) > 0 then
982 --------------------------------------------------------------------------------
983 function modmgr.get_global_mod(idx)
985 if modmgr.global_mods == nil then
989 if idx < 1 or idx > filterlist.size(modmgr.global_mods) then
993 return filterlist.get_list(modmgr.global_mods)[idx]
996 --------------------------------------------------------------------------------
997 function modmgr.refresh_globals()
998 modmgr.global_mods = filterlist.create(
999 modmgr.preparemodlist, --refresh
1000 modmgr.comparemod, --compare
1001 function(element,uid) --uid match
1002 if element.name == uid then
1011 --------------------------------------------------------------------------------
1012 function modmgr.identify_filetype(name)
1014 if name:sub(-3):lower() == "zip" then
1021 if name:sub(-6):lower() == "tar.gz" or
1022 name:sub(-3):lower() == "tgz"then
1029 if name:sub(-6):lower() == "tar.bz2" then
1036 if name:sub(-2):lower() == "7z" then