Skip to content

Commit f47fa0a

Browse files
For zip downloaded routes added check for changed or new files when deleting a route. Added this to the Info information also.
1 parent e2dbf1b commit f47fa0a

File tree

3 files changed

+160
-43
lines changed

3 files changed

+160
-43
lines changed

Source/Menu/DownloadContentForm.cs

Lines changed: 87 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ public DownloadContentForm(UserSettings settings)
5959
{
6060
string routeName = Routes.ElementAt(index).Key;
6161
RouteSettings.Route route = Routes.ElementAt(index).Value;
62-
dataGridViewDownloadContent.Rows.Add(new string[] { routeName, route.DateInstalled, route.Url });
62+
dataGridViewDownloadContent.Rows.Add(new string[] {
63+
routeName,
64+
route.Installed ? route.DateInstalled.ToString(CultureInfo.CurrentCulture.DateTimeFormat) : "",
65+
route.Url });
6366
}
6467

6568
dataGridViewDownloadContent.Sort(dataGridViewDownloadContent.Columns[0], ListSortDirection.Ascending);
@@ -151,6 +154,8 @@ private void InstallPathBrowseButton_Click(object sender, EventArgs e)
151154
#region DownloadContentButton
152155
private void DownloadContentButton_Click(object sender, EventArgs e)
153156
{
157+
RouteSettings.Route route = Routes[RouteName];
158+
154159
string installPath = InstallPathTextBox.Text;
155160
if (installPath.EndsWith(@"\"))
156161
{
@@ -181,7 +186,7 @@ private void DownloadContentButton_Click(object sender, EventArgs e)
181186

182187
DriveInfo dInfo = new DriveInfo(installPathRoute);
183188

184-
long size = Routes[RouteName].InstallSize + Routes[RouteName].DownloadSize;
189+
long size = route.InstallSize + route.DownloadSize;
185190

186191
if (size > (dInfo.AvailableFreeSpace * 1.1))
187192
{
@@ -270,16 +275,18 @@ private void DownloadContentButton_Click(object sender, EventArgs e)
270275
return;
271276
}
272277

273-
string dateTimeNowStr = DateTime.Now.ToString(CultureInfo.CurrentCulture.DateTimeFormat);
274-
dataGridViewDownloadContent.CurrentRow.Cells[1].Value = dateTimeNowStr;
278+
route.Installed = true;
279+
280+
DateTime dateTimeNow = DateTime.Now;
281+
dataGridViewDownloadContent.CurrentRow.Cells[1].Value = dateTimeNow.ToString(CultureInfo.CurrentCulture.DateTimeFormat);
282+
route.DateInstalled = dateTimeNow;
275283

276-
Routes[RouteName].DateInstalled = dateTimeNowStr;
277-
Routes[RouteName].DirectoryInstalledIn = installPathRoute;
284+
route.DirectoryInstalledIn = installPathRoute;
278285

279286
Settings.Folders.Save();
280287
Settings.Routes.Save();
281288

282-
if (!string.IsNullOrWhiteSpace(Routes[RouteName].Start.Route))
289+
if (!string.IsNullOrWhiteSpace(route.Start.Route))
283290
{
284291
// start information available
285292
MessageBox.Show(Catalog.GetString("Route installed, press 'Start' button to start Open Rails for this route."),
@@ -306,17 +313,18 @@ private void DownloadContentButton_Click(object sender, EventArgs e)
306313

307314
private bool downloadRoute(string installPathRoute)
308315
{
316+
RouteSettings.Route route = Routes[RouteName];
309317
bool returnValue = false;
310318

311319
Thread downloadThread = new Thread(() =>
312320
{
313-
if (Routes[RouteName].Url.EndsWith(".git"))
321+
if (route.getDownloadType() == RouteSettings.DownloadType.github)
314322
{
315323
returnValue = doTheClone(installPathRoute);
316324
}
317-
if (Routes[RouteName].Url.EndsWith(".zip"))
325+
if (route.getDownloadType() == RouteSettings.DownloadType.zip)
318326
{
319-
returnValue = doTheZipDownload(Routes[RouteName].Url, Path.Combine(installPathRoute, RouteName + ".zip"));
327+
returnValue = doTheZipDownload(route.Url, Path.Combine(installPathRoute, RouteName + ".zip"));
320328
}
321329
});
322330
// start download in thread to be able to show the progress in the main thread
@@ -337,7 +345,7 @@ private bool downloadRoute(string installPathRoute)
337345

338346
if (returnValue)
339347
{
340-
if (Routes[RouteName].Url.EndsWith(".zip"))
348+
if (route.Url.EndsWith(".zip"))
341349
{
342350
Thread installThread = new Thread(() =>
343351
{
@@ -440,10 +448,6 @@ private void sumMB(string path)
440448

441449
private bool insertRowInOptions(string installPathRoute)
442450
{
443-
// check if filesystem is case sensitive
444-
// ok, this check will be wrong if both upper- and lowercase named route directories exist
445-
bool directoryCaseInsensitive = Directory.Exists(installPathRoute.ToUpper()) && Directory.Exists(installPathRoute.ToLower());
446-
447451
// sometimes the route is located one directory level deeper, determine real installPathRoute
448452
string installPathRouteReal;
449453

@@ -490,8 +494,7 @@ private bool insertRowInOptions(string installPathRoute)
490494
}
491495
if (folderSetting.Key == routeName)
492496
{
493-
if (folderSetting.Value.Equals(installPathRouteReal,
494-
directoryCaseInsensitive ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal))
497+
if (folderSetting.Value.Equals(installPathRouteReal, StringComparison.OrdinalIgnoreCase))
495498
{
496499
updated = true;
497500
}
@@ -553,7 +556,7 @@ private void InfoButton_Click(object sender, EventArgs e)
553556
route.Screenshot, route.Screenshot));
554557
}
555558

556-
if (route.Url.EndsWith("git"))
559+
if (route.getDownloadType() == RouteSettings.DownloadType.github)
557560
{
558561
outputFile.WriteLine("<p>" + Catalog.GetString("Downloadable: GitHub format") + "<br>\n");
559562
outputFile.WriteLine(string.Format("- " + Catalog.GetString("From:") + "{0}<br>\n", route.Url));
@@ -563,7 +566,7 @@ private void InfoButton_Click(object sender, EventArgs e)
563566
(route.InstallSize / (1024.0 * 1024 * 1024)).ToString("N")) + "<br></p>\n");
564567
}
565568
}
566-
if (route.Url.EndsWith("zip"))
569+
if (route.getDownloadType() == RouteSettings.DownloadType.zip)
567570
{
568571
outputFile.WriteLine(string.Format("<p>Downloadable: zip format<br>\n"));
569572
outputFile.WriteLine(string.Format("- From: {0}<br>\n", route.Url));
@@ -579,10 +582,10 @@ private void InfoButton_Click(object sender, EventArgs e)
579582
}
580583
}
581584

582-
if (!string.IsNullOrWhiteSpace(route.DateInstalled))
585+
if (route.Installed)
583586
{
584587
outputFile.WriteLine("<p>" + Catalog.GetString("Installed") + ":<br>\n");
585-
outputFile.WriteLine(string.Format("- " + Catalog.GetString("At") + ": {0}<br>\n", route.DateInstalled));
588+
outputFile.WriteLine(string.Format("- " + Catalog.GetString("At") + ": {0}<br>\n", route.DateInstalled.ToString(CultureInfo.CurrentCulture.DateTimeFormat)));
586589
outputFile.WriteLine(string.Format("- " + Catalog.GetString("In") + ": \"{0}\"<br>\n", route.DirectoryInstalledIn));
587590
outputFile.WriteLine(string.Format("- " + Catalog.GetString("Content name") + ": \"{0}\"<br>\n", route.ContentName));
588591
outputFile.WriteLine(string.Format("- " + Catalog.GetString("Content Directory") + ": \"{0}\"<br></p>\n", route.ContentDirectory));
@@ -606,6 +609,38 @@ private void InfoButton_Click(object sender, EventArgs e)
606609
outputFile.WriteLine("- " + Catalog.GetString("Season") + ": " + route.Start.Season + "<br>\n");
607610
outputFile.WriteLine("- " + Catalog.GetString("Weather") + ": " + route.Start.Weather + "<br></p>\n");
608611
}
612+
613+
if (route.getDownloadType() == RouteSettings.DownloadType.zip)
614+
{
615+
List<FileInfo> changedAndAddedFiles = DirectoryAndFiles.getChangedAndAddedFiles(route.DirectoryInstalledIn, route.DateInstalled, true);
616+
outputFile.WriteLine("<p>" + Catalog.GetString("Changed file(s) after the download (timestamp check)") + ":<br>\n");
617+
if (changedAndAddedFiles.Count == 0)
618+
{
619+
outputFile.WriteLine("- " + Catalog.GetString("No changed files found") + "<br></p>\n");
620+
}
621+
else
622+
{
623+
foreach (FileInfo changedFile in changedAndAddedFiles)
624+
{
625+
outputFile.WriteLine(changedFile + "<br>\n");
626+
}
627+
outputFile.WriteLine("</p>");
628+
}
629+
changedAndAddedFiles = DirectoryAndFiles.getChangedAndAddedFiles(route.DirectoryInstalledIn, route.DateInstalled, false);
630+
outputFile.WriteLine("<p>" + Catalog.GetString("Added file(s) after the download (timestamp check)") + ":<br>\n");
631+
if (changedAndAddedFiles.Count == 0)
632+
{
633+
outputFile.WriteLine("- " + Catalog.GetString("No added files found") + "<br></p>\n");
634+
}
635+
else
636+
{
637+
foreach (FileInfo changedFile in changedAndAddedFiles)
638+
{
639+
outputFile.WriteLine(changedFile + "<br>\n");
640+
}
641+
outputFile.WriteLine("</p>");
642+
}
643+
}
609644
}
610645

611646
// show html file in default browser
@@ -750,27 +785,46 @@ public StartNotFound(string message)
750785
#region DeleteButton
751786
void DeleteButton_Click(object sender, EventArgs e)
752787
{
788+
RouteSettings.Route route = Routes[RouteName];
789+
string message = "";
790+
753791
DisableButtons();
754792

755-
string message = Catalog.GetStringFmt("Directory \"{0}\" is to be deleted, are you really sure?", Routes[RouteName].DirectoryInstalledIn);
793+
message = Catalog.GetStringFmt("Directory \"{0}\" is to be deleted, are you really sure?", route.DirectoryInstalledIn);
756794
if (MessageBox.Show(message, Catalog.GetString("Attention"), MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)
757795
{
758796
// cancelled
759797
EnableButtons();
760798
return;
761799
}
800+
801+
if ((DirectoryAndFiles.getChangedAndAddedFiles(route.DirectoryInstalledIn, route.DateInstalled, true).Count > 0) ||
802+
(DirectoryAndFiles.getChangedAndAddedFiles(route.DirectoryInstalledIn, route.DateInstalled, false).Count > 0))
803+
{
804+
805+
message = Catalog.GetStringFmt("Changed or added files found in Directory \"{0}\", see Info for more infomation. Do you want to continue?", route.DirectoryInstalledIn);
806+
if (MessageBox.Show(message, Catalog.GetString("Attention"), MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)
807+
{
808+
// cancelled
809+
EnableButtons();
810+
return;
811+
}
812+
}
813+
762814
Cursor.Current = Cursors.WaitCursor;
763815

764-
DirectoryAndFiles.directoryDelete(Routes[RouteName].DirectoryInstalledIn);
816+
DirectoryAndFiles.directoryDelete(route.DirectoryInstalledIn);
765817

766-
if (Settings.Folders.Folders[Routes[RouteName].ContentName] == Routes[RouteName].ContentDirectory)
818+
if (Settings.Folders.Folders[route.ContentName] == route.ContentDirectory)
767819
{
768-
Settings.Folders.Folders.Remove(Routes[RouteName].ContentName);
820+
Settings.Folders.Folders.Remove(route.ContentName);
769821
}
770822
Settings.Folders.Save();
771823

772-
Routes[RouteName].DirectoryInstalledIn = "";
773-
Routes[RouteName].DateInstalled = "";
824+
route.Installed = false;
825+
route.DateInstalled = DateTime.MinValue;
826+
route.DirectoryInstalledIn = "";
827+
774828
Settings.Routes.Save();
775829

776830
dataGridViewDownloadContent.CurrentRow.Cells[1].Value = "";
@@ -790,13 +844,15 @@ private void DisableButtons()
790844

791845
private void EnableButtons()
792846
{
793-
DownloadContentButton.Enabled = string.IsNullOrWhiteSpace(Routes[RouteName].DateInstalled);
847+
RouteSettings.Route route = Routes[RouteName];
848+
849+
DownloadContentButton.Enabled = !route.Installed;
794850

795851
startButton.Enabled =
796-
(!string.IsNullOrWhiteSpace(Routes[RouteName].DateInstalled)) &&
797-
(!string.IsNullOrWhiteSpace(Routes[RouteName].Start.Route));
852+
route.Installed &&
853+
!string.IsNullOrWhiteSpace(route.Start.Route);
798854

799-
deleteButton.Enabled = !string.IsNullOrWhiteSpace(Routes[RouteName].DateInstalled);
855+
deleteButton.Enabled = route.Installed;
800856
}
801857

802858
private void DownloadContentForm_FormClosing(object sender, FormClosingEventArgs e)

Source/ORTS.Common/DirectoryAndFiles.cs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@
1717
//
1818

1919
using System;
20+
using System.Collections.Generic;
2021
using System.IO;
21-
using Microsoft.SqlServer.Server;
2222

2323
namespace ORTS.Common
2424
{
2525
public class DirectoryAndFiles
2626
{
27-
2827
public static void directoryDelete(string directoryName)
2928
{
3029
if (Directory.Exists(directoryName))
@@ -69,5 +68,45 @@ public static string determineTopDirectory(string directoryName)
6968

7069
return topDirectoryName;
7170
}
71+
72+
public static List<FileInfo> getChangedAndAddedFiles(string directoryName, DateTime dateInstalled, bool checkForChanged)
73+
{
74+
List<FileInfo> changedFiles = new List<FileInfo>();
75+
76+
if (Directory.Exists(directoryName))
77+
{
78+
getChangedAndAddedFilesDeeper(directoryName, dateInstalled, changedFiles, checkForChanged);
79+
}
80+
81+
return changedFiles;
82+
}
83+
84+
public static void getChangedAndAddedFilesDeeper(string directoryName, DateTime dateInstalled, List<FileInfo> changedFiles, bool checkForChanged)
85+
{
86+
foreach (string filename in Directory.GetFiles(directoryName))
87+
{
88+
FileInfo fi = new FileInfo(filename);
89+
if (checkForChanged)
90+
{
91+
if (fi.LastWriteTime > dateInstalled)
92+
{
93+
changedFiles.Add(fi);
94+
}
95+
}
96+
else
97+
{
98+
// check for new files, creation date after date installed
99+
if (fi.CreationTime > dateInstalled)
100+
{
101+
changedFiles.Add(fi);
102+
}
103+
}
104+
105+
}
106+
foreach (string subDirectoryName in Directory.GetDirectories(directoryName))
107+
{
108+
getChangedAndAddedFilesDeeper(subDirectoryName, dateInstalled, changedFiles, checkForChanged);
109+
}
110+
}
72111
}
73112
}

0 commit comments

Comments
 (0)