Skip to content

Commit e2dbf1b

Browse files
Added check for install path. Disallows installing content into into the directory where the OR exe's reside. Might be a bit too strict, but placing routes in "C:\Program Files (x86)" might not be a good idea anyway.
1 parent 02f616f commit e2dbf1b

File tree

5 files changed

+113
-64
lines changed

5 files changed

+113
-64
lines changed

Source/Menu/DownloadContentForm.Designer.cs

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/Menu/DownloadContentForm.cs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
using System.Net;
3131
using System.IO.Compression;
3232
using System.Drawing;
33+
using ORTS.Common;
3334

3435
namespace ORTS
3536
{
@@ -131,8 +132,8 @@ private void dataGridViewDownloadContent_SelectionChanged(object sender, EventAr
131132
}
132133
#endregion
133134

134-
#region InstallPathButton
135-
private void InstallPathButton_Click(object sender, EventArgs e)
135+
#region InstallPathBrowseButton
136+
private void InstallPathBrowseButton_Click(object sender, EventArgs e)
136137
{
137138
using (FolderBrowserDialog folderBrowser = new FolderBrowserDialog())
138139
{
@@ -151,13 +152,33 @@ private void InstallPathButton_Click(object sender, EventArgs e)
151152
private void DownloadContentButton_Click(object sender, EventArgs e)
152153
{
153154
string installPath = InstallPathTextBox.Text;
155+
if (installPath.EndsWith(@"\"))
156+
{
157+
installPath = installPath.Remove(installPath.Length - 1, 1);
158+
}
159+
installPath = Path.GetFullPath(installPath);
160+
154161
string installPathRoute = Path.Combine(installPath, RouteName);
155162
string message;
156163

157164
DisableButtons();
158165

159166
// various checks for the directory where the route is installed
160167

168+
string pathDirectoryExe = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
169+
170+
string topPathDirectoryExe = DirectoryAndFiles.determineTopDirectory(pathDirectoryExe.Substring(6));
171+
string topInstallPath = DirectoryAndFiles.determineTopDirectory(installPath);
172+
173+
if (topPathDirectoryExe.Equals(topInstallPath, StringComparison.OrdinalIgnoreCase))
174+
{
175+
message = Catalog.GetStringFmt("Top directory {0} is the same for exe and route, installing in this directory not allowed", topInstallPath);
176+
MessageBox.Show(message, Catalog.GetString("Attention"), MessageBoxButtons.OK, MessageBoxIcon.Error);
177+
// cancelled
178+
EnableButtons();
179+
return;
180+
}
181+
161182
DriveInfo dInfo = new DriveInfo(installPathRoute);
162183

163184
long size = Routes[RouteName].InstallSize + Routes[RouteName].DownloadSize;
@@ -600,14 +621,15 @@ void StartButton_Click(object sender, EventArgs e)
600621
Cursor.Current = Cursors.WaitCursor;
601622

602623
RouteSettings.Route route = Routes[RouteName];
624+
string contentName = route.ContentName;
603625
MainForm mainForm = ((MainForm)Owner);
604626

605627
mainForm.DoWithTask = false;
606628

607629
try
608630
{
609631
mainForm.LoadFolderList();
610-
mainForm.comboBoxFolder.SelectedIndex = determineSelectedIndex(mainForm.comboBoxFolder, RouteName);
632+
mainForm.comboBoxFolder.SelectedIndex = determineSelectedIndex(mainForm.comboBoxFolder, contentName);
611633

612634
mainForm.LoadRouteList();
613635
mainForm.comboBoxRoute.SelectedIndex = determineSelectedIndex(mainForm.comboBoxRoute, route.Start.Route);
@@ -739,7 +761,7 @@ void DeleteButton_Click(object sender, EventArgs e)
739761
}
740762
Cursor.Current = Cursors.WaitCursor;
741763

742-
directoryDelete(Routes[RouteName].DirectoryInstalledIn);
764+
DirectoryAndFiles.directoryDelete(Routes[RouteName].DirectoryInstalledIn);
743765

744766
if (Settings.Folders.Folders[Routes[RouteName].ContentName] == Routes[RouteName].ContentDirectory)
745767
{
@@ -757,28 +779,6 @@ void DeleteButton_Click(object sender, EventArgs e)
757779

758780
EnableButtons();
759781
}
760-
761-
private void directoryDelete(string directoryName)
762-
{
763-
if (Directory.Exists(directoryName))
764-
{
765-
directoryRemoveReadOnlyFlags(directoryName);
766-
Directory.Delete(directoryName, true);
767-
}
768-
}
769-
770-
private void directoryRemoveReadOnlyFlags(string directoryName)
771-
{
772-
foreach (string filename in Directory.GetFiles(directoryName))
773-
{
774-
FileInfo file = new FileInfo(filename);
775-
file.IsReadOnly = false;
776-
}
777-
foreach (string subDirectoryName in Directory.GetDirectories(directoryName))
778-
{
779-
directoryRemoveReadOnlyFlags(subDirectoryName);
780-
}
781-
}
782782
#endregion
783783

784784
private void DisableButtons()
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// COPYRIGHT 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2023 by the Open Rails project.
2+
//
3+
// This file is part of Open Rails.
4+
//
5+
// Open Rails is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// Open Rails is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
17+
//
18+
19+
using System;
20+
using System.IO;
21+
using Microsoft.SqlServer.Server;
22+
23+
namespace ORTS.Common
24+
{
25+
public class DirectoryAndFiles
26+
{
27+
28+
public static void directoryDelete(string directoryName)
29+
{
30+
if (Directory.Exists(directoryName))
31+
{
32+
// remove the read only flags,
33+
// otherwise the Directory.delete does not work in case read only files exists
34+
directoryRemoveReadOnlyFlags(directoryName);
35+
Directory.Delete(directoryName, true);
36+
}
37+
}
38+
39+
private static void directoryRemoveReadOnlyFlags(string directoryName)
40+
{
41+
foreach (string filename in Directory.GetFiles(directoryName))
42+
{
43+
_ = new FileInfo(filename)
44+
{
45+
IsReadOnly = false
46+
};
47+
}
48+
foreach (string subDirectoryName in Directory.GetDirectories(directoryName))
49+
{
50+
directoryRemoveReadOnlyFlags(subDirectoryName);
51+
}
52+
}
53+
54+
//
55+
// example:
56+
// input: D:\OpenRailsMaster\Program
57+
// output: D:\OpenRailsMaster
58+
//
59+
public static string determineTopDirectory(string directoryName)
60+
{
61+
string tmpDirectoryName = directoryName;
62+
string topDirectoryName = directoryName;
63+
64+
while (Directory.GetParent(tmpDirectoryName) != null)
65+
{
66+
topDirectoryName = tmpDirectoryName;
67+
tmpDirectoryName = Directory.GetParent(tmpDirectoryName).ToString();
68+
}
69+
70+
return topDirectoryName;
71+
}
72+
}
73+
}

Source/ORTS.Menu/Routes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class Route
3030
public readonly string Description;
3131
public readonly string Path;
3232

33-
GettextResourceManager Catalog = new GettextResourceManager("ORTS.Menu");
33+
readonly GettextResourceManager Catalog = new GettextResourceManager("ORTS.Menu");
3434

3535
Route(string path)
3636
{

Source/ORTS.Settings/RouteSettings.cs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using Newtonsoft.Json;
2424
using Newtonsoft.Json.Linq;
2525
using LibGit2Sharp;
26+
using ORTS.Common;
2627

2728
namespace ORTS.Settings
2829
{
@@ -191,7 +192,7 @@ public void LoadContentAndInstalled()
191192
}
192193
}
193194

194-
directoryDelete(definedContentJsonDirectoryName);
195+
DirectoryAndFiles.directoryDelete(definedContentJsonDirectoryName);
195196
}
196197
catch (Exception error)
197198
{
@@ -245,31 +246,6 @@ private string convertResultToString(JToken result, string fieldName)
245246
}
246247
}
247248

248-
private void directoryDelete(string directoryName)
249-
{
250-
if (Directory.Exists(directoryName))
251-
{
252-
// remove the read only flags, otherwise the Directory.delete does not work
253-
directoryRemoveReadOnlyFlags(directoryName);
254-
Directory.Delete(directoryName, true);
255-
}
256-
}
257-
258-
private void directoryRemoveReadOnlyFlags(string directoryName)
259-
{
260-
foreach (string filename in Directory.GetFiles(directoryName))
261-
{
262-
_ = new FileInfo(filename)
263-
{
264-
IsReadOnly = false
265-
};
266-
}
267-
foreach (string subDirectoryName in Directory.GetDirectories(directoryName))
268-
{
269-
directoryRemoveReadOnlyFlags(subDirectoryName);
270-
}
271-
}
272-
273249
public void Save()
274250
{
275251
IDictionary<string, Route> routes = new Dictionary<string, Route>();

0 commit comments

Comments
 (0)