55using System . IO ;
66using System . Linq ;
77using System . Net ;
8+ using System . Net . Http ;
89using System . Net . NetworkInformation ;
910using System . Runtime . InteropServices ;
1011using System . Text ;
1112using System . Text . RegularExpressions ;
13+ using System . Threading ;
1214using System . Threading . Tasks ;
1315using System . Windows ;
1416using System . Windows . Controls ;
@@ -697,7 +699,7 @@ public static async void DownloadAndInstall(string version)
697699 if ( File . Exists ( tempFile ) == true ) File . Delete ( tempFile ) ;
698700
699701 // TODO make async
700- if ( DownloadFile ( exeURL , tempFile ) == true )
702+ if ( await DownloadFileAsync ( exeURL , tempFile ) )
701703 {
702704 // get base version, to use for install path
703705 // FIXME check if have any paths?
@@ -737,7 +739,7 @@ public static async void DownloadAndInstall(string version)
737739
738740 static readonly string initFileDefaultURL = "https://raw.githubusercontent.com/unitycoder/UnityInitializeProject/main/Assets/Editor/InitializeProject.cs" ;
739741
740- public static void DownloadInitScript ( string currentInitScriptFullPath , string currentInitScriptLocationOrURL )
742+ public static async Task DownloadInitScript ( string currentInitScriptFullPath , string currentInitScriptLocationOrURL )
741743 {
742744 string currentInitScriptFolder = Path . GetDirectoryName ( currentInitScriptFullPath ) ;
743745 string currentInitScriptFile = Path . GetFileName ( currentInitScriptFullPath ) ;
@@ -750,7 +752,7 @@ public static void DownloadInitScript(string currentInitScriptFullPath, string c
750752 if ( currentInitScriptLocationOrURL . ToLower ( ) . StartsWith ( "http" ) == true )
751753 {
752754 // download into temp first
753- if ( DownloadFile ( currentInitScriptLocationOrURL , tempFile ) == false ) return ;
755+ if ( await DownloadFileAsync ( currentInitScriptLocationOrURL , tempFile ) == false ) return ;
754756 }
755757 else // file is in local folders/drives/projects
756758 {
@@ -819,25 +821,6 @@ static void DeleteTempFile(string path)
819821 }
820822 }
821823
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-
841824 public static string DownloadHTML ( string url )
842825 {
843826 if ( string . IsNullOrEmpty ( url ) == true ) return null ;
@@ -2228,6 +2211,55 @@ internal static void OpenCustomAssetPath()
22282211 }
22292212 }
22302213 }
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+ }
22312263 } // class
22322264
22332265} // namespace
0 commit comments