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 .. "#GRN" .. 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 "#BLU" .. 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:find("<MODPACK>") ~= nil then
403 modname = modname:sub(0,modname:find("<") -2)
404 modpack_selected = true
407 local parts = modmgr.global_mods[worldmodidx]:split(DIR_DELIM)
408 shortname = parts[#parts]
410 modfolder = engine.get_modpath() .. DIR_DELIM .. modname
413 local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
417 "label[1.5,-0.25;World: " .. worldspec.name .. "]"
419 if modmgr.hide_gamemods then
420 retval = retval .. "checkbox[5.5,6.15;cb_hide_gamemods;Hide Game;true]"
422 retval = retval .. "checkbox[5.5,6.15;cb_hide_gamemods;Hide Game;false]"
425 "button[9.25,6.35;2,0.5;btn_config_world_save;Save]" ..
426 "button[7.4,6.35;2,0.5;btn_config_world_cancel;Cancel]" ..
427 "textlist[5.5,-0.25;5.5,6.5;world_config_modlist;"
430 if not modmgr.hide_gamemods then
431 retval = retval .. modmgr.render_gamemodlist()
434 retval = retval .. modmgr.render_worldmodlist()
436 retval = retval .. ";" .. modmgr.world_config_selected_mod .."]"
438 if not gamemod_selected then
440 "label[0,0.45;Mod:]" ..
441 "label[0.75,0.45;" .. modname .. "]" ..
442 "label[0,1.5;depends on:]" ..
443 "textlist[0,2;5,2;world_config_depends;" ..
444 modmgr.get_dependencys(modfolder) .. ";0]" ..
445 "label[0,4;depends on:]" ..
446 "textlist[0,4.5;5,2;world_config_is_required;;0]"
448 if modpack_selected then
450 "button[-0.05,1.05;2,0.5;btn_cfgw_enable_all;Enable All]" ..
451 "button[3.25,1.05;2,0.5;btn_cfgw_disable_all;Disable All]"
454 "checkbox[0,0.8;cb_mod_enabled;enabled;"
456 if modmgr.worldconfig.global_mods[shortname] then
457 print("checkbox " .. shortname .. " enabled")
458 retval = retval .. "true"
460 print("checkbox " .. shortname .. " disabled")
461 retval = retval .. "false"
464 retval = retval .. "]"
471 --------------------------------------------------------------------------------
472 function modmgr.handle_buttons(tab,fields)
476 if tab == "mod_mgr" then
477 retval = modmgr.handle_modmgr_buttons(fields)
480 if tab == "dialog_rename_modpack" then
481 retval = modmgr.handle_rename_modpack_buttons(fields)
484 if tab == "dialog_delete_mod" then
485 retval = modmgr.handle_delete_mod_buttons(fields)
488 if tab == "dialog_configure_world" then
489 retval = modmgr.handle_configure_world_buttons(fields)
495 --------------------------------------------------------------------------------
496 function modmgr.get_dependencys(modfolder)
497 local filename = modfolder ..
498 DIR_DELIM .. "depends.txt"
500 local dependencyfile = io.open(filename,"r")
503 if dependencyfile then
504 local dependency = dependencyfile:read("*l")
509 toadd = toadd .. dependency
510 dependency = dependencyfile:read()
512 dependencyfile:close()
514 print(filename .. " not found")
521 --------------------------------------------------------------------------------
522 function modmgr.get_worldconfig(worldpath)
523 local filename = worldpath ..
524 DIR_DELIM .. "world.mt"
526 local worldfile = io.open(filename,"r")
528 local worldconfig = {}
529 worldconfig.global_mods = {}
530 worldconfig.game_mods = {}
533 local dependency = worldfile:read("*l")
535 local parts = dependency:split("=")
537 local key = parts[1]:trim()
539 if key == "gameid" then
540 worldconfig.id = parts[2]:trim()
542 local key = parts[1]:trim():sub(10)
543 if parts[2]:trim() == "true" then
544 print("found enabled mod: >" .. key .. "<")
545 worldconfig.global_mods[key] = true
547 print("found disabled mod: >" .. key .. "<")
548 worldconfig.global_mods[key] = false
551 dependency = worldfile:read("*l")
555 print(filename .. " not found")
559 local gamemodpath = engine.get_gamepath() .. DIR_DELIM .. worldconfig.id .. DIR_DELIM .. "mods"
561 print("reading game mods from: " .. dump(gamemodpath))
562 get_mods(gamemodpath,worldconfig.game_mods)
566 --------------------------------------------------------------------------------
567 function modmgr.handle_modmgr_buttons(fields)
574 if fields["modlist"] ~= nil then
575 local event = explode_textlist_event(fields["modlist"])
576 modmgr.selected_mod = event.index
579 if fields["btn_mod_mgr_install_local"] ~= nil then
580 engine.show_file_open_dialog("mod_mgt_open_dlg","Select Mod File:")
583 if fields["btn_mod_mgr_download"] ~= nil then
584 retval.current_tab = "dialog_modstore_unsorted"
585 retval.is_dialog = true
586 retval.show_buttons = false
590 if fields["btn_mod_mgr_rename_modpack"] ~= nil then
591 retval.current_tab = "dialog_rename_modpack"
592 retval.is_dialog = true
593 retval.show_buttons = false
597 if fields["btn_mod_mgr_delete_mod"] ~= nil then
598 retval.current_tab = "dialog_delete_mod"
599 retval.is_dialog = true
600 retval.show_buttons = false
604 if fields["mod_mgt_open_dlg_accepted"] ~= nil and
605 fields["mod_mgt_open_dlg_accepted"] ~= "" then
606 modmgr.installmod(fields["mod_mgt_open_dlg_accepted"],nil)
612 --------------------------------------------------------------------------------
613 function modmgr.installmod(modfilename,basename)
614 local modfile = identify_filetype(modfilename)
616 local modpath = modmgr.extract(modfile)
618 if modpath == nil then
619 gamedata.errormessage = "Install Mod: file: " .. modfile.name ..
620 "\nInstall Mod: unsupported filetype \"" .. modfile.type .. "\""
625 local basefolder = modmgr.getbasefolder(modpath)
627 if basefolder.type == "modpack" then
628 local clean_path = nil
630 if basename ~= nil then
631 clean_path = "mp_" .. basename
634 if clean_path == nil then
635 clean_path = get_last_folder(cleanup_path(basefolder.path))
638 if clean_path ~= nil then
639 local targetpath = engine.get_modpath() .. DIR_DELIM .. clean_path
640 engine.copy_dir(basefolder.path,targetpath)
642 gamedata.errormessage = "Install Mod: unable to find suitable foldername for modpack "
647 if basefolder.type == "mod" then
648 local targetfolder = basename
650 if targetfolder == nil then
651 targetfolder = modmgr.identify_modname(basefolder.path,"init.lua")
654 --if heuristic failed try to use current foldername
655 if targetfolder == nil then
656 targetfolder = get_last_folder(basefolder.path)
659 if targetfolder ~= nil and modmgr.isValidModname(targetfolder) then
660 local targetpath = engine.get_modpath() .. DIR_DELIM .. targetfolder
661 engine.copy_dir(basefolder.path,targetpath)
663 gamedata.errormessage = "Install Mod: unable to find real modname for: "
668 engine.delete_dir(modpath)
671 --------------------------------------------------------------------------------
672 function modmgr.handle_rename_modpack_buttons(fields)
673 local oldname = modmgr.global_mods[modmgr.selected_mod]
674 oldname = oldname:sub(0,oldname:find("<") -2)
676 if fields["dlg_rename_modpack_confirm"] ~= nil then
677 local oldpath = engine.get_modpath() .. DIR_DELIM .. oldname
678 local targetpath = engine.get_modpath() .. DIR_DELIM .. fields["te_modpack_name"]
679 engine.copy_dir(oldpath,targetpath,false)
685 current_tab = engine.setting_get("main_menu_tab")
688 --------------------------------------------------------------------------------
689 function modmgr.handle_configure_world_buttons(fields)
690 if fields["world_config_modlist"] ~= nil then
691 local event = explode_textlist_event(fields["world_config_modlist"])
692 modmgr.world_config_selected_mod = event.index
695 if fields["cb_mod_enabled"] ~= nil then
696 local index = modmgr.get_worldmod_idx()
697 local modname = modmgr.global_mods[index]
699 local parts = modmgr.global_mods[index]:split(DIR_DELIM)
700 local shortname = parts[#parts]
702 if fields["cb_mod_enabled"] == "true" then
703 modmgr.worldconfig.global_mods[shortname] = true
705 modmgr.worldconfig.global_mods[shortname] = false
709 if fields["cb_hide_gamemods"] ~= nil then
710 if fields["cb_hide_gamemods"] == "true" then
711 modmgr.hide_gamemods = true
713 modmgr.hide_gamemods = false
717 if fields["btn_config_world_save"] then
718 local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
720 local filename = worldspec.path ..
721 DIR_DELIM .. "world.mt"
723 local worldfile = io.open(filename,"w")
726 worldfile:write("gameid = " .. modmgr.worldconfig.id .. "\n")
727 for key,value in pairs(modmgr.worldconfig.global_mods) do
729 worldfile:write("load_mod_" .. key .. " = true" .. "\n")
731 worldfile:write("load_mod_" .. key .. " = false" .. "\n")
738 modmgr.worldconfig = nil
743 current_tab = engine.setting_get("main_menu_tab")
747 if fields["btn_config_world_cancel"] then
749 modmgr.worldconfig = nil
754 current_tab = engine.setting_get("main_menu_tab")
758 if fields["btn_cfgw_enable_all"] then
759 local worldmodidx = modmgr.get_worldmod_idx()
760 modname = modmgr.global_mods[worldmodidx]
762 modname = modname:sub(0,modname:find("<") -2)
764 for i=1,#modmgr.global_mods,1 do
766 if modmgr.global_mods[i]:find("<MODPACK>") == nil then
767 local modpackpart = modmgr.global_mods[i]:sub(0,modname:len())
769 if modpackpart == modname then
770 local parts = modmgr.global_mods[i]:split(DIR_DELIM)
771 local shortname = parts[#parts]
772 modmgr.worldconfig.global_mods[shortname] = true
778 if fields["btn_cfgw_disable_all"] then
779 local worldmodidx = modmgr.get_worldmod_idx()
780 modname = modmgr.global_mods[worldmodidx]
782 modname = modname:sub(0,modname:find("<") -2)
784 for i=1,#modmgr.global_mods,1 do
785 local modpackpart = modmgr.global_mods[i]:sub(0,modname:len())
787 if modpackpart == modname then
788 local parts = modmgr.global_mods[i]:split(DIR_DELIM)
789 local shortname = parts[#parts]
790 modmgr.worldconfig.global_mods[shortname] = nil
797 --------------------------------------------------------------------------------
798 function modmgr.handle_delete_mod_buttons(fields)
799 local modname = modmgr.global_mods[modmgr.selected_mod]
801 if modname:find("<MODPACK>") ~= nil then
802 modname = modname:sub(0,modname:find("<") -2)
805 if fields["dlg_delete_mod_confirm"] ~= nil then
806 local oldpath = engine.get_modpath() .. DIR_DELIM .. modname
808 if oldpath ~= nil and
810 oldpath ~= engine.get_modpath() then
811 engine.delete_dir(oldpath)
818 current_tab = engine.setting_get("main_menu_tab")
822 --------------------------------------------------------------------------------
823 function modmgr.dialog_delete_mod()
825 local modname = modmgr.global_mods[modmgr.selected_mod]
827 if modname:find("<MODPACK>") ~= nil then
828 modname = modname:sub(0,modname:find("<") -2)
832 "field[1.75,1;10,3;;Are you sure you want to delete ".. modname .. "?;]"..
833 "button[4,4.2;1,0.5;dlg_delete_mod_confirm;Yes]" ..
834 "button[6.5,4.2;3,0.5;dlg_delete_mod_cancel;No of course not!]"
839 --------------------------------------------------------------------------------
840 function modmgr.init_worldconfig()
842 local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
844 if worldspec ~= nil then
846 modmgr.worldconfig = modmgr.get_worldconfig(worldspec.path)
848 if modmgr.worldconfig.id == nil or
849 modmgr.worldconfig.id == "" then
850 modmgr.worldconfig = nil
860 --------------------------------------------------------------------------------
861 function modmgr.gettab(name)
864 if name == "mod_mgr" then
865 retval = retval .. modmgr.tab()
868 if name == "dialog_rename_modpack" then
869 retval = retval .. modmgr.dialog_rename_modpack()
872 if name == "dialog_delete_mod" then
873 retval = retval .. modmgr.dialog_delete_mod()
876 if name == "dialog_configure_world" then
877 retval = retval .. modmgr.dialog_configure_world()