33using System . Diagnostics ;
44using System . IO ;
55using System . Linq ;
6+ using System . Net ;
67using System . Threading . Tasks ;
78using System . Windows . Forms ;
89
9- using Downloader ;
1010using FlashpointInstaller . Common ;
1111using Microsoft . WindowsAPICodePack . Taskbar ;
1212using SharpCompress . Archives . Zip ;
@@ -21,7 +21,7 @@ public partial class Operate : Form
2121
2222 List < Component > markedComponents = new List < Component > ( ) ;
2323
24- DownloadService downloader = new DownloadService ( ) ;
24+ WebClient client = new WebClient ( ) ;
2525
2626 Stream stream ;
2727 ZipArchive archive ;
@@ -38,8 +38,7 @@ private async void Operation_Load(object sender, EventArgs e)
3838 {
3939 TaskbarManager . Instance . SetProgressState ( TaskbarProgressBarState . Normal , FPM . Main . Handle ) ;
4040
41- downloader . DownloadProgressChanged += OnDownloadProgressChanged ;
42- downloader . DownloadFileCompleted += OnDownloadFileCompleted ;
41+ client . DownloadProgressChanged += OnDownloadProgressChanged ;
4342
4443 FPM . IterateList ( FPM . Main . ComponentList . Nodes , node =>
4544 {
@@ -55,9 +54,20 @@ private async void Operation_Load(object sender, EventArgs e)
5554 foreach ( var component in markedComponents )
5655 {
5756 workingComponent = component ;
58- if ( component . Size > 0 ) stream = await downloader . DownloadFileTaskAsync ( component . URL ) ;
5957
60- if ( cancelStatus != 0 ) return ;
58+ if ( component . Size > 0 )
59+ {
60+ try
61+ {
62+ stream = new MemoryStream ( await client . DownloadDataTaskAsync ( new Uri ( component . URL ) ) ) ;
63+ }
64+ catch ( WebException ex ) when ( ex . Status == WebExceptionStatus . RequestCanceled )
65+ {
66+ client . Dispose ( ) ;
67+ cancelStatus = 2 ;
68+ return ;
69+ }
70+ }
6171
6272 await Task . Run ( ExtractComponents ) ;
6373
@@ -75,7 +85,8 @@ private async void Operation_Load(object sender, EventArgs e)
7585
7686 if ( ! File . Exists ( redistPath ) )
7787 {
78- await new DownloadService ( ) . DownloadFileTaskAsync ( "https://aka.ms/vs/17/release/vc_redist.x86.exe" , redistPath ) ;
88+ client . DownloadProgressChanged -= OnDownloadProgressChanged ;
89+ await client . DownloadFileTaskAsync ( "https://aka.ms/vs/17/release/vc_redist.x86.exe" , redistPath ) ;
7990 }
8091
8192 await Task . Run ( ( ) =>
@@ -102,11 +113,11 @@ private void OnDownloadProgressChanged(object sender, DownloadProgressChangedEve
102113 {
103114 if ( cancelStatus != 0 )
104115 {
105- downloader . CancelAsync ( ) ;
116+ client . CancelAsync ( ) ;
106117 return ;
107118 }
108119
109- double currentProgress = ( double ) e . ReceivedBytesSize / e . TotalBytesToReceive ;
120+ double currentProgress = ( double ) e . BytesReceived / e . TotalBytesToReceive ;
110121 long currentSize = workingComponent . Size ;
111122 double totalProgress = ( byteProgress + ( currentProgress / 2 * currentSize ) ) / byteTotal ;
112123
@@ -119,7 +130,7 @@ private void OnDownloadProgressChanged(object sender, DownloadProgressChangedEve
119130 {
120131 ProgressLabel . Text =
121132 $ "[{ ( int ) ( ( double ) totalProgress * 100 ) } %] Downloading component \" { workingComponent . Title } \" ... " +
122- $ "{ FPM . GetFormattedBytes ( e . ReceivedBytesSize ) } of { FPM . GetFormattedBytes ( e . TotalBytesToReceive ) } ";
133+ $ "{ FPM . GetFormattedBytes ( e . BytesReceived ) } of { FPM . GetFormattedBytes ( e . TotalBytesToReceive ) } ";
123134 } ) ;
124135
125136 FPM . Main . Invoke ( ( MethodInvoker ) delegate
@@ -130,11 +141,6 @@ private void OnDownloadProgressChanged(object sender, DownloadProgressChangedEve
130141 } ) ;
131142 }
132143
133- private void OnDownloadFileCompleted ( object sender , System . ComponentModel . AsyncCompletedEventArgs e )
134- {
135- if ( e . Cancelled ) cancelStatus = 2 ;
136- }
137-
138144 private void ExtractComponents ( )
139145 {
140146 string rootPath = FPM . Main . DestinationPath . Text ;
0 commit comments