diff --git a/VisualPinball.Engine.DMD.Unity/Runtime/Plugins/DmdPlugin.cs b/VisualPinball.Engine.DMD.Unity/Runtime/Plugins/DmdPlugin.cs index a4e68e3..0c5ae09 100644 --- a/VisualPinball.Engine.DMD.Unity/Runtime/Plugins/DmdPlugin.cs +++ b/VisualPinball.Engine.DMD.Unity/Runtime/Plugins/DmdPlugin.cs @@ -47,21 +47,25 @@ public override Task PrepareGameAsync(PluginGameContext game, CancellationToken var scene = game.Capabilities.Require(UnityPluginCapability.GameScene); var effective = WithDmdDeviceIni(game); var settings = DmdPluginFactory.Settings(effective); - settings.RomName = game.Identity.Key; _gameSettings = effective; - _colorizationLibrary = DmdPluginFactory.Library(effective, game.Identity.Key); - var asset = ColorizationCatalog.Select(_colorizationLibrary, game.PackagePath, game.Identity, - effective.String("Colorization", "auto"), effective.String("VniKey").Length > 0); + // PinMAME can play a compatible ROM in place of the table's own: its colorization first, else the table ROM's. A + // colorization selected in the settings is one of the table ROM's. + string rom = RunningRom(scene, game.Identity), colorization = effective.String("Colorization", "auto"); + string key = colorization == "auto" ? rom : game.Identity.Key; + var asset = key == game.Identity.Key ? SelectColorization(game, effective, key, colorization) : SubstituteColorization(game, effective, key); + if (asset == null && key != game.Identity.Key) asset = SelectColorization(game, effective, key = game.Identity.Key, colorization); + if (asset == null && key != rom) _colorizationLibrary = DmdPluginFactory.Library(game.Settings, key = rom); + settings.RomName = key; settings.EnableColorization = asset != null; _asset = asset; _settings = settings; - _noColorizationReason = asset != null ? null : NoColorizationReason(game.Identity.Key, effective); + _noColorizationReason = asset != null ? null : NoColorizationReason(key, effective); _colorizationAsset = asset != null ? asset.Id + " (" + asset.Label + ")" : "none"; if (asset != null) { - _nativeResources.Own(ColorizationCatalog.Stage(asset, Path.Combine(_application.Configuration.DirectoryFor(_application.Plugin), "cache", "colorizations"), game.Identity.Key, out string staged)); + _nativeResources.Own(ColorizationCatalog.Stage(asset, Path.Combine(_application.Configuration.DirectoryFor(_application.Plugin), "cache", "colorizations"), key, out string staged)); settings.AltColorPath = staged; _stagedColorization = staged; } - _application.Logger.Info($"Colorization for {game.Identity.Key}: {_colorizationAsset}, source {asset?.Files[0] ?? _colorizationLibrary}."); + _application.Logger.Info($"Colorization for {key}{(key == game.Identity.Key ? "" : $" (table ROM {game.Identity.Key})")}: {_colorizationAsset}, source {asset?.Files[0] ?? _colorizationLibrary}."); ConfigureCabinetOutputs(game, effective, settings); _root = new GameObject("DMD plugin"); _root.SetActive(false); _root.transform.SetParent(scene.Table.transform, false); game.Lifetime.Own(new Subscription(() => { if (_root != null) UnityEngine.Object.Destroy(_root); })); @@ -90,6 +94,36 @@ public override Task PrepareGameAsync(PluginGameContext game, CancellationToken return Task.CompletedTask; } + /// The ROM the gamelogic engine plays, which PinMAME prepares first; the game's key for other engines. + private static string RunningRom(IUnityGameScene scene, GameIdentity game) + { + if (game.Provider != "pinmame") return game.Key; + string rom = scene.Table.GetComponentInChildren(true)?.RomName?.Trim().ToLowerInvariant(); + return string.IsNullOrEmpty(rom) || rom != Path.GetFileName(rom) || rom == "." || rom == ".." ? game.Key : rom; + } + + /// + /// The substitute ROM's colorization, or null when it has none or several: the user can only pick among the table ROM's, so + /// an ambiguous folder falls back to those. + /// + private ColorizationAsset SubstituteColorization(PluginGameContext game, PluginSettingsSnapshot settings, string key) + { + try { + return SelectColorization(game, settings, key, "auto"); + } catch (InvalidOperationException error) { + _application.Logger.Info($"Not using the colorizations of {key}: {error.Message}"); + return null; + } + } + + private ColorizationAsset SelectColorization(PluginGameContext game, PluginSettingsSnapshot settings, string key, string selection) + { + // the library the user saved, else the ROM's own altcolor in DmdDevice.ini: the imported one is the table ROM's + _colorizationLibrary = DmdPluginFactory.Library(game.Settings, key); + var identity = key == game.Identity.Key ? game.Identity : new GameIdentity(game.Identity.Provider, key); + return ColorizationCatalog.Select(_colorizationLibrary, game.PackagePath, identity, selection, settings.String("VniKey").Length > 0); + } + /// /// The game's settings with DmdDevice.ini underneath what the user saved: values in the global settings or the game's profile /// win, every other one comes from the ini when it has it. diff --git a/VisualPinball.Engine.DMD.Unity/Runtime/Plugins/DmdPluginFactory.cs b/VisualPinball.Engine.DMD.Unity/Runtime/Plugins/DmdPluginFactory.cs index 49e4ed6..53c3f88 100644 --- a/VisualPinball.Engine.DMD.Unity/Runtime/Plugins/DmdPluginFactory.cs +++ b/VisualPinball.Engine.DMD.Unity/Runtime/Plugins/DmdPluginFactory.cs @@ -32,7 +32,9 @@ internal sealed partial class DmdPluginFactory : IPlayerPluginFactory, IPluginSe internal const string Id = "org.visualpinball.dmd"; public IReadOnlyList CabinetRoles { get; } = Array.AsReadOnly(new[] { CabinetOutputRole.Dmd }); private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); - public PluginDescriptor Descriptor { get; } = new PluginDescriptor(Id, "DMD", "Colorizations, your in-scene DMD and native displays, with optional cabinet hardware.") { EnabledByDefault = true, Category = "Displays", Order = 20 }; + public PluginDescriptor Descriptor { get; } = new PluginDescriptor(Id, "DMD", "Colorizations, your in-scene DMD and native displays, with optional cabinet hardware.") { EnabledByDefault = true, Category = "Displays", Order = 20, + // PinMAME first: it decides which ROM plays, and the colorization follows it + After = new[] { "org.visualpinball.pinmame" } }; internal static readonly PluginSettingsSchema SettingsSchema = BuildSchema(); public PluginSettingsSchema Schema => SettingsSchema; public IPluginSettingsProvider SettingsProvider => this;