diff --git a/StarterNG.Tests/ConfigFileTests.cs b/StarterNG.Tests/ConfigFileTests.cs index 6618e99..ce1b208 100644 --- a/StarterNG.Tests/ConfigFileTests.cs +++ b/StarterNG.Tests/ConfigFileTests.cs @@ -174,6 +174,50 @@ public class ConfigFileTests Assert.Equal(new[] { "// width 800", "endconfig", "// width 1920", "// height 600", "" }, Lines(config)); } + private static readonly string[] Repeatable = { "network.server", "python.viewport" }; + + [Fact] + public void A_key_set_twice_keeps_only_the_line_the_simulator_reads() + { + var config = ConfigFile.Parse("width 800\nheight 600\nWIDTH 1024\t// mine\nwidth 1920\nfullscreen yes\n"); + + var removed = config.RemoveDuplicates(Repeatable); + + Assert.Equal(new[] { "height 600", "width 1920", "fullscreen yes", "" }, Lines(config)); + Assert.Equal("1920", config.GetRaw("width")); + Assert.Equal(new[] { "width", "WIDTH" }, removed); + } + + [Fact] + public void Keys_the_simulator_takes_many_of_stay_every_one() + { + const string text = "network.server zmq tcp://a\nnetwork.server zmq tcp://b\npython.viewport a\npython.viewport b\n"; + var config = ConfigFile.Parse(text); + + Assert.Empty(config.RemoveDuplicates(Repeatable)); + Assert.Equal(ConfigFile.Parse(text).ToText(), config.ToText()); + } + + [Fact] + public void Past_endconfig_and_in_comments_nothing_is_removed() + { + const string text = "width 800\nendconfig\nwidth 1920\nheight 1\nheight 2\n"; + var config = ConfigFile.Parse("// width 640\n/* width 700\nwidth 720 */\n" + text); + + Assert.Empty(config.RemoveDuplicates(Repeatable)); + Assert.Equal("800", config.GetRaw("width")); + } + + [Fact] + public void A_line_of_numbers_carrying_on_a_value_is_no_key_of_its_own() + { + const string text = "uarttune\n0 0 0\n0 0 0\nwidth 800\n"; + var config = ConfigFile.Parse(text); + + Assert.Empty(config.RemoveDuplicates(Repeatable)); + Assert.Equal(ConfigFile.Parse(text).ToText(), config.ToText()); + } + [Fact] public void Without_endconfig_a_new_key_goes_at_the_end_as_it_did() { diff --git a/StarterNG.Tests/ExternalIniTests.cs b/StarterNG.Tests/ExternalIniTests.cs index 7086035..55a4655 100644 --- a/StarterNG.Tests/ExternalIniTests.cs +++ b/StarterNG.Tests/ExternalIniTests.cs @@ -71,17 +71,17 @@ public class ExternalIniTests [Fact] public void A_renderer_written_in_by_hand_while_the_starter_runs_survives_its_save() { - // Notepad: "gfxrenderer vulkan", a word the starter does not offer. + // Notepad: "gfxrenderer null", a word the starter does not offer. var installation = WithIni("gfxrenderer default\nfieldofview 45\n"); - Edited(installation, "gfxrenderer vulkan\nfieldofview 45\n"); + Edited(installation, "gfxrenderer null\nfieldofview 45\n"); installation.SettingsStore.Settings.FieldOfView = 60; installation.SettingsStore.Save(); string saved = installation.Files.ReadAllText(UserIni); - Assert.Contains("gfxrenderer vulkan", saved); + Assert.Contains("gfxrenderer null", saved); Assert.Contains("fieldofview 60", saved); - Assert.Equal("vulkan", installation.SettingsStore.Settings.RendererWord); + Assert.Equal("null", installation.SettingsStore.Settings.RendererWord); } [Fact] @@ -188,8 +188,9 @@ public class ExternalIniTests string saved = installation.Files.ReadAllText(UserIni); Assert.Contains("maxcabtexturesize 4096", saved); Assert.Contains("shadowtune 4096 250 400 300", saved); - Assert.Contains("gfx.framebuffer.width 1920", saved); - Assert.Contains("gfx.framebuffer.height 1080", saved); + // The render scale is the simulator's own key now, at the whole window. + Assert.Contains("gfx.buffer.percent 1", saved); + Assert.DoesNotContain("\ngfx.framebuffer.width", "\n" + saved); Assert.Contains("fieldofview 50", saved); } diff --git a/StarterNG.Tests/GraphicsPresetTests.cs b/StarterNG.Tests/GraphicsPresetTests.cs index 6de00f4..7298e43 100644 --- a/StarterNG.Tests/GraphicsPresetTests.cs +++ b/StarterNG.Tests/GraphicsPresetTests.cs @@ -13,98 +13,199 @@ public class GraphicsPresetTests return installation.SettingsStore.Settings; } - private static SimulatorSettings AtHigh() + private static SimulatorSettings At(GraphicsPreset preset) { var settings = Defaults().Copy(); - GraphicsPreset.High.ApplyTo(settings); + preset.ApplyTo(settings); return settings; } + private static SimulatorSettings AtHigh() => At(GraphicsPreset.High); + + /// The keys two renderings give different values, a key one leaves out at its default. + private static IEnumerable Differing(IReadOnlyDictionary one, IReadOnlyDictionary other) + { + string? Value(IReadOnlyDictionary keys, string key) => + keys.TryGetValue(key, out string? value) ? value : SimulatorKeys.All.FirstOrDefault(k => k.Key == key)?.Default; + return one.Keys.Union(other.Keys).Where(key => Value(one, key) != Value(other, key)); + } + [Fact] - public void The_keys_named_are_the_keys_a_level_sets() + public void A_level_sets_no_key_but_those_it_names() { var store = new TestInstallation().SettingsStore; - var low = Defaults().Copy(); - var top = Defaults().Copy(); - (GraphicsPreset.Minimal with { Extras = null }).ApplyTo(low); - (GraphicsPreset.Ultra with { Extras = null, Top = null }).ApplyTo(top); + var own = store.Render(Defaults()); + foreach (var preset in GraphicsPreset.All) + Assert.Empty(Differing(own, store.Render(At(preset))).Except(GraphicsPreset.AllKeys)); + } - var lowKeys = store.Render(low); - var topKeys = store.Render(top); - var differing = lowKeys.Keys.Union(topKeys.Keys) - .Where(key => lowKeys.GetValueOrDefault(key) != topKeys.GetValueOrDefault(key)); + [Fact] + public void Every_key_of_a_level_belongs_to_one_part_or_to_the_anti_aliasing() + { + var inGroups = GraphicsGroup.All.SelectMany(group => group.Keys).ToList(); - Assert.Equal(GraphicsPreset.Keys.OrderBy(k => k), differing.OrderBy(k => k)); + Assert.Equal(inGroups.Distinct(), inGroups); + Assert.Empty(GraphicsPreset.All.SelectMany(preset => preset.Keys.Keys).Except(inGroups)); } // ── The scale ──────────────────────────────────────────────────────────── - private static void Climbs(Func value, int repeatsAllowed) + private static void Climbs(Func value) { var values = GraphicsPreset.All.Select(value).ToList(); - Assert.Equal(values.OrderBy(v => v), values); - Assert.True(values.Count - values.Distinct().Count() <= repeatsAllowed, string.Join(", ", values)); } [Fact] - public void Every_slider_moves_from_level_to_level_as_far_as_it_has_steps_for_six() + public void Every_part_of_the_picture_gets_better_or_stays_from_level_to_level() { - // Sizes and filtering run out of steps above High. - Climbs(p => p.TextureSize, 1); - Climbs(p => p.CabTextureSize, 2); - Climbs(p => p.Anisotropy, 1); + Climbs(p => p.TextureSize); + Climbs(p => p.CabTextureSize); + Climbs(p => p.Anisotropy); + Climbs(p => p.ShadowMap); + Climbs(p => p.ShadowRange); + Climbs(p => p.ShadowRank); + Climbs(p => p.ReflectionsFidelity); + Climbs(p => p.ReflectionsRate); + Climbs(p => p.SplineFidelity); + Climbs(p => p.DrawRangeFactor); + Climbs(p => p.SmokeFidelity); - // Four steps or three for six levels. - Climbs(p => (int)p.AntiAliasing, 2); - Climbs(p => p.SmokeFidelity, 2); - Climbs(p => p.SplineFidelity, 2); - Climbs(p => p.ReflectionsFidelity, 3); - Climbs(p => p.ShadowRank, 3); - - // The simulator takes no more than its own 7 lights. - Climbs(p => p.DynamicLights, 2); - Climbs(p => p.DrawRangeFactor, 3); - Climbs(p => p.CabShadowsRange, 1); - - Climbs(p => p.ShadowMap, 2); - Climbs(p => p.ShadowRange, 1); - // The old starter's steps begin at once a second: the lower levels share it. - Climbs(p => p.ReflectionsRate, 2); - } - - [Fact] - public void No_step_up_a_scale_is_more_than_a_few_steps_of_its_slider() - { - // In steps of what the screen offers, so Ultra does not jump to the top from far below. - var rates = GraphicsPreset.All.Select(p => Steps.IndexOf(SliderRanges.ReflectionUpdates, p.ReflectionsRate)).ToList(); - Assert.All(rates.Zip(rates.Skip(1)), step => Assert.InRange(step.Second - step.First, 0, 2)); - - foreach (Func fidelity in new Func[] { p => p.SmokeFidelity, p => p.SplineFidelity }) + foreach (string name in GraphicsPreset.Low.Keys.Keys) { - var levels = GraphicsPreset.All.Select(fidelity).ToList(); - Assert.All(levels.Zip(levels.Skip(1)), step => Assert.InRange(step.Second - step.First, 0, 1)); + var key = SimulatorKeys.Named(name); + Climbs(p => key.Kind switch + { + SimulatorKeyKind.Choice => key.ChoiceIndex(p.Keys[name]), + SimulatorKeyKind.Toggle => SimulatorKey.ParseToggle(p.Keys[name]) ? 1 : 0, + _ => key.ParseNumber(p.Keys[name]) + }); } } [Fact] - public void High_is_the_simulators_own_but_where_that_is_the_bottom_of_a_scale() + public void Every_level_sets_the_same_keys() { - var store = new TestInstallation().SettingsStore; - var own = store.Render(Defaults()); - var high = store.Render(AtHigh()); - - var differing = own.Keys.Union(high.Keys) - .Where(key => own.GetValueOrDefault(key) != high.GetValueOrDefault(key)); - - Assert.Equal(new[] { "gfx.reflections.fidelity", "gfx.reflections.framerate", "gfx.shadows.cab.range", - "gfx.smoke.fidelity", "splinefidelity" }, differing.OrderBy(k => k)); - Assert.Equal(1, Defaults().SmokeFidelity); - Assert.Equal(1, Defaults().SplineFidelity); + foreach (var preset in GraphicsPreset.All) + Assert.Equal(GraphicsPreset.High.Keys.Keys.OrderBy(k => k), preset.Keys.Keys.OrderBy(k => k)); } [Fact] - public void A_player_with_no_settings_of_their_own_starts_at_high() + public void Every_value_of_a_level_is_one_its_row_offers() + { + foreach (var preset in GraphicsPreset.All) + foreach (var (name, value) in preset.Keys) + { + var key = SimulatorKeys.Named(name); + switch (key.Kind) + { + case SimulatorKeyKind.Choice: + Assert.True(key.ChoiceIndex(value) >= 0, $"{preset.NameKey} {name} {value}"); + break; + case SimulatorKeyKind.Number: + Assert.InRange(key.ParseNumber(value), key.Minimum, key.Maximum); + break; + } + } + } + + [Fact] + public void High_is_the_simulators_own_but_for_the_smoke_and_the_track_curves() + { + var store = new TestInstallation().SettingsStore; + + var differing = Differing(store.Render(Defaults()), store.Render(AtHigh())); + + Assert.Equal(new[] { "gfx.smoke.fidelity", "splinefidelity" }, differing.OrderBy(k => k, StringComparer.Ordinal)); + } + + [Fact] + public void Very_high_is_not_everything_at_its_top() + { + var top = GraphicsPreset.VeryHigh; + + Assert.True(top.AntiAliasing <= AntiAliasingMode.Msaa4); + Assert.True(top.ShadowMap <= 4096); + Assert.False(top.Keys.ContainsKey("gfx.raytracing")); + Assert.NotEqual("ultra", top.Keys["gfx.clouds"]); + Assert.NotEqual("ultra", top.Keys["gfx.fog.quality"]); + } + + [Fact] + public void Low_draws_no_screen_space_effects() + { + Assert.Equal(AntiAliasingMode.Off, GraphicsPreset.Low.AntiAliasing); + Assert.Equal("no", GraphicsPreset.Low.Keys["gfx.ssr"]); + Assert.Equal("no", GraphicsPreset.Low.Keys["gfx.postfx.ao"]); + Assert.Equal("no", GraphicsPreset.Low.Keys["gfx.contactshadows"]); + } + + [Fact] + public void Every_level_sits_on_the_steps_the_screen_offers() + { + foreach (var preset in GraphicsPreset.All) + { + Assert.True(Steps.IndexOf(Steps.Anisotropy, preset.Anisotropy) >= 0, preset.NameKey); + Assert.True(Steps.IndexOf(SliderRanges.ReflectionUpdates, preset.ReflectionsRate) >= 0, preset.NameKey); + Assert.True(Steps.IndexOf(SliderRanges.DrawRangeFactors, preset.DrawRangeFactor) >= 0, preset.NameKey); + foreach (int size in new[] { preset.TextureSize, preset.CabTextureSize, preset.ShadowMap }) + Assert.True(size is >= 512 and <= 8192 && (size & (size - 1)) == 0, preset.NameKey); + Assert.True(preset.CabTextureSize <= GraphicsRules.MaxCabTextureSize, preset.NameKey); + Assert.True(preset.ShadowRange >= SimulatorSettings.ShadowRangeFloor(preset.ShadowMap), preset.NameKey); + Assert.True(preset.ShadowRange <= GraphicsRules.MaxShadowRange, preset.NameKey); + } + } + + [Fact] + public void A_level_leaves_the_screen_and_the_matters_of_taste_as_they_are() + { + var settings = Defaults().Copy(); + settings.MotionBlur = false; + settings.ChromaticAberration = false; + settings.Smoke = false; + settings.PythonScreens = false; + settings.FieldOfView = 60; + settings.BufferScalePercent = 75; + settings.Backend = GraphicsBackend.OpenGL; + + GraphicsPreset.VeryHigh.ApplyTo(settings); + + Assert.False(settings.MotionBlur); + Assert.False(settings.ChromaticAberration); + Assert.False(settings.Smoke); + Assert.False(settings.PythonScreens); + Assert.Equal(60, settings.FieldOfView); + Assert.Equal(75, settings.BufferScalePercent); + Assert.Equal(GraphicsBackend.OpenGL, settings.Backend); + } + + [Fact] + public void A_level_applied_is_recognised_until_one_value_moves() + { + foreach (var preset in GraphicsPreset.All) + { + var settings = At(preset); + Assert.Same(preset, GraphicsPreset.Matching(settings)); + + settings.Extra["gfx.ssr"] = preset.Keys["gfx.ssr"] == "high" ? "low" : "high"; + Assert.Null(GraphicsPreset.Matching(settings)); + } + } + + [Fact] + public void A_level_is_recognised_with_its_words_written_as_numbers() + { + var settings = AtHigh(); + settings.Extra["gfx.ssr"] = "2"; + settings.Extra["gfx.lights.shadows"] = "2"; + + Assert.Same(GraphicsPreset.High, GraphicsPreset.Matching(settings)); + } + + // ── The first start ────────────────────────────────────────────────────── + + [Fact] + public void A_player_with_no_settings_of_their_own_starts_at_high_on_vulkan() { var installation = new TestInstallation(); var store = installation.SettingsStore; @@ -118,8 +219,10 @@ public class GraphicsPresetTests var again = new TestInstallation(installation.Files).SettingsStore; again.Load(); Assert.Same(GraphicsPreset.High, GraphicsPreset.Matching(again.Settings)); - // Nothing of an older starter to bring into the standard mode. + Assert.Equal(GraphicsBackend.Vulkan, again.Settings.Backend); + // Nothing of an older starter to bring into the standard mode, nor of an older layout. Assert.True(again.Settings.MovedToStandard); + Assert.Equal(SimulatorSettings.CurrentRevision, again.Settings.Revision); } [Fact] @@ -159,369 +262,62 @@ public class GraphicsPresetTests Assert.Equal(first, installation.Files.ReadAllText(copy)); } + // ── Ray tracing ────────────────────────────────────────────────────────── + + [Theory] + [InlineData("no", "500", RayTracing.Off)] + [InlineData("no", "123", RayTracing.Off)] + [InlineData("yes", "250", 0)] + [InlineData("yes", "500", 1)] + [InlineData("yes", "1000", 2)] + [InlineData("yes", "2000", 3)] + public void Ray_tracing_is_off_or_at_the_level_of_its_reach(string enabled, string range, int level) => + Assert.Equal(level, RayTracing.LevelOf(enabled, range)); + [Fact] - public void Ultra_is_the_top_of_what_the_old_starter_offered() + public void Ray_tracing_at_a_reach_no_level_has_is_at_none() { - var ultra = GraphicsPreset.Ultra; - - Assert.Equal(8192, ultra.TextureSize); - Assert.Equal(GraphicsRules.MaxCabTextureSize, ultra.CabTextureSize); - Assert.Equal(8192, ultra.ShadowMap); - Assert.Equal(GraphicsRules.MaxShadowRange, ultra.ShadowRange); - Assert.Equal(Steps.Anisotropy.Max(), ultra.Anisotropy); - Assert.Equal(AntiAliasingMode.Msaa8, ultra.AntiAliasing); - Assert.Equal(SliderRanges.ReflectionUpdates.Max(), ultra.ReflectionsRate); - Assert.Equal(4, ultra.SmokeFidelity); - Assert.Equal(4, ultra.SplineFidelity); - Assert.Equal(SliderRanges.DrawRangeFactors.Max(), ultra.DrawRangeFactor); - Assert.Equal(100, ultra.CabShadowsRange); - - var extras = ultra.Extras!; - Assert.Equal(200, extras.PythonUpdateMs); - Assert.True(extras.MotionBlur && extras.ChromaticAberration && extras.Smoke); - - Assert.True(ultra.Top!.Screens); + Assert.Null(RayTracing.LevelOf("yes", "750")); } [Fact] - public void No_level_goes_past_what_the_old_starter_gave() + public void Ray_tracing_is_off_by_default_and_its_levels_reach_further_one_by_one() { - foreach (var preset in GraphicsPreset.All) - { - Assert.True(preset.ShadowRange <= GraphicsRules.MaxShadowRange, preset.NameKey); - Assert.True(preset.CabTextureSize <= GraphicsRules.MaxCabTextureSize, preset.NameKey); - } + Assert.Equal(RayTracing.Off, RayTracing.LevelOf(Defaults())); + var ranges = RayTracing.Levels.Select(level => level.Range).ToList(); + Assert.Equal(ranges.OrderBy(r => r).Distinct(), ranges); + var key = SimulatorKeys.Named(RayTracing.RangeKey); + Assert.All(ranges, range => Assert.InRange(range, key.Minimum, key.Maximum)); } [Fact] - public void No_level_sets_what_only_the_advanced_mode_shows() + public void Turned_off_ray_tracing_keeps_its_reach_for_the_next_time() { - var set = GraphicsPreset.Keys.Concat(GraphicsExtras.Keys).Concat(GraphicsTop.Keys).ToList(); + var settings = Defaults().Copy(); + RayTracing.Apply(settings, 3); + RayTracing.Apply(settings, RayTracing.Off); - // The rows of keys by name, the buffer and the resources are all the advanced mode's. - Assert.Empty(set.Intersect(SimulatorKeys.All.Select(key => key.Key))); - Assert.Empty(set.Intersect(new[] { "starter.bufferscale", "gfx.framebuffer.width", "gfx.framebuffer.height", - "compresstex", "python.mipmaps", "gfx.skippipeline" })); + Assert.Equal("no", settings.Extra[RayTracing.Key]); + Assert.Equal("2000", settings.Extra[RayTracing.RangeKey]); } [Fact] - public void A_level_leaves_what_only_the_advanced_mode_shows_as_it_is() + public void No_level_of_detail_turns_ray_tracing_on_or_off() { foreach (var preset in GraphicsPreset.All) { var settings = Defaults().Copy(); - settings.BufferScalePercent = 80; - settings.CompressTextures = false; - settings.ScreenMipmaps = false; - settings.Extra["basedrawrange"] = "3000"; - settings.Extra["targetfps"] = "60"; - + RayTracing.Apply(settings, 2); preset.ApplyTo(settings); - - Assert.Equal(80, settings.BufferScalePercent); - Assert.False(settings.CompressTextures); - Assert.False(settings.ScreenMipmaps); - Assert.Equal("3000", settings.Extra["basedrawrange"]); - Assert.Equal("60", settings.Extra["targetfps"]); + Assert.Equal(2, RayTracing.LevelOf(settings)); Assert.Same(preset, GraphicsPreset.Matching(settings)); } } - [Fact] - public void At_ultra_the_shadows_are_longest() - { - // The bottom of that scale. - Assert.Equal(0.2, GraphicsPreset.Ultra.Top!.ShadowAngleLimit); - } - - [Fact] - public void Ultra_undoes_what_was_moved_earlier() - { - var settings = Defaults().Copy(); - settings.ShadowAngleLimit = 0.8; - settings.IgnoreIrrelevantTrains = true; - settings.PythonScreens = false; - - GraphicsPreset.Ultra.ApplyTo(settings); - - Assert.Same(GraphicsPreset.Ultra, GraphicsPreset.Matching(settings)); - Assert.Equal(0.2, settings.ShadowAngleLimit); - Assert.True(settings.PythonScreens); - Assert.True(settings.IgnoreIrrelevantTrains); - } - - [Fact] - public void Ultra_leaves_the_screens_in_their_own_windows_to_the_player() - { - var settings = Defaults().Copy(); - settings.Extra["python.displaywindows"] = "yes"; - - GraphicsPreset.Ultra.ApplyTo(settings); - - Assert.Equal("yes", settings.Extra["python.displaywindows"]); - Assert.Same(GraphicsPreset.Ultra, GraphicsPreset.Matching(settings)); - } - - [Fact] - public void The_other_levels_leave_what_only_ultra_sets() - { - foreach (var preset in GraphicsPreset.All.Where(preset => preset != GraphicsPreset.Ultra)) - { - var settings = Defaults().Copy(); - settings.IgnoreIrrelevantTrains = true; - settings.ShadowAngleLimit = 0.8; - - preset.ApplyTo(settings); - - Assert.True(settings.IgnoreIrrelevantTrains, preset.NameKey); - Assert.Equal(0.8, settings.ShadowAngleLimit); - Assert.Same(preset, GraphicsPreset.Matching(settings)); - } - } - - [Fact] - public void Ultra_is_not_recognised_with_one_of_its_values_moved() - { - var settings = Defaults().Copy(); - GraphicsPreset.Ultra.ApplyTo(settings); - Assert.Same(GraphicsPreset.Ultra, GraphicsPreset.Matching(settings)); - - settings.ReflectionsFramerate = 25; - - Assert.Null(GraphicsPreset.Matching(settings)); - } - - [Fact] - public void Shadows_reach_further_with_every_level_that_has_them() - { - static double Reach(GraphicsPreset preset) => - SimulatorSettings.ShadowReach(preset.ShadowRange, preset.ShadowMap, legacyRenderer: false); - - var reaches = GraphicsPreset.All.Where(preset => preset.Shadows).Select(Reach).ToList(); - - Assert.Equal(reaches.Distinct().OrderBy(reach => reach), reaches); - Assert.Equal(250, Reach(GraphicsPreset.High)); - Assert.Equal(800, Reach(GraphicsPreset.VeryHigh)); - } - - [Fact] - public void Without_extras_the_settings_are_what_the_simulator_goes_by() - { - Assert.Equal(GraphicsExtras.None, GraphicsExtras.Of(Defaults())); - Assert.Equal(GraphicsTop.None, GraphicsTop.Of(Defaults())); - } - - [Fact] - public void A_level_leaves_what_only_the_lowest_one_sets() - { - var settings = Defaults().Copy(); - settings.MotionBlur = false; - - GraphicsPreset.Low.ApplyTo(settings); - - Assert.False(settings.MotionBlur); - Assert.Same(GraphicsPreset.Low, GraphicsPreset.Matching(settings)); - } - - [Fact] - public void Every_level_is_within_what_the_sliders_offer() - { - foreach (var preset in GraphicsPreset.All) - { - Assert.True(preset.TextureSize >= SliderRanges.MinTextureSize, preset.NameKey); - Assert.True(preset.ShadowMap >= SliderRanges.MinShadowMap, preset.NameKey); - Assert.True(preset.DynamicLights >= SliderRanges.MinDynamicLights, preset.NameKey); - Assert.Contains(preset.ReflectionsRate, SliderRanges.ReflectionUpdates); - } - } - - [Fact] - public void The_lowest_level_is_not_recognised_with_one_of_its_extras_undone() - { - var settings = Defaults().Copy(); - GraphicsPreset.Minimal.ApplyTo(settings); - - settings.Smoke = true; - - Assert.Null(GraphicsPreset.Matching(settings)); - } - - [Fact] - public void The_extras_sit_on_the_steps_the_screen_offers() - { - foreach (var extras in new[] { GraphicsPreset.Minimal.Extras!, GraphicsPreset.Ultra.Extras! }) - Assert.Contains(extras.PythonUpdateMs, new[] { 200, 400, 800, 1600 }); - } - - // ── Leaving an end of the scale ────────────────────────────────────────── - - [Fact] - public void A_level_between_the_ends_chooses_only_its_own_keys() - { - var settings = Defaults().Copy(); - settings.MotionBlur = false; - settings.ShadowAngleLimit = 0.8; - - var applied = new GraphicsPresetMemory().Apply(GraphicsPreset.Medium, settings); - - Assert.Same(GraphicsPreset.Medium, GraphicsPreset.Matching(settings)); - Assert.False(settings.MotionBlur); - Assert.Equal(0.8, settings.ShadowAngleLimit); - Assert.Equal(GraphicsPreset.Keys, applied.ChosenKeys); - } - - [Fact] - public void Leaving_the_lowest_level_brings_back_what_was_there_before_it() - { - var memory = new GraphicsPresetMemory(); - var settings = Defaults().Copy(); - settings.MotionBlur = false; - var before = GraphicsExtras.Of(settings); - - var entering = memory.Apply(GraphicsPreset.Minimal, settings); - Assert.Equal(GraphicsPreset.Minimal.Extras, GraphicsExtras.Of(settings)); - Assert.Equal(GraphicsPreset.Keys.Concat(GraphicsExtras.Keys), entering.ChosenKeys); - - var leaving = memory.Apply(GraphicsPreset.Medium, settings); - - Assert.Equal(before, GraphicsExtras.Of(settings)); - Assert.Same(GraphicsPreset.Medium, GraphicsPreset.Matching(settings)); - Assert.Equal(GraphicsPreset.Keys.Concat(GraphicsExtras.Keys), leaving.ChosenKeys); - } - - [Fact] - public void Leaving_ultra_brings_back_what_was_there_before_it() - { - var memory = new GraphicsPresetMemory(); - var settings = Defaults().Copy(); - settings.MotionBlur = false; - settings.ShadowAngleLimit = 0.8; - var extras = GraphicsExtras.Of(settings); - var top = GraphicsTop.Of(settings); - - memory.Apply(GraphicsPreset.Ultra, settings); - var applied = memory.Apply(GraphicsPreset.VeryHigh, settings); - - Assert.Equal(extras, GraphicsExtras.Of(settings)); - Assert.Equal(top, GraphicsTop.Of(settings)); - Assert.Same(GraphicsPreset.VeryHigh, GraphicsPreset.Matching(settings)); - Assert.Equal(GraphicsPreset.Keys.Concat(GraphicsExtras.Keys).Concat(GraphicsTop.Keys), applied.ChosenKeys); - } - - [Fact] - public void Leaving_an_end_chosen_before_this_run_gives_the_simulators_own_values() - { - var settings = Defaults().Copy(); - GraphicsPreset.Ultra.ApplyTo(settings); - - new GraphicsPresetMemory().Apply(GraphicsPreset.Low, settings); - - Assert.Equal(GraphicsExtras.None, GraphicsExtras.Of(settings)); - Assert.Equal(GraphicsTop.None, GraphicsTop.Of(settings)); - Assert.Same(GraphicsPreset.Low, GraphicsPreset.Matching(settings)); - } - - [Fact] - public void One_end_to_the_other_keeps_what_was_there_before_either() - { - var memory = new GraphicsPresetMemory(); - var settings = Defaults().Copy(); - settings.MotionBlur = false; - settings.ShadowAngleLimit = 0.8; - var extras = GraphicsExtras.Of(settings); - var top = GraphicsTop.Of(settings); - - memory.Apply(GraphicsPreset.Minimal, settings); - memory.Apply(GraphicsPreset.Ultra, settings); - Assert.Same(GraphicsPreset.Ultra, GraphicsPreset.Matching(settings)); - - memory.Apply(GraphicsPreset.Minimal, settings); - Assert.Same(GraphicsPreset.Minimal, GraphicsPreset.Matching(settings)); - // Ultra's own values go, Minimal has no say over them. - Assert.Equal(top, GraphicsTop.Of(settings)); - - memory.Apply(GraphicsPreset.Medium, settings); - Assert.Equal(extras, GraphicsExtras.Of(settings)); - } - - [Fact] - public void What_was_remembered_comes_back_only_once() - { - var memory = new GraphicsPresetMemory(); - var settings = Defaults().Copy(); - settings.MotionBlur = false; - - memory.Apply(GraphicsPreset.Minimal, settings); - memory.Apply(GraphicsPreset.Medium, settings); - Assert.False(settings.MotionBlur); - - // Moved since, and no end left behind this time: it stays as moved. - settings.MotionBlur = true; - var applied = memory.Apply(GraphicsPreset.High, settings); - - Assert.True(settings.MotionBlur); - Assert.Equal(GraphicsPreset.Keys, applied.ChosenKeys); - } - - [Fact] - public void A_level_applied_twice_at_an_end_remembers_what_came_before_the_first() - { - var memory = new GraphicsPresetMemory(); - var settings = Defaults().Copy(); - settings.MotionBlur = false; - var before = GraphicsExtras.Of(settings); - - memory.Apply(GraphicsPreset.Minimal, settings); - memory.Apply(GraphicsPreset.Minimal, settings); - memory.Apply(GraphicsPreset.Low, settings); - - Assert.Equal(before, GraphicsExtras.Of(settings)); - } - - [Fact] - public void A_level_applied_is_recognised_until_one_value_moves() - { - foreach (var preset in GraphicsPreset.All) - { - var settings = Defaults().Copy(); - preset.ApplyTo(settings); - Assert.Same(preset, GraphicsPreset.Matching(settings)); - - settings.DynamicLights = 6; - Assert.Null(GraphicsPreset.Matching(settings)); - } - } - - [Fact] - public void Every_level_sits_on_the_steps_the_screen_offers() - { - foreach (var preset in GraphicsPreset.All) - { - Assert.True(Steps.IndexOf(Steps.Anisotropy, preset.Anisotropy) >= 0, preset.NameKey); - Assert.True(Steps.IndexOf(SliderRanges.ReflectionUpdates, preset.ReflectionsRate) >= 0, preset.NameKey); - Assert.True(Steps.IndexOf(SliderRanges.DrawRangeFactors, preset.DrawRangeFactor) >= 0, preset.NameKey); - foreach (int size in new[] { preset.TextureSize, preset.CabTextureSize, preset.ShadowMap }) - Assert.True(size is >= 512 and <= 8192 && (size & (size - 1)) == 0, preset.NameKey); - Assert.True(preset.ShadowRange >= SimulatorSettings.ShadowRangeFloor(preset.ShadowMap), preset.NameKey); - Assert.True(Steps.IndexOf(SliderRanges.CabShadowRanges, preset.CabShadowsRange) >= 0, preset.NameKey); - } - } - // ── A level for one part ───────────────────────────────────────────────── private static GraphicsGroupLevels Groups() => new(new TestInstallation().SettingsStore); - [Fact] - public void Every_key_a_level_sets_but_those_of_the_whole_belongs_to_one_part() - { - var inGroups = GraphicsGroup.All.SelectMany(group => group.Keys).ToList(); - - Assert.Equal(inGroups.Distinct(), inGroups); - Assert.Empty(inGroups.Except(GraphicsPreset.Keys).Except(GraphicsExtras.Keys).Except(GraphicsTop.Keys)); - Assert.Equal(new[] { "dynamiclights", "gfx.extraeffects", "gfx.smoke.fidelity", "multisampling" }, - GraphicsPreset.Keys.Except(inGroups).OrderBy(key => key, StringComparer.Ordinal)); - } - [Fact] public void One_part_moves_alone() { @@ -533,9 +329,10 @@ public class GraphicsPresetTests Assert.Equal(GraphicsPreset.Low.ShadowMap, moved.ShadowMapResolution); Assert.Equal(GraphicsPreset.Low.ShadowRange, moved.ShadowProjectionRange); Assert.Equal(GraphicsPreset.Low.ShadowRank, moved.ShadowRankCutoff); + Assert.Equal("low", moved.Extra["gfx.lights.shadows"]); Assert.Equal(settings.MaxTextureSize, moved.MaxTextureSize); - Assert.Equal(settings.DynamicLights, moved.DynamicLights); - Assert.Equal(settings.BufferScalePercent, moved.BufferScalePercent); + Assert.Equal(settings.Extra["gfx.ssr"], moved.Extra["gfx.ssr"]); + Assert.Equal(settings.AntiAliasing, moved.AntiAliasing); var levels = groups.LevelsOf(moved); Assert.Same(GraphicsPreset.Low, levels[GraphicsGroup.Shadows]); @@ -544,12 +341,10 @@ public class GraphicsPresetTests } [Fact] - public void Every_part_shows_ultra_when_the_whole_is_at_ultra() + public void Every_part_shows_the_level_the_whole_is_at() { - var settings = Defaults().Copy(); - GraphicsPreset.Ultra.ApplyTo(settings); - - Assert.All(Groups().LevelsOf(settings).Values, level => Assert.Same(GraphicsPreset.Ultra, level)); + foreach (var preset in GraphicsPreset.All) + Assert.All(Groups().LevelsOf(At(preset)).Values, level => Assert.Same(preset, level)); } [Fact] @@ -565,17 +360,15 @@ public class GraphicsPresetTests } [Fact] - public void Every_part_at_a_level_and_the_rest_with_it_is_that_level() + public void Every_part_at_a_level_and_the_anti_aliasing_with_it_is_that_level() { var groups = Groups(); - foreach (var level in GraphicsPreset.All.Where(level => level.Extras is null)) + foreach (var level in GraphicsPreset.All) { var settings = Defaults().Copy(); foreach (var group in GraphicsGroup.All) settings = groups.Apply(group, level, settings); settings.AntiAliasing = level.AntiAliasing; - settings.DynamicLights = level.DynamicLights; - settings.SmokeFidelity = level.SmokeFidelity; Assert.Same(level, GraphicsPreset.Matching(settings)); } @@ -589,34 +382,4 @@ public class GraphicsPresetTests foreach (var level in GraphicsPreset.All) Assert.Same(level, groups.LevelsOf(groups.Apply(group, level, Defaults()))[group]); } - - [Fact] - public void Leaving_an_end_in_a_part_gives_the_simulators_own_for_what_only_the_end_sets() - { - var groups = Groups(); - var settings = AtHigh(); - settings.ShadowAngleLimit = 0.8; - - var ultra = groups.Apply(GraphicsGroup.Shadows, GraphicsPreset.Ultra, settings); - Assert.Equal(0.2, ultra.ShadowAngleLimit); - Assert.Same(GraphicsPreset.Ultra, groups.LevelsOf(ultra)[GraphicsGroup.Shadows]); - - var back = groups.Apply(GraphicsGroup.Shadows, GraphicsPreset.High, ultra); - Assert.Equal(GraphicsTop.None.ShadowAngleLimit, back.ShadowAngleLimit); - Assert.Same(GraphicsPreset.High, groups.LevelsOf(back)[GraphicsGroup.Shadows]); - } - - [Fact] - public void A_level_in_a_part_between_the_ends_leaves_what_only_an_end_sets() - { - var settings = AtHigh(); - settings.MotionBlur = false; - - settings.Smoke = false; - - var shadows = Groups().Apply(GraphicsGroup.Shadows, GraphicsPreset.VeryHigh, settings); - Assert.False(shadows.Smoke); - Assert.False(shadows.MotionBlur); - Assert.Equal(GraphicsPreset.VeryHigh.ShadowMap, shadows.ShadowMapResolution); - } } diff --git a/StarterNG.Tests/GraphicsSettingsTests.cs b/StarterNG.Tests/GraphicsSettingsTests.cs index 1ac97dd..985207b 100644 --- a/StarterNG.Tests/GraphicsSettingsTests.cs +++ b/StarterNG.Tests/GraphicsSettingsTests.cs @@ -37,88 +37,95 @@ public class GraphicsSettingsTests private static string? Line(string text, string key) => Lines(text).FirstOrDefault(l => l == key || l.StartsWith(key + " ", StringComparison.Ordinal)); - // As application.cpp on master has them; a word the starter does not know is the file's own, written back as it is. + // 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("default", "default")] - [InlineData("full", "full")] - [InlineData("gl46", "gl46")] [InlineData("vulkan", "vulkan")] - [InlineData("vulkan14", "vulkan14")] - [InlineData("legacy", "legacy")] - [InlineData("simpleshader", "legacy")] - [InlineData("off", "legacy")] - [InlineData("simple", "simple")] - [InlineData("experimental", "experimental")] - public void A_renderer_is_written_by_the_name_master_knows(string read, string written) + [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_the_full_one_is_chosen() + public void Without_a_renderer_in_the_file_vulkan_is_chosen_and_written() { - Assert.Equal(0, Read("").RenderEngine); - Assert.Equal("gfxrenderer default", Line(Write(Read("")), "gfxrenderer")); + // 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 An_older_renderer_stays_chosen_and_can_be_chosen_again() + public void Opengl_stays_chosen_and_vulkan_can_be_chosen_again() { - var installation = WithIni("gfxrenderer legacy\n"); + 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 legacy", installation.Files.ReadAllText(UserIni)); + Assert.Contains("gfxrenderer default", installation.Files.ReadAllText(UserIni)); - store.Settings.RenderEngine = SimulatorSettings.RenderEngineOf("simple"); + store.Settings.Backend = GraphicsBackend.Vulkan; store.Save(); - Assert.Contains("gfxrenderer simple", installation.Files.ReadAllText(UserIni)); + Assert.Equal("gfxrenderer vulkan", Line(installation.Files.ReadAllText(UserIni), "gfxrenderer")); } [Fact] - public void Keys_only_the_legacy_renderer_or_windows_read_stay_in_the_file() + 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" + "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.Settings.FieldOfView = 50; installation.SettingsStore.Save(); string saved = installation.Files.ReadAllText(UserIni); - Assert.All(old, line => Assert.Contains(line, saved)); + Assert.All(old, line => Assert.Null(Line(saved, line.Split(' ')[0]))); } [Fact] - public void Keys_of_the_vulkan_renderer_master_does_not_have_stay_on_a_save() + 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.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\n" + string.Join("\n", vulkan) + "\n"); + 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); - // Written in by hand for a build that has that renderer: the starter does not know them, and leaves them. 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 Levels_leave_the_switches_only_an_older_simulator_reads() + public void The_faster_choices_the_simulator_does_not_make_itself_are_written() { - var s = new SimulatorSettings { UseVbo = false, UseGLES = true, RenderAngleVulkan = true }; + var installation = WithIni("width 1280\n"); - GraphicsPreset.Ultra.ApplyTo(s); + installation.SettingsStore.Save(); - Assert.Equal((false, true, true), (s.UseVbo, s.UseGLES, s.RenderAngleVulkan)); + 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] @@ -127,20 +134,40 @@ public class GraphicsSettingsTests [InlineData("multisampling 2\n", AntiAliasingMode.Msaa4)] [InlineData("multisampling 3\n", AntiAliasingMode.Msaa8)] [InlineData("", AntiAliasingMode.Msaa4)] - public void Multisampling_is_read_as_master_reads_it(string text, AntiAliasingMode expected) + [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); } - [Fact] - public void Multisampling_is_written_to_its_own_key_only() + [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) { - var s = Read("multisampling 1\n"); + string text = Write(new SimulatorSettings { AntiAliasing = mode }); - string text = Write(s); + Assert.Equal($"multisampling {multisampling}", Line(text, "multisampling")); + Assert.Equal($"gfx.antialiasing {word}", Line(text, "gfx.antialiasing")); + } - Assert.Equal("multisampling 1", Line(text, "multisampling")); - Assert.Null(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] @@ -154,113 +181,37 @@ public class GraphicsSettingsTests } [Fact] - public void The_render_scale_is_the_starters_own_key_or_the_framebuffer_against_the_window() + 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_framebuffer_master_reads() + 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.framebuffer.width 960", Line(text, "gfx.framebuffer.width")); - Assert.Equal("gfx.framebuffer.height 540", Line(text, "gfx.framebuffer.height")); - Assert.Null(Line(text, "gfx.buffer.percent")); - } - - // Borderless, the simulator takes the window's size from the monitor (application.cpp) and - // leaves width and height unread; -1 is its own word for a buffer of the window's size - // (opengl33renderer.cpp). - [Fact] - public void A_borderless_screen_draws_into_a_buffer_of_the_windows_own_size() - { - var s = Read("fullscreenwindowed yes\nwidth 1280\nheight 720\n"); - - string text = Write(s); - - Assert.Equal("gfx.framebuffer.width -1", Line(text, "gfx.framebuffer.width")); - Assert.Equal("gfx.framebuffer.height -1", Line(text, "gfx.framebuffer.height")); - Assert.Equal("width 1280", Line(text, "width")); - Assert.Equal(100, Read(text).BufferScalePercent); - } - - [Fact] - public void A_borderless_screen_with_a_smaller_buffer_takes_its_part_of_the_monitor() - { - var s = Read("fullscreenwindowed yes\nfullscreenmonitor HDMI-1:1920,0\nwidth 1280\nheight 720\nstarter.bufferscale 50\n"); - var asked = new List(); - var serializer = new SettingsSerializer(id => - { - asked.Add(id); - return (2560, 1440); - }); - var c = new ConfigFile(); - - serializer.Write(s, c); - - Assert.Equal("1280", c.GetRaw("gfx.framebuffer.width")); - Assert.Equal("720", c.GetRaw("gfx.framebuffer.height")); - Assert.Equal("50", c.GetRaw("starter.bufferscale")); - Assert.Equal(new[] { "HDMI-1:1920,0" }, asked.Distinct()); - var read = new SimulatorSettings(); - serializer.Read(read, c); - Assert.Equal(50, read.BufferScalePercent); - } - - [Fact] - public void A_borderless_screen_on_a_monitor_of_unknown_size_keeps_the_windows_own_buffer() - { - var s = Read("fullscreenwindowed yes\nwidth 1280\nheight 720\nstarter.bufferscale 50\n"); - var c = new ConfigFile(); - - new SettingsSerializer(_ => null).Write(s, c); - - Assert.Equal("-1", c.GetRaw("gfx.framebuffer.width")); - Assert.Equal("-1", c.GetRaw("gfx.framebuffer.height")); - } - - [Fact] - public void A_buffer_of_the_window_left_in_a_borderless_file_goes_on_a_save() - { - // As this starter wrote it before: the size of the resolution row, which borderless does not use. - var installation = WithIni("fullscreenwindowed yes\nwidth 1280\nheight 720\nstarter.bufferscale 100\n" + - "gfx.framebuffer.width 1280\ngfx.framebuffer.height 720\n"); - - installation.SettingsStore.Save(); - - string saved = installation.Files.ReadAllText(UserIni); - Assert.Equal("gfx.framebuffer.width -1", Line(saved, "gfx.framebuffer.width")); - Assert.Equal("gfx.framebuffer.height -1", Line(saved, "gfx.framebuffer.height")); - } - - [Fact] - public void A_buffer_the_file_has_for_a_window_or_a_full_screen_stays_until_changed() - { - var installation = WithIni("fullscreenwindowed no\nwidth 1280\nheight 720\nstarter.bufferscale 100\n" + - "gfx.framebuffer.width 1280\ngfx.framebuffer.height 720\n"); - - installation.SettingsStore.Save(); - - string saved = installation.Files.ReadAllText(UserIni); - Assert.Equal("gfx.framebuffer.width 1280", Line(saved, "gfx.framebuffer.width")); - Assert.Equal("gfx.framebuffer.height 720", Line(saved, "gfx.framebuffer.height")); + 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() { - for (int engine = 0; engine < SimulatorSettings.RenderEngines.Length; engine++) + foreach (var backend in new[] { GraphicsBackend.Vulkan, GraphicsBackend.OpenGL }) { foreach (var level in GraphicsPreset.All) { var installation = WithIni(""); var store = installation.SettingsStore; - store.Settings.RenderEngine = engine; + store.Settings.Backend = backend; level.ApplyTo(store.Settings); Assert.Same(level, GraphicsPreset.Matching(store.Settings)); @@ -270,6 +221,7 @@ public class GraphicsSettingsTests again.Environment.With("HOME", "/home/kolejarz"); again.SettingsStore.Load(); Assert.Same(level, GraphicsPreset.Matching(again.SettingsStore.Settings)); + Assert.Equal(backend, again.SettingsStore.Settings.Backend); } } } @@ -279,7 +231,7 @@ public class GraphicsSettingsTests { var s = new SimulatorSettings(); GraphicsPreset.High.ApplyTo(s); - s.DynamicLights = 2; + s.ShadowRankCutoff = 1; Assert.Null(GraphicsPreset.Matching(s)); } @@ -318,7 +270,7 @@ public class GraphicsSettingsTests Assert.True(installation.SettingsStore.ImportFrom("/backup/eu07.ini")); var s = installation.SettingsStore.Settings; Assert.Equal(60, s.FieldOfView); - Assert.Equal(SimulatorSettings.RenderEngineOf("legacy"), s.RenderEngine); + 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. diff --git a/StarterNG.Tests/LocalizationTests.cs b/StarterNG.Tests/LocalizationTests.cs index cdb5b90..46cf1cf 100644 --- a/StarterNG.Tests/LocalizationTests.cs +++ b/StarterNG.Tests/LocalizationTests.cs @@ -31,9 +31,13 @@ public class LocalizationTests 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("Cmd.", StringComparison.Ordinal) && + !key.StartsWith("Ini.", StringComparison.Ordinal) && + !key.StartsWith("Choice.", StringComparison.Ordinal)) .Where(key => !other.ContainsKey(key)); Assert.Empty(missing); diff --git a/StarterNG.Tests/PhysicsPresetTests.cs b/StarterNG.Tests/PhysicsPresetTests.cs new file mode 100644 index 0000000..9d81787 --- /dev/null +++ b/StarterNG.Tests/PhysicsPresetTests.cs @@ -0,0 +1,105 @@ +using StarterNG.Domain.Settings; +using StarterNG.Tests.Fakes; + +namespace StarterNG.Tests; + +public class PhysicsPresetTests +{ + private static SimulatorSettings Defaults() + { + var installation = new TestInstallation(); + installation.SettingsStore.Load(); + return installation.SettingsStore.Settings.Copy(); + } + + [Fact] + public void The_simulators_own_physics_are_neither_arcade_nor_realistic() + { + Assert.Null(PhysicsPreset.Matching(Defaults())); + } + + [Fact] + public void A_level_of_realism_applied_is_recognised_until_one_setting_moves() + { + foreach (var preset in PhysicsPreset.All) + { + var settings = Defaults(); + preset.ApplyTo(settings); + Assert.Same(preset, PhysicsPreset.Matching(settings)); + + settings.CrashDamage = !settings.CrashDamage; + Assert.Null(PhysicsPreset.Matching(settings)); + } + } + + [Fact] + public void Arcade_forgives_and_realistic_takes_the_physical_models() + { + var arcade = Defaults(); + PhysicsPreset.Arcade.ApplyTo(arcade); + Assert.False(arcade.CrashDamage); + Assert.False(arcade.LiveTraction); + Assert.Equal(ConsistReadyOverride.AlwaysReady, arcade.ReadyOverride); + Assert.Equal("off", arcade.Extra["physics.derailment"]); + + var realistic = Defaults(); + PhysicsPreset.Realistic.ApplyTo(realistic); + Assert.True(realistic.CrashDamage); + Assert.Equal(ConsistReadyOverride.Scenery, realistic.ReadyOverride); + Assert.Equal("yes", realistic.Extra["couplerphysics"]); + Assert.Equal("new", realistic.Extra["physics.derailment"]); + Assert.Equal("new", realistic.Extra["physics.collisions"]); + Assert.Equal("auto", realistic.Extra["physics.trackbuckling"]); + } + + [Fact] + public void Every_value_of_a_level_is_one_its_row_offers() + { + foreach (var preset in PhysicsPreset.All) + foreach (var (name, value) in preset.Keys) + Assert.True(SimulatorKeys.Named(name).ChoiceIndex(value) >= 0, $"{preset.NameKey} {name} {value}"); + Assert.All(PhysicsPreset.All.SelectMany(preset => preset.Keys.Keys), key => Assert.Contains(key, PhysicsPreset.AllKeys)); + } + + [Fact] + public void No_level_of_realism_touches_the_picture_nor_a_level_of_detail_the_realism() + { + var settings = Defaults(); + GraphicsPreset.Low.ApplyTo(settings); + PhysicsPreset.Realistic.ApplyTo(settings); + Assert.Same(GraphicsPreset.Low, GraphicsPreset.Matching(settings)); + + GraphicsPreset.VeryHigh.ApplyTo(settings); + Assert.Same(PhysicsPreset.Realistic, PhysicsPreset.Matching(settings)); + } + + [Theory] + [InlineData("couplerphysics", "true", "yes")] + [InlineData("couplerphysics", "1", "yes")] + [InlineData("physics.derailment", "no", "off")] + [InlineData("physics.trackbuckling", "no", "off")] + [InlineData("couplerphysics", "no", "no")] + public void A_word_the_simulator_takes_for_another_stands_for_it(string name, string raw, string word) + { + var key = SimulatorKeys.Named(name); + Assert.Equal(key.ChoiceIndex(word), key.ChoiceIndex(raw)); + } + + [Fact] + public void A_level_of_realism_survives_a_save() + { + var installation = new TestInstallation(); + var store = installation.SettingsStore; + store.Load(); + PhysicsPreset.Realistic.ApplyTo(store.Settings); + + store.Save(); + + var again = new TestInstallation(installation.Files).SettingsStore; + again.Load(); + Assert.Same(PhysicsPreset.Realistic, PhysicsPreset.Matching(again.Settings)); + string saved = installation.Files.ReadAllText(store.SavePath); + Assert.Contains("physics.derailment new", saved); + Assert.Contains("couplerphysics yes", saved); + } +} diff --git a/StarterNG.Tests/RenderersTests.cs b/StarterNG.Tests/RenderersTests.cs deleted file mode 100644 index 589a1f4..0000000 --- a/StarterNG.Tests/RenderersTests.cs +++ /dev/null @@ -1,17 +0,0 @@ -using StarterNG.Domain.Settings; - -namespace StarterNG.Tests; - -public class RenderersTests -{ - [Theory] - [InlineData("default", RendererKind.Modern)] - [InlineData("full", RendererKind.Modern)] - [InlineData("vulkan", RendererKind.Modern)] - [InlineData("legacy", RendererKind.Legacy)] - [InlineData("simple", RendererKind.Legacy)] - [InlineData("simpleshader", RendererKind.Legacy)] - [InlineData("OFF", RendererKind.Legacy)] - public void A_renderer_word_draws_with_the_renderer_master_gives_it(string word, RendererKind kind) => - Assert.Equal(kind, Renderers.KindOf(word)); -} diff --git a/StarterNG.Tests/SettingsStoreTests.cs b/StarterNG.Tests/SettingsStoreTests.cs index 206fa5d..25c5bbf 100644 --- a/StarterNG.Tests/SettingsStoreTests.cs +++ b/StarterNG.Tests/SettingsStoreTests.cs @@ -57,6 +57,34 @@ public class SettingsStoreTests Assert.Equal(UserIni, installation.SettingsStore.LoadedFrom); } + [Fact] + public void A_key_set_twice_in_the_players_ini_is_left_once_on_the_first_start() + { + var installation = WithHome(new InMemoryFileSystem() + .WithFile(UserIni, "width 800\nheight 1080\nnetwork.server zmq a\nnetwork.server zmq b\nwidth 1920\n")); + installation.SettingsStore.Load(); + + installation.SettingsStore.RemoveDuplicatesNow(); + + string saved = SavedLines(installation); + Assert.DoesNotContain("width 800", saved); + Assert.Contains("width 1920\n", saved); + Assert.Contains("network.server zmq a\n", saved); + Assert.Contains("network.server zmq b\n", saved); + Assert.Equal(1920, installation.SettingsStore.Settings.Width); + } + + [Fact] + public void An_ini_setting_each_key_once_is_not_written_on_the_first_start() + { + var installation = WithHome(new InMemoryFileSystem().WithFile(UserIni, "width 1920\n")); + installation.SettingsStore.Load(); + + installation.SettingsStore.RemoveDuplicatesNow(); + + Assert.Equal("width 1920\n", installation.Files.ReadAllText(UserIni)); + } + [Fact] public void A_language_the_starter_does_not_ship_survives_a_round_trip() { @@ -923,13 +951,12 @@ public class SettingsStoreTests Assert.Equal(floor, SimulatorSettings.ShadowRangeFloor(mapSize)); [Theory] - [InlineData(150, 1024, false, 150)] - [InlineData(150, 4096, false, 300)] - [InlineData(150, 4096, true, 150)] - [InlineData(100, 8192, false, 1200)] - [InlineData(400, 8192, false, 1600)] - public void Shadows_reach_as_far_as_the_renderer_stretches_them(int range, int mapSize, bool legacy, int reach) => - Assert.Equal(reach, SimulatorSettings.ShadowReach(range, mapSize, legacy)); + [InlineData(150, 1024, 150)] + [InlineData(150, 4096, 300)] + [InlineData(100, 8192, 1200)] + [InlineData(400, 8192, 1600)] + public void Shadows_reach_as_far_as_the_renderer_stretches_them(int range, int mapSize, int reach) => + Assert.Equal(reach, SimulatorSettings.ShadowReach(range, mapSize)); [Theory] [InlineData("anisotropicfiltering 12")] @@ -947,10 +974,15 @@ public class SettingsStoreTests [Theory] [InlineData("default")] + [InlineData("full")] + [InlineData("opengl")] + [InlineData("gl46")] + [InlineData("vulkan14")] [InlineData("simpleshader")] [InlineData("off")] [InlineData("legacy")] [InlineData("simple")] + [InlineData("experimental")] public void A_renderer_word_the_simulator_reads_as_another_stays_until_changed(string word) { var installation = WithHome(new InMemoryFileSystem().WithFile(UserIni, $"gfxrenderer {word}\n")); @@ -961,16 +993,11 @@ public class SettingsStoreTests Assert.Contains($"gfxrenderer {word}\n", SavedLines(installation)); } - // A word the starter does not know is written in by hand for a build that has it, such as one with Vulkan: - // shown, and left as it is until another renderer is chosen. + // A word the starter does not know is written in by hand: "null" of the test runs, or one of a newer simulator. + // Shown, and left as it is until another renderer is chosen. [Theory] - [InlineData("full")] - [InlineData("opengl")] - [InlineData("gl46")] - [InlineData("vulkan")] - [InlineData("vulkan14")] - [InlineData("experimental")] [InlineData("null")] + [InlineData("whatever")] public void A_renderer_word_the_starter_does_not_know_stays_until_another_is_chosen(string word) { var installation = WithHome(new InMemoryFileSystem().WithFile(UserIni, $"gfxrenderer {word}\nfieldofview 45\n")); @@ -983,60 +1010,61 @@ public class SettingsStoreTests store.Save(); Assert.Contains($"gfxrenderer {word}\n", SavedLines(installation)); - // Picking one from the list, even the full renderer it runs as, is a choice. + // Picking one from the list, even the renderer it runs as, is a choice. store.Settings.RendererWord = null; - store.Settings.RenderEngine = 0; + store.Settings.Backend = GraphicsBackend.Vulkan; store.Save(); - Assert.Contains("gfxrenderer default\n", SavedLines(installation)); + Assert.Contains("gfxrenderer vulkan\n", SavedLines(installation)); Assert.False(store.UnreadableValues().ContainsKey("gfxrenderer")); } [Theory] - [InlineData(0, "default")] - [InlineData(1, "legacy")] - [InlineData(2, "simple")] - public void A_chosen_renderer_is_saved_as_the_simulator_reads_it(int engine, string saved) + [InlineData(GraphicsBackend.Vulkan, "vulkan")] + [InlineData(GraphicsBackend.OpenGL, "default")] + public void A_chosen_renderer_is_saved_as_the_simulator_reads_it(GraphicsBackend backend, string saved) { - // application.cpp on master knows default, legacy and simple; "simpleshader" and "off" - // from older starters are legacy, and any other word gets the full renderer. + // utilities/Globals.cpp knows default (OpenGL 4.6) and vulkan; the words of the renderers it no longer has + // it reads as default. var installation = WithHome(new InMemoryFileSystem().WithFile(UserIni, "gfxrenderer simpleshader\n")); installation.SettingsStore.Load(); - installation.SettingsStore.Settings.RenderEngine = engine == 1 ? 0 : engine; + installation.SettingsStore.Settings.Backend = backend == GraphicsBackend.Vulkan ? GraphicsBackend.OpenGL : GraphicsBackend.Vulkan; installation.SettingsStore.Save(); - installation.SettingsStore.Settings.RenderEngine = engine; + installation.SettingsStore.Settings.Backend = backend; installation.SettingsStore.Save(); Assert.Contains($"gfxrenderer {saved}\n", SavedLines(installation)); } [Theory] - [InlineData("full", 0)] - [InlineData("gl46", 0)] - [InlineData("opengl", 0)] - [InlineData("vulkan", 0)] - [InlineData("VULKAN14", 0)] - [InlineData("whatever", 0)] - [InlineData("experimental", 0)] - [InlineData("simpleshader", 1)] - public void A_renderer_word_stands_where_the_simulator_reads_it(string word, int engine) + [InlineData("full", GraphicsBackend.OpenGL)] + [InlineData("gl46", GraphicsBackend.OpenGL)] + [InlineData("opengl", GraphicsBackend.OpenGL)] + [InlineData("default", GraphicsBackend.OpenGL)] + [InlineData("vulkan", GraphicsBackend.Vulkan)] + [InlineData("VULKAN14", GraphicsBackend.Vulkan)] + [InlineData("whatever", GraphicsBackend.OpenGL)] + [InlineData("experimental", GraphicsBackend.OpenGL)] + [InlineData("simpleshader", GraphicsBackend.OpenGL)] + public void A_renderer_word_stands_where_the_simulator_reads_it(string word, GraphicsBackend backend) { - Assert.Equal(engine, SimulatorSettings.RenderEngineOf(word)); + Assert.Equal(backend, SimulatorSettings.BackendOf(word)); } [Fact] - public void The_buffer_is_written_as_the_framebuffer_master_reads() + public void The_buffer_is_written_as_the_simulator_reads_it() { - var installation = WithHome(new InMemoryFileSystem().WithFile(UserIni, "width 1000\nheight 500\n")); + var installation = WithHome(new InMemoryFileSystem().WithFile(UserIni, + "width 1000\nheight 500\ngfx.framebuffer.width 1000\ngfx.framebuffer.height 500\n")); installation.SettingsStore.Load(); installation.SettingsStore.Settings.BufferScalePercent = 50; installation.SettingsStore.Save(); string saved = SavedLines(installation); - Assert.DoesNotContain("gfx.buffer.percent", saved); - Assert.Contains("gfx.framebuffer.width 500\n", saved); - Assert.Contains("gfx.framebuffer.height 250\n", saved); + Assert.Contains("gfx.buffer.percent 0.5\n", saved); + Assert.DoesNotContain("\ngfx.framebuffer.width", saved); + Assert.DoesNotContain("\ngfx.framebuffer.height", saved); } [Theory] @@ -1046,8 +1074,9 @@ public class SettingsStoreTests [InlineData("gfx.framebuffer.width 500\n", 100)] [InlineData("starter.bufferscale 60\n", 60)] [InlineData("starter.bufferscale 60\nwidth 1000\ngfx.framebuffer.width 800\n", 60)] - [InlineData("gfx.buffer.percent 0.5\n", 100)] - public void The_buffer_is_read_from_the_starters_key_or_the_framebuffer(string ini, int percent) + [InlineData("gfx.buffer.percent 0.5\n", 50)] + [InlineData("gfx.buffer.percent 0.6\nstarter.bufferscale 80\n", 60)] + public void The_buffer_is_read_from_the_simulators_key_or_what_an_older_starter_left(string ini, int percent) { var installation = WithHome(new InMemoryFileSystem().WithFile(UserIni, ini)); @@ -1257,26 +1286,23 @@ public class SettingsStoreTests Assert.Equal(60, store.Settings.FieldOfView); } - // The move to the standard mode puts back the keys of the rows it hides: the row of the - // render scale has the frame buffer's two, and the percent it is worked out from is the starter's own. + // The render scale put back is the whole window, whichever way the file had it. [Theory] - [InlineData("width 1920\nheight 1080\nstarter.bufferscale 50\ngfx.framebuffer.width 960\ngfx.framebuffer.height 540\n")] + [InlineData("width 1920\nheight 1080\ngfx.buffer.percent 0.5\n")] + [InlineData("width 1920\nheight 1080\nstarter.bufferscale 50\n")] [InlineData("width 1920\nheight 1080\ngfx.framebuffer.width 960\ngfx.framebuffer.height 540\n")] - public void The_frame_buffer_put_back_takes_the_render_scale_with_it(string ini) + public void The_render_scale_put_back_is_the_whole_window(string ini) { var installation = WithHome(new InMemoryFileSystem().WithFile(UserIni, ini)); var store = installation.SettingsStore; store.Load(); Assert.Equal(50, store.Settings.BufferScalePercent); - var reset = store.WithDefaults(store.Settings, new[] { "gfx.framebuffer.width", "gfx.framebuffer.height" }); + var reset = store.WithDefaults(store.Settings, new[] { "gfx.buffer.percent" }); Assert.Equal(100, reset.BufferScalePercent); Assert.Equal(1920, reset.Width); - var written = store.Render(reset); - Assert.Equal("100", written["starter.bufferscale"]); - Assert.Equal("1920", written["gfx.framebuffer.width"]); - Assert.Equal("1080", written["gfx.framebuffer.height"]); + Assert.Equal("1", store.Render(reset)["gfx.buffer.percent"]); } // ── What the screen shows otherwise ────────────────────────────────────── @@ -1582,7 +1608,6 @@ public class SettingsStoreTests [Theory] [InlineData("starter.gpu")] - [InlineData("gfx.usegles")] public void The_move_to_the_standard_mode_keeps_what_lets_a_machine_run_the_simulator(string key) => Assert.Contains(key, StarterNG.Application.SettingsStore.KeptOnMoveToStandard); diff --git a/StarterNG.Tests/SettingsUnknownValueTests.cs b/StarterNG.Tests/SettingsUnknownValueTests.cs index 760829e..74251f0 100644 --- a/StarterNG.Tests/SettingsUnknownValueTests.cs +++ b/StarterNG.Tests/SettingsUnknownValueTests.cs @@ -47,6 +47,10 @@ public class SettingsUnknownValueTests private static string? Shown(ComboBox list) => (list.SelectedItem as ComboBoxItem)?.Content as string; + private static SettingRow Row(StarterNG.Views.Settings page, string key) => + Avalonia.LogicalTree.LogicalExtensions.GetLogicalDescendants(page).OfType() + .First(row => row.ConfigKey == key); + private static string? Note(StarterNG.Views.Settings page, string key) => Avalonia.LogicalTree.LogicalExtensions.GetLogicalDescendants(page).OfType() .First(row => row.ConfigKey == key).UnknownValue; @@ -54,24 +58,97 @@ public class SettingsUnknownValueTests [Fact] public void A_renderer_word_is_on_the_list_chosen_and_saved_as_it_is() { - WithPage(Standard + "gfxrenderer vulkan\nfieldofview 45\n", (page, _, saved) => + WithPage(Standard + "gfxrenderer null\nfieldofview 45\n", (page, _, saved) => { - Assert.Equal("vulkan", Shown(List(page, "RenderEngineCb"))); - Assert.Contains("vulkan", Note(page, "gfxrenderer")); + Assert.Equal("null", Shown(List(page, "RenderEngineCb"))); + Assert.Contains("null", Note(page, "gfxrenderer")); Assert.False(page.HasUnsavedChanges); page.Save(); - Assert.Contains("gfxrenderer vulkan\n", saved()); + Assert.Contains("gfxrenderer null\n", saved()); + }); + } + + [Fact] + public void The_standard_mode_has_the_choice_of_the_simulator_and_of_ray_tracing_but_not_of_the_renderer() + { + WithPage(Standard, (page, _, _) => + { + Assert.True(Row(page, "starter.exe.path").IsEffectivelyVisible); + Assert.True(Row(page, "gfx.raytracing, gfx.raytracing.range").IsVisible); + Assert.False(Row(page, "gfxrenderer").IsVisible); + }); + } + + [Fact] + public void A_level_of_realism_picked_is_saved_and_marked_until_a_setting_moves() + { + WithPage(Standard, (page, services, saved) => + { + var buttons = page.FindControl("PhysicsPresetButtons")!.Children.OfType