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
12 changes: 9 additions & 3 deletions VisualPinball.Engine.DOF.Test/DofInstanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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+"));
}

Expand Down Expand Up @@ -448,14 +448,20 @@ public void FailedWriteIsReported()
public void PortTestGoesThroughTheGate()
{
var unit = Unit("ArtNetA");
using var config = new TestConfig().GlobalConfig().Cabinet(8, new Dictionary<int, string> { [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<int, string> { [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);
Expand Down
72 changes: 68 additions & 4 deletions VisualPinball.Engine.DOF.Unity/Runtime/Player/DofHardware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -140,14 +141,19 @@ 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.");
}
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) {
Expand Down Expand Up @@ -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<DofDevicePorts> Devices(IDofTestNative native)
{
try {
return native.Devices();
} catch (Exception e) when (e is DofException || e is ObjectDisposedException || e is EntryPointNotFoundException) {
return null;
}
}

/// <summary>
/// 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.
/// </summary>
/// <param name="devices">The devices DOF set up; null if unknown.</param>
/// <param name="hasCabinet">Whether the configuration has a cabinet file, which sets up the devices.</param>
/// <param name="output">The output the port drives, as the page shows it.</param>
internal static string NotSettable(IReadOnlyList<DofDevicePorts> 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})"
: "";

/// <summary>
/// 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
Expand Down Expand Up @@ -225,13 +283,16 @@ internal readonly struct Dark
public readonly DofGateState State;
public readonly IReadOnlyList<string> Units;
public readonly IReadOnlyList<string> Missing;
/// <summary>Whether the configuration has a cabinet file, which sets up the devices; without one DOF sets up the boards it finds.</summary>
public readonly bool HasCabinet;

public Dark(bool applied, DofGateState state, IReadOnlyList<string> units, IReadOnlyList<string> missing)
public Dark(bool applied, DofGateState state, IReadOnlyList<string> units, IReadOnlyList<string> missing, bool hasCabinet)
{
Applied = applied;
State = state;
Units = units ?? Array.Empty<string>();
Missing = missing ?? Array.Empty<string>();
HasCabinet = hasCabinet;
}
}

Expand Down Expand Up @@ -292,15 +353,15 @@ 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<string>(), missing);
return new Dark(false, default, Array.Empty<string>(), missing, catalog.HasCabinet);
}
var watch = Stopwatch.StartNew();
var state = native.GetGateState(generation);
while (!state.Settled && watch.Elapsed < timeout && !cancellation.WaitHandle.WaitOne(5)) {
state = native.GetGateState(generation);
}
var units = state.Settled && state.Failed == 0 ? Array.Empty<string>() : native.GetGatePending(generation);
return new Dark(true, state, units, missing);
return new Dark(true, state, units, missing, catalog.HasCabinet);
}

/// <summary>
Expand Down Expand Up @@ -366,6 +427,8 @@ internal interface IDofTestNative : IDisposable
void Init(string tableFile, string romName);
/// <summary>The controllers DOF started (<see cref="DofInstance.Describe"/>), after init.</summary>
IReadOnlyList<DofController> Controllers();
/// <summary>The ini devices DOF set up and what their ports drive (<see cref="DofInstance.Describe"/>), after init.</summary>
IReadOnlyList<DofDevicePorts> Devices();
long SetOutputGate(bool open);
DofGateState GetGateState(long generation);
IReadOnlyList<string> GetGatePending(long generation);
Expand All @@ -382,6 +445,7 @@ internal sealed class DofTestInstance : IDofTestNative
public void SetOutputMap(int device, IReadOnlyList<int> targets) => _instance.SetOutputMap(device, targets);
public void Init(string tableFile, string romName) => _instance.Init(tableFile, romName);
public IReadOnlyList<DofController> Controllers() => _instance.Describe().Controllers;
public IReadOnlyList<DofDevicePorts> Devices() => _instance.Describe().Devices;
public long SetOutputGate(bool open) => _instance.SetOutputGate(open);
public DofGateState GetGateState(long generation) => _instance.GetGateState(generation);
public IReadOnlyList<string> GetGatePending(long generation) => _instance.GetGatePending(generation);
Expand Down
45 changes: 44 additions & 1 deletion VisualPinball.Engine.DOF.Unity/Tests/DofPluginTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -1012,6 +1044,9 @@ internal sealed class RecordingTestNative : IDofTestNative
public void SetOutputMap(int device, IReadOnlyList<int> targets) => Calls.Add($"map:{device}:{string.Join(",", targets)}");
public void Init(string tableFile, string romName) => Calls.Add("init");
public IReadOnlyList<DofController> Controllers() => Started;
/// <summary>The devices DOF set up; a test of a port that none of them can set is refused.</summary>
public DofDevicePorts[] Ports;
public IReadOnlyList<DofDevicePorts> Devices() => Ports;
public long SetOutputGate(bool open)
{
Calls.Add(open ? "gate:open" : "gate:closed");
Expand Down Expand Up @@ -1039,7 +1074,15 @@ public IReadOnlyList<string> 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");
Expand Down
57 changes: 55 additions & 2 deletions VisualPinball.Engine.DOF/DofInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,53 @@ public DofController(string type, string name, int outputs)
}
}

/// <summary>What a port of a device drives (<see cref="DofDevicePorts"/>).</summary>
public enum DofPortKind
{
/// <summary>Nothing: the device has no such port, or the port names no output of a controller.</summary>
None,
/// <summary>An output of a controller, which <see cref="DofInstance.TestOutput"/> sets.</summary>
Output,
/// <summary>A toy, such as an LED strip, which only its effects set.</summary>
Toy,
}

/// <summary>The ports of an ini device: a LedWiz equivalent of the cabinet, by its number.</summary>
public readonly struct DofDevicePorts
{
private readonly string _ports;

/// <summary>The ini device, N of directoutputconfigN.ini.</summary>
public int Number { get; }

/// <summary>The highest port described: the device's highest, unless <see cref="Truncated"/>.</summary>
public int Count { get; }

/// <summary>Whether the device has ports beyond the ones described.</summary>
public bool Truncated { get; }

/// <param name="ports">A character per port from 1 (vpe_dof_describe), and a '+' if the device has more.</param>
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
{
/// <summary>libdof's toy class (LedWizEquivalent, LedStrip, RGBAToy, ...).</summary>
Expand All @@ -102,11 +149,14 @@ public sealed class DofCabinetDescription
{
public IReadOnlyList<DofController> Controllers { get; }
public IReadOnlyList<DofToy> Toys { get; }
/// <summary>The ini devices the cabinet has, which <see cref="DofInstance.TestOutput"/> sets.</summary>
public IReadOnlyList<DofDevicePorts> Devices { get; }

internal DofCabinetDescription(IReadOnlyList<DofController> controllers, IReadOnlyList<DofToy> toys)
internal DofCabinetDescription(IReadOnlyList<DofController> controllers, IReadOnlyList<DofToy> toys, IReadOnlyList<DofDevicePorts> devices = null)
{
Controllers = controllers;
Toys = toys;
Devices = devices ?? Array.Empty<DofDevicePorts>();
}
}

Expand Down Expand Up @@ -418,15 +468,18 @@ public DofCabinetDescription Describe()
DofException.ThrowIfFailed(status, "Describing the DOF cabinet");
var controllers = new List<DofController>();
var toys = new List<DofToy>();
var devices = new List<DofDevicePorts>();
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);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion VisualPinball.Engine.DOF/Native/DofNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions native/include/vpe_dof.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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|<type>|<name>|<outputs>" and "toy|<type>|<name>" lines describing the cabinet.
/// After init: lists "controller|<type>|<name>|<outputs>" and "toy|<type>|<name>" lines describing the cabinet, and a
/// "device|<number>|<ports>" 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 "<device>|<rom>" lines,
Expand Down
Loading
Loading