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