fix(scenery): a vehicle without its .fiz is a fault, an include ending the file is read

A vehicle whose .fiz is not in its folder under dynamic is left out by the
simulator, so its trainset runs short: the scenery check now names the
missing file. An include that is the last thing in the file, with no end,
is inlined as the simulator reads it, and still reported as not closed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
maj00r
2026-11-22 00:02:20 +01:00
parent ecf3067d85
commit e63aa71bf8
6 changed files with 135 additions and 13 deletions

View File

@@ -270,7 +270,7 @@ public class ScenerySyntaxTests
trainset td/rozklad tdo_n1734 0.0 0.1 trainset td/rozklad tdo_n1734 0.0 0.1
//$o Skład gotowy na żeberku. //$o Skład gotowy na żeberku.
endtrainset endtrainset
include koniec.scm include koniec.scm end
"""; """;
var scenery = TestScenery.Parser().Parse("scenery/x.scn", scn, _ => true); var scenery = TestScenery.Parser().Parse("scenery/x.scn", scn, _ => true);
@@ -331,13 +331,61 @@ public class ScenerySyntaxTests
Assert.Equal((SceneryFaultKind.MissingInclude, "x.scn", 2, null, "brak.scm"), Seen(Assert.Single(faults))); Assert.Equal((SceneryFaultKind.MissingInclude, "x.scn", 2, null, "brak.scm"), Seen(Assert.Single(faults)));
} }
[Fact]
public void A_vehicle_without_its_fiz_is_reported_as_one_the_simulator_leaves_out()
{
// As on Bałtyk Cargo: the wagons of a folder gone empty drop out of the consist.
const string scn = """
trainset none tor1 0 0.1
node -1 0 sm42 dynamic PKP\SM42_V1 6D-468 6D -1 headdriver 3 0 enddynamic
node -1 0 eaos-33 dynamic PKP\412W_V1 5340976-0 412wc 0 nobody 3 60 wegiel3 enddynamic
node -1 0 eaos-34 dynamic PKP\412W_V1 5351442-9 412W 0 nobody 0 60 mial1 enddynamic
endtrainset
node -1 0 auto dynamic car car car tor1 0 headdriver 0 0 enddynamic
""";
var asked = new List<(string, string)>();
var scenery = TestScenery.Parser().Parse("scenery/x.scn", scn, loadsVehicle: (folder, mmd) =>
{
asked.Add((folder, mmd));
return !folder.Equals(@"PKP\412W_V1", StringComparison.Ordinal);
});
Assert.Equal(new[]
{
(SceneryFaultKind.VehicleMissing, "x.scn", 3, (string?)"eaos-33", (string?)@"PKP\412W_V1/412wc.fiz"),
(SceneryFaultKind.VehicleMissing, "x.scn", 4, "eaos-34", @"PKP\412W_V1/412W.fiz")
}, scenery.Faults.Select(Seen));
Assert.Contains(("car", "car"), asked);
// Still listed whole: the starter writes them as they are, for when they are back.
Assert.Equal(3, Assert.Single(scenery.Trainsets).Vehicles.Count);
}
[Fact]
public void A_vehicle_folder_there_but_empty_is_no_vehicle()
{
var files = new InMemoryFileSystem(caseSensitive: true)
.WithLegacyFile("game/scenery/x.scn",
"trainset none tor1 0 0\n" +
"node -1 0 a1 dynamic PKP\\430W_V2 skin 430W 0 nobody 3 0 enddynamic\n" +
"node -1 0 a2 dynamic PKP\\412W_V1 skin 412W 0 nobody 0 0 enddynamic\n" +
"endtrainset\n")
.WithLegacyFile("game/dynamic/pkp/430w_v2/430w.fiz", "")
.WithLegacyFile("game/dynamic/pkp/412w_v1/412w.mmd", "");
var faults = TestScenery.Repository(files).Load("game/scenery/x.scn")!.Faults;
Assert.Equal((SceneryFaultKind.VehicleMissing, "x.scn", 3, "a2", @"PKP\412W_V1/412W.fiz"), Seen(Assert.Single(faults)));
}
[Fact] [Fact]
public void An_include_named_in_another_case_is_found_as_the_simulator_finds_it() public void An_include_named_in_another_case_is_found_as_the_simulator_finds_it()
{ {
var files = new InMemoryFileSystem(caseSensitive: true) var files = new InMemoryFileSystem(caseSensitive: true)
.WithLegacyFile("game/scenery/x.scn", "FirstInit\ninclude Tomaszewo/Auta.SCM end\n") .WithLegacyFile("game/scenery/x.scn", "FirstInit\ninclude Tomaszewo/Auta.SCM end\n")
.WithLegacyFile("game/scenery/tomaszewo/auta.scm", .WithLegacyFile("game/scenery/tomaszewo/auta.scm",
"trainset auto road1 0 50\nnode -1 0 a1 dynamic car car car 0 headdriver 3 0 enddynamic\nendtrainset\n"); "trainset auto road1 0 50\nnode -1 0 a1 dynamic car car car 0 headdriver 3 0 enddynamic\nendtrainset\n")
.WithLegacyFile("game/dynamic/car/car.fiz", "");
var repository = TestScenery.Repository(files); var repository = TestScenery.Repository(files);
var scenery = repository.Load("game/scenery/x.scn")!; var scenery = repository.Load("game/scenery/x.scn")!;
@@ -378,4 +426,35 @@ public class ScenerySyntaxTests
Assert.Equal(sbt, scenery.HasCompanionTimetable); Assert.Equal(sbt, scenery.HasCompanionTimetable);
Assert.Equal(sbt ? 0 : 1, scenery.Faults.Count); Assert.Equal(sbt ? 0 : 1, scenery.Faults.Count);
} }
[Fact]
public void An_include_with_no_end_is_a_fault_even_as_the_last_line()
{
const string scn = "FirstInit\ntrainset a t 0 0\nendtrainset\ninclude dekoracje.scm //Sklady dekoracyjne\n";
var faults = TestScenery.Parser().Parse("scenery/x.scn", scn).Faults.Select(f => (f.Kind, f.Line)).ToList();
Assert.Equal(new[] { (SceneryFaultKind.IncludeNotClosed, 4) }, faults);
}
[Fact]
public void The_consists_of_a_last_include_with_no_end_are_listed_as_the_simulator_loads_them()
{
const string decorations = "trainset d1 t1 0 0\nnode -1 0 a dynamic d s m 0 nobody 3 0 enddynamic\nendtrainset\n";
string text = SceneryIncludes.Inline("FirstInit\ninclude dekoracje.scm //Sklady dekoracyjne\n",
name => (name, decorations));
Assert.Contains(SceneryIncludes.BeginMarker + "dekoracje.scm", text);
Assert.Contains("trainset d1 t1 0 0", text);
}
[Fact]
public void An_include_with_no_end_before_more_of_the_file_is_left_alone()
{
string text = SceneryIncludes.Inline("FirstInit\ninclude dekoracje.scm\ntrainset a t 0 0\nendtrainset\n",
name => (name, "trainset d1 t1 0 0\nendtrainset\n"));
Assert.DoesNotContain(SceneryIncludes.BeginMarker, text);
}
} }

View File

@@ -64,6 +64,13 @@ public enum SceneryFaultKind
/// <summary>An include with no <c>end</c>, which swallows what follows it.</summary> /// <summary>An include with no <c>end</c>, which swallows what follows it.</summary>
IncludeNotClosed, IncludeNotClosed,
/// <summary>
/// A vehicle whose <c>.fiz</c> is not in its folder under dynamic: the simulator
/// leaves it out, and its trainset runs short. <see cref="SceneryFault.Token"/>
/// names the file.
/// </summary>
VehicleMissing,
/// <summary>A trainset the starter could not read, for a reason none of the above names.</summary> /// <summary>A trainset the starter could not read, for a reason none of the above names.</summary>
TrainsetUnreadable, TrainsetUnreadable,

View File

@@ -15,9 +15,10 @@ namespace StarterNG.Domain.Sceneries;
/// include, so they are not loaded twice. /// include, so they are not loaded twice.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// Only the plain <c>include path end</c> form: an include with parameters needs /// Only the plain <c>include path end</c> form, or <c>include path</c> as the last
/// them put in, and one whose <c>end</c> is not on its line is read by the /// thing in the file, where the simulator runs out of words before it would
/// simulator in ways not worth guessing. One marked <c>$optional</c> stays too: /// look for an <c>end</c>: an include with parameters needs them put in, and one
/// whose <c>end</c> is not on its line is read by the simulator in ways not worth guessing. One marked <c>$optional</c> stays too:
/// whether it is loaded is the player's to choose. Each inlined file is wrapped in /// whether it is loaded is the player's to choose. Each inlined file is wrapped in
/// marker comments, which is how a trainset is told to belong to it. /// marker comments, which is how a trainset is told to belong to it.
/// </remarks> /// </remarks>
@@ -29,7 +30,8 @@ public static class SceneryIncludes
private static readonly Regex FirstInit = new(@"\bFirstInit\b", RegexOptions.IgnoreCase); private static readonly Regex FirstInit = new(@"\bFirstInit\b", RegexOptions.IgnoreCase);
private static readonly Regex Include = new( private static readonly Regex Include = new(
@"^[ \t]*include[ \t]+(\S+)[ \t]+end\b[^\r\n]*", RegexOptions.Multiline | RegexOptions.IgnoreCase); @"^[ \t]*include[ \t]+(\S+?)(?:[ \t]+end\b[^\r\n]*|[ \t]*(?://[^\r\n]*)?\s*\z)",
RegexOptions.Multiline | RegexOptions.IgnoreCase);
private static readonly Regex HasTrainset = new(@"^[ \t]*trainset\b", RegexOptions.Multiline | RegexOptions.IgnoreCase); private static readonly Regex HasTrainset = new(@"^[ \t]*trainset\b", RegexOptions.Multiline | RegexOptions.IgnoreCase);

View File

@@ -33,13 +33,18 @@ public static class ScenerySyntax
/// Whether the simulator opens an include, named as the include names it; null /// Whether the simulator opens an include, named as the include names it; null
/// leaves includes unchecked. /// leaves includes unchecked.
/// </param> /// </param>
public static Result Scan(string text, string file, Func<string, bool>? opensInclude = null) /// <param name="loadsVehicle">
/// Whether the simulator finds a vehicle's definition, by its folder under dynamic
/// and its .mmd's name, as the scenery writes them; null leaves vehicles unchecked.
/// </param>
public static Result Scan(string text, string file, Func<string, bool>? opensInclude = null,
Func<string, string, bool>? loadsVehicle = null)
{ {
string code = SceneryCode.Mask(text, out int unclosed); string code = SceneryCode.Mask(text, out int unclosed);
var tokens = SceneryCode.Tokens(code); var tokens = SceneryCode.Tokens(code);
var lines = new SceneryLines(text, file); var lines = new SceneryLines(text, file);
var simulator = new Reading(tokens, lines, opensInclude, asSimulator: true); var simulator = new Reading(tokens, lines, opensInclude, asSimulator: true, loadsVehicle);
simulator.Run(); simulator.Run();
var author = new Reading(tokens, lines, null, asSimulator: false); var author = new Reading(tokens, lines, null, asSimulator: false);
author.Run(); author.Run();
@@ -110,7 +115,8 @@ public static class ScenerySyntax
} }
private sealed class Reading( private sealed class Reading(
List<SceneryCode.Token> tokens, SceneryLines lines, Func<string, bool>? opensInclude, bool asSimulator) List<SceneryCode.Token> tokens, SceneryLines lines, Func<string, bool>? opensInclude, bool asSimulator,
Func<string, string, bool>? loadsVehicle = null)
{ {
public readonly List<(int Start, int End, string Name)> Trainsets = new(); public readonly List<(int Start, int End, string Name)> Trainsets = new();
public readonly List<(int, int)> Loose = new(); public readonly List<(int, int)> Loose = new();
@@ -253,6 +259,9 @@ public static class ScenerySyntax
return tokens.Count - 1; return tokens.Count - 1;
} }
if (complete)
Definition(at, name);
var (fault, trackless) = CheckFields(at, complete, inTrainset, name); var (fault, trackless) = CheckFields(at, complete, inTrainset, name);
if (fault is null && !closed) if (fault is null && !closed)
fault = SceneryFaultKind.VehicleNotClosed; fault = SceneryFaultKind.VehicleNotClosed;
@@ -267,6 +276,16 @@ public static class ScenerySyntax
return end; return end;
} }
// Without its .fiz the simulator makes nothing of the vehicle, and reads on to the next.
private void Definition(int at, string name)
{
string folder = tokens[at + 5].Text;
string mmd = tokens[at + 7].Text;
if (loadsVehicle is null || !Plain(folder) || !Plain(mmd) || loadsVehicle(folder, mmd))
return;
Fault(SceneryFaultKind.VehicleMissing, tokens[at].Index, name, $"{folder}/{mmd}.fiz");
}
// As the author meant it: there to be listed, whatever is wrong with it. // As the author meant it: there to be listed, whatever is wrong with it.
private int ListVehicle(int at, bool complete, bool closed, int meant) private int ListVehicle(int at, bool complete, bool closed, int meant)
{ {
@@ -378,7 +397,8 @@ public static class ScenerySyntax
while (end < tokens.Count && tokens[end].Text != "end") while (end < tokens.Count && tokens[end].Text != "end")
end++; end++;
int last = Math.Min(end, tokens.Count - 1); int last = Math.Min(end, tokens.Count - 1);
if (Lost(at + 1, last) is not null) // No "end" at all is a mistake too, even where the file runs out before it costs anything.
if (end >= tokens.Count || Lost(at + 1, last) is not null)
Fault(SceneryFaultKind.IncludeNotClosed, token.Index, token: name, swallowed: (at + 1, last)); Fault(SceneryFaultKind.IncludeNotClosed, token.Index, token: name, swallowed: (at + 1, last));
return last; return last;
} }

View File

@@ -24,7 +24,12 @@ public sealed class SceneryParser
/// Whether the simulator opens an include, as the include names it; null leaves /// Whether the simulator opens an include, as the include names it; null leaves
/// includes unchecked. /// includes unchecked.
/// </param> /// </param>
public Scenery Parse(string path, string content, Func<string, bool>? opensInclude = null) /// <param name="loadsVehicle">
/// Whether the simulator finds a vehicle's .fiz, by its folder and .mmd name as
/// written; null leaves vehicles unchecked.
/// </param>
public Scenery Parse(string path, string content, Func<string, bool>? opensInclude = null,
Func<string, string, bool>? loadsVehicle = null)
{ {
string original = content; string original = content;
@@ -38,7 +43,7 @@ public sealed class SceneryParser
// Read from the text with its optional includes blanked, which keeps every // 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. // 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), opensInclude); var syntax = ScenerySyntax.Scan(content, System.IO.Path.GetFileName(path), opensInclude, loadsVehicle);
var faults = syntax.Faults.ToList(); var faults = syntax.Faults.ToList();
var inlined = SceneryIncludes.Ranges(content); var inlined = SceneryIncludes.Ranges(content);

View File

@@ -98,7 +98,7 @@ public sealed class SceneryRepository : ISceneryRepository
string own = LegacyText.Decode(_files.ReadAllBytes(path), out var ownEncoding); string own = LegacyText.Decode(_files.ReadAllBytes(path), out var ownEncoding);
string text = SceneryIncludes.Inline(own, ReadInclude); string text = SceneryIncludes.Inline(own, ReadInclude);
var scenery = _parser.Parse(path, text, OpensInclude); var scenery = _parser.Parse(path, text, OpensInclude, LoadsVehicle);
scenery.HasCompanionTimetable = binaryTerrain; scenery.HasCompanionTimetable = binaryTerrain;
scenery.Sources = sources; scenery.Sources = sources;
scenery.TextEncoding = ownEncoding; scenery.TextEncoding = ownEncoding;
@@ -119,6 +119,15 @@ public sealed class SceneryRepository : ISceneryRepository
/// </summary> /// </summary>
private bool Opens(string name) => ResolveInclude(name) is not null; private bool Opens(string name) => ResolveInclude(name) is not null;
/// <summary>
/// Whether the simulator finds a vehicle's definition - its .fiz, named after its
/// .mmd, in its folder under dynamic - whatever the case of the letters. Without
/// it the simulator leaves the vehicle out; a folder there all the same, empty,
/// makes no difference.
/// </summary>
private bool LoadsVehicle(string folder, string mmd) =>
_names.Resolve(_paths.Dynamic, $"{folder}/{mmd}.fiz") is not null;
/// <summary> /// <summary>
/// An include's file as it is named on disk, from the scenery folder, whatever /// 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 /// the case of its letters - as Windows, or Wine, finds it: whose consists the