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
2 changes: 1 addition & 1 deletion 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(6));
Assert.That(DofLibrary.NativeAbiVersion, Is.EqualTo(7));
Assert.That(DofLibrary.LibdofVersion, Does.Match(@"^\d+\.\d+\.\d+"));
}

Expand Down
96 changes: 94 additions & 2 deletions VisualPinball.Engine.DOF.Test/DofOutputWiringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,102 @@ public void SavesAndLoads()
public void LeavesOutLinesThatAreNoPermutation()
{
Directory.CreateDirectory(_root);
File.WriteAllText(Path.Combine(_root, DofOutputWiring.FileName), "1=2,1\n2=1,1\n3=3,1\nx=1\n0=1\n4=\n5=2,1,3\n6=2,,1\n");
File.WriteAllText(Path.Combine(_root, DofOutputWiring.FileName), "1=2,1\n2=1,1\n3=3,1\nx=1\n0=1\n4=\n5=2,1,3\n6=2,,1\n7=0,1\n8=-1,1\n9=0,0\n");
var loaded = DofOutputWiring.Load(_root);
Assert.That(loaded.Devices, Is.EqualTo(new[] { 1, 5 }));
Assert.That(loaded.Devices, Is.EqualTo(new[] { 1, 5, 7, 9 }));
Assert.That(loaded.MapOf(5), Is.EqualTo(new[] { 2, 1 }), "trailing ports that keep their own are left out");
Assert.That(loaded.MapOf(7), Is.EqualTo(new[] { -1, 1 }), "0 is a port that drives nothing, which the native layer takes as -1");
Assert.That(loaded.PortOn(9, 2), Is.EqualTo(DofOutputWiring.Nothing), "two ports may drive nothing");
}

[Test]
public void AnOutputWhosePortMovedAwayDrivesNothing()
{
var wiring = DofOutputWiring.None.Remove(1, new[] { 3 });
Assert.That(wiring.OutputOf(1, 3), Is.EqualTo(DofOutputWiring.Nothing));
Assert.That(wiring.PortOn(1, 3), Is.EqualTo(DofOutputWiring.Nothing));
Assert.That(wiring.PortOn(1, 4), Is.EqualTo(4), "past the map, every port drives its own");
Assert.That(wiring.Extent(1), Is.EqualTo(3), "the output stays on the board, to be given a toy again");

// swapping an output nothing drives leaves the other one driven by nothing
wiring = wiring.Swap(1, 3, 7, 1);
Assert.That(wiring.OutputOf(1, 7), Is.EqualTo(3));
Assert.That(wiring.PortOn(1, 7), Is.EqualTo(DofOutputWiring.Nothing));
Assert.That(wiring.Extent(1), Is.EqualTo(7));
}

[Test]
public void AssigningSwapsWithWhereTheToyWas()
{
// a toy on port 16 goes to output 14; what 14 had goes to 16
var wiring = DofOutputWiring.None.Assign(1, new[] { 16 }, 14);
Assert.That(wiring.OutputOf(1, 16), Is.EqualTo(14));
Assert.That(wiring.OutputOf(1, 14), Is.EqualTo(16));

// an RGB toy one output further: the output it leaves takes what it moved onto
wiring = DofOutputWiring.None.Assign(1, new[] { 3, 4, 5 }, 4);
Assert.That(Enumerable.Range(3, 3).Select(p => wiring.OutputOf(1, p)), Is.EqualTo(new[] { 4, 5, 6 }));
Assert.That(wiring.OutputOf(1, 6), Is.EqualTo(3));
}

[Test]
public void AssigningAToyOnNoOutputLeavesWhatItDisplacesOnNone()
{
var wiring = DofOutputWiring.None.Remove(1, new[] { 5 }).Assign(1, new[] { 5 }, 2);
Assert.That(wiring.OutputOf(1, 5), Is.EqualTo(2));
Assert.That(wiring.OutputOf(1, 2), Is.EqualTo(DofOutputWiring.Nothing));
Assert.That(wiring.PortOn(1, 5), Is.EqualTo(DofOutputWiring.Nothing), "output 5 lost its port");

// a new toy on a port past the board: what output 2 had drives nothing, the board is no longer for it
wiring = DofOutputWiring.None.Assign(1, new[] { 40 }, 2, true);
Assert.That(wiring.OutputOf(1, 40), Is.EqualTo(2));
Assert.That(wiring.OutputOf(1, 2), Is.EqualTo(DofOutputWiring.Nothing));
Assert.That(wiring.Extent(1), Is.EqualTo(40), "the ports in between keep their own; the output the port left stays on the board");
Assert.That(wiring.PortOn(1, 33), Is.EqualTo(33));
Assert.Throws<ArgumentOutOfRangeException>(() => wiring.Assign(1, new[] { 2, 2 }, 1), "a port twice");
}

[Test]
public void SavesAndLoadsPortsThatDriveNothing()
{
var wiring = DofOutputWiring.None.Assign(1, new[] { 40 }, 2, true).Remove(1, new[] { 7 });
wiring.Save(_root);
var loaded = DofOutputWiring.Load(_root);
Assert.That(loaded.MapOf(1), Is.EqualTo(wiring.MapOf(1)));
Assert.That(loaded.OutputOf(1, 2), Is.EqualTo(DofOutputWiring.Nothing));
Assert.That(loaded.OutputOf(1, 40), Is.EqualTo(2));
}

[Test]
public void KeepsTheToysTheUserNamed()
{
var names = DofToyNames.None.With(1, 40, " Gear\nmotor ").With(2, 33, "Fan");
Assert.That(names.NameOf(1, 40), Is.EqualTo("Gear motor"), "on one line, without the spaces around it");
names.Save(_root);
var loaded = DofToyNames.Load(_root);
Assert.That(loaded.NameOf(1, 40), Is.EqualTo("Gear motor"));
Assert.That(loaded.PortsOf(2), Is.EqualTo(new[] { 33 }));
loaded.With(1, 40, " ").With(2, 33, "").Save(_root);
Assert.That(File.Exists(Path.Combine(_root, DofToyNames.FileName)), Is.False, "no file without a name");
Assert.That(DofToyNames.Clean(new string('x', 100)).Length, Is.EqualTo(DofToyNames.MaxLength));

// cut before a character of two UTF-16 units, not inside it, so the name saved is the name shown
var target = new string('x', DofToyNames.MaxLength - 1) + char.ConvertFromUtf32(0x1F3AF);
Assert.That(DofToyNames.Clean(target), Is.EqualTo(new string('x', DofToyNames.MaxLength - 1)));
DofToyNames.None.With(1, 40, target).Save(_root);
Assert.That(DofToyNames.Load(_root).NameOf(1, 40), Is.EqualTo(DofToyNames.None.With(1, 40, target).NameOf(1, 40)));
}

[Test]
public void KeepsAnOutputNothingDrivesOnTheBoard()
{
// swapped, then the output the port moved to cleared: that output stays on the board to be given a toy again
var wiring = DofOutputWiring.None.Swap(17, 2, 60, 1).Remove(17, new[] { 2 });
Assert.That(wiring.PortOn(17, 60), Is.EqualTo(DofOutputWiring.Nothing));
Assert.That(wiring.Extent(17), Is.EqualTo(60));
// a toy the user added is on a port far past the board's: only the output it is on counts
var added = DofOutputWiring.None.Assign(17, new[] { DofToyNames.FirstPort }, 5, true);
Assert.That(added.Extent(17), Is.EqualTo(5));
}
}
}
7 changes: 5 additions & 2 deletions VisualPinball.Engine.DOF.Unity/Runtime/Player/DofHardware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,16 @@ internal static class DofPortTest
/// <summary>How long the controller units have to confirm that their outputs are off, once DOF has started.</summary>
internal static readonly TimeSpan DefaultConfirmTimeout = TimeSpan.FromSeconds(3);

/// <summary>An output to test: the device (N of directoutputconfigN.ini) and its port, from 1.</summary>
/// <summary>
/// An output to test: the device (N of directoutputconfigN.ini) and its port, from 1; or the number of an output nothing
/// drives, as a negative port.
/// </summary>
internal static bool TryParse(string id, out int device, out int port)
{
device = port = 0;
var separator = id?.IndexOf(':') ?? -1;
return separator > 0 && int.TryParse(id.Substring(0, separator), out device) && int.TryParse(id.Substring(separator + 1), out port)
&& device > 0 && port > 0;
&& device > 0 && port != 0;
}

internal static string Id(int device, int port) => device + ":" + port;
Expand Down
Loading
Loading