11using System ;
22using System . Collections . Generic ;
3+ using System . ComponentModel ;
34using System . IO ;
45using System . Threading . Tasks ;
56using System . Windows . Forms ;
@@ -21,7 +22,7 @@ public partial class Install : Form
2122 List < Dictionary < string , string > > componentInfo = new List < Dictionary < string , string > > ( ) ;
2223 Dictionary < string , string > workingComponent ;
2324
24- DownloadService downloader = new DownloadService ( new DownloadConfiguration ( ) ) ;
25+ DownloadService downloader = new DownloadService ( new DownloadConfiguration { OnTheFlyDownload = false } ) ;
2526
2627 Stream stream ;
2728 ZipArchive archive ;
@@ -30,13 +31,14 @@ public partial class Install : Form
3031 long byteProgress = 0 ;
3132 long byteTotal = ( ( Main ) Application . OpenForms [ "Main" ] ) . DownloadSize ;
3233
33- bool doneCancelling = false ;
34+ int cancelStatus = 0 ;
3435
3536 public Install ( ) => InitializeComponent ( ) ;
3637
3738 private async void Install_Load ( object sender , EventArgs e )
3839 {
3940 downloader . DownloadProgressChanged += OnDownloadProgressChanged ;
41+ downloader . DownloadFileCompleted += OnDownloadFileCompleted ;
4042
4143 void Iterate ( TreeNodeCollection parent )
4244 {
@@ -57,21 +59,17 @@ void Iterate(TreeNodeCollection parent)
5759 foreach ( var component in componentInfo )
5860 {
5961 workingComponent = component ;
60-
6162 stream = await downloader . DownloadFileTaskAsync ( component [ "url" ] ) ;
63+
64+ if ( cancelStatus == 2 ) { return ; }
6265
63- if ( ! downloader . IsCancelled )
64- {
65- Directory . CreateDirectory ( mainForm . FolderTextBox . Text ) ;
66-
67- await Task . Run ( ExtractTask ) ;
68- }
69- else { return ; }
66+ Directory . CreateDirectory ( mainForm . FolderTextBox . Text ) ;
67+ await Task . Run ( ExtractTask ) ;
7068
7169 byteProgress += int . Parse ( component [ "size" ] ) ;
7270 }
7371
74- FinishInstallation ( ) ;
72+ if ( cancelStatus == 0 ) { FinishInstallation ( ) ; }
7573 }
7674
7775 private void OnDownloadProgressChanged ( object sender , DownloadProgressChangedEventArgs e )
@@ -85,15 +83,20 @@ private void OnDownloadProgressChanged(object sender, DownloadProgressChangedEve
8583 ( ( byteProgress + ( currentProgress / 2 * currentSize ) ) / byteTotal * Progress . Maximum )
8684 ) ;
8785 } ) ;
88-
86+
8987 Info . Invoke ( ( MethodInvoker ) delegate
9088 {
91- Info . Text =
89+ Info . Text =
9290 $ "Downloading component \" { workingComponent [ "title" ] } \" ... " +
9391 $ "{ Math . Truncate ( currentProgress * 100 ) } %";
9492 } ) ;
9593 }
9694
95+ private void OnDownloadFileCompleted ( object sender , AsyncCompletedEventArgs e )
96+ {
97+ if ( e . Cancelled ) { cancelStatus = 2 ; }
98+ }
99+
97100 private void ExtractTask ( )
98101 {
99102 using ( archive = ZipArchive . Open ( stream ) )
@@ -110,7 +113,7 @@ private void ExtractTask()
110113
111114 using ( TextWriter writer = File . CreateText ( infoPath + infoFile ) )
112115 {
113- writer . WriteLine ( $ "{ workingComponent [ "url " ] } { workingComponent [ "hash " ] } ") ;
116+ writer . WriteLine ( $ "{ workingComponent [ "hash " ] } { workingComponent [ "url " ] } ") ;
114117 }
115118
116119 while ( ! reader . Cancelled && reader . MoveToNextEntry ( ) )
@@ -123,38 +126,34 @@ private void ExtractTask()
123126
124127 using ( TextWriter writer = File . AppendText ( infoPath + infoFile ) )
125128 {
126- writer . WriteLine ( reader . Entry . Key ) ;
129+ writer . WriteLine ( $ " { reader . Entry . Crc } { reader . Entry . Key } " ) ;
127130 }
128131
129132 extractedSize += reader . Entry . Size ;
130133
131- try
134+ double currentProgress = ( double ) extractedSize / totalSize ;
135+ long currentSize = long . Parse ( workingComponent [ "size" ] ) ;
136+
137+ Progress . Invoke ( ( MethodInvoker ) delegate
132138 {
133- double currentProgress = ( double ) extractedSize / totalSize ;
134- long currentSize = long . Parse ( workingComponent [ "size" ] ) ;
135-
136- Progress . Invoke ( ( MethodInvoker ) delegate
137- {
138- Progress . Value = ( int ) (
139- ( byteProgress + ( currentSize / 2 ) + ( currentProgress / 2 * currentSize ) ) / byteTotal * Progress . Maximum
140- ) ;
141- } ) ;
142-
143- Info . Invoke ( ( MethodInvoker ) delegate
144- {
145- Info . Text =
146- $ "Extracting component \" { workingComponent [ "title" ] } \" ... " +
147- $ "{ Math . Truncate ( currentProgress * 100 ) } %";
148- } ) ;
149- }
150- catch { }
139+ Progress . Value = ( int ) (
140+ ( byteProgress + ( currentSize / 2 ) + ( currentProgress / 2 * currentSize ) ) / byteTotal * Progress . Maximum
141+ ) ;
142+ } ) ;
143+
144+ Info . Invoke ( ( MethodInvoker ) delegate
145+ {
146+ Info . Text =
147+ $ "Extracting component \" { workingComponent [ "title" ] } \" ... " +
148+ $ "{ Math . Truncate ( currentProgress * 100 ) } %";
149+ } ) ;
151150 }
152151
153152 if ( reader . Cancelled )
154153 {
154+ cancelStatus = 1 ;
155155 Directory . Delete ( mainForm . FolderTextBox . Text , true ) ;
156-
157- doneCancelling = true ;
156+ cancelStatus = 2 ;
158157 }
159158 }
160159 }
@@ -182,30 +181,21 @@ private void FinishInstallation()
182181 shortcut . Save ( ) ;
183182 }
184183
185- Invoke ( ( MethodInvoker ) delegate
186- {
187- Hide ( ) ;
188- mainForm . Hide ( ) ;
184+ Hide ( ) ;
185+ mainForm . Hide ( ) ;
189186
190- var FinishWindow = new Finish ( ) ;
191- FinishWindow . ShowDialog ( ) ;
192- } ) ;
187+ var FinishWindow = new Finish ( ) ;
188+ FinishWindow . ShowDialog ( ) ;
193189 }
194190
195191 private async void Cancel_Click ( object sender , EventArgs e )
196192 {
197193 Cancel . Enabled = false ;
198194
199- if ( downloader . IsBusy )
200- {
201- downloader . CancelAsync ( ) ;
202- }
203- else if ( reader != null )
204- {
205- reader . Cancel ( ) ;
195+ if ( downloader . IsBusy ) { downloader . CancelAsync ( ) ; }
196+ if ( reader != null ) { reader . Cancel ( ) ; }
206197
207- await Task . Run ( ( ) => { while ( ! doneCancelling ) { } } ) ;
208- }
198+ await Task . Run ( ( ) => { while ( cancelStatus != 2 ) { } } ) ;
209199
210200 Close ( ) ;
211201 }
0 commit comments