Files
StarterNG/StarterNG.Tests/LocalizationTests.cs
T
Hirek 76de23c706 feat(settings): a standard mode a player understands, on Vulkan, and a developer tab for the rest
- The renderer is Vulkan, written whatever the file had: the simulator's own is
  OpenGL 4.6, to which it falls back by itself where Vulkan does not run.
  OpenGL 4.6 is chosen only in the developer tab. The words of the renderers the
  simulator no longer has (legacy, simple, ...) are read as it reads them.
- The standard mode shows what a player understands: the language, the interface
  scale, the pauses and the mouse; the screen with its render scale, VSync, frame
  limit and field of view; four levels of detail - Low, Medium, High, Very high -
  and a level for each part of the picture to take further (textures, shadows,
  reflections, lighting and sky, draw distance and grass, smoke, rain and snow);
  the anti-aliasing with TAA, MSAA and DLSS/DLAA; the effects of taste; the sound;
  the physics a player changes, the recording, the devices and the keys. A search
  in the standard mode finds nothing more.
- The levels set the keys of the OpenGL 4.6 and Vulkan renderers too: lamp and
  contact shadows, SSR, the cab probe, AO, fog, clouds, grass, rain and snow.
  High is about the simulator's own values and where a new player starts; Very
  high is the most worth having, without ray tracing, MSAA x8 or the largest
  shadow maps.
- Everything else is in the Developer tab, which only the advanced mode shows,
  behind its warning: the renderer and what only Vulkan reads, the details of
  the picture, the switches of the optimisations, the logs and diagnostics, and
  the choice of the simulator.
- What makes the simulator faster stays on in the standard mode. Where the
  simulator's own default is off, the starter writes it on: gfx.gpucull,
  gfx.textures.streaming, sound.asyncload, physics.sleepheld, physics.roadlod.
  Leaving the advanced mode puts the developer tab back at the starter's values;
  the details of the picture stay as their levels hold them.
- A player in the standard mode of an older starter is moved once
  (starter.settings.revision 2): Vulkan, and the developer tab at the starter's
  values, eu07.ini copied aside to eu07.ini.before-revision2 first. A player in
  the advanced mode keeps what they chose.
- The keys the simulator reads now are written: gfx.antialiasing, below the older
  multisampling that older simulators read, and gfx.buffer.percent for the render
  scale. The ones it no longer goes by are taken out of the file on a save:
  usevbo, gfx.usegles, gfx.angleplatform, gfx.framebuffer.width/height,
  starter.bufferscale, shadows, gfx.shadows.cab.range, smoothtraction, skyenabled,
  hideconsole, slowmotion. A simulator without gfx.buffer.percent draws at the
  whole window. gfx.reflections.fidelity missing from the file is read as the
  simulator's own 2.
- The developer tab's rows and the words of its lists come in English where a
  language lacks them; the standard mode's texts are in every language.
2026-11-29 19:29:46 +01:00

256 lines
9.3 KiB
C#

using System.Xml.Linq;
using StarterNG.Infrastructure.Adapters;
using StarterNG.Services;
using StarterNG.Tests.Fakes;
namespace StarterNG.Tests;
public class LocalizationTests
{
private static LocalizationService Build() =>
new(TestLog.Over());
private static Dictionary<string, string> Strings(string path) =>
XDocument.Load(path).Descendants("String")
.ToDictionary(s => (string)s.Attribute("key")!, s => s.Value);
public static TheoryData<string> Languages()
{
var data = new TheoryData<string>();
foreach (var lang in LocalizationService.AvailableLanguages())
if (lang.Code != "en")
data.Add(lang.Code);
return data;
}
[Theory]
[MemberData(nameof(Languages))]
public void Every_language_has_every_key_but_the_optional_ones(string code)
{
var all = LocalizationService.AvailableLanguages();
var english = Strings(all.Single(l => l.Code == "en").Path);
var other = Strings(all.Single(l => l.Code == code).Path);
// The developer tab's rows (Ini.*) and the words of their lists (Choice.*) come in English where a
// language lacks them (SimulatorKeyRows), as the descriptions do.
var missing = english.Keys
.Where(key => !key.EndsWith("Desc", StringComparison.Ordinal) &&
!key.StartsWith("Cmd.", StringComparison.Ordinal) &&
!key.StartsWith("Ini.", StringComparison.Ordinal) &&
!key.StartsWith("Choice.", StringComparison.Ordinal))
.Where(key => !other.ContainsKey(key));
Assert.Empty(missing);
}
[Fact]
public void A_key_description_only_english_has_falls_back_to_english()
{
var loc = Build();
loc.Load("cz");
Assert.Equal(string.Empty, loc.Opt("Cmd.mastercontrollerincrease"));
Assert.Equal("master controller up", loc.OptOrEnglish("Cmd.mastercontrollerincrease"));
}
[Fact]
public void A_string_the_language_has_is_not_replaced_with_english()
{
var loc = Build();
loc.Load("pl");
Assert.Equal(loc["Language"], loc.OptOrEnglish("Language"));
Assert.NotEqual("Language", loc.OptOrEnglish("Language"));
}
private const string English =
"<Language code=\"en\" name=\"English\"><String key=\"Hello\">Hello</String></Language>";
/// <summary>A file cut off right after its header: it still looks like a language.</summary>
private const string CutShort = "<Language code=\"pl\" name=\"Polski\">";
private static (LocalizationService Loc, FileDiagnosticsLog Log, string Directory) InFolderOf(
params (string File, string Text)[] languages)
{
string directory = Directory.CreateTempSubdirectory("starterng-lang-").FullName;
foreach (var (file, text) in languages)
File.WriteAllText(Path.Combine(directory, file), text);
var log = TestLog.Over();
return (new LocalizationService(log, directory), log, directory);
}
[Fact]
public void A_translation_cut_short_counts_as_missing_and_is_logged()
{
var (loc, log, directory) = InFolderOf(("en.xml", English), ("pl.xml", CutShort));
try
{
Assert.Equal("English", loc.Load("pl"));
Assert.Equal("en", loc.CurrentLangCode);
Assert.Equal("Hello", loc["Hello"]);
Assert.Contains("lang/pl.xml", log.Text);
}
finally
{
Directory.Delete(directory, recursive: true);
}
}
[Fact]
public void With_every_translation_cut_short_the_starter_still_starts()
{
var (loc, log, directory) = InFolderOf(("pl.xml", CutShort));
try
{
Assert.Equal("English", loc.Load("pl"));
Assert.Equal("en", loc.CurrentLangCode);
Assert.Equal("Hello", loc["Hello"]);
Assert.Contains("lang/pl.xml", log.Text);
}
finally
{
Directory.Delete(directory, recursive: true);
}
}
[Fact]
public void A_language_cut_short_leaves_english_rather_than_the_one_in_use_before()
{
var (loc, _, directory) = InFolderOf(
("cz.xml", "<Language code=\"cz\" name=\"Cestina\"><String key=\"Hello\">Ahoj</String></Language>"),
("en.xml", English),
("pl.xml", CutShort));
try
{
loc.Load("cz");
Assert.Equal("Ahoj", loc["Hello"]);
loc.Load("pl");
Assert.Equal("en", loc.CurrentLangCode);
Assert.Equal("Hello", loc["Hello"]);
}
finally
{
Directory.Delete(directory, recursive: true);
}
}
/// <summary>A player's folder, the bundled one with English in it, the old starter's and the simulator's.</summary>
private sealed class Folders : IDisposable
{
public readonly string Root = Directory.CreateTempSubdirectory("starterng-langs-").FullName;
public string Own => Sub("own");
public string Bundled => Sub("bundled");
public string OldStarter => Sub("starter");
public string Simulator => Sub("lang");
private string Sub(string name) => Directory.CreateDirectory(Path.Combine(Root, name)).FullName;
public LocalizationService Service() =>
new(TestLog.Over(),
Bundled, new LanguageFolders(new[] { Own, Bundled }, OldStarter, Simulator));
public void Dispose() => Directory.Delete(Root, recursive: true);
}
[Fact]
public void A_players_own_translation_wins_over_the_bundled_one()
{
using var folders = new Folders();
File.WriteAllText(Path.Combine(folders.Bundled, "en.xml"), English);
File.WriteAllText(Path.Combine(folders.Bundled, "pl.xml"),
"<Language code=\"pl\" name=\"Polski\"><String key=\"Hello\">Czesc</String></Language>");
File.WriteAllText(Path.Combine(folders.Own, "moj.xml"),
"<Language code=\"pl\" name=\"Moj polski\"><String key=\"Hello\">Witaj</String></Language>");
var loc = folders.Service();
Assert.Equal("Moj polski", loc.Load("pl"));
Assert.Equal("Witaj", loc["Hello"]);
Assert.True(loc.HasOwnTexts);
Assert.Single(loc.Languages(), lang => lang.Code == "pl");
}
[Fact]
public void An_old_starters_language_is_offered_in_english_with_its_key_descriptions()
{
using var folders = new Folders();
File.WriteAllText(Path.Combine(folders.Bundled, "en.xml"), English);
File.WriteAllText(Path.Combine(folders.OldStarter, "lang-ZH.txt"),
"[Main]\ncbLang.Items.Text=PL|EN\n[labels]\nx=y\n<eu07_input-keyboard.ini\nhornhighactivate=gwizd\n\n[frmOther]\nz=q\n");
var loc = folders.Service();
Assert.Contains(loc.Languages(), lang => lang.Code == "zh" && lang.Name == "ZH" && lang.Path.Length == 0);
loc.Load("zh");
Assert.Equal("zh", loc.CurrentLangCode);
Assert.False(loc.HasOwnTexts);
Assert.Equal("Hello", loc["Hello"]);
Assert.Equal("gwizd", loc.Opt("Cmd.hornhighactivate"));
Assert.Equal(string.Empty, loc.Opt("Cmd.z"));
}
[Fact]
public void An_old_starters_key_descriptions_are_read_in_its_own_code_page()
{
using var folders = new Folders();
File.WriteAllText(Path.Combine(folders.Bundled, "en.xml"), English);
File.WriteAllBytes(Path.Combine(folders.OldStarter, "lang-cz.txt"), LegacyText.CodePage1250.GetBytes(
"[labels]\r\n<eu07_input-keyboard.ini\r\nhornhighactivate=Houkačka výšková\r\n\r\n"));
var loc = folders.Service();
loc.Load("cz");
Assert.Equal("Houkačka výšková", loc.Opt("Cmd.hornhighactivate"));
}
[Fact]
public void Of_the_simulators_files_only_a_language_code_counts()
{
using var folders = new Folders();
File.WriteAllText(Path.Combine(folders.Bundled, "en.xml"), English);
foreach (string file in new[] { "hu.po", "editor_pl.po", "template.po" })
File.WriteAllText(Path.Combine(folders.Simulator, file), string.Empty);
var codes = folders.Service().Languages().Select(lang => lang.Code).ToList();
Assert.Contains("hu", codes);
Assert.DoesNotContain("editor_pl", codes);
Assert.DoesNotContain("template", codes);
}
[Fact]
public void A_code_eu07_ini_names_stays_on_the_list_once()
{
using var folders = new Folders();
File.WriteAllText(Path.Combine(folders.Bundled, "en.xml"), English);
var loc = folders.Service();
loc.Keep("sk");
loc.Keep("SK");
loc.Keep("en");
Assert.Single(loc.Languages(), lang => lang.Code == "sk");
Assert.Equal(2, loc.Languages().Count);
}
[Fact]
public void A_key_a_translation_lacks_comes_in_english()
{
using var folders = new Folders();
File.WriteAllText(Path.Combine(folders.Bundled, "en.xml"), English);
File.WriteAllText(Path.Combine(folders.Own, "sk.xml"),
"<Language code=\"sk\" name=\"Slovensky\"><String key=\"Other\">Ine</String></Language>");
var loc = folders.Service();
loc.Load("sk");
Assert.Equal("Hello", loc["Hello"]);
Assert.Equal("Ine", loc["Other"]);
}
}