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
149 changes: 149 additions & 0 deletions VisualPinball.Engine.DOF.Test/DofGameTriggersTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Visual Pinball Engine
// Copyright (C) 2026 freezy and VPE Team
//
// SPDX-License-Identifier: GPL-3.0-or-later

using System;
using System.IO;
using NUnit.Framework;
using VisualPinball.Engine.DOF.Configuration;

namespace VisualPinball.Engine.DOF.Test
{
public class DofGameTriggersTests
{
// Combo1 and Combo2 are RGB toys on 1-3 and 4-6, the knocker is on 7, as the Config Tool writes them.
private const string Ini = "[version]\r\nversion=5302\r\n\r\n[Colors DOF]\r\nRed=#FF0000FF\r\n\r\n[Variables DOF]\r\n@flash@=W30 500\r\n@wide@=shape Red\r\n\r\n"
+ "[Config DOF]\r\n# \"Rom\",\"Combo1\",\"\",\"\",\"Combo2\",\"\",\"\",\"Knocker\"\r\n"
+ "afm,E101 Red,0,0,0,S5\r\n"
+ "t2,0,0,0,L12 Blue/S3 Red,@flash@\r\n"
+ "afm,S9\r\n"
+ "fh,S1\r\n";

private string _root;

[SetUp]
public void SetUp() => _root = Path.Combine(Path.GetTempPath(), "vpe-dof-triggers-" + Guid.NewGuid().ToString("N"));

[TearDown]
public void TearDown()
{
if (Directory.Exists(_root)) {
Directory.Delete(_root, true);
}
}

[Test]
public void ReadsWhatFiresEachToyOfARow()
{
var afm = DofIniRow.Parse(Ini, "AFM");
Assert.That(afm.Rom, Is.EqualTo("afm"), "the first row of a ROM, as libdof keeps it");
Assert.That(afm.SettingOf(1, 3), Is.EqualTo("E101 Red"));
Assert.That(afm.SettingOf(4, 3), Is.EqualTo(""), "three 0s for an RGB toy without effects");
Assert.That(afm.SettingOf(7, 1), Is.EqualTo("S5"));
Assert.That(afm.SettingOf(8, 1), Is.EqualTo(""), "past the row");
Assert.That(afm.SettingOf(2, 1), Is.Null, "inside the colour column of another toy");

var t2 = DofIniRow.Parse(Ini, "t2");
Assert.That(t2.SettingOf(4, 3), Is.EqualTo("L12 Blue/S3 Red"));
Assert.That(t2.SettingOf(7, 1), Is.EqualTo("@flash@"), "as written, its variable kept");
Assert.That(t2.Extent, Is.EqualTo(7));
Assert.That(DofIniRow.Parse(Ini, "gnr"), Is.Null);
}

[Test]
public void ChangesAToyAndKeepsTheOthersOnTheirPorts()
{
var afm = DofIniRow.Parse(Ini, "afm");
var combo2 = afm.With(4, 3, "S28 Red", out var problem);
Assert.That(problem, Is.Null);
Assert.That(combo2.Line, Is.EqualTo("afm,E101 Red,S28 Red,S5"), "one colour column for the three 0s");
Assert.That(combo2.SettingOf(7, 1), Is.EqualTo("S5"), "the knocker stays on its port");

var cleared = combo2.With(1, 3, "", out problem);
Assert.That(cleared.Line, Is.EqualTo("afm,0,0,0,S28 Red,S5"), "nothing takes the RGB toy's three ports");
Assert.That(cleared.SettingOf(4, 3), Is.EqualTo("S28 Red"));

var knocker = afm.With(7, 1, "W25 500", out problem);
Assert.That(knocker.Line, Is.EqualTo("afm,E101 Red,0,0,0,W25 500"));

var added = afm.With(10, 1, "S40", out problem);
Assert.That(added.Line, Is.EqualTo("afm,E101 Red,0,0,0,S5,0,0,S40"), "0s up to a toy past the row");
Assert.That(added.SettingOf(10, 1), Is.EqualTo("S40"));
Assert.That(afm.With(10, 1, "", out problem), Is.SameAs(afm), "nothing past the row is nothing to write");

Assert.That(afm.With(2, 1, "S1", out problem), Is.Null);
Assert.That(problem, Does.Contain("colour"));
Assert.That(afm.With(10, 1, "S40 Red", out problem), Is.Null, "past the row too, a colour would drive the ports after the toy");
Assert.That(problem, Does.Contain("takes one output"));
Assert.That(afm.With(10, 3, "S40", out problem), Is.Null);
Assert.That(problem, Does.Contain("RGB toy"));
Assert.That(afm.With(4, 1, "S7 SHPshape,", out problem), Is.Null, "a comma would move the knocker to port 8");
Assert.That(problem, Does.Contain("move the toys after it"));
Assert.That(afm.With(4, 1, "S7 SHP@wide@", out problem), Is.Null, "a variable with a colour takes three ports as libdof reads it");
Assert.That(problem, Does.Contain("move the toys after it"));
Assert.That(DofIniRow.Parse(Ini, "t2").With(1, 3, "S1 Red", out problem).Line, Is.EqualTo("t2,S1 Red,L12 Blue/S3 Red,@flash@"),
"a row with a variable in a column of its own stays editable");
}

[Test]
public void FitsWhatTheUserWrites()
{
Assert.That(DofIniRow.Fit(" S28 Blink ", false, out var problem), Is.EqualTo("S28 Blink"));
Assert.That(DofIniRow.Fit("S28", true, out problem), Is.EqualTo("S28 #FFFFFF"), "an RGB toy takes a colour, in one every configuration knows");
Assert.That(DofIniRow.Fit("S28/L5 Red", true, out problem), Is.EqualTo("S28 #FFFFFF/L5 Red"));
Assert.That(DofIniRow.Fit("0", false, out problem), Is.EqualTo(""));
Assert.That(DofIniRow.Fit("", true, out problem), Is.EqualTo(""));
Assert.That(DofIniRow.Fit("S28 Red", false, out problem), Is.Null);
Assert.That(problem, Does.Contain("leave out the colour"));
Assert.That(DofIniRow.Fit("S28,S29", false, out problem), Is.Null);
Assert.That(problem, Does.Contain("comma"));
Assert.That(DofIniRow.Fit("Q12", false, out problem), Is.Null);
Assert.That(problem, Does.Contain("Q12"));
Assert.That(DofIniRow.Fit("S28 Rougeé", true, out problem), Is.Null);
Assert.That(DofIniRow.Fit("S7 SHP(", false, out problem), Is.Null, "libdof would read the rest of the row as the column");
Assert.That(problem, Does.Contain("brackets"));
Assert.That(DofIniRow.Fit("(S7 or S8) Red", true, out problem), Is.EqualTo("(S7 or S8) Red"));
Assert.That(DofIniRow.Fit("S7 SHPshape,", false, out problem), Is.Null, "a comma at the end is a column too");
Assert.That(problem, Does.Contain("comma"));
Assert.That(DofIniRow.Fit("S7 SHP@wide@", false, out problem), Is.Null, "a variable may stand for columns of its own");
Assert.That(problem, Does.Contain("variables"));
}

[Test]
public void ReplacesTheRowInTheText()
{
var row = DofIniRow.Parse(Ini, "afm").With(7, 1, "S6", out _);
var text = row.ReplaceIn(Ini);
Assert.That(text, Does.Contain("\r\nafm,E101 Red,0,0,0,S6\r\nt2,"), "the row in place, its line ending kept");
Assert.That(text, Does.Contain("\r\nafm,S9\r\n"), "the second row of the ROM, which libdof skips, as it was");
Assert.That(DofIniRow.Parse(text, "afm").SettingOf(7, 1), Is.EqualTo("S6"));

const string old = "[Config DOF]\rafm,S1\rt2,S2\r";
var mac = DofIniRow.Parse(old, "afm");
Assert.That(mac.SettingOf(1, 1), Is.EqualTo("S1"), "lines that end with \\r alone, as libdof reads them");
Assert.That(mac.With(1, 1, "S7", out _).ReplaceIn(old), Is.EqualTo("[Config DOF]\rafm,S7\rt2,S2\r"));
}

[Test]
public void KeepsTriggersPerRowDeviceAndPort()
{
var triggers = DofGameTriggers.None.With("afm", 30, 7, "S6\t").With("AFM", 30, 1, "").With("t2", 30, 7, "S8");
Assert.That(triggers.SettingOf("afm", 30, 7), Is.EqualTo("S6"));
Assert.That(triggers.SettingOf("afm", 30, 1), Is.EqualTo(""), "nothing fires it");
Assert.That(triggers.SettingOf("afm", 30, 4), Is.Null, "unchanged");
Assert.That(triggers.SettingOf("afm", 31, 7), Is.Null);
Assert.That(triggers.Of("Afm", 30), Is.EqualTo(new[] { (1, ""), (7, "S6") }), "ROMs as libdof compares them");

triggers.Save(_root);
var loaded = DofGameTriggers.Load(_root);
Assert.That(loaded.Of("afm", 30), Is.EqualTo(new[] { (1, ""), (7, "S6") }));
Assert.That(loaded.SettingOf("t2", 30, 7), Is.EqualTo("S8"));

var reset = loaded.With("afm", 30, 7, null).With("afm", 30, 1, null).With("t2", 30, 7, null);
Assert.That(reset.IsEmpty, Is.True);
reset.Save(_root);
Assert.That(File.Exists(Path.Combine(_root, DofGameTriggers.FileName)), Is.False, "no file for nothing changed");
}
}
}
48 changes: 48 additions & 0 deletions VisualPinball.Engine.DOF.Test/DofImporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -751,5 +751,53 @@ public void GlobalConfigUsesPathsLibdofCanOpen()
Assert.That(DofPaths.IsAscii(global.Element(element).Value), Is.True, global.Element(element).Value);
}
}

[Test]
public void BuildsAGameCopyWithTheUsersTriggers()
{
Accepted(_importer.Import(Zip("config.zip", ("directoutputconfig.ini", MainIni), ("directoutputconfig51.ini", SecondIni),
("DirectOutputShapes.xml", Shapes), ("DirectOutputShapes.png", "png"))));
var revision = _store.Active;
var catalog = DofCatalog.Load(revision);
var problems = new List<string>();
Assert.That(DofGameConfig.Build(revision, catalog, _store.Root, catalog.Match("afm_113b"), DofGameTriggers.None.With("t2", 1, 2, "S9"), problems),
Is.Null, "another game's triggers leave the revision to the game");

var triggers = DofGameTriggers.None.With("afm", 1, 2, "S7 500").With("afm", 51, 3, "L9").With("afm", 1, 1, "");
var basePath = DofGameConfig.Build(revision, catalog, _store.Root, catalog.Match("afm_113b"), triggers, problems);
Assert.That(problems, Is.Empty);
var config = Path.Combine(basePath, DofRevision.ConfigFolder);
Assert.That(File.ReadAllText(Path.Combine(config, "directoutputconfig.ini")), Does.Contain("\r\nafm,0,S7 500\r\nt2,S3,0\r\n"));
Assert.That(File.ReadAllText(Path.Combine(config, "directoutputconfig51.ini")), Does.Contain("\r\nafm,L5,0,L9\r\n"), "0s up to the toy");
Assert.That(File.ReadAllText(Path.Combine(revision.ConfigDir, "directoutputconfig.ini")), Does.Contain("afm,S1,S2"), "the revision as it was imported");
Assert.That(File.Exists(Path.Combine(config, "DirectOutputShapes.png")), Is.True);
var global = XDocument.Load(Path.Combine(config, DofGlobalConfig.FileName)).Root;
Assert.That(global.Element("IniFilesPath").Value, Is.EqualTo(DofPaths.ToNative(config)));
Assert.That(global.Element("ShapeDefinitionFilePattern").Value, Is.EqualTo(Path.Combine(DofPaths.ToNative(config), "DirectOutputShapes.xml")));

// libdof runs with it, confined to the copy
using (var dof = DofInstance.Create(basePath)) {
dof.Init(null, "afm_113b");
dof.Finish();
}

// a row that changed since: what can't be made is said, the rest is made
triggers = DofGameTriggers.None.With("afm", 1, 2, "S7").With("afm", 51, 2, "L1");
File.WriteAllText(Path.Combine(revision.ConfigDir, "directoutputconfig51.ini"), Header + "afm,L5 Red\r\n");
basePath = DofGameConfig.Build(revision, DofCatalog.Load(revision), _store.Root, catalog.Match("afm"), triggers, problems);
Assert.That(problems, NUnit.Framework.Has.Count.EqualTo(1));
Assert.That(problems[0], Does.Contain("Device 51, port 2"));
Assert.That(File.ReadAllText(Path.Combine(basePath, DofRevision.ConfigFolder, "directoutputconfig.ini")), Does.Contain("afm,S1,S7"));
// a toy an import made an RGB one fires in white; a colour on a toy of one output is said and left out
triggers = DofGameTriggers.None.With("afm", 51, 1, "L1").With("afm", 1, 2, "S7 Red").With("afm", 51, 2, "L2");
problems.Clear();
basePath = DofGameConfig.Build(revision, DofCatalog.Load(revision), _store.Root, catalog.Match("afm"), triggers, problems);
Assert.That(File.ReadAllText(Path.Combine(basePath, DofRevision.ConfigFolder, "directoutputconfig51.ini")), Does.Contain("\r\nafm,L1 #FFFFFF\r\n"));
Assert.That(problems, NUnit.Framework.Has.Count.EqualTo(2), string.Join("\n", problems));
Assert.That(problems, NUnit.Framework.Has.Some.Contains("Device 1, port 2").And.Some.Contains("takes one output"));
Assert.That(problems, NUnit.Framework.Has.Some.Contains("Device 51, port 2").And.Some.Contains("part of an RGB toy"), "its green is changed with the toy");
DofGameConfig.Delete(_store.Root);
Assert.That(Directory.Exists(basePath), Is.False);
}
}
}
57 changes: 56 additions & 1 deletion VisualPinball.Engine.DOF.Unity/Runtime/Player/DofPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,10 @@ public override async Task PrepareGameAsync(PluginGameContext game, Cancellation
var selection = new PluginResourceSelection(ResourceKind.RealtimeOutputs) { Controller = game.ControllerId, Session = game.Session };
var outputs = activity.Own(game.Resources.Bind<IPluginRealtimeOutputSource>(selection));
var feed = FeedOf(outputs);
// what the user changed of the game's rows, in a copy only this game uses: the previous game's is done with it
var basePath = (_rom != null ? GameConfig(revision, catalog, game) : null) ?? revision.BasePath;
session = new DofGameSession(new DofSessionOptions {
BasePath = revision.BasePath,
BasePath = basePath,
RomName = _rom,
GameSession = unchecked((int)game.Session),
// Without a feed only the triggers drive DOF; there is no initial state to wait for.
Expand Down Expand Up @@ -403,6 +405,59 @@ private void SessionLog(DofLogLevel level, string message)
}
}

/// <summary>
/// The base path of a copy of the configuration with what the user changed of the game's rows; null when nothing is
/// changed or the copy can't be made, so the game uses the configuration as it is. The log says what wasn't applied.
/// </summary>
private string GameConfig(DofRevision revision, DofCatalog catalog, PluginGameContext game)
{
var root = _factory.Store.Root;
try {
var rows = catalog.Match(_rom);
var triggers = DofGameTriggers.Load(root);
// The settings page changes the rows of the ROM it finds without a running game. The game may play a compatible
// ROM, or have a name for DOF, with rows of their own: on a port where the user's trigger of those doesn't apply,
// the page's does, where it applies on the page.
var problems = new List<string>();
var (pageRom, _) = DofGameSetup.ResolveRom(game.Settings.String("Rom", DofGameSetup.Auto), game.Identity, null, null, catalog);
foreach (var pageRow in pageRom != null ? catalog.Match(pageRom) : Array.Empty<DofRomRow>()) {
var device = catalog.Devices.FirstOrDefault(d => d.Number == pageRow.Device);
// the files are large: read only for a device the user changed
if (triggers.Of(pageRow.Rom, pageRow.Device).Count == 0) {
continue;
}
foreach (var row in rows.Where(r => device != null && r.Device == pageRow.Device && !string.Equals(r.Rom, pageRow.Rom, StringComparison.OrdinalIgnoreCase)).Take(1)) {
var path = Path.Combine(revision.ConfigDir, device.FileName);
var (own, page) = (DofIniRow.Read(path, row.Rom), DofIniRow.Read(path, pageRow.Rom));
var rgb = DofGameConfig.RgbStarts(device);
foreach (var (port, setting) in triggers.Of(pageRow.Rom, pageRow.Device)) {
var mine = triggers.SettingOf(row.Rom, row.Device, port);
if (own == null || page == null || mine != null && DofGameConfig.Change(own, rgb, port, mine, out _) != null) {
continue;
}
// the page shows it doesn't fit; the log says so too, as it does for one of the running rows
if (DofGameConfig.Change(page, rgb, port, setting, out var problem) == null) {
problems.Add($"Device {pageRow.Device}, port {port}: {problem}");
continue;
}
triggers = triggers.With(row.Rom, row.Device, port, setting);
}
}
}
var basePath = DofGameConfig.Build(revision, catalog, root, rows, triggers, problems);
foreach (var problem in problems) {
_application.Logger.Warn("A trigger you changed for this game is not applied: " + problem);
}
if (basePath != null) {
_application.Logger.Info("DOF fires the toys with the triggers you changed for this game.");
}
return basePath;
} catch (Exception e) when (e is IOException || e is UnauthorizedAccessException || e is System.Xml.XmlException || e is FormatException || e is DofException) {
_application.Logger.Warn("The triggers you changed for this game could not be applied; DOF uses the configuration as it is.", e);
return null;
}
}

/// <summary>The ROM the gamelogic engine plays, which PinMAME prepares first; null for other engines.</summary>
private static string RunningRom(IUnityGameScene scene, GameIdentity game)
{
Expand Down
Loading
Loading