Skip to content

Commit f4ac1a5

Browse files
committed
feat: Normalise process file/directory/info to avoid Windows.Forms
1 parent 433d161 commit f4ac1a5

File tree

20 files changed

+70
-54
lines changed

20 files changed

+70
-54
lines changed

Source/ContentChecker/Program.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,10 @@ static bool OptionsContain(string[] args, IEnumerable<string> optionNames) {
7777
/// </summary>
7878
static void ShowHelp()
7979
{
80-
var version = FileVersionInfo.GetVersionInfo(typeof(Program).Assembly.Location);
81-
Console.WriteLine("{0} {1}", version.FileDescription, VersionInfo.VersionOrBuild);
80+
Console.WriteLine("{0} {1}", ApplicationInfo.ApplicationName, VersionInfo.VersionOrBuild);
8281
Console.WriteLine();
8382
Console.WriteLine("Usage:");
84-
Console.WriteLine(" {0} [options] <FILE> [...]", Path.GetFileNameWithoutExtension(version.FileName));
83+
Console.WriteLine(" {0} [options] <FILE> [...]", Path.GetFileNameWithoutExtension(ApplicationInfo.ProcessFile));
8584
Console.WriteLine();
8685
Console.WriteLine("Arguments:");
8786
Console.WriteLine(" <FILE> Data files to check; may contain wildcards");

Source/Contrib/DataCollector/Program.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
using ORTS.Common;
2020
using System;
2121
using System.Collections.Generic;
22-
using System.Diagnostics;
2322
using System.IO;
2423
using System.Linq;
2524

@@ -39,11 +38,10 @@ static void Main(string[] args)
3938

4039
static void ShowHelp()
4140
{
42-
var version = FileVersionInfo.GetVersionInfo(typeof(Program).Assembly.Location);
43-
Console.WriteLine("{0} {1}", version.FileDescription, VersionInfo.VersionOrBuild);
41+
Console.WriteLine("{0} {1}", ApplicationInfo.ApplicationName, VersionInfo.VersionOrBuild);
4442
Console.WriteLine();
4543
Console.WriteLine("Usage:");
46-
Console.WriteLine(" {0} [options] [<PATH> [...]]", Path.GetFileNameWithoutExtension(version.FileName));
44+
Console.WriteLine(" {0} [options] [<PATH> [...]]", Path.GetFileNameWithoutExtension(ApplicationInfo.ProcessFile));
4745
Console.WriteLine();
4846
Console.WriteLine("Arguments:");
4947
Console.WriteLine(" <PATH> Directories to scan for specific options");

Source/Contrib/DataConverter/Program.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
using System;
1919
using System.Collections.Generic;
20-
using System.Diagnostics;
2120
using System.IO;
2221
using System.Linq;
2322
using System.Runtime.Serialization;
@@ -87,11 +86,10 @@ static void Main(string[] args)
8786

8887
static void ShowHelp(List<IDataConverter> converters)
8988
{
90-
var version = FileVersionInfo.GetVersionInfo(typeof(Program).Assembly.Location);
91-
Console.WriteLine("{0} {1}", version.FileDescription, VersionInfo.VersionOrBuild);
89+
Console.WriteLine("{0} {1}", ApplicationInfo.ApplicationName, VersionInfo.VersionOrBuild);
9290
Console.WriteLine();
9391
Console.WriteLine("Usage:");
94-
Console.WriteLine(" {0} /input <INPUT> [/output] [<OUTPUT> [...]]", Path.GetFileNameWithoutExtension(version.FileName));
92+
Console.WriteLine(" {0} /input <INPUT> [/output] [<OUTPUT> [...]]", Path.GetFileNameWithoutExtension(ApplicationInfo.ProcessFile));
9593
Console.WriteLine();
9694
Console.WriteLine("Arguments:");
9795
Console.WriteLine(" <INPUT> Specifies the file to read");

Source/Contrib/DataValidator/Program.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@ static void Main(string[] args)
3939

4040
static void ShowHelp()
4141
{
42-
var version = FileVersionInfo.GetVersionInfo(typeof(Program).Assembly.Location);
43-
Console.WriteLine("{0} {1}", version.FileDescription, VersionInfo.VersionOrBuild);
42+
Console.WriteLine("{0} {1}", ApplicationInfo.ApplicationName, VersionInfo.VersionOrBuild);
4443
Console.WriteLine();
4544
Console.WriteLine("Usage:");
46-
Console.WriteLine(" {0} [options] <FILE> [...]", Path.GetFileNameWithoutExtension(version.FileName));
45+
Console.WriteLine(" {0} [options] <FILE> [...]", Path.GetFileNameWithoutExtension(ApplicationInfo.ProcessFile));
4746
Console.WriteLine();
4847
Console.WriteLine("Arguments:");
4948
Console.WriteLine(" <FILE> Data files to validate; may contain wildcards");

Source/Contrib/SimulatorTester/Program.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
using Orts.Simulation;
2222
using ORTS.Common;
2323
using System.IO;
24-
using System.Windows.Forms;
2524
using Orts.Common;
26-
using System.Diagnostics;
2725

2826
namespace SimulatorTester
2927
{
@@ -37,23 +35,22 @@ static void Main(string[] args)
3735

3836
if (files.Count != 1 || options.Contains("help", StringComparer.InvariantCultureIgnoreCase))
3937
{
40-
var version = FileVersionInfo.GetVersionInfo(Application.ExecutablePath);
41-
Console.WriteLine("{0} {1}", version.FileDescription, VersionInfo.VersionOrBuild);
38+
Console.WriteLine("{0} {1}", ApplicationInfo.ApplicationName, VersionInfo.VersionOrBuild);
4239
Console.WriteLine();
4340
Console.WriteLine("Usage:");
44-
Console.WriteLine(" {0} [options] <SAVE_FILE>", Path.GetFileNameWithoutExtension(Application.ExecutablePath));
41+
Console.WriteLine(" {0} [options] <SAVE_FILE>", Path.GetFileNameWithoutExtension(ApplicationInfo.ProcessFile));
4542
Console.WriteLine();
4643
Console.WriteLine("Arguments:");
47-
Console.WriteLine(" <SAVE_FILE> {0} save file to use", Application.ProductName);
44+
Console.WriteLine(" <SAVE_FILE> {0} save file to use", ApplicationInfo.ProductName);
4845
Console.WriteLine();
4946
Console.WriteLine("Options:");
5047
Console.WriteLine(" /quiet Do not show summary of simulation (only exit code is set)");
51-
Console.WriteLine(" /verbose Show version and settings (similar to a {0} log)", Application.ProductName);
48+
Console.WriteLine(" /verbose Show version and settings (similar to a {0} log)", ApplicationInfo.ProductName);
5249
Console.WriteLine(" /fps <FPS> Set the simulation frame-rate [default: 10]");
5350
Console.WriteLine(" /help Show help and usage information");
54-
Console.WriteLine(" ...and any standard {0} option", Application.ProductName);
51+
Console.WriteLine(" ...and any standard {0} option", ApplicationInfo.ProductName);
5552
Console.WriteLine();
56-
Console.WriteLine("The {0} takes a save file and:", version.FileDescription);
53+
Console.WriteLine("The {0} takes a save file and:", ApplicationInfo.ApplicationName);
5754
Console.WriteLine(" - Loads the same activity as contained in the save file");
5855
Console.WriteLine(" - Runs the simulation at the specified FPS for the same duration as the save file");
5956
Console.WriteLine(" - Compares the final position with that contained in the save file");
@@ -65,15 +62,15 @@ static void Main(string[] args)
6562

6663
if (settings.Verbose)
6764
{
68-
Console.WriteLine("This is a log file for {0}. Please include this file in bug reports.", Application.ProductName);
65+
Console.WriteLine("This is a log file for {0}. Please include this file in bug reports.", ApplicationInfo.ProductName);
6966
LogSeparator();
7067

7168
SystemInfo.WriteSystemDetails(Console.Out);
7269
LogSeparator();
7370

7471
Console.WriteLine("Version = {0}", VersionInfo.Version.Length > 0 ? VersionInfo.Version : "<none>");
7572
Console.WriteLine("Build = {0}", VersionInfo.Build);
76-
Console.WriteLine("Executable = {0}", Path.GetFileName(Application.ExecutablePath));
73+
Console.WriteLine("Executable = {0}", Path.GetFileName(ApplicationInfo.ProcessFile));
7774
foreach (var arg in args)
7875
Console.WriteLine("Argument = {0}", arg);
7976
LogSeparator();

Source/Menu/MainForm.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ internal string RunActivityProgram
7777
{
7878
get
7979
{
80-
return System.IO.Path.Combine(Application.StartupPath, "RunActivity.exe");
80+
return System.IO.Path.Combine(ApplicationInfo.ProcessDirectory, "RunActivity.exe");
8181
}
8282
}
8383

@@ -127,7 +127,7 @@ public MainForm()
127127
panelModeTimetable.Location = panelModeActivity.Location;
128128
ShowDetails();
129129
UpdateEnabled();
130-
UpdateManager = new UpdateManager(System.IO.Path.GetDirectoryName(Application.ExecutablePath), Application.ProductName, VersionInfo.VersionOrBuild);
130+
UpdateManager = new UpdateManager(ApplicationInfo.ProcessDirectory, Application.ProductName, VersionInfo.VersionOrBuild);
131131
ElevationIcon = new Icon(SystemIcons.Shield, SystemInformation.SmallIconSize).ToBitmap();
132132
}
133133

@@ -183,7 +183,7 @@ void MainForm_Shown(object sender, EventArgs e)
183183
"Updater.exe",
184184
};
185185
var tools = new List<ToolStripItem>();
186-
foreach (var executable in Directory.GetFiles(System.IO.Path.GetDirectoryName(Application.ExecutablePath), "*.exe"))
186+
foreach (var executable in Directory.GetFiles(ApplicationInfo.ProcessDirectory, "*.exe"))
187187
{
188188
// Don't show any of the core parts of the application.
189189
if (coreExecutables.Contains(System.IO.Path.GetFileName(executable)))
@@ -353,7 +353,7 @@ void LoadLanguage()
353353

354354
void RestartMenu()
355355
{
356-
Process.Start(Application.ExecutablePath);
356+
Process.Start(ApplicationInfo.ProcessFile);
357357
Close();
358358
}
359359
#endregion

Source/Menu/Options.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
using GNU.Gettext;
2828
using GNU.Gettext.WinForms;
2929
using MSTS;
30+
using ORTS.Common;
3031
using ORTS.Common.Input;
3132
using ORTS.Settings;
3233
using ORTS.Updater;
@@ -72,7 +73,7 @@ public OptionsForm(UserSettings settings, UpdateManager updateManager, bool init
7273
// Collect all the available language codes by searching for
7374
// localisation files, but always include English (base language).
7475
var languageCodes = new List<string> { "en" };
75-
foreach (var path in Directory.GetDirectories(Path.GetDirectoryName(Application.ExecutablePath)))
76+
foreach (var path in Directory.GetDirectories(ApplicationInfo.ProcessDirectory))
7677
if (Directory.GetFiles(path, "*.Messages.resources.dll").Length > 0)
7778
languageCodes.Add(Path.GetFileName(path));
7879

@@ -728,11 +729,11 @@ private void textBoxContentName_TextChanged(object sender, EventArgs e)
728729
var current = bindingSourceContent.Current as ContentFolder;
729730
if (current != null && current.Name != textBoxContentName.Text)
730731
{
731-
if (current.Path.ToLower().Contains(Application.StartupPath.ToLower()))
732+
if (current.Path.ToLower().Contains(ApplicationInfo.ProcessDirectory.ToLower()))
732733
{
733734
// Block added because a succesful Update operation will empty the Open Rails folder and lose any content stored within it.
734735
MessageBox.Show(catalog.GetString
735-
($"Cannot use content from any folder which lies inside the Open Rails folder {Application.StartupPath}\n\n")
736+
($"Cannot use content from any folder which lies inside the Open Rails folder {ApplicationInfo.ProcessDirectory}\n\n")
736737
, "Invalid content location"
737738
, MessageBoxButtons.OK
738739
, MessageBoxIcon.Error);

Source/Menu/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ static void MainForm()
168168
FileName = MainForm.RunActivityProgram,
169169
Arguments = joinedParameters,
170170
WindowStyle = ProcessWindowStyle.Normal,
171-
WorkingDirectory = Application.StartupPath,
171+
WorkingDirectory = ApplicationInfo.ProcessDirectory,
172172
};
173173
var process = Process.Start(processStartInfo);
174174
process.WaitForExit();

Source/Menu/TestingForm.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using System.Windows.Forms;
2626
using GNU.Gettext;
2727
using GNU.Gettext.WinForms;
28+
using ORTS.Common;
2829
using ORTS.Menu;
2930
using ORTS.Settings;
3031
using Activity = ORTS.Menu.Activity;
@@ -186,7 +187,7 @@ void TestMarkedActivities(IEnumerable<DataGridViewRow> rows)
186187
var processStartInfo = new ProcessStartInfo();
187188
processStartInfo.FileName = MainForm.RunActivityProgram;
188189
processStartInfo.WindowStyle = ProcessWindowStyle.Normal;
189-
processStartInfo.WorkingDirectory = Application.StartupPath;
190+
processStartInfo.WorkingDirectory = ApplicationInfo.ProcessDirectory;
190191

191192
if (!ClearedLogs)
192193
{

Source/ORTS.Common/ApplicationInfo.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// COPYRIGHT 2009 - 2024 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+
using System.Diagnostics;
19+
using System.IO;
20+
21+
namespace ORTS.Common
22+
{
23+
public static class ApplicationInfo
24+
{
25+
public static string ProcessFile => Process.GetCurrentProcess().MainModule.FileName;
26+
public static string ProcessDirectory => Path.GetDirectoryName(ProcessFile);
27+
static FileVersionInfo VersionInfo => FileVersionInfo.GetVersionInfo(ProcessFile);
28+
public static string ProductName => VersionInfo.ProductName;
29+
public static string ApplicationName => VersionInfo.FileDescription;
30+
}
31+
}

0 commit comments

Comments
 (0)