diff --git a/StarterNG.Tests/ScenarioPresentationTests.cs b/StarterNG.Tests/ScenarioPresentationTests.cs index 8448790..75b82cb 100644 --- a/StarterNG.Tests/ScenarioPresentationTests.cs +++ b/StarterNG.Tests/ScenarioPresentationTests.cs @@ -209,3 +209,26 @@ public class TimetableLocatorTests Assert.Null(locator.Resolve(scenery, null)); } } + +public class AttachmentLocatorTests +{ + [Theory] + [InlineData(@"scenery\Braniewo\opis.PDF")] + [InlineData("SCENERY/braniewo/Opis.pdf")] + public void An_attachment_named_in_other_case_is_found_as_on_disk(string named) + { + var installation = new TestInstallation(new InMemoryFileSystem(caseSensitive: true) + .WithFile(TestInstallation.At("scenery", "Braniewo", "Opis.pdf"), "%PDF")); + + Assert.Equal(TestInstallation.At("scenery", "Braniewo", "Opis.pdf"), + new AttachmentLocator(installation.Files, installation.Paths) + .Resolve(new SceneryAttachment { FilePath = named })); + } + + [Theory] + [InlineData("scenery/brak.pdf")] + [InlineData("")] + public void An_attachment_that_is_not_there_is_not_found(string named) => + Assert.Null(new AttachmentLocator(new InMemoryFileSystem(caseSensitive: true), new TestInstallation().Paths) + .Resolve(new SceneryAttachment { FilePath = named })); +} diff --git a/StarterNG/Application/AppServices.cs b/StarterNG/Application/AppServices.cs index 7f7b6ce..0bf1693 100644 --- a/StarterNG/Application/AppServices.cs +++ b/StarterNG/Application/AppServices.cs @@ -56,6 +56,7 @@ public sealed class AppServices var missingAssets = new MissingAssetScanner(files, paths); SceneryTexts = new SceneryTranslations(files, log); Timetables = new TimetableLocator(files); + Attachments = new AttachmentLocator(files, paths); LoadWeights = new LoadWeightsRepository(files, paths, log); Library = new GameLibrary(Vehicles, Sceneries, MiniTextures, Physics, log); State = new AppState(); @@ -119,6 +120,8 @@ public sealed class AppServices public TimetableLocator Timetables { get; } + public AttachmentLocator Attachments { get; } + public LoadWeightsRepository LoadWeights { get; } public GameLibrary Library { get; } diff --git a/StarterNG/Infrastructure/Sceneries/AttachmentLocator.cs b/StarterNG/Infrastructure/Sceneries/AttachmentLocator.cs new file mode 100644 index 0000000..dab11ec --- /dev/null +++ b/StarterNG/Infrastructure/Sceneries/AttachmentLocator.cs @@ -0,0 +1,24 @@ +using StarterNG.Application.Abstractions; +using StarterNG.Domain.Sceneries; + +namespace StarterNG.Infrastructure.Sceneries; + +/// Where the file a scenery attaches (//$f) is on disk. +public sealed class AttachmentLocator +{ + private readonly IGamePaths _paths; + private readonly CaseInsensitivePaths _names; + + public AttachmentLocator(IFileSystem files, IGamePaths paths) + { + _paths = paths; + _names = new CaseInsensitivePaths(files); + } + + /// + /// The attachment's file, named from the game's folder or in full, with its letters + /// as on disk; null when it is not there. + /// + public string? Resolve(SceneryAttachment attachment) => + string.IsNullOrWhiteSpace(attachment.FilePath) ? null : _names.Find(_paths.Root, attachment.FilePath.Trim()); +} diff --git a/StarterNG/Views/Scenarios.axaml.cs b/StarterNG/Views/Scenarios.axaml.cs index eb00a93..4c0643f 100644 --- a/StarterNG/Views/Scenarios.axaml.cs +++ b/StarterNG/Views/Scenarios.axaml.cs @@ -492,20 +492,18 @@ public partial class Scenarios : UserControl btn.Classes.Add("Flat"); ToolTip.SetTip(btn, att.FilePath); // A button that does nothing is no way to say the file is not there. - btn.IsEnabled = File.Exists(Path.GetFullPath(att.FilePath)); + btn.IsEnabled = AppServices.Current.Attachments.Resolve(att) is not null; ToolTip.SetShowOnDisabled(btn, true); - btn.Click += (_, _) => OpenAttachment(att.FilePath); + btn.Click += (_, _) => OpenAttachment(att); attachmentsPanel.Children.Add(btn); } } - private static void OpenAttachment(string relativePath) + private static void OpenAttachment(SceneryAttachment attachment) { - string full = Path.GetFullPath(relativePath); - if (!File.Exists(full)) - full = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), relativePath)); - if (!File.Exists(full)) + if (AppServices.Current.Attachments.Resolve(attachment) is not { } found) return; + string full = Path.GetFullPath(found); try { Process.Start(new ProcessStartInfo(full) { UseShellExecute = true });