]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/modmgr.lua
Replace print()s with minetest.log() in builtin
[dragonfireclient.git] / builtin / modmgr.lua
1 --Minetest
2 --Copyright (C) 2013 sapier
3 --
4 --This program is free software; you can redistribute it and/or modify
5 --it under the terms of the GNU Lesser General Public License as published by
6 --the Free Software Foundation; either version 2.1 of the License, or
7 --(at your option) any later version.
8 --
9 --This program is distributed in the hope that it will be useful,
10 --but WITHOUT ANY WARRANTY; without even the implied warranty of
11 --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 --GNU Lesser General Public License for more details.
13 --
14 --You should have received a copy of the GNU Lesser General Public License along
15 --with this program; if not, write to the Free Software Foundation, Inc.,
16 --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 --------------------------------------------------------------------------------
19 function get_mods(path,retval,modpack)
20
21         local mods = engine.get_dirlist(path,true)
22         for i=1,#mods,1 do
23                 local toadd = {}
24                 local modpackfile = nil
25                 
26                 toadd.name              = mods[i]
27                 toadd.path              = path .. DIR_DELIM .. mods[i] .. DIR_DELIM
28                 if modpack ~= nil and
29                         modpack ~= "" then
30                         toadd.modpack   = modpack
31                 else
32                         local filename = path .. DIR_DELIM .. mods[i] .. DIR_DELIM .. "modpack.txt"
33                         local error = nil
34                         modpackfile,error = io.open(filename,"r")
35                 end
36                         
37                 if modpackfile ~= nil then
38                         modpackfile:close()
39                         toadd.is_modpack = true
40                         table.insert(retval,toadd)
41                         get_mods(path .. DIR_DELIM .. mods[i],retval,mods[i])
42                 else
43                         table.insert(retval,toadd)
44                 end
45         end
46 end
47
48 --modmanager implementation
49 modmgr = {}
50
51 --------------------------------------------------------------------------------
52 function modmgr.extract(modfile)
53         if modfile.type == "zip" then
54                 local tempfolder = os.tempfolder()
55                 
56                 if tempfolder ~= nil and
57                         tempfodler ~= "" then
58                         engine.create_dir(tempfolder)
59                         engine.extract_zip(modfile.name,tempfolder)
60                         return tempfolder
61                 end
62         end
63 end
64
65 -------------------------------------------------------------------------------
66 function modmgr.getbasefolder(temppath)
67
68         if temppath == nil then
69                 return {
70                 type = "invalid",
71                 path = ""
72                 }
73         end
74
75         local testfile = io.open(temppath .. DIR_DELIM .. "init.lua","r")
76         if testfile ~= nil then
77                 testfile:close()
78                 return {
79                                 type="mod",
80                                 path=temppath
81                                 }
82         end
83         
84         testfile = io.open(temppath .. DIR_DELIM .. "modpack.txt","r")
85         if testfile ~= nil then
86                 testfile:close()
87                 return {
88                                 type="modpack",
89                                 path=temppath
90                                 }
91         end
92         
93         local subdirs = engine.get_dirlist(temppath,true)
94         
95         --only single mod or modpack allowed
96         if #subdirs ~= 1 then
97                 return {
98                         type = "invalid",
99                         path = ""
100                         }
101         end
102
103         testfile = 
104         io.open(temppath .. DIR_DELIM .. subdirs[1] ..DIR_DELIM .."init.lua","r")
105         if testfile ~= nil then
106                 testfile:close()
107                 return {
108                         type="mod",
109                         path= temppath .. DIR_DELIM .. subdirs[1]
110                         }
111         end
112         
113         testfile = 
114         io.open(temppath .. DIR_DELIM .. subdirs[1] ..DIR_DELIM .."modpack.txt","r")
115         if testfile ~= nil then
116                 testfile:close()
117                 return {
118                         type="modpack",
119                         path=temppath ..  DIR_DELIM .. subdirs[1]
120                         }
121         end
122
123         return {
124                 type = "invalid",
125                 path = ""
126                 }
127 end
128
129 --------------------------------------------------------------------------------
130 function modmgr.isValidModname(modpath)
131         if modpath:find("-") ~= nil then
132                 return false
133         end
134         
135         return true
136 end
137
138 --------------------------------------------------------------------------------
139 function modmgr.parse_register_line(line)
140         local pos1 = line:find("\"")
141         local pos2 = nil
142         if pos1 ~= nil then
143                 pos2 = line:find("\"",pos1+1)
144         end
145         
146         if pos1 ~= nil and pos2 ~= nil then
147                 local item = line:sub(pos1+1,pos2-1)
148                 
149                 if item ~= nil and
150                         item ~= "" then
151                         local pos3 = item:find(":")
152                         
153                         if pos3 ~= nil then
154                                 local retval = item:sub(1,pos3-1)
155                                 if retval ~= nil and
156                                         retval ~= "" then
157                                         return retval
158                                 end 
159                         end
160                 end
161         end
162         return nil
163 end
164
165 --------------------------------------------------------------------------------
166 function modmgr.parse_dofile_line(modpath,line)
167         local pos1 = line:find("\"")
168         local pos2 = nil
169         if pos1 ~= nil then
170                 pos2 = line:find("\"",pos1+1)
171         end
172         
173         if pos1 ~= nil and pos2 ~= nil then
174                 local filename = line:sub(pos1+1,pos2-1)
175                 
176                 if filename ~= nil and
177                         filename ~= "" and
178                         filename:find(".lua") then
179                         return modmgr.identify_modname(modpath,filename)
180                 end
181         end
182         return nil
183 end
184
185 --------------------------------------------------------------------------------
186 function modmgr.identify_modname(modpath,filename)
187         local testfile = io.open(modpath .. DIR_DELIM .. filename,"r")
188         if testfile ~= nil then
189                 local line = testfile:read()
190                 
191                 while line~= nil do
192                         local modname = nil
193                 
194                         if line:find("minetest.register_tool") then
195                                 modname = modmgr.parse_register_line(line)
196                         end
197                         
198                         if line:find("minetest.register_craftitem") then
199                                 modname = modmgr.parse_register_line(line)
200                         end
201                         
202                         
203                         if line:find("minetest.register_node") then
204                                 modname = modmgr.parse_register_line(line)
205                         end
206                         
207                         if line:find("dofile") then
208                                 modname = modmgr.parse_dofile_line(modpath,line)
209                         end
210                 
211                         if modname ~= nil then
212                                 testfile:close()
213                                 return modname
214                         end
215                         
216                         line = testfile:read()
217                 end
218                 testfile:close()
219         end
220         
221         return nil
222 end
223
224 --------------------------------------------------------------------------------
225 function modmgr.tab()
226
227         if modmgr.global_mods == nil then
228                 modmgr.refresh_globals()
229         end
230
231         if modmgr.selected_mod == nil then
232                 modmgr.selected_mod = 1
233         end
234         
235         local retval = 
236                 "vertlabel[0,-0.25;".. fgettext("MODS") .. "]" ..
237                 "label[0.8,-0.25;".. fgettext("Installed Mods:") .. "]" ..
238                 "textlist[0.75,0.25;4.5,4;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                 "button[0.75,4.85;1.8,0.5;btn_mod_mgr_install_local;".. fgettext("Local install") .. "]" ..
245                 "button[2.45,4.85;3.05,0.5;btn_mod_mgr_download;".. fgettext("Online mod repository") .. "]"
246                 
247         local selected_mod = nil
248                 
249         if filterlist.size(modmgr.global_mods) >= modmgr.selected_mod then
250                 selected_mod = filterlist.get_list(modmgr.global_mods)[modmgr.selected_mod]
251         end
252         
253         if selected_mod ~= nil then
254                 local modscreenshot = nil
255                 
256                 --check for screenshot beeing available
257                 local screenshotfilename = selected_mod.path .. DIR_DELIM .. "screenshot.png"
258                 local error = nil
259                 screenshotfile,error = io.open(screenshotfilename,"r")
260                 if error == nil then
261                         screenshotfile:close()
262                         modscreenshot = screenshotfilename
263                 end
264         
265                 if modscreenshot == nil then
266                                 modscreenshot = modstore.basetexturedir .. "no_screenshot.png"
267                 end
268                 
269                 retval = retval 
270                                 .. "image[5.5,0;3,2;" .. modscreenshot .. "]"
271                                 .. "label[8.25,0.6;" .. selected_mod.name .. "]"
272                                 
273                 local descriptionlines = nil
274                 error = nil
275                 local descriptionfilename = selected_mod.path .. "description.txt"
276                 descriptionfile,error = io.open(descriptionfilename,"r")
277                 if error == nil then
278                         descriptiontext = descriptionfile:read("*all")
279                         
280                         descriptionlines = engine.splittext(descriptiontext,42)
281                         descriptionfile:close()
282                 else
283                         descriptionlines = {}
284                         table.insert(descriptionlines,fgettext("No mod description available"))
285                 end
286         
287                 retval = retval .. 
288                         "label[5.5,1.7;".. fgettext("Mod information:") .. "]" ..
289                         "textlist[5.5,2.2;6.2,2.4;description;"
290                         
291                 for i=1,#descriptionlines,1 do
292                         retval = retval .. engine.formspec_escape(descriptionlines[i]) .. ","
293                 end
294                 
295                 
296                 if selected_mod.is_modpack then
297                         retval = retval .. ";0]" .. 
298                                 "button[10,4.85;2,0.5;btn_mod_mgr_rename_modpack;" ..
299                                 fgettext("Rename") .. "]"
300                         retval = retval .. "button[5.5,4.85;4.5,0.5;btn_mod_mgr_delete_mod;"
301                                 .. fgettext("Uninstall selected modpack") .. "]"
302                 else
303                         --show dependencies
304
305                         retval = retval .. ",Depends:,"
306                                 
307                         toadd = modmgr.get_dependencies(selected_mod.path)
308                         
309                         retval = retval .. toadd .. ";0]"
310                         
311                         retval = retval .. "button[5.5,4.85;4.5,0.5;btn_mod_mgr_delete_mod;"
312                                 .. fgettext("Uninstall selected mod") .. "]"
313                 end
314         end
315         return retval
316 end
317
318 --------------------------------------------------------------------------------
319 function modmgr.dialog_rename_modpack()
320
321         local mod = filterlist.get_list(modmgr.modlist)[modmgr.selected_mod]
322         
323         local retval = 
324                 "label[1.75,1;".. fgettext("Rename Modpack:") .. "]"..
325                 "field[4.5,1.4;6,0.5;te_modpack_name;;" ..
326                 mod.name ..
327                 "]" ..
328                 "button[5,4.2;2.6,0.5;dlg_rename_modpack_confirm;".. 
329                                 fgettext("Accept") .. "]" ..
330                 "button[7.5,4.2;2.8,0.5;dlg_rename_modpack_cancel;".. 
331                                 fgettext("Cancel") .. "]"
332
333         return retval
334 end
335
336 --------------------------------------------------------------------------------
337 function modmgr.precheck()
338
339         if modmgr.world_config_selected_world == nil then
340                 modmgr.world_config_selected_world = 1
341         end
342         
343         if modmgr.world_config_selected_mod == nil then
344                 modmgr.world_config_selected_mod = 1
345         end
346         
347         if modmgr.hide_gamemods == nil then
348                 modmgr.hide_gamemods = true
349         end
350         
351         if modmgr.hide_modpackcontents == nil then
352                 modmgr.hide_modpackcontents = true
353         end
354 end
355
356 --------------------------------------------------------------------------------
357 function modmgr.render_modlist(render_list)
358         local retval = ""
359         
360         if render_list == nil then
361                 if modmgr.global_mods == nil then
362                         modmgr.refresh_globals()
363                 end
364                 render_list = modmgr.global_mods
365         end
366         
367         local list = filterlist.get_list(render_list)
368         local last_modpack = nil
369         
370         for i,v in ipairs(list) do
371                 if retval ~= "" then
372                         retval = retval ..","
373                 end
374
375                 local color = ""
376                 
377                 if v.is_modpack then
378                         local rawlist = filterlist.get_raw_list(render_list)
379                         
380                         local all_enabled = true
381                         for j=1,#rawlist,1 do
382                                 if rawlist[j].modpack == list[i].name and
383                                         rawlist[j].enabled ~= true then
384                                                 all_enabled = false
385                                                 break
386                                 end
387                         end
388                         
389                         if all_enabled == false then
390                                 color = mt_color_grey
391                         else
392                                 color = mt_color_dark_green
393                         end
394                 end
395                 
396                 if v.typ == "game_mod" then
397                         color = mt_color_blue
398                 else
399                         if v.enabled then
400                                 color = mt_color_green
401                         end
402                 end
403
404                 retval = retval .. color
405                 if v.modpack  ~= nil then
406                         retval = retval .. "    "
407                 end
408                 retval = retval .. v.name
409         end
410         
411         return retval
412 end
413
414 --------------------------------------------------------------------------------
415 function modmgr.dialog_configure_world()
416         modmgr.precheck()
417         
418         local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
419         local mod = filterlist.get_list(modmgr.modlist)[modmgr.world_config_selected_mod]
420         
421         local retval =
422                 "size[11,6.5]" ..
423                 "label[0.5,-0.25;" .. fgettext("World:") .. "]" ..
424                 "label[1.75,-0.25;" .. worldspec.name .. "]"
425                 
426         if modmgr.hide_gamemods then
427                 retval = retval .. "checkbox[0,5.75;cb_hide_gamemods;" .. fgettext("Hide Game") .. ";true]"
428         else
429                 retval = retval .. "checkbox[0,5.75;cb_hide_gamemods;" .. fgettext("Hide Game") .. ";false]"
430         end
431         
432         if modmgr.hide_modpackcontents then
433                 retval = retval .. "checkbox[2,5.75;cb_hide_mpcontent;" .. fgettext("Hide mp content") .. ";true]"
434         else
435                 retval = retval .. "checkbox[2,5.75;cb_hide_mpcontent;" .. fgettext("Hide mp content") .. ";false]"
436         end
437         
438         if mod == nil then
439                 mod = {name=""}
440         end
441         retval = retval ..
442                 "label[0,0.45;" .. fgettext("Mod:") .. "]" ..
443                 "label[0.75,0.45;" .. mod.name .. "]" ..
444                 "label[0,1;" .. fgettext("Depends:") .. "]" ..
445                 "textlist[0,1.5;5,4.25;world_config_depends;" ..
446                 modmgr.get_dependencies(mod.path) .. ";0]" ..
447                 "button[9.25,6.35;2,0.5;btn_config_world_save;" .. fgettext("Save") .. "]" ..
448                 "button[7.4,6.35;2,0.5;btn_config_world_cancel;" .. fgettext("Cancel") .. "]"
449         
450         if mod ~= nil and mod.name ~= "" and mod.typ ~= "game_mod" then
451                 if mod.is_modpack then
452                         local rawlist = filterlist.get_raw_list(modmgr.modlist)
453                         
454                         local all_enabled = true
455                         for j=1,#rawlist,1 do
456                                 if rawlist[j].modpack == mod.name and
457                                         rawlist[j].enabled ~= true then
458                                                 all_enabled = false
459                                                 break
460                                 end
461                         end
462                         
463                         if all_enabled == false then
464                                 retval = retval .. "button[5.5,-0.125;2,0.5;btn_mp_enable;" .. fgettext("Enable MP") .. "]"
465                         else
466                                 retval = retval .. "button[5.5,-0.125;2,0.5;btn_mp_disable;" .. fgettext("Disable MP") .. "]"
467                         end
468                 else
469                         if mod.enabled then
470                                 retval = retval .. "checkbox[5.5,-0.375;cb_mod_enable;" .. fgettext("enabled") .. ";true]"
471                         else
472                                 retval = retval .. "checkbox[5.5,-0.375;cb_mod_enable;" .. fgettext("enabled") .. ";false]"
473                         end
474                 end
475         end
476         
477         retval = retval ..
478                 "button[8.5,-0.125;2.5,0.5;btn_all_mods;" .. fgettext("Enable all") .. "]" ..
479                 "textlist[5.5,0.5;5.5,5.75;world_config_modlist;"
480         
481         retval = retval .. modmgr.render_modlist(modmgr.modlist)
482         
483         retval = retval .. ";" .. modmgr.world_config_selected_mod .."]"
484         
485         return retval
486 end
487
488 --------------------------------------------------------------------------------
489 function modmgr.handle_buttons(tab,fields)
490
491         local retval = nil
492         
493         if tab == "mod_mgr" then
494                 retval = modmgr.handle_modmgr_buttons(fields)
495         end
496         
497         if tab == "dialog_rename_modpack" then
498                 retval = modmgr.handle_rename_modpack_buttons(fields)
499         end
500         
501         if tab == "dialog_delete_mod" then
502                 retval = modmgr.handle_delete_mod_buttons(fields)
503         end
504         
505         if tab == "dialog_configure_world" then
506                 retval = modmgr.handle_configure_world_buttons(fields)
507         end
508         
509         return retval
510 end
511
512 --------------------------------------------------------------------------------
513 function modmgr.get_dependencies(modfolder)
514         local toadd = ""
515         if modfolder ~= nil then
516                 local filename = modfolder ..
517                                         DIR_DELIM .. "depends.txt"
518         
519                 local dependencyfile = io.open(filename,"r")
520                 
521                 if dependencyfile then
522                         local dependency = dependencyfile:read("*l")
523                         while dependency do
524                                 if toadd ~= "" then     
525                                         toadd = toadd .. ","
526                                 end
527                                 toadd = toadd .. dependency
528                                 dependency = dependencyfile:read()
529                         end
530                         dependencyfile:close()
531                 end
532         end
533
534         return toadd
535 end
536
537
538 --------------------------------------------------------------------------------
539 function modmgr.get_worldconfig(worldpath)
540         local filename = worldpath ..
541                                 DIR_DELIM .. "world.mt"
542
543         local worldfile = Settings(filename)
544         
545         local worldconfig = {}
546         worldconfig.global_mods = {}
547         worldconfig.game_mods = {}
548         
549         for key,value in pairs(worldfile:to_table()) do
550                 if key == "gameid" then
551                         worldconfig.id = value
552                 else
553                         worldconfig.global_mods[key] = engine.is_yes(value)
554                 end
555         end
556         
557         --read gamemods
558         local gamespec = gamemgr.find_by_gameid(worldconfig.id)
559         gamemgr.get_game_mods(gamespec, worldconfig.game_mods)
560
561         return worldconfig
562 end
563 --------------------------------------------------------------------------------
564 function modmgr.handle_modmgr_buttons(fields)
565         local retval = {
566                         tab = nil,
567                         is_dialog = nil,
568                         show_buttons = nil,
569                 }
570
571         if fields["modlist"] ~= nil then
572                 local event = explode_textlist_event(fields["modlist"])
573                 modmgr.selected_mod = event.index
574         end
575         
576         if fields["btn_mod_mgr_install_local"] ~= nil then
577                 engine.show_file_open_dialog("mod_mgt_open_dlg",fgettext("Select Mod File:"))
578         end
579         
580         if fields["btn_mod_mgr_download"] ~= nil then
581                 modstore.update_modlist()
582                 retval.current_tab = "dialog_modstore_unsorted"
583                 retval.is_dialog = true
584                 retval.show_buttons = false
585                 return retval
586         end
587         
588         if fields["btn_mod_mgr_rename_modpack"] ~= nil then
589                 retval.current_tab = "dialog_rename_modpack"
590                 retval.is_dialog = true
591                 retval.show_buttons = false
592                 return retval
593         end
594         
595         if fields["btn_mod_mgr_delete_mod"] ~= nil then
596                 retval.current_tab = "dialog_delete_mod"
597                 retval.is_dialog = true
598                 retval.show_buttons = false
599                 return retval
600         end
601         
602         if fields["mod_mgt_open_dlg_accepted"] ~= nil and
603                 fields["mod_mgt_open_dlg_accepted"] ~= "" then
604                 modmgr.installmod(fields["mod_mgt_open_dlg_accepted"],nil)
605         end
606         
607         return nil;
608 end
609
610 --------------------------------------------------------------------------------
611 function modmgr.installmod(modfilename,basename)
612         local modfile = modmgr.identify_filetype(modfilename)
613         local modpath = modmgr.extract(modfile)
614         
615         if modpath == nil then
616                 gamedata.errormessage = fgettext("Install Mod: file: \"$1\"", modfile.name) ..
617                         fgettext("\nInstall Mod: unsupported filetype \"$1\"", modfile.type)
618                 return
619         end
620         
621         
622         local basefolder = modmgr.getbasefolder(modpath)
623         
624         if basefolder.type == "modpack" then
625                 local clean_path = nil
626                 
627                 if basename ~= nil then
628                         clean_path = "mp_" .. basename
629                 end
630                 
631                 if clean_path == nil then
632                         clean_path = get_last_folder(cleanup_path(basefolder.path))
633                 end
634                 
635                 if clean_path ~= nil then
636                         local targetpath = engine.get_modpath() .. DIR_DELIM .. clean_path
637                         if not engine.copy_dir(basefolder.path,targetpath) then
638                                 gamedata.errormessage = fgettext("Failed to install $1 to $2", basename, targetpath)
639                         end
640                 else
641                         gamedata.errormessage = fgettext("Install Mod: unable to find suitable foldername for modpack $1", modfilename)
642                 end
643         end
644         
645         if basefolder.type == "mod" then
646                 local targetfolder = basename
647                 
648                 if targetfolder == nil then
649                         targetfolder = modmgr.identify_modname(basefolder.path,"init.lua")
650                 end
651                 
652                 --if heuristic failed try to use current foldername
653                 if targetfolder == nil then
654                         targetfolder = get_last_folder(basefolder.path)
655                 end     
656                 
657                 if targetfolder ~= nil and modmgr.isValidModname(targetfolder) then
658                         local targetpath = engine.get_modpath() .. DIR_DELIM .. targetfolder
659                         engine.copy_dir(basefolder.path,targetpath)
660                 else
661                         gamedata.errormessage = fgettext("Install Mod: unable to find real modname for: $1", modfilename)
662                 end
663         end
664         
665         engine.delete_dir(modpath)
666
667         modmgr.refresh_globals()
668
669 end
670
671 --------------------------------------------------------------------------------
672 function modmgr.handle_rename_modpack_buttons(fields)
673         
674         if fields["dlg_rename_modpack_confirm"] ~= nil then
675                 local mod = filterlist.get_list(modmgr.modlist)[modmgr.selected_mod]
676                 local oldpath = engine.get_modpath() .. DIR_DELIM .. mod.name
677                 local targetpath = engine.get_modpath() .. DIR_DELIM .. fields["te_modpack_name"]
678                 engine.copy_dir(oldpath,targetpath,false)
679         end
680         
681         return {
682                 is_dialog = false,
683                 show_buttons = true,
684                 current_tab = engine.setting_get("main_menu_tab")
685                 }
686 end
687 --------------------------------------------------------------------------------
688 function modmgr.handle_configure_world_buttons(fields)
689         if fields["world_config_modlist"] ~= nil then
690                 local event = explode_textlist_event(fields["world_config_modlist"])
691                 modmgr.world_config_selected_mod = event.index
692
693                 if event.typ == "DCL" then
694                         modmgr.world_config_enable_mod(nil)
695                 end
696         end
697         
698         if fields["key_enter"] ~= nil then
699                 modmgr.world_config_enable_mod(nil)
700         end
701         
702         if fields["cb_mod_enable"] ~= nil then
703                 local toset = engine.is_yes(fields["cb_mod_enable"])
704                 modmgr.world_config_enable_mod(toset)
705         end
706         
707         if fields["btn_mp_enable"] ~= nil or
708                 fields["btn_mp_disable"] then
709                 local toset = (fields["btn_mp_enable"] ~= nil)
710                 modmgr.world_config_enable_mod(toset)
711         end
712         
713         if fields["cb_hide_gamemods"] ~= nil then
714                 local current = filterlist.get_filtercriteria(modmgr.modlist)
715                 
716                 if current == nil then
717                         current = {}
718                 end
719
720                 if engine.is_yes(fields["cb_hide_gamemods"]) then
721                         current.hide_game = true
722                         modmgr.hide_gamemods = true
723                 else
724                         current.hide_game = false
725                         modmgr.hide_gamemods = false
726                 end
727                 
728                 filterlist.set_filtercriteria(modmgr.modlist,current)
729         end
730         
731                 if fields["cb_hide_mpcontent"] ~= nil then
732                 local current = filterlist.get_filtercriteria(modmgr.modlist)
733                 
734                 if current == nil then
735                         current = {}
736                 end
737
738                 if engine.is_yes(fields["cb_hide_mpcontent"]) then
739                         current.hide_modpackcontents = true
740                         modmgr.hide_modpackcontents = true
741                 else
742                         current.hide_modpackcontents = false
743                         modmgr.hide_modpackcontents = false
744                 end
745                 
746                 filterlist.set_filtercriteria(modmgr.modlist,current)
747         end
748         
749         if fields["btn_config_world_save"] then
750                 local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
751                 
752                 local filename = worldspec.path ..
753                                 DIR_DELIM .. "world.mt"
754                 
755                 local worldfile = Settings(filename)
756                 local mods = worldfile:to_table()
757                 
758                 local rawlist = filterlist.get_raw_list(modmgr.modlist)
759                 
760                 local i,mod
761                 for i,mod in ipairs(rawlist) do
762                         if not mod.is_modpack and
763                                         mod.typ ~= "game_mod" then
764                                 if mod.enabled then
765                                         worldfile:set("load_mod_"..mod.name, "true")
766                                 else
767                                         worldfile:set("load_mod_"..mod.name, "false")
768                                 end
769                                 mods["load_mod_"..mod.name] = nil
770                         end
771                 end
772                 
773                 -- Remove mods that are not present anymore
774                 for key,value in pairs(mods) do
775                         if key:sub(1,9) == "load_mod_" then
776                                 worldfile:remove(key)
777                         end
778                 end
779                 
780                 if not worldfile:write() then
781                         minetest.log("error", "Failed to write world config file")
782                 end
783                 
784                 modmgr.modlist = nil
785                 modmgr.worldconfig = nil
786         
787                 return {
788                         is_dialog = false,
789                         show_buttons = true,
790                         current_tab = engine.setting_get("main_menu_tab")
791                 }
792         end
793         
794         if fields["btn_config_world_cancel"] then
795         
796                 modmgr.worldconfig = nil
797                 
798                 return {
799                         is_dialog = false,
800                         show_buttons = true,
801                         current_tab = engine.setting_get("main_menu_tab")
802                 }
803         end
804         
805         if fields["btn_all_mods"] then
806                 local list = filterlist.get_raw_list(modmgr.modlist)
807                 
808                 for i=1,#list,1 do
809                         if list[i].typ ~= "game_mod" and
810                                 not list[i].is_modpack then
811                                 list[i].enabled = true
812                         end
813                 end
814         end
815         
816
817         
818         return nil
819 end
820 --------------------------------------------------------------------------------
821 function modmgr.world_config_enable_mod(toset)
822         local mod = filterlist.get_list(modmgr.modlist)
823                 [engine.get_textlist_index("world_config_modlist")]
824
825         if mod.typ == "game_mod" then
826                 -- game mods can't be enabled or disabled
827         elseif not mod.is_modpack then
828                 if toset == nil then
829                         mod.enabled = not mod.enabled
830                 else
831                         mod.enabled = toset
832                 end
833         else
834                 local list = filterlist.get_raw_list(modmgr.modlist)
835                 for i=1,#list,1 do
836                         if list[i].modpack == mod.name then
837                                 if toset == nil then
838                                         toset = not list[i].enabled
839                                 end
840                                 list[i].enabled = toset
841                         end
842                 end
843         end
844 end
845 --------------------------------------------------------------------------------
846 function modmgr.handle_delete_mod_buttons(fields)
847         local mod = filterlist.get_list(modmgr.global_mods)[modmgr.selected_mod]
848         
849         if fields["dlg_delete_mod_confirm"] ~= nil then
850                 
851                 if mod.path ~= nil and
852                         mod.path ~= "" and
853                         mod.path ~= engine.get_modpath() then
854                         if not engine.delete_dir(mod.path) then
855                                 gamedata.errormessage = fgettext("Modmgr: failed to delete \"$1\"", mod.path)
856                         end
857                         modmgr.refresh_globals()
858                 else
859                         gamedata.errormessage = fgettext("Modmgr: invalid modpath \"$1\"", mod.path)
860                 end
861         end
862         
863         return {
864                 is_dialog = false,
865                 show_buttons = true,
866                 current_tab = engine.setting_get("main_menu_tab")
867                 }
868 end
869
870 --------------------------------------------------------------------------------
871 function modmgr.dialog_delete_mod()
872
873         local mod = filterlist.get_list(modmgr.global_mods)[modmgr.selected_mod]
874         
875         local retval = 
876                 "field[1.75,1;10,3;;" .. fgettext("Are you sure you want to delete \"$1\"?", mod.name) ..  ";]"..
877                 "button[4,4.2;1,0.5;dlg_delete_mod_confirm;" .. fgettext("Yes") .. "]" ..
878                 "button[6.5,4.2;3,0.5;dlg_delete_mod_cancel;" .. fgettext("No of course not!") .. "]"
879
880         return retval
881 end
882
883 --------------------------------------------------------------------------------
884 function modmgr.preparemodlist(data)
885         local retval = {}
886         
887         local global_mods = {}
888         local game_mods = {}
889         
890         --read global mods
891         local modpath = engine.get_modpath()
892
893         if modpath ~= nil and
894                 modpath ~= "" then
895                 get_mods(modpath,global_mods)
896         end
897         
898         for i=1,#global_mods,1 do
899                 global_mods[i].typ = "global_mod"
900                 table.insert(retval,global_mods[i])
901         end
902         
903         --read game mods
904         local gamespec = gamemgr.find_by_gameid(data.gameid)
905         gamemgr.get_game_mods(gamespec, game_mods)
906         
907         for i=1,#game_mods,1 do
908                 game_mods[i].typ = "game_mod"
909                 table.insert(retval,game_mods[i])
910         end
911         
912         if data.worldpath == nil then
913                 return retval
914         end
915         
916         --read world mod configuration
917         local filename = data.worldpath ..
918                                 DIR_DELIM .. "world.mt"
919
920         local worldfile = Settings(filename)
921         
922         for key,value in pairs(worldfile:to_table()) do
923                 if key:sub(1, 9) == "load_mod_" then
924                         key = key:sub(10)
925                         local element = nil
926                         for i=1,#retval,1 do
927                                 if retval[i].name == key then
928                                         element = retval[i]
929                                         break
930                                 end
931                         end
932                         if element ~= nil then
933                                 element.enabled = engine.is_yes(value)
934                         else
935                                 minetest.log("info", "Mod: " .. key .. " " .. dump(value) .. " but not found")
936                         end
937                 end
938         end
939
940         return retval
941 end
942
943 --------------------------------------------------------------------------------
944 function modmgr.init_worldconfig()
945         modmgr.precheck()
946         local worldspec = engine.get_worlds()[modmgr.world_config_selected_world]
947         
948         if worldspec ~= nil then
949                 --read worldconfig
950                 modmgr.worldconfig = modmgr.get_worldconfig(worldspec.path)
951                 
952                 if modmgr.worldconfig.id == nil or
953                         modmgr.worldconfig.id == "" then
954                         modmgr.worldconfig = nil
955                         return false
956                 end
957                 
958                 modmgr.modlist = filterlist.create(
959                                                 modmgr.preparemodlist, --refresh
960                                                 modmgr.comparemod, --compare
961                                                 function(element,uid) --uid match
962                                                         if element.name == uid then
963                                                                 return true
964                                                         end
965                                                 end, 
966                                                 function(element,criteria)
967                                                         if criteria.hide_game and
968                                                                 element.typ == "game_mod" then
969                                                                         return false
970                                                         end
971                                                         
972                                                         if criteria.hide_modpackcontents and
973                                                                 element.modpack ~= nil then
974                                                                         return false
975                                                                 end
976                                                         return true
977                                                 end, --filter
978                                                 { worldpath= worldspec.path,
979                                                   gameid = worldspec.gameid }
980                                         )
981                                         
982                 filterlist.set_filtercriteria(modmgr.modlist, {
983                                                                         hide_game=modmgr.hide_gamemods,
984                                                                         hide_modpackcontents= modmgr.hide_modpackcontents
985                                                                         })
986                 filterlist.add_sort_mechanism(modmgr.modlist, "alphabetic", sort_mod_list)
987                 filterlist.set_sortmode(modmgr.modlist, "alphabetic")
988                 
989                 return true     
990         end
991
992         return false
993 end
994
995 --------------------------------------------------------------------------------
996 function modmgr.comparemod(elem1,elem2)
997         if elem1 == nil or elem2 == nil then
998                 return false
999         end
1000         if elem1.name ~= elem2.name then
1001                 return false
1002         end
1003         if elem1.is_modpack ~= elem2.is_modpack then
1004                 return false
1005         end
1006         if elem1.typ ~= elem2.typ then
1007                 return false
1008         end
1009         if elem1.modpack ~= elem2.modpack then
1010                 return false
1011         end
1012         
1013         if elem1.path ~= elem2.path then
1014                 return false
1015         end
1016         
1017         return true
1018 end
1019
1020 --------------------------------------------------------------------------------
1021 function modmgr.gettab(name)
1022         local retval = ""
1023         
1024         if name == "mod_mgr" then
1025                 retval = retval .. modmgr.tab()
1026         end
1027         
1028         if name == "dialog_rename_modpack" then
1029                 retval = retval .. modmgr.dialog_rename_modpack()
1030         end
1031         
1032         if name == "dialog_delete_mod" then
1033                 retval = retval .. modmgr.dialog_delete_mod()
1034         end
1035         
1036         if name == "dialog_configure_world" then
1037                 retval = retval .. modmgr.dialog_configure_world()
1038         end
1039         
1040         return retval
1041 end
1042
1043 --------------------------------------------------------------------------------
1044 function modmgr.mod_exists(basename)
1045
1046         if modmgr.global_mods == nil then
1047                 modmgr.refresh_globals()
1048         end
1049
1050         if filterlist.raw_index_by_uid(modmgr.global_mods,basename) > 0 then
1051                 return true
1052         end
1053         
1054         return false
1055 end
1056
1057 --------------------------------------------------------------------------------
1058 function modmgr.get_global_mod(idx)
1059
1060         if modmgr.global_mods == nil then
1061                 return nil
1062         end
1063         
1064         if idx < 1 or idx > filterlist.size(modmgr.global_mods) then
1065                 return nil
1066         end
1067
1068         return filterlist.get_list(modmgr.global_mods)[idx]
1069 end
1070
1071 --------------------------------------------------------------------------------
1072 function modmgr.refresh_globals()
1073         modmgr.global_mods = filterlist.create(
1074                                         modmgr.preparemodlist, --refresh
1075                                         modmgr.comparemod, --compare
1076                                         function(element,uid) --uid match
1077                                                 if element.name == uid then
1078                                                         return true
1079                                                 end
1080                                         end, 
1081                                         nil, --filter
1082                                         {}
1083                                         )
1084         filterlist.add_sort_mechanism(modmgr.global_mods, "alphabetic", sort_mod_list)
1085         filterlist.set_sortmode(modmgr.global_mods, "alphabetic")
1086 end
1087
1088 --------------------------------------------------------------------------------
1089 function modmgr.identify_filetype(name)
1090
1091         if name:sub(-3):lower() == "zip" then
1092                 return {
1093                                 name = name,
1094                                 type = "zip"
1095                                 }
1096         end
1097         
1098         if name:sub(-6):lower() == "tar.gz" or
1099                 name:sub(-3):lower() == "tgz"then
1100                 return {
1101                                 name = name,
1102                                 type = "tgz"
1103                                 }
1104         end
1105         
1106         if name:sub(-6):lower() == "tar.bz2" then
1107                 return {
1108                                 name = name,
1109                                 type = "tbz"
1110                                 }
1111         end
1112         
1113         if name:sub(-2):lower() == "7z" then
1114                 return {
1115                                 name = name,
1116                                 type = "7z"
1117                                 }
1118         end
1119
1120         return {
1121                 name = name,
1122                 type = "ukn"
1123         }
1124 end