Files
StarterNG/StarterNG/Application/HaslerTapes.cs
maj00r 76693c1494 fix(settings): an imported ini moved to the trains' tapes too, a closed tape window forgets only its own tape
- ImportFrom moves an ini of before the trains' tapes as a profile is
- HaslerTapes.Forget takes the file: a window closing lets go of its own tape, not of the one
  another window follows
2026-11-16 00:09:36 +01:00

538 lines
25 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using StarterNG.Application.Abstractions;
using StarterNG.Classes;
using StarterNG.Domain.Sceneries;
using StarterNG.Domain.Tapes;
using StarterNG.Infrastructure.Adapters;
using StarterNG.Infrastructure.Sceneries;
namespace StarterNG.Application;
public sealed record TapeFile(string Path, string Vehicle, DateTime LastWriteUtc, long Size,
TapeRole Role = TapeRole.Other);
/// <summary>The black box of a run: the whole session in one file, a new one each run.</summary>
public sealed record BlackBoxRecording(string Path, DateTime LastWriteUtc, long Size);
/// <summary>
/// One run of the simulator and what is left of its tapes: a later run overwrites those of the
/// vehicles it has too. Its black box stays, newest first should there be more.
/// </summary>
public sealed record TapeSession(TapeLaunch? Launch, DateTime StartedUtc, DateTime LastWrittenUtc, bool Running,
IReadOnlyList<TapeFile> Tapes, IReadOnlyList<BlackBoxRecording> BlackBoxes);
/// <summary>What is known about a tape beyond its samples: the clock it started at, the recorder it is read as, the scenario and the train.</summary>
public sealed record TapeContext(TimeSpan? StartClock, RecorderKind Recorder,
string? Scenery = null, string? Train = null);
/// <summary>The tapes the simulator left in physicslog/ - one per driven vehicle, overwritten every run - and the black boxes, one a run.</summary>
public sealed class HaslerTapes
{
private const int RememberedLaunches = 30;
private static readonly TimeSpan QuietBetweenRuns = TimeSpan.FromMinutes(2);
private static readonly TimeSpan TapesAfterBlackBox = TimeSpan.FromSeconds(10);
private readonly IFileSystem _files;
private readonly IGamePaths _paths;
private readonly IDiagnosticsLog _log;
private readonly IClock _clock;
private readonly TimetableLocator _timetables;
private readonly Func<bool> _simulatorRunning;
private readonly object _playerLock = new();
private IReadOnlyList<TapeLaunch>? _launches;
private (DateTime Written, long Size) _launchesAt;
private PlayerVehicles _player = PlayerVehicles.None;
private DateTime _playerLaunch;
private string? _playerLog;
private long _playerLogRead;
public HaslerTapes(IFileSystem files, IGamePaths paths, IDiagnosticsLog log, IClock clock,
TimetableLocator timetables, Func<bool> simulatorRunning)
{
_files = files;
_paths = paths;
_log = log;
_clock = clock;
_timetables = timetables;
_simulatorRunning = simulatorRunning;
}
public string Folder => _paths.FromRoot("physicslog");
/// <summary>Not a .dat, so the simulator's own logs are never mistaken for it, nor it for one.</summary>
private string LaunchFile => Path.Combine(Folder, "starter-launch.txt");
/// <summary>
/// The simulator logs every driven vehicle under its scenery name, with the
/// time counted from the start; the clock it starts at and what each name is
/// are only known here. So is whose tape is the player's: a scenery with no
/// consist picked starts with the free camera, in no vehicle at all.
/// </summary>
public void RecordLaunch(Scenery scenery, Trainset? trainset, string? driven)
{
var clock = TimeSpan.TryParseExact(scenery.Weather.ScenarioTimeOverride, @"h\:mm",
CultureInfo.InvariantCulture, out var over)
? over
: TimeSpan.TryParseExact(scenery.Weather.Time, @"h\:mm", CultureInfo.InvariantCulture, out var time)
? time
: TimeSpan.Zero;
var own = trainset?.Vehicles ?? new List<Dynamic>();
var vehicles = scenery.Trainsets.SelectMany(t => t.Vehicles).Concat(own)
.Select(v => new TapeVehicle(v.Name, v.DataFolder ?? "", v.MmdFile ?? ""));
RecordLaunch(Path.GetFileName(scenery.Path), clock, vehicles, driven,
driven is null ? null : own.Select(v => v.Name), scenery.Name,
driven is null ? null : _timetables.TrainNumber(scenery, trainset?.Name));
}
/// <summary>Notes down, next to the logs the simulator is about to write, what only the starter knows.</summary>
public void RecordLaunch(string scenery, TimeSpan startClock, IEnumerable<TapeVehicle> vehicles,
string? driven = null, IEnumerable<string>? consist = null,
string? sceneryName = null, string? train = null)
{
var launch = new TapeLaunch(_clock.UtcNow, scenery, startClock,
vehicles.Where(v => !string.IsNullOrEmpty(v.Name))
.DistinctBy(v => v.Name, StringComparer.OrdinalIgnoreCase)
.ToList(),
driven, consist?.ToList(), sceneryName, train);
try
{
var noted = Launches().ToList();
// The log is about to begin anew: what it says of the run before stays with that run.
if (noted.Count > 0)
noted[^1] = noted[^1] with { Player = PlayerIn(noted[^1]) };
noted.Add(launch);
_files.CreateDirectory(Folder);
_files.WriteAllTextAtomic(LaunchFile,
string.Concat(noted.TakeLast(RememberedLaunches).Select(l => l.Format())));
lock (_playerLock)
_launches = null;
}
catch (Exception ex)
{
_log.Log($"writing {LaunchFile}", ex);
}
}
/// <summary>
/// Reads what the log of the last launch has gained since the last look. A start
/// notes that down before the log begins anew, and after a long run, with nothing
/// read of it yet, it would wait on the whole log.
/// </summary>
public void FollowLastRun()
{
if (Launches().LastOrDefault() is { } launch)
PlayerIn(launch);
}
/// <summary>
/// The launches noted so far, oldest first. Read again only when the file has
/// changed: this runs every few seconds next to a running simulator.
/// </summary>
private IReadOnlyList<TapeLaunch> Launches()
{
lock (_playerLock)
{
try
{
if (!_files.FileExists(LaunchFile))
return Array.Empty<TapeLaunch>();
var at = (_files.GetLastWriteTimeUtc(LaunchFile), _files.GetFileSize(LaunchFile));
if (_launches is null || at != _launchesAt)
{
_launches = TapeLaunch.ParseAll(_files.ReadAllText(LaunchFile, Encoding.UTF8));
_launchesAt = at;
}
return _launches;
}
catch (Exception ex)
{
_log.Log($"reading {LaunchFile}", ex);
return Array.Empty<TapeLaunch>();
}
}
}
/// <summary>The simulator's log of this launch: log.txt or, with logs kept, one of its own in logs/.</summary>
private string? LogOf(TapeLaunch launch)
{
var logs = new List<string> { _paths.FromRoot("log.txt") };
string kept = _paths.FromRoot("logs");
if (_files.DirectoryExists(kept))
logs.AddRange(_files.GetFiles(kept, "log (*.txt"));
return logs.Where(_files.FileExists)
.Where(log => launch.Covers(_files.GetLastWriteTimeUtc(log)))
.OrderByDescending(_files.GetLastWriteTimeUtc)
.FirstOrDefault();
}
/// <summary>
/// Where the player has been in this launch. Only what the log has gained since
/// the last look is read: this runs every few seconds next to a running simulator.
/// </summary>
private PlayerVehicles PlayerIn(TapeLaunch launch)
{
lock (_playerLock)
{
string? path = null;
try
{
path = LogOf(launch);
if (path is null)
return PlayerVehicles.Unknown;
long size = _files.GetFileSize(path);
if (launch.LaunchedUtc != _playerLaunch || path != _playerLog || size < _playerLogRead)
{
_player = PlayerVehicles.None;
_playerLaunch = launch.LaunchedUtc;
_playerLog = path;
_playerLogRead = 0;
}
if (size == _playerLogRead)
return _player;
using var stream = _files.OpenRead(path);
stream.Seek(_playerLogRead, SeekOrigin.Begin);
using var added = new MemoryStream();
stream.CopyTo(added);
byte[] bytes = added.ToArray();
// A line still being written waits for the next look.
int end = Array.LastIndexOf(bytes, (byte)'\n') + 1;
if (end > 0)
{
_player = _player.After(LegacyText.Decode(bytes[..end]));
_playerLogRead += end;
}
}
catch (Exception ex)
{
_log.Log($"reading {path}", ex);
}
return _player;
}
}
/// <summary>The vehicle the player is in, or went off in while the log has not said otherwise, if the starter launched one.</summary>
public string? DrivenVehicle =>
Launches().LastOrDefault() is { } launch ? PlayerIn(launch).Current ?? launch.Driven : null;
public TapeContext ContextOf(TapeFile file)
{
var launch = LaunchOf(file, Launches(), ReadBlackBoxes());
if (launch is null)
return new TapeContext(null, Recorders.Guess(null, file.Vehicle));
var vehicle = launch.Find(file.Vehicle);
string scenery = string.IsNullOrWhiteSpace(launch.SceneryName) ? launch.Scenery : launch.SceneryName;
return new TapeContext(launch.StartClock, Recorders.Guess(vehicle, file.Vehicle),
string.IsNullOrWhiteSpace(scenery) ? null : scenery,
launch.RoleOf(file.Vehicle) == TapeRole.Other ? null : launch.Train);
}
/// <summary>Every tape, session by session.</summary>
public IReadOnlyList<TapeFile> List() => Sessions().SelectMany(session => session.Tapes).ToList();
/// <summary>
/// The runs the tapes came from, the running or latest one first. A tape belongs
/// to the last launch before it was written, unless a run the starter didn't launch
/// began between them; one with no launch goes with the black box recording when it
/// was written, or else with those written about the same time, as a run writes its
/// logs until it ends.
/// </summary>
public IReadOnlyList<TapeSession> Sessions()
{
try
{
var launches = Launches();
var tapes = _files.DirectoryExists(Folder)
? _files.GetFiles(Folder, "*.dat").Select(ReadTape).ToList()
: new List<TapeFile>();
var boxes = ReadBlackBoxes();
var tapeLaunch = tapes.ToDictionary(tape => tape, tape => LaunchOf(tape, launches, boxes));
var behind = tapes.GroupBy(tape => tapeLaunch[tape])
.Where(group => group.Key is not null)
.ToDictionary(group => group.Key!, group => group.ToList());
var boxesBehind = boxes.GroupBy(box => LaunchOf(box, launches))
.Where(group => group.Key is not null)
.ToDictionary(group => group.Key!, group => group.ToList());
var sessions = new List<TapeSession>();
foreach (var launch in launches)
{
bool last = ReferenceEquals(launch, launches[^1]);
bool running = last && _simulatorRunning();
if (!running && !behind.ContainsKey(launch) && !boxesBehind.ContainsKey(launch))
continue;
var player = last ? PlayerIn(launch) : launch.Player ?? PlayerVehicles.Unknown;
sessions.Add(Session(launch, running, behind.GetValueOrDefault(launch, new List<TapeFile>())
.Select(t => t with { Role = launch.RoleOf(t.Vehicle, player) }),
boxesBehind.GetValueOrDefault(launch, new List<BlackBoxRecording>())));
}
// What no launch covers goes together by when it was written: a run writes its logs until it ends. A tape
// written while a black box recorded is of its run, though: the black box writes the tapes of the player's
// vehicles as it goes, and that of a vehicle the player left ends before the run does.
var looseBoxes = boxes.Where(b => LaunchOf(b, launches) is null).ToList();
var recordings = Recordings(looseBoxes);
var loose = tapes.Where(t => tapeLaunch[t] is null)
.Select(t => (Written: RecordingAt(t.LastWriteUtc, recordings)?.LastWriteUtc ?? t.LastWriteUtc,
Tape: (TapeFile?)t, Box: (BlackBoxRecording?)null))
.Concat(looseBoxes.Select(b => (Written: b.LastWriteUtc, Tape: (TapeFile?)null, Box: (BlackBoxRecording?)b)))
.OrderBy(file => file.Written);
var run = new List<TapeFile>();
var runBoxes = new List<BlackBoxRecording>();
DateTime? lastWritten = null;
foreach (var (written, tape, box) in loose)
{
if (lastWritten is { } before && written - before > QuietBetweenRuns)
{
sessions.Add(Session(null, running: false, run, runBoxes));
run = new List<TapeFile>();
runBoxes = new List<BlackBoxRecording>();
}
if (tape is not null)
run.Add(tape);
if (box is not null)
runBoxes.Add(box);
lastWritten = written;
}
if (lastWritten is not null)
sessions.Add(Session(null, running: false, run, runBoxes));
return sessions.OrderByDescending(session => session.Running)
.ThenByDescending(session => session.LastWrittenUtc)
.ToList();
}
catch (Exception ex)
{
_log.Log($"listing {Folder}", ex);
return Array.Empty<TapeSession>();
}
}
/// <summary>
/// When each black box recorded, its name read once: from the start its name has, to when it was last written,
/// and the tapes a moment after. No longer than until the next one started, though: a box downloaded or copied
/// later was last written then, not when it stopped, and would take in the tapes of the runs since.
/// </summary>
public static IReadOnlyList<(BlackBoxRecording Box, DateTime FromUtc, DateTime ToUtc)> Recordings(IEnumerable<BlackBoxRecording> boxes)
{
var named = boxes.Select(box => (Box: box, Named: NamedBy(box.Path)))
.Where(each => each.Named is not null)
.Select(each => (each.Box, FromUtc: each.Named!.Value.StartedUtc))
.OrderBy(each => each.FromUtc)
.ToList();
var recordings = new List<(BlackBoxRecording, DateTime, DateTime)>(named.Count);
for (int i = 0; i < named.Count; i++)
{
var to = named[i].Box.LastWriteUtc + TapesAfterBlackBox;
if (i + 1 < named.Count && named[i + 1].FromUtc < to)
to = named[i + 1].FromUtc;
recordings.Add((named[i].Box, named[i].FromUtc, to));
}
return recordings;
}
/// <summary>
/// The black box that was recording at the moment, the tapes being written last, a moment after it is closed.
/// Of more, the one that stopped nearest to it.
/// </summary>
public static BlackBoxRecording? RecordingAt(DateTime momentUtc, IEnumerable<(BlackBoxRecording Box, DateTime FromUtc, DateTime ToUtc)> recordings) =>
recordings.Where(each => each.FromUtc <= momentUtc && momentUtc <= each.ToUtc)
.OrderBy(each => (each.Box.LastWriteUtc - momentUtc).Duration())
.Select(each => each.Box)
.FirstOrDefault();
private TapeFile ReadTape(string path) =>
new(path, Path.GetFileNameWithoutExtension(path), _files.GetLastWriteTimeUtc(path), _files.GetFileSize(path));
private List<BlackBoxRecording> ReadBlackBoxes() =>
_files.DirectoryExists(Folder)
? _files.GetFiles(Folder, "*.blackbox").Select(ReadBlackBox).ToList()
: new List<BlackBoxRecording>();
/// <summary>
/// The launch a tape came from. A tape tells nothing of its run but when it was written: the last launch
/// before then, unless a black box shows another run, one the starter didn't launch, began between the two.
/// That run wrote the tape, not the launch's.
/// </summary>
public static TapeLaunch? LaunchOf(TapeFile tape, IReadOnlyList<TapeLaunch> launches, IReadOnlyList<BlackBoxRecording> boxes)
{
var launch = launches.LastOrDefault(l => l.Covers(tape.LastWriteUtc));
if (launch is null)
return null;
bool another = boxes.Any(box => NamedBy(box.Path) is { } named
&& named.StartedUtc > launch.LaunchedUtc && named.StartedUtc < tape.LastWriteUtc
&& LaunchOf(box, launches) != launch);
return another ? null : launch;
}
/// <summary>
/// The launch a black box came from. Unlike a tape, it says which: its name has the scenery and the moment
/// the recording started. So it is the last launch before that moment, and only if that launch was of the
/// same scenery; one the starter didn't launch, copied in or started on its own, goes with no launch.
/// </summary>
public static TapeLaunch? LaunchOf(BlackBoxRecording box, IReadOnlyList<TapeLaunch> launches)
{
if (NamedBy(box.Path) is not { } named)
return launches.LastOrDefault(l => l.Covers(box.LastWriteUtc));
var launch = launches.LastOrDefault(l => l.Covers(named.StartedUtc));
return launch is not null && SameStem(SceneryStem(launch.Scenery), named.Scenery) ? launch : null;
}
/// <summary>
/// Two stems of one scenery. The simulator turns each byte of a letter outside ASCII into '_': one for 'ł'
/// on Windows, two where the name comes as UTF-8. Runs of '_' are taken as one, and the case doesn't count.
/// </summary>
public static bool SameStem(string a, string b) =>
string.Equals(Squeezed(a), Squeezed(b), StringComparison.OrdinalIgnoreCase);
private static string Squeezed(string stem)
{
var text = new StringBuilder(stem.Length);
foreach (char c in stem)
if (c != '_' || text.Length == 0 || text[^1] != '_')
text.Append(c);
return text.ToString();
}
private const string BlackBoxStamp = "yyyy-MM-dd_HH-mm-ss";
/// <summary>
/// The scenery and the start a black box is named by, <c>scenery_YYYY-MM-DD_HH-MM-SS</c> in the local time
/// of the computer that ran it; null for a name not made so.
/// </summary>
public static (string Scenery, DateTime StartedUtc)? NamedBy(string path)
{
string name = Path.GetFileNameWithoutExtension(path);
int stamp = BlackBoxStamp.Length;
if (name.Length < stamp + 2 || name[^(stamp + 1)] != '_'
|| !DateTime.TryParseExact(name[^stamp..], BlackBoxStamp, CultureInfo.InvariantCulture,
DateTimeStyles.AssumeLocal, out var local))
return null;
return (name[..^(stamp + 1)], local.ToUniversalTime());
}
/// <summary>A scenery's file as the simulator names its black box: no '$' in front, but for ASCII letters, digits, '-' and '_' all '_'.</summary>
public static string SceneryStem(string scenery)
{
string stem = Path.GetFileNameWithoutExtension(scenery);
if (stem.StartsWith('$'))
stem = stem[1..];
var text = new StringBuilder(stem.Length);
foreach (char c in stem)
text.Append(char.IsAsciiLetterOrDigit(c) || c is '-' or '_' ? c : '_');
return text.Length == 0 ? "scenery" : text.ToString();
}
private BlackBoxRecording ReadBlackBox(string path) =>
new(path, _files.GetLastWriteTimeUtc(path), _files.GetFileSize(path));
/// <summary>The player's own tapes first: every manned vehicle leaves one.</summary>
private static TapeSession Session(TapeLaunch? launch, bool running, IEnumerable<TapeFile> tapes,
IEnumerable<BlackBoxRecording> boxes)
{
var listed = tapes.OrderByDescending(tape => tape.Role).ThenByDescending(tape => tape.LastWriteUtc).ToList();
var recorded = boxes.OrderByDescending(box => box.LastWriteUtc).ToList();
var written = listed.Select(tape => tape.LastWriteUtc).Concat(recorded.Select(box => box.LastWriteUtc)).ToList();
var started = launch?.LaunchedUtc ?? written.Min();
return new TapeSession(launch, started, written.Count > 0 ? written.Max() : started, running, listed, recorded);
}
// the tape followed as the simulator writes it: what of its file was read, and where it ended
private readonly object _followedLock = new();
private (string Path, long Read, byte[] Tail, HaslerTape.Reader Reader)? _followed;
// what of the end of the read part is kept, to tell the file still has it: one begun anew doesn't
private const int FollowedTail = 256;
public HaslerTape? Load(TapeFile file) => Load(file, follow: false);
/// <summary>
/// The tape of this file no longer followed: what was kept of it is let go. Another file's, followed for another
/// window, stays.
/// </summary>
public void Forget(string path)
{
lock (_followedLock)
if (_followed?.Path == path)
_followed = null;
}
// what the file has between the two: less, up to its end, when it was cut shorter since its length was taken
private static byte[] Bytes(Stream stream, long from, long to)
{
stream.Seek(from, SeekOrigin.Begin);
var bytes = new byte[to - from];
int read = stream.ReadAtLeast(bytes, bytes.Length, throwOnEndOfStream: false);
return read == bytes.Length ? bytes : bytes[..read];
}
// the end of what was read, after the part added to it
private static byte[] Tail(byte[] before, byte[] added)
{
if (added.Length >= FollowedTail)
return added[^FollowedTail..];
var keptBefore = Math.Min(before.Length, FollowedTail - added.Length);
return [.. before[^keptBefore..], .. added];
}
/// <summary>
/// The tape of the file. Followed, the one read last and grown since: only what was added is read, so a log
/// written fast, at a higher time speed, is kept up with; a line not ended yet waits for the next time. Not
/// followed, or another file, or one shorter than was read - begun anew - it is read whole, and kept to be
/// followed from there.
/// </summary>
public HaslerTape? Load(TapeFile file, bool follow)
{
lock (_followedLock)
{
try
{
// Through OpenRead, which does not mind the simulator still writing the file.
using var stream = _files.OpenRead(file.Path);
if (!stream.CanSeek)
{
_followed = null;
using var whole = new StreamReader(stream, Encoding.ASCII);
return HaslerTape.Parse(whole.ReadToEnd());
}
long length = stream.Length;
// the same file grown, its end as it was read: else another, or begun anew, read whole
if (!follow || _followed is not { } followed || followed.Path != file.Path || length < followed.Read
|| !Bytes(stream, followed.Read - followed.Tail.Length, followed.Read).AsSpan().SequenceEqual(followed.Tail))
followed = (file.Path, 0, Array.Empty<byte>(), new HaslerTape.Reader());
// what was read, not the length taken before: the file may be cut shorter meanwhile
var added = Bytes(stream, followed.Read, length);
followed.Reader.Feed(Encoding.ASCII.GetString(added));
// the time went back in what was added: the log begun anew and already longer than was read
if (followed.Read > 0 && followed.Reader.WentBack)
{
followed = (file.Path, 0, Array.Empty<byte>(), new HaslerTape.Reader());
added = Bytes(stream, 0, length);
followed.Reader.Feed(Encoding.ASCII.GetString(added));
}
_followed = (file.Path, followed.Read + added.Length, Tail(followed.Tail, added), followed.Reader);
return followed.Reader.Tape(final: !follow);
}
catch (Exception ex)
{
_followed = null;
_log.Log($"reading {file.Path}", ex);
return null;
}
}
}
}