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