fix(sceneries): an .sbt goes only when every scenery reading it can do without

With an .sbt there the simulator passes over the triangles and lines of every
file of the scenery, so a scenery file (.scm, .scn) that is gone keeps the
.sbt whatever its name, not only one named *_ter.scm. A small terrain file is
read too: some only list the files that hold the terrain. Node templates
(.inc) still do not count.

A word reading "terrain" outside a directive - the name of a node, of a
texture - no longer hides the includes after it.

An .sbt is judged by all the sceneries reading it: the one it is named after,
that one with a $ ahead of its name, and any other naming it in a terrain
directive. What such a scenery lacks comes with its name.

Each file is read once for a cleanup, however many sceneries include it, and
only around the words that matter: on an installation of 180 sceneries the
cleanup at the first start took minutes and takes seconds.
This commit is contained in:
maj00r
2026-11-22 15:30:51 +01:00
parent 7e249a7a9a
commit 900161a6c4
2 changed files with 475 additions and 83 deletions

View File

@@ -1,3 +1,5 @@
using System.Reflection;
using StarterNG.Application.Abstractions;
using StarterNG.Infrastructure.Adapters;
using StarterNG.Infrastructure.Sceneries;
using StarterNG.Tests.Fakes;
@@ -194,4 +196,209 @@ public class BinaryTerrainFilesTests
Assert.True(sbt.Delete(Scenery("Kato.scn")));
Assert.Null(sbt.Of(Scenery("Kato.scn")));
}
// With an .sbt there the simulator passes over the triangles and lines of every file, not
// only of those named *_ter.scm: a scenery file that is gone took its shapes with it.
[Theory]
[InlineData("td/teren.scm", true)]
[InlineData("td/teren.scm", false)]
[InlineData("TD\\Czesc.SCN", false)]
public void A_missing_scenery_file_keeps_the_sbt_whatever_its_name(string include, bool present)
{
var files = new InMemoryFileSystem()
.WithLegacyFile(Scenery("td.scn"), $"include td/base.scm end\nFirstInit\n")
.WithLegacyFile(Scenery("td/base.scm"), $"include {include} 1 2 end\n")
.WithFile(Scenery("td.sbt"), "EU07SBT2");
if (present)
files.WithLegacyFile(Scenery(include), "node -1 0 none triangles grass 0 0 0 endtri\n");
var cleanup = Build(files).CleanUpOnce(1)!;
if (present)
Assert.Equal("td.sbt", Assert.Single(cleanup.Deleted));
else
Assert.Equal(("td.sbt", include), Assert.Single(cleanup.Kept));
Assert.Equal(present, !files.FileExists(Scenery("td.sbt")));
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void A_terrain_file_that_only_lists_others_is_read_for_them(bool present)
{
var files = new InMemoryFileSystem()
.WithLegacyFile(Scenery("td.scn"), "include td/citygml_ter.scm end\nFirstInit\n")
.WithLegacyFile(Scenery("td/citygml_ter.scm"), "include td/citygml/n-34.scm end\n")
.WithFile(Scenery("td.sbt"), "EU07SBT2");
if (present)
files.WithFile(Scenery("td/citygml/n-34.scm"), "node");
var cleanup = Build(files).CleanUpOnce(1)!;
Assert.Equal(present, cleanup.Deleted.Count == 1);
if (!present)
Assert.Equal(("td.sbt", "td/citygml/n-34.scm"), Assert.Single(cleanup.Kept));
}
[Fact]
public void A_large_terrain_file_is_not_opened()
{
var files = new InMemoryFileSystem()
.WithLegacyFile(Scenery("td.scn"), "include td/td_ter.scm end\nFirstInit\n")
.WithFile(Scenery("td/td_ter.scm"), new string(' ', 1024 * 1024) + "include td/gone.scm end\n")
.WithFile(Scenery("td.sbt"), "EU07SBT2");
Assert.Single(Build(files).CleanUpOnce(1)!.Deleted);
}
// "terrain" starts a directive only where the simulator looks for one. As the name of a
// node or of a texture it starts nothing, and the includes after it count as any other.
[Theory]
[InlineData("node -1 0 terrain triangles grass 0 0 0 endtri")]
[InlineData("node -1 0 none triangles terrain 0 0 0 endtri")]
public void A_word_reading_terrain_hides_no_include_after_it(string node)
{
var files = new InMemoryFileSystem()
.WithLegacyFile(Scenery("td.scn"), $"{node}\ninclude td/gone_ter.scm end\ninclude td/more.scm end\nFirstInit\n")
.WithLegacyFile(Scenery("td/more.scm"), "include td/gone.scm end\n")
.WithFile(Scenery("td.sbt"), "EU07SBT2");
Assert.Equal(("td.sbt", "td/gone_ter.scm"), Assert.Single(Build(files).CleanUpOnce(1)!.Kept));
files.WithFile(Scenery("td/gone_ter.scm"), "node");
var again = Build(files).CleanUpOnce(2)!;
Assert.Equal(("td.sbt", "td/gone.scm"), Assert.Single(again.Kept));
}
[Fact]
public void A_word_reading_terrain_alone_does_not_keep_the_sbt()
{
var files = new InMemoryFileSystem()
.WithLegacyFile(Scenery("td.scn"),
"node -1 0 terrain triangles grass 0 0 0 endtri\ninclude td/td_ter.scm end\nFirstInit\n")
.WithFile(Scenery("td/td_ter.scm"), "node")
.WithFile(Scenery("td.sbt"), "EU07SBT2");
Assert.Single(Build(files).CleanUpOnce(1)!.Deleted);
}
[Fact]
public void A_terrain_directive_is_still_read_after_a_word_reading_terrain()
{
var files = new InMemoryFileSystem()
.WithLegacyFile(Scenery("td.scn"),
"node -1 0 terrain triangles grass 0 0 0 endtri\nterrain td/a.txtf endterrain\nFirstInit\n")
.WithFile(Scenery("td.sbt"), "EU07SBT2");
Assert.Equal(("td.sbt", "td/a.txtf"), Assert.Single(Build(files).CleanUpOnce(1)!.Kept));
}
// An .sbt serves every scenery naming it in a terrain directive, not only the one it is
// named after. With it gone such a scenery is read from its own text, until the simulator
// writes the file again on a load of the scenery it belongs to.
[Theory]
[InlineData("wspolny.sbt")]
[InlineData("Wspolny.SBT")]
[InlineData("$wspolny.sbt")]
public void An_sbt_another_scenery_cannot_do_without_stays(string named)
{
var files = new InMemoryFileSystem()
.WithLegacyFile(Scenery("wspolny.scn"), "include wspolny/wspolny_ter.scm end\nFirstInit\n")
.WithFile(Scenery("wspolny/wspolny_ter.scm"), "node")
.WithFile(Scenery("wspolny.sbt"), "EU07SBT2")
.WithLegacyFile(Scenery("nocny.scn"), $"include nocny/base.scm end\nFirstInit\n")
.WithLegacyFile(Scenery("nocny/base.scm"), $"terrain {named} endterrain\ninclude nmt/nmt100_ter.scm end\n");
var sbt = Build(files);
Assert.Equal("nmt/nmt100_ter.scm (nocny.scn)", sbt.MissingOriginal(Scenery("wspolny.scn")));
var cleanup = sbt.CleanUpOnce(1)!;
Assert.Empty(cleanup.Deleted);
Assert.Equal(("wspolny.sbt", "nmt/nmt100_ter.scm (nocny.scn)"), Assert.Single(cleanup.Kept));
Assert.True(files.FileExists(Scenery("wspolny.sbt")));
}
[Fact]
public void An_sbt_another_scenery_can_do_without_goes()
{
var files = new InMemoryFileSystem()
.WithLegacyFile(Scenery("wspolny.scn"), "include wspolny/wspolny_ter.scm end\nFirstInit\n")
.WithFile(Scenery("wspolny/wspolny_ter.scm"), "node")
.WithFile(Scenery("wspolny.sbt"), "EU07SBT2")
.WithLegacyFile(Scenery("nocny.scn"),
"terrain wspolny.sbt endterrain\ninclude wspolny/wspolny_ter.scm end\nFirstInit\n");
var sbt = Build(files);
Assert.Null(sbt.MissingOriginal(Scenery("wspolny.scn")));
Assert.Equal("wspolny.sbt", Assert.Single(sbt.CleanUpOnce(1)!.Deleted));
}
[Fact]
public void An_sbt_of_the_same_name_in_another_folder_is_another_file()
{
var files = new InMemoryFileSystem()
.WithLegacyFile(Scenery("teren.scn"), "FirstInit\n")
.WithFile(Scenery("teren.sbt"), "EU07SBT2")
.WithFile(Scenery("l107/teren.sbt"), "EU07SBT2")
.WithLegacyFile(Scenery("lupkow.scn"),
"terrain l107/teren.sbt endterrain\ninclude nmt/nmt100_ter.scm end\nFirstInit\n");
var cleanup = Build(files).CleanUpOnce(1)!;
Assert.Equal("teren.sbt", Assert.Single(cleanup.Deleted));
Assert.True(files.FileExists(Scenery("l107/teren.sbt")));
}
// The simulator drops the $ a scenery editor puts ahead of the name of a changed scenery,
// and reads and writes the .sbt of the one it was made from.
[Fact]
public void An_sbt_also_serves_the_scenery_named_with_a_dollar_ahead()
{
var files = new InMemoryFileSystem()
.WithLegacyFile(Scenery("td.scn"), "FirstInit\n")
.WithLegacyFile(Scenery("$td.scn"), "include td/nowy_ter.scm end\nFirstInit\n")
.WithFile(Scenery("td.sbt"), "EU07SBT2");
Assert.Equal(("td.sbt", "td/nowy_ter.scm ($td.scn)"), Assert.Single(Build(files).CleanUpOnce(1)!.Kept));
files.WithFile(Scenery("td/nowy_ter.scm"), "node");
Assert.Single(Build(files).CleanUpOnce(2)!.Deleted);
}
[Fact]
public void A_file_many_sceneries_include_is_read_once()
{
var files = new InMemoryFileSystem()
.WithLegacyFile(Scenery("dzien.scn"), "include td/base.scm end\nFirstInit\n")
.WithLegacyFile(Scenery("noc.scn"), "include td/base.scm end\nFirstInit\n")
.WithLegacyFile(Scenery("td/base.scm"), "include td/td_ter.scm end\n")
.WithFile(Scenery("td/td_ter.scm"), "node")
.WithFile(Scenery("dzien.sbt"), "EU07SBT2")
.WithFile(Scenery("noc.sbt"), "EU07SBT2");
var counted = DispatchProxy.Create<IFileSystem, ReadCounter>();
var counter = (ReadCounter)(object)counted;
counter.Files = files;
var sbt = new BinaryTerrainFiles(counted, new GamePaths("game"), TestLog.Over(files), Record);
Assert.Equal(2, sbt.CleanUpOnce(1)!.Deleted.Count);
Assert.Single(counter.Read, path => Path.GetFileName(path) == "base.scm");
}
public class ReadCounter : DispatchProxy
{
public IFileSystem Files { get; set; } = null!;
public List<string> Read { get; } = new();
protected override object? Invoke(MethodInfo? targetMethod, object?[]? args)
{
if (targetMethod!.Name == nameof(IFileSystem.ReadAllBytes))
Read.Add((string)args![0]!);
return targetMethod.Invoke(Files, args);
}
}
}

View File

@@ -1,8 +1,8 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using StarterNG.Application.Abstractions;
using StarterNG.Domain;
using StarterNG.Domain.Sceneries;
@@ -13,13 +13,14 @@ namespace StarterNG.Infrastructure.Sceneries;
/// <summary>
/// The sceneries' binary terrain files (.sbt): the simulator writes one beside a
/// scenery on its first load and from then on takes the terrain from it, passing
/// the scenery's terrain includes (<c>*_ter.scm</c>) over. It never asks whether
/// the text changed since, so a file made by an older simulator, or before the
/// scenery was updated, keeps showing what was there then.
/// the scenery's terrain includes (<c>*_ter.scm</c>) over, and the triangles and
/// lines of its other files with them. It never asks whether the text changed
/// since, so a file made by an older simulator, or before the scenery was updated,
/// keeps showing what was there then.
/// <para>
/// A file goes only when the scenery's text still holds what it was made of: some
/// sceneries come with the .sbt in place of their terrain includes, and without it
/// they would have no terrain at all.
/// A file goes only when the text of every scenery reading it still holds what it
/// was made of: some sceneries come with the .sbt in place of their terrain
/// includes, and without it they would have no terrain at all.
/// </para>
/// </summary>
public sealed class BinaryTerrainFiles
@@ -33,8 +34,9 @@ public sealed class BinaryTerrainFiles
private const string Extension = ".sbt";
// The simulator splits its text into words at spaces, tabs, line ends and semicolons.
private static readonly Regex Word = new(@"[^\s;]+", RegexOptions.CultureInvariant);
// The only words that tell what comes after them; "endterrain" is found by the second.
private static readonly SearchValues<string> Keywords =
SearchValues.Create(["include", "terrain"], StringComparison.OrdinalIgnoreCase);
private readonly IFileSystem _files;
private readonly IGamePaths _paths;
@@ -60,103 +62,277 @@ public sealed class BinaryTerrainFiles
/// <summary>
/// What the simulator would miss to make the scenery's .sbt again - the first
/// terrain file its text names and the installation lacks, or the terrain directive
/// that brings terrain from elsewhere; null when the text holds it all.
/// file with geometry its text names and the installation lacks, or the terrain
/// directive that brings terrain from elsewhere; null when the text holds it all.
/// Another scenery reading the same .sbt counts too: what that one lacks comes
/// with its name.
/// </summary>
public string? MissingOriginal(string sceneryPath)
{
string name = Path.GetFileName(sceneryPath);
try
{
var names = new CaseInsensitivePaths(_files, keepListings: true);
string? own = names.Resolve(_paths.Scenery, Path.GetFileName(sceneryPath));
if (own is null)
return Path.GetFileName(sceneryPath);
var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var pending = new Stack<string>();
pending.Push(own);
while (pending.Count > 0)
{
string file = pending.Pop();
if (!seen.Add(file))
continue;
if (Scan(file, names, pending) is { } missing)
return missing;
}
return null;
var texts = new Texts(this);
return texts.Resolve(name) is null
? name
: Unmakeable(Path.GetFileNameWithoutExtension(name) + Extension, texts);
}
catch (Exception ex)
{
_log.Log($"sbt/{Path.GetFileName(sceneryPath)}", ex);
return Path.GetFileName(sceneryPath);
_log.Log($"sbt/{name}", ex);
return name;
}
}
/// <summary>
/// Reads one file of the scenery for its includes: the terrain ones have to be
/// there, the others are read in turn. Terrain files are not opened - they are the
/// largest of all and include nothing.
/// What keeps an .sbt of the scenery folder from being made again as it is, for
/// every scenery reading it: the one it is named after, which is where the
/// simulator writes it, the same one with the <c>$</c> a scenery editor puts
/// ahead of the name, which the simulator drops, and any other naming it in a
/// terrain directive. Null when all of them can do without it.
/// </summary>
private string? Scan(string file, CaseInsensitivePaths names, Stack<string> pending)
private string? Unmakeable(string sbt, Texts texts)
{
string text = LegacyText.Decode(_files.ReadAllBytes(Path.Combine(_paths.Scenery, file)));
string code = SceneryCode.Mask(text, out _);
string stem = Path.GetFileNameWithoutExtension(sbt);
var sceneries = texts.Sceneries();
bool includeNext = false;
bool terrainDirective = false;
for (var word = Word.Match(code); word.Success; word = word.NextMatch())
// The scenery named as the file goes first: what it lacks is told without its name.
var owners = sceneries.Where(scenery => SameName(Stem(scenery), stem)).ToList();
owners.AddRange(sceneries.Where(scenery => !SameName(Stem(scenery), stem)
&& SameName(Stem(scenery).TrimStart('$'), stem)));
if (owners.Count == 0)
return stem + ".scn";
foreach (string owner in owners)
{
string token = word.Value;
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");
string? found = names.Resolve(_paths.Scenery, token);
if (found is null && terrain)
return token;
// Any other file the simulator reads from the text, .sbt or not: missing,
// it is missing either way, and the .sbt made again lacks nothing.
if (found is not null && !terrain)
pending.Push(found);
}
else if (token.Equals("include", StringComparison.OrdinalIgnoreCase))
includeNext = true;
else if (token.Equals("terrain", StringComparison.OrdinalIgnoreCase))
terrainDirective = true;
var text = texts.Of(owner);
string? missing = text.Missing
?? (text.BinaryTerrain.Count > 0 ? "terrain " + text.BinaryTerrain[0] : null);
if (missing is not null)
return SameName(Stem(owner), stem) ? missing : $"{missing} ({owner})";
}
// A scenery naming the file in a terrain directive takes its geometry from it. With the
// file gone the simulator reads that scenery's own text, which then has to hold it all.
foreach (string other in sceneries.Except(owners))
{
var text = texts.Of(other);
if (text.Missing is { } missing && text.BinaryTerrain.Any(named => Names(named, stem)))
return $"{missing} ({other})";
}
return null;
}
private static string Stem(string file) => Path.GetFileNameWithoutExtension(file);
private static bool SameName(string one, string other) =>
one.Equals(other, StringComparison.OrdinalIgnoreCase);
/// <summary>
/// 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.
/// Whether a terrain directive's word names the .sbt of the scenery folder with this
/// name, the way the simulator reads it: the <c>$</c> ahead dropped, the file looked
/// for under the scenery folder.
/// </summary>
private string? TerrainFileMissing(string token, CaseInsensitivePaths names)
private static bool Names(string word, string stem)
{
string extension = Path.GetExtension(token);
if (extension.Equals(Extension, StringComparison.OrdinalIgnoreCase))
return "terrain " + token;
string name = word.Replace('\\', '/').TrimStart('$').TrimStart('/');
return !name.Contains('/') && SameName(Stem(name), stem);
}
if (!extension.Equals(".txtf", StringComparison.OrdinalIgnoreCase)
&& !extension.Equals(".btf", StringComparison.OrdinalIgnoreCase))
return null;
/// <summary>
/// The scenery files read for one question, each of them once however many
/// sceneries include it: most of an installation's sceneries share their files.
/// </summary>
private sealed class Texts
{
private readonly BinaryTerrainFiles _owner;
private readonly CaseInsensitivePaths _names;
private readonly Dictionary<string, string?> _resolved = new(StringComparer.Ordinal);
private readonly Dictionary<string, FileText> _files;
private readonly Dictionary<string, SceneryText> _texts;
private List<string>? _sceneries;
string stem = token[..^extension.Length];
return names.Resolve(_paths.Scenery, stem + ".txtf") is null
&& names.Resolve(_paths.Scenery, stem + ".btf") is null
public Texts(BinaryTerrainFiles owner)
{
_owner = owner;
_names = new CaseInsensitivePaths(owner._files, keepListings: true);
var comparer = owner._files.IgnoresCase ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal;
_files = new Dictionary<string, FileText>(comparer);
_texts = new Dictionary<string, SceneryText>(comparer);
}
/// <summary>The file named so under the scenery folder as it is on disk; null when there is none.</summary>
public string? Resolve(string name)
{
if (!_resolved.TryGetValue(name, out string? found))
_resolved[name] = found = _names.Resolve(_owner._paths.Scenery, name);
return found;
}
/// <summary>The sceneries of the installation, by their file names.</summary>
public IReadOnlyList<string> Sceneries() =>
_sceneries ??= _owner._files.GetFiles(_owner._paths.Scenery)
.Where(file => Path.GetExtension(file).Equals(".scn", StringComparison.OrdinalIgnoreCase))
.Select(file => Path.GetFileName(file))
.OrderBy(file => file, StringComparer.Ordinal)
.ToList();
/// <summary>The whole text of a scenery, the files it includes read in turn.</summary>
public SceneryText Of(string scenery)
{
if (_texts.TryGetValue(scenery, out var known))
return known;
string? missing = null;
var binaryTerrain = new List<string>();
try
{
var seen = new HashSet<string>(_files.Comparer);
var pending = new Stack<string>();
pending.Push(scenery);
while (pending.Count > 0)
{
string file = pending.Pop();
if (!seen.Add(file))
continue;
if (!_files.TryGetValue(file, out var text))
_files[file] = text = _owner.Scan(file, this);
missing ??= text.Missing;
binaryTerrain.AddRange(text.BinaryTerrain);
foreach (string include in text.Includes)
pending.Push(include);
}
}
catch (Exception ex)
{
// Not read to its end, nothing tells what it would take to make again.
_owner._log.Log($"sbt/{scenery}", ex);
missing ??= scenery;
}
return _texts[scenery] = new SceneryText(missing, binaryTerrain);
}
}
/// <param name="Missing">The first file with geometry named there that the installation lacks.</param>
/// <param name="Includes">The files it includes that are to be read in turn.</param>
/// <param name="BinaryTerrain">The .sbt files its terrain directives name.</param>
private sealed record FileText(string? Missing, IReadOnlyList<string> Includes, IReadOnlyList<string> BinaryTerrain);
/// <param name="Missing">The first file with geometry its text names that the installation lacks.</param>
/// <param name="BinaryTerrain">The .sbt files its terrain directives name.</param>
private sealed record SceneryText(string? Missing, IReadOnlyList<string> BinaryTerrain);
// A terrain file larger than this is geometry and is left unopened - they are the largest
// files of all. A smaller one may be a list of the files that hold it.
private const long LargestTerrainFileRead = 1024 * 1024;
/// <summary>
/// Reads one file of the scenery for the files it names. With an .sbt there the
/// simulator passes over every triangle and line of the text, in any file, so
/// each scenery file included has to be there for the .sbt to be made again:
/// the terrain ones (<c>*_ter.scm</c>) and any other .scm or .scn. The node
/// templates (.inc) do not count: a scenery naming one that is not there is
/// common, and none comes with an .sbt in its place.
/// </summary>
private FileText Scan(string file, Texts texts)
{
string text = LegacyText.Decode(_files.ReadAllBytes(Path.Combine(_paths.Scenery, file)));
string code = SceneryCode.Mask(text, out _);
string? missing = null;
var includes = new List<string>();
var binaryTerrain = new List<string>();
bool includeNext = false;
bool terrainDirective = false;
int at = 0;
while (true)
{
if (!includeNext && !terrainDirective)
{
// Nothing is asked of the words up to the next one that may be a keyword,
// and a scenery file is mostly numbers.
int hit = code.AsSpan(at).IndexOfAny(Keywords);
if (hit < 0)
break;
for (at += hit; at > 0 && !Separates(code[at - 1]); at--) { }
}
if (!NextWord(code, ref at, out var token))
break;
if (includeNext)
{
includeNext = false;
string name = token.ToString();
bool terrain = SimulatorNames.Lower(name).Contains("_ter.scm");
string? found = texts.Resolve(name);
if (found is null)
{
if (terrain || IsSceneryFile(name))
missing ??= name;
}
else if (!terrain
|| _files.GetFileSize(Path.Combine(_paths.Scenery, found)) <= LargestTerrainFileRead)
includes.Add(found);
}
// An include is one wherever it stands, and a word that only reads as the start of
// a terrain directive - the name of a node, of a texture - must not hide those after it.
else if (token.Equals("include", StringComparison.OrdinalIgnoreCase))
includeNext = true;
else if (token.Equals("terrain", StringComparison.OrdinalIgnoreCase))
terrainDirective = true;
else if (token.Equals("endterrain", StringComparison.OrdinalIgnoreCase))
terrainDirective = false;
else if (terrainDirective)
{
var extension = Path.GetExtension(token);
if (extension.Equals(Extension, StringComparison.OrdinalIgnoreCase))
binaryTerrain.Add(token.ToString());
else if (extension.Equals(".txtf", StringComparison.OrdinalIgnoreCase)
|| extension.Equals(".btf", StringComparison.OrdinalIgnoreCase))
missing ??= TerrainFileMissing(token.ToString(), texts);
}
}
return new FileText(missing, includes, binaryTerrain);
}
// The simulator splits its text into words at spaces, tabs, line ends and semicolons.
private static bool Separates(char c) => c == ';' || char.IsWhiteSpace(c);
private static bool NextWord(string code, ref int at, out ReadOnlySpan<char> word)
{
while (at < code.Length && Separates(code[at]))
at++;
int start = at;
while (at < code.Length && !Separates(code[at]))
at++;
word = code.AsSpan(start, at - start);
return at > start;
}
private static bool IsSceneryFile(string name)
{
string extension = Path.GetExtension(name);
return extension.Equals(".scm", StringComparison.OrdinalIgnoreCase)
|| extension.Equals(".scn", StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// A terrain file (.txtf) named by a terrain directive 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. An .sbt named there is another matter: it holds the geometry of the
/// whole scenery and nothing makes it again, so the scenery's own goes only
/// without one.
/// </summary>
private static string? TerrainFileMissing(string token, Texts texts)
{
string stem = token[..^Path.GetExtension(token).Length];
return texts.Resolve(stem + ".txtf") is null && texts.Resolve(stem + ".btf") is null
? stem + ".txtf"
: null;
}
@@ -198,14 +374,23 @@ public sealed class BinaryTerrainFiles
var deleted = new List<string>();
var kept = new List<(string File, string Reason)>();
var failed = new List<string>();
var names = new CaseInsensitivePaths(_files);
var texts = new Texts(this);
foreach (string sbt in _files.GetFiles(_paths.Scenery)
.Where(f => Path.GetExtension(f).Equals(Extension, StringComparison.OrdinalIgnoreCase)))
{
string name = Path.GetFileName(sbt);
string? scenery = names.Resolve(_paths.Scenery, Path.GetFileNameWithoutExtension(sbt) + ".scn");
string? missing = scenery is null ? Path.GetFileNameWithoutExtension(sbt) + ".scn" : MissingOriginal(scenery);
string? missing;
try
{
missing = Unmakeable(name, texts);
}
catch (Exception ex)
{
_log.Log($"sbt/{name}", ex);
missing = Path.GetFileNameWithoutExtension(name) + ".scn";
}
if (missing is not null)
{
kept.Add((name, missing));