]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/mainmenu/pkgmgr.lua
171ba54ea251f8a7834f77081c92b7813816c952
[dragonfireclient.git] / builtin / mainmenu / pkgmgr.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         local mods = core.get_dir_list(path, true)
21
22         for _, name in ipairs(mods) do
23                 if name:sub(1, 1) ~= "." then
24                         local prefix = path .. DIR_DELIM .. name
25                         local toadd = {}
26                         retval[#retval + 1] = toadd
27
28                         -- Get config file
29                         local mod_conf
30                         local modpack_conf = io.open(prefix .. DIR_DELIM .. "modpack.conf")
31                         if modpack_conf then
32                                 toadd.is_modpack = true
33                                 modpack_conf:close()
34
35                                 mod_conf = Settings(prefix .. DIR_DELIM .. "modpack.conf"):to_table()
36                                 if mod_conf.name then
37                                         name = mod_conf.name
38                                 end
39                         else
40                                 mod_conf = Settings(prefix .. DIR_DELIM .. "mod.conf"):to_table()
41                                 if mod_conf.name then
42                                         name = mod_conf.name
43                                 end
44                         end
45
46                         -- Read from config
47                         toadd.name = name
48                         toadd.author = mod_conf.author
49                         toadd.release = tonumber(mod_conf.release or "0")
50                         toadd.path = prefix
51                         toadd.type = "mod"
52
53                         -- Check modpack.txt
54                         --  Note: modpack.conf is already checked above
55                         local modpackfile = io.open(prefix .. DIR_DELIM .. "modpack.txt")
56                         if modpackfile then
57                                 modpackfile:close()
58                                 toadd.is_modpack = true
59                         end
60
61                         -- Deal with modpack contents
62                         if modpack and modpack ~= "" then
63                                 toadd.modpack = modpack
64                         elseif toadd.is_modpack then
65                                 toadd.type = "modpack"
66                                 toadd.is_modpack = true
67                                 get_mods(prefix, retval, name)
68                         end
69                 end
70         end
71 end
72
73 --modmanager implementation
74 pkgmgr = {}
75
76 function pkgmgr.get_texture_packs()
77         local txtpath = core.get_texturepath()
78         local list = core.get_dir_list(txtpath, true)
79         local retval = {}
80
81         local current_texture_path = core.settings:get("texture_path")
82
83         for _, item in ipairs(list) do
84                 if item ~= "base" then
85                         local name = item
86
87                         local path = txtpath .. DIR_DELIM .. item .. DIR_DELIM
88                         if path == current_texture_path then
89                                 name = fgettext("$1 (Enabled)", name)
90                         end
91
92                         local conf = Settings(path .. "texture_pack.conf")
93
94                         retval[#retval + 1] = {
95                                 name = item,
96                                 author = conf:get("author"),
97                                 release = tonumber(conf:get("release") or "0"),
98                                 list_name = name,
99                                 type = "txp",
100                                 path = path,
101                                 enabled = path == current_texture_path,
102                         }
103                 end
104         end
105
106         table.sort(retval, function(a, b)
107                 return a.name > b.name
108         end)
109
110         return retval
111 end
112
113 --------------------------------------------------------------------------------
114 function pkgmgr.extract(modfile)
115         if modfile.type == "zip" then
116                 local tempfolder = os.tempfolder()
117
118                 if tempfolder ~= nil and
119                         tempfolder ~= "" then
120                         core.create_dir(tempfolder)
121                         if core.extract_zip(modfile.name,tempfolder) then
122                                 return tempfolder
123                         end
124                 end
125         end
126         return nil
127 end
128
129 function pkgmgr.get_folder_type(path)
130         local testfile = io.open(path .. DIR_DELIM .. "init.lua","r")
131         if testfile ~= nil then
132                 testfile:close()
133                 return { type = "mod", path = path }
134         end
135
136         testfile = io.open(path .. DIR_DELIM .. "modpack.conf","r")
137         if testfile ~= nil then
138                 testfile:close()
139                 return { type = "modpack", path = path }
140         end
141
142         testfile = io.open(path .. DIR_DELIM .. "modpack.txt","r")
143         if testfile ~= nil then
144                 testfile:close()
145                 return { type = "modpack", path = path }
146         end
147
148         testfile = io.open(path .. DIR_DELIM .. "game.conf","r")
149         if testfile ~= nil then
150                 testfile:close()
151                 return { type = "game", path = path }
152         end
153
154         testfile = io.open(path .. DIR_DELIM .. "texture_pack.conf","r")
155         if testfile ~= nil then
156                 testfile:close()
157                 return { type = "txp", path = path }
158         end
159
160         return nil
161 end
162
163 -------------------------------------------------------------------------------
164 function pkgmgr.get_base_folder(temppath)
165         if temppath == nil then
166                 return { type = "invalid", path = "" }
167         end
168
169         local ret = pkgmgr.get_folder_type(temppath)
170         if ret then
171                 return ret
172         end
173
174         local subdirs = core.get_dir_list(temppath, true)
175         if #subdirs == 1 then
176                 ret = pkgmgr.get_folder_type(temppath .. DIR_DELIM .. subdirs[1])
177                 if ret then
178                         return ret
179                 else
180                         return { type = "invalid", path = temppath .. DIR_DELIM .. subdirs[1] }
181                 end
182         end
183
184         return nil
185 end
186
187 --------------------------------------------------------------------------------
188 function pkgmgr.isValidModname(modpath)
189         if modpath:find("-") ~= nil then
190                 return false
191         end
192
193         return true
194 end
195
196 --------------------------------------------------------------------------------
197 function pkgmgr.parse_register_line(line)
198         local pos1 = line:find("\"")
199         local pos2 = nil
200         if pos1 ~= nil then
201                 pos2 = line:find("\"",pos1+1)
202         end
203
204         if pos1 ~= nil and pos2 ~= nil then
205                 local item = line:sub(pos1+1,pos2-1)
206
207                 if item ~= nil and
208                         item ~= "" then
209                         local pos3 = item:find(":")
210
211                         if pos3 ~= nil then
212                                 local retval = item:sub(1,pos3-1)
213                                 if retval ~= nil and
214                                         retval ~= "" then
215                                         return retval
216                                 end
217                         end
218                 end
219         end
220         return nil
221 end
222
223 --------------------------------------------------------------------------------
224 function pkgmgr.parse_dofile_line(modpath,line)
225         local pos1 = line:find("\"")
226         local pos2 = nil
227         if pos1 ~= nil then
228                 pos2 = line:find("\"",pos1+1)
229         end
230
231         if pos1 ~= nil and pos2 ~= nil then
232                 local filename = line:sub(pos1+1,pos2-1)
233
234                 if filename ~= nil and
235                         filename ~= "" and
236                         filename:find(".lua") then
237                         return pkgmgr.identify_modname(modpath,filename)
238                 end
239         end
240         return nil
241 end
242
243 --------------------------------------------------------------------------------
244 function pkgmgr.identify_modname(modpath,filename)
245         local testfile = io.open(modpath .. DIR_DELIM .. filename,"r")
246         if testfile ~= nil then
247                 local line = testfile:read()
248
249                 while line~= nil do
250                         local modname = nil
251
252                         if line:find("minetest.register_tool") then
253                                 modname = pkgmgr.parse_register_line(line)
254                         end
255
256                         if line:find("minetest.register_craftitem") then
257                                 modname = pkgmgr.parse_register_line(line)
258                         end
259
260
261                         if line:find("minetest.register_node") then
262                                 modname = pkgmgr.parse_register_line(line)
263                         end
264
265                         if line:find("dofile") then
266                                 modname = pkgmgr.parse_dofile_line(modpath,line)
267                         end
268
269                         if modname ~= nil then
270                                 testfile:close()
271                                 return modname
272                         end
273
274                         line = testfile:read()
275                 end
276                 testfile:close()
277         end
278
279         return nil
280 end
281 --------------------------------------------------------------------------------
282 function pkgmgr.render_packagelist(render_list)
283         local retval = ""
284
285         if render_list == nil then
286                 if pkgmgr.global_mods == nil then
287                         pkgmgr.refresh_globals()
288                 end
289                 render_list = pkgmgr.global_mods
290         end
291
292         local list = render_list:get_list()
293         local last_modpack = nil
294         local retval = {}
295         for i, v in ipairs(list) do
296                 local color = ""
297                 if v.is_modpack then
298                         local rawlist = render_list:get_raw_list()
299                         color = mt_color_dark_green
300
301                         for j = 1, #rawlist, 1 do
302                                 if rawlist[j].modpack == list[i].name and
303                                                 rawlist[j].enabled ~= true then
304                                         -- Modpack not entirely enabled so showing as grey
305                                         color = mt_color_grey
306                                         break
307                                 end
308                         end
309                 elseif v.is_game_content or v.type == "game" then
310                         color = mt_color_blue
311                 elseif v.enabled or v.type == "txp" then
312                         color = mt_color_green
313                 end
314
315                 retval[#retval + 1] = color
316                 if v.modpack ~= nil or v.loc == "game" then
317                         retval[#retval + 1] = "1"
318                 else
319                         retval[#retval + 1] = "0"
320                 end
321                 retval[#retval + 1] = core.formspec_escape(v.list_name or v.name)
322         end
323
324         return table.concat(retval, ",")
325 end
326
327 --------------------------------------------------------------------------------
328 function pkgmgr.get_dependencies(path)
329         if path == nil then
330                 return "", ""
331         end
332
333         local info = core.get_content_info(path)
334         return table.concat(info.depends or {}, ","), table.concat(info.optional_depends or {}, ",")
335 end
336
337 ----------- tests whether all of the mods in the modpack are enabled -----------
338 function pkgmgr.is_modpack_entirely_enabled(data, name)
339         local rawlist = data.list:get_raw_list()
340         for j = 1, #rawlist do
341                 if rawlist[j].modpack == name and not rawlist[j].enabled then
342                         return false
343                 end
344         end
345         return true
346 end
347
348 ---------- toggles or en/disables a mod or modpack -----------------------------
349 function pkgmgr.enable_mod(this, toset)
350         local mod = this.data.list:get_list()[this.data.selected_mod]
351
352         -- game mods can't be enabled or disabled
353         if mod.is_game_content then
354                 return
355         end
356
357         -- toggle or en/disable the mod
358         if not mod.is_modpack then
359                 if toset == nil then
360                         mod.enabled = not mod.enabled
361                 else
362                         mod.enabled = toset
363                 end
364                 return
365         end
366
367         -- toggle or en/disable every mod in the modpack, interleaved unsupported
368         local list = this.data.list:get_raw_list()
369         for i = 1, #list do
370                 if list[i].modpack == mod.name then
371                         if toset == nil then
372                                 toset = not list[i].enabled
373                         end
374                         list[i].enabled = toset
375                 end
376         end
377 end
378
379 --------------------------------------------------------------------------------
380 function pkgmgr.get_worldconfig(worldpath)
381         local filename = worldpath ..
382                                 DIR_DELIM .. "world.mt"
383
384         local worldfile = Settings(filename)
385
386         local worldconfig = {}
387         worldconfig.global_mods = {}
388         worldconfig.game_mods = {}
389
390         for key,value in pairs(worldfile:to_table()) do
391                 if key == "gameid" then
392                         worldconfig.id = value
393                 elseif key:sub(0, 9) == "load_mod_" then
394                         -- Compatibility: Check against "nil" which was erroneously used
395                         -- as value for fresh configured worlds
396                         worldconfig.global_mods[key] = value ~= "false" and value ~= "nil"
397                                 and value
398                 else
399                         worldconfig[key] = value
400                 end
401         end
402
403         --read gamemods
404         local gamespec = pkgmgr.find_by_gameid(worldconfig.id)
405         pkgmgr.get_game_mods(gamespec, worldconfig.game_mods)
406
407         return worldconfig
408 end
409
410 --------------------------------------------------------------------------------
411 function pkgmgr.install_dir(type, path, basename, targetpath)
412         local basefolder = pkgmgr.get_base_folder(path)
413
414         -- There's no good way to detect a texture pack, so let's just assume
415         -- it's correct for now.
416         if type == "txp" then
417                 if basefolder and basefolder.type ~= "invalid" and basefolder.type ~= "txp" then
418                         return nil, fgettext("Unable to install a $1 as a texture pack", basefolder.type)
419                 end
420
421                 local from = basefolder and basefolder.path or path
422                 if targetpath then
423                         core.delete_dir(targetpath)
424                         core.create_dir(targetpath)
425                 else
426                         targetpath = core.get_texturepath() .. DIR_DELIM .. basename
427                 end
428                 if not core.copy_dir(from, targetpath) then
429                         return nil,
430                                 fgettext("Failed to install $1 to $2", basename, targetpath)
431                 end
432                 return targetpath, nil
433
434         elseif not basefolder then
435                 return nil, fgettext("Unable to find a valid mod or modpack")
436         end
437
438         --
439         -- Get destination
440         --
441         if basefolder.type == "modpack" then
442                 if type ~= "mod" then
443                         return nil, fgettext("Unable to install a modpack as a $1", type)
444                 end
445
446                 -- Get destination name for modpack
447                 if targetpath then
448                         core.delete_dir(targetpath)
449                         core.create_dir(targetpath)
450                 else
451                         local clean_path = nil
452                         if basename ~= nil then
453                                 clean_path = basename
454                         end
455                         if not clean_path then
456                                 clean_path = get_last_folder(cleanup_path(basefolder.path))
457                         end
458                         if clean_path then
459                                 targetpath = core.get_modpath() .. DIR_DELIM .. clean_path
460                         else
461                                 return nil,
462                                         fgettext("Install Mod: Unable to find suitable folder name for modpack $1",
463                                         modfilename)
464                         end
465                 end
466         elseif basefolder.type == "mod" then
467                 if type ~= "mod" then
468                         return nil, fgettext("Unable to install a mod as a $1", type)
469                 end
470
471                 if targetpath then
472                         core.delete_dir(targetpath)
473                         core.create_dir(targetpath)
474                 else
475                         local targetfolder = basename
476                         if targetfolder == nil then
477                                 targetfolder = pkgmgr.identify_modname(basefolder.path, "init.lua")
478                         end
479
480                         -- If heuristic failed try to use current foldername
481                         if targetfolder == nil then
482                                 targetfolder = get_last_folder(basefolder.path)
483                         end
484
485                         if targetfolder ~= nil and pkgmgr.isValidModname(targetfolder) then
486                                 targetpath = core.get_modpath() .. DIR_DELIM .. targetfolder
487                         else
488                                 return nil, fgettext("Install Mod: Unable to find real mod name for: $1", modfilename)
489                         end
490                 end
491
492         elseif basefolder.type == "game" then
493                 if type ~= "game" then
494                         return nil, fgettext("Unable to install a game as a $1", type)
495                 end
496
497                 if targetpath then
498                         core.delete_dir(targetpath)
499                         core.create_dir(targetpath)
500                 else
501                         targetpath = core.get_gamepath() .. DIR_DELIM .. basename
502                 end
503         end
504
505         -- Copy it
506         if not core.copy_dir(basefolder.path, targetpath) then
507                 return nil,
508                         fgettext("Failed to install $1 to $2", basename, targetpath)
509         end
510
511         if basefolder.type == "game" then
512                 pkgmgr.update_gamelist()
513         else
514                 pkgmgr.refresh_globals()
515         end
516
517         return targetpath, nil
518 end
519
520 --------------------------------------------------------------------------------
521 function pkgmgr.install(type, modfilename, basename, dest)
522         local archive_info = pkgmgr.identify_filetype(modfilename)
523         local path = pkgmgr.extract(archive_info)
524
525         if path == nil then
526                 return nil,
527                         fgettext("Install: file: \"$1\"", archive_info.name) .. "\n" ..
528                         fgettext("Install: Unsupported file type \"$1\" or broken archive",
529                                 archive_info.type)
530         end
531
532         local targetpath, msg = pkgmgr.install_dir(type, path, basename, dest)
533         core.delete_dir(path)
534         return targetpath, msg
535 end
536
537 --------------------------------------------------------------------------------
538 function pkgmgr.preparemodlist(data)
539         local retval = {}
540
541         local global_mods = {}
542         local game_mods = {}
543
544         --read global mods
545         local modpath = core.get_modpath()
546
547         if modpath ~= nil and
548                 modpath ~= "" then
549                 get_mods(modpath,global_mods)
550         end
551
552         for i=1,#global_mods,1 do
553                 global_mods[i].type = "mod"
554                 global_mods[i].loc = "global"
555                 retval[#retval + 1] = global_mods[i]
556         end
557
558         --read game mods
559         local gamespec = pkgmgr.find_by_gameid(data.gameid)
560         pkgmgr.get_game_mods(gamespec, game_mods)
561
562         if #game_mods > 0 then
563                 -- Add title
564                 retval[#retval + 1] = {
565                         type = "game",
566                         is_game_content = true,
567                         name = fgettext(gamespec.name .. " mods"),
568                         path = gamespec.path
569                 }
570         end
571
572         for i=1,#game_mods,1 do
573                 game_mods[i].type = "mod"
574                 game_mods[i].loc = "game"
575                 game_mods[i].is_game_content = true
576                 retval[#retval + 1] = game_mods[i]
577         end
578
579         if data.worldpath == nil then
580                 return retval
581         end
582
583         --read world mod configuration
584         local filename = data.worldpath ..
585                                 DIR_DELIM .. "world.mt"
586
587         local worldfile = Settings(filename)
588
589         for key,value in pairs(worldfile:to_table()) do
590                 if key:sub(1, 9) == "load_mod_" then
591                         key = key:sub(10)
592                         local element = nil
593                         for i=1,#retval,1 do
594                                 if retval[i].name == key and
595                                         not retval[i].is_modpack then
596                                         element = retval[i]
597                                         break
598                                 end
599                         end
600                         if element ~= nil then
601                                 element.enabled = value ~= "false" and value ~= "nil" and value
602                         else
603                                 core.log("info", "Mod: " .. key .. " " .. dump(value) .. " but not found")
604                         end
605                 end
606         end
607
608         return retval
609 end
610
611 function pkgmgr.compare_package(a, b)
612         return a and b and a.name == b.name and a.path == b.path
613 end
614
615 --------------------------------------------------------------------------------
616 function pkgmgr.comparemod(elem1,elem2)
617         if elem1 == nil or elem2 == nil then
618                 return false
619         end
620         if elem1.name ~= elem2.name then
621                 return false
622         end
623         if elem1.is_modpack ~= elem2.is_modpack then
624                 return false
625         end
626         if elem1.type ~= elem2.type then
627                 return false
628         end
629         if elem1.modpack ~= elem2.modpack then
630                 return false
631         end
632
633         if elem1.path ~= elem2.path then
634                 return false
635         end
636
637         return true
638 end
639
640 --------------------------------------------------------------------------------
641 function pkgmgr.mod_exists(basename)
642
643         if pkgmgr.global_mods == nil then
644                 pkgmgr.refresh_globals()
645         end
646
647         if pkgmgr.global_mods:raw_index_by_uid(basename) > 0 then
648                 return true
649         end
650
651         return false
652 end
653
654 --------------------------------------------------------------------------------
655 function pkgmgr.get_global_mod(idx)
656
657         if pkgmgr.global_mods == nil then
658                 return nil
659         end
660
661         if idx == nil or idx < 1 or
662                 idx > pkgmgr.global_mods:size() then
663                 return nil
664         end
665
666         return pkgmgr.global_mods:get_list()[idx]
667 end
668
669 --------------------------------------------------------------------------------
670 function pkgmgr.refresh_globals()
671         local function is_equal(element,uid) --uid match
672                 if element.name == uid then
673                         return true
674                 end
675         end
676         pkgmgr.global_mods = filterlist.create(pkgmgr.preparemodlist,
677                         pkgmgr.comparemod, is_equal, nil, {})
678         pkgmgr.global_mods:add_sort_mechanism("alphabetic", sort_mod_list)
679         pkgmgr.global_mods:set_sortmode("alphabetic")
680 end
681
682 --------------------------------------------------------------------------------
683 function pkgmgr.identify_filetype(name)
684
685         if name:sub(-3):lower() == "zip" then
686                 return {
687                                 name = name,
688                                 type = "zip"
689                                 }
690         end
691
692         if name:sub(-6):lower() == "tar.gz" or
693                 name:sub(-3):lower() == "tgz"then
694                 return {
695                                 name = name,
696                                 type = "tgz"
697                                 }
698         end
699
700         if name:sub(-6):lower() == "tar.bz2" then
701                 return {
702                                 name = name,
703                                 type = "tbz"
704                                 }
705         end
706
707         if name:sub(-2):lower() == "7z" then
708                 return {
709                                 name = name,
710                                 type = "7z"
711                                 }
712         end
713
714         return {
715                 name = name,
716                 type = "ukn"
717         }
718 end
719
720
721 --------------------------------------------------------------------------------
722 function pkgmgr.find_by_gameid(gameid)
723         for i=1,#pkgmgr.games,1 do
724                 if pkgmgr.games[i].id == gameid then
725                         return pkgmgr.games[i], i
726                 end
727         end
728         return nil, nil
729 end
730
731 --------------------------------------------------------------------------------
732 function pkgmgr.get_game_mods(gamespec, retval)
733         if gamespec ~= nil and
734                 gamespec.gamemods_path ~= nil and
735                 gamespec.gamemods_path ~= "" then
736                 get_mods(gamespec.gamemods_path, retval)
737         end
738 end
739
740 --------------------------------------------------------------------------------
741 function pkgmgr.get_game_modlist(gamespec)
742         local retval = ""
743         local game_mods = {}
744         pkgmgr.get_game_mods(gamespec, game_mods)
745         for i=1,#game_mods,1 do
746                 if retval ~= "" then
747                         retval = retval..","
748                 end
749                 retval = retval .. game_mods[i].name
750         end
751         return retval
752 end
753
754 --------------------------------------------------------------------------------
755 function pkgmgr.get_game(index)
756         if index > 0 and index <= #pkgmgr.games then
757                 return pkgmgr.games[index]
758         end
759
760         return nil
761 end
762
763 --------------------------------------------------------------------------------
764 function pkgmgr.update_gamelist()
765         pkgmgr.games = core.get_games()
766 end
767
768 --------------------------------------------------------------------------------
769 function pkgmgr.gamelist()
770         local retval = ""
771         if #pkgmgr.games > 0 then
772                 retval = retval .. core.formspec_escape(pkgmgr.games[1].name)
773
774                 for i=2,#pkgmgr.games,1 do
775                         retval = retval .. "," .. core.formspec_escape(pkgmgr.games[i].name)
776                 end
777         end
778         return retval
779 end
780
781 --------------------------------------------------------------------------------
782 -- read initial data
783 --------------------------------------------------------------------------------
784 pkgmgr.update_gamelist()