]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/modmgr.lua
Remove lots of debug output from modmgr
[dragonfireclient.git] / builtin / modmgr.lua
1 --Minetest
2 --Copyright (C) 2013 sapier
3 --
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.
8 --
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.
13 --
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.
17
18 --------------------------------------------------------------------------------
19 function get_mods(path,retval,basefolder)
20
21         local mods = engine.get_dirlist(path,true)
22                         
23         for i=1,#mods,1 do
24                 local filename = path .. DIR_DELIM .. mods[i] .. DIR_DELIM .. "modpack.txt"
25                 local modpackfile,error = io.open(filename,"r")
26                 
27                 local name = mods[i]
28                 if basefolder ~= nil and
29                         basefolder ~= "" then
30                         name = basefolder .. DIR_DELIM .. mods[i]
31                 end
32                         
33                 if modpackfile ~= nil then
34                         modpackfile:close()
35                         table.insert(retval,name .. " <MODPACK>")
36                         get_mods(path .. DIR_DELIM .. name,retval,name)
37                 else
38
39                         table.insert(retval,name)
40                 end
41         end
42 end
43
44 --modmanager implementation
45 modmgr = {}
46
47 --------------------------------------------------------------------------------
48 function modmgr.extract(modfile)
49         if modfile.type == "zip" then
50                 local tempfolder = os.tempfolder()
51                 
52                 if tempfolder ~= nil and
53                         tempfodler ~= "" then
54                         engine.create_dir(tempfolder)
55                         engine.extract_zip(modfile.name,tempfolder)
56                         return tempfolder
57                 end
58         end
59 end
60
61 -------------------------------------------------------------------------------
62 function modmgr.getbasefolder(temppath)
63
64         if temppath == nil then
65                 return {
66                 type = "invalid",
67                 path = ""
68                 }
69         end
70
71         local testfile = io.open(temppath .. DIR_DELIM .. "init.lua","r")
72         if testfile ~= nil then
73                 testfile:close()
74                 return {
75                                 type="mod",
76                                 path=temppath
77                                 }
78         end
79         
80         testfile = io.open(temppath .. DIR_DELIM .. "modpack.txt","r")
81         if testfile ~= nil then
82                 testfile:close()
83                 return {
84                                 type="modpack",
85                                 path=temppath
86                                 }
87         end
88         
89         local subdirs = engine.get_dirlist(temppath,true)
90         
91         --only single mod or modpack allowed
92         if #subdirs ~= 1 then
93                 return {
94                         type = "invalid",
95                         path = ""
96                         }
97         end
98
99         testfile = 
100         io.open(temppath .. DIR_DELIM .. subdirs[1] ..DIR_DELIM .."init.lua","r")
101         if testfile ~= nil then
102                 testfile:close()
103                 return {
104                         type="mod",
105                         path= temppath .. DIR_DELIM .. subdirs[1]
106                         }
107         end
108         
109         testfile = 
110         io.open(temppath .. DIR_DELIM .. subdirs[1] ..DIR_DELIM .."modpack.txt","r")
111         if testfile ~= nil then
112                 testfile:close()
113                 return {
114                         type="modpack",
115                         path=temppath ..  DIR_DELIM .. subdirs[1]
116                         }
117         end
118
119         return {
120                 type = "invalid",
121                 path = ""
122                 }
123 end
124
125 --------------------------------------------------------------------------------
126 function modmgr.isValidModname(modpath)
127         if modpath:find("-") ~= nil then
128                 return false
129         end
130         
131         return true
132 end
133
134 --------------------------------------------------------------------------------
135 function modmgr.parse_register_line(line)
136         local pos1 = line:find("\"")
137         local pos2 = nil
138         if pos1 ~= nil then
139                 pos2 = line:find("\"",pos1+1)
140         end
141         
142         if pos1 ~= nil and pos2 ~= nil then
143                 local item = line:sub(pos1+1,pos2-1)
144                 
145                 if item ~= nil and
146                         item ~= "" then
147                         local pos3 = item:find(":")
148                         
149                         if pos3 ~= nil then
150                                 return item:sub(1,pos3-1)
151                         end
152                 end
153         end
154         return nil
155 end
156
157 --------------------------------------------------------------------------------
158 function modmgr.parse_dofile_line(modpath,line)
159         local pos1 = line:find("\"")
160         local pos2 = nil
161         if pos1 ~= nil then
162                 pos2 = line:find("\"",pos1+1)
163         end
164         
165         if pos1 ~= nil and pos2 ~= nil then
166                 local filename = line:sub(pos1+1,pos2-1)
167                 
168                 if filename ~= nil and
169                         filename ~= "" and
170                         filename:find(".lua") then
171                         return modmgr.identify_modname(modpath,filename)
172                 end
173         end
174         return nil
175 end
176
177 --------------------------------------------------------------------------------
178 function modmgr.update_global_mods()
179         local modpath = engine.get_modpath()
180         modmgr.global_mods = {}
181         if modpath ~= nil and
182                 modpath ~= "" then
183                 get_mods(modpath,modmgr.global_mods)
184         end
185 end
186
187 --------------------------------------------------------------------------------
188 function modmgr.get_mods_list()
189         local toadd = ""
190         
191         modmgr.update_global_mods()
192         
193         if modmgr.global_mods ~= nil then
194                 for i=1,#modmgr.global_mods,1 do
195                         if toadd ~= "" then
196                                 toadd = toadd..","
197                         end
198                         toadd = toadd .. modmgr.global_mods[i]
199                 end 
200         end
201         
202         return toadd
203 end
204
205 --------------------------------------------------------------------------------
206 function modmgr.mod_exists(basename)
207         modmgr.update_global_mods()
208         
209         if modmgr.global_mods ~= nil then
210                 for i=1,#modmgr.global_mods,1 do
211                         if modmgr.global_mods[i] == basename then
212                                 return true
213                         end
214                 end
215         end
216         
217         return false
218 end
219
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()
225                 
226                 while line~= nil do
227                         local modname = nil
228                 
229                         if line:find("minetest.register_tool") then
230                                 modname = modmgr.parse_register_line(line)
231                         end
232                         
233                         if line:find("minetest.register_craftitem") then
234                                 modname = modmgr.parse_register_line(line)
235                         end
236                         
237                         
238                         if line:find("minetest.register_node") then
239                                 modname = modmgr.parse_register_line(line)
240                         end
241                         
242                         if line:find("dofile") then
243                                 modname = modmgr.parse_dofile_line(modpath,line)
244                         end
245                 
246                         if modname ~= nil then
247                                 testfile:close()
248                                 return modname
249                         end
250                         
251                         line = testfile:read()
252                 end
253                 testfile:close()
254         end
255         
256         return nil
257 end
258
259 --------------------------------------------------------------------------------
260 function modmgr.tab()
261         if modmgr.selected_mod == nil then
262                 modmgr.selected_mod = 1
263         end
264         
265         local retval = 
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 .. "]"
271
272         retval = retval ..
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]"
275                 
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]"
279         end
280         
281         if #modmgr.global_mods >= modmgr.selected_mod then
282                 local modpath = engine.get_modpath()
283                 --show dependencys
284                 if modmgr.global_mods[modmgr.selected_mod]:find("<MODPACK>") == nil then
285                         retval = retval .. 
286                                 "label[6,1.9;Depends:]" ..
287                                 "textlist[6,2.4;5.7,2;deplist;"
288                                 
289                         toadd = modmgr.get_dependencys(modpath .. DIR_DELIM .. 
290                                                 modmgr.global_mods[modmgr.selected_mod])
291                         
292                         retval = retval .. toadd .. ";0;true,false]"
293                         
294                         --TODO read modinfo
295                 end
296                 --show delete button
297                 retval = retval .. "button[8,4.85;2,0.5;btn_mod_mgr_delete_mod;Delete]"
298         end
299         return retval
300 end
301
302 --------------------------------------------------------------------------------
303 function modmgr.dialog_rename_modpack()
304
305         local modname = modmgr.global_mods[modmgr.selected_mod]
306         modname = modname:sub(0,modname:find("<") -2)
307         
308         local retval = 
309                 "label[1.75,1;Rename Modpack:]"..
310                 "field[4.5,1.4;6,0.5;te_modpack_name;;" ..
311                 modname ..
312                 "]" ..
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]"
315
316         return retval
317 end
318
319 --------------------------------------------------------------------------------
320 function modmgr.precheck()
321         if modmgr.global_mods == nil then
322                 modmgr.update_global_mods()
323         end
324         
325         if modmgr.world_config_selected_world == nil then
326                 modmgr.world_config_selected_world = 1
327         end
328         
329         if modmgr.world_config_selected_mod == nil then
330                 modmgr.world_config_selected_mod = 1
331         end
332         
333         if modmgr.hide_gamemods == nil then
334                 modmgr.hide_gamemods = true
335         end
336 end
337
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
342         else
343                 return modmgr.world_config_selected_mod
344         end
345 end
346
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
351                         return true
352                 else
353                         return false
354                 end
355         else
356                 return false
357         end
358 end
359
360 --------------------------------------------------------------------------------
361 function modmgr.render_worldmodlist()
362         local retval = ""
363         
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] .. ","
369                 else
370                         retval = retval .. modmgr.global_mods[i] .. ","
371                 end
372         end
373         
374         return retval
375 end
376
377 --------------------------------------------------------------------------------
378 function modmgr.render_gamemodlist()
379         local retval = ""
380         for i=1,#modmgr.worldconfig.game_mods,1 do
381                 retval = retval ..
382                         "#0000FF" .. modmgr.worldconfig.game_mods[i] .. ","
383         end
384         
385         return retval
386 end
387
388 --------------------------------------------------------------------------------
389 function modmgr.dialog_configure_world()
390         modmgr.precheck()
391         
392         local modpack_selected = false
393         local gamemod_selected = modmgr.is_gamemod()
394         local modname = ""
395         local modfolder = ""
396         local shortname = ""
397         
398         if not gamemod_selected then
399                 local worldmodidx = modmgr.get_worldmod_idx()
400                 modname = modmgr.global_mods[worldmodidx]
401
402                 if modname ~= nil then
403                 
404                         if modname:find("<MODPACK>") ~= nil then
405                                 modname = modname:sub(0,modname:find("<") -2)
406                                 modpack_selected = true
407                         end
408                 
409                         local parts = modmgr.global_mods[worldmodidx]:split(DIR_DELIM)
410                         shortname = parts[#parts]
411                 
412                         modfolder = engine.get_modpath() .. DIR_DELIM .. modname
413                 else
414                         modname = ""
415                 end
416         end
417
418         local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
419         
420         local retval =
421                 "size[11,6.5]" ..
422                 "label[1.5,-0.25;World: " .. worldspec.name .. "]"
423                 
424         if modmgr.hide_gamemods then
425                 retval = retval .. "checkbox[5.5,6.15;cb_hide_gamemods;Hide Game;true]"
426         else
427                 retval = retval .. "checkbox[5.5,6.15;cb_hide_gamemods;Hide Game;false]"
428         end
429         retval = retval ..
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;"
433                 
434
435         if not modmgr.hide_gamemods then
436                 retval = retval .. modmgr.render_gamemodlist()
437         end
438         
439         retval = retval .. modmgr.render_worldmodlist() 
440
441         retval = retval .. ";" .. modmgr.world_config_selected_mod .."]"
442         
443         if not gamemod_selected then
444                 retval = retval ..
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]"
452         
453                 if modpack_selected then
454                         retval = retval ..
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]"
457                 else
458                         retval = retval .. 
459                                 "checkbox[0,0.8;cb_mod_enabled;enabled;"
460                         
461                         if modmgr.worldconfig.global_mods[shortname] then
462                                 retval = retval .. "true"
463                         else
464                                 retval = retval .. "false"
465                         end
466                         
467                         retval = retval .. "]"
468                 end
469         end
470         
471         return retval
472 end
473
474 --------------------------------------------------------------------------------
475 function modmgr.handle_buttons(tab,fields)
476
477         local retval = nil
478         
479         if tab == "mod_mgr" then
480                 retval = modmgr.handle_modmgr_buttons(fields)
481         end
482         
483         if tab == "dialog_rename_modpack" then
484                 retval = modmgr.handle_rename_modpack_buttons(fields)
485         end
486         
487         if tab == "dialog_delete_mod" then
488                 retval = modmgr.handle_delete_mod_buttons(fields)
489         end
490         
491         if tab == "dialog_configure_world" then
492                 retval = modmgr.handle_configure_world_buttons(fields)
493         end
494         
495         return retval
496 end
497
498 --------------------------------------------------------------------------------
499 function modmgr.get_dependencys(modfolder)
500         local filename = modfolder ..
501                                 DIR_DELIM .. "depends.txt"
502
503         local dependencyfile = io.open(filename,"r")
504         
505         local toadd = ""
506         if dependencyfile then
507                 local dependency = dependencyfile:read("*l")
508                 while dependency do
509                         if toadd ~= "" then     
510                                 toadd = toadd .. ","
511                         end
512                         toadd = toadd .. dependency
513                         dependency = dependencyfile:read()
514                 end
515                 dependencyfile:close()
516         else
517                 print("Modmgr:" .. filename .. " not found")
518         end
519
520         return toadd
521 end
522
523
524 --------------------------------------------------------------------------------
525 function modmgr.get_worldconfig(worldpath)
526         local filename = worldpath ..
527                                 DIR_DELIM .. "world.mt"
528
529         local worldfile = io.open(filename,"r")
530         
531         local worldconfig = {}
532         worldconfig.global_mods = {}
533         worldconfig.game_mods = {}
534         
535         if worldfile then
536                 local dependency = worldfile:read("*l")
537                 while dependency do
538                         local parts = dependency:split("=")
539
540                         local key = parts[1]:trim()
541
542                         if key == "gameid" then
543                                 worldconfig.id = parts[2]:trim()
544                         else
545                                 local key = parts[1]:trim():sub(10)
546                                 if parts[2]:trim() == "true" then
547                                         worldconfig.global_mods[key] = true
548                                 else
549                                         worldconfig.global_mods[key] = false
550                                 end
551                         end
552                         dependency = worldfile:read("*l")
553                 end
554                 worldfile:close()
555         else
556                 print("Modmgr: " .. filename .. " not found")
557         end
558         
559         --read gamemods
560         local gamemodpath = engine.get_gamepath() .. DIR_DELIM .. worldconfig.id .. DIR_DELIM .. "mods"
561         
562         get_mods(gamemodpath,worldconfig.game_mods)
563
564         return worldconfig
565 end
566 --------------------------------------------------------------------------------
567 function modmgr.handle_modmgr_buttons(fields)
568         local retval = {
569                         tab = nil,
570                         is_dialog = nil,
571                         show_buttons = nil,
572                 }
573
574         if fields["modlist"] ~= nil then
575                 local event = explode_textlist_event(fields["modlist"])
576                 modmgr.selected_mod = event.index
577         end
578         
579         if fields["btn_mod_mgr_install_local"] ~= nil then
580                 engine.show_file_open_dialog("mod_mgt_open_dlg","Select Mod File:")
581         end
582         
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
587                 return retval
588         end
589         
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
594                 return retval
595         end
596         
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
601                 return retval
602         end
603         
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)
607         end
608         
609         return nil;
610 end
611
612 --------------------------------------------------------------------------------
613 function modmgr.installmod(modfilename,basename)
614         local modfile = identify_filetype(modfilename)
615         
616         local modpath = modmgr.extract(modfile)
617         
618         if modpath == nil then
619                 gamedata.errormessage = "Install Mod: file: " .. modfile.name ..
620                         "\nInstall Mod: unsupported filetype \"" .. modfile.type .. "\""
621                 return
622         end
623         
624         
625         local basefolder = modmgr.getbasefolder(modpath)
626         
627         if basefolder.type == "modpack" then
628                 local clean_path = nil
629                 
630                 if basename ~= nil then
631                         clean_path = "mp_" .. basename
632                 end
633                 
634                 if clean_path == nil then
635                         clean_path = get_last_folder(cleanup_path(basefolder.path))
636                 end
637                 
638                 if clean_path ~= nil then
639                         local targetpath = engine.get_modpath() .. DIR_DELIM .. clean_path
640                         if not engine.copy_dir(basefolder.path,targetpath) then
641                                 gamedata.errormessage = "Failed to install " .. basename .. " to " .. targetpath
642                         end
643                 else
644                         gamedata.errormessage = "Install Mod: unable to find suitable foldername for modpack " 
645                                 .. modfilename
646                 end
647         end
648         
649         if basefolder.type == "mod" then
650                 local targetfolder = basename
651                 
652                 if targetfolder == nil then
653                         targetfolder = modmgr.identify_modname(basefolder.path,"init.lua")
654                 end
655                 
656                 --if heuristic failed try to use current foldername
657                 if targetfolder == nil then
658                         targetfolder = get_last_folder(basefolder.path)
659                 end     
660                 
661                 if targetfolder ~= nil and modmgr.isValidModname(targetfolder) then
662                         local targetpath = engine.get_modpath() .. DIR_DELIM .. targetfolder
663                         engine.copy_dir(basefolder.path,targetpath)
664                 else
665                         gamedata.errormessage = "Install Mod: unable to find real modname for: " 
666                                 .. modfilename
667                 end
668         end
669         
670         engine.delete_dir(modpath)
671 end
672
673 --------------------------------------------------------------------------------
674 function modmgr.handle_rename_modpack_buttons(fields)
675         local oldname = modmgr.global_mods[modmgr.selected_mod]
676         oldname = oldname:sub(0,oldname:find("<") -2)
677         
678         if fields["dlg_rename_modpack_confirm"] ~= nil then
679                 local oldpath = engine.get_modpath() .. DIR_DELIM .. oldname
680                 local targetpath = engine.get_modpath() .. DIR_DELIM .. fields["te_modpack_name"]
681                 engine.copy_dir(oldpath,targetpath,false)
682         end
683         
684         return {
685                 is_dialog = false,
686                 show_buttons = true,
687                 current_tab = engine.setting_get("main_menu_tab")
688                 }
689 end
690 --------------------------------------------------------------------------------
691 function modmgr.handle_configure_world_buttons(fields)
692         if fields["world_config_modlist"] ~= nil then
693                 local event = explode_textlist_event(fields["world_config_modlist"])
694                 modmgr.world_config_selected_mod = event.index
695         end
696
697         if fields["cb_mod_enabled"] ~= nil then
698                 local index = modmgr.get_worldmod_idx()
699                 local modname = modmgr.global_mods[index]
700                                                                 
701                 local parts = modmgr.global_mods[index]:split(DIR_DELIM)
702                 local shortname = parts[#parts]
703                 
704                 if fields["cb_mod_enabled"] == "true" then
705                         modmgr.worldconfig.global_mods[shortname] = true
706                 else
707                         modmgr.worldconfig.global_mods[shortname] = false
708                 end
709         end
710         
711         if fields["cb_hide_gamemods"] ~= nil then
712                 if fields["cb_hide_gamemods"] == "true" then
713                         modmgr.hide_gamemods = true
714                 else
715                         modmgr.hide_gamemods = false
716                 end
717         end
718         
719         if fields["btn_config_world_save"] then
720                 local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
721                 
722                 local filename = worldspec.path ..
723                                 DIR_DELIM .. "world.mt"
724
725                 local worldfile = io.open(filename,"w")
726                 
727                 if worldfile then
728                         worldfile:write("gameid = " .. modmgr.worldconfig.id .. "\n")
729                         for key,value in pairs(modmgr.worldconfig.global_mods) do
730                                 if value then
731                                         worldfile:write("load_mod_" .. key .. " = true" .. "\n")
732                                 else
733                                         worldfile:write("load_mod_" .. key .. " = false" .. "\n")
734                                 end
735                         end
736                         
737                         worldfile:close()
738                 end
739                 
740                 modmgr.worldconfig = nil
741         
742                 return {
743                         is_dialog = false,
744                         show_buttons = true,
745                         current_tab = engine.setting_get("main_menu_tab")
746                 }
747         end
748         
749         if fields["btn_config_world_cancel"] then
750         
751                 modmgr.worldconfig = nil
752                 
753                 return {
754                         is_dialog = false,
755                         show_buttons = true,
756                         current_tab = engine.setting_get("main_menu_tab")
757                 }
758         end
759         
760         if fields["btn_cfgw_enable_all"] then
761                 local worldmodidx = modmgr.get_worldmod_idx()
762                 modname = modmgr.global_mods[worldmodidx]
763
764                 modname = modname:sub(0,modname:find("<") -2)
765
766                 for i=1,#modmgr.global_mods,1 do
767                 
768                         if modmgr.global_mods[i]:find("<MODPACK>") == nil then
769                                 local modpackpart = modmgr.global_mods[i]:sub(0,modname:len())
770                                 
771                                 if modpackpart == modname then
772                                         local parts = modmgr.global_mods[i]:split(DIR_DELIM)
773                                         local shortname = parts[#parts]
774                                         modmgr.worldconfig.global_mods[shortname] = true
775                                 end
776                         end
777                 end
778         end
779         
780         if fields["btn_cfgw_disable_all"] then
781                 local worldmodidx = modmgr.get_worldmod_idx()
782                 modname = modmgr.global_mods[worldmodidx]
783
784                 modname = modname:sub(0,modname:find("<") -2)
785
786                 for i=1,#modmgr.global_mods,1 do
787                         local modpackpart = modmgr.global_mods[i]:sub(0,modname:len())
788                         
789                         if modpackpart == modname then
790                                 local parts = modmgr.global_mods[i]:split(DIR_DELIM)
791                                 local shortname = parts[#parts]
792                                 modmgr.worldconfig.global_mods[shortname] = nil
793                         end
794                 end
795         end
796         
797         return nil
798 end
799 --------------------------------------------------------------------------------
800 function modmgr.handle_delete_mod_buttons(fields)
801         local modname = modmgr.global_mods[modmgr.selected_mod]
802         
803         if modname:find("<MODPACK>") ~= nil then
804                 modname = modname:sub(0,modname:find("<") -2)
805         end
806         
807         if fields["dlg_delete_mod_confirm"] ~= nil then
808                 local oldpath = engine.get_modpath() .. DIR_DELIM .. modname
809                 
810                 if oldpath ~= nil and
811                         oldpath ~= "" and
812                         oldpath ~= engine.get_modpath() then
813                         engine.delete_dir(oldpath)
814                 end
815         end
816         
817         return {
818                 is_dialog = false,
819                 show_buttons = true,
820                 current_tab = engine.setting_get("main_menu_tab")
821                 }
822 end
823
824 --------------------------------------------------------------------------------
825 function modmgr.dialog_delete_mod()
826
827         local modname = modmgr.global_mods[modmgr.selected_mod]
828
829         if modname:find("<MODPACK>") ~= nil then
830                 modname = modname:sub(0,modname:find("<") -2)
831         end
832         
833         local retval = 
834                 "field[1.75,1;10,3;;Are you sure you want to delete ".. modname .. "?;]"..
835                 "button[4,4.2;1,0.5;dlg_delete_mod_confirm;Yes]" ..
836                 "button[6.5,4.2;3,0.5;dlg_delete_mod_cancel;No of course not!]"
837
838         return retval
839 end
840
841 --------------------------------------------------------------------------------
842 function modmgr.init_worldconfig()
843
844         local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
845         
846         if worldspec ~= nil then
847                 --read worldconfig
848                 modmgr.worldconfig = modmgr.get_worldconfig(worldspec.path)
849                 
850                 if modmgr.worldconfig.id == nil or
851                         modmgr.worldconfig.id == "" then
852                         modmgr.worldconfig = nil
853                         return false
854                 end
855                 
856                 return true     
857         end
858
859         return false
860 end
861
862 --------------------------------------------------------------------------------
863 function modmgr.gettab(name)
864         local retval = ""
865         
866         if name == "mod_mgr" then
867                 retval = retval .. modmgr.tab()
868         end
869         
870         if name == "dialog_rename_modpack" then
871                 retval = retval .. modmgr.dialog_rename_modpack()
872         end
873         
874         if name == "dialog_delete_mod" then
875                 retval = retval .. modmgr.dialog_delete_mod()
876         end
877         
878         if name == "dialog_configure_world" then
879                 retval = retval .. modmgr.dialog_configure_world()
880         end
881         
882         return retval
883 end