From e100b31b36a70fc25ada65a1b49c23da492bbf61 Mon Sep 17 00:00:00 2001 From: freezy Date: Mon, 30 Nov 2026 12:57:45 +0100 Subject: [PATCH 1/3] plugins: let a plugin choose what each output drives An output can now name the toy it drives, and a board lists its toys and whether it takes new ones, so the player can offer them. A plugin that implements IPluginOutputAssigner gives an output a toy, nothing, or a new toy by name; the settings session forwards both and says when a plugin cannot. --- .../SetupSettingsTests.cs | 23 ++++- .../Configuration/PluginSchema.cs | 85 ++++++++++++++++++- .../Configuration/PluginSettingsSession.cs | 25 ++++++ 3 files changed, 129 insertions(+), 4 deletions(-) diff --git a/VisualPinball.Plugins.Test/SetupSettingsTests.cs b/VisualPinball.Plugins.Test/SetupSettingsTests.cs index c8de3ed54b..99677e42ab 100644 --- a/VisualPinball.Plugins.Test/SetupSettingsTests.cs +++ b/VisualPinball.Plugins.Test/SetupSettingsTests.cs @@ -133,6 +133,14 @@ public void OutputMapsComeFromTheProviderAndAreNeverStored() Assert.That(swap.Result.Success, Is.True); Assert.That(provider.Swapped, Is.EqualTo("ports:1>2x1")); Assert.That(session.SwapOutputsAsync("ports", "1", "2", 0, default).Result.Success, Is.False, "nothing to swap"); Assert.That(session.HasEdits("outputs"), Is.False, "a swap takes effect without the draft"); + var assign = session.AssignOutputAsync("ports", "1", "shaker", default); PluginHostTests.Run(host, assign); + Assert.That(assign.Result.Success, Is.True); Assert.That(provider.Assigned, Is.EqualTo("ports:1=shaker")); + assign = session.AssignOutputAsync("ports", "2", null, default); PluginHostTests.Run(host, assign); + Assert.That(provider.Assigned, Is.EqualTo("ports:2=nothing")); + var add = session.AddToyAsync("ports", "2", " Gear motor ", default); PluginHostTests.Run(host, add); + Assert.That(add.Result.Success, Is.True); Assert.That(provider.Added, Is.EqualTo("ports:2+Gear motor"), "without the spaces around it"); + Assert.That(session.AddToyAsync("ports", "2", " ", default).Result.Success, Is.False, "a toy needs a name"); + Assert.That(session.HasEdits("outputs"), Is.False, "an assignment takes effect without the draft"); Assert.Throws(() => host.CommitSettingAsync(factory, "outputs", new JValue("x"))); session.Reset(); Assert.That(session.HasEdits("outputs"), Is.False); var plain = new TestFactory("plain") { SettingsProvider = new PlainProvider() }; @@ -140,8 +148,15 @@ public void OutputMapsComeFromTheProviderAndAreNeverStored() Assert.That(plainSession.DescribeOutputsAsync("ports", default).Result.Boards, Is.Empty); Assert.That(plainSession.TestOutputAsync("ports", "1", default).Result.Success, Is.False); Assert.That(plainSession.SwapOutputsAsync("ports", "1", "2", 1, default).Result.Success, Is.False); + Assert.That(plainSession.AssignOutputAsync("ports", "1", null, default).Result.Success, Is.False); + Assert.That(plainSession.AddToyAsync("ports", "1", "Shaker", default).Result.Success, Is.False); var twice = new PluginOutputBoard("twice", "", new[] { new PluginOutput("1", 1, ""), new PluginOutput("1", 1, "") }); Assert.Throws(() => new PluginOutputMap(new[] { twice }), "a test could not tell the outputs apart"); + var toys = new PluginOutputBoard("toys", "", new[] { new PluginOutput("1", 1, "Shaker") { Toy = "shaker" } }) { + Toys = new[] { new PluginOutputToy("shaker", "Shaker"), new PluginOutputToy("shaker", "Shaker 2") } }; + Assert.Throws(() => new PluginOutputMap(new[] { toys }), "an assignment could not tell the toys apart"); + var unlisted = new PluginOutputBoard("unlisted", "", new[] { new PluginOutput("1", 1, "Shaker") { Toy = "shaker" } }); + Assert.Throws(() => new PluginOutputMap(new[] { unlisted }), "an output drives a toy its board doesn't list"); } private sealed class PlainProvider : IPluginSettingsProvider { @@ -153,10 +168,16 @@ public Task RunActionAsync(string action, PluginChoiceReques } private sealed class ProgressSink : IProgress { internal PluginActionProgress Last; public void Report(PluginActionProgress value) => Last = value; } - private sealed class Provider : IPluginSettingsProvider, IPluginSettingsInfoProvider, IPluginOutputMapProvider, IPluginOutputRearranger + private sealed class Provider : IPluginSettingsProvider, IPluginSettingsInfoProvider, IPluginOutputMapProvider, IPluginOutputRearranger, IPluginOutputAssigner { internal string? Tested; internal string? Swapped; + internal string? Assigned; + internal string? Added; + public Task AssignOutputAsync(string provider, string output, string? toy, PluginChoiceRequest request, CancellationToken cancellation) + { Assigned = $"{provider}:{output}={toy ?? "nothing"}"; return Task.FromResult(new PluginActionResult(true, "Assigned")); } + public Task AddToyAsync(string provider, string output, string name, PluginChoiceRequest request, CancellationToken cancellation) + { Added = $"{provider}:{output}+{name}"; return Task.FromResult(new PluginActionResult(true, "Added")); } public Task SwapOutputsAsync(string provider, string first, string second, int count, PluginChoiceRequest request, CancellationToken cancellation) { Swapped = $"{provider}:{first}>{second}x{count}"; return Task.FromResult(new PluginActionResult(true, "Swapped")); } internal PluginOutputLeases? Leases; diff --git a/VisualPinball.Plugins/Configuration/PluginSchema.cs b/VisualPinball.Plugins/Configuration/PluginSchema.cs index 85fb19418c..d00201d2ff 100644 --- a/VisualPinball.Plugins/Configuration/PluginSchema.cs +++ b/VisualPinball.Plugins/Configuration/PluginSchema.cs @@ -964,6 +964,11 @@ public sealed class PluginOutput /// empty for none. /// public string Note { get; set; } = ""; + /// + /// The of the toy of its board's it drives, on all + /// three outputs of an RGB toy; empty for none, or when the board lists no toys. + /// + public string Toy { get; set; } = ""; /// Creates an output. /// What the test gets; see . /// The number on its board. @@ -973,6 +978,25 @@ public PluginOutput(string id, int number, string label, PluginOutputChannel cha { Id = id ?? ""; Number = number; Label = label ?? ""; Channel = channel; } } + /// + /// A toy an output of a can be given through , such as a + /// shaker or a flasher the configuration names, whether an output drives it or not. + /// + public sealed class PluginOutputToy + { + /// What gets to give an output this toy, unique in its board. Private to the plugin. + public string Id { get; } + /// Its name, such as Shaker. + public string Label { get; } + /// Whether it is an RGB toy, which takes three outputs in a row: its red, green and blue. + public bool Rgb { get; } + /// Creates a toy. + /// What the assignment gets; see . + /// Its name. + /// Whether it takes three outputs, red, green and blue. + public PluginOutputToy(string id, string label, bool rgb = false) { Id = id ?? ""; Label = label ?? ""; Rgb = rgb; } + } + /// A board of a and its outputs. public sealed class PluginOutputBoard { @@ -987,6 +1011,16 @@ public sealed class PluginOutputBoard /// cabling that differs from what the configuration says. /// public bool Rearrangeable { get; set; } + /// + /// The toys its outputs can be given through , those no output drives included; empty + /// when what its outputs drive can't be chosen. Each output names its own in . + /// + public IReadOnlyList Toys { get; set; } = Array.Empty(); + /// + /// Whether an output can be given a toy the board doesn't list yet, by a name the user types, through + /// ; such as a gear motor added to the cabinet that no configuration names. + /// + public bool AcceptsNewToys { get; set; } /// Creates a board. /// The board's name. /// Where it is set up or how it is connected, or null for nothing. @@ -1011,14 +1045,24 @@ public sealed class PluginOutputMap /// The boards in display order. /// Shown when there are no boards. /// Why nothing can be tested now, or null when it can. - /// Two outputs have the same , so a test could not tell them apart. + /// + /// Two outputs have the same , so a test could not tell them apart; two toys of a board have the + /// same ; or an output drives a toy its board doesn't list. + /// public PluginOutputMap(IReadOnlyList boards, string emptyMessage = "", string testUnavailableReason = "") { Boards = boards ?? Array.Empty(); EmptyMessage = emptyMessage ?? ""; TestUnavailableReason = testUnavailableReason ?? ""; var ids = new HashSet(StringComparer.Ordinal); - foreach (var board in Boards) - foreach (var output in board.Outputs) + foreach (var board in Boards) { + var toys = new HashSet(StringComparer.Ordinal); + foreach (var toy in board.Toys ?? Array.Empty()) + if (!toys.Add(toy.Id)) throw new ArgumentException($"Toy {toy.Id} of {board.Title} is listed more than once.", nameof(boards)); + foreach (var output in board.Outputs) { if (!ids.Add(output.Id)) throw new ArgumentException($"Output {output.Id} is in the map more than once.", nameof(boards)); + if (!string.IsNullOrEmpty(output.Toy) && !toys.Contains(output.Toy)) + throw new ArgumentException($"Output {output.Id} drives toy {output.Toy}, which {board.Title} doesn't list.", nameof(boards)); + } + } } } @@ -1075,4 +1119,39 @@ public interface IPluginOutputRearranger /// The result to show; is ignored. Task SwapOutputsAsync(string provider, string first, string second, int count, PluginChoiceRequest request, CancellationToken cancellation); } + + /// + /// Implemented, in addition to , by a provider whose boards list the toys their outputs + /// can be given (): the user picks what an output drives, such as the shaker on output 14, + /// or that it drives nothing. + /// + /// + /// Like a swap, an assignment takes effect at once, without the settings page's Apply: the plugin keeps it itself, and the + /// Player draws the map again afterwards. The same threading rules as for apply. + /// + public interface IPluginOutputAssigner + { + /// + /// Gives an output a toy of its board, or nothing. What the output drove before moves to where the toy was, so nothing is + /// lost, or drives nothing when the toy was on no output. An RGB toy takes the output and the next two. + /// + /// The field's . + /// The of the output. + /// The of the toy, or null for nothing. + /// The game, table and draft settings; is empty. + /// Cancelled when the page is closed. + /// The result to show; is ignored. + Task AssignOutputAsync(string provider, string output, string? toy, PluginChoiceRequest request, CancellationToken cancellation); + /// + /// Gives an output a new toy of the name the user typed, on a board that ; + /// what the output drove before then drives nothing. The board lists the toy from then on. + /// + /// The field's . + /// The of the output. + /// The toy's name, not empty. + /// The game, table and draft settings; is empty. + /// Cancelled when the page is closed. + /// The result to show; is ignored. + Task AddToyAsync(string provider, string output, string name, PluginChoiceRequest request, CancellationToken cancellation); + } } diff --git a/VisualPinball.Plugins/Configuration/PluginSettingsSession.cs b/VisualPinball.Plugins/Configuration/PluginSettingsSession.cs index e3e7780010..becd9abdd0 100644 --- a/VisualPinball.Plugins/Configuration/PluginSettingsSession.cs +++ b/VisualPinball.Plugins/Configuration/PluginSettingsSession.cs @@ -216,6 +216,31 @@ public Task SwapOutputsAsync(string provider, string first, : (_factory.SettingsProvider as IPluginOutputRearranger)?.SwapOutputsAsync(provider, first, second, count, Request(Effective), cancellation) ?? Task.FromResult(new PluginActionResult(false, "This plugin cannot rearrange outputs.")); /// + /// Gives an output of an field a toy of its board, or nothing, through + /// . Takes effect at once; the draft is not involved. + /// + /// The field's . + /// The of the output. + /// The of the toy, or null for nothing. + /// Passed to the provider. + /// The provider's result, or a failure when the plugin cannot assign outputs. + public Task AssignOutputAsync(string provider, string output, string? toy, CancellationToken cancellation) + => (_factory.SettingsProvider as IPluginOutputAssigner)?.AssignOutputAsync(provider, output, toy, Request(Effective), cancellation) + ?? Task.FromResult(new PluginActionResult(false, "This plugin cannot change what its outputs drive.")); + /// + /// Gives an output of an field a new toy of a name the user typed, through + /// . Takes effect at once; the draft is not involved. + /// + /// The field's . + /// The of the output. + /// The toy's name; surrounding spaces are left out. + /// Passed to the provider. + /// The provider's result, or a failure for an empty name or when the plugin cannot add toys. + public Task AddToyAsync(string provider, string output, string name, CancellationToken cancellation) + => string.IsNullOrWhiteSpace(name) ? Task.FromResult(new PluginActionResult(false, "Type the toy's name.")) + : (_factory.SettingsProvider as IPluginOutputAssigner)?.AddToyAsync(provider, output, name.Trim(), Request(Effective), cancellation) + ?? Task.FromResult(new PluginActionResult(false, "This plugin cannot add toys.")); + /// /// Runs the action of an action field through , such as PinUP locating /// LibVLC. When it succeeds and returns , the values of the fields editable in this /// scope are put into the draft as pending edits; the user still has to apply them. From d226290a3545d2ccbf0e1a7a8d0c9cdda841e04a Mon Sep 17 00:00:00 2001 From: freezy Date: Mon, 30 Nov 2026 13:02:38 +0100 Subject: [PATCH 2/3] plugins: say what a failed output change leaves --- VisualPinball.Plugins/Configuration/PluginSchema.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/VisualPinball.Plugins/Configuration/PluginSchema.cs b/VisualPinball.Plugins/Configuration/PluginSchema.cs index d00201d2ff..acc873ecc7 100644 --- a/VisualPinball.Plugins/Configuration/PluginSchema.cs +++ b/VisualPinball.Plugins/Configuration/PluginSchema.cs @@ -1128,6 +1128,8 @@ public interface IPluginOutputRearranger /// /// Like a swap, an assignment takes effect at once, without the settings page's Apply: the plugin keeps it itself, and the /// Player draws the map again afterwards. The same threading rules as for apply. + /// Each call is one change: a call that fails or is cancelled leaves the outputs and the toys as they were, so the user + /// can try again. The Player waits for a call to finish before it makes the next. /// public interface IPluginOutputAssigner { From 7d3840a38ace9d5cf22e2e83f7669358b22c5e3f Mon Sep 17 00:00:00 2001 From: freezy Date: Mon, 30 Nov 2026 13:16:18 +0100 Subject: [PATCH 3/3] plugins: reject a toy without an id --- VisualPinball.Plugins.Test/SetupSettingsTests.cs | 2 ++ VisualPinball.Plugins/Configuration/PluginSchema.cs | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/VisualPinball.Plugins.Test/SetupSettingsTests.cs b/VisualPinball.Plugins.Test/SetupSettingsTests.cs index 99677e42ab..f7ce5e6996 100644 --- a/VisualPinball.Plugins.Test/SetupSettingsTests.cs +++ b/VisualPinball.Plugins.Test/SetupSettingsTests.cs @@ -155,6 +155,8 @@ public void OutputMapsComeFromTheProviderAndAreNeverStored() var toys = new PluginOutputBoard("toys", "", new[] { new PluginOutput("1", 1, "Shaker") { Toy = "shaker" } }) { Toys = new[] { new PluginOutputToy("shaker", "Shaker"), new PluginOutputToy("shaker", "Shaker 2") } }; Assert.Throws(() => new PluginOutputMap(new[] { toys }), "an assignment could not tell the toys apart"); + var empty = new PluginOutputBoard("empty", "", new[] { new PluginOutput("1", 1, "") }) { Toys = new[] { new PluginOutputToy("", "Shaker") } }; + Assert.Throws(() => new PluginOutputMap(new[] { empty }), "an empty toy is an output that drives none"); var unlisted = new PluginOutputBoard("unlisted", "", new[] { new PluginOutput("1", 1, "Shaker") { Toy = "shaker" } }); Assert.Throws(() => new PluginOutputMap(new[] { unlisted }), "an output drives a toy its board doesn't list"); } diff --git a/VisualPinball.Plugins/Configuration/PluginSchema.cs b/VisualPinball.Plugins/Configuration/PluginSchema.cs index acc873ecc7..01cd06ad5b 100644 --- a/VisualPinball.Plugins/Configuration/PluginSchema.cs +++ b/VisualPinball.Plugins/Configuration/PluginSchema.cs @@ -1046,8 +1046,8 @@ public sealed class PluginOutputMap /// Shown when there are no boards. /// Why nothing can be tested now, or null when it can. /// - /// Two outputs have the same , so a test could not tell them apart; two toys of a board have the - /// same ; or an output drives a toy its board doesn't list. + /// Two outputs have the same , so a test could not tell them apart; a toy has an empty + /// , or two toys of a board have the same; or an output drives a toy its board doesn't list. /// public PluginOutputMap(IReadOnlyList boards, string emptyMessage = "", string testUnavailableReason = "") { @@ -1055,8 +1055,11 @@ public PluginOutputMap(IReadOnlyList boards, string emptyMess var ids = new HashSet(StringComparer.Ordinal); foreach (var board in Boards) { var toys = new HashSet(StringComparer.Ordinal); - foreach (var toy in board.Toys ?? Array.Empty()) + foreach (var toy in board.Toys ?? Array.Empty()) { + // an empty toy is an output that drives none + if (toy.Id.Length == 0) throw new ArgumentException($"A toy of {board.Title} has no id.", nameof(boards)); if (!toys.Add(toy.Id)) throw new ArgumentException($"Toy {toy.Id} of {board.Title} is listed more than once.", nameof(boards)); + } foreach (var output in board.Outputs) { if (!ids.Add(output.Id)) throw new ArgumentException($"Output {output.Id} is in the map more than once.", nameof(boards)); if (!string.IsNullOrEmpty(output.Toy) && !toys.Contains(output.Toy))