11using System ;
2+ using System . Collections . Generic ;
23using System . IO ;
34using System . Threading . Tasks ;
45using System . Windows . Forms ;
910using SharpCompress . Common ;
1011using SharpCompress . Readers ;
1112
13+ using File = System . IO . File ;
14+
1215namespace FlashpointInstaller
1316{
1417 public partial class Install : Form
1518 {
1619 Main mainForm = ( Main ) Application . OpenForms [ "Main" ] ;
1720
21+ List < Dictionary < string , string > > componentInfo = new List < Dictionary < string , string > > ( ) ;
22+ Dictionary < string , string > workingComponent ;
23+
1824 DownloadService downloader = new DownloadService ( new DownloadConfiguration ( ) ) ;
1925
2026 Stream stream ;
2127 ZipArchive archive ;
2228 IReader reader ;
2329
30+ long byteProgress = 0 ;
31+ long byteTotal = ( ( Main ) Application . OpenForms [ "Main" ] ) . DownloadSize ;
32+
2433 bool doneCancelling = false ;
2534
2635 public Install ( ) => InitializeComponent ( ) ;
@@ -29,28 +38,59 @@ private async void Install_Load(object sender, EventArgs e)
2938 {
3039 downloader . DownloadProgressChanged += OnDownloadProgressChanged ;
3140
32- //stream = await downloader.DownloadFileTaskAsync("https://bluepload.unstable.life/selif/flashpointdummy.zip");
33- stream = await downloader . DownloadFileTaskAsync ( "http://localhost/flashpointdummy.zip" ) ;
34- //stream = System.IO.File.OpenRead(@"E:\flashpointdummy.zip");
41+ void Iterate ( TreeNodeCollection parent )
42+ {
43+ foreach ( TreeNode childNode in parent )
44+ {
45+ var attributes = childNode . Tag as Dictionary < string , string > ;
46+
47+ if ( attributes [ "type" ] == "component" && childNode . Checked )
48+ {
49+ componentInfo . Add ( childNode . Tag as Dictionary < string , string > ) ;
50+ }
3551
36- if ( ! downloader . IsCancelled )
52+ Iterate ( childNode . Nodes ) ;
53+ }
54+ }
55+ Iterate ( mainForm . ComponentQueue . Nodes ) ;
56+
57+ foreach ( var component in componentInfo )
3758 {
38- Directory . CreateDirectory ( mainForm . FolderTextBox . Text ) ;
59+ workingComponent = component ;
60+
61+ stream = await downloader . DownloadFileTaskAsync ( component [ "url" ] ) ;
3962
40- await Task . Run ( ExtractTask ) ;
63+ if ( ! downloader . IsCancelled )
64+ {
65+ Directory . CreateDirectory ( mainForm . FolderTextBox . Text ) ;
66+
67+ await Task . Run ( ExtractTask ) ;
68+ }
69+ else { return ; }
70+
71+ byteProgress += int . Parse ( component [ "size" ] ) ;
4172 }
73+
74+ FinishInstallation ( ) ;
4275 }
4376
4477 private void OnDownloadProgressChanged ( object sender , DownloadProgressChangedEventArgs e )
4578 {
79+ double currentProgress = ( double ) e . ReceivedBytesSize / e . TotalBytesToReceive ;
80+ long currentSize = long . Parse ( workingComponent [ "size" ] ) ;
81+
4682 Progress . Invoke ( ( MethodInvoker ) delegate
4783 {
48- Progress . Value = ( int ) ( ( double ) e . ReceivedBytesSize / e . TotalBytesToReceive * ( Progress . Maximum / 2 ) ) ;
84+ Progress . Value = ( int ) ( ( double )
85+ ( ( byteProgress + ( currentProgress / 2 * currentSize ) ) / byteTotal * Progress . Maximum )
86+ ) ;
4987 } ) ;
50-
88+
5189 Info . Invoke ( ( MethodInvoker ) delegate
5290 {
53- Info . Text = $ "1/2: Downloading archive - { e . ReceivedBytesSize / 1000000 } MB of { e . TotalBytesToReceive / 1000000 } MB";
91+ Info . Text =
92+ $ "Downloading component \" { workingComponent [ "title" ] } \" ... " +
93+ $ "{ Math . Truncate ( currentProgress * 100 ) } %";
5494 } ) ;
5595 }
5696
@@ -63,29 +103,48 @@ private void ExtractTask()
63103 long extractedSize = 0 ;
64104 long totalSize = archive . TotalUncompressSize ;
65105
106+ string infoPath = Path . Combine ( mainForm . FolderTextBox . Text , "Components" , workingComponent [ "path" ] ) ;
107+ string infoFile = $ "{ workingComponent [ "title" ] } .txt";
108+
109+ Directory . CreateDirectory ( infoPath ) ;
110+
111+ using ( TextWriter writer = File . CreateText ( infoPath + infoFile ) )
112+ {
113+ writer . WriteLine ( $ "{ workingComponent [ "url" ] } { workingComponent [ "hash" ] } ") ;
114+ }
115+
66116 while ( ! reader . Cancelled && reader . MoveToNextEntry ( ) )
67117 {
68- if ( reader . Entry . IsDirectory )
69- {
70- continue ;
71- }
118+ if ( reader . Entry . IsDirectory ) { continue ; }
72119
73120 reader . WriteEntryToDirectory (
74121 mainForm . FolderTextBox . Text , new ExtractionOptions { ExtractFullPath = true , Overwrite = true }
75122 ) ;
76123
124+ using ( TextWriter writer = File . AppendText ( infoPath + infoFile ) )
125+ {
126+ writer . WriteLine ( reader . Entry . Key ) ;
127+ }
128+
77129 extractedSize += reader . Entry . Size ;
78130
79131 try
80132 {
133+ double currentProgress = ( double ) extractedSize / totalSize ;
134+ long currentSize = long . Parse ( workingComponent [ "size" ] ) ;
135+
81136 Progress . Invoke ( ( MethodInvoker ) delegate
82137 {
83- Progress . Value = ( Progress . Maximum / 2 ) + ( int ) ( ( double ) extractedSize / totalSize * ( Progress . Maximum / 2 ) ) ;
138+ Progress . Value = ( int ) (
139+ ( byteProgress + ( currentSize / 2 ) + ( currentProgress / 2 * currentSize ) ) / byteTotal * Progress . Maximum
140+ ) ;
84141 } ) ;
85142
86143 Info . Invoke ( ( MethodInvoker ) delegate
87144 {
88- Info . Text = $ "2/2: Extracting files - { extractedSize / 1000000 } MB of { totalSize / 1000000 } MB";
145+ Info . Text =
146+ $ "Extracting component \" { workingComponent [ "title" ] } \" ... " +
147+ $ "{ Math . Truncate ( currentProgress * 100 ) } %";
89148 } ) ;
90149 }
91150 catch { }
@@ -97,22 +156,26 @@ private void ExtractTask()
97156
98157 doneCancelling = true ;
99158 }
100- else
101- {
102- FinishInstallation ( ) ;
103- }
104159 }
105160 }
106161 }
107162
108163 private void FinishInstallation ( )
109164 {
110- if ( mainForm . Shortcut . Checked )
165+ var shortcutPaths = new List < string > ( ) ;
166+
167+ if ( mainForm . ShortcutDesktop . Checked )
168+ {
169+ shortcutPaths . Add ( Environment . GetFolderPath ( Environment . SpecialFolder . Desktop ) ) ;
170+ }
171+ if ( mainForm . ShortcutStartMenu . Checked )
172+ {
173+ shortcutPaths . Add ( Environment . GetFolderPath ( Environment . SpecialFolder . StartMenu ) ) ;
174+ }
175+
176+ foreach ( string path in shortcutPaths )
111177 {
112- IWshShortcut shortcut = new WshShell ( ) . CreateShortcut ( Path . Combine (
113- Environment . GetFolderPath ( Environment . SpecialFolder . Desktop ) ,
114- "Flashpoint.lnk"
115- ) ) ;
178+ IWshShortcut shortcut = new WshShell ( ) . CreateShortcut ( Path . Combine ( path , "Flashpoint.lnk" ) ) ;
116179 shortcut . TargetPath = Path . Combine ( mainForm . FolderTextBox . Text , @"Launcher\Flashpoint.exe" ) ;
117180 shortcut . WorkingDirectory = Path . Combine ( mainForm . FolderTextBox . Text , @"Launcher" ) ;
118181 shortcut . Description = "Shortcut to Flashpoint" ;
0 commit comments