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
19 changes: 18 additions & 1 deletion VisualPinball.Plugins.Test/SetupSettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,17 @@ public void OutputMapsComeFromTheProviderAndAreNeverStored()
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");
var global = session.SetOutputTriggerAsync("ports", "1", "S28", default);
Assert.That(global.Result.Success, Is.False, "a trigger is for a game"); Assert.That(provider.Triggered, Is.Null);
using (var gameSession = new PluginSettingsSession(factory, host.Configuration, "windows", new GameIdentity("pinmame", "t2_l8"))) {
var trigger = gameSession.SetOutputTriggerAsync("ports", "1", " S28 Blink ", default); PluginHostTests.Run(host, trigger);
Assert.That(trigger.Result.Success, Is.True); Assert.That(provider.Triggered, Is.EqualTo("ports:1=S28 Blink@t2_l8"), "without the spaces around it, for the game");
trigger = gameSession.SetOutputTriggerAsync("ports", "1", null, default); PluginHostTests.Run(host, trigger);
Assert.That(provider.Triggered, Is.EqualTo("ports:1=configuration@t2_l8"));
trigger = gameSession.SetOutputTriggerAsync("ports", "1", " ", default); PluginHostTests.Run(host, trigger);
Assert.That(provider.Triggered, Is.EqualTo("ports:1=@t2_l8"), "nothing fires it in this game");
Assert.That(gameSession.HasEdits("outputs"), Is.False, "a trigger 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() };
Expand All @@ -150,6 +161,8 @@ public void OutputMapsComeFromTheProviderAndAreNeverStored()
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);
using (var plainGame = new PluginSettingsSession(plain, Host(plain).Configuration, "windows", new GameIdentity("pinmame", "t2_l8")))
Assert.That(plainGame.SetOutputTriggerAsync("ports", "1", "S28", 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" } }) {
Expand All @@ -170,8 +183,12 @@ 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, IPluginOutputAssigner
private sealed class Provider : IPluginSettingsProvider, IPluginSettingsInfoProvider, IPluginOutputMapProvider, IPluginOutputRearranger, IPluginOutputAssigner,
IPluginOutputTriggerEditor
{
internal string? Triggered;
public Task<PluginActionResult> SetTriggerAsync(string provider, string output, string? trigger, PluginChoiceRequest request, CancellationToken cancellation)
{ Triggered = $"{provider}:{output}={trigger ?? "configuration"}@{request.Game?.Key}"; return Task.FromResult(new PluginActionResult(true, "Set")); }
internal string? Tested;
internal string? Swapped;
internal string? Assigned;
Expand Down
58 changes: 58 additions & 0 deletions VisualPinball.Plugins/Configuration/PluginSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,13 @@ public sealed class PluginOutput
/// three outputs of an RGB toy; empty for none, or when the board lists no toys.
/// </summary>
public string Toy { get; set; } = "";
/// <summary>
/// For a game: what makes the output fire in it, as the plugin writes it, such as <c>S28 Blink</c> for while solenoid 28
/// is on; empty when nothing does, and outside a game. Of an RGB toy, its first output has it.
/// </summary>
public string Trigger { get; set; } = "";
/// <summary>Whether <see cref="Trigger"/> is the user's own for the game instead of the configuration's.</summary>
public bool TriggerChanged { 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 Down Expand Up @@ -997,6 +1004,22 @@ public sealed class PluginOutputToy
public PluginOutputToy(string id, string label, bool rgb = false) { Id = id ?? ""; Label = label ?? ""; Rgb = rgb; }
}

/// <summary>
/// Something of a game that can make an output fire, offered to pick when changing its <see cref="PluginOutput.Trigger"/>,
/// such as solenoid 28, the shaker.
/// </summary>
public sealed class PluginGameOutput
{
/// <summary>The trigger as the plugin writes it, such as <c>S28</c>; unique in its map.</summary>
public string Trigger { get; }
/// <summary>What the game calls it, such as <c>Shaker</c>; may be empty.</summary>
public string Label { get; }
/// <summary>Creates a game output.</summary>
/// <param name="trigger">The trigger, such as <c>S28</c>.</param>
/// <param name="label">What the game calls it, or null for nothing.</param>
public PluginGameOutput(string trigger, string label) { Trigger = trigger ?? ""; Label = label ?? ""; }
}

/// <summary>A board of a <see cref="PluginOutputMap"/> and its outputs.</summary>
public sealed class PluginOutputBoard
{
Expand Down Expand Up @@ -1041,6 +1064,16 @@ public sealed class PluginOutputMap
/// grid and turns the test buttons off.
/// </summary>
public string TestUnavailableReason { get; }
/// <summary>
/// For a game: whether the user can change what makes an output fire in it, through <see cref="IPluginOutputTriggerEditor"/>.
/// The outputs say what does in <see cref="PluginOutput.Trigger"/>.
/// </summary>
public bool TriggersEditable { get; set; }
/// <summary>
/// For a game whose <see cref="TriggersEditable"/>: what of the game can make an output fire, such as its solenoids and
/// lamps by name, for the user to pick from instead of typing a trigger; may be empty.
/// </summary>
public IReadOnlyList<PluginGameOutput> GameOutputs { get; set; } = Array.Empty<PluginGameOutput>();
/// <summary>Creates the map of an <see cref="SettingKind.Outputs"/> field.</summary>
/// <param name="boards">The boards in display order.</param>
/// <param name="emptyMessage">Shown when there are no boards.</param>
Expand Down Expand Up @@ -1159,4 +1192,29 @@ public interface IPluginOutputAssigner
/// <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);
}

/// <summary>
/// Implemented, in addition to <see cref="IPluginOutputMapProvider"/>, by a provider whose map lets the user change, for one
/// game, what makes an output fire (<see cref="PluginOutputMap.TriggersEditable"/>), such as the shaker on a solenoid the
/// configuration doesn't use for it.
/// </summary>
/// <remarks>
/// Like an assignment, a change takes effect at once, without the settings page's Apply, and only for the game of the request:
/// the plugin keeps it itself, also when the configuration is imported again. A call that fails or is cancelled changes
/// nothing. The same threading rules as for <see cref="IPluginOutputMapProvider"/> apply.
/// </remarks>
public interface IPluginOutputTriggerEditor
{
/// <summary>Sets what makes an output fire in the request's game, or gives it back to the configuration.</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="trigger">
/// The trigger as the user typed or picked it, such as a <see cref="PluginGameOutput.Trigger"/>; empty for nothing to fire it
/// in this game; null for what the configuration says.
/// </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> SetTriggerAsync(string provider, string output, string? trigger, PluginChoiceRequest request, CancellationToken cancellation);
}
}
16 changes: 16 additions & 0 deletions VisualPinball.Plugins/Configuration/PluginSettingsSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,22 @@ public Task<PluginActionResult> AddToyAsync(string provider, string output, stri
: (_factory.SettingsProvider as IPluginOutputAssigner)?.AddToyAsync(provider, output, name.Trim(), Request(Effective), cancellation)
?? Task.FromResult(new PluginActionResult(false, "This plugin cannot add toys."));
/// <summary>
/// Sets what makes an output of an <see cref="SettingKind.Outputs"/> field fire in the session's game, or gives it back to
/// the configuration, through <see cref="IPluginOutputTriggerEditor.SetTriggerAsync"/>. 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="trigger">
/// The trigger, surrounding spaces left out; empty for nothing to fire it in this game; null for what the configuration says.
/// </param>
/// <param name="cancellation">Passed to the provider.</param>
/// <returns>The provider's result, or a failure outside a game or when the plugin cannot change triggers.</returns>
public Task<PluginActionResult> SetOutputTriggerAsync(string provider, string output, string? trigger, CancellationToken cancellation)
=> !Game.HasValue ? Task.FromResult(new PluginActionResult(false, "Choose a game first: a trigger is changed for one game."))
: (_factory.SettingsProvider as IPluginOutputTriggerEditor)?.SetTriggerAsync(provider, output, trigger?.Trim(), Request(Effective), cancellation)
?? Task.FromResult(new PluginActionResult(false, "This plugin cannot change what makes its outputs fire."));
/// <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
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ await ReportProgressAndYieldAsync(progress, RuntimePackageLoadStage.RestoringGlo
ReportProgress(progress, RuntimePackageLoadStage.RestoringGlobals, 1f, "Table wiring ready.");
}

private static List<T> ReadGlobalList<T>(IPackageFolder folder, string fileName)
internal static List<T> ReadGlobalList<T>(IPackageFolder folder, string fileName)
{
if (!folder.TryGetFile(fileName, out var file, PackageApi.Packer.FileExtension)) {
return new List<T>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,32 @@ private sealed class Cached
/// <exception cref="InvalidDataException">The readers found more than one distinct identity in the package.</exception>
/// <exception cref="OperationCanceledException"><paramref name="cancellation"/> was cancelled.</exception>
public static Task<GameIdentity> ReadAsync(string path, CancellationToken cancellation = default) => Task.Run(() => Read(path, cancellation), cancellation);
/// <summary>
/// Reads what a table package wires, its switches, coils, lamps and wires with their ids and descriptions, on a worker thread
/// and without loading the table; such as for a plugin that names a game's outputs on its settings page. Safe to call from any
/// thread. The references to the table's elements are not restored, and nothing is cached.
/// </summary>
/// <param name="path">Path of the table package; a relative path is resolved against the current directory.</param>
/// <param name="cancellation">Cancels the read.</param>
/// <returns>The mappings, empty lists for a package without any. Errors opening or reading the file fault the task.</returns>
/// <exception cref="OperationCanceledException"><paramref name="cancellation"/> was cancelled.</exception>
public static Task<MappingConfig> ReadMappingsAsync(string path, CancellationToken cancellation = default) => Task.Run(() => {
using var storage = PackageApi.StorageManager.OpenStorage(Path.GetFullPath(path), cancellation);
cancellation.ThrowIfCancellationRequested();
if (!storage.GetFolder(PackageApi.TableFolder).TryGetFolder(PackageApi.GlobalFolder, out var global)) return new MappingConfig();
List<T> Read<T>(string file)
{
var list = RuntimePackageReader.ReadGlobalList<T>(global, file);
cancellation.ThrowIfCancellationRequested();
return list;
}
return new MappingConfig {
Switches = Read<SwitchMapping>(PackageApi.SwitchesFile),
Coils = Read<CoilMapping>(PackageApi.CoilsFile),
Lamps = Read<LampMapping>(PackageApi.LampsFile),
Wires = Read<WireMapping>(PackageApi.WiresFile),
};
}, cancellation);
private static GameIdentity Read(string path, CancellationToken cancellation)
{
path = Path.GetFullPath(path);
Expand Down
Loading