]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - builtin/mainmenu/pkgmgr.lua
Make advanced settings noiseparams strings translatable (#8062)
[dragonfireclient.git] / builtin / mainmenu / pkgmgr.lua
index b83488848d9bb436e752d77b3b9a9630d33bf050..7863f1ca1a893fa65d5bfca93a16a368101d4a56 100644 (file)
@@ -21,30 +21,50 @@ function get_mods(path,retval,modpack)
 
        for _, name in ipairs(mods) do
                if name:sub(1, 1) ~= "." then
-                       local prefix = path .. DIR_DELIM .. name .. DIR_DELIM
+                       local prefix = path .. DIR_DELIM .. name
                        local toadd = {}
                        retval[#retval + 1] = toadd
 
-                       local mod_conf = Settings(prefix .. "mod.conf"):to_table()
-                       if mod_conf.name then
-                               name = mod_conf.name
+                       -- Get config file
+                       local mod_conf
+                       local modpack_conf = io.open(prefix .. DIR_DELIM .. "modpack.conf")
+                       if modpack_conf then
+                               toadd.is_modpack = true
+                               modpack_conf:close()
+
+                               mod_conf = Settings(prefix .. DIR_DELIM .. "modpack.conf"):to_table()
+                               if mod_conf.name then
+                                       name = mod_conf.name
+                               end
+                       else
+                               mod_conf = Settings(prefix .. DIR_DELIM .. "mod.conf"):to_table()
+                               if mod_conf.name then
+                                       name = mod_conf.name
+                               end
                        end
 
+                       -- Read from config
                        toadd.name = name
                        toadd.author = mod_conf.author
+                       toadd.release = tonumber(mod_conf.release or "0")
                        toadd.path = prefix
                        toadd.type = "mod"
 
-                       if modpack ~= nil and modpack ~= "" then
+                       -- Check modpack.txt
+                       --  Note: modpack.conf is already checked above
+                       local modpackfile = io.open(prefix .. DIR_DELIM .. "modpack.txt")
+                       if modpackfile then
+                               modpackfile:close()
+                               toadd.is_modpack = true
+                       end
+
+                       -- Deal with modpack contents
+                       if modpack and modpack ~= "" then
                                toadd.modpack = modpack
-                       else
-                               local modpackfile = io.open(prefix .. "modpack.txt")
-                               if modpackfile then
-                                       modpackfile:close()
-                                       toadd.type = "modpack"
-                                       toadd.is_modpack = true
-                                       get_mods(prefix, retval, name)
-                               end
+                       elseif toadd.is_modpack then
+                               toadd.type = "modpack"
+                               toadd.is_modpack = true
+                               get_mods(prefix, retval, name)
                        end
                end
        end
@@ -74,6 +94,7 @@ function pkgmgr.get_texture_packs()
                        retval[#retval + 1] = {
                                name = item,
                                author = conf:get("author"),
+                               release = tonumber(conf:get("release") or "0"),
                                list_name = name,
                                type = "txp",
                                path = path,
@@ -112,6 +133,12 @@ function pkgmgr.get_folder_type(path)
                return { type = "mod", path = path }
        end
 
+       testfile = io.open(path .. DIR_DELIM .. "modpack.conf","r")
+       if testfile ~= nil then
+               testfile:close()
+               return { type = "modpack", path = path }
+       end
+
        testfile = io.open(path .. DIR_DELIM .. "modpack.txt","r")
        if testfile ~= nil then
                testfile:close()
@@ -307,6 +334,48 @@ function pkgmgr.get_dependencies(path)
        return table.concat(info.depends or {}, ","), table.concat(info.optional_depends or {}, ",")
 end
 
+----------- tests whether all of the mods in the modpack are enabled -----------
+function pkgmgr.is_modpack_entirely_enabled(data, name)
+       local rawlist = data.list:get_raw_list()
+       for j = 1, #rawlist do
+               if rawlist[j].modpack == name and not rawlist[j].enabled then
+                       return false
+               end
+       end
+       return true
+end
+
+---------- toggles or en/disables a mod or modpack -----------------------------
+function pkgmgr.enable_mod(this, toset)
+       local mod = this.data.list:get_list()[this.data.selected_mod]
+
+       -- game mods can't be enabled or disabled
+       if mod.is_game_content then
+               return
+       end
+
+       -- toggle or en/disable the mod
+       if not mod.is_modpack then
+               if toset == nil then
+                       mod.enabled = not mod.enabled
+               else
+                       mod.enabled = toset
+               end
+               return
+       end
+
+       -- toggle or en/disable every mod in the modpack, interleaved unsupported
+       local list = this.data.list:get_raw_list()
+       for i = 1, #list do
+               if list[i].modpack == mod.name then
+                       if toset == nil then
+                               toset = not list[i].enabled
+                       end
+                       list[i].enabled = toset
+               end
+       end
+end
+
 --------------------------------------------------------------------------------
 function pkgmgr.get_worldconfig(worldpath)
        local filename = worldpath ..
@@ -322,7 +391,10 @@ function pkgmgr.get_worldconfig(worldpath)
                if key == "gameid" then
                        worldconfig.id = value
                elseif key:sub(0, 9) == "load_mod_" then
-                       worldconfig.global_mods[key] = core.is_yes(value)
+                       -- Compatibility: Check against "nil" which was erroneously used
+                       -- as value for fresh configured worlds
+                       worldconfig.global_mods[key] = value ~= "false" and value ~= "nil"
+                               and value
                else
                        worldconfig[key] = value
                end
@@ -336,101 +408,128 @@ function pkgmgr.get_worldconfig(worldpath)
 end
 
 --------------------------------------------------------------------------------
-function pkgmgr.install_dir(type, path, basename)
+function pkgmgr.install_dir(type, path, basename, targetpath)
        local basefolder = pkgmgr.get_base_folder(path)
 
-       local targetpath
+       -- There's no good way to detect a texture pack, so let's just assume
+       -- it's correct for now.
        if type == "txp" then
                if basefolder and basefolder.type ~= "invalid" and basefolder.type ~= "txp" then
                        return nil, fgettext("Unable to install a $1 as a texture pack", basefolder.type)
                end
 
                local from = basefolder and basefolder.path or path
-               targetpath = core.get_texturepath() .. DIR_DELIM .. basename
-               core.copy_dir(from, targetpath)
+               if targetpath then
+                       core.delete_dir(targetpath)
+                       core.create_dir(targetpath)
+               else
+                       targetpath = core.get_texturepath() .. DIR_DELIM .. basename
+               end
+               if not core.copy_dir(from, targetpath) then
+                       return nil,
+                               fgettext("Failed to install $1 to $2", basename, targetpath)
+               end
                return targetpath, nil
 
        elseif not basefolder then
                return nil, fgettext("Unable to find a valid mod or modpack")
        end
 
+       --
+       -- Get destination
+       --
        if basefolder.type == "modpack" then
                if type ~= "mod" then
                        return nil, fgettext("Unable to install a modpack as a $1", type)
                end
-               local clean_path = nil
-
-               if basename ~= nil then
-                       clean_path = "mp_" .. basename
-               end
-
-               if clean_path == nil then
-                       clean_path = get_last_folder(cleanup_path(basefolder.path))
-               end
 
-               if clean_path ~= nil then
-                       targetpath = core.get_modpath() .. DIR_DELIM .. clean_path
-                       if not core.copy_dir(basefolder.path,targetpath) then
+               -- Get destination name for modpack
+               if targetpath then
+                       core.delete_dir(targetpath)
+                       core.create_dir(targetpath)
+               else
+                       local clean_path = nil
+                       if basename ~= nil then
+                               clean_path = basename
+                       end
+                       if not clean_path then
+                               clean_path = get_last_folder(cleanup_path(basefolder.path))
+                       end
+                       if clean_path then
+                               targetpath = core.get_modpath() .. DIR_DELIM .. clean_path
+                       else
                                return nil,
-                                       fgettext("Failed to install $1 to $2", basename, targetpath)
+                                       fgettext("Install Mod: Unable to find suitable folder name for modpack $1",
+                                       modfilename)
                        end
-               else
-                       return nil,
-                               fgettext("Install Mod: unable to find suitable foldername for modpack $1",
-                               modfilename)
                end
-
-               pkgmgr.refresh_globals()
-
        elseif basefolder.type == "mod" then
                if type ~= "mod" then
                        return nil, fgettext("Unable to install a mod as a $1", type)
                end
-               local targetfolder = basename
 
-               if targetfolder == nil then
-                       targetfolder = pkgmgr.identify_modname(basefolder.path,"init.lua")
-               end
+               if targetpath then
+                       core.delete_dir(targetpath)
+                       core.create_dir(targetpath)
+               else
+                       local targetfolder = basename
+                       if targetfolder == nil then
+                               targetfolder = pkgmgr.identify_modname(basefolder.path, "init.lua")
+                       end
 
-               --if heuristic failed try to use current foldername
-               if targetfolder == nil then
-                       targetfolder = get_last_folder(basefolder.path)
-               end
+                       -- If heuristic failed try to use current foldername
+                       if targetfolder == nil then
+                               targetfolder = get_last_folder(basefolder.path)
+                       end
 
-               if targetfolder ~= nil and pkgmgr.isValidModname(targetfolder) then
-                       targetpath = core.get_modpath() .. DIR_DELIM .. targetfolder
-                       core.copy_dir(basefolder.path, targetpath)
-               else
-                       return nil, fgettext("Install Mod: unable to find real modname for: $1", modfilename)
+                       if targetfolder ~= nil and pkgmgr.isValidModname(targetfolder) then
+                               targetpath = core.get_modpath() .. DIR_DELIM .. targetfolder
+                       else
+                               return nil, fgettext("Install Mod: Unable to find real mod name for: $1", modfilename)
+                       end
                end
 
-               pkgmgr.refresh_globals()
-
        elseif basefolder.type == "game" then
                if type ~= "game" then
                        return nil, fgettext("Unable to install a game as a $1", type)
                end
 
-               targetpath = core.get_gamepath() .. DIR_DELIM .. basename
-               core.copy_dir(basefolder.path, targetpath)
+               if targetpath then
+                       core.delete_dir(targetpath)
+                       core.create_dir(targetpath)
+               else
+                       targetpath = core.get_gamepath() .. DIR_DELIM .. basename
+               end
+       end
+
+       -- Copy it
+       if not core.copy_dir(basefolder.path, targetpath) then
+               return nil,
+                       fgettext("Failed to install $1 to $2", basename, targetpath)
+       end
+
+       if basefolder.type == "game" then
+               pkgmgr.update_gamelist()
+       else
+               pkgmgr.refresh_globals()
        end
 
        return targetpath, nil
 end
 
 --------------------------------------------------------------------------------
-function pkgmgr.install(type, modfilename, basename)
+function pkgmgr.install(type, modfilename, basename, dest)
        local archive_info = pkgmgr.identify_filetype(modfilename)
        local path = pkgmgr.extract(archive_info)
 
        if path == nil then
                return nil,
                        fgettext("Install: file: \"$1\"", archive_info.name) .. "\n" ..
-                       fgettext("Install: unsupported filetype \"$1\" or broken archive",
+                       fgettext("Install: Unsupported file type \"$1\" or broken archive",
                                archive_info.type)
        end
 
-       local targetpath, msg = pkgmgr.install_dir(type, path, basename)
+       local targetpath, msg = pkgmgr.install_dir(type, path, basename, dest)
        core.delete_dir(path)
        return targetpath, msg
 end
@@ -465,7 +564,8 @@ function pkgmgr.preparemodlist(data)
                retval[#retval + 1] = {
                        type = "game",
                        is_game_content = true,
-                       name = fgettext("Subgame Mods")
+                       name = fgettext("$1 mods", gamespec.name),
+                       path = gamespec.path
                }
        end
 
@@ -498,7 +598,7 @@ function pkgmgr.preparemodlist(data)
                                end
                        end
                        if element ~= nil then
-                               element.enabled = core.is_yes(value)
+                               element.enabled = value ~= "false" and value ~= "nil" and value
                        else
                                core.log("info", "Mod: " .. key .. " " .. dump(value) .. " but not found")
                        end