5
5
using System . IO ;
6
6
using System . Linq ;
7
7
using System . Net ;
8
+ using System . Net . Http ;
8
9
using System . Net . NetworkInformation ;
9
10
using System . Runtime . InteropServices ;
10
11
using System . Text ;
11
12
using System . Text . RegularExpressions ;
13
+ using System . Threading ;
12
14
using System . Threading . Tasks ;
13
15
using System . Windows ;
14
16
using System . Windows . Controls ;
@@ -697,7 +699,7 @@ public static async void DownloadAndInstall(string version)
697
699
if ( File . Exists ( tempFile ) == true ) File . Delete ( tempFile ) ;
698
700
699
701
// TODO make async
700
- if ( DownloadFile ( exeURL , tempFile ) == true )
702
+ if ( await DownloadFileAsync ( exeURL , tempFile ) )
701
703
{
702
704
// get base version, to use for install path
703
705
// FIXME check if have any paths?
@@ -737,7 +739,7 @@ public static async void DownloadAndInstall(string version)
737
739
738
740
static readonly string initFileDefaultURL = "https://raw.githubusercontent.com/unitycoder/UnityInitializeProject/main/Assets/Editor/InitializeProject.cs" ;
739
741
740
- public static void DownloadInitScript ( string currentInitScriptFullPath , string currentInitScriptLocationOrURL )
742
+ public static async Task DownloadInitScript ( string currentInitScriptFullPath , string currentInitScriptLocationOrURL )
741
743
{
742
744
string currentInitScriptFolder = Path . GetDirectoryName ( currentInitScriptFullPath ) ;
743
745
string currentInitScriptFile = Path . GetFileName ( currentInitScriptFullPath ) ;
@@ -750,7 +752,7 @@ public static void DownloadInitScript(string currentInitScriptFullPath, string c
750
752
if ( currentInitScriptLocationOrURL . ToLower ( ) . StartsWith ( "http" ) == true )
751
753
{
752
754
// download into temp first
753
- if ( DownloadFile ( currentInitScriptLocationOrURL , tempFile ) == false ) return ;
755
+ if ( await DownloadFileAsync ( currentInitScriptLocationOrURL , tempFile ) == false ) return ;
754
756
}
755
757
else // file is in local folders/drives/projects
756
758
{
@@ -819,25 +821,6 @@ static void DeleteTempFile(string path)
819
821
}
820
822
}
821
823
822
- static bool DownloadFile ( string url , string tempFile )
823
- {
824
- bool result = false ;
825
- try
826
- {
827
- using ( WebClient client = new WebClient ( ) )
828
- {
829
- client . DownloadFile ( url , tempFile ) ;
830
- // TODO check if actually exists
831
- result = true ;
832
- }
833
- }
834
- catch ( Exception e )
835
- {
836
- Console . WriteLine ( "Error> DownloadFile: " + e ) ;
837
- }
838
- return result ;
839
- }
840
-
841
824
public static string DownloadHTML ( string url )
842
825
{
843
826
if ( string . IsNullOrEmpty ( url ) == true ) return null ;
@@ -2228,6 +2211,55 @@ internal static void OpenCustomAssetPath()
2228
2211
}
2229
2212
}
2230
2213
}
2214
+
2215
+ private static async Task < bool > DownloadFileAsync ( string fileUrl , string destinationPath )
2216
+ {
2217
+ var cancellationTokenSource = new CancellationTokenSource ( ) ;
2218
+ var fileName = Path . GetFileName ( fileUrl ) ;
2219
+ var progressWindow = new DownloadProgressWindow ( fileName , ( ) => cancellationTokenSource . Cancel ( ) ) ;
2220
+ progressWindow . Show ( ) ;
2221
+ var result = false ;
2222
+ try
2223
+ {
2224
+ using ( var client = new HttpClient ( ) )
2225
+ using ( var response = await client . GetAsync ( fileUrl , HttpCompletionOption . ResponseHeadersRead , cancellationTokenSource . Token ) )
2226
+ {
2227
+ response . EnsureSuccessStatusCode ( ) ;
2228
+
2229
+ var totalBytes = response . Content . Headers . ContentLength ?? 1 ;
2230
+ var buffer = new byte [ 8192 ] ;
2231
+ var totalRead = 0 ;
2232
+
2233
+ using ( var contentStream = await response . Content . ReadAsStreamAsync ( ) )
2234
+ using ( var fileStream = new FileStream ( destinationPath , FileMode . Create , FileAccess . Write ,
2235
+ FileShare . None , buffer . Length , true ) )
2236
+ {
2237
+ int bytesRead ;
2238
+ while ( ( bytesRead = await contentStream . ReadAsync ( buffer , 0 , buffer . Length , cancellationTokenSource . Token ) ) > 0 )
2239
+ {
2240
+ await fileStream . WriteAsync ( buffer , 0 , bytesRead , cancellationTokenSource . Token ) ;
2241
+ totalRead += bytesRead ;
2242
+ progressWindow . UpdateProgress ( new DownloadProgress ( totalRead , totalBytes ) ) ;
2243
+ }
2244
+ result = true ;
2245
+ }
2246
+ }
2247
+ }
2248
+ catch ( TaskCanceledException )
2249
+ {
2250
+ Console . WriteLine ( "Download cancelled" ) ;
2251
+ }
2252
+ catch ( Exception e )
2253
+ {
2254
+ Console . WriteLine ( e ) ;
2255
+ }
2256
+ finally
2257
+ {
2258
+ DeleteTempFile ( destinationPath ) ;
2259
+ progressWindow . Close ( ) ;
2260
+ }
2261
+ return result ;
2262
+ }
2231
2263
} // class
2232
2264
2233
2265
} // namespace
0 commit comments