1- using System ;
1+ using ModApi . Common ;
2+ using ModAPI . Common . Update ;
3+ using System ;
24using System . IO ;
5+ using System . IO . Pipes ;
36using System . Net . Http ;
4- using ModAPI . Common . Update ;
57
68namespace ModAPI . Common
79{
@@ -11,32 +13,7 @@ public class DownloadClient : IDisposable
1113 private HttpRequestMessage httpRequestMessage = new HttpRequestMessage ( ) ;
1214 private static readonly string httpUserAgent = "Spore-ModAPI-Launcher-Kit/" + UpdateManager . CurrentVersion . ToString ( ) ;
1315
14- public delegate void DownloadClientEventHandler ( object source , int percentage ) ;
15- public event DownloadClientEventHandler DownloadProgressChanged = null ;
16-
17- private void copyStreamWithProgress ( Stream inputStrem , Stream outputStream )
18- {
19- long streamLength = inputStrem . Length ;
20- byte [ ] buffer = new byte [ 4096 ] ;
21- long totalBytesRead = 0 ;
22- int bytesRead ;
23- int percentageDownloaded = 0 ;
24- int percentage ;
25-
26- while ( ( bytesRead = inputStrem . Read ( buffer , 0 , buffer . Length ) ) > 0 )
27- {
28- outputStream . Write ( buffer , 0 , bytesRead ) ;
29- totalBytesRead += bytesRead ;
30-
31- // only trigger event when percentage has changed
32- percentage = ( int ) ( ( double ) totalBytesRead / ( double ) streamLength * 100.0 ) ;
33- if ( percentageDownloaded != percentage )
34- {
35- percentageDownloaded = percentage ;
36- DownloadProgressChanged ? . Invoke ( this , percentage ) ;
37- }
38- }
39- }
16+ public event StreamUtils . StreamProgressEventHandler DownloadProgressChanged = null ;
4017
4118 public DownloadClient ( string url )
4219 {
@@ -79,7 +56,7 @@ public void DownloadToFile(string file)
7956 using ( var downloadStream = response . Content . ReadAsStreamAsync ( ) . Result )
8057 using ( var fileStream = new FileStream ( file , FileMode . Create ) )
8158 {
82- copyStreamWithProgress ( downloadStream , fileStream ) ;
59+ StreamUtils . CopyStreamWithProgress ( downloadStream , fileStream , this , DownloadProgressChanged ) ;
8360 }
8461 }
8562
@@ -96,7 +73,7 @@ public MemoryStream DownloadToMemory()
9673 using ( var downloadStream = response . Content . ReadAsStreamAsync ( ) . Result )
9774 {
9875 memoryStream = new MemoryStream ( ( int ) downloadStream . Length ) ;
99- copyStreamWithProgress ( downloadStream , memoryStream ) ;
76+ StreamUtils . CopyStreamWithProgress ( downloadStream , memoryStream , this , DownloadProgressChanged ) ;
10077 }
10178
10279 return memoryStream ;
0 commit comments