Keep scenery files intact on save and follow them on disk

Saves keep comments and every byte they do not change. Faults in a scenery's
text are found and described. Files that change while the starter runs are
picked up without a manual reload.
This commit is contained in:
maj00r
2026-11-01 16:06:15 +01:00
parent b6735fe345
commit 69aa075bfa
30 changed files with 3596 additions and 215 deletions

View File

@@ -5,7 +5,6 @@ using System.Linq;
using System.Text.RegularExpressions;
using StarterNG.Application.Abstractions;
using StarterNG.Classes;
using StarterNG.Domain;
using StarterNG.Domain.Sceneries;
namespace StarterNG.Infrastructure.Sceneries;
@@ -21,58 +20,97 @@ public sealed class SceneryParser
_random = random;
}
public Scenery Parse(string path, string content)
/// <param name="findInclude">
/// What the simulator makes of an include, as the include names it; null leaves
/// includes unchecked.
/// </param>
public Scenery Parse(string path, string content, Func<string, IncludeLookup>? findInclude = null)
{
string original = content;
var faults = new List<string>();
var attachments = ParseAttachments(original);
// A file put in place of its include has directives of its own, for itself.
string own = SceneryIncludes.WithoutInlined(original);
var attachments = ParseAttachments(own);
var includes = new List<SceneryInclude>();
content = StripOptionalIncludes(content, includes);
var trainsetEntries = new List<string>();
content = Regex.Replace(content, @"trainset\b[\s\S]*?\bendtrainset\b",
match =>
{
trainsetEntries.Add(match.Value);
return $"{{{{{trainsetEntries.Count - 1}}}}}";
},
RegexOptions.IgnoreCase);
// Read from the text with its optional includes blanked, which keeps every
// place where it was, so a fault's line is the line in the file.
var syntax = ScenerySyntax.Scan(content, System.IO.Path.GetFileName(path), findInclude);
var faults = syntax.Faults.ToList();
var inlined = SceneryIncludes.Ranges(content);
var trainsets = new List<Trainset>();
var looseVehicles = new List<Dynamic>();
var looseNames = new List<string>();
content = ExtractLooseVehicles(content, looseVehicles, looseNames, faults);
var cuts = new List<(int Start, int End, string With)>();
var scenery = new Scenery(path, content)
foreach (var (start, end) in syntax.Trainsets)
{
Group = MatchDirective(original, "l"),
Name = MatchDirective(original, "n"),
Description = MatchAllDirectives(original, "d"),
ImageName = MatchDirective(original, "i"),
Archival = MatchDirective(original, "a") != null
var trainset = new Trainset(content[start..end]) { SourceFile = SceneryIncludes.FileAt(inlined, start) };
if (!trainset.Parsed && !faults.Any(f => f.Offset >= start && f.Offset < end))
faults.Add(syntax.Lines.Fault(SceneryFaultKind.TrainsetUnreadable, start, trainset.Name));
cuts.Add((start, end, $"{{{{{trainsets.Count}}}}}"));
trainsets.Add(trainset);
}
foreach (var (start, end) in syntax.LooseVehicles)
{
// Included files' loose vehicles - road traffic, mostly - stay where they
// are as written, only their names are noted.
var vehicle = ReadLooseVehicle(content[start..end]);
looseNames.Add(vehicle?.Name ?? LooseName(content[start..end]));
if (SceneryIncludes.FileAt(inlined, start) != null)
continue;
if (vehicle is null)
{
if (!faults.Any(f => f.Offset >= start && f.Offset < end))
faults.Add(syntax.Lines.Fault(SceneryFaultKind.VehicleUnreadable, start, LooseName(content[start..end])));
continue;
}
looseVehicles.Add(vehicle);
cuts.Add((start, end, "\n"));
}
var scenery = new Scenery(path, Cut(content, cuts))
{
Group = MatchDirective(own, "l"),
Name = MatchDirective(own, "n"),
Description = MatchAllDirectives(own, "d"),
ImageName = MatchDirective(own, "i"),
Archival = MatchDirective(own, "a") != null
};
scenery.Attachments.AddRange(attachments);
scenery.Includes.AddRange(includes);
scenery.Trainsets.AddRange(trainsets);
scenery.LooseVehicles.AddRange(looseVehicles);
scenery.LooseVehicleNames.AddRange(looseNames);
scenery.Faults.AddRange(faults);
scenery.Faults.AddRange(faults.OrderBy(f => f.Offset));
ParseWeather(original, scenery.Weather);
// Without its comments: what is read here is what the writer later puts down as active.
ParseWeather(SceneryCode.Mask(original, out _), scenery.Weather);
scenery.Weather.AcceptAsAuthored();
foreach (string entry in trainsetEntries)
{
var trainset = new Trainset(entry);
scenery.Trainsets.Add(trainset);
if (!trainset.Parsed)
scenery.Faults.Add($"# trainset parse fault: {trainset.Name} ({trainset.Track})");
}
return scenery;
}
private static string Cut(string text, List<(int Start, int End, string With)> cuts)
{
var sb = new System.Text.StringBuilder(text.Length);
int at = 0;
foreach (var (start, end, with) in cuts.OrderBy(c => c.Start))
{
sb.Append(text, at, start - at).Append(with);
at = end;
}
return sb.Append(text, at, text.Length - at).ToString();
}
private static List<SceneryAttachment> ParseAttachments(string content)
{
var attachments = new List<SceneryAttachment>();
@@ -115,7 +153,7 @@ public sealed class SceneryParser
return Regex.Replace(
content,
@"include\s+(\S+(?:\s+\S+)*?)\s+end\s*//[^\r\n]*\$optional([^\r\n]*)\r?\n([\s\S]*?)endoptional\b",
@"include[ \t]+(\S+(?:[ \t]+\S+)*?)[ \t]+end[ \t]*//[^\r\n]*\$optional([^\r\n]*)\r?\n([\s\S]*?)endoptional\b",
m =>
{
string includePath = Regex.Replace(m.Groups[1].Value.Trim(), @"\s+", " ");
@@ -137,65 +175,55 @@ public sealed class SceneryParser
Kind = kind,
Selected = kind == 1
});
return " ";
return Regex.Replace(m.Value, @"[^\r\n]", " ");
},
RegexOptions.IgnoreCase);
}
private static string ExtractLooseVehicles(string content, List<Dynamic> vehicles, List<string> names,
List<string> faults)
private static readonly Regex LooseNode = new(
@"(?is)^node\s+(\S+)\s+(\S+)\s+(\S+)\s+dynamic\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)((?:\s+(?!enddynamic\b)\S+)*)\s*enddynamic$");
private static string LooseName(string node) =>
SceneryCode.Tokens(SceneryCode.Mask(node, out _)).ElementAtOrDefault(3).Text ?? "";
/// <summary>A vehicle standing on its own, or null when its node cannot be read.</summary>
private static Dynamic? ReadLooseVehicle(string node)
{
var inv = CultureInfo.InvariantCulture;
var m = LooseNode.Match(SceneryCode.Mask(node, out _));
if (!m.Success)
return null;
return Regex.Replace(
content,
@"(?is)\bnode\s+(\S+)\s+(\S+)\s+(\S+)\s+dynamic\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)((?:\s+(?!enddynamic)\S+)*)\s*enddynamic\b",
m =>
try
{
var vehicle = new Dynamic
{
string name = m.Groups[3].Value;
RangeMax = float.Parse(m.Groups[1].Value, inv),
RangeMin = float.Parse(m.Groups[2].Value, inv),
Name = m.Groups[3].Value,
DataFolder = m.Groups[4].Value,
SkinFile = Dynamic.StripSkinExtension(m.Groups[5].Value),
MmdFile = m.Groups[6].Value,
PathName = m.Groups[7].Value,
Offset = float.Parse(m.Groups[8].Value, inv),
DriverType = Dynamic.ParseDriver(m.Groups[9].Value),
HasVelocity = true,
Velocity = float.Parse(m.Groups[10].Value, inv)
};
if (name.StartsWith("{{", StringComparison.Ordinal))
return m.Value;
try
{
var vehicle = new Dynamic
{
RangeMax = float.Parse(m.Groups[1].Value, inv),
RangeMin = float.Parse(m.Groups[2].Value, inv),
Name = name,
DataFolder = m.Groups[4].Value,
SkinFile = Dynamic.StripSkinExtension(m.Groups[5].Value),
MmdFile = m.Groups[6].Value,
PathName = m.Groups[7].Value,
Offset = float.Parse(m.Groups[8].Value, inv),
DriverType = m.Groups[9].Value.ToLowerInvariant() switch
{
"headdriver" => eDriverType.Headdriver,
"reardriver" => eDriverType.Reardriver,
"passenger" => eDriverType.Passenger,
_ => eDriverType.Nobody
},
HasVelocity = true,
Velocity = float.Parse(m.Groups[10].Value, inv)
};
var trailing = m.Groups[11].Value
.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries)
.ToList();
vehicle.ReadTrailing(trailing);
vehicles.Add(vehicle);
names.Add(vehicle.Name);
return "\n";
}
catch
{
faults.Add($"# loose dynamic parse fault: {name}");
names.Add(name);
return m.Value;
}
});
vehicle.ReadTrailing(m.Groups[11].Value
.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries)
.ToList());
return vehicle;
}
catch (FormatException)
{
return null;
}
catch (OverflowException)
{
return null;
}
}
private void ParseWeather(string content, SceneryWeather weather)

View File

@@ -3,10 +3,10 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using StarterNG.Application.Abstractions;
using StarterNG.Domain;
using StarterNG.Domain.Sceneries;
using StarterNG.Infrastructure.Adapters;
@@ -21,6 +21,9 @@ public sealed class SceneryRepository : ISceneryRepository
private readonly IDiagnosticsLog _log;
private readonly SceneryParser _parser;
/// <summary>The scenery folders' files and folders, listed once per load of them all.</summary>
private readonly ConcurrentDictionary<(string, bool), IReadOnlyList<string>> _listings = new();
public SceneryRepository(IFileSystem files, IGamePaths paths, IDiagnosticsLog log, SceneryParser parser)
{
_files = files;
@@ -31,6 +34,7 @@ public sealed class SceneryRepository : ISceneryRepository
public IReadOnlyList<Scenery> LoadAll(IProgress<SceneryLoadProgress>? progress = null)
{
_listings.Clear();
var files = SceneryFiles().ToList();
var loaded = new ConcurrentBag<Scenery>();
int total = Math.Max(1, files.Count);
@@ -40,7 +44,7 @@ public sealed class SceneryRepository : ISceneryRepository
new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount },
file =>
{
var scenery = Load(file);
var scenery = LoadOne(file);
if (scenery is not null)
loaded.Add(scenery);
@@ -52,14 +56,44 @@ public sealed class SceneryRepository : ISceneryRepository
}
public Scenery? Load(string path)
{
_listings.Clear();
return LoadOne(path);
}
public IReadOnlyList<string>? ListFiles() =>
_files.DirectoryExists(_paths.Scenery) ? SceneryFiles().ToList() : null;
private Scenery? LoadOne(string path)
{
try
{
if (!_files.FileExists(path))
return null;
var scenery = _parser.Parse(path, LegacyText.Decode(_files.ReadAllBytes(path)));
scenery.HasCompanionTimetable = _files.FileExists(Path.ChangeExtension(path, ".sbt"));
// Looked at ahead of the reading: changed meanwhile, it counts as changed.
var sources = new Dictionary<string, FileStamp?> { [path] = FileStamp.Of(_files, path) };
// With its own .sbt the scenery's terrain comes from it, and the simulator
// passes its terrain includes over without opening them.
bool binaryTerrain = Opens(Path.GetFileNameWithoutExtension(path) + ".sbt");
IncludeLookup FindInclude(string name) =>
Opens(name) || (binaryTerrain && SimulatorNames.Lower(name).Contains("_ter.scm"))
? new IncludeLookup(true)
: new IncludeLookup(false, ResolveInclude(name));
(string File, string Text)? ReadInclude(string name)
{
string? file = ResolveInclude(name);
string full = Path.Combine(_paths.Scenery, file ?? name.Replace('\\', '/'));
sources[full] = FileStamp.Of(_files, full);
return file is null ? null : (file, LegacyText.Decode(_files.ReadAllBytes(full)));
}
string text = SceneryIncludes.Inline(LegacyText.Decode(_files.ReadAllBytes(path)), ReadInclude);
var scenery = _parser.Parse(path, text, FindInclude);
scenery.HasCompanionTimetable = binaryTerrain;
scenery.Sources = sources;
return scenery;
}
catch (Exception ex)
@@ -69,6 +103,58 @@ public sealed class SceneryRepository : ISceneryRepository
}
}
/// <summary>
/// Whether the simulator opens a file of the scenery folder named so: by the
/// name in lower case, which on Linux has to be how it is named on disk.
/// </summary>
private bool Opens(string name) =>
_files.FileExists(Path.Combine(_paths.Scenery, SimulatorNames.Lower(name.Replace('\\', '/'))));
/// <summary>
/// An include's file as it is named on disk, from the scenery folder, whatever
/// the case of its letters - as Windows, or Wine, finds it: whose consists the
/// copy written for a start carries in place of the include, so they come along
/// on Linux too.
/// </summary>
private string? ResolveInclude(string name)
{
string relative = name.Replace('\\', '/');
if (_files.FileExists(Path.Combine(_paths.Scenery, relative)))
return relative;
string[] parts = relative.Split('/', StringSplitOptions.RemoveEmptyEntries);
string directory = _paths.Scenery;
var found = new List<string>();
for (int i = 0; i < parts.Length; i++)
{
if (parts[i] is "." or "..")
{
directory = Path.Combine(directory, parts[i]);
found.Add(parts[i]);
continue;
}
bool last = i == parts.Length - 1;
string? match = Listing(directory, last)
.FirstOrDefault(entry => string.Equals(Path.GetFileName(entry), parts[i],
StringComparison.OrdinalIgnoreCase));
if (match is null)
return null;
directory = match;
found.Add(Path.GetFileName(match));
}
return string.Join('/', found);
}
private IReadOnlyList<string> Listing(string directory, bool files) =>
_listings.GetOrAdd((directory, files), key =>
{
if (!_files.DirectoryExists(key.Item1))
return Array.Empty<string>();
return key.Item2 ? _files.GetFiles(key.Item1) : _files.GetDirectories(key.Item1);
});
private IEnumerable<string> SceneryFiles() =>
_files.GetFiles(_paths.Scenery, "*.scn")
.Where(path => Path.GetFileName(path).FirstOrDefault() != ExcludedPrefix);

View File

@@ -1,8 +1,10 @@
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using StarterNG.Application.Abstractions;
using StarterNG.Domain.Sceneries;
using StarterNG.Infrastructure.Adapters;
namespace StarterNG.Infrastructure.Sceneries;
@@ -10,6 +12,10 @@ public sealed class TimetableLocator
{
private const string NoTimetable = "none";
// The heading of a timetable: [ Rodzaj i numer pociagu | EIE1531 ]
private static readonly Regex NumberLine = new(@"^\s*\[\s*Rodzaj\b[^|\r\n]*\|([^\]\r\n]+)",
RegexOptions.Multiline | RegexOptions.IgnoreCase);
private readonly IFileSystem _files;
public TimetableLocator(IFileSystem files)
@@ -36,4 +42,22 @@ public sealed class TimetableLocator
return candidates.FirstOrDefault(_files.FileExists);
}
/// <summary>The number the timetable gives its train; null with no timetable or no such heading in it.</summary>
public string? TrainNumber(Scenery scenery, string? name)
{
if (Resolve(scenery, name) is not { } path)
return null;
try
{
var heading = NumberLine.Match(LegacyText.Decode(_files.ReadAllBytes(path)));
string number = heading.Groups[1].Value.Trim();
return number.Length > 0 ? number : null;
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
return null;
}
}
}