Skip to content

Commit 3593371

Browse files
Support for downloading and unzip zip files
1 parent d5b3639 commit 3593371

File tree

2 files changed

+95
-19
lines changed

2 files changed

+95
-19
lines changed

Source/Menu/DownloadContentForm.cs

Lines changed: 83 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
using System.Diagnostics;
2828
using System.Threading;
2929
using System.ComponentModel;
30+
using System.Net;
31+
using System.IO.Compression;
3032

3133
namespace 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
}

Source/ORTS.Settings/RouteSettings.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@
1818

1919
using System;
2020
using System.Collections.Generic;
21-
using System.Diagnostics;
2221
using System.IO;
2322
using System.Linq;
2423
using Newtonsoft.Json;
2524
using Newtonsoft.Json.Linq;
2625
using LibGit2Sharp;
27-
using static ORTS.Settings.RouteSettings;
28-
using System.Windows.Forms;
2926

3027
namespace ORTS.Settings
3128
{
@@ -79,17 +76,18 @@ public void LoadContentAndInstalled()
7976
}
8077

8178
string definedContentJsonName = @"d:\content\routes.json";
82-
string definedContentJsonDirectoryName = Path.Combine(UserSettings.UserDataFolder, "ContentJson");
83-
string githubUrl = "https://github.com/openrails/content.git";
79+
80+
string definedContentJsonDirectoryName = Path.GetTempFileName();
81+
File.Delete(definedContentJsonDirectoryName);
82+
83+
string githubUrl = "https://github.com/openrails/content.git";;
8484

8585
if (Environment.GetEnvironmentVariable("TstLoadContentAndInstalled") == null)
8686
{
8787
try
8888
{
8989
// normal non test behaviour, retrieve json file from github
9090

91-
directoryDelete(definedContentJsonDirectoryName);
92-
9391
Repository.Clone(githubUrl, definedContentJsonDirectoryName);
9492

9593
definedContentJsonName = Path.Combine(definedContentJsonDirectoryName, "routes.json");
@@ -109,12 +107,14 @@ public void LoadContentAndInstalled()
109107
IList<JToken> results = JsonConvert.DeserializeObject<JToken>(json) as IList<JToken>;
110108
foreach (JToken result in results)
111109
{
112-
if (result["url"].ToString().EndsWith(".git"))
110+
string routeName = result["name"].ToString();
111+
string url = result["url"].ToString();
112+
113+
if (url.EndsWith(".git") || url.EndsWith(".zip"))
113114
{
114-
string routeName = result["name"].ToString();
115115
if (!Routes.ContainsKey(routeName))
116116
{
117-
Routes.Add(routeName, new RouteSettings.Route("", result["url"].ToString()));
117+
Routes.Add(routeName, new RouteSettings.Route("", url));
118118
}
119119
}
120120
}
@@ -134,6 +134,7 @@ private void directoryDelete(string directoryName)
134134
{
135135
if (Directory.Exists(directoryName))
136136
{
137+
// remove the read only flags, otherwise the Directory.delete does not work
137138
directoryRemoveReadOnlyFlags(directoryName);
138139
Directory.Delete(directoryName, true);
139140
}
@@ -158,6 +159,7 @@ public void Save()
158159

159160
for (int index = 0; index < Routes.Count; index++)
160161
{
162+
// only save the installed routes
161163
if (!string.IsNullOrWhiteSpace(Routes.ElementAt(index).Value.DateInstalled))
162164
{
163165
routes.Add(Routes.ElementAt(index));

0 commit comments

Comments
 (0)