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);
/// The black box of a run: the whole session in one file, a new one each run.
public sealed record BlackBoxRecording(string Path, DateTime LastWriteUtc, long Size);
///
/// 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.
///
public sealed record TapeSession(TapeLaunch? Launch, DateTime StartedUtc, DateTime LastWrittenUtc, bool Running,
IReadOnlyList Tapes, IReadOnlyList BlackBoxes);
/// 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.
public sealed record TapeContext(TimeSpan? StartClock, RecorderKind Recorder,
string? Scenery = null, string? Train = null);
/// The tapes the simulator left in physicslog/ - one per driven vehicle, overwritten every run - and the black boxes, one a run.
public sealed class HaslerTapes
{
private const int RememberedLaunches = 30;
private static readonly TimeSpan QuietBetweenRuns = TimeSpan.FromMinutes(2);
private readonly IFileSystem _files;
private readonly IGamePaths _paths;
private readonly IDiagnosticsLog _log;
private readonly IClock _clock;
private readonly TimetableLocator _timetables;
private readonly Func _simulatorRunning;
private readonly object _playerLock = new();
private IReadOnlyList? _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 simulatorRunning)
{
_files = files;
_paths = paths;
_log = log;
_clock = clock;
_timetables = timetables;
_simulatorRunning = simulatorRunning;
}
public string Folder => _paths.FromRoot("physicslog");
/// Not a .dat, so the simulator's own logs are never mistaken for it, nor it for one.
private string LaunchFile => Path.Combine(Folder, "starter-launch.txt");
///
/// 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.
///
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();
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));
}
/// Notes down, next to the logs the simulator is about to write, what only the starter knows.
public void RecordLaunch(string scenery, TimeSpan startClock, IEnumerable vehicles,
string? driven = null, IEnumerable? 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);
}
}
///
/// 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.
///
public void FollowLastRun()
{
if (Launches().LastOrDefault() is { } launch)
PlayerIn(launch);
}
///
/// 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.
///
private IReadOnlyList Launches()
{
lock (_playerLock)
{
try
{
if (!_files.FileExists(LaunchFile))
return Array.Empty();
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();
}
}
}
/// The simulator's log of this launch: log.txt or, with logs kept, one of its own in logs/.
private string? LogOf(TapeLaunch launch)
{
var logs = new List { _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();
}
///
/// 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.
///
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;
}
}
/// The vehicle the player is in, or went off in while the log has not said otherwise, if the starter launched one.
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);
}
/// Every tape, session by session.
public IReadOnlyList List() => Sessions().SelectMany(session => session.Tapes).ToList();
///
/// 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 those written about the same
/// time, as a run writes them all at its end.
///
public IReadOnlyList Sessions()
{
try
{
var launches = Launches();
var tapes = _files.DirectoryExists(Folder)
? _files.GetFiles(Folder, "*.dat").Select(ReadTape).ToList()
: new List();
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();
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())
.Select(t => t with { Role = launch.RoleOf(t.Vehicle, player) }),
boxesBehind.GetValueOrDefault(launch, new List())));
}
// What no launch covers goes together by when it was written: a run writes it all at its end.
var loose = tapes.Where(t => tapeLaunch[t] is null)
.Select(t => (Written: t.LastWriteUtc, Tape: (TapeFile?)t, Box: (BlackBoxRecording?)null))
.Concat(boxes.Where(b => LaunchOf(b, launches) is null)
.Select(b => (Written: b.LastWriteUtc, Tape: (TapeFile?)null, Box: (BlackBoxRecording?)b)))
.OrderBy(file => file.Written);
var run = new List();
var runBoxes = new List();
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();
runBoxes = new List();
}
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();
}
}
private TapeFile ReadTape(string path) =>
new(path, Path.GetFileNameWithoutExtension(path), _files.GetLastWriteTimeUtc(path), _files.GetFileSize(path));
private List ReadBlackBoxes() =>
_files.DirectoryExists(Folder)
? _files.GetFiles(Folder, "*.blackbox").Select(ReadBlackBox).ToList()
: new List();
///
/// 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.
///
public static TapeLaunch? LaunchOf(TapeFile tape, IReadOnlyList launches, IReadOnlyList 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;
}
///
/// 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.
///
public static TapeLaunch? LaunchOf(BlackBoxRecording box, IReadOnlyList 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;
}
///
/// 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.
///
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";
///
/// The scenery and the start a black box is named by, scenery_YYYY-MM-DD_HH-MM-SS in the local time
/// of the computer that ran it; null for a name not made so.
///
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());
}
/// A scenery's file as the simulator names its black box: no '$' in front, but for ASCII letters, digits, '-' and '_' all '_'.
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));
/// The player's own tapes first: every manned vehicle leaves one.
private static TapeSession Session(TapeLaunch? launch, bool running, IEnumerable tapes,
IEnumerable 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);
}
public HaslerTape? Load(TapeFile file)
{
try
{
// Through OpenRead, which does not mind the simulator still writing the file.
using var stream = _files.OpenRead(file.Path);
using var reader = new StreamReader(stream, Encoding.ASCII);
return HaslerTape.Parse(reader.ReadToEnd());
}
catch (Exception ex)
{
_log.Log($"reading {file.Path}", ex);
return null;
}
}
}