]> git.lizzy.rs Git - minetest.git/blob - builtin/modmgr.lua
a8ae4f1d8a30662368f91fcadfe5837c9990d2a8
[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 ~= 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                                 print("checkbox " .. shortname .. " enabled")
463                                 retval = retval .. "true"
464                         else
465                                 print("checkbox " .. shortname .. " disabled")
466                                 retval = retval .. "false"
467                         end
468                         
469                         retval = retval .. "]"
470                 end
471         end
472         
473         return retval
474 end
475
476 --------------------------------------------------------------------------------
477 function modmgr.handle_buttons(tab,fields)
478
479         local retval = nil
480         
481         if tab == "mod_mgr" then
482                 retval = modmgr.handle_modmgr_buttons(fields)
483         end
484         
485         if tab == "dialog_rename_modpack" then
486                 retval = modmgr.handle_rename_modpack_buttons(fields)
487         end
488         
489         if tab == "dialog_delete_mod" then
490                 retval = modmgr.handle_delete_mod_buttons(fields)
491         end
492         
493         if tab == "dialog_configure_world" then
494                 retval = modmgr.handle_configure_world_buttons(fields)
495         end
496         
497         return retval
498 end
499
500 --------------------------------------------------------------------------------
501 function modmgr.get_dependencys(modfolder)
502         local filename = modfolder ..
503                                 DIR_DELIM .. "depends.txt"
504
505         local dependencyfile = io.open(filename,"r")
506         
507         local toadd = ""
508         if dependencyfile then
509                 local dependency = dependencyfile:read("*l")
510                 while dependency do
511                         if toadd ~= "" then     
512                                 toadd = toadd .. ","
513                         end
514                         toadd = toadd .. dependency
515                         dependency = dependencyfile:read()
516                 end
517                 dependencyfile:close()
518         else
519                 print(filename .. " not found")
520         end
521
522         return toadd
523 end
524
525
526 --------------------------------------------------------------------------------
527 function modmgr.get_worldconfig(worldpath)
528         local filename = worldpath ..
529                                 DIR_DELIM .. "world.mt"
530
531         local worldfile = io.open(filename,"r")
532         
533         local worldconfig = {}
534         worldconfig.global_mods = {}
535         worldconfig.game_mods = {}
536         
537         if worldfile then
538                 local dependency = worldfile:read("*l")
539                 while dependency do
540                         local parts = dependency:split("=")
541
542                         local key = parts[1]:trim()
543
544                         if key == "gameid" then
545                                 worldconfig.id = parts[2]:trim()
546                         else
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
551                                 else
552                                         print("found disabled mod: >" .. key .. "<")
553                                         worldconfig.global_mods[key] = false
554                                 end
555                         end
556                         dependency = worldfile:read("*l")
557                 end
558                 worldfile:close()
559         else
560                 print(filename .. " not found")
561         end
562         
563         --read gamemods
564         local gamemodpath = engine.get_gamepath() .. DIR_DELIM .. worldconfig.id .. DIR_DELIM .. "mods"
565         
566         print("reading game mods from: " .. dump(gamemodpath))
567         get_mods(gamemodpath,worldconfig.game_mods)
568
569         return worldconfig
570 end
571 --------------------------------------------------------------------------------
572 function modmgr.handle_modmgr_buttons(fields)
573         local retval = {
574                         tab = nil,
575                         is_dialog = nil,
576                         show_buttons = nil,
577                 }
578
579         if fields["modlist"] ~= nil then
580                 local event = explode_textlist_event(fields["modlist"])
581                 modmgr.selected_mod = event.index
582         end
583         
584         if fields["btn_mod_mgr_install_local"] ~= nil then
585                 engine.show_file_open_dialog("mod_mgt_open_dlg","Select Mod File:")
586         end
587         
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
592                 return retval
593         end
594         
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
599                 return retval
600         end
601         
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
606                 return retval
607         end
608         
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)
612         end
613         
614         return nil;
615 end
616
617 --------------------------------------------------------------------------------
618 function modmgr.installmod(modfilename,basename)
619         local modfile = identify_filetype(modfilename)
620         
621         local modpath = modmgr.extract(modfile)
622         
623         if modpath == nil then
624                 gamedata.errormessage = "Install Mod: file: " .. modfile.name ..
625                         "\nInstall Mod: unsupported filetype \"" .. modfile.type .. "\""
626                 return
627         end
628         
629         
630         local basefolder = modmgr.getbasefolder(modpath)
631         
632         if basefolder.type == "modpack" then
633                 local clean_path = nil
634                 
635                 if basename ~= nil then
636                         clean_path = "mp_" .. basename
637                 end
638                 
639                 if clean_path == nil then
640                         clean_path = get_last_folder(cleanup_path(basefolder.path))
641                 end
642                 
643                 if clean_path ~= nil then
644                         local targetpath = engine.get_modpath() .. DIR_DELIM .. clean_path
645                         engine.copy_dir(basefolder.path,targetpath)
646                 else
647                         gamedata.errormessage = "Install Mod: unable to find suitable foldername for modpack " 
648                                 .. modfilename
649                 end
650         end
651         
652         if basefolder.type == "mod" then
653                 local targetfolder = basename
654                 
655                 if targetfolder == nil then
656                         targetfolder = modmgr.identify_modname(basefolder.path,"init.lua")
657                 end
658                 
659                 --if heuristic failed try to use current foldername
660                 if targetfolder == nil then
661                         targetfolder = get_last_folder(basefolder.path)
662                 end     
663                 
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)
667                 else
668                         gamedata.errormessage = "Install Mod: unable to find real modname for: " 
669                                 .. modfilename
670                 end
671         end
672         
673         engine.delete_dir(modpath)
674 end
675
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)
680         
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)
685         end
686         
687         return {
688                 is_dialog = false,
689                 show_buttons = true,
690                 current_tab = engine.setting_get("main_menu_tab")
691                 }
692 end
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
698         end
699
700         if fields["cb_mod_enabled"] ~= nil then
701                 local index = modmgr.get_worldmod_idx()
702                 local modname = modmgr.global_mods[index]
703                                                                 
704                 local parts = modmgr.global_mods[index]:split(DIR_DELIM)
705                 local shortname = parts[#parts]
706                 
707                 if fields["cb_mod_enabled"] == "true" then
708                         modmgr.worldconfig.global_mods[shortname] = true
709                 else
710                         modmgr.worldconfig.global_mods[shortname] = false
711                 end
712         end
713         
714         if fields["cb_hide_gamemods"] ~= nil then
715                 if fields["cb_hide_gamemods"] == "true" then
716                         modmgr.hide_gamemods = true
717                 else
718                         modmgr.hide_gamemods = false
719                 end
720         end
721         
722         if fields["btn_config_world_save"] then
723                 local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
724                 
725                 local filename = worldspec.path ..
726                                 DIR_DELIM .. "world.mt"
727
728                 local worldfile = io.open(filename,"w")
729                 
730                 if worldfile then
731                         worldfile:write("gameid = " .. modmgr.worldconfig.id .. "\n")
732                         for key,value in pairs(modmgr.worldconfig.global_mods) do
733                                 if value then
734                                         worldfile:write("load_mod_" .. key .. " = true" .. "\n")
735                                 else
736                                         worldfile:write("load_mod_" .. key .. " = false" .. "\n")
737                                 end
738                         end
739                         
740                         worldfile:close()
741                 end
742                 
743                 modmgr.worldconfig = nil
744         
745                 return {
746                         is_dialog = false,
747                         show_buttons = true,
748                         current_tab = engine.setting_get("main_menu_tab")
749                 }
750         end
751         
752         if fields["btn_config_world_cancel"] then
753         
754                 modmgr.worldconfig = nil
755                 
756                 return {
757                         is_dialog = false,
758                         show_buttons = true,
759                         current_tab = engine.setting_get("main_menu_tab")
760                 }
761         end
762         
763         if fields["btn_cfgw_enable_all"] then
764                 local worldmodidx = modmgr.get_worldmod_idx()
765                 modname = modmgr.global_mods[worldmodidx]
766
767                 modname = modname:sub(0,modname:find("<") -2)
768
769                 for i=1,#modmgr.global_mods,1 do
770                 
771                         if modmgr.global_mods[i]:find("<MODPACK>") == nil then
772                                 local modpackpart = modmgr.global_mods[i]:sub(0,modname:len())
773                                 
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
778                                 end
779                         end
780                 end
781         end
782         
783         if fields["btn_cfgw_disable_all"] then
784                 local worldmodidx = modmgr.get_worldmod_idx()
785                 modname = modmgr.global_mods[worldmodidx]
786
787                 modname = modname:sub(0,modname:find("<") -2)
788
789                 for i=1,#modmgr.global_mods,1 do
790                         local modpackpart = modmgr.global_mods[i]:sub(0,modname:len())
791                         
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
796                         end
797                 end
798         end
799         
800         return nil
801 end
802 --------------------------------------------------------------------------------
803 function modmgr.handle_delete_mod_buttons(fields)
804         local modname = modmgr.global_mods[modmgr.selected_mod]
805         
806         if modname:find("<MODPACK>") ~= nil then
807                 modname = modname:sub(0,modname:find("<") -2)
808         end
809         
810         if fields["dlg_delete_mod_confirm"] ~= nil then
811                 local oldpath = engine.get_modpath() .. DIR_DELIM .. modname
812                 
813                 if oldpath ~= nil and
814                         oldpath ~= "" and
815                         oldpath ~= engine.get_modpath() then
816                         engine.delete_dir(oldpath)
817                 end
818         end
819         
820         return {
821                 is_dialog = false,
822                 show_buttons = true,
823                 current_tab = engine.setting_get("main_menu_tab")
824                 }
825 end
826
827 --------------------------------------------------------------------------------
828 function modmgr.dialog_delete_mod()
829
830         local modname = modmgr.global_mods[modmgr.selected_mod]
831
832         if modname:find("<MODPACK>") ~= nil then
833                 modname = modname:sub(0,modname:find("<") -2)
834         end
835         
836         local retval = 
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!]"
840
841         return retval
842 end
843
844 --------------------------------------------------------------------------------
845 function modmgr.init_worldconfig()
846
847         local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
848         
849         if worldspec ~= nil then
850                 --read worldconfig
851                 modmgr.worldconfig = modmgr.get_worldconfig(worldspec.path)
852                 
853                 if modmgr.worldconfig.id == nil or
854                         modmgr.worldconfig.id == "" then
855                         modmgr.worldconfig = nil
856                         return false
857                 end
858                 
859                 return true     
860         end
861
862         return false
863 end
864
865 --------------------------------------------------------------------------------
866 function modmgr.gettab(name)
867         local retval = ""
868         
869         if name == "mod_mgr" then
870                 retval = retval .. modmgr.tab()
871         end
872         
873         if name == "dialog_rename_modpack" then
874                 retval = retval .. modmgr.dialog_rename_modpack()
875         end
876         
877         if name == "dialog_delete_mod" then
878                 retval = retval .. modmgr.dialog_delete_mod()
879         end
880         
881         if name == "dialog_configure_world" then
882                 retval = retval .. modmgr.dialog_configure_world()
883         end
884         
885         return retval
886 end