Skip to content

Commit 2b5075f

Browse files
committed
Move archive extraction in content store to async job
1 parent 2d5b7b5 commit 2b5075f

6 files changed

Lines changed: 44 additions & 102 deletions

File tree

builtin/common/misc_helpers.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ if INIT == "mainmenu" then
532532
end
533533
end
534534

535-
if INIT == "client" or INIT == "mainmenu" then
535+
if core.gettext then -- for client and mainmenu
536536
function fgettext_ne(text, ...)
537537
text = core.gettext(text)
538538
local arg = {n=select('#', ...), ...}

builtin/mainmenu/common.lua

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,9 @@ function render_serverlist_row(spec)
119119

120120
return table.concat(details, ",")
121121
end
122-
123-
--------------------------------------------------------------------------------
124-
os.tempfolder = function()
125-
local temp = core.get_temp_path()
126-
return temp .. DIR_DELIM .. "MT_" .. math.random(0, 10000)
127-
end
128-
122+
---------------------------------------------------------------------------------
129123
os.tmpname = function()
130-
local path = os.tempfolder()
131-
io.open(path, "w"):close()
132-
return path
124+
error('do not use') -- instead use core.get_temp_path()
133125
end
134126
--------------------------------------------------------------------------------
135127

builtin/mainmenu/dlg_contentstore.lua

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,34 +72,52 @@ local function get_download_url(package, reason)
7272
end
7373

7474

75-
local function download_package(param)
76-
if core.download_file(param.url, param.filename) then
75+
local function download_and_extract(param)
76+
local package = param.package
77+
78+
local filename = core.get_temp_path(true)
79+
if filename == "" or not core.download_file(param.url, filename) then
80+
core.log("error", "Downloading " .. dump(param.url) .. " failed")
7781
return {
78-
filename = param.filename,
79-
successful = true,
82+
msg = fgettext("Failed to download $1", package.name)
8083
}
84+
end
85+
86+
local tempfolder = core.get_temp_path()
87+
if tempfolder ~= "" then
88+
tempfolder = tempfolder .. DIR_DELIM .. "MT_" .. math.random(1, 1024000)
89+
if not core.extract_zip(filename, tempfolder) then
90+
tempfolder = nil
91+
end
8192
else
82-
core.log("error", "downloading " .. dump(param.url) .. " failed")
93+
tempfolder = nil
94+
end
95+
os.remove(filename)
96+
if not tempfolder then
8397
return {
84-
successful = false,
98+
msg = fgettext("Install: Unsupported file type or broken archive"),
8599
}
86100
end
101+
102+
return {
103+
path = tempfolder
104+
}
87105
end
88106

89107
local function start_install(package, reason)
90108
local params = {
91109
package = package,
92110
url = get_download_url(package, reason),
93-
filename = os.tempfolder() .. "_MODNAME_" .. package.name .. ".zip",
94111
}
95112

96113
number_downloading = number_downloading + 1
97114

98115
local function callback(result)
99-
if result.successful then
100-
local path, msg = pkgmgr.install(package.type,
101-
result.filename, package.name,
102-
package.path)
116+
if result.msg then
117+
gamedata.errormessage = result.msg
118+
else
119+
local path, msg = pkgmgr.install_dir(package.type, result.path, package.name, package.path)
120+
core.delete_dir(result.path)
103121
if not path then
104122
gamedata.errormessage = msg
105123
else
@@ -137,9 +155,6 @@ local function start_install(package, reason)
137155
conf:write()
138156
end
139157
end
140-
os.remove(result.filename)
141-
else
142-
gamedata.errormessage = fgettext("Failed to download $1", package.name)
143158
end
144159

145160
package.downloading = false
@@ -159,7 +174,7 @@ local function start_install(package, reason)
159174
package.queued = false
160175
package.downloading = true
161176

162-
if not core.handle_async(download_package, params, callback) then
177+
if not core.handle_async(download_and_extract, params, callback) then
163178
core.log("error", "ERROR: async event failed")
164179
gamedata.errormessage = fgettext("Failed to download $1", package.name)
165180
return

builtin/mainmenu/pkgmgr.lua

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -181,21 +181,6 @@ function pkgmgr.get_texture_packs()
181181
end
182182

183183
--------------------------------------------------------------------------------
184-
function pkgmgr.extract(modfile)
185-
if modfile.type == "zip" then
186-
local tempfolder = os.tempfolder()
187-
188-
if tempfolder ~= nil and
189-
tempfolder ~= "" then
190-
core.create_dir(tempfolder)
191-
if core.extract_zip(modfile.name,tempfolder) then
192-
return tempfolder
193-
end
194-
end
195-
end
196-
return nil
197-
end
198-
199184
function pkgmgr.get_folder_type(path)
200185
local testfile = io.open(path .. DIR_DELIM .. "init.lua","r")
201186
if testfile ~= nil then
@@ -657,23 +642,6 @@ function pkgmgr.install_dir(type, path, basename, targetpath)
657642
return targetpath, nil
658643
end
659644

660-
--------------------------------------------------------------------------------
661-
function pkgmgr.install(type, modfilename, basename, dest)
662-
local archive_info = pkgmgr.identify_filetype(modfilename)
663-
local path = pkgmgr.extract(archive_info)
664-
665-
if path == nil then
666-
return nil,
667-
fgettext("Install: file: \"$1\"", archive_info.name) .. "\n" ..
668-
fgettext("Install: Unsupported file type \"$1\" or broken archive",
669-
archive_info.type)
670-
end
671-
672-
local targetpath, msg = pkgmgr.install_dir(type, path, basename, dest)
673-
core.delete_dir(path)
674-
return targetpath, msg
675-
end
676-
677645
--------------------------------------------------------------------------------
678646
function pkgmgr.preparemodlist(data)
679647
local retval = {}
@@ -817,45 +785,6 @@ function pkgmgr.refresh_globals()
817785
pkgmgr.global_mods:set_sortmode("alphabetic")
818786
end
819787

820-
--------------------------------------------------------------------------------
821-
function pkgmgr.identify_filetype(name)
822-
823-
if name:sub(-3):lower() == "zip" then
824-
return {
825-
name = name,
826-
type = "zip"
827-
}
828-
end
829-
830-
if name:sub(-6):lower() == "tar.gz" or
831-
name:sub(-3):lower() == "tgz"then
832-
return {
833-
name = name,
834-
type = "tgz"
835-
}
836-
end
837-
838-
if name:sub(-6):lower() == "tar.bz2" then
839-
return {
840-
name = name,
841-
type = "tbz"
842-
}
843-
end
844-
845-
if name:sub(-2):lower() == "7z" then
846-
return {
847-
name = name,
848-
type = "7z"
849-
}
850-
end
851-
852-
return {
853-
name = name,
854-
type = "ukn"
855-
}
856-
end
857-
858-
859788
--------------------------------------------------------------------------------
860789
function pkgmgr.find_by_gameid(gameid)
861790
for i=1,#pkgmgr.games,1 do

doc/menu_lua_api.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ core.get_video_drivers()
8585
core.get_mapgen_names([include_hidden=false]) -> table of map generator algorithms
8686
registered in the core (possible in async calls)
8787
core.get_cache_path() -> path of cache
88-
core.get_temp_path() -> path of temp folder
88+
core.get_temp_path([param]) (possible in async calls)
89+
^ param=true: returns path to a temporary file
90+
^ otherwise: returns path to the temporary folder
8991

9092

9193
HTTP Requests

src/script/lua_api/l_mainmenu.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,10 @@ int ModApiMainMenu::l_get_cache_path(lua_State *L)
563563
/******************************************************************************/
564564
int ModApiMainMenu::l_get_temp_path(lua_State *L)
565565
{
566-
lua_pushstring(L, fs::TempPath().c_str());
566+
if (lua_isnoneornil(L, 1) || !lua_toboolean(L, 1))
567+
lua_pushstring(L, fs::TempPath().c_str());
568+
else
569+
lua_pushstring(L, fs::CreateTempFile().c_str());
567570
return 1;
568571
}
569572

@@ -770,8 +773,9 @@ int ModApiMainMenu::l_get_video_drivers(lua_State *L)
770773
/******************************************************************************/
771774
int ModApiMainMenu::l_gettext(lua_State *L)
772775
{
773-
std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
774-
lua_pushstring(L, text.c_str());
776+
const char *srctext = luaL_checkstring(L, 1);
777+
const char *text = *srctext ? gettext(srctext) : "";
778+
lua_pushstring(L, text);
775779

776780
return 1;
777781
}
@@ -921,5 +925,5 @@ void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
921925
API_FCT(download_file);
922926
API_FCT(get_min_supp_proto);
923927
API_FCT(get_max_supp_proto);
924-
//API_FCT(gettext); (gettext lib isn't threadsafe)
928+
API_FCT(gettext);
925929
}

0 commit comments

Comments
 (0)