diff --git a/VisualPinball.Engine.DOF.Test/DofInstanceTests.cs b/VisualPinball.Engine.DOF.Test/DofInstanceTests.cs index b5b7398..051ccc6 100644 --- a/VisualPinball.Engine.DOF.Test/DofInstanceTests.cs +++ b/VisualPinball.Engine.DOF.Test/DofInstanceTests.cs @@ -171,7 +171,7 @@ private void BoundedCleanup(DofInstance dof) [Test] public void ReportsAbiAndLibdofVersion() { - Assert.That(DofLibrary.NativeAbiVersion, Is.EqualTo(7)); + Assert.That(DofLibrary.NativeAbiVersion, Is.EqualTo(8)); Assert.That(DofLibrary.LibdofVersion, Does.Match(@"^\d+\.\d+\.\d+")); } @@ -448,14 +448,20 @@ public void FailedWriteIsReported() public void PortTestGoesThroughTheGate() { var unit = Unit("ArtNetA"); - using var config = new TestConfig().GlobalConfig().Cabinet(8, new Dictionary { [9] = "Matrix1" }, unit).Ini(1, "vpetest,S1"); + // port 10 names a toy (the equivalent itself, for want of another), as a cabinet names an LED strip + using var config = new TestConfig().GlobalConfig().Cabinet(8, new Dictionary { [9] = "Matrix1", [10] = "LedWizEquivalent 1" }, unit).Ini(1, "vpetest,S1"); using var dof = Start(config); + var device = dof.Describe().Devices.Single(); + Assert.That((device.Number, device.Count), Is.EqualTo((1, 10))); + Assert.That(Enumerable.Range(0, 12).Select(device.KindOf), Is.EqualTo(Enumerable.Repeat(DofPortKind.None, 1).Concat(Enumerable.Repeat(DofPortKind.Output, 8)) + .Concat(new[] { DofPortKind.None, DofPortKind.Toy, DofPortKind.None })), "what the test can set on each port"); Assert.That(dof.TestOutput(1, 6, 200), Is.EqualTo(DofStatus.Ok)); var shown = ExpectIndex(unit.Universe, f => f[5] == 200, "frame"); Assert.That(dof.TestOutput(1, 99, 255), Is.EqualTo(DofStatus.InvalidArgument), "unknown port"); Assert.That(dof.TestOutput(2, 1, 255), Is.EqualTo(DofStatus.InvalidArgument), "unknown device"); - Assert.That(dof.TestOutput(1, 9, 255), Is.EqualTo(DofStatus.InvalidArgument), "a port naming a toy"); + Assert.That(dof.TestOutput(1, 9, 255), Is.EqualTo(DofStatus.InvalidArgument), "a port naming nothing there is"); + Assert.That(dof.TestOutput(1, 10, 255), Is.EqualTo(DofStatus.InvalidArgument), "a port naming a toy"); var closed = dof.SetOutputGate(false); WaitSettled(dof, closed); diff --git a/VisualPinball.Engine.DOF.Unity/Runtime/Player/DofHardware.cs b/VisualPinball.Engine.DOF.Unity/Runtime/Player/DofHardware.cs index b6c95d2..c3f7a02 100644 --- a/VisualPinball.Engine.DOF.Unity/Runtime/Player/DofHardware.cs +++ b/VisualPinball.Engine.DOF.Unity/Runtime/Player/DofHardware.cs @@ -6,6 +6,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Linq; using System.Threading; using System.Threading.Tasks; using VisualPinball.Engine.DOF.Configuration; @@ -140,6 +141,11 @@ internal static (bool Success, string Message) Pulse(DofConfigStore store, Plugi + "are off, so nothing was turned on: a controller that is still connecting could run its own test."); } if (dark.State.Written == 0) { + // most likely the board of the device isn't there; DOF says so before the gate would open + var devices = Devices(native); + if (devices != null && !devices.Any(d => d.Number == device)) { + return (false, NotSettable(devices, dark.HasCabinet, device, port, output)); + } return (false, dark.State.Failed > 0 ? $"No controller can send the pulse: {Units(dark.Units, "every controller")} failed, so nothing was turned on." : "No controller took part, so nothing was turned on."); @@ -147,7 +153,7 @@ internal static (bool Success, string Message) Pulse(DofConfigStore store, Plugi var status = native.TestOutput(device, port, 255); if (status != DofStatus.Ok) { return (false, status == DofStatus.InvalidArgument - ? $"Device {device} has no output {output} DOF can set (it may be part of a toy, such as an RGB LED)." + ? NotSettable(Devices(native), dark.HasCabinet, device, port, output) : $"DOF could not set output {output} of device {device} ({status})."); } if (cancellation.IsCancellationRequested) { @@ -178,6 +184,58 @@ internal static (bool Success, string Message) Pulse(DofConfigStore store, Plugi private const string Cancelled = "Cancelled; nothing was turned on."; + private static IReadOnlyList Devices(IDofTestNative native) + { + try { + return native.Devices(); + } catch (Exception e) when (e is DofException || e is ObjectDisposedException || e is EntryPointNotFoundException) { + return null; + } + } + + /// + /// Why DOF can't set a port: it has no such device, the device has fewer ports, or the port drives a toy or nothing. + /// + /// The devices DOF set up; null if unknown. + /// Whether the configuration has a cabinet file, which sets up the devices. + /// The output the port drives, as the page shows it. + internal static string NotSettable(IReadOnlyList devices, bool hasCabinet, int device, int port, int output) + { + var what = output == port ? $"output {output}" : $"port {port} (wired to output {output})"; + if (devices == null) { + return $"Device {device} has no {what} DOF can set."; + } + DofDevicePorts? ports = null; + foreach (var d in devices) { + if (d.Number == device) { + ports = d; + break; + } + } + if (ports == null) { + // a cabinet file can let DOF set up the boards it finds as well + var setup = hasCabinet + ? $"the cabinet file sets up no device {device}, and DOF set up none for a board it found{BoardHint(device)}" + : $"it found no board that is device {device}{BoardHint(device)}, and without a cabinet file it sets up only the boards it finds"; + return $"DOF has no device {device}: {setup}. A game can't fire its outputs either."; + } + // beyond what DOF describes + if (ports.Value.Truncated && port > ports.Value.Count) { + return $"Device {device} has no {what} DOF can set."; + } + if (port > ports.Value.Count) { + return $"Device {device} goes up to {(output == port ? "output" : "port")} {ports.Value.Count}, so there is no {what}. A game can't fire it either."; + } + return ports.Value.KindOf(port) == DofPortKind.Toy + ? $"On device {device}, {what} names a toy, such as an LED strip, which a test can't turn on directly." + : $"On device {device}, {what} is connected to no controller output. A game can't fire it either."; + } + + // Which board a device number stands for, where DOF numbers a board it finds by a setting of the board. + private static string BoardHint(int device) => device >= 51 && device <= 66 + ? $" (a Pinscape is device 50 plus its unit number, so this is unit {device - 50})" + : ""; + /// /// Opens DOF dark, waits until every unit confirmed it, and finishes DOF, which turns every output off, such as one /// a crashed game left on. It succeeds when controllers sent their outputs off and none failed, did not confirm or @@ -225,13 +283,16 @@ internal readonly struct Dark public readonly DofGateState State; public readonly IReadOnlyList Units; public readonly IReadOnlyList Missing; + /// Whether the configuration has a cabinet file, which sets up the devices; without one DOF sets up the boards it finds. + public readonly bool HasCabinet; - public Dark(bool applied, DofGateState state, IReadOnlyList units, IReadOnlyList missing) + public Dark(bool applied, DofGateState state, IReadOnlyList units, IReadOnlyList missing, bool hasCabinet) { Applied = applied; State = state; Units = units ?? Array.Empty(); Missing = missing ?? Array.Empty(); + HasCabinet = hasCabinet; } } @@ -292,7 +353,7 @@ private static Dark ConfirmDark(IDofTestNative native, DofCatalog catalog, TimeS var missing = NotStarted(native, catalog); var generation = native.SetOutputGate(false); if (generation < 0) { - return new Dark(false, default, Array.Empty(), missing); + return new Dark(false, default, Array.Empty(), missing, catalog.HasCabinet); } var watch = Stopwatch.StartNew(); var state = native.GetGateState(generation); @@ -300,7 +361,7 @@ private static Dark ConfirmDark(IDofTestNative native, DofCatalog catalog, TimeS state = native.GetGateState(generation); } var units = state.Settled && state.Failed == 0 ? Array.Empty() : native.GetGatePending(generation); - return new Dark(true, state, units, missing); + return new Dark(true, state, units, missing, catalog.HasCabinet); } /// @@ -366,6 +427,8 @@ internal interface IDofTestNative : IDisposable void Init(string tableFile, string romName); /// The controllers DOF started (), after init. IReadOnlyList Controllers(); + /// The ini devices DOF set up and what their ports drive (), after init. + IReadOnlyList Devices(); long SetOutputGate(bool open); DofGateState GetGateState(long generation); IReadOnlyList GetGatePending(long generation); @@ -382,6 +445,7 @@ internal sealed class DofTestInstance : IDofTestNative public void SetOutputMap(int device, IReadOnlyList targets) => _instance.SetOutputMap(device, targets); public void Init(string tableFile, string romName) => _instance.Init(tableFile, romName); public IReadOnlyList Controllers() => _instance.Describe().Controllers; + public IReadOnlyList Devices() => _instance.Describe().Devices; public long SetOutputGate(bool open) => _instance.SetOutputGate(open); public DofGateState GetGateState(long generation) => _instance.GetGateState(generation); public IReadOnlyList GetGatePending(long generation) => _instance.GetGatePending(generation); diff --git a/VisualPinball.Engine.DOF.Unity/Tests/DofPluginTests.cs b/VisualPinball.Engine.DOF.Unity/Tests/DofPluginTests.cs index e18b579..bcd3b9b 100644 --- a/VisualPinball.Engine.DOF.Unity/Tests/DofPluginTests.cs +++ b/VisualPinball.Engine.DOF.Unity/Tests/DofPluginTests.cs @@ -283,6 +283,38 @@ public void TestsAPortOutsideAGame() } } + [Test] + public void SaysWhyATestCantSetAPort() + { + var native = new RecordingTestNative { Ports = new[] { new DofDevicePorts(1, "ooooot-") } }; + var host = Host(); + host.Factory.TestNativeFactory = _ => native; + host.Factory.PulseDuration = TimeSpan.FromMilliseconds(20); + host.Import(371, "vpetest,S1", null, "\"Rom\",\"Flasher\""); + Assert.That(Pulse(host, "1:1").Success, Is.True); + Assert.That(Pulse(host, "1:6").Message, Is.EqualTo("On device 1, output 6 names a toy, such as an LED strip, which a test can't turn on directly.")); + Assert.That(Pulse(host, "1:7").Message, Is.EqualTo("On device 1, output 7 is connected to no controller output. A game can't fire it either.")); + Assert.That(Pulse(host, "1:9").Message, Is.EqualTo("Device 1 goes up to output 7, so there is no output 9. A game can't fire it either.")); + native.Ports = new DofDevicePorts[0]; + Assert.That(Pulse(host, "1:1").Message, Is.EqualTo("DOF has no device 1: the cabinet file sets up no device 1, and DOF set up none for a board it found. A game can't fire its outputs either.")); + // a board that isn't there takes no part: that is what is said, not that no controller did + native.Units = new string[0]; + native.Started = new DofController[0]; + Assert.That(Pulse(host, "1:1").Message, Is.EqualTo("DOF has no device 1: the cabinet file sets up no device 1, and DOF set up none for a board it found. A game can't fire its outputs either.")); + native.Ports = new[] { new DofDevicePorts(1, "o") }; + Assert.That(Pulse(host, "1:1").Message, Is.EqualTo("No controller took part, so nothing was turned on.")); + + // without a cabinet file, DOF sets up the boards it finds + Assert.That(DofPortTest.NotSettable(new DofDevicePorts[0], false, 51, 22, 22), Is.EqualTo("DOF has no device 51: it found no board that is device 51 " + + "(a Pinscape is device 50 plus its unit number, so this is unit 1), and without a cabinet file it sets up only the boards it finds. A game can't fire its outputs either.")); + Assert.That(DofPortTest.NotSettable(new DofDevicePorts[0], false, 30, 1, 1), Is.EqualTo("DOF has no device 30: it found no board that is device 30, " + + "and without a cabinet file it sets up only the boards it finds. A game can't fire its outputs either.")); + Assert.That(DofPortTest.NotSettable(new[] { new DofDevicePorts(51, new string('o', 16)) }, false, 51, 22, 5), + Is.EqualTo("Device 51 goes up to port 16, so there is no port 22 (wired to output 5). A game can't fire it either.")); + Assert.That(DofPortTest.NotSettable(null, false, 51, 22, 22), Is.EqualTo("Device 51 has no output 22 DOF can set."), "DOF couldn't say"); + Assert.That(DofPortTest.NotSettable(new[] { new DofDevicePorts(51, "ooooooo+") }, false, 51, 8, 8), Is.EqualTo("Device 51 has no output 8 DOF can set."), "nor past what it describes"); + } + [Test] public void TheOutputMapShowsRgbToysAsTheirChannels() { @@ -1012,6 +1044,9 @@ internal sealed class RecordingTestNative : IDofTestNative public void SetOutputMap(int device, IReadOnlyList targets) => Calls.Add($"map:{device}:{string.Join(",", targets)}"); public void Init(string tableFile, string romName) => Calls.Add("init"); public IReadOnlyList Controllers() => Started; + /// The devices DOF set up; a test of a port that none of them can set is refused. + public DofDevicePorts[] Ports; + public IReadOnlyList Devices() => Ports; public long SetOutputGate(bool open) { Calls.Add(open ? "gate:open" : "gate:closed"); @@ -1039,7 +1074,15 @@ public IReadOnlyList GetGatePending(long generation) public DofStatus TestOutput(int device, int port, int value) { Calls.Add($"{device}:{port}={value}"); - return DofStatus.Ok; + if (Ports == null) { + return DofStatus.Ok; + } + foreach (var d in Ports) { + if (d.Number == device && d.KindOf(port) == DofPortKind.Output) { + return DofStatus.Ok; + } + } + return DofStatus.InvalidArgument; } public void Finish() => Calls.Add("finish"); public void Dispose() => Calls.Add("dispose"); diff --git a/VisualPinball.Engine.DOF/DofInstance.cs b/VisualPinball.Engine.DOF/DofInstance.cs index f6ad8be..8e6a5d9 100644 --- a/VisualPinball.Engine.DOF/DofInstance.cs +++ b/VisualPinball.Engine.DOF/DofInstance.cs @@ -84,6 +84,53 @@ public DofController(string type, string name, int outputs) } } + /// What a port of a device drives (). + public enum DofPortKind + { + /// Nothing: the device has no such port, or the port names no output of a controller. + None, + /// An output of a controller, which sets. + Output, + /// A toy, such as an LED strip, which only its effects set. + Toy, + } + + /// The ports of an ini device: a LedWiz equivalent of the cabinet, by its number. + public readonly struct DofDevicePorts + { + private readonly string _ports; + + /// The ini device, N of directoutputconfigN.ini. + public int Number { get; } + + /// The highest port described: the device's highest, unless . + public int Count { get; } + + /// Whether the device has ports beyond the ones described. + public bool Truncated { get; } + + /// A character per port from 1 (vpe_dof_describe), and a '+' if the device has more. + public DofDevicePorts(int number, string ports) + { + Number = number; + _ports = ports ?? string.Empty; + Truncated = _ports.EndsWith("+", StringComparison.Ordinal); + Count = Truncated ? _ports.Length - 1 : _ports.Length; + } + + public DofPortKind KindOf(int port) + { + if (port < 1 || port > Count) { + return DofPortKind.None; + } + switch (_ports[port - 1]) { + case 'o': return DofPortKind.Output; + case 't': return DofPortKind.Toy; + default: return DofPortKind.None; + } + } + } + public readonly struct DofToy { /// libdof's toy class (LedWizEquivalent, LedStrip, RGBAToy, ...). @@ -102,11 +149,14 @@ public sealed class DofCabinetDescription { public IReadOnlyList Controllers { get; } public IReadOnlyList Toys { get; } + /// The ini devices the cabinet has, which sets. + public IReadOnlyList Devices { get; } - internal DofCabinetDescription(IReadOnlyList controllers, IReadOnlyList toys) + internal DofCabinetDescription(IReadOnlyList controllers, IReadOnlyList toys, IReadOnlyList devices = null) { Controllers = controllers; Toys = toys; + Devices = devices ?? Array.Empty(); } } @@ -418,15 +468,18 @@ public DofCabinetDescription Describe() DofException.ThrowIfFailed(status, "Describing the DOF cabinet"); var controllers = new List(); var toys = new List(); + var devices = new List(); foreach (var line in lines) { var f = line.Split('|'); if (f[0] == "controller" && f.Length >= 4) { controllers.Add(new DofController(f[1], f[2], int.TryParse(f[3], out var n) ? n : 0)); } else if (f[0] == "toy" && f.Length >= 3) { toys.Add(new DofToy(f[1], f[2])); + } else if (f[0] == "device" && f.Length >= 3 && int.TryParse(f[1], out var number)) { + devices.Add(new DofDevicePorts(number, f[2])); } } - return new DofCabinetDescription(controllers, toys); + return new DofCabinetDescription(controllers, toys, devices); } /// diff --git a/VisualPinball.Engine.DOF/Native/DofNative.cs b/VisualPinball.Engine.DOF/Native/DofNative.cs index 4b8e4ba..eb2aff2 100644 --- a/VisualPinball.Engine.DOF/Native/DofNative.cs +++ b/VisualPinball.Engine.DOF/Native/DofNative.cs @@ -15,7 +15,7 @@ namespace VisualPinball.Engine.DOF.Native internal static class DofNative { public const string Library = "VpeDof"; - public const int AbiVersion = 7; + public const int AbiVersion = 8; [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate void LogCallback(int level, IntPtr message, IntPtr user); diff --git a/native/include/vpe_dof.h b/native/include/vpe_dof.h index b8f0879..34b0c18 100644 --- a/native/include/vpe_dof.h +++ b/native/include/vpe_dof.h @@ -36,7 +36,7 @@ extern "C" { #endif /// Version of this ABI. Bumped on every incompatible change; the managed binding refuses a library with another one. -#define VPE_DOF_ABI_VERSION 7 +#define VPE_DOF_ABI_VERSION 8 enum vpe_dof_status { @@ -159,7 +159,10 @@ VPE_DOF_API int32_t VPE_DOF_CALL vpe_dof_test_output(vpe_dof* dof, int32_t devic /// VPE_DOF_INVALID_STATE after init. VPE_DOF_API int32_t VPE_DOF_CALL vpe_dof_set_output_map(vpe_dof* dof, int32_t device, const int32_t* targets, int32_t count); -/// After init: lists "controller|||" and "toy||" lines describing the cabinet. +/// After init: lists "controller|||" and "toy||" lines describing the cabinet, and a +/// "device||" line for each LedWiz equivalent, which vpe_dof_test_output sets: a character per port from 1, +/// 'o' for one that drives a controller output, 't' for one that names a toy, such as an LED strip, and '-' for one that +/// drives nothing; up to port 4096, followed by a '+' if the device has ports beyond. VPE_DOF_API int32_t VPE_DOF_CALL vpe_dof_describe(vpe_dof* dof, vpe_dof_string_fn callback, void* user); /// Lists the table rows of the ini files libdof finds under `base_path` for `table_file` as "|" lines, diff --git a/native/src/vpe_dof.cpp b/native/src/vpe_dof.cpp index 642533e..4b6d506 100644 --- a/native/src/vpe_dof.cpp +++ b/native/src/vpe_dof.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -806,6 +807,50 @@ VPE_DOF_API int32_t VPE_DOF_CALL vpe_dof_describe(vpe_dof* dof, vpe_dof_string_f std::string line = "toy|" + Sanitize(TypeName(*toy)) + "|" + Sanitize(toy->GetName()); callback(line.c_str(), user); } + // What vpe_dof_test_output finds on each device: the first equivalent of a number, and the first output of a port + std::set described; + for (DOF::IToy* toy : *toys) + { + auto* lwe = dynamic_cast(toy); + if (!lwe || !described.insert(lwe->GetLedWizNumber()).second) + continue; + std::string ports; + std::vector seen; + bool more = false; + for (const DOF::LedWizEquivalentOutput* output : lwe->GetOutputs()) + { + const int number = output ? output->GetLedWizEquivalentOutputNumber() : 0; + more |= number > 4096; + if (number < 1 || number > 4096) + continue; + if (static_cast(ports.size()) < number) + { + ports.resize(number, '-'); + seen.resize(number, false); + } + if (seen[number - 1]) + continue; + seen[number - 1] = true; + char kind = '-'; + if (output->GetOutput()) + kind = 'o'; + else if (!output->GetOutputName().empty()) + { + for (DOF::IToy* other : *toys) + { + if (other && other->GetName() == output->GetOutputName()) + { + kind = 't'; + break; + } + } + } + ports[number - 1] = kind; + } + // a '+' after the ports: the device has ports beyond them + std::string line = "device|" + std::to_string(lwe->GetLedWizNumber()) + "|" + ports + (more ? "+" : ""); + callback(line.c_str(), user); + } } return VPE_DOF_OK; }