mirror of
https://github.com/MaSzyna-EU07/StarterNG.git
synced 2026-11-18 22:45:59 +01:00
fix: open a scenery's attachments whatever the case of their names
A //$f attachment is looked up from the game folder through the new AttachmentLocator, so on Linux its button is enabled and opens the file when the scenery names it in other letter case or with backslashes.
This commit is contained in:
@@ -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 }));
|
||||
}
|
||||
|
||||
@@ -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; }
|
||||
|
||||
24
StarterNG/Infrastructure/Sceneries/AttachmentLocator.cs
Normal file
24
StarterNG/Infrastructure/Sceneries/AttachmentLocator.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using StarterNG.Application.Abstractions;
|
||||
using StarterNG.Domain.Sceneries;
|
||||
|
||||
namespace StarterNG.Infrastructure.Sceneries;
|
||||
|
||||
/// <summary>Where the file a scenery attaches (<c>//$f</c>) is on disk.</summary>
|
||||
public sealed class AttachmentLocator
|
||||
{
|
||||
private readonly IGamePaths _paths;
|
||||
private readonly CaseInsensitivePaths _names;
|
||||
|
||||
public AttachmentLocator(IFileSystem files, IGamePaths paths)
|
||||
{
|
||||
_paths = paths;
|
||||
_names = new CaseInsensitivePaths(files);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public string? Resolve(SceneryAttachment attachment) =>
|
||||
string.IsNullOrWhiteSpace(attachment.FilePath) ? null : _names.Find(_paths.Root, attachment.FilePath.Trim());
|
||||
}
|
||||
@@ -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 });
|
||||
|
||||
Reference in New Issue
Block a user