From 97a2c2c75c3265a372ba699ab30e937161c839d6 Mon Sep 17 00:00:00 2001 From: maj00r Date: Tue, 17 Nov 2026 22:14:45 +0100 Subject: [PATCH] fix(sceneries): an .sbt in the way of terrain files that are there can go Any terrain directive kept the scenery's .sbt, as if it brought the terrain from elsewhere. The directive names either .sbt files, which nothing makes again, or terrain files (.txtf) with their binary copies (.btf). A scenery with the terrain files has no use for its own .sbt: the simulator never writes one for it, and an old one keeps the terrain files from being used. - An .sbt named by the directive still keeps the scenery's own. - A .txtf, or its .btf, on disk lets the scenery's .sbt go; with neither, the .sbt stays and the missing .txtf is given as the reason. --- StarterNG.Tests/BinaryTerrainFilesTests.cs | 40 +++++++++++++++++++ .../Sceneries/BinaryTerrainFiles.cs | 36 ++++++++++++++++- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/StarterNG.Tests/BinaryTerrainFilesTests.cs b/StarterNG.Tests/BinaryTerrainFilesTests.cs index 159d8d3..7f7e3ff 100644 --- a/StarterNG.Tests/BinaryTerrainFilesTests.cs +++ b/StarterNG.Tests/BinaryTerrainFilesTests.cs @@ -91,6 +91,46 @@ public class BinaryTerrainFilesTests Assert.Equal("td.sbt", Assert.Single(Build(files).CleanUpOnce(1)!.Kept).File); } + [Theory] + [InlineData("td/teren.txtf")] + [InlineData("td/Teren.btf")] + public void An_sbt_in_the_way_of_terrain_files_that_are_there_goes(string onDisk) + { + var files = new InMemoryFileSystem(caseSensitive: true) + .WithLegacyFile(Scenery("td.scn"), "terrain td/teren.txtf endterrain\nFirstInit\n") + .WithFile(Scenery(onDisk), "x") + .WithFile(Scenery("td.sbt"), "EU07SBT2"); + + Assert.Single(Build(files).CleanUpOnce(1)!.Deleted); + Assert.False(files.FileExists(Scenery("td.sbt"))); + } + + [Fact] + public void An_sbt_standing_in_for_a_missing_terrain_file_stays() + { + var files = new InMemoryFileSystem() + .WithLegacyFile(Scenery("td.scn"), "terrain td/a.txtf td/b.txtf endterrain\nFirstInit\n") + .WithFile(Scenery("td/a.txtf"), "x") + .WithFile(Scenery("td.sbt"), "EU07SBT2"); + + Assert.Equal(("td.sbt", "td/b.txtf"), Assert.Single(Build(files).CleanUpOnce(1)!.Kept)); + } + + [Fact] + public void An_sbt_named_beside_terrain_files_keeps_the_scenery_s_own() + { + var files = new InMemoryFileSystem() + .WithLegacyFile(Scenery("td.scn"), "terrain td/a.txtf wspolny.sbt endterrain\nFirstInit\n") + .WithFile(Scenery("td/a.txtf"), "x") + .WithFile(Scenery("wspolny.sbt"), "EU07SBT2") + .WithFile(Scenery("td.sbt"), "EU07SBT2"); + + var cleanup = Build(files).CleanUpOnce(1)!; + + Assert.Empty(cleanup.Deleted); + Assert.Contains(("td.sbt", "terrain wspolny.sbt"), cleanup.Kept); + } + [Fact] public void An_sbt_of_no_scenery_stays() { diff --git a/StarterNG/Infrastructure/Sceneries/BinaryTerrainFiles.cs b/StarterNG/Infrastructure/Sceneries/BinaryTerrainFiles.cs index 2036ba0..d24103e 100644 --- a/StarterNG/Infrastructure/Sceneries/BinaryTerrainFiles.cs +++ b/StarterNG/Infrastructure/Sceneries/BinaryTerrainFiles.cs @@ -105,10 +105,18 @@ public sealed class BinaryTerrainFiles string code = SceneryCode.Mask(text, out _); bool includeNext = false; + bool terrainDirective = false; for (var word = Word.Match(code); word.Success; word = word.NextMatch()) { string token = word.Value; - if (includeNext) + if (terrainDirective) + { + if (token.Equals("endterrain", StringComparison.OrdinalIgnoreCase)) + terrainDirective = false; + else if (TerrainFileMissing(token, names) is { } missing) + return missing; + } + else if (includeNext) { includeNext = false; bool terrain = SimulatorNames.Lower(token).Contains("_ter.scm"); @@ -123,12 +131,36 @@ public sealed class BinaryTerrainFiles else if (token.Equals("include", StringComparison.OrdinalIgnoreCase)) includeNext = true; else if (token.Equals("terrain", StringComparison.OrdinalIgnoreCase)) - return token; + terrainDirective = true; } return null; } + /// + /// One file named by a terrain directive. An .sbt named there holds the geometry + /// of the whole scenery and nothing makes it again, so the scenery's own goes + /// only without one. A terrain file (.txtf) has its geometry in itself or in its + /// binary copy (.btf) beside it: with either one there, the scenery's .sbt only + /// keeps that terrain from being used, and the simulator asks for it to go. + /// + private string? TerrainFileMissing(string token, CaseInsensitivePaths names) + { + string extension = Path.GetExtension(token); + if (extension.Equals(Extension, StringComparison.OrdinalIgnoreCase)) + return "terrain " + token; + + if (!extension.Equals(".txtf", StringComparison.OrdinalIgnoreCase) + && !extension.Equals(".btf", StringComparison.OrdinalIgnoreCase)) + return null; + + string stem = token[..^extension.Length]; + return names.Resolve(_paths.Scenery, stem + ".txtf") is null + && names.Resolve(_paths.Scenery, stem + ".btf") is null + ? stem + ".txtf" + : null; + } + /// Removes the scenery's .sbt; false when it has none or it could not go. public bool Delete(string sceneryPath) {