1111
1212namespace Unity . Editor . Tasks
1313{
14+ using Internal . IO ;
1415 using Logging ;
1516 using Unity . Editor . Tasks . Helpers ;
1617
@@ -31,7 +32,7 @@ public DownloadTask(
3132 RetryCount = retryCount ;
3233 Url = url ;
3334 Filename = string . IsNullOrEmpty ( filename ) ? url . Filename : filename ;
34- TargetDirectory = targetDirectory ;
35+ this . targetDirectory = targetDirectory . ToSPath ( ) ;
3536 Name = $ "Download { Url } ";
3637 Message = Filename ;
3738 }
@@ -73,8 +74,8 @@ protected virtual string RunDownload(bool success)
7374 Exception exception = null ;
7475 var attempts = 0 ;
7576 bool result = false ;
76- var partialFile = Path . Combine ( TargetDirectory , Filename + ".partial" ) ;
77- Directory . CreateDirectory ( TargetDirectory ) ;
77+ var partialFile = targetDirectory . Combine ( Filename + ".partial" ) ;
78+ targetDirectory . EnsureDirectoryExists ( ) ;
7879 do
7980 {
8081 exception = null ;
@@ -86,7 +87,7 @@ protected virtual string RunDownload(bool success)
8687 {
8788 Logger . Trace ( $ "Download of { Url } to { Destination } Attempt { attempts + 1 } of { RetryCount + 1 } ") ;
8889
89- using ( var destinationStream = File . OpenWrite ( partialFile ) )
90+ using ( var destinationStream = partialFile . OpenWrite ( FileMode . Append ) )
9091 {
9192 result = Downloader . Download ( Logger , Url , destinationStream ,
9293 ( value , total ) => {
@@ -97,7 +98,7 @@ protected virtual string RunDownload(bool success)
9798
9899 if ( result )
99100 {
100- File . Move ( partialFile , Destination ) ;
101+ partialFile . Move ( Destination ) ;
101102 }
102103 }
103104 catch ( Exception ex )
@@ -118,11 +119,12 @@ protected virtual string RunDownload(bool success)
118119
119120 public UriString Url { get ; }
120121
121- public string TargetDirectory { get ; }
122+ private SPath targetDirectory ;
123+ public string TargetDirectory => targetDirectory . ToString ( ) ;
122124
123125 public string Filename { get ; }
124126
125- public string Destination => Path . Combine ( TargetDirectory , Filename ) ;
127+ public string Destination => targetDirectory . Combine ( Filename ) . ToString ( ) ;
126128
127129 protected int RetryCount { get ; }
128130 }
@@ -170,10 +172,25 @@ public DownloadData(UriString url, string file)
170172
171173 public class Downloader : TaskQueue < string , DownloadData >
172174 {
175+ /// <summary>
176+ /// Called for every queued download task when it finishes.
177+ /// </summary>
173178 public event Action < UriString , string > OnDownloadComplete ;
179+ /// <summary>
180+ /// Called for every queued download task when it fails.
181+ /// </summary>
174182 public event Action < UriString , Exception > OnDownloadFailed ;
183+ /// <summary>
184+ /// Called for every queued download task when it starts.
185+ /// </summary>
175186 public event Action < UriString > OnDownloadStart ;
176187
188+ /// <summary>
189+ /// TaskQueue of DownloaderTask objects that can download multiple
190+ /// things in parallel.
191+ /// </summary>
192+ /// <param name="taskManager"></param>
193+ /// <param name="token"></param>
177194 public Downloader ( ITaskManager taskManager , CancellationToken token = default )
178195 : base ( taskManager , t => {
179196 var dt = t as DownloadTask ;
0 commit comments