Skip to content

Commit 58b2418

Browse files
authored
Merge pull request #970 from twpol/feature/less-windows-forms
feat: Remove unnecessary Windows.Forms usage
2 parents ca63633 + fd28a41 commit 58b2418

28 files changed

+102
-83
lines changed

Source/ContentChecker/ContentChecker.csproj

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
<TargetFramework Condition="'$(BuildDotNet)' == 'true'">net6-windows</TargetFramework>
44
<TargetFramework Condition="'$(TargetFramework)' == ''">net472</TargetFramework>
55
<OutputType>Exe</OutputType>
6-
<UseWindowsForms>true</UseWindowsForms>
7-
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
86
<IsPublishable>False</IsPublishable>
97
<AssemblyTitle>Open Rails Content Checker</AssemblyTitle>
108
<Description>Open Rails Transport Simulator</Description>

Source/ContentChecker/Loader.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
using System.Linq;
4444
using System.Text;
4545
using Microsoft.Xna.Framework.Graphics;
46-
using System.Windows.Forms;
4746

4847
namespace ContentChecker
4948
{
@@ -70,7 +69,7 @@ abstract class Loader
7069
/// <summary> The number of files that were actually loaded </summary>
7170
public int FilesLoaded { get; protected set; }
7271
/// <summary> The number of files that were not loaded but skipped </summary>
73-
public int FilesSkipped {get; protected set;}
72+
public int FilesSkipped { get; protected set; }
7473

7574
/// <summary> The action to take when an additonal file has been identified. This is intended to be set externally </summary>
7675
protected Action<string, Loader> AddAdditionalFileAction { get; set; }
@@ -158,17 +157,13 @@ protected static GraphicsDevice GetGraphicsDevice()
158157
{
159158
if (_graphicsDevice == null)
160159
{
161-
// We use a Windows.Forms Control instead of an xna GAME because it is much easier to use.
162-
var _c = new Control();
163-
164160
// Details probably do not matter too much
165161
PresentationParameters parameters = new PresentationParameters()
166162
{
167163
BackBufferWidth = 100,
168164
BackBufferHeight = 100,
169165
BackBufferFormat = SurfaceFormat.Color,
170166
//DepthStencilFormat = DepthFormat.Depth24,
171-
DeviceWindowHandle = _c.Handle,
172167
PresentationInterval = PresentInterval.Immediate,
173168
IsFullScreen = false,
174169
};

Source/ContentChecker/Program.cs

+2-3
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

+2-4
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

+2-4
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

+2-3
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

+8-11
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/Contrib/SimulatorTester/SimulatorTester.csproj

-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
<RootNamespace>Orts.SimulatorTester</RootNamespace>
77
<AssemblyName>Contrib.SimulatorTester</AssemblyName>
88
<ApplicationIcon>..\..\ORTS.ico</ApplicationIcon>
9-
<UseWindowsForms>true</UseWindowsForms>
10-
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
11-
<OutputPath>..\..\..\Program\</OutputPath>
129
<IsPublishable>False</IsPublishable>
1310
<AssemblyTitle>Open Rails Simulator Tester (Contributed)</AssemblyTitle>
1411
<Description>Open Rails Transport Simulator</Description>

Source/Menu/MainForm.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ internal string RunActivityProgram
7878
{
7979
get
8080
{
81-
return System.IO.Path.Combine(Application.StartupPath, "RunActivity.exe");
81+
return System.IO.Path.Combine(ApplicationInfo.ProcessDirectory, "RunActivity.exe");
8282
}
8383
}
8484

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

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

349349
void RestartMenu()
350350
{
351-
Process.Start(Application.ExecutablePath);
351+
Process.Start(ApplicationInfo.ProcessFile);
352352
Close();
353353
}
354354
#endregion

Source/Menu/Options.cs

+4-3
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;
@@ -88,7 +89,7 @@ public OptionsForm(UserSettings settings, UpdateManager updateManager, bool init
8889
// Collect all the available language codes by searching for
8990
// localisation files, but always include English (base language).
9091
var languageCodes = new List<string> { "en" };
91-
foreach (var path in Directory.GetDirectories(Path.GetDirectoryName(Application.ExecutablePath)))
92+
foreach (var path in Directory.GetDirectories(ApplicationInfo.ProcessDirectory))
9293
if (Directory.GetFiles(path, "*.Messages.resources.dll").Length > 0)
9394
languageCodes.Add(Path.GetFileName(path));
9495

@@ -744,11 +745,11 @@ private void textBoxContentName_TextChanged(object sender, EventArgs e)
744745
var current = bindingSourceContent.Current as ContentFolder;
745746
if (current != null && current.Name != textBoxContentName.Text)
746747
{
747-
if (current.Path.ToLower().Contains(Application.StartupPath.ToLower()))
748+
if (current.Path.ToLower().Contains(ApplicationInfo.ProcessDirectory.ToLower()))
748749
{
749750
// Block added because a succesful Update operation will empty the Open Rails folder and lose any content stored within it.
750751
MessageBox.Show(catalog.GetString
751-
($"Cannot use content from any folder which lies inside the Open Rails folder {Application.StartupPath}\n\n")
752+
($"Cannot use content from any folder which lies inside the Open Rails folder {ApplicationInfo.ProcessDirectory}\n\n")
752753
, "Invalid content location"
753754
, MessageBoxButtons.OK
754755
, MessageBoxIcon.Error);

Source/Menu/Program.cs

+1-1
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

+2-1
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

+31
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+
}

Source/ORTS.Common/ORTS.Common.csproj

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
<TargetFramework Condition="'$(BuildDotNet)' == 'true'">net6-windows</TargetFramework>
44
<TargetFramework Condition="'$(TargetFramework)' == ''">net472</TargetFramework>
55
<OutputType>Library</OutputType>
6-
<UseWindowsForms>true</UseWindowsForms>
7-
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
86
<IsPublishable>False</IsPublishable>
97
<AssemblyTitle>Open Rails Common Library</AssemblyTitle>
108
<Description>Open Rails Transport Simulator</Description>

Source/ORTS.Common/SystemInfo.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static SystemInfo()
4040
{
4141
Application = new Platform
4242
{
43-
Name = System.Windows.Forms.Application.ProductName,
43+
Name = ApplicationInfo.ProductName,
4444
Version = VersionInfo.VersionOrBuild,
4545
Architecture = RuntimeInformation.ProcessArchitecture.ToString(),
4646
};

Source/ORTS.Common/VersionInfo.cs

+2-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
using System.Collections.Generic;
2020
using System.Diagnostics;
2121
using System.IO;
22-
using System.Linq;
2322

2423
namespace ORTS.Common
2524
{
@@ -28,8 +27,6 @@ namespace ORTS.Common
2827
/// </summary>
2928
public static class VersionInfo
3029
{
31-
static readonly string ApplicationPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
32-
3330
/// <summary>Full version, e.g. stable: "1.4", testing: "T1.4-1-g1234567", unstable: "U2021.01.01-0000", local: ""</summary>
3431
public static readonly string Version = GetVersion("OpenRails.exe");
3532

@@ -43,7 +40,7 @@ static string GetVersion(string fileName)
4340
{
4441
try
4542
{
46-
var version = FileVersionInfo.GetVersionInfo(Path.Combine(ApplicationPath, fileName));
43+
var version = FileVersionInfo.GetVersionInfo(Path.Combine(ApplicationInfo.ProcessDirectory, fileName));
4744
if (version.ProductVersion != version.FileVersion)
4845
return version.ProductVersion;
4946
}
@@ -58,7 +55,7 @@ static string GetBuild(string fileName)
5855
var builds = new Dictionary<TimeSpan, string>();
5956
try
6057
{
61-
var version = FileVersionInfo.GetVersionInfo(Path.Combine(ApplicationPath, fileName));
58+
var version = FileVersionInfo.GetVersionInfo(Path.Combine(ApplicationInfo.ProcessDirectory, fileName));
6259
var datetime = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc);
6360
var timespan = new TimeSpan(version.FileBuildPart, 0, 0, version.FilePrivatePart * 2);
6461
return String.Format("{0} ({1:u})", version.FileVersion, datetime + timespan);

Source/ORTS.Settings/ORTS.Settings.csproj

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
<TargetFramework Condition="'$(BuildDotNet)' == 'true'">net6-windows</TargetFramework>
44
<TargetFramework Condition="'$(TargetFramework)' == ''">net472</TargetFramework>
55
<OutputType>Library</OutputType>
6-
<UseWindowsForms>true</UseWindowsForms>
7-
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
86
<IsPublishable>False</IsPublishable>
97
<AssemblyTitle>Open Rails Settings Library</AssemblyTitle>
108
<Description>Open Rails Transport Simulator</Description>

Source/ORTS.Settings/UpdateSettings.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020
using System.IO;
2121
using System.Linq;
2222
using System.Reflection;
23-
using System.Windows.Forms;
2423
using ORTS.Common;
2524

2625
namespace ORTS.Settings
2726
{
2827
public class UpdateSettings : SettingsBase
2928
{
30-
public static readonly string SettingsFilePath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "Updater.ini");
29+
public static readonly string SettingsFilePath = Path.Combine(ApplicationInfo.ProcessDirectory, "Updater.ini");
3130

3231
#region User Settings
3332

0 commit comments

Comments
 (0)