Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion VisualPinball.Plugins.Test/SetupSettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,32 @@ 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<ArgumentException>(() => host.CommitSettingAsync(factory, "outputs", new JValue("x")));
session.Reset(); Assert.That(session.HasEdits("outputs"), Is.False);
var plain = new TestFactory("plain") { SettingsProvider = new PlainProvider() };
using var plainSession = new PluginSettingsSession(plain, Host(plain).Configuration, "windows");
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<ArgumentException>(() => 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<ArgumentException>(() => 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<ArgumentException>(() => 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<ArgumentException>(() => new PluginOutputMap(new[] { unlisted }), "an output drives a toy its board doesn't list");
}
private sealed class PlainProvider : IPluginSettingsProvider
{
Expand All @@ -153,10 +170,16 @@ public Task<PluginActionResult> RunActionAsync(string action, PluginChoiceReques
}
private sealed class ProgressSink : IProgress<PluginActionProgress>
{ 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<PluginActionResult> 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<PluginActionResult> AddToyAsync(string provider, string output, string name, PluginChoiceRequest request, CancellationToken cancellation)
{ Added = $"{provider}:{output}+{name}"; return Task.FromResult(new PluginActionResult(true, "Added")); }
public Task<PluginActionResult> 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;
Expand Down
90 changes: 87 additions & 3 deletions VisualPinball.Plugins/Configuration/PluginSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,11 @@ public sealed class PluginOutput
/// empty for none.
/// </summary>
public string Note { get; set; } = "";
/// <summary>
/// The <see cref="PluginOutputToy.Id"/> of the toy of its board's <see cref="PluginOutputBoard.Toys"/> it drives, on all
/// three outputs of an RGB toy; empty for none, or when the board lists no toys.
/// </summary>
public string Toy { get; set; } = "";
/// <summary>Creates an output.</summary>
/// <param name="id">What the test gets; see <see cref="Id"/>.</param>
/// <param name="number">The number on its board.</param>
Expand All @@ -973,6 +978,25 @@ public PluginOutput(string id, int number, string label, PluginOutputChannel cha
{ Id = id ?? ""; Number = number; Label = label ?? ""; Channel = channel; }
}

/// <summary>
/// A toy an output of a <see cref="PluginOutputBoard"/> can be given through <see cref="IPluginOutputAssigner"/>, such as a
/// shaker or a flasher the configuration names, whether an output drives it or not.
/// </summary>
public sealed class PluginOutputToy
{
/// <summary>What <see cref="IPluginOutputAssigner.AssignOutputAsync"/> gets to give an output this toy, unique in its board. Private to the plugin.</summary>
public string Id { get; }
/// <summary>Its name, such as <c>Shaker</c>.</summary>
public string Label { get; }
/// <summary>Whether it is an RGB toy, which takes three outputs in a row: its red, green and blue.</summary>
public bool Rgb { get; }
/// <summary>Creates a toy.</summary>
/// <param name="id">What the assignment gets; see <see cref="Id"/>.</param>
/// <param name="label">Its name.</param>
/// <param name="rgb">Whether it takes three outputs, red, green and blue.</param>
public PluginOutputToy(string id, string label, bool rgb = false) { Id = id ?? ""; Label = label ?? ""; Rgb = rgb; }
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

/// <summary>A board of a <see cref="PluginOutputMap"/> and its outputs.</summary>
public sealed class PluginOutputBoard
{
Expand All @@ -987,6 +1011,16 @@ public sealed class PluginOutputBoard
/// cabling that differs from what the configuration says.
/// </summary>
public bool Rearrangeable { get; set; }
/// <summary>
/// The toys its outputs can be given through <see cref="IPluginOutputAssigner"/>, those no output drives included; empty
/// when what its outputs drive can't be chosen. Each output names its own in <see cref="PluginOutput.Toy"/>.
/// </summary>
public IReadOnlyList<PluginOutputToy> Toys { get; set; } = Array.Empty<PluginOutputToy>();
/// <summary>
/// Whether an output can be given a toy the board doesn't list yet, by a name the user types, through
/// <see cref="IPluginOutputAssigner.AddToyAsync"/>; such as a gear motor added to the cabinet that no configuration names.
/// </summary>
public bool AcceptsNewToys { get; set; }
/// <summary>Creates a board.</summary>
/// <param name="title">The board's name.</param>
/// <param name="detail">Where it is set up or how it is connected, or null for nothing.</param>
Expand All @@ -1011,14 +1045,27 @@ public sealed class PluginOutputMap
/// <param name="boards">The boards in display order.</param>
/// <param name="emptyMessage">Shown when there are no boards.</param>
/// <param name="testUnavailableReason">Why nothing can be tested now, or null when it can.</param>
/// <exception cref="ArgumentException">Two outputs have the same <see cref="PluginOutput.Id"/>, so a test could not tell them apart.</exception>
/// <exception cref="ArgumentException">
/// Two outputs have the same <see cref="PluginOutput.Id"/>, so a test could not tell them apart; a toy has an empty
/// <see cref="PluginOutputToy.Id"/>, or two toys of a board have the same; or an output drives a toy its board doesn't list.
/// </exception>
public PluginOutputMap(IReadOnlyList<PluginOutputBoard> boards, string emptyMessage = "", string testUnavailableReason = "")
{
Boards = boards ?? Array.Empty<PluginOutputBoard>(); EmptyMessage = emptyMessage ?? ""; TestUnavailableReason = testUnavailableReason ?? "";
var ids = new HashSet<string>(StringComparer.Ordinal);
foreach (var board in Boards)
foreach (var output in board.Outputs)
foreach (var board in Boards) {
var toys = new HashSet<string>(StringComparer.Ordinal);
foreach (var toy in board.Toys ?? Array.Empty<PluginOutputToy>()) {
// 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))
throw new ArgumentException($"Output {output.Id} drives toy {output.Toy}, which {board.Title} doesn't list.", nameof(boards));
}
}
}
}

Expand Down Expand Up @@ -1075,4 +1122,41 @@ public interface IPluginOutputRearranger
/// <returns>The result to show; <see cref="PluginActionResult.Changes"/> is ignored.</returns>
Task<PluginActionResult> SwapOutputsAsync(string provider, string first, string second, int count, PluginChoiceRequest request, CancellationToken cancellation);
}

/// <summary>
/// Implemented, in addition to <see cref="IPluginOutputMapProvider"/>, by a provider whose boards list the toys their outputs
/// can be given (<see cref="PluginOutputBoard.Toys"/>): the user picks what an output drives, such as the shaker on output 14,
/// or that it drives nothing.
/// </summary>
/// <remarks>
/// 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 <see cref="IPluginOutputMapProvider"/> 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.
/// </remarks>
public interface IPluginOutputAssigner
{
/// <summary>
/// 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.
/// </summary>
/// <param name="provider">The field's <see cref="PluginField.Provider"/>.</param>
/// <param name="output">The <see cref="PluginOutput.Id"/> of the output.</param>
/// <param name="toy">The <see cref="PluginOutputToy.Id"/> of the toy, or null for nothing.</param>
/// <param name="request">The game, table and draft settings; <see cref="PluginChoiceRequest.Resources"/> is empty.</param>
/// <param name="cancellation">Cancelled when the page is closed.</param>
/// <returns>The result to show; <see cref="PluginActionResult.Changes"/> is ignored.</returns>
Task<PluginActionResult> AssignOutputAsync(string provider, string output, string? toy, PluginChoiceRequest request, CancellationToken cancellation);
/// <summary>
/// Gives an output a new toy of the name the user typed, on a board that <see cref="PluginOutputBoard.AcceptsNewToys"/>;
/// what the output drove before then drives nothing. The board lists the toy from then on.
/// </summary>
/// <param name="provider">The field's <see cref="PluginField.Provider"/>.</param>
/// <param name="output">The <see cref="PluginOutput.Id"/> of the output.</param>
/// <param name="name">The toy's name, not empty.</param>
/// <param name="request">The game, table and draft settings; <see cref="PluginChoiceRequest.Resources"/> is empty.</param>
/// <param name="cancellation">Cancelled when the page is closed.</param>
/// <returns>The result to show; <see cref="PluginActionResult.Changes"/> is ignored.</returns>
Task<PluginActionResult> AddToyAsync(string provider, string output, string name, PluginChoiceRequest request, CancellationToken cancellation);
}
}
25 changes: 25 additions & 0 deletions VisualPinball.Plugins/Configuration/PluginSettingsSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,31 @@ public Task<PluginActionResult> 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."));
/// <summary>
/// Gives an output of an <see cref="SettingKind.Outputs"/> field a toy of its board, or nothing, through
/// <see cref="IPluginOutputAssigner.AssignOutputAsync"/>. Takes effect at once; the draft is not involved.
/// </summary>
/// <param name="provider">The field's <see cref="PluginField.Provider"/>.</param>
/// <param name="output">The <see cref="PluginOutput.Id"/> of the output.</param>
/// <param name="toy">The <see cref="PluginOutputToy.Id"/> of the toy, or null for nothing.</param>
/// <param name="cancellation">Passed to the provider.</param>
/// <returns>The provider's result, or a failure when the plugin cannot assign outputs.</returns>
public Task<PluginActionResult> 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."));
/// <summary>
/// Gives an output of an <see cref="SettingKind.Outputs"/> field a new toy of a name the user typed, through
/// <see cref="IPluginOutputAssigner.AddToyAsync"/>. Takes effect at once; the draft is not involved.
/// </summary>
/// <param name="provider">The field's <see cref="PluginField.Provider"/>.</param>
/// <param name="output">The <see cref="PluginOutput.Id"/> of the output.</param>
/// <param name="name">The toy's name; surrounding spaces are left out.</param>
/// <param name="cancellation">Passed to the provider.</param>
/// <returns>The provider's result, or a failure for an empty name or when the plugin cannot add toys.</returns>
public Task<PluginActionResult> 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."));
/// <summary>
/// Runs the action of an action field through <see cref="IPluginSettingsProvider.RunActionAsync"/>, such as PinUP locating
/// LibVLC. When it succeeds and returns <see cref="PluginActionResult.Changes"/>, the values of the fields editable in this
/// scope are put into the draft as pending edits; the user still has to apply them.
Expand Down
Loading