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