]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Move archive extraction in content store to async job
authorsfan5 <sfan5@live.de>
Sun, 19 Sep 2021 15:55:01 +0000 (17:55 +0200)
committersfan5 <sfan5@live.de>
Wed, 6 Oct 2021 22:20:01 +0000 (00:20 +0200)
builtin/common/misc_helpers.lua
builtin/mainmenu/common.lua
builtin/mainmenu/dlg_contentstore.lua
builtin/mainmenu/pkgmgr.lua
doc/menu_lua_api.txt
src/script/lua_api/l_mainmenu.cpp

index c2452fe00d931775a80c27dbcc9b0eb383e15c9e..f5f89acd7942acdfb6963064fb42437e745d8bba 100644 (file)
@@ -532,7 +532,7 @@ if INIT == "mainmenu" then
        end
 end
 
-if INIT == "client" or INIT == "mainmenu" then
+if core.gettext then -- for client and mainmenu
        function fgettext_ne(text, ...)
                text = core.gettext(text)
                local arg = {n=select('#', ...), ...}
index 6db3510481e1701c89d2090f8de2f7643513cdc1..b36c9596a03aa8edb1c8d94e45bbf1dea13aa8ba 100644 (file)
@@ -119,17 +119,9 @@ function render_serverlist_row(spec)
 
        return table.concat(details, ",")
 end
-
---------------------------------------------------------------------------------
-os.tempfolder = function()
-       local temp = core.get_temp_path()
-       return temp .. DIR_DELIM .. "MT_" .. math.random(0, 10000)
-end
-
+---------------------------------------------------------------------------------
 os.tmpname = function()
-       local path = os.tempfolder()
-       io.open(path, "w"):close()
-       return path
+       error('do not use') -- instead use core.get_temp_path()
 end
 --------------------------------------------------------------------------------
 
index a3c72aee4e0da28a7f2bc9c9547d7fa8016005a9..58421ef759c704996d49bfe4ced32183ac06809f 100644 (file)
@@ -72,34 +72,52 @@ local function get_download_url(package, reason)
 end
 
 
-local function download_package(param)
-       if core.download_file(param.url, param.filename) then
+local function download_and_extract(param)
+       local package = param.package
+
+       local filename = core.get_temp_path(true)
+       if filename == "" or not core.download_file(param.url, filename) then
+               core.log("error", "Downloading " .. dump(param.url) .. " failed")
                return {
-                       filename = param.filename,
-                       successful = true,
+                       msg = fgettext("Failed to download $1", package.name)
                }
+       end
+
+       local tempfolder = core.get_temp_path()
+       if tempfolder ~= "" then
+               tempfolder = tempfolder .. DIR_DELIM .. "MT_" .. math.random(1, 1024000)
+               if not core.extract_zip(filename, tempfolder) then
+                       tempfolder = nil
+               end
        else
-               core.log("error", "downloading " .. dump(param.url) .. " failed")
+               tempfolder = nil
+       end
+       os.remove(filename)
+       if not tempfolder then
                return {
-                       successful = false,
+                       msg = fgettext("Install: Unsupported file type or broken archive"),
                }
        end
+
+       return {
+               path = tempfolder
+       }
 end
 
 local function start_install(package, reason)
        local params = {
                package = package,
                url = get_download_url(package, reason),
-               filename = os.tempfolder() .. "_MODNAME_" .. package.name .. ".zip",
        }
 
        number_downloading = number_downloading + 1
 
        local function callback(result)
-               if result.successful then
-                       local path, msg = pkgmgr.install(package.type,
-                                       result.filename, package.name,
-                                       package.path)
+               if result.msg then
+                       gamedata.errormessage = result.msg
+               else
+                       local path, msg = pkgmgr.install_dir(package.type, result.path, package.name, package.path)
+                       core.delete_dir(result.path)
                        if not path then
                                gamedata.errormessage = msg
                        else
@@ -137,9 +155,6 @@ local function start_install(package, reason)
                                        conf:write()
                                end
                        end
-                       os.remove(result.filename)
-               else
-                       gamedata.errormessage = fgettext("Failed to download $1", package.name)
                end
 
                package.downloading = false
@@ -159,7 +174,7 @@ local function start_install(package, reason)
        package.queued = false
        package.downloading = true
 
-       if not core.handle_async(download_package, params, callback) then
+       if not core.handle_async(download_and_extract, params, callback) then
                core.log("error", "ERROR: async event failed")
                gamedata.errormessage = fgettext("Failed to download $1", package.name)
                return
index 76d4a4123f22ed0227b14ba10a91a40fd1553434..d07dc019c8377ba8572ec7c10b8abd8c7155068f 100644 (file)
@@ -181,21 +181,6 @@ function pkgmgr.get_texture_packs()
 end
 
 --------------------------------------------------------------------------------
-function pkgmgr.extract(modfile)
-       if modfile.type == "zip" then
-               local tempfolder = os.tempfolder()
-
-               if tempfolder ~= nil and
-                       tempfolder ~= "" then
-                       core.create_dir(tempfolder)
-                       if core.extract_zip(modfile.name,tempfolder) then
-                               return tempfolder
-                       end
-               end
-       end
-       return nil
-end
-
 function pkgmgr.get_folder_type(path)
        local testfile = io.open(path .. DIR_DELIM .. "init.lua","r")
        if testfile ~= nil then
@@ -657,23 +642,6 @@ function pkgmgr.install_dir(type, path, basename, targetpath)
        return targetpath, nil
 end
 
---------------------------------------------------------------------------------
-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 file type \"$1\" or broken archive",
-                               archive_info.type)
-       end
-
-       local targetpath, msg = pkgmgr.install_dir(type, path, basename, dest)
-       core.delete_dir(path)
-       return targetpath, msg
-end
-
 --------------------------------------------------------------------------------
 function pkgmgr.preparemodlist(data)
        local retval = {}
@@ -817,45 +785,6 @@ function pkgmgr.refresh_globals()
        pkgmgr.global_mods:set_sortmode("alphabetic")
 end
 
---------------------------------------------------------------------------------
-function pkgmgr.identify_filetype(name)
-
-       if name:sub(-3):lower() == "zip" then
-               return {
-                               name = name,
-                               type = "zip"
-                               }
-       end
-
-       if name:sub(-6):lower() == "tar.gz" or
-               name:sub(-3):lower() == "tgz"then
-               return {
-                               name = name,
-                               type = "tgz"
-                               }
-       end
-
-       if name:sub(-6):lower() == "tar.bz2" then
-               return {
-                               name = name,
-                               type = "tbz"
-                               }
-       end
-
-       if name:sub(-2):lower() == "7z" then
-               return {
-                               name = name,
-                               type = "7z"
-                               }
-       end
-
-       return {
-               name = name,
-               type = "ukn"
-       }
-end
-
-
 --------------------------------------------------------------------------------
 function pkgmgr.find_by_gameid(gameid)
        for i=1,#pkgmgr.games,1 do
index b4b6eaba2a4e306d3fb56eb203aeb5a48f539876..9bc0c46bd555bb17a16ec2125ace70b03b7b09d3 100644 (file)
@@ -85,7 +85,9 @@ core.get_video_drivers()
 core.get_mapgen_names([include_hidden=false]) -> table of map generator algorithms
     registered in the core (possible in async calls)
 core.get_cache_path() -> path of cache
-core.get_temp_path() -> path of temp folder
+core.get_temp_path([param]) (possible in async calls)
+^ param=true: returns path to a temporary file
+^ otherwise: returns path to the temporary folder
 
 
 HTTP Requests
index 4cfbaec711fbc378c30f9b5232df73b02acf66a4..2a6a9c32d7f5ce15e9f66e46ad10266ae509f405 100644 (file)
@@ -563,7 +563,10 @@ int ModApiMainMenu::l_get_cache_path(lua_State *L)
 /******************************************************************************/
 int ModApiMainMenu::l_get_temp_path(lua_State *L)
 {
-       lua_pushstring(L, fs::TempPath().c_str());
+       if (lua_isnoneornil(L, 1) || !lua_toboolean(L, 1))
+               lua_pushstring(L, fs::TempPath().c_str());
+       else
+               lua_pushstring(L, fs::CreateTempFile().c_str());
        return 1;
 }
 
@@ -770,8 +773,9 @@ int ModApiMainMenu::l_get_video_drivers(lua_State *L)
 /******************************************************************************/
 int ModApiMainMenu::l_gettext(lua_State *L)
 {
-       std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
-       lua_pushstring(L, text.c_str());
+       const char *srctext = luaL_checkstring(L, 1);
+       const char *text = *srctext ? gettext(srctext) : "";
+       lua_pushstring(L, text);
 
        return 1;
 }
@@ -921,5 +925,5 @@ void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
        API_FCT(download_file);
        API_FCT(get_min_supp_proto);
        API_FCT(get_max_supp_proto);
-       //API_FCT(gettext); (gettext lib isn't threadsafe)
+       API_FCT(gettext);
 }