diff --git a/VisualPinball.Plugins.Test/SetupSettingsTests.cs b/VisualPinball.Plugins.Test/SetupSettingsTests.cs index f7ce5e6996..728fa275f7 100644 --- a/VisualPinball.Plugins.Test/SetupSettingsTests.cs +++ b/VisualPinball.Plugins.Test/SetupSettingsTests.cs @@ -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(() => host.CommitSettingAsync(factory, "outputs", new JValue("x"))); session.Reset(); Assert.That(session.HasEdits("outputs"), Is.False); var plain = new TestFactory("plain") { SettingsProvider = new PlainProvider() }; @@ -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(() => 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" } }) { @@ -170,8 +183,12 @@ 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, IPluginOutputAssigner + private sealed class Provider : IPluginSettingsProvider, IPluginSettingsInfoProvider, IPluginOutputMapProvider, IPluginOutputRearranger, IPluginOutputAssigner, + IPluginOutputTriggerEditor { + internal string? Triggered; + public Task 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; diff --git a/VisualPinball.Plugins/Configuration/PluginSchema.cs b/VisualPinball.Plugins/Configuration/PluginSchema.cs index 01cd06ad5b..9b8bfb6026 100644 --- a/VisualPinball.Plugins/Configuration/PluginSchema.cs +++ b/VisualPinball.Plugins/Configuration/PluginSchema.cs @@ -969,6 +969,13 @@ public sealed class PluginOutput /// three outputs of an RGB toy; empty for none, or when the board lists no toys. /// public string Toy { get; set; } = ""; + /// + /// For a game: what makes the output fire in it, as the plugin writes it, such as S28 Blink for while solenoid 28 + /// is on; empty when nothing does, and outside a game. Of an RGB toy, its first output has it. + /// + public string Trigger { get; set; } = ""; + /// Whether is the user's own for the game instead of the configuration's. + public bool TriggerChanged { get; set; } /// Creates an output. /// What the test gets; see . /// The number on its board. @@ -997,6 +1004,22 @@ public sealed class PluginOutputToy public PluginOutputToy(string id, string label, bool rgb = false) { Id = id ?? ""; Label = label ?? ""; Rgb = rgb; } } + /// + /// Something of a game that can make an output fire, offered to pick when changing its , + /// such as solenoid 28, the shaker. + /// + public sealed class PluginGameOutput + { + /// The trigger as the plugin writes it, such as S28; unique in its map. + public string Trigger { get; } + /// What the game calls it, such as Shaker; may be empty. + public string Label { get; } + /// Creates a game output. + /// The trigger, such as S28. + /// What the game calls it, or null for nothing. + public PluginGameOutput(string trigger, string label) { Trigger = trigger ?? ""; Label = label ?? ""; } + } + /// A board of a and its outputs. public sealed class PluginOutputBoard { @@ -1041,6 +1064,16 @@ public sealed class PluginOutputMap /// grid and turns the test buttons off. /// public string TestUnavailableReason { get; } + /// + /// For a game: whether the user can change what makes an output fire in it, through . + /// The outputs say what does in . + /// + public bool TriggersEditable { get; set; } + /// + /// For a game whose : 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. + /// + public IReadOnlyList GameOutputs { get; set; } = Array.Empty(); /// Creates the map of an field. /// The boards in display order. /// Shown when there are no boards. @@ -1159,4 +1192,29 @@ public interface IPluginOutputAssigner /// The result to show; is ignored. Task AddToyAsync(string provider, string output, string name, PluginChoiceRequest request, CancellationToken cancellation); } + + /// + /// Implemented, in addition to , by a provider whose map lets the user change, for one + /// game, what makes an output fire (), such as the shaker on a solenoid the + /// configuration doesn't use for it. + /// + /// + /// 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 apply. + /// + public interface IPluginOutputTriggerEditor + { + /// Sets what makes an output fire in the request's game, or gives it back to the configuration. + /// The field's . + /// The of the output. + /// + /// The trigger as the user typed or picked it, such as a ; empty for nothing to fire it + /// in this game; null for what the configuration says. + /// + /// The game, table and draft settings; is empty. + /// Cancelled when the page is closed. + /// The result to show; is ignored. + Task SetTriggerAsync(string provider, string output, string? trigger, PluginChoiceRequest request, CancellationToken cancellation); + } } diff --git a/VisualPinball.Plugins/Configuration/PluginSettingsSession.cs b/VisualPinball.Plugins/Configuration/PluginSettingsSession.cs index becd9abdd0..dd357bc8fb 100644 --- a/VisualPinball.Plugins/Configuration/PluginSettingsSession.cs +++ b/VisualPinball.Plugins/Configuration/PluginSettingsSession.cs @@ -241,6 +241,22 @@ public Task 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.")); /// + /// Sets what makes an output of an field fire in the session's game, or gives it back to + /// the configuration, through . Takes effect at once; the draft is + /// not involved. + /// + /// The field's . + /// The of the output. + /// + /// The trigger, surrounding spaces left out; empty for nothing to fire it in this game; null for what the configuration says. + /// + /// Passed to the provider. + /// The provider's result, or a failure outside a game or when the plugin cannot change triggers. + public Task 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.")); + /// /// 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. diff --git a/VisualPinball.Unity/VisualPinball.Unity/Packaging/RuntimePackageReader.cs b/VisualPinball.Unity/VisualPinball.Unity/Packaging/RuntimePackageReader.cs index b968ee5407..aaf6687dcc 100644 --- a/VisualPinball.Unity/VisualPinball.Unity/Packaging/RuntimePackageReader.cs +++ b/VisualPinball.Unity/VisualPinball.Unity/Packaging/RuntimePackageReader.cs @@ -721,7 +721,7 @@ await ReportProgressAndYieldAsync(progress, RuntimePackageLoadStage.RestoringGlo ReportProgress(progress, RuntimePackageLoadStage.RestoringGlobals, 1f, "Table wiring ready."); } - private static List ReadGlobalList(IPackageFolder folder, string fileName) + internal static List ReadGlobalList(IPackageFolder folder, string fileName) { if (!folder.TryGetFile(fileName, out var file, PackageApi.Packer.FileExtension)) { return new List(); diff --git a/VisualPinball.Unity/VisualPinball.Unity/Plugins/PackagedGameInformation.cs b/VisualPinball.Unity/VisualPinball.Unity/Plugins/PackagedGameInformation.cs index 93eb70c595..eed9727447 100644 --- a/VisualPinball.Unity/VisualPinball.Unity/Plugins/PackagedGameInformation.cs +++ b/VisualPinball.Unity/VisualPinball.Unity/Plugins/PackagedGameInformation.cs @@ -58,6 +58,32 @@ private sealed class Cached /// The readers found more than one distinct identity in the package. /// was cancelled. public static Task ReadAsync(string path, CancellationToken cancellation = default) => Task.Run(() => Read(path, cancellation), cancellation); + /// + /// 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. + /// + /// Path of the table package; a relative path is resolved against the current directory. + /// Cancels the read. + /// The mappings, empty lists for a package without any. Errors opening or reading the file fault the task. + /// was cancelled. + public static Task 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 Read(string file) + { + var list = RuntimePackageReader.ReadGlobalList(global, file); + cancellation.ThrowIfCancellationRequested(); + return list; + } + return new MappingConfig { + Switches = Read(PackageApi.SwitchesFile), + Coils = Read(PackageApi.CoilsFile), + Lamps = Read(PackageApi.LampsFile), + Wires = Read(PackageApi.WiresFile), + }; + }, cancellation); private static GameIdentity Read(string path, CancellationToken cancellation) { path = Path.GetFullPath(path);