]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/modmgr.lua
Fix background/overlay/footer/header handling
[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,modpack)
20
21         local mods = engine.get_dirlist(path,true)
22         for i=1,#mods,1 do
23                 local toadd = {}
24                 local modpackfile = nil
25                 
26                 toadd.name              = mods[i]
27                 toadd.path              = path .. DIR_DELIM .. mods[i] .. DIR_DELIM
28                 if modpack ~= nil and
29                         modpack ~= "" then
30                         toadd.modpack   = modpack
31                 else
32                         local filename = path .. DIR_DELIM .. mods[i] .. DIR_DELIM .. "modpack.txt"
33                         local error = nil
34                         modpackfile,error = io.open(filename,"r")
35                 end
36                         
37                 if modpackfile ~= nil then
38                         modpackfile:close()
39                         toadd.is_modpack = true
40                         table.insert(retval,toadd)
41                         get_mods(path .. DIR_DELIM .. mods[i],retval,mods[i])
42                 else
43                         table.insert(retval,toadd)
44                 end
45         end
46 end
47
48 --modmanager implementation
49 modmgr = {}
50
51 --------------------------------------------------------------------------------
52 function modmgr.extract(modfile)
53         if modfile.type == "zip" then
54                 local tempfolder = os.tempfolder()
55                 
56                 if tempfolder ~= nil and
57                         tempfodler ~= "" then
58                         engine.create_dir(tempfolder)
59                         engine.extract_zip(modfile.name,tempfolder)
60                         return tempfolder
61                 end
62         end
63 end
64
65 -------------------------------------------------------------------------------
66 function modmgr.getbasefolder(temppath)
67
68         if temppath == nil then
69                 return {
70                 type = "invalid",
71                 path = ""
72                 }
73         end
74
75         local testfile = io.open(temppath .. DIR_DELIM .. "init.lua","r")
76         if testfile ~= nil then
77                 testfile:close()
78                 return {
79                                 type="mod",
80                                 path=temppath
81                                 }
82         end
83         
84         testfile = io.open(temppath .. DIR_DELIM .. "modpack.txt","r")
85         if testfile ~= nil then
86                 testfile:close()
87                 return {
88                                 type="modpack",
89                                 path=temppath
90                                 }
91         end
92         
93         local subdirs = engine.get_dirlist(temppath,true)
94         
95         --only single mod or modpack allowed
96         if #subdirs ~= 1 then
97                 return {
98                         type = "invalid",
99                         path = ""
100                         }
101         end
102
103         testfile = 
104         io.open(temppath .. DIR_DELIM .. subdirs[1] ..DIR_DELIM .."init.lua","r")
105         if testfile ~= nil then
106                 testfile:close()
107                 return {
108                         type="mod",
109                         path= temppath .. DIR_DELIM .. subdirs[1]
110                         }
111         end
112         
113         testfile = 
114         io.open(temppath .. DIR_DELIM .. subdirs[1] ..DIR_DELIM .."modpack.txt","r")
115         if testfile ~= nil then
116                 testfile:close()
117                 return {
118                         type="modpack",
119                         path=temppath ..  DIR_DELIM .. subdirs[1]
120                         }
121         end
122
123         return {
124                 type = "invalid",
125                 path = ""
126                 }
127 end
128
129 --------------------------------------------------------------------------------
130 function modmgr.isValidModname(modpath)
131         if modpath:find("-") ~= nil then
132                 return false
133         end
134         
135         return true
136 end
137
138 --------------------------------------------------------------------------------
139 function modmgr.parse_register_line(line)
140         local pos1 = line:find("\"")
141         local pos2 = nil
142         if pos1 ~= nil then
143                 pos2 = line:find("\"",pos1+1)
144         end
145         
146         if pos1 ~= nil and pos2 ~= nil then
147                 local item = line:sub(pos1+1,pos2-1)
148                 
149                 if item ~= nil and
150                         item ~= "" then
151                         local pos3 = item:find(":")
152                         
153                         if pos3 ~= nil then
154                                 return item:sub(1,pos3-1)
155                         end
156                 end
157         end
158         return nil
159 end
160
161 --------------------------------------------------------------------------------
162 function modmgr.parse_dofile_line(modpath,line)
163         local pos1 = line:find("\"")
164         local pos2 = nil
165         if pos1 ~= nil then
166                 pos2 = line:find("\"",pos1+1)
167         end
168         
169         if pos1 ~= nil and pos2 ~= nil then
170                 local filename = line:sub(pos1+1,pos2-1)
171                 
172                 if filename ~= nil and
173                         filename ~= "" and
174                         filename:find(".lua") then
175                         return modmgr.identify_modname(modpath,filename)
176                 end
177         end
178         return nil
179 end
180
181 --------------------------------------------------------------------------------
182 function modmgr.identify_modname(modpath,filename)
183         local testfile = io.open(modpath .. DIR_DELIM .. filename,"r")
184         if testfile ~= nil then
185                 local line = testfile:read()
186                 
187                 while line~= nil do
188                         local modname = nil
189                 
190                         if line:find("minetest.register_tool") then
191                                 modname = modmgr.parse_register_line(line)
192                         end
193                         
194                         if line:find("minetest.register_craftitem") then
195                                 modname = modmgr.parse_register_line(line)
196                         end
197                         
198                         
199                         if line:find("minetest.register_node") then
200                                 modname = modmgr.parse_register_line(line)
201                         end
202                         
203                         if line:find("dofile") then
204                                 modname = modmgr.parse_dofile_line(modpath,line)
205                         end
206                 
207                         if modname ~= nil then
208                                 testfile:close()
209                                 return modname
210                         end
211                         
212                         line = testfile:read()
213                 end
214                 testfile:close()
215         end
216         
217         return nil
218 end
219
220 --------------------------------------------------------------------------------
221 function modmgr.tab()
222
223         if modmgr.global_mods == nil then
224                 modmgr.refresh_globals()
225         end
226
227         if modmgr.selected_mod == nil then
228                 modmgr.selected_mod = 1
229         end
230         
231         local retval = 
232                 "vertlabel[0,-0.25;MODS]" ..
233                 "label[0.8,-0.25;Installed Mods:]" ..
234                 "textlist[0.75,0.25;4.5,4.3;modlist;" ..
235                 modmgr.render_modlist(modmgr.global_mods) .. 
236                 ";" .. modmgr.selected_mod .. "]"
237
238         retval = retval ..
239                 "button[1,4.85;2,0.5;btn_mod_mgr_install_local;Install]" ..
240                 "button[3,4.85;2,0.5;btn_mod_mgr_download;Download]"
241                 
242         local selected_mod = nil
243                 
244         if filterlist.size(modmgr.global_mods) >= modmgr.selected_mod then
245                 selected_mod = filterlist.get_list(modmgr.global_mods)[modmgr.selected_mod]
246         end
247         
248         if selected_mod ~= nil then
249                 if selected_mod.is_modpack then
250                         retval = retval .. "button[10,4.85;2,0.5;btn_mod_mgr_rename_modpack;Rename]"
251                 else
252                 --show dependencies
253                         retval = retval .. 
254                                 "label[6,1.9;Depends:]" ..
255                                 "textlist[6,2.4;5.7,2;deplist;"
256                                 
257                         toadd = modmgr.get_dependencies(selected_mod.path)
258                         
259                         retval = retval .. toadd .. ";0;true,false]"
260                         
261                         --TODO read modinfo
262                 end
263                 --show delete button
264                 retval = retval .. "button[8,4.85;2,0.5;btn_mod_mgr_delete_mod;Delete]"
265         end
266         return retval
267 end
268
269 --------------------------------------------------------------------------------
270 function modmgr.dialog_rename_modpack()
271
272         local mod = filterlist.get_list(modmgr.modlist)[modmgr.selected_mod]
273         
274         local retval = 
275                 "label[1.75,1;Rename Modpack:]"..
276                 "field[4.5,1.4;6,0.5;te_modpack_name;;" ..
277                 mod.name ..
278                 "]" ..
279                 "button[5,4.2;2.6,0.5;dlg_rename_modpack_confirm;Accept]" ..
280                 "button[7.5,4.2;2.8,0.5;dlg_rename_modpack_cancel;Cancel]"
281
282         return retval
283 end
284
285 --------------------------------------------------------------------------------
286 function modmgr.precheck()
287
288         if modmgr.world_config_selected_world == nil then
289                 modmgr.world_config_selected_world = 1
290         end
291         
292         if modmgr.world_config_selected_mod == nil then
293                 modmgr.world_config_selected_mod = 1
294         end
295         
296         if modmgr.hide_gamemods == nil then
297                 modmgr.hide_gamemods = true
298         end
299         
300         if modmgr.hide_modpackcontents == nil then
301                 modmgr.hide_modpackcontents = true
302         end
303 end
304
305 --------------------------------------------------------------------------------
306 function modmgr.render_modlist(render_list)
307         local retval = ""
308         
309         if render_list == nil then
310                 if modmgr.global_mods == nil then
311                         modmgr.refresh_globals()
312                 end
313                 render_list = modmgr.global_mods
314         end
315         
316         local list = filterlist.get_list(render_list)
317         local last_modpack = nil
318         
319         for i,v in ipairs(list) do
320                 if retval ~= "" then
321                         retval = retval ..","
322                 end
323                 
324                 if v.is_modpack then
325                         local rawlist = filterlist.get_raw_list(render_list)
326                         
327                         local all_enabled = true
328                         for j=1,#rawlist,1 do
329                                 if rawlist[j].modpack == list[i].name and
330                                         rawlist[j].enabled ~= true then
331                                                 all_enabled = false
332                                                 break
333                                 end
334                         end
335                         
336                         if all_enabled == false then
337                                 retval = retval .. mt_color_grey
338                         else
339                                 retval = retval .. mt_color_dark_green
340                         end
341                 end
342                 
343                 if v.typ == "game_mod" then
344                         retval = retval .. mt_color_blue
345                 else
346                         if v.enabled then
347                                 retval = retval .. mt_color_green
348                         end
349                 end
350                 if v.modpack  ~= nil then
351                         retval = retval .. "    "
352                 end
353                 retval = retval .. v.name
354         end
355         
356         return retval
357 end
358
359 --------------------------------------------------------------------------------
360 function modmgr.dialog_configure_world()
361         modmgr.precheck()
362         
363         local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
364         local mod = filterlist.get_list(modmgr.modlist)[modmgr.world_config_selected_mod]
365         
366         local retval =
367                 "size[11,6.5]" ..
368                 "label[1.5,-0.25;World: " .. worldspec.name .. "]"
369                 
370         if modmgr.hide_gamemods then
371                 retval = retval .. "checkbox[0,5.75;cb_hide_gamemods;Hide Game;true]"
372         else
373                 retval = retval .. "checkbox[0,5.75;cb_hide_gamemods;Hide Game;false]"
374         end
375         
376         if modmgr.hide_modpackcontents then
377                 retval = retval .. "checkbox[2,5.75;cb_hide_mpcontent;Hide mp content;true]"
378         else
379                 retval = retval .. "checkbox[2,5.75;cb_hide_mpcontent;Hide mp content;false]"
380         end
381         
382         if mod == nil then
383                 mod = {name=""}
384         end
385         retval = retval ..
386                 "label[0,0.45;Mod:]" ..
387                 "label[0.75,0.45;" .. mod.name .. "]" ..
388                 "label[0,1;Depends:]" ..
389                 "textlist[0,1.5;5,4.25;world_config_depends;" ..
390                 modmgr.get_dependencies(mod.path) .. ";0]" ..
391                 "button[9.25,6.35;2,0.5;btn_config_world_save;Save]" ..
392                 "button[7.4,6.35;2,0.5;btn_config_world_cancel;Cancel]" ..
393                 "button[5.5,-0.125;5.75,0.5;btn_all_mods;Enable all Mods]" ..
394                 "textlist[5.5,0.5;5.5,5.75;world_config_modlist;"
395                 
396
397         retval = retval .. modmgr.render_modlist(modmgr.modlist)
398
399         retval = retval .. ";" .. modmgr.world_config_selected_mod .."]"
400         
401         return retval
402 end
403
404 --------------------------------------------------------------------------------
405 function modmgr.handle_buttons(tab,fields)
406
407         local retval = nil
408         
409         if tab == "mod_mgr" then
410                 retval = modmgr.handle_modmgr_buttons(fields)
411         end
412         
413         if tab == "dialog_rename_modpack" then
414                 retval = modmgr.handle_rename_modpack_buttons(fields)
415         end
416         
417         if tab == "dialog_delete_mod" then
418                 retval = modmgr.handle_delete_mod_buttons(fields)
419         end
420         
421         if tab == "dialog_configure_world" then
422                 retval = modmgr.handle_configure_world_buttons(fields)
423         end
424         
425         return retval
426 end
427
428 --------------------------------------------------------------------------------
429 function modmgr.get_dependencies(modfolder)
430         local toadd = ""
431         if modfolder ~= nil then
432                 local filename = modfolder ..
433                                         DIR_DELIM .. "depends.txt"
434         
435                 local dependencyfile = io.open(filename,"r")
436                 
437                 if dependencyfile then
438                         local dependency = dependencyfile:read("*l")
439                         while dependency do
440                                 if toadd ~= "" then     
441                                         toadd = toadd .. ","
442                                 end
443                                 toadd = toadd .. dependency
444                                 dependency = dependencyfile:read()
445                         end
446                         dependencyfile:close()
447                 end
448         end
449
450         return toadd
451 end
452
453
454 --------------------------------------------------------------------------------
455 function modmgr.get_worldconfig(worldpath)
456         local filename = worldpath ..
457                                 DIR_DELIM .. "world.mt"
458
459         local worldfile = io.open(filename,"r")
460         
461         local worldconfig = {}
462         worldconfig.global_mods = {}
463         worldconfig.game_mods = {}
464         
465         if worldfile then
466                 local dependency = worldfile:read("*l")
467                 while dependency do
468                         local parts = dependency:split("=")
469
470                         local key = parts[1]:trim()
471
472                         if key == "gameid" then
473                                 worldconfig.id = parts[2]:trim()
474                         else
475                                 local key = parts[1]:trim():sub(10)
476                                 if parts[2]:trim() == "true" then
477                                         worldconfig.global_mods[key] = true
478                                 else
479                                         worldconfig.global_mods[key] = false
480                                 end
481                         end
482                         dependency = worldfile:read("*l")
483                 end
484                 worldfile:close()
485         else
486                 print("Modmgr: " .. filename .. " not found")
487         end
488         
489         --read gamemods
490         local gamemodpath = engine.get_gamepath() .. DIR_DELIM .. worldconfig.id .. DIR_DELIM .. "mods"
491         
492         get_mods(gamemodpath,worldconfig.game_mods)
493
494         return worldconfig
495 end
496 --------------------------------------------------------------------------------
497 function modmgr.handle_modmgr_buttons(fields)
498         local retval = {
499                         tab = nil,
500                         is_dialog = nil,
501                         show_buttons = nil,
502                 }
503
504         if fields["modlist"] ~= nil then
505                 local event = explode_textlist_event(fields["modlist"])
506                 modmgr.selected_mod = event.index
507         end
508         
509         if fields["btn_mod_mgr_install_local"] ~= nil then
510                 engine.show_file_open_dialog("mod_mgt_open_dlg","Select Mod File:")
511         end
512         
513         if fields["btn_mod_mgr_download"] ~= nil then
514                 retval.current_tab = "dialog_modstore_unsorted"
515                 retval.is_dialog = true
516                 retval.show_buttons = false
517                 return retval
518         end
519         
520         if fields["btn_mod_mgr_rename_modpack"] ~= nil then
521                 retval.current_tab = "dialog_rename_modpack"
522                 retval.is_dialog = true
523                 retval.show_buttons = false
524                 return retval
525         end
526         
527         if fields["btn_mod_mgr_delete_mod"] ~= nil then
528                 retval.current_tab = "dialog_delete_mod"
529                 retval.is_dialog = true
530                 retval.show_buttons = false
531                 return retval
532         end
533         
534         if fields["mod_mgt_open_dlg_accepted"] ~= nil and
535                 fields["mod_mgt_open_dlg_accepted"] ~= "" then
536                 modmgr.installmod(fields["mod_mgt_open_dlg_accepted"],nil)
537         end
538         
539         return nil;
540 end
541
542 --------------------------------------------------------------------------------
543 function modmgr.installmod(modfilename,basename)
544         local modfile = modmgr.identify_filetype(modfilename)
545         local modpath = modmgr.extract(modfile)
546         
547         if modpath == nil then
548                 gamedata.errormessage = "Install Mod: file: " .. modfile.name ..
549                         "\nInstall Mod: unsupported filetype \"" .. modfile.type .. "\""
550                 return
551         end
552         
553         
554         local basefolder = modmgr.getbasefolder(modpath)
555         
556         if basefolder.type == "modpack" then
557                 local clean_path = nil
558                 
559                 if basename ~= nil then
560                         clean_path = "mp_" .. basename
561                 end
562                 
563                 if clean_path == nil then
564                         clean_path = get_last_folder(cleanup_path(basefolder.path))
565                 end
566                 
567                 if clean_path ~= nil then
568                         local targetpath = engine.get_modpath() .. DIR_DELIM .. clean_path
569                         if not engine.copy_dir(basefolder.path,targetpath) then
570                                 gamedata.errormessage = "Failed to install " .. basename .. " to " .. targetpath
571                         end
572                 else
573                         gamedata.errormessage = "Install Mod: unable to find suitable foldername for modpack " 
574                                 .. modfilename
575                 end
576         end
577         
578         if basefolder.type == "mod" then
579                 local targetfolder = basename
580                 
581                 if targetfolder == nil then
582                         targetfolder = modmgr.identify_modname(basefolder.path,"init.lua")
583                 end
584                 
585                 --if heuristic failed try to use current foldername
586                 if targetfolder == nil then
587                         targetfolder = get_last_folder(basefolder.path)
588                 end     
589                 
590                 if targetfolder ~= nil and modmgr.isValidModname(targetfolder) then
591                         local targetpath = engine.get_modpath() .. DIR_DELIM .. targetfolder
592                         engine.copy_dir(basefolder.path,targetpath)
593                 else
594                         gamedata.errormessage = "Install Mod: unable to find real modname for: " 
595                                 .. modfilename
596                 end
597         end
598         
599         engine.delete_dir(modpath)
600
601         modmgr.refresh_globals()
602
603 end
604
605 --------------------------------------------------------------------------------
606 function modmgr.handle_rename_modpack_buttons(fields)
607         
608         if fields["dlg_rename_modpack_confirm"] ~= nil then
609                 local mod = filterlist.get_list(modmgr.modlist)[modmgr.selected_mod]
610                 local oldpath = engine.get_modpath() .. DIR_DELIM .. mod.name
611                 local targetpath = engine.get_modpath() .. DIR_DELIM .. fields["te_modpack_name"]
612                 engine.copy_dir(oldpath,targetpath,false)
613         end
614         
615         return {
616                 is_dialog = false,
617                 show_buttons = true,
618                 current_tab = engine.setting_get("main_menu_tab")
619                 }
620 end
621 --------------------------------------------------------------------------------
622 function modmgr.handle_configure_world_buttons(fields)
623         if fields["world_config_modlist"] ~= nil then
624                 local event = explode_textlist_event(fields["world_config_modlist"])
625                 modmgr.world_config_selected_mod = event.index
626                 
627                 if event.typ == "DCL" then
628                         local mod = filterlist.get_list(modmgr.modlist)[event.index]
629                         
630                         if mod.typ == "game_mod" then
631                                 return nil
632                         end
633                         
634                         if not mod.is_modpack then
635                                 mod.enabled = not mod.enabled
636                         else
637                                 local list = filterlist.get_raw_list(modmgr.modlist)
638                                 local toset = nil
639                                 
640                                 for i=1,#list,1 do
641                                         if list[i].modpack == mod.name then
642                                                 if toset == nil then
643                                                         toset = not list[i].enabled
644                                                 end
645                                                 
646                                                 list[i].enabled = toset
647                                         end
648                                 end
649                         end
650                 end
651         end
652
653         if fields["cb_hide_gamemods"] ~= nil then
654                 local current = filterlist.get_filtercriteria(modmgr.modlist)
655                 
656                 if current == nil then
657                         current = {}
658                 end
659
660                 if fields["cb_hide_gamemods"] == "true" then
661                         current.hide_game = true
662                         modmgr.hide_gamemods = true
663                 else
664                         current.hide_game = false
665                         modmgr.hide_gamemods = false
666                 end
667                 
668                 filterlist.set_filtercriteria(modmgr.modlist,current)
669         end
670         
671                 if fields["cb_hide_mpcontent"] ~= nil then
672                 local current = filterlist.get_filtercriteria(modmgr.modlist)
673                 
674                 if current == nil then
675                         current = {}
676                 end
677
678                 if fields["cb_hide_mpcontent"] == "true" then
679                         current.hide_modpackcontents = true
680                         modmgr.hide_modpackcontents = true
681                 else
682                         current.hide_modpackcontents = false
683                         modmgr.hide_modpackcontents = false
684                 end
685                 
686                 filterlist.set_filtercriteria(modmgr.modlist,current)
687         end
688         
689         if fields["btn_config_world_save"] then
690                 local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
691                 
692                 local filename = worldspec.path ..
693                                 DIR_DELIM .. "world.mt"
694
695                 local worldfile = io.open(filename,"w")
696                 
697                 if worldfile then
698                         worldfile:write("gameid = " .. modmgr.worldconfig.id .. "\n")
699                         
700                         local rawlist = filterlist.get_raw_list(modmgr.modlist)
701                         
702                         for i=1,#rawlist,1 do
703                         
704                                 if not rawlist[i].is_modpack and
705                                         rawlist[i].typ ~= "game_mod" then
706                                         if rawlist[i].enabled then
707                                                 worldfile:write("load_mod_" .. rawlist[i].name .. " = true" .. "\n")
708                                         else
709                                                 worldfile:write("load_mod_" .. rawlist[i].name .. " = false" .. "\n")
710                                         end
711                                 end
712                         end
713                         
714                         worldfile:close()
715                 else
716                         print("failed to open world config file")
717                 end
718                 
719                 modmgr.modlist = nil
720                 modmgr.worldconfig = nil
721         
722                 return {
723                         is_dialog = false,
724                         show_buttons = true,
725                         current_tab = engine.setting_get("main_menu_tab")
726                 }
727         end
728         
729         if fields["btn_config_world_cancel"] then
730         
731                 modmgr.worldconfig = nil
732                 
733                 return {
734                         is_dialog = false,
735                         show_buttons = true,
736                         current_tab = engine.setting_get("main_menu_tab")
737                 }
738         end
739         
740         if fields["btn_all_mods"] then
741                 local list = filterlist.get_raw_list(modmgr.modlist)
742                 
743                 for i=1,#list,1 do
744                         if list[i].typ ~= "game_mod" and
745                                 not list[i].is_modpack then
746                                 list[i].enabled = true
747                         end
748                 end
749         end
750         
751
752         
753         return nil
754 end
755 --------------------------------------------------------------------------------
756 function modmgr.handle_delete_mod_buttons(fields)
757         local mod = filterlist.get_list(modmgr.global_mods)[modmgr.selected_mod]
758         
759         if fields["dlg_delete_mod_confirm"] ~= nil then
760                 
761                 if mod.path ~= nil and
762                         mod.path ~= "" and
763                         mod.path ~= engine.get_modpath() then
764                         if not engine.delete_dir(mod.path) then
765                                 gamedata.errormessage ="Modmgr: failed to delete >" .. mod.path .. "<"
766                         end
767                         modmgr.refresh_globals()
768                 else
769                         gamedata.errormessage ="Modmgr: invalid modpath >" .. mod.path .. "<"
770                 end
771         end
772         
773         return {
774                 is_dialog = false,
775                 show_buttons = true,
776                 current_tab = engine.setting_get("main_menu_tab")
777                 }
778 end
779
780 --------------------------------------------------------------------------------
781 function modmgr.dialog_delete_mod()
782
783         local mod = filterlist.get_list(modmgr.global_mods)[modmgr.selected_mod]
784         
785         local retval = 
786                 "field[1.75,1;10,3;;Are you sure you want to delete ".. mod.name .. "?;]"..
787                 "button[4,4.2;1,0.5;dlg_delete_mod_confirm;Yes]" ..
788                 "button[6.5,4.2;3,0.5;dlg_delete_mod_cancel;No of course not!]"
789
790         return retval
791 end
792
793 --------------------------------------------------------------------------------
794 function modmgr.preparemodlist(data)
795         local retval = {}
796         
797         local global_mods = {}
798         local game_mods = {}
799         
800         --read global mods
801         local modpath = engine.get_modpath()
802
803         if modpath ~= nil and
804                 modpath ~= "" then
805                 get_mods(modpath,global_mods)
806         end
807         
808         for i=1,#global_mods,1 do
809                 global_mods[i].typ = "global_mod"
810                 table.insert(retval,global_mods[i])
811         end
812         
813         --read game mods
814         if data.gameid ~= nil and
815                 data.gameid ~= "" then
816                 local gamemodpath = engine.get_gamepath() .. DIR_DELIM .. data.gameid .. DIR_DELIM .. "mods"
817                 
818                 get_mods(gamemodpath,game_mods)
819         end
820         
821         for i=1,#game_mods,1 do
822                 game_mods[i].typ = "game_mod"
823                 table.insert(retval,game_mods[i])
824         end
825         
826         if data.worldpath == nil then
827                 return retval
828         end
829         
830         --read world mod configuration
831         local filename = data.worldpath ..
832                                 DIR_DELIM .. "world.mt"
833
834         local worldfile = io.open(filename,"r")
835         if worldfile then
836                 local dependency = worldfile:read("*l")
837                 while dependency do
838                         local parts = dependency:split("=")
839
840                         local key = parts[1]:trim()
841
842                         if key ~= "gameid" then
843                                 local key = parts[1]:trim():sub(10)
844                                 local element = nil
845                                 for i=1,#retval,1 do
846                                         if retval[i].name == key then
847                                                 element = retval[i]
848                                                 break
849                                         end
850                                 end
851                                 if element ~= nil then
852                                         if parts[2]:trim() == "true" then
853                                                 element.enabled = true
854                                         else
855                                                 element.enabled = false
856                                         end
857                                 else
858                                         print("Mod: " .. key .. " " .. dump(parts[2]) .. " but not found")
859                                 end
860                         end
861                         dependency = worldfile:read("*l")
862                 end
863                 worldfile:close()
864
865         end
866
867         return retval
868 end
869
870 --------------------------------------------------------------------------------
871 function modmgr.init_worldconfig()
872         modmgr.precheck()
873         local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
874         
875         if worldspec ~= nil then
876                 --read worldconfig
877                 modmgr.worldconfig = modmgr.get_worldconfig(worldspec.path)
878                 
879                 if modmgr.worldconfig.id == nil or
880                         modmgr.worldconfig.id == "" then
881                         modmgr.worldconfig = nil
882                         return false
883                 end
884                 
885                 modmgr.modlist = filterlist.create(
886                                                 modmgr.preparemodlist, --refresh
887                                                 modmgr.comparemod, --compare
888                                                 function(element,uid) --uid match
889                                                         if element.name == uid then
890                                                                 return true
891                                                         end
892                                                 end, 
893                                                 function(element,criteria)
894                                                         if criteria.hide_game and
895                                                                 element.typ == "game_mod" then
896                                                                         return false
897                                                         end
898                                                         
899                                                         if criteria.hide_modpackcontents and
900                                                                 element.modpack ~= nil then
901                                                                         return false
902                                                                 end
903                                                         return true
904                                                 end, --filter
905                                                 { worldpath= worldspec.path,
906                                                   gameid = worldspec.gameid }
907                                         )
908                                         
909                 filterlist.set_filtercriteria(modmgr.modlist, {
910                                                                         hide_game=modmgr.hide_gamemods,
911                                                                         hide_modpackcontents= modmgr.hide_modpackcontents
912                                                                         })
913                 
914                 return true     
915         end
916
917         return false
918 end
919
920 --------------------------------------------------------------------------------
921 function modmgr.comparemod(elem1,elem2)
922         if elem1 == nil or elem2 == nil then
923                 return false
924         end
925         if elem1.name ~= elem2.name then
926                 return false
927         end
928         if elem1.is_modpack ~= elem2.is_modpack then
929                 return false
930         end
931         if elem1.typ ~= elem2.typ then
932                 return false
933         end
934         if elem1.modpack ~= elem2.modpack then
935                 return false
936         end
937         
938         if elem1.path ~= elem2.path then
939                 return false
940         end
941         
942         return true
943 end
944
945 --------------------------------------------------------------------------------
946 function modmgr.gettab(name)
947         local retval = ""
948         
949         if name == "mod_mgr" then
950                 retval = retval .. modmgr.tab()
951         end
952         
953         if name == "dialog_rename_modpack" then
954                 retval = retval .. modmgr.dialog_rename_modpack()
955         end
956         
957         if name == "dialog_delete_mod" then
958                 retval = retval .. modmgr.dialog_delete_mod()
959         end
960         
961         if name == "dialog_configure_world" then
962                 retval = retval .. modmgr.dialog_configure_world()
963         end
964         
965         return retval
966 end
967
968 --------------------------------------------------------------------------------
969 function modmgr.mod_exists(basename)
970
971         if modmgr.global_mods == nil then
972                 modmgr.refresh_globals()
973         end
974
975         if filterlist.raw_index_by_uid(modmgr.global_mods,basename) > 0 then
976                 return true
977         end
978         
979         return false
980 end
981
982 --------------------------------------------------------------------------------
983 function modmgr.get_global_mod(idx)
984
985         if modmgr.global_mods == nil then
986                 return nil
987         end
988         
989         if idx < 1 or idx > filterlist.size(modmgr.global_mods) then
990                 return nil
991         end
992
993         return filterlist.get_list(modmgr.global_mods)[idx]
994 end
995
996 --------------------------------------------------------------------------------
997 function modmgr.refresh_globals()
998         modmgr.global_mods = filterlist.create(
999                                         modmgr.preparemodlist, --refresh
1000                                         modmgr.comparemod, --compare
1001                                         function(element,uid) --uid match
1002                                                 if element.name == uid then
1003                                                         return true
1004                                                 end
1005                                         end, 
1006                                         nil, --filter
1007                                         {}
1008                                         )
1009 end
1010
1011 --------------------------------------------------------------------------------
1012 function modmgr.identify_filetype(name)
1013
1014         if name:sub(-3):lower() == "zip" then
1015                 return {
1016                                 name = name,
1017                                 type = "zip"
1018                                 }
1019         end
1020         
1021         if name:sub(-6):lower() == "tar.gz" or
1022                 name:sub(-3):lower() == "tgz"then
1023                 return {
1024                                 name = name,
1025                                 type = "tgz"
1026                                 }
1027         end
1028         
1029         if name:sub(-6):lower() == "tar.bz2" then
1030                 return {
1031                                 name = name,
1032                                 type = "tbz"
1033                                 }
1034         end
1035         
1036         if name:sub(-2):lower() == "7z" then
1037                 return {
1038                                 name = name,
1039                                 type = "7z"
1040                                 }
1041         end
1042
1043         return {
1044                 name = name,
1045                 type = "ukn"
1046         }
1047 end