|
23 | 23 | using GNU.Gettext; |
24 | 24 | using LibGit2Sharp; |
25 | 25 | using ORTS.Settings; |
26 | | -using Newtonsoft.Json; |
27 | 26 | using System.Linq; |
28 | | -using System.Security.Policy; |
| 27 | +using System.Diagnostics; |
| 28 | +using System.Threading; |
29 | 29 |
|
30 | 30 | namespace ORTS |
31 | 31 | { |
@@ -79,7 +79,7 @@ void dataGridViewDownloadContent_SelectionChanged(object sender, EventArgs e) |
79 | 79 |
|
80 | 80 | private void InstallPathButton_Click(object sender, EventArgs e) |
81 | 81 | { |
82 | | - using (var folderBrowser = new FolderBrowserDialog()) |
| 82 | + using (FolderBrowserDialog folderBrowser = new FolderBrowserDialog()) |
83 | 83 | { |
84 | 84 | folderBrowser.SelectedPath = InstallPathTextBox.Text; |
85 | 85 | folderBrowser.Description = "Main Path where route is to be installed"; |
@@ -154,19 +154,13 @@ private void DownloadContentButton_Click(object sender, EventArgs e) |
154 | 154 | Cursor.Current = Cursors.WaitCursor; |
155 | 155 |
|
156 | 156 | dataGridViewDownloadContent.CurrentRow.Cells[1].Value = Catalog.GetString("Installing..."); |
157 | | - this.Refresh(); |
| 157 | + Refresh(); |
158 | 158 |
|
159 | | - try |
160 | | - { |
161 | | - Repository.Clone(Routes[RouteName].Url, installPathRoute); |
162 | | - } |
163 | | - catch (LibGit2SharpException libGit2SharpException) |
| 159 | + // actual download |
| 160 | + |
| 161 | + if (!downloadRoute(installPathRoute)) |
164 | 162 | { |
165 | | - { |
166 | | - message = Catalog.GetStringFmt("Error during download: {0}", libGit2SharpException.Message); |
167 | | - MessageBox.Show(message, Catalog.GetString("Attention"), MessageBoxButtons.OK, MessageBoxIcon.Error); |
168 | | - return; |
169 | | - } |
| 163 | + return; |
170 | 164 | } |
171 | 165 |
|
172 | 166 | // insert row in Options, tab Content |
@@ -228,5 +222,67 @@ private void DownloadContentButton_Click(object sender, EventArgs e) |
228 | 222 |
|
229 | 223 | Close(); |
230 | 224 | } |
| 225 | + |
| 226 | + private bool downloadRoute(string installPathRoute) |
| 227 | + { |
| 228 | + bool returnValue = false; |
| 229 | + |
| 230 | + Thread cloneThread = new Thread(() => |
| 231 | + { |
| 232 | + returnValue = doTheClone(installPathRoute); |
| 233 | + }); |
| 234 | + cloneThread.Start(); |
| 235 | + |
| 236 | + while (cloneThread.IsAlive) |
| 237 | + { |
| 238 | + Stopwatch sw = Stopwatch.StartNew(); |
| 239 | + |
| 240 | + TotalBytes = 0; |
| 241 | + sumMB(installPathRoute); |
| 242 | + dataGridViewDownloadContent.CurrentRow.Cells[1].Value = |
| 243 | + string.Format("downloaded: {0} kB", Math.Round((double)(TotalBytes / 1024))); |
| 244 | + Refresh(); |
| 245 | + |
| 246 | + while ((cloneThread.IsAlive) && (sw.ElapsedMilliseconds <= 3000)) { } |
| 247 | + } |
| 248 | + |
| 249 | + dataGridViewDownloadContent.CurrentRow.Cells[1].Value = ""; |
| 250 | + |
| 251 | + return returnValue; |
| 252 | + } |
| 253 | + |
| 254 | + private bool doTheClone(string installPathRoute) |
| 255 | + { |
| 256 | + try |
| 257 | + { |
| 258 | + Repository.Clone(Routes[RouteName].Url, installPathRoute); |
| 259 | + } |
| 260 | + catch (LibGit2SharpException libGit2SharpException) |
| 261 | + { |
| 262 | + { |
| 263 | + string message = Catalog.GetStringFmt("Error during download: {0}", libGit2SharpException.Message); |
| 264 | + MessageBox.Show(message, Catalog.GetString("Attention"), MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 265 | + return false; |
| 266 | + } |
| 267 | + } |
| 268 | + |
| 269 | + return true; |
| 270 | + } |
| 271 | + |
| 272 | + long TotalBytes = 0; |
| 273 | + |
| 274 | + private void sumMB(string path) |
| 275 | + { |
| 276 | + foreach (string fileName in Directory.GetFiles(path)) |
| 277 | + { |
| 278 | + TotalBytes += new System.IO.FileInfo(fileName).Length; |
| 279 | + } |
| 280 | + |
| 281 | + foreach (string directoryName in Directory.GetDirectories(path)) |
| 282 | + { |
| 283 | + sumMB(directoryName); |
| 284 | + } |
| 285 | + } |
231 | 286 | } |
| 287 | + |
232 | 288 | } |
0 commit comments