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