]> git.lizzy.rs Git - minetest.git/blob - builtin/modmgr.lua
045b529859a7a59ae78c0416cc4b59d5b0d62a35
[minetest.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:find("<MODPACK>") ~= nil then
403                         modname = modname:sub(0,modname:find("<") -2)
404                         modpack_selected = true
405                 end
406                 
407                 local parts = modmgr.global_mods[worldmodidx]:split(DIR_DELIM)
408                 shortname = parts[#parts]
409                 
410                 modfolder = engine.get_modpath() .. DIR_DELIM .. modname
411         end
412
413         local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
414         
415         local retval =
416                 "size[11,6.5]" ..
417                 "label[1.5,-0.25;World: " .. worldspec.name .. "]"
418                 
419         if modmgr.hide_gamemods then
420                 retval = retval .. "checkbox[5.5,6.15;cb_hide_gamemods;Hide Game;true]"
421         else
422                 retval = retval .. "checkbox[5.5,6.15;cb_hide_gamemods;Hide Game;false]"
423         end
424         retval = retval ..
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;"
428                 
429
430         if not modmgr.hide_gamemods then
431                 retval = retval .. modmgr.render_gamemodlist()
432         end
433         
434         retval = retval .. modmgr.render_worldmodlist() 
435
436         retval = retval .. ";" .. modmgr.world_config_selected_mod .."]"
437         
438         if not gamemod_selected then
439                 retval = retval ..
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]"
447         
448                 if modpack_selected then
449                         retval = retval ..
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]"
452                 else
453                         retval = retval .. 
454                                 "checkbox[0,0.8;cb_mod_enabled;enabled;"
455                         
456                         if modmgr.worldconfig.global_mods[shortname] then
457                                 print("checkbox " .. shortname .. " enabled")
458                                 retval = retval .. "true"
459                         else
460                                 print("checkbox " .. shortname .. " disabled")
461                                 retval = retval .. "false"
462                         end
463                         
464                         retval = retval .. "]"
465                 end
466         end
467         
468         return retval
469 end
470
471 --------------------------------------------------------------------------------
472 function modmgr.handle_buttons(tab,fields)
473
474         local retval = nil
475         
476         if tab == "mod_mgr" then
477                 retval = modmgr.handle_modmgr_buttons(fields)
478         end
479         
480         if tab == "dialog_rename_modpack" then
481                 retval = modmgr.handle_rename_modpack_buttons(fields)
482         end
483         
484         if tab == "dialog_delete_mod" then
485                 retval = modmgr.handle_delete_mod_buttons(fields)
486         end
487         
488         if tab == "dialog_configure_world" then
489                 retval = modmgr.handle_configure_world_buttons(fields)
490         end
491         
492         return retval
493 end
494
495 --------------------------------------------------------------------------------
496 function modmgr.get_dependencys(modfolder)
497         local filename = modfolder ..
498                                 DIR_DELIM .. "depends.txt"
499
500         local dependencyfile = io.open(filename,"r")
501         
502         local toadd = ""
503         if dependencyfile then
504                 local dependency = dependencyfile:read("*l")
505                 while dependency do
506                         if toadd ~= "" then     
507                                 toadd = toadd .. ","
508                         end
509                         toadd = toadd .. dependency
510                         dependency = dependencyfile:read()
511                 end
512                 dependencyfile:close()
513         else
514                 print(filename .. " not found")
515         end
516
517         return toadd
518 end
519
520
521 --------------------------------------------------------------------------------
522 function modmgr.get_worldconfig(worldpath)
523         local filename = worldpath ..
524                                 DIR_DELIM .. "world.mt"
525
526         local worldfile = io.open(filename,"r")
527         
528         local worldconfig = {}
529         worldconfig.global_mods = {}
530         worldconfig.game_mods = {}
531         
532         if worldfile then
533                 local dependency = worldfile:read("*l")
534                 while dependency do
535                         local parts = dependency:split("=")
536
537                         local key = parts[1]:trim()
538
539                         if key == "gameid" then
540                                 worldconfig.id = parts[2]:trim()
541                         else
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
546                                 else
547                                         print("found disabled mod: >" .. key .. "<")
548                                         worldconfig.global_mods[key] = false
549                                 end
550                         end
551                         dependency = worldfile:read("*l")
552                 end
553                 worldfile:close()
554         else
555                 print(filename .. " not found")
556         end
557         
558         --read gamemods
559         local gamemodpath = engine.get_gamepath() .. DIR_DELIM .. worldconfig.id .. DIR_DELIM .. "mods"
560         
561         print("reading game mods from: " .. dump(gamemodpath))
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                         engine.copy_dir(basefolder.path,targetpath)
641                 else
642                         gamedata.errormessage = "Install Mod: unable to find suitable foldername for modpack " 
643                                 .. modfilename
644                 end
645         end
646         
647         if basefolder.type == "mod" then
648                 local targetfolder = basename
649                 
650                 if targetfolder == nil then
651                         targetfolder = modmgr.identify_modname(basefolder.path,"init.lua")
652                 end
653                 
654                 --if heuristic failed try to use current foldername
655                 if targetfolder == nil then
656                         targetfolder = get_last_folder(basefolder.path)
657                 end     
658                 
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)
662                 else
663                         gamedata.errormessage = "Install Mod: unable to find real modname for: " 
664                                 .. modfilename
665                 end
666         end
667         
668         engine.delete_dir(modpath)
669 end
670
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)
675         
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)
680         end
681         
682         return {
683                 is_dialog = false,
684                 show_buttons = true,
685                 current_tab = engine.setting_get("main_menu_tab")
686                 }
687 end
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
693         end
694
695         if fields["cb_mod_enabled"] ~= nil then
696                 local index = modmgr.get_worldmod_idx()
697                 local modname = modmgr.global_mods[index]
698                                                                 
699                 local parts = modmgr.global_mods[index]:split(DIR_DELIM)
700                 local shortname = parts[#parts]
701                 
702                 if fields["cb_mod_enabled"] == "true" then
703                         modmgr.worldconfig.global_mods[shortname] = true
704                 else
705                         modmgr.worldconfig.global_mods[shortname] = false
706                 end
707         end
708         
709         if fields["cb_hide_gamemods"] ~= nil then
710                 if fields["cb_hide_gamemods"] == "true" then
711                         modmgr.hide_gamemods = true
712                 else
713                         modmgr.hide_gamemods = false
714                 end
715         end
716         
717         if fields["btn_config_world_save"] then
718                 local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
719                 
720                 local filename = worldspec.path ..
721                                 DIR_DELIM .. "world.mt"
722
723                 local worldfile = io.open(filename,"w")
724                 
725                 if worldfile then
726                         worldfile:write("gameid = " .. modmgr.worldconfig.id .. "\n")
727                         for key,value in pairs(modmgr.worldconfig.global_mods) do
728                                 if value then
729                                         worldfile:write("load_mod_" .. key .. " = true" .. "\n")
730                                 else
731                                         worldfile:write("load_mod_" .. key .. " = false" .. "\n")
732                                 end
733                         end
734                         
735                         worldfile:close()
736                 end
737                 
738                 modmgr.worldconfig = nil
739         
740                 return {
741                         is_dialog = false,
742                         show_buttons = true,
743                         current_tab = engine.setting_get("main_menu_tab")
744                 }
745         end
746         
747         if fields["btn_config_world_cancel"] then
748         
749                 modmgr.worldconfig = nil
750                 
751                 return {
752                         is_dialog = false,
753                         show_buttons = true,
754                         current_tab = engine.setting_get("main_menu_tab")
755                 }
756         end
757         
758         if fields["btn_cfgw_enable_all"] then
759                 local worldmodidx = modmgr.get_worldmod_idx()
760                 modname = modmgr.global_mods[worldmodidx]
761
762                 modname = modname:sub(0,modname:find("<") -2)
763
764                 for i=1,#modmgr.global_mods,1 do
765                 
766                         if modmgr.global_mods[i]:find("<MODPACK>") == nil then
767                                 local modpackpart = modmgr.global_mods[i]:sub(0,modname:len())
768                                 
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
773                                 end
774                         end
775                 end
776         end
777         
778         if fields["btn_cfgw_disable_all"] then
779                 local worldmodidx = modmgr.get_worldmod_idx()
780                 modname = modmgr.global_mods[worldmodidx]
781
782                 modname = modname:sub(0,modname:find("<") -2)
783
784                 for i=1,#modmgr.global_mods,1 do
785                         local modpackpart = modmgr.global_mods[i]:sub(0,modname:len())
786                         
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
791                         end
792                 end
793         end
794         
795         return nil
796 end
797 --------------------------------------------------------------------------------
798 function modmgr.handle_delete_mod_buttons(fields)
799         local modname = modmgr.global_mods[modmgr.selected_mod]
800         
801         if modname:find("<MODPACK>") ~= nil then
802                 modname = modname:sub(0,modname:find("<") -2)
803         end
804         
805         if fields["dlg_delete_mod_confirm"] ~= nil then
806                 local oldpath = engine.get_modpath() .. DIR_DELIM .. modname
807                 
808                 if oldpath ~= nil and
809                         oldpath ~= "" and
810                         oldpath ~= engine.get_modpath() then
811                         engine.delete_dir(oldpath)
812                 end
813         end
814         
815         return {
816                 is_dialog = false,
817                 show_buttons = true,
818                 current_tab = engine.setting_get("main_menu_tab")
819                 }
820 end
821
822 --------------------------------------------------------------------------------
823 function modmgr.dialog_delete_mod()
824
825         local modname = modmgr.global_mods[modmgr.selected_mod]
826
827         if modname:find("<MODPACK>") ~= nil then
828                 modname = modname:sub(0,modname:find("<") -2)
829         end
830         
831         local retval = 
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!]"
835
836         return retval
837 end
838
839 --------------------------------------------------------------------------------
840 function modmgr.init_worldconfig()
841
842         local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
843         
844         if worldspec ~= nil then
845                 --read worldconfig
846                 modmgr.worldconfig = modmgr.get_worldconfig(worldspec.path)
847                 
848                 if modmgr.worldconfig.id == nil or
849                         modmgr.worldconfig.id == "" then
850                         modmgr.worldconfig = nil
851                         return false
852                 end
853                 
854                 return true     
855         end
856
857         return false
858 end
859
860 --------------------------------------------------------------------------------
861 function modmgr.gettab(name)
862         local retval = ""
863         
864         if name == "mod_mgr" then
865                 retval = retval .. modmgr.tab()
866         end
867         
868         if name == "dialog_rename_modpack" then
869                 retval = retval .. modmgr.dialog_rename_modpack()
870         end
871         
872         if name == "dialog_delete_mod" then
873                 retval = retval .. modmgr.dialog_delete_mod()
874         end
875         
876         if name == "dialog_configure_world" then
877                 retval = retval .. modmgr.dialog_configure_world()
878         end
879         
880         return retval
881 end