From d98e30a33cd1d353fadf02140dff60828f4e9899 Mon Sep 17 00:00:00 2001 From: freezy Date: Sat, 7 Nov 2026 15:38:08 +0100 Subject: [PATCH 1/2] plugins: resolve shared table media paths --- CHANGELOG.md | 2 + .../TableMediaPathsTests.cs | 47 +++++++++++++ .../TableMediaPathsTests.cs.meta | 2 + VisualPinball.Plugins/Api/TableMediaPaths.cs | 66 +++++++++++++++++++ .../Api/TableMediaPaths.cs.meta | 2 + 5 files changed, 119 insertions(+) create mode 100644 VisualPinball.Plugins.Test/TableMediaPathsTests.cs create mode 100644 VisualPinball.Plugins.Test/TableMediaPathsTests.cs.meta create mode 100644 VisualPinball.Plugins/Api/TableMediaPaths.cs create mode 100644 VisualPinball.Plugins/Api/TableMediaPaths.cs.meta 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..f8e09dcd1b --- /dev/null +++ b/VisualPinball.Plugins.Test/TableMediaPathsTests.cs @@ -0,0 +1,47 @@ +// 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)); + } + + [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..2b03201b84 --- /dev/null +++ b/VisualPinball.Plugins/Api/TableMediaPaths.cs @@ -0,0 +1,66 @@ +// 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 on both case-sensitive and case-insensitive filesystems. + 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; + bool ambiguous = false; + 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.Ordinal)) return path; + if (!string.Equals(actual, name, StringComparison.OrdinalIgnoreCase)) continue; + ambiguous |= match != null; + match = path; + } + return ambiguous ? null : 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 From 4148a9ada8951db6eb49196839fd013ef4fed406 Mon Sep 17 00:00:00 2001 From: freezy Date: Sat, 7 Nov 2026 16:46:45 +0100 Subject: [PATCH 2/2] plugins: reject case collisions in table media lookup --- .../TableMediaPathsTests.cs | 18 ++++++++++++++++++ VisualPinball.Plugins/Api/TableMediaPaths.cs | 8 +++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/VisualPinball.Plugins.Test/TableMediaPathsTests.cs b/VisualPinball.Plugins.Test/TableMediaPathsTests.cs index f8e09dcd1b..53119231ed 100644 --- a/VisualPinball.Plugins.Test/TableMediaPathsTests.cs +++ b/VisualPinball.Plugins.Test/TableMediaPathsTests.cs @@ -34,6 +34,24 @@ public void LooksUpEachComponentAndRetainsItsSpelling() 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")] diff --git a/VisualPinball.Plugins/Api/TableMediaPaths.cs b/VisualPinball.Plugins/Api/TableMediaPaths.cs index 2b03201b84..aaf0b896ad 100644 --- a/VisualPinball.Plugins/Api/TableMediaPaths.cs +++ b/VisualPinball.Plugins/Api/TableMediaPaths.cs @@ -10,7 +10,7 @@ namespace VisualPinball.Plugins { - /// Locates VPX-compatible media paths on both case-sensitive and case-insensitive filesystems. + /// Locates VPX-compatible media paths, skipping ambiguous case-insensitive matches. public static class TableMediaPaths { private static readonly char[] InvalidNameCharacters = { '/', '\\', ':', '\0' }; @@ -47,16 +47,14 @@ private static bool IsName(string? name) => !string.IsNullOrWhiteSpace(name) && if (string.IsNullOrWhiteSpace(directory) || !IsName(name)) return null; try { string? match = null; - bool ambiguous = false; 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.Ordinal)) return path; if (!string.Equals(actual, name, StringComparison.OrdinalIgnoreCase)) continue; - ambiguous |= match != null; + if (match != null) return null; match = path; } - return ambiguous ? null : match; + return match; } catch (Exception error) when (error is IOException || error is UnauthorizedAccessException || error is ArgumentException || error is NotSupportedException) { return null;