66using System . Windows . Forms ;
77
88using Downloader ;
9+ using FlashpointInstaller . Common ;
910using IWshRuntimeLibrary ;
1011using SharpCompress . Archives . Zip ;
1112using SharpCompress . Common ;
@@ -61,40 +62,45 @@ void Iterate(TreeNodeCollection parent)
6162 workingComponent = component ;
6263 stream = await downloader . DownloadFileTaskAsync ( component [ "url" ] ) ;
6364
64- if ( cancelStatus == 2 ) { return ; }
65+ if ( cancelStatus != 0 ) return ;
6566
6667 Directory . CreateDirectory ( mainForm . FolderTextBox . Text ) ;
6768 await Task . Run ( ExtractTask ) ;
6869
6970 byteProgress += int . Parse ( component [ "size" ] ) ;
7071 }
7172
72- if ( cancelStatus == 0 ) { FinishInstallation ( ) ; }
73+ if ( cancelStatus == 0 ) FinishInstallation ( ) ;
7374 }
7475
7576 private void OnDownloadProgressChanged ( object sender , DownloadProgressChangedEventArgs e )
7677 {
78+ if ( cancelStatus != 0 )
79+ {
80+ downloader . CancelAsync ( ) ;
81+ return ;
82+ }
83+
7784 double currentProgress = ( double ) e . ReceivedBytesSize / e . TotalBytesToReceive ;
7885 long currentSize = long . Parse ( workingComponent [ "size" ] ) ;
86+ double totalProgress = ( byteProgress + ( currentProgress / 2 * currentSize ) ) / byteTotal ;
7987
8088 Progress . Invoke ( ( MethodInvoker ) delegate
8189 {
82- Progress . Value = ( int ) ( ( double )
83- ( ( byteProgress + ( currentProgress / 2 * currentSize ) ) / byteTotal * Progress . Maximum )
84- ) ;
90+ Progress . Value = ( int ) ( ( double ) totalProgress * Progress . Maximum ) ;
8591 } ) ;
8692
8793 Info . Invoke ( ( MethodInvoker ) delegate
8894 {
8995 Info . Text =
90- $ "Downloading component \" { workingComponent [ "title" ] } \" ... " +
91- $ "{ Math . Truncate ( currentProgress * 100 ) } % ";
96+ $ "[ { ( int ) ( ( double ) totalProgress * 100 ) } %] Downloading component \" { workingComponent [ "title" ] } \" ... " +
97+ $ "{ FPM . GetFormattedBytes ( e . ReceivedBytesSize ) } of { FPM . GetFormattedBytes ( e . TotalBytesToReceive ) } ";
9298 } ) ;
9399 }
94100
95101 private void OnDownloadFileCompleted ( object sender , AsyncCompletedEventArgs e )
96102 {
97- if ( e . Cancelled ) { cancelStatus = 2 ; }
103+ if ( e . Cancelled ) cancelStatus = 2 ;
98104 }
99105
100106 private void ExtractTask ( )
@@ -116,9 +122,9 @@ private void ExtractTask()
116122 writer . WriteLine ( $ "{ workingComponent [ "hash" ] } { workingComponent [ "url" ] } ") ;
117123 }
118124
119- while ( ! reader . Cancelled && reader . MoveToNextEntry ( ) )
125+ while ( cancelStatus == 0 && reader . MoveToNextEntry ( ) )
120126 {
121- if ( reader . Entry . IsDirectory ) { continue ; }
127+ if ( reader . Entry . IsDirectory ) continue ;
122128
123129 reader . WriteEntryToDirectory (
124130 mainForm . FolderTextBox . Text , new ExtractionOptions { ExtractFullPath = true , Overwrite = true }
@@ -133,53 +139,54 @@ private void ExtractTask()
133139
134140 double currentProgress = ( double ) extractedSize / totalSize ;
135141 long currentSize = long . Parse ( workingComponent [ "size" ] ) ;
136-
142+ double totalProgress = ( byteProgress + ( currentSize / 2 ) + ( currentProgress / 2 * currentSize ) ) / byteTotal ;
143+
137144 Progress . Invoke ( ( MethodInvoker ) delegate
138145 {
139- Progress . Value = ( int ) (
140- ( byteProgress + ( currentSize / 2 ) + ( currentProgress / 2 * currentSize ) ) / byteTotal * Progress . Maximum
141- ) ;
146+ Progress . Value = ( int ) ( ( double ) totalProgress * Progress . Maximum ) ;
142147 } ) ;
143148
144149 Info . Invoke ( ( MethodInvoker ) delegate
145150 {
146151 Info . Text =
147- $ "Extracting component \" { workingComponent [ "title" ] } \" ... " +
148- $ "{ Math . Truncate ( currentProgress * 100 ) } % ";
152+ $ "[ { ( int ) ( ( double ) totalProgress * 100 ) } %] Extracting component \" { workingComponent [ "title" ] } \" ... " +
153+ $ "{ FPM . GetFormattedBytes ( extractedSize ) } of { FPM . GetFormattedBytes ( totalSize ) } ";
149154 } ) ;
150155 }
151156
152- if ( reader . Cancelled )
157+ if ( cancelStatus != 0 )
153158 {
154- cancelStatus = 1 ;
155- Directory . Delete ( mainForm . FolderTextBox . Text , true ) ;
159+ reader . Cancel ( ) ;
156160 cancelStatus = 2 ;
157161 }
158162 }
159163 }
160164 }
161165
162- private void FinishInstallation ( )
166+ private async void FinishInstallation ( )
163167 {
164- var shortcutPaths = new List < string > ( ) ;
165-
166- if ( mainForm . ShortcutDesktop . Checked )
168+ await Task . Run ( ( ) =>
167169 {
168- shortcutPaths . Add ( Environment . GetFolderPath ( Environment . SpecialFolder . Desktop ) ) ;
169- }
170- if ( mainForm . ShortcutStartMenu . Checked )
171- {
172- shortcutPaths . Add ( Environment . GetFolderPath ( Environment . SpecialFolder . StartMenu ) ) ;
173- }
170+ var shortcutPaths = new List < string > ( ) ;
174171
175- foreach ( string path in shortcutPaths )
176- {
177- IWshShortcut shortcut = new WshShell ( ) . CreateShortcut ( Path . Combine ( path , "Flashpoint.lnk" ) ) ;
178- shortcut . TargetPath = Path . Combine ( mainForm . FolderTextBox . Text , @"Launcher\Flashpoint.exe" ) ;
179- shortcut . WorkingDirectory = Path . Combine ( mainForm . FolderTextBox . Text , @"Launcher" ) ;
180- shortcut . Description = "Shortcut to Flashpoint" ;
181- shortcut . Save ( ) ;
182- }
172+ if ( mainForm . ShortcutDesktop . Checked )
173+ {
174+ shortcutPaths . Add ( Environment . GetFolderPath ( Environment . SpecialFolder . Desktop ) ) ;
175+ }
176+ if ( mainForm . ShortcutStartMenu . Checked )
177+ {
178+ shortcutPaths . Add ( Environment . GetFolderPath ( Environment . SpecialFolder . StartMenu ) ) ;
179+ }
180+
181+ foreach ( string path in shortcutPaths )
182+ {
183+ IWshShortcut shortcut = new WshShell ( ) . CreateShortcut ( Path . Combine ( path , "Flashpoint.lnk" ) ) ;
184+ shortcut . TargetPath = Path . Combine ( mainForm . FolderTextBox . Text , @"Launcher\Flashpoint.exe" ) ;
185+ shortcut . WorkingDirectory = Path . Combine ( mainForm . FolderTextBox . Text , @"Launcher" ) ;
186+ shortcut . Description = "Shortcut to Flashpoint" ;
187+ shortcut . Save ( ) ;
188+ }
189+ } ) ;
183190
184191 Hide ( ) ;
185192 mainForm . Hide ( ) ;
@@ -191,12 +198,18 @@ private void FinishInstallation()
191198 private async void Cancel_Click ( object sender , EventArgs e )
192199 {
193200 Cancel . Enabled = false ;
201+ cancelStatus = 1 ;
194202
195- if ( downloader . IsBusy ) { downloader . CancelAsync ( ) ; }
196- if ( reader != null ) { reader . Cancel ( ) ; }
197-
198- await Task . Run ( ( ) => { while ( cancelStatus != 2 ) { } } ) ;
203+ await Task . Run ( ( ) =>
204+ {
205+ while ( cancelStatus != 2 ) { }
199206
207+ if ( Directory . Exists ( mainForm . FolderTextBox . Text ) )
208+ {
209+ Directory . Delete ( mainForm . FolderTextBox . Text , true ) ;
210+ }
211+ } ) ;
212+
200213 Close ( ) ;
201214 }
202215 }
0 commit comments