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