diff --git a/CHANGELOG.md b/CHANGELOG.md index 7133c72270..f52836997d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ Built with Unity 6.5 ### Added +- Plugin helpers resolve table-local media directories by controller namespace and game ID, with case-insensitive matching for VPX folder layouts. + - Windows Open Pinball Device support for Pinscape Pico with named nudge axes, firmware scale presets and preassigned cabinet buttons. The updated NativeInput library is required for OPD discovery. - Shared Pinscape joystick nudge setup and sampled rest calibration in the editor and Player, with explicit velocity presets, device-axis selection, editable SI scales, and sensor state reset on disconnect. diff --git a/VisualPinball.Plugins.Test/TableMediaPathsTests.cs b/VisualPinball.Plugins.Test/TableMediaPathsTests.cs new file mode 100644 index 0000000000..53119231ed --- /dev/null +++ b/VisualPinball.Plugins.Test/TableMediaPathsTests.cs @@ -0,0 +1,65 @@ +// Visual Pinball Engine +// Copyright (C) 2026 freezy and VPE Team +// SPDX-License-Identifier: MIT + +using System; +using System.IO; +using System.Linq; +using NUnit.Framework; + +namespace VisualPinball.Plugins.Test +{ + public sealed class TableMediaPathsTests + { + private string _root = null!; + + [SetUp] + public void SetUp() => _root = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"))).FullName; + + [TearDown] + public void TearDown() => Directory.Delete(_root, recursive: true); + + [Test] + public void LooksUpEachComponentAndRetainsItsSpelling() + { + string mediaRoot = Directory.CreateDirectory(Path.Combine(_root, "Sérum")).FullName; + string local = Directory.CreateDirectory(Path.Combine(mediaRoot, "PINMAME", "ROM")).FullName; + string fallback = Directory.CreateDirectory(Path.Combine(mediaRoot, "Rom")).FullName; + string file = Path.Combine(local, "ROM.cRZ"); + File.WriteAllText(file, ""); + + var media = TableMediaPaths.FindDirectory(_root, "sérum"); + Assert.That(TableMediaPaths.GameDirectories(media, new GameIdentity("pinmame", "rom")).ToArray(), + Is.EqualTo(new[] { local, fallback })); + Assert.That(TableMediaPaths.FindFile(local, "rom.crz"), Is.EqualTo(file)); + } + + [Test] + public void RejectsCaseCollisionsEvenWithAnExactMatch() + { + Directory.CreateDirectory(Path.Combine(_root, "rom")); + Directory.CreateDirectory(Path.Combine(_root, "ROM")); + if (Directory.GetDirectories(_root).Length != 2) { + Assert.Ignore("Requires a case-sensitive filesystem."); + } + File.WriteAllText(Path.Combine(_root, "rom.crz"), ""); + File.WriteAllText(Path.Combine(_root, "ROM.CRZ"), ""); + + Assert.Multiple(() => { + Assert.That(TableMediaPaths.FindDirectory(_root, "rom"), Is.Null); + Assert.That(TableMediaPaths.GameDirectories(_root, new GameIdentity("pinmame", "rom")), Is.Empty); + Assert.That(TableMediaPaths.FindFile(_root, "rom.crz"), Is.Null); + }); + } + + [TestCase("..")] + [TestCase("../outside")] + [TestCase("pinmame\\rom")] + [TestCase("C:outside")] + public void RejectsPathsAsGameKeys(string key) + { + Assert.That(TableMediaPaths.GameDirectories(_root, new GameIdentity("pinmame", key)), Is.Empty); + Assert.That(TableMediaPaths.FindDirectory(_root, key), Is.Null); + } + } +} diff --git a/VisualPinball.Plugins.Test/TableMediaPathsTests.cs.meta b/VisualPinball.Plugins.Test/TableMediaPathsTests.cs.meta new file mode 100644 index 0000000000..a522554b24 --- /dev/null +++ b/VisualPinball.Plugins.Test/TableMediaPathsTests.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: c1e57e83a7c8d7c4eb637d5d788c96bc \ No newline at end of file diff --git a/VisualPinball.Plugins/Api/TableMediaPaths.cs b/VisualPinball.Plugins/Api/TableMediaPaths.cs new file mode 100644 index 0000000000..aaf0b896ad --- /dev/null +++ b/VisualPinball.Plugins/Api/TableMediaPaths.cs @@ -0,0 +1,64 @@ +// Visual Pinball Engine +// Copyright (C) 2026 freezy and VPE Team +// SPDX-License-Identifier: MIT + +#nullable enable + +using System; +using System.Collections.Generic; +using System.IO; + +namespace VisualPinball.Plugins +{ + /// Locates VPX-compatible media paths, skipping ambiguous case-insensitive matches. + public static class TableMediaPaths + { + private static readonly char[] InvalidNameCharacters = { '/', '\\', ':', '\0' }; + + /// Existing game directories, with the controller namespace taking precedence over a bare game key. + public static IEnumerable GameDirectories(string? root, GameIdentity game) + { + if (!IsName(game.Provider) || !IsName(game.Key)) yield break; + var scoped = FindDirectory(root, game.Provider, game.Key); + if (scoped != null) yield return scoped; + var unscoped = FindDirectory(root, game.Key); + if (unscoped != null) yield return unscoped; + } + + /// Finds an existing child directory by matching each component separately. + public static string? FindDirectory(string? root, params string[] names) + { + if (string.IsNullOrWhiteSpace(root) || names.Length == 0) return null; + foreach (string name in names) { + root = FindChild(root, name, directoryOnly: true); + if (root == null) return null; + } + return root; + } + + /// Finds a file directly in the given directory. + public static string? FindFile(string? directory, string name) => FindChild(directory, name, directoryOnly: false); + + private static bool IsName(string? name) => !string.IsNullOrWhiteSpace(name) && name != "." && name != ".." + && name.IndexOfAny(InvalidNameCharacters) < 0; + + private static string? FindChild(string? directory, string name, bool directoryOnly) + { + if (string.IsNullOrWhiteSpace(directory) || !IsName(name)) return null; + try { + string? match = null; + var entries = directoryOnly ? Directory.EnumerateDirectories(directory) : Directory.EnumerateFiles(directory); + foreach (string path in entries) { + string actual = Path.GetFileName(path); + if (!string.Equals(actual, name, StringComparison.OrdinalIgnoreCase)) continue; + if (match != null) return null; + match = path; + } + return match; + } catch (Exception error) when (error is IOException || error is UnauthorizedAccessException + || error is ArgumentException || error is NotSupportedException) { + return null; + } + } + } +} diff --git a/VisualPinball.Plugins/Api/TableMediaPaths.cs.meta b/VisualPinball.Plugins/Api/TableMediaPaths.cs.meta new file mode 100644 index 0000000000..36daad7ce1 --- /dev/null +++ b/VisualPinball.Plugins/Api/TableMediaPaths.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 014f7d397e76968458317f8760127d22 \ No newline at end of file