Skip to content

Commit 1f77ce3

Browse files
authored
Merge pull request #538 from pzgulyas/preprocesspaths
Delete binary paths if the ascii version or the tdb was modified. Blueprint: https://blueprints.launchpad.net/or/+spec/binary-timetable-paths
2 parents 4dced6b + d079b3e commit 1f77ce3

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

Source/Documentation/Manual/timetable.rst

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,9 @@ Special Rows
303303
will read that path. If not, it will read the 'normal' path, and will then
304304
store this as binary for future use. Binary paths are stored in a subdirectory
305305
named ``OpenRails`` which must be created in the Paths directory of the route.
306-
307-
**Important:**
308306

309-
- If a path is edited, the binary version must be deleted manually, otherwise the program will still use this older version.
310-
- If a route is edited, such that the .tdb might have been changed, all binary paths must be deleted.
307+
Note: If a path or the route is edited, then the binary data will be out of date.
308+
If so, it is deleted and re-created automatically when the user starts the route.
311309

312310
- ``#consist`` row
313311

Source/Orts.Formats.Msts/TrackDatabaseFile.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public class TrackDatabaseFile
3939
/// </summary>
4040
public TrackDB TrackDB { get; set; }
4141

42+
/// <summary>
43+
/// The .tdb file last modified time is stored for being able to validate derived files such as binary paths.
44+
/// </summary>
45+
public DateTime LastWriteTime { get; }
46+
4247
/// <summary>
4348
/// Constructor from file
4449
/// </summary>
@@ -49,6 +54,8 @@ public TrackDatabaseFile(string filenamewithpath)
4954
stf.ParseFile(new STFReader.TokenProcessor[] {
5055
new STFReader.TokenProcessor("trackdb", ()=>{ TrackDB = new TrackDB(stf); }),
5156
});
57+
58+
LastWriteTime = File.GetLastWriteTime(filenamewithpath);
5259
}
5360

5461
/// <summary>

Source/Orts.Simulation/Simulation/Timetables/ProcessTimetable.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ public bool PreProcessRoutes(CancellationToken cancellation)
11601160
{
11611161
// read route
11621162
bool pathValid = true;
1163-
AIPath newPath = LoadPath(thisRoute, out pathValid);
1163+
LoadPath(thisRoute, out pathValid);
11641164
if (!pathValid) allPathsLoaded = false;
11651165
if (cancellation.IsCancellationRequested)
11661166
return (false);
@@ -1187,23 +1187,23 @@ public AIPath LoadPath(string pathstring, out bool validPath)
11871187
if (String.IsNullOrEmpty(pathExtension))
11881188
formedpathFilefull = Path.ChangeExtension(formedpathFilefull, "pat");
11891189

1190-
// try to load binary path if required
1191-
bool binaryloaded = false;
1192-
AIPath outPath = null;
1193-
1194-
if (Paths.ContainsKey(formedpathFilefull))
1195-
{
1196-
outPath = new AIPath(Paths[formedpathFilefull]);
1197-
}
1198-
else
1190+
if (!Paths.TryGetValue(formedpathFilefull, out var outPath))
11991191
{
1192+
// try to load binary path if required
1193+
bool binaryloaded = false;
12001194
string formedpathFilefullBinary = Path.Combine(Path.GetDirectoryName(formedpathFilefull), "OpenRails");
12011195
formedpathFilefullBinary = Path.Combine(formedpathFilefullBinary, Path.GetFileNameWithoutExtension(formedpathFilefull));
12021196
formedpathFilefullBinary = Path.ChangeExtension(formedpathFilefullBinary, "or-binpat");
12031197

1204-
if (BinaryPaths)
1198+
if (BinaryPaths && File.Exists(formedpathFilefullBinary))
12051199
{
1206-
if (File.Exists(formedpathFilefullBinary))
1200+
var binaryLastWriteTime = File.GetLastWriteTime(formedpathFilefullBinary);
1201+
if (binaryLastWriteTime < simulator.TDB.LastWriteTime ||
1202+
File.Exists(formedpathFilefull) && binaryLastWriteTime < File.GetLastWriteTime(formedpathFilefull))
1203+
{
1204+
File.Delete(formedpathFilefullBinary);
1205+
}
1206+
else
12071207
{
12081208
try
12091209
{
@@ -1213,7 +1213,7 @@ public AIPath LoadPath(string pathstring, out bool validPath)
12131213

12141214
if (outPath.Nodes != null)
12151215
{
1216-
Paths.Add(formedpathFilefull, new AIPath(outPath));
1216+
Paths.Add(formedpathFilefull, outPath);
12171217
binaryloaded = true;
12181218
}
12191219
}
@@ -1235,7 +1235,7 @@ public AIPath LoadPath(string pathstring, out bool validPath)
12351235
{
12361236
try
12371237
{
1238-
Paths.Add(formedpathFilefull, new AIPath(outPath));
1238+
Paths.Add(formedpathFilefull, outPath);
12391239
}
12401240
catch (Exception e)
12411241
{

0 commit comments

Comments
 (0)