2727using System . Diagnostics ;
2828using System . Threading ;
2929using System . ComponentModel ;
30+ using System . Net ;
31+ using System . IO . Compression ;
3032
3133namespace ORTS
3234{
@@ -86,10 +88,10 @@ private void DownloadContentButton_Click(object sender, EventArgs e)
8688 string installPathRoute = Path . Combine ( installPath , RouteName ) ;
8789 string message ;
8890
89- message = Catalog . GetStringFmt ( "Route to be installed in \" {0}\" , are you sure?" , installPathRoute ) ;
90-
9191 // various checks for the directory where the route is installed
9292
93+ message = Catalog . GetStringFmt ( "Route to be installed in \" {0}\" , are you sure?" , installPathRoute ) ;
94+
9395 if ( MessageBox . Show ( message , Catalog . GetString ( "Attention" ) , MessageBoxButtons . OKCancel , MessageBoxIcon . Warning ) != DialogResult . OK )
9496 {
9597 // cancelled
@@ -170,23 +172,57 @@ private bool downloadRoute(string installPathRoute)
170172 {
171173 bool returnValue = false ;
172174
173- Thread cloneThread = new Thread ( ( ) =>
175+ Thread downloadThread = new Thread ( ( ) =>
174176 {
175- returnValue = doTheClone ( installPathRoute ) ;
177+ if ( Routes [ RouteName ] . Url . EndsWith ( ".git" ) )
178+ {
179+ returnValue = doTheClone ( installPathRoute ) ;
180+ }
181+ if ( Routes [ RouteName ] . Url . EndsWith ( ".zip" ) )
182+ {
183+ returnValue = doTheZipDownload ( Routes [ RouteName ] . Url , Path . Combine ( installPathRoute , RouteName + ".zip" ) ) ;
184+ }
176185 } ) ;
177- cloneThread . Start ( ) ;
186+ downloadThread . Start ( ) ;
178187
179- while ( cloneThread . IsAlive )
188+ while ( downloadThread . IsAlive )
180189 {
181190 Stopwatch sw = Stopwatch . StartNew ( ) ;
182191
183192 TotalBytes = 0 ;
184193 sumMB ( installPathRoute ) ;
185194 dataGridViewDownloadContent . CurrentRow . Cells [ 1 ] . Value =
186- string . Format ( "downloaded: {0} kB" , Math . Round ( ( double ) ( TotalBytes / 1024 ) ) ) ;
195+ string . Format ( "downloaded: {0} kB" , ( TotalBytes / 1024 ) . ToString ( "N0" ) ) ;
187196 Refresh ( ) ;
188197
189- while ( ( cloneThread . IsAlive ) & & ( sw . ElapsedMilliseconds <= 3000 ) ) { }
198+ while ( ( downloadThread . IsAlive ) & & ( sw . ElapsedMilliseconds <= 3000 ) ) { }
199+ }
200+
201+ if ( returnValue )
202+ {
203+ if ( Routes [ RouteName ] . Url . EndsWith ( ".zip" ) )
204+ {
205+ Thread installThread = new Thread ( ( ) =>
206+ {
207+ returnValue = doTheUnzipInstall ( Path . Combine ( installPathRoute , RouteName + ".zip" ) , installPathRoute ) ;
208+ } ) ;
209+ installThread . Start ( ) ;
210+
211+ long bytesZipfile = TotalBytes ;
212+
213+ while ( installThread . IsAlive )
214+ {
215+ Stopwatch sw = Stopwatch . StartNew ( ) ;
216+
217+ TotalBytes = - bytesZipfile ;
218+ sumMB ( installPathRoute ) ;
219+ dataGridViewDownloadContent . CurrentRow . Cells [ 1 ] . Value =
220+ string . Format ( "Installed: {0} kB" , ( TotalBytes / 1024 ) . ToString ( "N0" ) ) ;
221+ Refresh ( ) ;
222+
223+ while ( ( installThread . IsAlive ) & & ( sw . ElapsedMilliseconds <= 3000 ) ) { }
224+ }
225+ }
190226 }
191227
192228 dataGridViewDownloadContent . CurrentRow . Cells [ 1 ] . Value = "" ;
@@ -203,7 +239,45 @@ private bool doTheClone(string installPathRoute)
203239 catch ( LibGit2SharpException libGit2SharpException )
204240 {
205241 {
206- string message = Catalog . GetStringFmt ( "Error during download: {0}" , libGit2SharpException . Message ) ;
242+ string message = Catalog . GetStringFmt ( "Error during github download: {0}" , libGit2SharpException . Message ) ;
243+ MessageBox . Show ( message , Catalog . GetString ( "Attention" ) , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
244+ return false ;
245+ }
246+ }
247+
248+ return true ;
249+ }
250+
251+ private bool doTheZipDownload ( string url , string installPathRouteZipfileName )
252+ {
253+ try
254+ {
255+ WebClient myWebClient = new WebClient ( ) ;
256+ myWebClient . DownloadFile ( url , installPathRouteZipfileName ) ;
257+ }
258+ catch ( Exception error )
259+ {
260+ {
261+ string message = Catalog . GetStringFmt ( "Error during download zipfile {0}: {1}" , url , error . Message ) ;
262+ MessageBox . Show ( message , Catalog . GetString ( "Attention" ) , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
263+ return false ;
264+ }
265+ }
266+
267+ return true ;
268+ }
269+
270+ private bool doTheUnzipInstall ( string installPathRouteZipfileName , string installPathRoute )
271+ {
272+ try
273+ {
274+ ZipFile . ExtractToDirectory ( installPathRouteZipfileName , installPathRoute ) ;
275+ File . Delete ( installPathRouteZipfileName ) ;
276+ }
277+ catch ( Exception error )
278+ {
279+ {
280+ string message = Catalog . GetStringFmt ( "Error during unzip zipfile {0}: {1}" , installPathRouteZipfileName , error . Message ) ;
207281 MessageBox . Show ( message , Catalog . GetString ( "Attention" ) , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
208282 return false ;
209283 }
0 commit comments