Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete binary paths if the ascii version or the tdb was modified. Blueprint: https://blueprints.launchpad.net/or/+spec/binary-timetable-paths #538

Merged
merged 4 commits into from Dec 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions Source/Documentation/Manual/timetable.rst
Expand Up @@ -303,11 +303,9 @@ Special Rows
will read that path. If not, it will read the 'normal' path, and will then
store this as binary for future use. Binary paths are stored in a subdirectory
named ``OpenRails`` which must be created in the Paths directory of the route.

**Important:**

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

- ``#consist`` row

Expand Down
7 changes: 7 additions & 0 deletions Source/Orts.Formats.Msts/TrackDatabaseFile.cs
Expand Up @@ -39,6 +39,11 @@ public class TrackDatabaseFile
/// </summary>
public TrackDB TrackDB { get; set; }

/// <summary>
/// The .tdb file last modified time is stored for being able to validate derived files such as binary paths.
/// </summary>
public DateTime LastWriteTime { get; }

/// <summary>
/// Constructor from file
/// </summary>
Expand All @@ -49,6 +54,8 @@ public TrackDatabaseFile(string filenamewithpath)
stf.ParseFile(new STFReader.TokenProcessor[] {
new STFReader.TokenProcessor("trackdb", ()=>{ TrackDB = new TrackDB(stf); }),
});

LastWriteTime = File.GetLastWriteTime(filenamewithpath);
}

/// <summary>
Expand Down
28 changes: 14 additions & 14 deletions Source/Orts.Simulation/Simulation/Timetables/ProcessTimetable.cs
Expand Up @@ -1160,7 +1160,7 @@ public bool PreProcessRoutes(CancellationToken cancellation)
{
// read route
bool pathValid = true;
AIPath newPath = LoadPath(thisRoute, out pathValid);
LoadPath(thisRoute, out pathValid);
if (!pathValid) allPathsLoaded = false;
if (cancellation.IsCancellationRequested)
return (false);
Expand All @@ -1187,23 +1187,23 @@ public AIPath LoadPath(string pathstring, out bool validPath)
if (String.IsNullOrEmpty(pathExtension))
formedpathFilefull = Path.ChangeExtension(formedpathFilefull, "pat");

// try to load binary path if required
bool binaryloaded = false;
AIPath outPath = null;

if (Paths.ContainsKey(formedpathFilefull))
{
outPath = new AIPath(Paths[formedpathFilefull]);
}
else
if (!Paths.TryGetValue(formedpathFilefull, out var outPath))
{
// try to load binary path if required
bool binaryloaded = false;
string formedpathFilefullBinary = Path.Combine(Path.GetDirectoryName(formedpathFilefull), "OpenRails");
formedpathFilefullBinary = Path.Combine(formedpathFilefullBinary, Path.GetFileNameWithoutExtension(formedpathFilefull));
formedpathFilefullBinary = Path.ChangeExtension(formedpathFilefullBinary, "or-binpat");

if (BinaryPaths)
if (BinaryPaths && File.Exists(formedpathFilefullBinary))
{
if (File.Exists(formedpathFilefullBinary))
var binaryLastWriteTime = File.GetLastWriteTime(formedpathFilefullBinary);
if (binaryLastWriteTime < simulator.TDB.LastWriteTime ||
File.Exists(formedpathFilefull) && binaryLastWriteTime < File.GetLastWriteTime(formedpathFilefull))
{
File.Delete(formedpathFilefullBinary);
}
else
{
try
{
Expand All @@ -1213,7 +1213,7 @@ public AIPath LoadPath(string pathstring, out bool validPath)

if (outPath.Nodes != null)
{
Paths.Add(formedpathFilefull, new AIPath(outPath));
Paths.Add(formedpathFilefull, outPath);
binaryloaded = true;
}
}
Expand All @@ -1235,7 +1235,7 @@ public AIPath LoadPath(string pathstring, out bool validPath)
{
try
{
Paths.Add(formedpathFilefull, new AIPath(outPath));
Paths.Add(formedpathFilefull, outPath);
}
catch (Exception e)
{
Expand Down