]> git.lizzy.rs Git - minetest.git/blob - builtin/mainmenu/pkgmgr.lua
Import strstr function from FreeBSD 11 libc
[minetest.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                         worldconfig.global_mods[key] = core.is_yes(value)
395                 else
396                         worldconfig[key] = value
397                 end
398         end
399
400         --read gamemods
401         local gamespec = pkgmgr.find_by_gameid(worldconfig.id)
402         pkgmgr.get_game_mods(gamespec, worldconfig.game_mods)
403
404         return worldconfig
405 end
406
407 --------------------------------------------------------------------------------
408 function pkgmgr.install_dir(type, path, basename, targetpath)
409         local basefolder = pkgmgr.get_base_folder(path)
410
411         -- There's no good way to detect a texture pack, so let's just assume
412         -- it's correct for now.
413         if type == "txp" then
414                 if basefolder and basefolder.type ~= "invalid" and basefolder.type ~= "txp" then
415                         return nil, fgettext("Unable to install a $1 as a texture pack", basefolder.type)
416                 end
417
418                 local from = basefolder and basefolder.path or path
419                 if targetpath then
420                         core.delete_dir(targetpath)
421                         core.create_dir(targetpath)
422                 else
423                         targetpath = core.get_texturepath() .. DIR_DELIM .. basename
424                 end
425                 if not core.copy_dir(from, targetpath) then
426                         return nil,
427                                 fgettext("Failed to install $1 to $2", basename, targetpath)
428                 end
429                 return targetpath, nil
430
431         elseif not basefolder then
432                 return nil, fgettext("Unable to find a valid mod or modpack")
433         end
434
435         --
436         -- Get destination
437         --
438         if basefolder.type == "modpack" then
439                 if type ~= "mod" then
440                         return nil, fgettext("Unable to install a modpack as a $1", type)
441                 end
442
443                 -- Get destination name for modpack
444                 if targetpath then
445                         core.delete_dir(targetpath)
446                         core.create_dir(targetpath)
447                 else
448                         local clean_path = nil
449                         if basename ~= nil then
450                                 clean_path = basename
451                         end
452                         if not clean_path then
453                                 clean_path = get_last_folder(cleanup_path(basefolder.path))
454                         end
455                         if clean_path then
456                                 targetpath = core.get_modpath() .. DIR_DELIM .. clean_path
457                         else
458                                 return nil,
459                                         fgettext("Install Mod: Unable to find suitable folder name for modpack $1",
460                                         modfilename)
461                         end
462                 end
463         elseif basefolder.type == "mod" then
464                 if type ~= "mod" then
465                         return nil, fgettext("Unable to install a mod as a $1", type)
466                 end
467
468                 if targetpath then
469                         core.delete_dir(targetpath)
470                         core.create_dir(targetpath)
471                 else
472                         local targetfolder = basename
473                         if targetfolder == nil then
474                                 targetfolder = pkgmgr.identify_modname(basefolder.path, "init.lua")
475                         end
476
477                         -- If heuristic failed try to use current foldername
478                         if targetfolder == nil then
479                                 targetfolder = get_last_folder(basefolder.path)
480                         end
481
482                         if targetfolder ~= nil and pkgmgr.isValidModname(targetfolder) then
483                                 targetpath = core.get_modpath() .. DIR_DELIM .. targetfolder
484                         else
485                                 return nil, fgettext("Install Mod: Unable to find real mod name for: $1", modfilename)
486                         end
487                 end
488
489         elseif basefolder.type == "game" then
490                 if type ~= "game" then
491                         return nil, fgettext("Unable to install a game as a $1", type)
492                 end
493
494                 if targetpath then
495                         core.delete_dir(targetpath)
496                         core.create_dir(targetpath)
497                 else
498                         targetpath = core.get_gamepath() .. DIR_DELIM .. basename
499                 end
500         end
501
502         -- Copy it
503         if not core.copy_dir(basefolder.path, targetpath) then
504                 return nil,
505                         fgettext("Failed to install $1 to $2", basename, targetpath)
506         end
507
508         if basefolder.type == "game" then
509                 pkgmgr.update_gamelist()
510         else
511                 pkgmgr.refresh_globals()
512         end
513
514         return targetpath, nil
515 end
516
517 --------------------------------------------------------------------------------
518 function pkgmgr.install(type, modfilename, basename, dest)
519         local archive_info = pkgmgr.identify_filetype(modfilename)
520         local path = pkgmgr.extract(archive_info)
521
522         if path == nil then
523                 return nil,
524                         fgettext("Install: file: \"$1\"", archive_info.name) .. "\n" ..
525                         fgettext("Install: Unsupported file type \"$1\" or broken archive",
526                                 archive_info.type)
527         end
528
529         local targetpath, msg = pkgmgr.install_dir(type, path, basename, dest)
530         core.delete_dir(path)
531         return targetpath, msg
532 end
533
534 --------------------------------------------------------------------------------
535 function pkgmgr.preparemodlist(data)
536         local retval = {}
537
538         local global_mods = {}
539         local game_mods = {}
540
541         --read global mods
542         local modpath = core.get_modpath()
543
544         if modpath ~= nil and
545                 modpath ~= "" then
546                 get_mods(modpath,global_mods)
547         end
548
549         for i=1,#global_mods,1 do
550                 global_mods[i].type = "mod"
551                 global_mods[i].loc = "global"
552                 retval[#retval + 1] = global_mods[i]
553         end
554
555         --read game mods
556         local gamespec = pkgmgr.find_by_gameid(data.gameid)
557         pkgmgr.get_game_mods(gamespec, game_mods)
558
559         if #game_mods > 0 then
560                 -- Add title
561                 retval[#retval + 1] = {
562                         type = "game",
563                         is_game_content = true,
564                         name = fgettext(gamespec.name .. " mods"),
565                         path = gamespec.path
566                 }
567         end
568
569         for i=1,#game_mods,1 do
570                 game_mods[i].type = "mod"
571                 game_mods[i].loc = "game"
572                 game_mods[i].is_game_content = true
573                 retval[#retval + 1] = game_mods[i]
574         end
575
576         if data.worldpath == nil then
577                 return retval
578         end
579
580         --read world mod configuration
581         local filename = data.worldpath ..
582                                 DIR_DELIM .. "world.mt"
583
584         local worldfile = Settings(filename)
585
586         for key,value in pairs(worldfile:to_table()) do
587                 if key:sub(1, 9) == "load_mod_" then
588                         key = key:sub(10)
589                         local element = nil
590                         for i=1,#retval,1 do
591                                 if retval[i].name == key and
592                                         not retval[i].is_modpack then
593                                         element = retval[i]
594                                         break
595                                 end
596                         end
597                         if element ~= nil then
598                                 element.enabled = core.is_yes(value)
599                         else
600                                 core.log("info", "Mod: " .. key .. " " .. dump(value) .. " but not found")
601                         end
602                 end
603         end
604
605         return retval
606 end
607
608 function pkgmgr.compare_package(a, b)
609         return a and b and a.name == b.name and a.path == b.path
610 end
611
612 --------------------------------------------------------------------------------
613 function pkgmgr.comparemod(elem1,elem2)
614         if elem1 == nil or elem2 == nil then
615                 return false
616         end
617         if elem1.name ~= elem2.name then
618                 return false
619         end
620         if elem1.is_modpack ~= elem2.is_modpack then
621                 return false
622         end
623         if elem1.type ~= elem2.type then
624                 return false
625         end
626         if elem1.modpack ~= elem2.modpack then
627                 return false
628         end
629
630         if elem1.path ~= elem2.path then
631                 return false
632         end
633
634         return true
635 end
636
637 --------------------------------------------------------------------------------
638 function pkgmgr.mod_exists(basename)
639
640         if pkgmgr.global_mods == nil then
641                 pkgmgr.refresh_globals()
642         end
643
644         if pkgmgr.global_mods:raw_index_by_uid(basename) > 0 then
645                 return true
646         end
647
648         return false
649 end
650
651 --------------------------------------------------------------------------------
652 function pkgmgr.get_global_mod(idx)
653
654         if pkgmgr.global_mods == nil then
655                 return nil
656         end
657
658         if idx == nil or idx < 1 or
659                 idx > pkgmgr.global_mods:size() then
660                 return nil
661         end
662
663         return pkgmgr.global_mods:get_list()[idx]
664 end
665
666 --------------------------------------------------------------------------------
667 function pkgmgr.refresh_globals()
668         local function is_equal(element,uid) --uid match
669                 if element.name == uid then
670                         return true
671                 end
672         end
673         pkgmgr.global_mods = filterlist.create(pkgmgr.preparemodlist,
674                         pkgmgr.comparemod, is_equal, nil, {})
675         pkgmgr.global_mods:add_sort_mechanism("alphabetic", sort_mod_list)
676         pkgmgr.global_mods:set_sortmode("alphabetic")
677 end
678
679 --------------------------------------------------------------------------------
680 function pkgmgr.identify_filetype(name)
681
682         if name:sub(-3):lower() == "zip" then
683                 return {
684                                 name = name,
685                                 type = "zip"
686                                 }
687         end
688
689         if name:sub(-6):lower() == "tar.gz" or
690                 name:sub(-3):lower() == "tgz"then
691                 return {
692                                 name = name,
693                                 type = "tgz"
694                                 }
695         end
696
697         if name:sub(-6):lower() == "tar.bz2" then
698                 return {
699                                 name = name,
700                                 type = "tbz"
701                                 }
702         end
703
704         if name:sub(-2):lower() == "7z" then
705                 return {
706                                 name = name,
707                                 type = "7z"
708                                 }
709         end
710
711         return {
712                 name = name,
713                 type = "ukn"
714         }
715 end
716
717
718 --------------------------------------------------------------------------------
719 function pkgmgr.find_by_gameid(gameid)
720         for i=1,#pkgmgr.games,1 do
721                 if pkgmgr.games[i].id == gameid then
722                         return pkgmgr.games[i], i
723                 end
724         end
725         return nil, nil
726 end
727
728 --------------------------------------------------------------------------------
729 function pkgmgr.get_game_mods(gamespec, retval)
730         if gamespec ~= nil and
731                 gamespec.gamemods_path ~= nil and
732                 gamespec.gamemods_path ~= "" then
733                 get_mods(gamespec.gamemods_path, retval)
734         end
735 end
736
737 --------------------------------------------------------------------------------
738 function pkgmgr.get_game_modlist(gamespec)
739         local retval = ""
740         local game_mods = {}
741         pkgmgr.get_game_mods(gamespec, game_mods)
742         for i=1,#game_mods,1 do
743                 if retval ~= "" then
744                         retval = retval..","
745                 end
746                 retval = retval .. game_mods[i].name
747         end
748         return retval
749 end
750
751 --------------------------------------------------------------------------------
752 function pkgmgr.get_game(index)
753         if index > 0 and index <= #pkgmgr.games then
754                 return pkgmgr.games[index]
755         end
756
757         return nil
758 end
759
760 --------------------------------------------------------------------------------
761 function pkgmgr.update_gamelist()
762         pkgmgr.games = core.get_games()
763 end
764
765 --------------------------------------------------------------------------------
766 function pkgmgr.gamelist()
767         local retval = ""
768         if #pkgmgr.games > 0 then
769                 retval = retval .. core.formspec_escape(pkgmgr.games[1].name)
770
771                 for i=2,#pkgmgr.games,1 do
772                         retval = retval .. "," .. core.formspec_escape(pkgmgr.games[i].name)
773                 end
774         end
775         return retval
776 end
777
778 --------------------------------------------------------------------------------
779 -- read initial data
780 --------------------------------------------------------------------------------
781 pkgmgr.update_gamelist()