diff --git a/StarterNG.Tests/ScenerySyntaxTests.cs b/StarterNG.Tests/ScenerySyntaxTests.cs index 39fede4..22fa078 100644 --- a/StarterNG.Tests/ScenerySyntaxTests.cs +++ b/StarterNG.Tests/ScenerySyntaxTests.cs @@ -270,7 +270,7 @@ public class ScenerySyntaxTests trainset td/rozklad tdo_n1734 0.0 0.1 //$o Skład gotowy na żeberku. endtrainset - include koniec.scm + include koniec.scm end """; 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))); } + [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] public void An_include_named_in_another_case_is_found_as_the_simulator_finds_it() { var files = new InMemoryFileSystem(caseSensitive: true) .WithLegacyFile("game/scenery/x.scn", "FirstInit\ninclude Tomaszewo/Auta.SCM end\n") .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 scenery = repository.Load("game/scenery/x.scn")!; @@ -378,4 +426,35 @@ public class ScenerySyntaxTests Assert.Equal(sbt, scenery.HasCompanionTimetable); 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); + } } diff --git a/StarterNG/Domain/Sceneries/SceneryFault.cs b/StarterNG/Domain/Sceneries/SceneryFault.cs index 1b4f5f7..3ee91c6 100644 --- a/StarterNG/Domain/Sceneries/SceneryFault.cs +++ b/StarterNG/Domain/Sceneries/SceneryFault.cs @@ -64,6 +64,13 @@ public enum SceneryFaultKind /// An include with no end, which swallows what follows it. IncludeNotClosed, + /// + /// A vehicle whose .fiz is not in its folder under dynamic: the simulator + /// leaves it out, and its trainset runs short. + /// names the file. + /// + VehicleMissing, + /// A trainset the starter could not read, for a reason none of the above names. TrainsetUnreadable, diff --git a/StarterNG/Domain/Sceneries/SceneryIncludes.cs b/StarterNG/Domain/Sceneries/SceneryIncludes.cs index 0f11dda..8804730 100644 --- a/StarterNG/Domain/Sceneries/SceneryIncludes.cs +++ b/StarterNG/Domain/Sceneries/SceneryIncludes.cs @@ -15,9 +15,10 @@ namespace StarterNG.Domain.Sceneries; /// include, so they are not loaded twice. /// /// -/// Only the plain include path end form: an include with parameters needs -/// them put in, and one whose end is not on its line is read by the -/// simulator in ways not worth guessing. One marked $optional stays too: +/// Only the plain include path end form, or include path as the last +/// thing in the file, where the simulator runs out of words before it would +/// look for an end: an include with parameters needs them put in, and one +/// whose end is not on its line is read by the simulator in ways not worth guessing. One marked $optional stays too: /// 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. /// @@ -29,7 +30,8 @@ public static class SceneryIncludes private static readonly Regex FirstInit = new(@"\bFirstInit\b", RegexOptions.IgnoreCase); 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); diff --git a/StarterNG/Domain/Sceneries/ScenerySyntax.cs b/StarterNG/Domain/Sceneries/ScenerySyntax.cs index 87ee063..3e35196 100644 --- a/StarterNG/Domain/Sceneries/ScenerySyntax.cs +++ b/StarterNG/Domain/Sceneries/ScenerySyntax.cs @@ -33,13 +33,18 @@ public static class ScenerySyntax /// Whether the simulator opens an include, named as the include names it; null /// leaves includes unchecked. /// - public static Result Scan(string text, string file, Func? opensInclude = null) + /// + /// 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. + /// + public static Result Scan(string text, string file, Func? opensInclude = null, + Func? loadsVehicle = null) { string code = SceneryCode.Mask(text, out int unclosed); var tokens = SceneryCode.Tokens(code); 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(); var author = new Reading(tokens, lines, null, asSimulator: false); author.Run(); @@ -110,7 +115,8 @@ public static class ScenerySyntax } private sealed class Reading( - List tokens, SceneryLines lines, Func? opensInclude, bool asSimulator) + List tokens, SceneryLines lines, Func? opensInclude, bool asSimulator, + Func? loadsVehicle = null) { public readonly List<(int Start, int End, string Name)> Trainsets = new(); public readonly List<(int, int)> Loose = new(); @@ -253,6 +259,9 @@ public static class ScenerySyntax return tokens.Count - 1; } + if (complete) + Definition(at, name); + var (fault, trackless) = CheckFields(at, complete, inTrainset, name); if (fault is null && !closed) fault = SceneryFaultKind.VehicleNotClosed; @@ -267,6 +276,16 @@ public static class ScenerySyntax 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. 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") end++; 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)); return last; } diff --git a/StarterNG/Infrastructure/Sceneries/SceneryParser.cs b/StarterNG/Infrastructure/Sceneries/SceneryParser.cs index f8072ba..031a05f 100644 --- a/StarterNG/Infrastructure/Sceneries/SceneryParser.cs +++ b/StarterNG/Infrastructure/Sceneries/SceneryParser.cs @@ -24,7 +24,12 @@ public sealed class SceneryParser /// Whether the simulator opens an include, as the include names it; null leaves /// includes unchecked. /// - public Scenery Parse(string path, string content, Func? opensInclude = null) + /// + /// Whether the simulator finds a vehicle's .fiz, by its folder and .mmd name as + /// written; null leaves vehicles unchecked. + /// + public Scenery Parse(string path, string content, Func? opensInclude = null, + Func? loadsVehicle = null) { string original = content; @@ -38,7 +43,7 @@ public sealed class SceneryParser // 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), opensInclude); + var syntax = ScenerySyntax.Scan(content, System.IO.Path.GetFileName(path), opensInclude, loadsVehicle); var faults = syntax.Faults.ToList(); var inlined = SceneryIncludes.Ranges(content); diff --git a/StarterNG/Infrastructure/Sceneries/SceneryRepository.cs b/StarterNG/Infrastructure/Sceneries/SceneryRepository.cs index 229d59a..e7b7862 100644 --- a/StarterNG/Infrastructure/Sceneries/SceneryRepository.cs +++ b/StarterNG/Infrastructure/Sceneries/SceneryRepository.cs @@ -98,7 +98,7 @@ public sealed class SceneryRepository : ISceneryRepository string own = LegacyText.Decode(_files.ReadAllBytes(path), out var ownEncoding); 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.Sources = sources; scenery.TextEncoding = ownEncoding; @@ -119,6 +119,15 @@ public sealed class SceneryRepository : ISceneryRepository /// private bool Opens(string name) => ResolveInclude(name) is not null; + /// + /// 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. + /// + private bool LoadsVehicle(string folder, string mmd) => + _names.Resolve(_paths.Dynamic, $"{folder}/{mmd}.fiz") is not null; + /// /// 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