mirror of
https://github.com/MaSzyna-EU07/StarterNG.git
synced 2026-11-22 16:19:51 +01:00
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:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user