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