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,basefolder)
21 local mods = engine.get_dirlist(path,true)
24 local filename = path .. DIR_DELIM .. mods[i] .. DIR_DELIM .. "modpack.txt"
25 local modpackfile,error = io.open(filename,"r")
28 if basefolder ~= nil and
30 name = basefolder .. DIR_DELIM .. mods[i]
33 if modpackfile ~= nil then
35 table.insert(retval,name .. " <MODPACK>")
36 get_mods(path .. DIR_DELIM .. name,retval,name)
39 table.insert(retval,name)
44 --modmanager implementation
47 --------------------------------------------------------------------------------
48 function modmgr.extract(modfile)
49 if modfile.type == "zip" then
50 local tempfolder = os.tempfolder()
52 if tempfolder ~= nil and
54 engine.create_dir(tempfolder)
55 engine.extract_zip(modfile.name,tempfolder)
61 -------------------------------------------------------------------------------
62 function modmgr.getbasefolder(temppath)
64 if temppath == nil then
71 local testfile = io.open(temppath .. DIR_DELIM .. "init.lua","r")
72 if testfile ~= nil then
80 testfile = io.open(temppath .. DIR_DELIM .. "modpack.txt","r")
81 if testfile ~= nil then
89 local subdirs = engine.get_dirlist(temppath,true)
91 --only single mod or modpack allowed
100 io.open(temppath .. DIR_DELIM .. subdirs[1] ..DIR_DELIM .."init.lua","r")
101 if testfile ~= nil then
105 path= temppath .. DIR_DELIM .. subdirs[1]
110 io.open(temppath .. DIR_DELIM .. subdirs[1] ..DIR_DELIM .."modpack.txt","r")
111 if testfile ~= nil then
115 path=temppath .. DIR_DELIM .. subdirs[1]
125 --------------------------------------------------------------------------------
126 function modmgr.isValidModname(modpath)
127 if modpath:find("-") ~= nil then
134 --------------------------------------------------------------------------------
135 function modmgr.parse_register_line(line)
136 local pos1 = line:find("\"")
139 pos2 = line:find("\"",pos1+1)
142 if pos1 ~= nil and pos2 ~= nil then
143 local item = line:sub(pos1+1,pos2-1)
147 local pos3 = item:find(":")
150 return item:sub(1,pos3-1)
157 --------------------------------------------------------------------------------
158 function modmgr.parse_dofile_line(modpath,line)
159 local pos1 = line:find("\"")
162 pos2 = line:find("\"",pos1+1)
165 if pos1 ~= nil and pos2 ~= nil then
166 local filename = line:sub(pos1+1,pos2-1)
168 if filename ~= nil and
170 filename:find(".lua") then
171 return modmgr.identify_modname(modpath,filename)
177 --------------------------------------------------------------------------------
178 function modmgr.update_global_mods()
179 local modpath = engine.get_modpath()
180 modmgr.global_mods = {}
181 if modpath ~= nil and
183 get_mods(modpath,modmgr.global_mods)
187 --------------------------------------------------------------------------------
188 function modmgr.get_mods_list()
191 modmgr.update_global_mods()
193 if modmgr.global_mods ~= nil then
194 for i=1,#modmgr.global_mods,1 do
198 toadd = toadd .. modmgr.global_mods[i]
205 --------------------------------------------------------------------------------
206 function modmgr.mod_exists(basename)
207 modmgr.update_global_mods()
209 if modmgr.global_mods ~= nil then
210 for i=1,#modmgr.global_mods,1 do
211 if modmgr.global_mods[i] == basename then
220 --------------------------------------------------------------------------------
221 function modmgr.identify_modname(modpath,filename)
222 local testfile = io.open(modpath .. DIR_DELIM .. filename,"r")
223 if testfile ~= nil then
224 local line = testfile:read()
229 if line:find("minetest.register_tool") then
230 modname = modmgr.parse_register_line(line)
233 if line:find("minetest.register_craftitem") then
234 modname = modmgr.parse_register_line(line)
238 if line:find("minetest.register_node") then
239 modname = modmgr.parse_register_line(line)
242 if line:find("dofile") then
243 modname = modmgr.parse_dofile_line(modpath,line)
246 if modname ~= nil then
251 line = testfile:read()
259 --------------------------------------------------------------------------------
260 function modmgr.tab()
261 if modmgr.selected_mod == nil then
262 modmgr.selected_mod = 1
266 "vertlabel[0,-0.25;MODS]" ..
267 "label[0.8,-0.25;Installed Mods:]" ..
268 "textlist[0.75,0.25;4.5,4.3;modlist;" ..
269 modmgr.get_mods_list() ..
270 ";" .. modmgr.selected_mod .. "]"
273 "button[1,4.85;2,0.5;btn_mod_mgr_install_local;Install]" ..
274 "button[3,4.85;2,0.5;btn_mod_mgr_download;Download]"
276 if #modmgr.global_mods >= modmgr.selected_mod and
277 modmgr.global_mods[modmgr.selected_mod]:find("<MODPACK>") then
278 retval = retval .. "button[10,4.85;2,0.5;btn_mod_mgr_rename_modpack;Rename]"
281 if #modmgr.global_mods >= modmgr.selected_mod then
282 local modpath = engine.get_modpath()
284 if modmgr.global_mods[modmgr.selected_mod]:find("<MODPACK>") == nil then
286 "label[6,1.9;Depends:]" ..
287 "textlist[6,2.4;5.7,2;deplist;"
289 toadd = modmgr.get_dependencys(modpath .. DIR_DELIM ..
290 modmgr.global_mods[modmgr.selected_mod])
292 retval = retval .. toadd .. ";0;true,false]"
297 retval = retval .. "button[8,4.85;2,0.5;btn_mod_mgr_delete_mod;Delete]"
302 --------------------------------------------------------------------------------
303 function modmgr.dialog_rename_modpack()
305 local modname = modmgr.global_mods[modmgr.selected_mod]
306 modname = modname:sub(0,modname:find("<") -2)
309 "label[1.75,1;Rename Modpack:]"..
310 "field[4.5,1.4;6,0.5;te_modpack_name;;" ..
313 "button[5,4.2;2.6,0.5;dlg_rename_modpack_confirm;Accept]" ..
314 "button[7.5,4.2;2.8,0.5;dlg_rename_modpack_cancel;Cancel]"
319 --------------------------------------------------------------------------------
320 function modmgr.precheck()
321 if modmgr.global_mods == nil then
322 modmgr.update_global_mods()
325 if modmgr.world_config_selected_world == nil then
326 modmgr.world_config_selected_world = 1
329 if modmgr.world_config_selected_mod == nil then
330 modmgr.world_config_selected_mod = 1
333 if modmgr.hide_gamemods == nil then
334 modmgr.hide_gamemods = true
338 --------------------------------------------------------------------------------
339 function modmgr.get_worldmod_idx()
340 if not modmgr.hide_gamemods then
341 return modmgr.world_config_selected_mod - #modmgr.worldconfig.game_mods
343 return modmgr.world_config_selected_mod
347 --------------------------------------------------------------------------------
348 function modmgr.is_gamemod()
349 if not modmgr.hide_gamemods then
350 if modmgr.world_config_selected_mod <= #modmgr.worldconfig.game_mods then
360 --------------------------------------------------------------------------------
361 function modmgr.render_worldmodlist()
364 for i=1,#modmgr.global_mods,1 do
365 local parts = modmgr.global_mods[i]:split(DIR_DELIM)
366 local shortname = parts[#parts]
367 if modmgr.worldconfig.global_mods[shortname] then
368 retval = retval .. "#22F922" .. modmgr.global_mods[i] .. ","
370 retval = retval .. modmgr.global_mods[i] .. ","
377 --------------------------------------------------------------------------------
378 function modmgr.render_gamemodlist()
380 for i=1,#modmgr.worldconfig.game_mods,1 do
382 "#0000FF" .. modmgr.worldconfig.game_mods[i] .. ","
388 --------------------------------------------------------------------------------
389 function modmgr.dialog_configure_world()
392 local modpack_selected = false
393 local gamemod_selected = modmgr.is_gamemod()
398 if not gamemod_selected then
399 local worldmodidx = modmgr.get_worldmod_idx()
400 modname = modmgr.global_mods[worldmodidx]
402 if modname ~= nil then
404 if modname:find("<MODPACK>") ~= nil then
405 modname = modname:sub(0,modname:find("<") -2)
406 modpack_selected = true
409 local parts = modmgr.global_mods[worldmodidx]:split(DIR_DELIM)
410 shortname = parts[#parts]
412 modfolder = engine.get_modpath() .. DIR_DELIM .. modname
418 local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
422 "label[1.5,-0.25;World: " .. worldspec.name .. "]"
424 if modmgr.hide_gamemods then
425 retval = retval .. "checkbox[5.5,6.15;cb_hide_gamemods;Hide Game;true]"
427 retval = retval .. "checkbox[5.5,6.15;cb_hide_gamemods;Hide Game;false]"
430 "button[9.25,6.35;2,0.5;btn_config_world_save;Save]" ..
431 "button[7.4,6.35;2,0.5;btn_config_world_cancel;Cancel]" ..
432 "textlist[5.5,-0.25;5.5,6.5;world_config_modlist;"
435 if not modmgr.hide_gamemods then
436 retval = retval .. modmgr.render_gamemodlist()
439 retval = retval .. modmgr.render_worldmodlist()
441 retval = retval .. ";" .. modmgr.world_config_selected_mod .."]"
443 if not gamemod_selected then
445 "label[0,0.45;Mod:]" ..
446 "label[0.75,0.45;" .. modname .. "]" ..
447 "label[0,1.5;depends on:]" ..
448 "textlist[0,2;5,2;world_config_depends;" ..
449 modmgr.get_dependencys(modfolder) .. ";0]" ..
450 "label[0,4;depends on:]" ..
451 "textlist[0,4.5;5,2;world_config_is_required;;0]"
453 if modpack_selected then
455 "button[-0.05,1.05;2,0.5;btn_cfgw_enable_all;Enable All]" ..
456 "button[3.25,1.05;2,0.5;btn_cfgw_disable_all;Disable All]"
459 "checkbox[0,0.8;cb_mod_enabled;enabled;"
461 if modmgr.worldconfig.global_mods[shortname] then
462 print("checkbox " .. shortname .. " enabled")
463 retval = retval .. "true"
465 print("checkbox " .. shortname .. " disabled")
466 retval = retval .. "false"
469 retval = retval .. "]"
476 --------------------------------------------------------------------------------
477 function modmgr.handle_buttons(tab,fields)
481 if tab == "mod_mgr" then
482 retval = modmgr.handle_modmgr_buttons(fields)
485 if tab == "dialog_rename_modpack" then
486 retval = modmgr.handle_rename_modpack_buttons(fields)
489 if tab == "dialog_delete_mod" then
490 retval = modmgr.handle_delete_mod_buttons(fields)
493 if tab == "dialog_configure_world" then
494 retval = modmgr.handle_configure_world_buttons(fields)
500 --------------------------------------------------------------------------------
501 function modmgr.get_dependencys(modfolder)
502 local filename = modfolder ..
503 DIR_DELIM .. "depends.txt"
505 local dependencyfile = io.open(filename,"r")
508 if dependencyfile then
509 local dependency = dependencyfile:read("*l")
514 toadd = toadd .. dependency
515 dependency = dependencyfile:read()
517 dependencyfile:close()
519 print(filename .. " not found")
526 --------------------------------------------------------------------------------
527 function modmgr.get_worldconfig(worldpath)
528 local filename = worldpath ..
529 DIR_DELIM .. "world.mt"
531 local worldfile = io.open(filename,"r")
533 local worldconfig = {}
534 worldconfig.global_mods = {}
535 worldconfig.game_mods = {}
538 local dependency = worldfile:read("*l")
540 local parts = dependency:split("=")
542 local key = parts[1]:trim()
544 if key == "gameid" then
545 worldconfig.id = parts[2]:trim()
547 local key = parts[1]:trim():sub(10)
548 if parts[2]:trim() == "true" then
549 print("found enabled mod: >" .. key .. "<")
550 worldconfig.global_mods[key] = true
552 print("found disabled mod: >" .. key .. "<")
553 worldconfig.global_mods[key] = false
556 dependency = worldfile:read("*l")
560 print(filename .. " not found")
564 local gamemodpath = engine.get_gamepath() .. DIR_DELIM .. worldconfig.id .. DIR_DELIM .. "mods"
566 print("reading game mods from: " .. dump(gamemodpath))
567 get_mods(gamemodpath,worldconfig.game_mods)
571 --------------------------------------------------------------------------------
572 function modmgr.handle_modmgr_buttons(fields)
579 if fields["modlist"] ~= nil then
580 local event = explode_textlist_event(fields["modlist"])
581 modmgr.selected_mod = event.index
584 if fields["btn_mod_mgr_install_local"] ~= nil then
585 engine.show_file_open_dialog("mod_mgt_open_dlg","Select Mod File:")
588 if fields["btn_mod_mgr_download"] ~= nil then
589 retval.current_tab = "dialog_modstore_unsorted"
590 retval.is_dialog = true
591 retval.show_buttons = false
595 if fields["btn_mod_mgr_rename_modpack"] ~= nil then
596 retval.current_tab = "dialog_rename_modpack"
597 retval.is_dialog = true
598 retval.show_buttons = false
602 if fields["btn_mod_mgr_delete_mod"] ~= nil then
603 retval.current_tab = "dialog_delete_mod"
604 retval.is_dialog = true
605 retval.show_buttons = false
609 if fields["mod_mgt_open_dlg_accepted"] ~= nil and
610 fields["mod_mgt_open_dlg_accepted"] ~= "" then
611 modmgr.installmod(fields["mod_mgt_open_dlg_accepted"],nil)
617 --------------------------------------------------------------------------------
618 function modmgr.installmod(modfilename,basename)
619 local modfile = identify_filetype(modfilename)
621 local modpath = modmgr.extract(modfile)
623 if modpath == nil then
624 gamedata.errormessage = "Install Mod: file: " .. modfile.name ..
625 "\nInstall Mod: unsupported filetype \"" .. modfile.type .. "\""
630 local basefolder = modmgr.getbasefolder(modpath)
632 if basefolder.type == "modpack" then
633 local clean_path = nil
635 if basename ~= nil then
636 clean_path = "mp_" .. basename
639 if clean_path == nil then
640 clean_path = get_last_folder(cleanup_path(basefolder.path))
643 if clean_path ~= nil then
644 local targetpath = engine.get_modpath() .. DIR_DELIM .. clean_path
645 engine.copy_dir(basefolder.path,targetpath)
647 gamedata.errormessage = "Install Mod: unable to find suitable foldername for modpack "
652 if basefolder.type == "mod" then
653 local targetfolder = basename
655 if targetfolder == nil then
656 targetfolder = modmgr.identify_modname(basefolder.path,"init.lua")
659 --if heuristic failed try to use current foldername
660 if targetfolder == nil then
661 targetfolder = get_last_folder(basefolder.path)
664 if targetfolder ~= nil and modmgr.isValidModname(targetfolder) then
665 local targetpath = engine.get_modpath() .. DIR_DELIM .. targetfolder
666 engine.copy_dir(basefolder.path,targetpath)
668 gamedata.errormessage = "Install Mod: unable to find real modname for: "
673 engine.delete_dir(modpath)
676 --------------------------------------------------------------------------------
677 function modmgr.handle_rename_modpack_buttons(fields)
678 local oldname = modmgr.global_mods[modmgr.selected_mod]
679 oldname = oldname:sub(0,oldname:find("<") -2)
681 if fields["dlg_rename_modpack_confirm"] ~= nil then
682 local oldpath = engine.get_modpath() .. DIR_DELIM .. oldname
683 local targetpath = engine.get_modpath() .. DIR_DELIM .. fields["te_modpack_name"]
684 engine.copy_dir(oldpath,targetpath,false)
690 current_tab = engine.setting_get("main_menu_tab")
693 --------------------------------------------------------------------------------
694 function modmgr.handle_configure_world_buttons(fields)
695 if fields["world_config_modlist"] ~= nil then
696 local event = explode_textlist_event(fields["world_config_modlist"])
697 modmgr.world_config_selected_mod = event.index
700 if fields["cb_mod_enabled"] ~= nil then
701 local index = modmgr.get_worldmod_idx()
702 local modname = modmgr.global_mods[index]
704 local parts = modmgr.global_mods[index]:split(DIR_DELIM)
705 local shortname = parts[#parts]
707 if fields["cb_mod_enabled"] == "true" then
708 modmgr.worldconfig.global_mods[shortname] = true
710 modmgr.worldconfig.global_mods[shortname] = false
714 if fields["cb_hide_gamemods"] ~= nil then
715 if fields["cb_hide_gamemods"] == "true" then
716 modmgr.hide_gamemods = true
718 modmgr.hide_gamemods = false
722 if fields["btn_config_world_save"] then
723 local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
725 local filename = worldspec.path ..
726 DIR_DELIM .. "world.mt"
728 local worldfile = io.open(filename,"w")
731 worldfile:write("gameid = " .. modmgr.worldconfig.id .. "\n")
732 for key,value in pairs(modmgr.worldconfig.global_mods) do
734 worldfile:write("load_mod_" .. key .. " = true" .. "\n")
736 worldfile:write("load_mod_" .. key .. " = false" .. "\n")
743 modmgr.worldconfig = nil
748 current_tab = engine.setting_get("main_menu_tab")
752 if fields["btn_config_world_cancel"] then
754 modmgr.worldconfig = nil
759 current_tab = engine.setting_get("main_menu_tab")
763 if fields["btn_cfgw_enable_all"] then
764 local worldmodidx = modmgr.get_worldmod_idx()
765 modname = modmgr.global_mods[worldmodidx]
767 modname = modname:sub(0,modname:find("<") -2)
769 for i=1,#modmgr.global_mods,1 do
771 if modmgr.global_mods[i]:find("<MODPACK>") == nil then
772 local modpackpart = modmgr.global_mods[i]:sub(0,modname:len())
774 if modpackpart == modname then
775 local parts = modmgr.global_mods[i]:split(DIR_DELIM)
776 local shortname = parts[#parts]
777 modmgr.worldconfig.global_mods[shortname] = true
783 if fields["btn_cfgw_disable_all"] then
784 local worldmodidx = modmgr.get_worldmod_idx()
785 modname = modmgr.global_mods[worldmodidx]
787 modname = modname:sub(0,modname:find("<") -2)
789 for i=1,#modmgr.global_mods,1 do
790 local modpackpart = modmgr.global_mods[i]:sub(0,modname:len())
792 if modpackpart == modname then
793 local parts = modmgr.global_mods[i]:split(DIR_DELIM)
794 local shortname = parts[#parts]
795 modmgr.worldconfig.global_mods[shortname] = nil
802 --------------------------------------------------------------------------------
803 function modmgr.handle_delete_mod_buttons(fields)
804 local modname = modmgr.global_mods[modmgr.selected_mod]
806 if modname:find("<MODPACK>") ~= nil then
807 modname = modname:sub(0,modname:find("<") -2)
810 if fields["dlg_delete_mod_confirm"] ~= nil then
811 local oldpath = engine.get_modpath() .. DIR_DELIM .. modname
813 if oldpath ~= nil and
815 oldpath ~= engine.get_modpath() then
816 engine.delete_dir(oldpath)
823 current_tab = engine.setting_get("main_menu_tab")
827 --------------------------------------------------------------------------------
828 function modmgr.dialog_delete_mod()
830 local modname = modmgr.global_mods[modmgr.selected_mod]
832 if modname:find("<MODPACK>") ~= nil then
833 modname = modname:sub(0,modname:find("<") -2)
837 "field[1.75,1;10,3;;Are you sure you want to delete ".. modname .. "?;]"..
838 "button[4,4.2;1,0.5;dlg_delete_mod_confirm;Yes]" ..
839 "button[6.5,4.2;3,0.5;dlg_delete_mod_cancel;No of course not!]"
844 --------------------------------------------------------------------------------
845 function modmgr.init_worldconfig()
847 local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
849 if worldspec ~= nil then
851 modmgr.worldconfig = modmgr.get_worldconfig(worldspec.path)
853 if modmgr.worldconfig.id == nil or
854 modmgr.worldconfig.id == "" then
855 modmgr.worldconfig = nil
865 --------------------------------------------------------------------------------
866 function modmgr.gettab(name)
869 if name == "mod_mgr" then
870 retval = retval .. modmgr.tab()
873 if name == "dialog_rename_modpack" then
874 retval = retval .. modmgr.dialog_rename_modpack()
877 if name == "dialog_delete_mod" then
878 retval = retval .. modmgr.dialog_delete_mod()
881 if name == "dialog_configure_world" then
882 retval = retval .. modmgr.dialog_configure_world()