using StarterNG.Classes; using StarterNG.Domain.Settings; using StarterNG.Infrastructure.Settings; using StarterNG.Tests.Fakes; namespace StarterNG.Tests; public class GraphicsSettingsTests { private static readonly string UserIni = Path.Combine("/home/kolejarz", ".config", "MaSzyna", "eu07.ini"); private static TestInstallation WithIni(string text) { var installation = new TestInstallation(new InMemoryFileSystem().WithFile(UserIni, text)); installation.Environment.With("HOME", "/home/kolejarz"); installation.SettingsStore.Load(); return installation; } private static SimulatorSettings Read(string text) { var s = new SimulatorSettings(); new SettingsSerializer().Read(s, ConfigFile.Parse(text)); return s; } private static string Write(SimulatorSettings s, string text = "") { var c = ConfigFile.Parse(text); new SettingsSerializer().Write(s, c); return c.ToText(); } private static string[] Lines(string text) => text.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None); private static string? Line(string text, string key) => Lines(text).FirstOrDefault(l => l == key || l.StartsWith(key + " ", StringComparison.Ordinal)); // As utilities/Globals.cpp has them; a word the simulator does not know is the file's own, written back as it is. [Theory] [InlineData("vulkan", "vulkan")] [InlineData("VULKAN14", "vulkan")] [InlineData("default", "default")] [InlineData("full", "default")] [InlineData("gl46", "default")] [InlineData("opengl", "default")] [InlineData("legacy", "default")] [InlineData("simple", "default")] [InlineData("experimental", "default")] [InlineData("null", "null")] public void A_renderer_is_written_by_the_name_the_simulator_reads_it_as(string read, string written) { Assert.Equal($"gfxrenderer {written}", Line(Write(Read($"gfxrenderer {read}\n")), "gfxrenderer")); } [Fact] public void Without_a_renderer_in_the_file_vulkan_is_chosen_and_written() { // The simulator's own is OpenGL: the starter writes Vulkan whatever the file had. Assert.Equal(GraphicsBackend.Vulkan, Read("").Backend); Assert.Equal("gfxrenderer vulkan", Line(Write(Read("")), "gfxrenderer")); } [Fact] public void Opengl_stays_chosen_and_vulkan_can_be_chosen_again() { var installation = WithIni("gfxrenderer default\n"); var store = installation.SettingsStore; Assert.Equal(GraphicsBackend.OpenGL, store.Settings.Backend); store.Settings.FieldOfView = 50; store.Save(); Assert.Contains("gfxrenderer default", installation.Files.ReadAllText(UserIni)); store.Settings.Backend = GraphicsBackend.Vulkan; store.Save(); Assert.Equal("gfxrenderer vulkan", Line(installation.Files.ReadAllText(UserIni), "gfxrenderer")); } [Fact] public void Keys_the_simulator_no_longer_goes_by_are_taken_out_on_a_save() { string[] old = { "usevbo no", "gfx.angleplatform vulkan", "gfx.usegles yes", "gfx.shadows.cab.range 20", "shadows yes", "skyenabled yes", "smoothtraction yes", "gfx.framebuffer.width 1280", "gfx.framebuffer.height 720", "starter.bufferscale 100" }; var installation = WithIni(string.Join("\n", old) + "\n"); installation.SettingsStore.Save(); string saved = installation.Files.ReadAllText(UserIni); Assert.All(old, line => Assert.Null(Line(saved, line.Split(' ')[0]))); } [Fact] public void Keys_of_the_new_renderers_are_kept_as_the_file_has_them() { string[] vulkan = { "gfx.antialiasing dlss", "gfx.dlss.preset k", "gfx.framegeneration x2", "gfx.reflex on", "gfx.raytracing yes", "gfx.ssr high", "gfx.postfx.ao gtao", "gfx.clouds ultra", "gfx.grass yes", "gfx.buffer.percent 0.67", "gfx.vulkan.device RTX", "gfx.fog yes", "gfx.cabprobe medium", "gfx.gpucull no" }; var installation = WithIni("calibratein 0 0 0 0\nmultisampling 0\n" + string.Join("\n", vulkan) + "\n"); installation.SettingsStore.Save(); string saved = installation.Files.ReadAllText(UserIni); Assert.Contains("calibratein 0 0 0 0", saved); Assert.All(vulkan, line => Assert.Equal(line, Line(saved, line.Split(' ')[0]))); Assert.Equal(AntiAliasingMode.Dlss, installation.SettingsStore.Settings.AntiAliasing); Assert.Equal(67, installation.SettingsStore.Settings.BufferScalePercent); } [Fact] public void The_faster_choices_the_simulator_does_not_make_itself_are_written() { var installation = WithIni("width 1280\n"); installation.SettingsStore.Save(); string saved = installation.Files.ReadAllText(UserIni); foreach (var key in SimulatorKeys.All.Where(key => key.Writes)) Assert.Equal($"{key.Key} {key.Default}", Line(saved, key.Key)); Assert.Equal("gfx.gpucull yes", Line(saved, "gfx.gpucull")); } [Theory] [InlineData("multisampling 0\n", AntiAliasingMode.Off)] [InlineData("multisampling 1\n", AntiAliasingMode.Msaa2)] [InlineData("multisampling 2\n", AntiAliasingMode.Msaa4)] [InlineData("multisampling 3\n", AntiAliasingMode.Msaa8)] [InlineData("", AntiAliasingMode.Msaa4)] [InlineData("gfx.antialiasing taa\n", AntiAliasingMode.Taa)] [InlineData("multisampling 3\ngfx.antialiasing taa\n", AntiAliasingMode.Taa)] [InlineData("gfx.antialiasing taa\nmultisampling 3\n", AntiAliasingMode.Msaa8)] [InlineData("multisampling 2\ngfx.antialiasing dlaa\n", AntiAliasingMode.Dlaa)] [InlineData("multisampling 1\ngfx.antialiasing off\n", AntiAliasingMode.Off)] public void The_anti_aliasing_is_read_from_the_key_further_down(string text, AntiAliasingMode expected) { Assert.Equal(expected, Read(text).AntiAliasing); } [Theory] [InlineData(AntiAliasingMode.Msaa2, "1", "msaax2")] [InlineData(AntiAliasingMode.Taa, "0", "taa")] [InlineData(AntiAliasingMode.Dlss, "0", "dlss")] [InlineData(AntiAliasingMode.Off, "0", "off")] public void The_anti_aliasing_is_written_to_both_keys(AntiAliasingMode mode, string multisampling, string word) { string text = Write(new SimulatorSettings { AntiAliasing = mode }); Assert.Equal($"multisampling {multisampling}", Line(text, "multisampling")); Assert.Equal($"gfx.antialiasing {word}", Line(text, "gfx.antialiasing")); } [Fact] public void A_save_puts_the_newer_anti_aliasing_key_below_the_older() { var installation = WithIni("gfx.antialiasing msaax4\nwidth 1280\nmultisampling 2\n"); installation.SettingsStore.Settings.AntiAliasing = AntiAliasingMode.Taa; installation.SettingsStore.Save(); var lines = Lines(installation.Files.ReadAllText(UserIni)).ToList(); Assert.True(lines.IndexOf("gfx.antialiasing taa") > lines.IndexOf("multisampling 0")); Assert.Equal(AntiAliasingMode.Taa, Read(installation.Files.ReadAllText(UserIni)).AntiAliasing); } [Fact] public void Sizes_are_rounded_down_to_what_the_simulator_uses() { var s = Read("maxtexturesize 16384\nmaxcabtexturesize 3000\nshadowtune 3000 0 250 0\n"); Assert.Equal(8192, s.MaxTextureSize); Assert.Equal(2048, s.MaxCabTextureSize); Assert.Equal(2048, s.ShadowMapResolution); } [Fact] public void The_render_scale_is_the_buffer_percent_or_what_an_older_starter_left() { Assert.Equal(50, Read("gfx.buffer.percent 0.5\n").BufferScalePercent); Assert.Equal(75, Read("gfx.buffer.percent 75\n").BufferScalePercent); Assert.Equal(75, Read("starter.bufferscale 75\n").BufferScalePercent); Assert.Equal(50, Read("width 1920\ngfx.framebuffer.width 960\n").BufferScalePercent); Assert.Equal(100, Read("").BufferScalePercent); } [Fact] public void The_render_scale_is_written_as_the_buffer_percent() { var s = Read("width 1920\nheight 1080\nstarter.bufferscale 50\n"); string text = Write(s); Assert.Equal("gfx.buffer.percent 0.5", Line(text, "gfx.buffer.percent")); Assert.Null(Line(text, "gfx.framebuffer.width")); Assert.Null(Line(text, "starter.bufferscale")); } [Fact] public void A_level_is_recognised_after_it_is_applied_and_survives_a_save() { foreach (var backend in new[] { GraphicsBackend.Vulkan, GraphicsBackend.OpenGL }) { foreach (var level in GraphicsPreset.All) { var installation = WithIni(""); var store = installation.SettingsStore; store.Settings.Backend = backend; level.ApplyTo(store.Settings); Assert.Same(level, GraphicsPreset.Matching(store.Settings)); store.Save(); var again = new TestInstallation(installation.Files); again.Environment.With("HOME", "/home/kolejarz"); again.SettingsStore.Load(); Assert.Same(level, GraphicsPreset.Matching(again.SettingsStore.Settings)); Assert.Equal(backend, again.SettingsStore.Settings.Backend); } } } [Fact] public void A_change_after_a_level_leaves_the_level() { var s = new SimulatorSettings(); GraphicsPreset.High.ApplyTo(s); s.ShadowRankCutoff = 1; Assert.Null(GraphicsPreset.Matching(s)); } [Fact] public void Levels_leave_alone_what_is_not_a_matter_of_quality() { var s = new SimulatorSettings { Width = 2560, Height = 1440, VSync = true, FieldOfView = 60, MotionBlur = false }; GraphicsPreset.Low.ApplyTo(s); Assert.Equal((2560, 1440, true, 60, false), (s.Width, s.Height, s.VSync, s.FieldOfView, s.MotionBlur)); } [Fact] public void No_level_leaves_decorative_consists_out() { foreach (var level in GraphicsPreset.All) { var s = new SimulatorSettings { IgnoreIrrelevantTrains = false }; level.ApplyTo(s); Assert.False(s.IgnoreIrrelevantTrains, level.NameKey); s.IgnoreIrrelevantTrains = true; level.ApplyTo(s); Assert.True(s.IgnoreIrrelevantTrains, level.NameKey); } } [Fact] public void An_import_takes_the_settings_but_keeps_this_installations_simulator_and_the_unmanaged_lines() { var installation = WithIni("starter.exe.auto no\nstarter.exe.path eu07_local.exe\ncalibratein 0 0 0 0\nfieldofview 45\n"); installation.Files.WithFile("/backup/eu07.ini", "starter.exe.auto no\nstarter.exe.path eu07_other.exe\nfieldofview 60\ngfxrenderer legacy\n"); Assert.True(installation.SettingsStore.ImportFrom("/backup/eu07.ini")); var s = installation.SettingsStore.Settings; Assert.Equal(60, s.FieldOfView); Assert.Equal(GraphicsBackend.OpenGL, s.Backend); Assert.Equal("eu07_local.exe", s.ExecutablePath); // Nothing is written until saved, and the save keeps the user's own lines. Assert.Contains("fieldofview 45", installation.Files.ReadAllText(UserIni)); installation.SettingsStore.Save(); string saved = installation.Files.ReadAllText(UserIni); Assert.Contains("calibratein 0 0 0 0", saved); Assert.Contains("fieldofview 60", saved); Assert.Contains("starter.exe.path eu07_local.exe", saved); } [Fact] public void An_import_of_a_missing_file_changes_nothing() { var installation = WithIni("fieldofview 50\n"); Assert.False(installation.SettingsStore.ImportFrom("/nowhere/eu07.ini")); Assert.Equal(50, installation.SettingsStore.Settings.FieldOfView); } [Fact] public void An_export_writes_a_complete_file_elsewhere_and_leaves_the_users_one_alone() { var installation = WithIni("calibratein 0 0 0 0\nfieldofview 45\n"); installation.SettingsStore.Settings.FieldOfView = 60; Assert.True(installation.SettingsStore.ExportTo("/export/eu07.ini")); string exported = installation.Files.ReadAllText("/export/eu07.ini"); Assert.Contains("calibratein 0 0 0 0", exported); Assert.Contains("fieldofview 60", exported); Assert.Contains("fieldofview 45", installation.Files.ReadAllText(UserIni)); } [Fact] public void An_export_keeps_the_older_renderer_word_of_the_file() { var installation = WithIni("gfxrenderer legacy\n"); Assert.True(installation.SettingsStore.ExportTo("/export/eu07.ini")); Assert.Contains("gfxrenderer legacy", installation.Files.ReadAllText("/export/eu07.ini")); } [Fact] public void The_gamepad_is_ignored_unless_the_file_turns_it_on() { Assert.True(Read("").IgnoreGamepad); Assert.Equal("input.gamepad no", Line(Write(Read("")), "input.gamepad")); Assert.False(Read("input.gamepad yes\n").IgnoreGamepad); Assert.True(Read("input.gamepad no\n").IgnoreGamepad); } [Fact] public void The_standard_mode_is_where_a_player_starts_and_the_choice_is_kept() { Assert.False(Read("").AdvancedSettings); Assert.True(Read("starter.settings.advanced yes\n").AdvancedSettings); var s = Read(""); s.AdvancedSettings = true; Assert.Equal("starter.settings.advanced yes", Line(Write(s), "starter.settings.advanced")); } [Fact] public void The_move_to_the_standard_mode_is_marked_in_the_file() { Assert.False(Read("").MovedToStandard); Assert.True(Read("starter.settings.standard yes\n").MovedToStandard); var s = Read(""); s.MovedToStandard = true; Assert.Equal("starter.settings.standard yes", Line(Write(s), "starter.settings.standard")); } [Fact] public void A_later_line_moved_up_keeps_the_index_of_every_key() { var c = ConfigFile.Parse("a 1\nb 2\nc 3\n"); c.MoveAfter("a", "c"); Assert.Equal("b 2\nc 3\na 1\n", c.ToText().Replace("\r\n", "\n")); Assert.True(c.LineOf("a") > c.LineOf("c")); c.Set("b", "9"); Assert.Equal("b 9\nc 3\na 1\n", c.ToText().Replace("\r\n", "\n")); } }