@@ -72,34 +72,52 @@ local function get_download_url(package, reason)
7272end
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+ }
87105end
88106
89107local 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
0 commit comments