11using Downloader ;
2- using SharpCompress . Archives . SevenZip ;
2+ using IWshRuntimeLibrary ;
3+ using SharpCompress . Archives . Zip ;
34using SharpCompress . Common ;
45using SharpCompress . Readers ;
6+ using File = System . IO . File ;
57
68namespace FlashpointInstaller
79{
810 public partial class Install : Form
911 {
12+ Main mainForm = ( Main ) Application . OpenForms [ "Main" ] ;
13+
1014 DownloadService downloader = new ( new DownloadConfiguration ( ) ) ;
1115
1216 Stream stream ;
13- SevenZipArchive archive ;
17+ ZipArchive archive ;
1418 IReader reader ;
1519
16- string lastEntry = "" ;
17- long extractedSize = 0 ;
20+ bool finishedWriting = false ;
1821
19- public Install ( )
20- {
21- InitializeComponent ( ) ;
22- }
22+ public Install ( ) => InitializeComponent ( ) ;
2323
2424 private async void Install_Load ( object sender , EventArgs e )
2525 {
2626 downloader . DownloadStarted += OnDownloadStarted ;
2727 downloader . DownloadProgressChanged += OnDownloadProgressChanged ;
2828
2929 stream = await downloader . DownloadFileTaskAsync ( "http://localhost/Flashpoint%2011%20Infinity.7z" ) ;
30-
31- await Task . Run ( ( ) =>
30+ //stream = File.OpenRead(@"E:\Flashpoint 11 Infinity.zip");
31+
32+ if ( ! downloader . IsCancelled )
3233 {
33- using ( archive = SevenZipArchive . Open ( stream ) )
34- {
35- using ( reader = archive . ExtractAllEntries ( ) )
36- {
37- reader . EntryExtractionProgress += OnEntryExtractionProgressChanged ;
38- reader . WriteAllToDirectory ( ( ( Main ) Application . OpenForms [ "Main" ] ) . FolderText . Text , new ExtractionOptions { ExtractFullPath = true , Overwrite = true } ) ;
39- }
40- }
41- } ) ;
34+ await Task . Run ( ExtractTask ) ;
35+ }
4236 }
4337
4438 private void OnDownloadStarted ( object ? sender , DownloadStartedEventArgs e )
4539 {
4640 Info . Invoke ( ( MethodInvoker ) delegate
4741 {
48- Info . Text = $ "Downloaded 0MB of { Math . Round ( ( double ) e . TotalBytesToReceive / 1000000 ) } MB";
42+ Info . Text = $ "Downloaded 0MB of { e . TotalBytesToReceive / 1000000 } MB";
4943 } ) ;
5044 }
5145
@@ -62,38 +56,85 @@ private void OnDownloadProgressChanged(object? sender, DownloadProgressChangedEv
6256 } ) ;
6357 }
6458
65- private void OnEntryExtractionProgressChanged ( object ? sender , ReaderExtractionEventArgs < IEntry > e )
59+ private void ExtractTask ( )
6660 {
67- if ( e . Item . Key != lastEntry )
61+ using ( archive = ZipArchive . Open ( stream ) )
6862 {
69- extractedSize += e . Item . Size ;
70- lastEntry = e . Item . Key ;
71- }
63+ using ( reader = archive . ExtractAllEntries ( ) )
64+ {
65+ int extractedFiles = 0 ;
66+ int totalFiles = archive . Entries . Where ( entry => ! entry . IsDirectory ) . Count ( ) ;
67+
68+ while ( ! reader . Cancelled && reader . MoveToNextEntry ( ) )
69+ {
70+ if ( reader . Entry . IsDirectory )
71+ {
72+ continue ;
73+ }
7274
73- long extractedSizeExact = extractedSize - ( e . Item . Size - e . ReaderProgress . BytesTransferred ) ;
75+ reader . WriteEntryToDirectory ( mainForm . FolderText . Text , new ExtractionOptions { ExtractFullPath = true , Overwrite = true } ) ;
7476
75- Progress . Invoke ( ( MethodInvoker ) delegate
76- {
77- Progress . Value = ( Progress . Maximum / 2 ) + ( ( int ) ( ( double ) extractedSizeExact / archive . TotalUncompressSize * ( Progress . Maximum / 2 ) ) ) ;
78- } ) ;
79-
80- Info . Invoke ( ( MethodInvoker ) delegate
77+ extractedFiles ++ ;
78+
79+ try
80+ {
81+ Progress . Invoke ( ( MethodInvoker ) delegate
82+ {
83+ Progress . Value = ( Progress . Maximum / 2 ) + ( ( int ) ( ( double ) extractedFiles / totalFiles * ( Progress . Maximum / 2 ) ) ) ;
84+ } ) ;
85+
86+ Info . Invoke ( ( MethodInvoker ) delegate
87+ {
88+ Info . Text = $ "Extracted { extractedFiles } of { totalFiles } files";
89+ } ) ;
90+ }
91+ catch { }
92+ }
93+
94+ if ( reader . Cancelled )
95+ {
96+ finishedWriting = true ;
97+ }
98+ else
99+ {
100+ FinishInstallation ( ) ;
101+ }
102+ }
103+ }
104+ }
105+
106+ private void FinishInstallation ( )
107+ {
108+ if ( mainForm . ShortcutsDesktop . Checked )
81109 {
82- Info . Text = $ "Extracted { extractedSize / 1000000 } MB of { archive . TotalUncompressSize / 1000000 } MB";
83- } ) ;
110+ string desktopPath = Environment . GetFolderPath ( Environment . SpecialFolder . Desktop ) ;
111+
112+ IWshShortcut shortcut = new WshShell ( ) . CreateShortcut ( Path . Combine ( desktopPath , "Flashpoint Infinity.lnk" ) ) ;
113+ shortcut . TargetPath = Path . Combine ( mainForm . FolderText . Text , @"Flashpoint 11 Infinity\Launcher\Flashpoint.exe" ) ;
114+ shortcut . Save ( ) ;
115+ }
116+
117+ Finish FinishWindow = new ( ) ;
118+ FinishWindow . ShowDialog ( ) ;
84119 }
85120
86- private void Cancel_Click ( object sender , EventArgs e )
121+ private async void Cancel_Click ( object sender , EventArgs e )
87122 {
123+ Cancel . Enabled = false ;
124+ Info . Text = "Cancelling installation..." ;
125+
88126 if ( downloader . IsBusy )
89127 {
90128 downloader . CancelAsync ( ) ;
91129 }
92130 else if ( reader != null )
93131 {
94132 reader . Cancel ( ) ;
95- archive . Dispose ( ) ;
96- stream . Dispose ( ) ;
133+
134+ await Task . Run ( ( ) =>
135+ {
136+ while ( ! finishedWriting ) { }
137+ } ) ;
97138 }
98139
99140 Close ( ) ;
0 commit comments