Skip to content

Commit 7c98da1

Browse files
authored
Merge pull request #640 from twpol/feature/simulator-test
feat: Initial implementation of Simulation Tester
2 parents 6908fa6 + e80ec99 commit 7c98da1

File tree

9 files changed

+442
-108
lines changed

9 files changed

+442
-108
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
5+
</startup>
6+
</configuration>
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// COPYRIGHT 2022 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;
19+
using System.Collections.Generic;
20+
using System.Linq;
21+
using Orts.Simulation;
22+
using ORTS.Common;
23+
using System.IO;
24+
using System.Windows.Forms;
25+
using Orts.Common;
26+
27+
namespace SimulatorTester
28+
{
29+
internal class Program
30+
{
31+
static void Main(string[] args)
32+
{
33+
var options = args.Where(a => a.StartsWith("-") || a.StartsWith("/")).Select(a => a.Substring(1));
34+
var files = args.Where(a => !a.StartsWith("-") && !a.StartsWith("/"));
35+
var settings = new UserSettings(options);
36+
37+
if (settings.Verbose)
38+
{
39+
Console.WriteLine("This is a log file for {0}. Please include this file in bug reports.", Application.ProductName);
40+
LogSeparator();
41+
42+
SystemInfo.WriteSystemDetails(Console.Out);
43+
LogSeparator();
44+
45+
Console.WriteLine("Version = {0}", VersionInfo.Version.Length > 0 ? VersionInfo.Version : "<none>");
46+
Console.WriteLine("Build = {0}", VersionInfo.Build);
47+
Console.WriteLine("Executable = {0}", Path.GetFileName(Application.ExecutablePath));
48+
foreach (var arg in args)
49+
Console.WriteLine("Argument = {0}", arg);
50+
LogSeparator();
51+
52+
settings.Log();
53+
LogSeparator();
54+
}
55+
56+
var saveFile = files.First();
57+
using (BinaryReader inf = new BinaryReader(new FileStream(saveFile, FileMode.Open, FileAccess.Read)))
58+
{
59+
var cts = new CancellationTokenSource(() => { });
60+
var data = GetSaveData(inf);
61+
var activityFile = data.Args[0];
62+
63+
if (!settings.Quiet)
64+
{
65+
foreach (var arg in data.Args)
66+
Console.WriteLine("Argument = {0}", arg);
67+
Console.WriteLine("Initial Pos = {0}, {1}", data.InitialTileX, data.InitialTileZ);
68+
Console.WriteLine("Expected Pos = {0}, {1}", data.ExpectedTileX, data.ExpectedTileZ);
69+
Console.Write("Loading... ");
70+
}
71+
72+
var startTime = DateTimeOffset.Now;
73+
var simulator = new Simulator(settings, activityFile, useOpenRailsDirectory: false, deterministic: true);
74+
simulator.SetActivity(activityFile);
75+
simulator.Start(cts.Token);
76+
simulator.SetCommandReceivers();
77+
simulator.Log.LoadLog(Path.ChangeExtension(saveFile, "replay"));
78+
simulator.ReplayCommandList = new List<ICommand>();
79+
simulator.ReplayCommandList.AddRange(simulator.Log.CommandList);
80+
simulator.Log.CommandList.Clear();
81+
82+
var loadTime = DateTimeOffset.Now;
83+
if (!settings.Quiet)
84+
{
85+
Console.WriteLine("{0:N1} seconds", (loadTime - startTime).TotalSeconds);
86+
Console.Write("Replaying... ");
87+
}
88+
89+
var step = 1f / settings.FPS;
90+
for (var tick = 0f; tick < data.TimeElapsed; tick += step)
91+
{
92+
simulator.Update(step);
93+
simulator.Log.Update(simulator.ReplayCommandList);
94+
}
95+
96+
var endTime = DateTimeOffset.Now;
97+
var actualTileX = simulator.Trains[0].FrontTDBTraveller.TileX + (simulator.Trains[0].FrontTDBTraveller.X / WorldPosition.TileSize);
98+
var actualTileZ = simulator.Trains[0].FrontTDBTraveller.TileZ + (simulator.Trains[0].FrontTDBTraveller.Z / WorldPosition.TileSize);
99+
var initialToExpectedM = Math.Sqrt(Math.Pow(data.ExpectedTileX - data.InitialTileX, 2) + Math.Pow(data.ExpectedTileZ - data.InitialTileZ, 2)) * WorldPosition.TileSize;
100+
var expectedToActualM = Math.Sqrt(Math.Pow(actualTileX - data.ExpectedTileX, 2) + Math.Pow(actualTileZ - data.ExpectedTileZ, 2)) * WorldPosition.TileSize;
101+
102+
if (!settings.Quiet)
103+
{
104+
Console.WriteLine("{0:N1} seconds ({1:F0}x speed-up)", (endTime - loadTime).TotalSeconds, data.TimeElapsed / (endTime - loadTime).TotalSeconds);
105+
Console.WriteLine("Actual Pos = {0}, {1}", actualTileX, actualTileZ);
106+
Console.WriteLine("Distance = {0:N3} m ({1:P1})", expectedToActualM, 1 - expectedToActualM / initialToExpectedM);
107+
}
108+
109+
Environment.ExitCode = (int)expectedToActualM;
110+
}
111+
}
112+
113+
static void LogSeparator()
114+
{
115+
Console.WriteLine(new String('-', 80));
116+
}
117+
118+
static SaveData GetSaveData(BinaryReader inf)
119+
{
120+
var values = new SaveData();
121+
122+
inf.ReadString(); // Version
123+
inf.ReadString(); // Build
124+
125+
var routeName = inf.ReadString();
126+
if (routeName == "$Multipl$")
127+
{
128+
inf.ReadString(); // Route name
129+
}
130+
131+
inf.ReadString(); // Path name
132+
values.TimeElapsed = inf.ReadInt32(); // Time elapsed in game (secs)
133+
inf.ReadInt64(); // Date and time in real world
134+
135+
values.ExpectedTileX = inf.ReadSingle(); // Current location of player train TileX
136+
values.ExpectedTileZ = inf.ReadSingle(); // Current location of player train TileZ
137+
138+
values.InitialTileX = inf.ReadSingle(); // Initial location of player train TileX
139+
values.InitialTileZ = inf.ReadSingle(); // Initial location of player train TileZ
140+
141+
values.Args = new string[inf.ReadInt32()];
142+
for (var i = 0; i < values.Args.Length; i++)
143+
values.Args[i] = inf.ReadString();
144+
145+
inf.ReadString(); // Activity type
146+
147+
return values;
148+
}
149+
150+
struct SaveData
151+
{
152+
public int TimeElapsed;
153+
public float InitialTileX;
154+
public float InitialTileZ;
155+
public float ExpectedTileX;
156+
public float ExpectedTileZ;
157+
public string[] Args;
158+
}
159+
}
160+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Open Rails Simulator Tester (Contributed)")]
9+
[assembly: AssemblyDescription("Open Rails Transport Simulator")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("Open Rails")]
12+
[assembly: AssemblyProduct("Open Rails")]
13+
[assembly: AssemblyCopyright("Copyright © 2009 - 2022")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("792b01a1-5c43-4ffa-a4c0-bbdc95a4a178")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("0.0.*")]
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{792B01A1-5C43-4FFA-A4C0-BBDC95A4A178}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>Orts.SimulatorTester</RootNamespace>
10+
<AssemblyName>Contrib.SimulatorTester</AssemblyName>
11+
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<TargetFrameworkProfile />
15+
</PropertyGroup>
16+
<PropertyGroup>
17+
<ApplicationIcon>..\..\ORTS.ico</ApplicationIcon>
18+
</PropertyGroup>
19+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
20+
<PlatformTarget>AnyCPU</PlatformTarget>
21+
<DebugSymbols>true</DebugSymbols>
22+
<DebugType>full</DebugType>
23+
<Optimize>false</Optimize>
24+
<OutputPath>..\..\..\Program\</OutputPath>
25+
<DefineConstants>DEBUG;TRACE</DefineConstants>
26+
<ErrorReport>prompt</ErrorReport>
27+
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
28+
<Prefer32Bit>false</Prefer32Bit>
29+
<LangVersion>7.3</LangVersion>
30+
</PropertyGroup>
31+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
32+
<PlatformTarget>AnyCPU</PlatformTarget>
33+
<DebugType>pdbonly</DebugType>
34+
<Optimize>true</Optimize>
35+
<OutputPath>..\..\..\Program\</OutputPath>
36+
<DefineConstants>TRACE</DefineConstants>
37+
<ErrorReport>prompt</ErrorReport>
38+
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
39+
<Prefer32Bit>false</Prefer32Bit>
40+
<LangVersion>7.3</LangVersion>
41+
</PropertyGroup>
42+
<ItemGroup>
43+
<Reference Include="System" />
44+
<Reference Include="System.Core" />
45+
<Reference Include="System.Xml.Linq" />
46+
<Reference Include="System.Data.DataSetExtensions" />
47+
<Reference Include="Microsoft.CSharp" />
48+
<Reference Include="System.Data" />
49+
<Reference Include="System.Net.Http" />
50+
<Reference Include="System.Windows.Forms" />
51+
<Reference Include="System.Xml" />
52+
</ItemGroup>
53+
<ItemGroup>
54+
<Compile Include="Program.cs" />
55+
<Compile Include="UserSettings.cs" />
56+
<Compile Include="Properties\AssemblyInfo.cs" />
57+
</ItemGroup>
58+
<ItemGroup>
59+
<ProjectReference Include="..\..\ORTS.Common\ORTS.Common.csproj">
60+
<Project>{DA94D876-7D35-46C3-AECE-AFACE72C686C}</Project>
61+
<Name>ORTS.Common</Name>
62+
</ProjectReference>
63+
<ProjectReference Include="..\..\ORTS.Settings\ORTS.Settings.csproj">
64+
<Project>{67f84996-8769-4fd8-819b-464af269037b}</Project>
65+
<Name>ORTS.Settings</Name>
66+
</ProjectReference>
67+
<ProjectReference Include="..\..\Orts.Simulation\Orts.Simulation.csproj">
68+
<Project>{333c4378-d82e-4b47-b561-6ba923b8fdfd}</Project>
69+
<Name>Orts.Simulation</Name>
70+
</ProjectReference>
71+
</ItemGroup>
72+
<ItemGroup>
73+
<None Include="App.config" />
74+
</ItemGroup>
75+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
76+
</Project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// COPYRIGHT 2022 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.Collections.Generic;
19+
using ORTS.Settings;
20+
21+
namespace SimulatorTester
22+
{
23+
public class UserSettings : ORTS.Settings.UserSettings
24+
{
25+
[Default(false)]
26+
public bool Quiet { get; set; }
27+
28+
[Default(false)]
29+
public bool Verbose { get; set; }
30+
31+
[Default(10)]
32+
public int FPS { get; set; }
33+
34+
public UserSettings(IEnumerable<string> options)
35+
: base(options)
36+
{
37+
}
38+
}
39+
}

Source/ORTS.Common/SystemInfo.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,19 @@ static string WriteInstalledRuntimes(TextWriter output, string versionKeyName, R
201201

202202
static void WriteGraphicsAdapter(TextWriter output)
203203
{
204-
foreach (var adapter in GraphicsAdapter.Adapters)
205-
{
206-
try
204+
try {
205+
foreach (var adapter in GraphicsAdapter.Adapters)
207206
{
208-
output.WriteLine("{0} = {1}", adapter.DeviceName, adapter.Description);
207+
try
208+
{
209+
output.WriteLine("{0} = {1}", adapter.DeviceName, adapter.Description);
210+
}
211+
catch (Exception) { }
209212
}
210-
catch (Exception) { }
213+
}
214+
catch (Exception error)
215+
{
216+
output.WriteLine(error);
211217
}
212218
}
213219

Source/ORTS.sln

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.31605.320
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.1.32210.238
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RunActivity", "RunActivity\RunActivity.csproj", "{9BE44F64-417D-4AA6-AF4D-2A6A0CBC814D}"
77
EndProject
@@ -53,6 +53,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataValidator", "Contrib\Da
5353
EndProject
5454
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ContentChecker", "ContentChecker\ContentChecker.csproj", "{316A8CAC-8BD4-4A64-A384-4A3C28C5D3D8}"
5555
EndProject
56+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimulatorTester", "Contrib\SimulatorTester\SimulatorTester.csproj", "{792B01A1-5C43-4FFA-A4C0-BBDC95A4A178}"
57+
EndProject
5658
Global
5759
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5860
Debug|Any CPU = Debug|Any CPU
@@ -159,6 +161,10 @@ Global
159161
{316A8CAC-8BD4-4A64-A384-4A3C28C5D3D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
160162
{316A8CAC-8BD4-4A64-A384-4A3C28C5D3D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
161163
{316A8CAC-8BD4-4A64-A384-4A3C28C5D3D8}.Release|Any CPU.Build.0 = Release|Any CPU
164+
{792B01A1-5C43-4FFA-A4C0-BBDC95A4A178}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
165+
{792B01A1-5C43-4FFA-A4C0-BBDC95A4A178}.Debug|Any CPU.Build.0 = Debug|Any CPU
166+
{792B01A1-5C43-4FFA-A4C0-BBDC95A4A178}.Release|Any CPU.ActiveCfg = Release|Any CPU
167+
{792B01A1-5C43-4FFA-A4C0-BBDC95A4A178}.Release|Any CPU.Build.0 = Release|Any CPU
162168
EndGlobalSection
163169
GlobalSection(SolutionProperties) = preSolution
164170
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)