From d170972e3c7b3218e30fa83b119a5eb4a04fa60a Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Tue, 8 Sep 2020 00:01:24 +1000 Subject: [PATCH] Cleanup various Hawk cores' controller decks The main point is referencing types directly instead of iterating them by reflection and also calling their ctors by reflection. (disclaimer: [DisplayName] attrs are still necessarily read by reflection) --- .../config/A7800/A7800ControllerSettings.cs | 5 +- .../ColecoVision/ColecoControllerSettings.cs | 5 +- .../config/INTV/IntvControllerSettings.cs | 5 +- .../Atari/2600/Atari2600ControllerDeck.cs | 41 ++++---------- .../Atari/A7800Hawk/A7800Hawk.ISettable.cs | 4 +- .../A7800Hawk/A7800HawkControllerDeck.cs | 41 +++++--------- .../Consoles/Coleco/ColecoControllerDeck.cs | 41 +++++--------- .../Consoles/Coleco/ColecoVision.ISettable.cs | 4 +- .../GCE/Vectrex/VectrexHawkControllerDeck.cs | 39 ++++--------- .../IntellivisionControllerDeck.cs | 40 +++++-------- .../Intellivision/Intellivision.ISettable.cs | 4 +- .../Magnavox/Odyssey2/O2HawkControllerDeck.cs | 38 ++++--------- .../Nintendo/GBHawk/GBHawkControllerDeck.cs | 30 +++------- .../GBHawkLink/GBHawkLinkControllerDeck.cs | 38 ++++--------- .../GBHawkLink3xControllerDeck.cs | 47 +++++----------- .../GBHawkLink4xControllerDeck.cs | 56 ++++++------------- .../GGHawkLink/GGHawkLinkControllerDeck.cs | 38 ++++--------- 17 files changed, 147 insertions(+), 329 deletions(-) diff --git a/src/BizHawk.Client.EmuHawk/config/A7800/A7800ControllerSettings.cs b/src/BizHawk.Client.EmuHawk/config/A7800/A7800ControllerSettings.cs index 1c5ec876a7..af367753c7 100644 --- a/src/BizHawk.Client.EmuHawk/config/A7800/A7800ControllerSettings.cs +++ b/src/BizHawk.Client.EmuHawk/config/A7800/A7800ControllerSettings.cs @@ -1,5 +1,4 @@ using System; -using System.Linq; using System.Windows.Forms; using BizHawk.Emulation.Cores.Atari.A7800Hawk; @@ -22,9 +21,7 @@ namespace BizHawk.Client.EmuHawk private void IntvControllerSettings_Load(object sender, EventArgs e) { - var possibleControllers = A7800HawkControllerDeck.ValidControllerTypes.Select(t => t.Key); - - foreach (var val in possibleControllers) + foreach (var val in A7800HawkControllerDeck.ControllerCtors.Keys) { Port1ComboBox.Items.Add(val); Port2ComboBox.Items.Add(val); diff --git a/src/BizHawk.Client.EmuHawk/config/ColecoVision/ColecoControllerSettings.cs b/src/BizHawk.Client.EmuHawk/config/ColecoVision/ColecoControllerSettings.cs index 2f52770d0c..2b70ec677e 100644 --- a/src/BizHawk.Client.EmuHawk/config/ColecoVision/ColecoControllerSettings.cs +++ b/src/BizHawk.Client.EmuHawk/config/ColecoVision/ColecoControllerSettings.cs @@ -1,5 +1,4 @@ using System; -using System.Linq; using System.Windows.Forms; using BizHawk.Emulation.Cores.ColecoVision; @@ -22,9 +21,7 @@ namespace BizHawk.Client.EmuHawk private void ColecoControllerSettings_Load(object sender, EventArgs e) { - var possibleControllers = ColecoVisionControllerDeck.ValidControllerTypes.Select(t => t.Key); - - foreach (var val in possibleControllers) + foreach (var val in ColecoVisionControllerDeck.ControllerCtors.Keys) { Port1ComboBox.Items.Add(val); Port2ComboBox.Items.Add(val); diff --git a/src/BizHawk.Client.EmuHawk/config/INTV/IntvControllerSettings.cs b/src/BizHawk.Client.EmuHawk/config/INTV/IntvControllerSettings.cs index e98cda371b..10f2ff5f53 100644 --- a/src/BizHawk.Client.EmuHawk/config/INTV/IntvControllerSettings.cs +++ b/src/BizHawk.Client.EmuHawk/config/INTV/IntvControllerSettings.cs @@ -1,5 +1,4 @@ using System; -using System.Linq; using System.Windows.Forms; using BizHawk.Emulation.Cores.Intellivision; @@ -22,9 +21,7 @@ namespace BizHawk.Client.EmuHawk private void IntvControllerSettings_Load(object sender, EventArgs e) { - var possibleControllers = IntellivisionControllerDeck.ValidControllerTypes.Select(t => t.Key); - - foreach (var val in possibleControllers) + foreach (var val in IntellivisionControllerDeck.ControllerCtors.Keys) { Port1ComboBox.Items.Add(val); Port2ComboBox.Items.Add(val); diff --git a/src/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600ControllerDeck.cs b/src/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600ControllerDeck.cs index 1b7e2fc35b..432b590522 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600ControllerDeck.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600ControllerDeck.cs @@ -3,27 +3,17 @@ using System.Collections.Generic; using System.Linq; using BizHawk.Common; -using BizHawk.Common.ReflectionExtensions; using BizHawk.Emulation.Common; namespace BizHawk.Emulation.Cores.Atari.Atari2600 { public class Atari2600ControllerDeck { - private static readonly Type[] Implementors = - { - typeof(UnpluggedController), // Order must match Atari2600ControllerTypes enum values - typeof(StandardController), - typeof(PaddleController), - typeof(BoostGripController), - typeof(DrivingController), - typeof(KeyboardController) - }; public Atari2600ControllerDeck(Atari2600ControllerTypes controller1, Atari2600ControllerTypes controller2) { - Port1 = (IPort)Activator.CreateInstance(Implementors[(int)controller1], 1); - Port2 = (IPort)Activator.CreateInstance(Implementors[(int)controller2], 2); + Port1 = ControllerCtors[controller1](1); + Port2 = ControllerCtors[controller1](2); Definition = new ControllerDefinition { @@ -77,24 +67,17 @@ namespace BizHawk.Emulation.Cores.Atari.Atari2600 private readonly IPort Port1; private readonly IPort Port2; - private static Dictionary _controllerTypes; + private static IReadOnlyDictionary> _controllerCtors; - public static Dictionary ValidControllerTypes - { - get + public static IReadOnlyDictionary> ControllerCtors => _controllerCtors + ??= new Dictionary> { - if (_controllerTypes == null) - { - _controllerTypes = Emulation.Cores.ReflectionCache.Types - .Where(t => typeof(IPort).IsAssignableFrom(t)) - .Where(t => !t.IsAbstract && !t.IsInterface) - .ToDictionary(tkey => tkey.DisplayName()); - } - - return _controllerTypes; - } - } - - public static string DefaultControllerName => typeof(StandardController).DisplayName(); + [Atari2600ControllerTypes.Unplugged] = portNum => new UnpluggedController(portNum), + [Atari2600ControllerTypes.Joystick] = portNum => new StandardController(portNum), + [Atari2600ControllerTypes.Paddle] = portNum => new PaddleController(portNum), + [Atari2600ControllerTypes.BoostGrip] = portNum => new BoostGripController(portNum), + [Atari2600ControllerTypes.Driving] = portNum => new DrivingController(portNum), + [Atari2600ControllerTypes.Keyboard] = portNum => new KeyboardController(portNum) + }; } } diff --git a/src/BizHawk.Emulation.Cores/Consoles/Atari/A7800Hawk/A7800Hawk.ISettable.cs b/src/BizHawk.Emulation.Cores/Consoles/Atari/A7800Hawk/A7800Hawk.ISettable.cs index 311c533cd8..9ae394fe54 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Atari/A7800Hawk/A7800Hawk.ISettable.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Atari/A7800Hawk/A7800Hawk.ISettable.cs @@ -66,7 +66,7 @@ namespace BizHawk.Emulation.Cores.Atari.A7800Hawk get => _port1; set { - if (!A7800HawkControllerDeck.ValidControllerTypes.ContainsKey(value)) + if (!A7800HawkControllerDeck.ControllerCtors.ContainsKey(value)) { throw new InvalidOperationException("Invalid controller type: " + value); } @@ -81,7 +81,7 @@ namespace BizHawk.Emulation.Cores.Atari.A7800Hawk get => _port2; set { - if (!A7800HawkControllerDeck.ValidControllerTypes.ContainsKey(value)) + if (!A7800HawkControllerDeck.ControllerCtors.ContainsKey(value)) { throw new InvalidOperationException("Invalid controller type: " + value); } diff --git a/src/BizHawk.Emulation.Cores/Consoles/Atari/A7800Hawk/A7800HawkControllerDeck.cs b/src/BizHawk.Emulation.Cores/Consoles/Atari/A7800Hawk/A7800HawkControllerDeck.cs index 48a1e976a0..7735d6cf71 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Atari/A7800Hawk/A7800HawkControllerDeck.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Atari/A7800Hawk/A7800HawkControllerDeck.cs @@ -12,18 +12,12 @@ namespace BizHawk.Emulation.Cores.Atari.A7800Hawk { public A7800HawkControllerDeck(string controller1Name, string controller2Name) { - if (!ValidControllerTypes.ContainsKey(controller1Name)) - { - throw new InvalidOperationException("Invalid controller type: " + controller1Name); - } - - if (!ValidControllerTypes.ContainsKey(controller2Name)) - { - throw new InvalidOperationException("Invalid controller type: " + controller2Name); - } - - Port1 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller1Name], 1); - Port2 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller2Name], 2); + Port1 = ControllerCtors.TryGetValue(controller1Name, out var ctor1) + ? ctor1(1) + : throw new InvalidOperationException($"Invalid controller type: {controller1Name}"); + Port2 = ControllerCtors.TryGetValue(controller2Name, out var ctor2) + ? ctor2(2) + : throw new InvalidOperationException($"Invalid controller type: {controller2Name}"); Definition = new ControllerDefinition { @@ -113,23 +107,16 @@ namespace BizHawk.Emulation.Cores.Atari.A7800Hawk private readonly IPort Port1; private readonly IPort Port2; - private static Dictionary _controllerTypes; + private static IReadOnlyDictionary> _controllerCtors; - public static Dictionary ValidControllerTypes - { - get + public static IReadOnlyDictionary> ControllerCtors => _controllerCtors + ??= new Dictionary> { - if (_controllerTypes == null) - { - _controllerTypes = Emulation.Cores.ReflectionCache.Types - .Where(t => typeof(IPort).IsAssignableFrom(t)) - .Where(t => !t.IsAbstract && !t.IsInterface) - .ToDictionary(tkey => tkey.DisplayName()); - } - - return _controllerTypes; - } - } + [typeof(UnpluggedController).DisplayName()] = portNum => new UnpluggedController(portNum), + [typeof(StandardController).DisplayName()] = portNum => new StandardController(portNum), + [typeof(ProLineController).DisplayName()] = portNum => new ProLineController(portNum), + [typeof(LightGunController).DisplayName()] = portNum => new LightGunController(portNum) + }; public static string DefaultControllerName => typeof(StandardController).DisplayName(); } diff --git a/src/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoControllerDeck.cs b/src/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoControllerDeck.cs index 7f93f032a5..a741fbf74d 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoControllerDeck.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoControllerDeck.cs @@ -12,18 +12,12 @@ namespace BizHawk.Emulation.Cores.ColecoVision { public ColecoVisionControllerDeck(string controller1Name, string controller2Name) { - if (!ValidControllerTypes.ContainsKey(controller1Name)) - { - throw new InvalidOperationException("Invalid controller type: " + controller1Name); - } - - if (!ValidControllerTypes.ContainsKey(controller2Name)) - { - throw new InvalidOperationException("Invalid controller type: " + controller2Name); - } - - Port1 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller1Name], 1); - Port2 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller2Name], 2); + Port1 = ControllerCtors.TryGetValue(controller1Name, out var ctor1) + ? ctor1(1) + : throw new InvalidOperationException($"Invalid controller type: {controller1Name}"); + Port2 = ControllerCtors.TryGetValue(controller2Name, out var ctor2) + ? ctor2(2) + : throw new InvalidOperationException($"Invalid controller type: {controller2Name}"); Definition = new ControllerDefinition { @@ -79,23 +73,16 @@ namespace BizHawk.Emulation.Cores.ColecoVision public IPort Port1 { get; } public IPort Port2 { get; } - private static Dictionary _controllerTypes = null; + private static IReadOnlyDictionary> _controllerCtors; - public static Dictionary ValidControllerTypes - { - get + public static IReadOnlyDictionary> ControllerCtors => _controllerCtors + ??= new Dictionary> { - if (_controllerTypes == null) - { - _controllerTypes = Emulation.Cores.ReflectionCache.Types - .Where(t => typeof(IPort).IsAssignableFrom(t)) - .Where(t => !t.IsAbstract && !t.IsInterface) - .ToDictionary(tkey => tkey.DisplayName()); - } - - return _controllerTypes; - } - } + [typeof(UnpluggedController).DisplayName()] = portNum => new UnpluggedController(portNum), + [typeof(StandardController).DisplayName()] = portNum => new StandardController(portNum), + [typeof(ColecoTurboController).DisplayName()] = portNum => new ColecoTurboController(portNum), + [typeof(ColecoSuperActionController).DisplayName()] = portNum => new ColecoSuperActionController(portNum) + }; public static string DefaultControllerName => typeof(StandardController).DisplayName(); } diff --git a/src/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.ISettable.cs b/src/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.ISettable.cs index 1ba06ed945..83d7352afe 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.ISettable.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.ISettable.cs @@ -57,7 +57,7 @@ namespace BizHawk.Emulation.Cores.ColecoVision get => _port1; set { - if (!ColecoVisionControllerDeck.ValidControllerTypes.ContainsKey(value)) + if (!ColecoVisionControllerDeck.ControllerCtors.ContainsKey(value)) { throw new InvalidOperationException("Invalid controller type: " + value); } @@ -72,7 +72,7 @@ namespace BizHawk.Emulation.Cores.ColecoVision get => _port2; set { - if (!ColecoVisionControllerDeck.ValidControllerTypes.ContainsKey(value)) + if (!ColecoVisionControllerDeck.ControllerCtors.ContainsKey(value)) { throw new InvalidOperationException("Invalid controller type: " + value); } diff --git a/src/BizHawk.Emulation.Cores/Consoles/GCE/Vectrex/VectrexHawkControllerDeck.cs b/src/BizHawk.Emulation.Cores/Consoles/GCE/Vectrex/VectrexHawkControllerDeck.cs index 6c88c306cc..3bc7554143 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/GCE/Vectrex/VectrexHawkControllerDeck.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/GCE/Vectrex/VectrexHawkControllerDeck.cs @@ -12,18 +12,12 @@ namespace BizHawk.Emulation.Cores.Consoles.Vectrex { public VectrexHawkControllerDeck(string controller1Name, string controller2Name) { - if (!ValidControllerTypes.ContainsKey(controller1Name)) - { - throw new InvalidOperationException("Invalid controller type: " + controller1Name); - } - - if (!ValidControllerTypes.ContainsKey(controller2Name)) - { - throw new InvalidOperationException("Invalid controller type: " + controller2Name); - } - - Port1 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller1Name], 1); - Port2 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller2Name], 2); + Port1 = ControllerCtors.TryGetValue(controller1Name, out var ctor1) + ? ctor1(1) + : throw new InvalidOperationException($"Invalid controller type: {controller1Name}"); + Port2 = ControllerCtors.TryGetValue(controller2Name, out var ctor2) + ? ctor2(2) + : throw new InvalidOperationException($"Invalid controller type: {controller2Name}"); Definition = new ControllerDefinition { @@ -68,23 +62,14 @@ namespace BizHawk.Emulation.Cores.Consoles.Vectrex private readonly IPort Port1; private readonly IPort Port2; - private static Dictionary _controllerTypes; + private static IReadOnlyDictionary> _controllerCtors; - public static Dictionary ValidControllerTypes - { - get + public static IReadOnlyDictionary> ControllerCtors => _controllerCtors + ??= new Dictionary> { - if (_controllerTypes == null) - { - _controllerTypes = Emulation.Cores.ReflectionCache.Types - .Where(t => typeof(IPort).IsAssignableFrom(t)) - .Where(t => !t.IsAbstract && !t.IsInterface) - .ToDictionary(tkey => tkey.DisplayName()); - } - - return _controllerTypes; - } - } + [typeof(StandardControls).DisplayName()] = portNum => new StandardControls(portNum), + [typeof(AnalogControls).DisplayName()] = portNum => new AnalogControls(portNum) + }; public static string DefaultControllerName => typeof(StandardControls).DisplayName(); } diff --git a/src/BizHawk.Emulation.Cores/Consoles/Intellivision/Controllers/IntellivisionControllerDeck.cs b/src/BizHawk.Emulation.Cores/Consoles/Intellivision/Controllers/IntellivisionControllerDeck.cs index 1679a911d8..6c0494b0ef 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Intellivision/Controllers/IntellivisionControllerDeck.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Intellivision/Controllers/IntellivisionControllerDeck.cs @@ -12,18 +12,12 @@ namespace BizHawk.Emulation.Cores.Intellivision { public IntellivisionControllerDeck(string controller1Name, string controller2Name) { - if (!ValidControllerTypes.ContainsKey(controller1Name)) - { - throw new InvalidOperationException("Invalid controller type: " + controller1Name); - } - - if (!ValidControllerTypes.ContainsKey(controller2Name)) - { - throw new InvalidOperationException("Invalid controller type: " + controller2Name); - } - - Port1 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller1Name], 1); - Port2 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller2Name], 2); + Port1 = ControllerCtors.TryGetValue(controller1Name, out var ctor1) + ? ctor1(1) + : throw new InvalidOperationException($"Invalid controller type: {controller1Name}"); + Port2 = ControllerCtors.TryGetValue(controller2Name, out var ctor2) + ? ctor2(2) + : throw new InvalidOperationException($"Invalid controller type: {controller2Name}"); Definition = new ControllerDefinition { @@ -68,23 +62,15 @@ namespace BizHawk.Emulation.Cores.Intellivision private readonly IPort Port1; private readonly IPort Port2; - private static Dictionary _controllerTypes; + private static IReadOnlyDictionary> _controllerCtors; - public static Dictionary ValidControllerTypes - { - get + public static IReadOnlyDictionary> ControllerCtors => _controllerCtors + ??= new Dictionary> { - if (_controllerTypes == null) - { - _controllerTypes = Emulation.Cores.ReflectionCache.Types - .Where(t => typeof(IPort).IsAssignableFrom(t)) - .Where(t => !t.IsAbstract && !t.IsInterface) - .ToDictionary(tkey => tkey.DisplayName()); - } - - return _controllerTypes; - } - } + [typeof(UnpluggedController).DisplayName()] = portNum => new UnpluggedController(portNum), + [typeof(StandardController).DisplayName()] = portNum => new StandardController(portNum), + [typeof(FakeAnalogController).DisplayName()] = portNum => new FakeAnalogController(portNum) + }; public static string DefaultControllerName => typeof(FakeAnalogController).DisplayName(); } diff --git a/src/BizHawk.Emulation.Cores/Consoles/Intellivision/Intellivision.ISettable.cs b/src/BizHawk.Emulation.Cores/Consoles/Intellivision/Intellivision.ISettable.cs index ca93fa33f9..999929b2f3 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Intellivision/Intellivision.ISettable.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Intellivision/Intellivision.ISettable.cs @@ -53,7 +53,7 @@ namespace BizHawk.Emulation.Cores.Intellivision get => _port1; set { - if (!IntellivisionControllerDeck.ValidControllerTypes.ContainsKey(value)) + if (!IntellivisionControllerDeck.ControllerCtors.ContainsKey(value)) { throw new InvalidOperationException("Invalid controller type: " + value); } @@ -68,7 +68,7 @@ namespace BizHawk.Emulation.Cores.Intellivision get => _port2; set { - if (!IntellivisionControllerDeck.ValidControllerTypes.ContainsKey(value)) + if (!IntellivisionControllerDeck.ControllerCtors.ContainsKey(value)) { throw new InvalidOperationException("Invalid controller type: " + value); } diff --git a/src/BizHawk.Emulation.Cores/Consoles/Magnavox/Odyssey2/O2HawkControllerDeck.cs b/src/BizHawk.Emulation.Cores/Consoles/Magnavox/Odyssey2/O2HawkControllerDeck.cs index 228a7ab695..72e218e338 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Magnavox/Odyssey2/O2HawkControllerDeck.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Magnavox/Odyssey2/O2HawkControllerDeck.cs @@ -12,18 +12,12 @@ namespace BizHawk.Emulation.Cores.Consoles.O2Hawk { public O2HawkControllerDeck(string controller1Name, string controller2Name, bool is_G7400) { - if (!ValidControllerTypes.ContainsKey(controller1Name)) - { - throw new InvalidOperationException("Invalid controller type: " + controller1Name); - } - - if (!ValidControllerTypes.ContainsKey(controller2Name)) - { - throw new InvalidOperationException("Invalid controller type: " + controller2Name); - } - - Port1 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller1Name], 1); - Port2 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller2Name], 2); + Port1 = ControllerCtors.TryGetValue(controller1Name, out var ctor1) + ? ctor1(1) + : throw new InvalidOperationException($"Invalid controller type: {controller1Name}"); + Port2 = ControllerCtors.TryGetValue(controller2Name, out var ctor2) + ? ctor2(2) + : throw new InvalidOperationException($"Invalid controller type: {controller2Name}"); if (is_G7400) { @@ -94,23 +88,13 @@ namespace BizHawk.Emulation.Cores.Consoles.O2Hawk private readonly IPort Port1, Port2; - private static Dictionary _controllerTypes; + private static IReadOnlyDictionary> _controllerCtors; - public static Dictionary ValidControllerTypes - { - get + public static IReadOnlyDictionary> ControllerCtors => _controllerCtors + ??= new Dictionary> { - if (_controllerTypes == null) - { - _controllerTypes = Emulation.Cores.ReflectionCache.Types - .Where(t => typeof(IPort).IsAssignableFrom(t)) - .Where(t => !t.IsAbstract && !t.IsInterface) - .ToDictionary(tkey => tkey.DisplayName()); - } - - return _controllerTypes; - } - } + [typeof(StandardControls).DisplayName()] = portNum => new StandardControls(portNum) + }; public static string DefaultControllerName => typeof(StandardControls).DisplayName(); } diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawkControllerDeck.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawkControllerDeck.cs index f425a73685..7d04eec601 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawkControllerDeck.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawk/GBHawkControllerDeck.cs @@ -12,12 +12,9 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk { public GBHawkControllerDeck(string controller1Name) { - if (!ValidControllerTypes.ContainsKey(controller1Name)) - { - throw new InvalidOperationException("Invalid controller type: " + controller1Name); - } - - Port1 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller1Name], 1); + Port1 = ControllerCtors.TryGetValue(controller1Name, out var ctor1) + ? ctor1(1) + : throw new InvalidOperationException($"Invalid controller type: {controller1Name}"); Definition = new ControllerDefinition { @@ -55,23 +52,14 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk private readonly IPort Port1; - private static Dictionary _controllerTypes; + private static IReadOnlyDictionary> _controllerCtors; - public static Dictionary ValidControllerTypes - { - get + public static IReadOnlyDictionary> ControllerCtors => _controllerCtors + ??= new Dictionary> { - if (_controllerTypes == null) - { - _controllerTypes = Emulation.Cores.ReflectionCache.Types - .Where(t => typeof(IPort).IsAssignableFrom(t)) - .Where(t => !t.IsAbstract && !t.IsInterface) - .ToDictionary(tkey => tkey.DisplayName()); - } - - return _controllerTypes; - } - } + [typeof(StandardControls).DisplayName()] = portNum => new StandardControls(portNum), + [typeof(StandardTilt).DisplayName()] = portNum => new StandardTilt(portNum) + }; public static string DefaultControllerName => typeof(StandardControls).DisplayName(); } diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawkLink/GBHawkLinkControllerDeck.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawkLink/GBHawkLinkControllerDeck.cs index 38f36ab78d..74085bf775 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawkLink/GBHawkLinkControllerDeck.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawkLink/GBHawkLinkControllerDeck.cs @@ -12,18 +12,12 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawkLink { public GBHawkLinkControllerDeck(string controller1Name, string controller2Name) { - if (!ValidControllerTypes.ContainsKey(controller1Name)) - { - throw new InvalidOperationException("Invalid controller type: " + controller1Name); - } - - if (!ValidControllerTypes.ContainsKey(controller2Name)) - { - throw new InvalidOperationException("Invalid controller type: " + controller2Name); - } - - Port1 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller1Name], 1); - Port2 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller2Name], 2); + Port1 = ControllerCtors.TryGetValue(controller1Name, out var ctor1) + ? ctor1(1) + : throw new InvalidOperationException($"Invalid controller type: {controller1Name}"); + Port2 = ControllerCtors.TryGetValue(controller2Name, out var ctor2) + ? ctor2(2) + : throw new InvalidOperationException($"Invalid controller type: {controller2Name}"); Definition = new ControllerDefinition { @@ -61,23 +55,13 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawkLink private readonly IPort Port1; private readonly IPort Port2; - private static Dictionary _controllerTypes; + private static IReadOnlyDictionary> _controllerCtors; - public static Dictionary ValidControllerTypes - { - get + public static IReadOnlyDictionary> ControllerCtors => _controllerCtors + ??= new Dictionary> { - if (_controllerTypes == null) - { - _controllerTypes = Emulation.Cores.ReflectionCache.Types - .Where(t => typeof(IPort).IsAssignableFrom(t)) - .Where(t => !t.IsAbstract && !t.IsInterface) - .ToDictionary(tkey => tkey.DisplayName()); - } - - return _controllerTypes; - } - } + [typeof(StandardControls).DisplayName()] = portNum => new StandardControls(portNum) + }; public static string DefaultControllerName => typeof(StandardControls).DisplayName(); } diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawkLink3x/GBHawkLink3xControllerDeck.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawkLink3x/GBHawkLink3xControllerDeck.cs index 4970acad31..e477022c6f 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawkLink3x/GBHawkLink3xControllerDeck.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawkLink3x/GBHawkLink3xControllerDeck.cs @@ -12,24 +12,15 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawkLink3x { public GBHawkLink3xControllerDeck(string controller1Name, string controller2Name, string controller3Name) { - if (!ValidControllerTypes.ContainsKey(controller1Name)) - { - throw new InvalidOperationException("Invalid controller type: " + controller1Name); - } - - if (!ValidControllerTypes.ContainsKey(controller2Name)) - { - throw new InvalidOperationException("Invalid controller type: " + controller2Name); - } - - if (!ValidControllerTypes.ContainsKey(controller3Name)) - { - throw new InvalidOperationException("Invalid controller type: " + controller3Name); - } - - Port1 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller1Name], 1); - Port2 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller2Name], 2); - Port3 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller3Name], 3); + Port1 = ControllerCtors.TryGetValue(controller1Name, out var ctor1) + ? ctor1(1) + : throw new InvalidOperationException($"Invalid controller type: {controller1Name}"); + Port2 = ControllerCtors.TryGetValue(controller2Name, out var ctor2) + ? ctor2(2) + : throw new InvalidOperationException($"Invalid controller type: {controller2Name}"); + Port3 = ControllerCtors.TryGetValue(controller3Name, out var ctor3) + ? ctor3(3) + : throw new InvalidOperationException($"Invalid controller type: {controller3Name}"); Definition = new ControllerDefinition { @@ -80,23 +71,13 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawkLink3x private readonly IPort Port2; private readonly IPort Port3; - private static Dictionary _controllerTypes; + private static IReadOnlyDictionary> _controllerCtors; - public static Dictionary ValidControllerTypes - { - get + public static IReadOnlyDictionary> ControllerCtors => _controllerCtors + ??= new Dictionary> { - if (_controllerTypes == null) - { - _controllerTypes = Emulation.Cores.ReflectionCache.Types - .Where(t => typeof(IPort).IsAssignableFrom(t)) - .Where(t => !t.IsAbstract && !t.IsInterface) - .ToDictionary(tkey => tkey.DisplayName()); - } - - return _controllerTypes; - } - } + [typeof(StandardControls).DisplayName()] = portNum => new StandardControls(portNum) + }; public static string DefaultControllerName => typeof(StandardControls).DisplayName(); } diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawkLink4x/GBHawkLink4xControllerDeck.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawkLink4x/GBHawkLink4xControllerDeck.cs index 2092ec5223..35b5f1609b 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawkLink4x/GBHawkLink4xControllerDeck.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBHawkLink4x/GBHawkLink4xControllerDeck.cs @@ -12,30 +12,18 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawkLink4x { public GBHawkLink4xControllerDeck(string controller1Name, string controller2Name, string controller3Name, string controller4Name) { - if (!ValidControllerTypes.ContainsKey(controller1Name)) - { - throw new InvalidOperationException("Invalid controller type: " + controller1Name); - } - - if (!ValidControllerTypes.ContainsKey(controller2Name)) - { - throw new InvalidOperationException("Invalid controller type: " + controller2Name); - } - - if (!ValidControllerTypes.ContainsKey(controller3Name)) - { - throw new InvalidOperationException("Invalid controller type: " + controller3Name); - } - - if (!ValidControllerTypes.ContainsKey(controller4Name)) - { - throw new InvalidOperationException("Invalid controller type: " + controller4Name); - } - - Port1 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller1Name], 1); - Port2 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller2Name], 2); - Port3 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller3Name], 3); - Port4 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller3Name], 4); + Port1 = ControllerCtors.TryGetValue(controller1Name, out var ctor1) + ? ctor1(1) + : throw new InvalidOperationException($"Invalid controller type: {controller1Name}"); + Port2 = ControllerCtors.TryGetValue(controller2Name, out var ctor2) + ? ctor2(2) + : throw new InvalidOperationException($"Invalid controller type: {controller2Name}"); + Port3 = ControllerCtors.TryGetValue(controller3Name, out var ctor3) + ? ctor3(3) + : throw new InvalidOperationException($"Invalid controller type: {controller3Name}"); + Port4 = ControllerCtors.TryGetValue(controller4Name, out var ctor4) + ? ctor4(4) + : throw new InvalidOperationException($"Invalid controller type: {controller4Name}"); Definition = new ControllerDefinition { @@ -98,23 +86,13 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawkLink4x private readonly IPort Port3; private readonly IPort Port4; - private static Dictionary _controllerTypes; + private static IReadOnlyDictionary> _controllerCtors; - public static Dictionary ValidControllerTypes - { - get + public static IReadOnlyDictionary> ControllerCtors => _controllerCtors + ??= new Dictionary> { - if (_controllerTypes == null) - { - _controllerTypes = Emulation.Cores.ReflectionCache.Types - .Where(t => typeof(IPort).IsAssignableFrom(t)) - .Where(t => !t.IsAbstract && !t.IsInterface) - .ToDictionary(tkey => tkey.DisplayName()); - } - - return _controllerTypes; - } - } + [typeof(StandardControls).DisplayName()] = portNum => new StandardControls(portNum) + }; public static string DefaultControllerName => typeof(StandardControls).DisplayName(); } diff --git a/src/BizHawk.Emulation.Cores/Consoles/Sega/GGHawkLink/GGHawkLinkControllerDeck.cs b/src/BizHawk.Emulation.Cores/Consoles/Sega/GGHawkLink/GGHawkLinkControllerDeck.cs index 398b3f4fee..e2649e95eb 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Sega/GGHawkLink/GGHawkLinkControllerDeck.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Sega/GGHawkLink/GGHawkLinkControllerDeck.cs @@ -12,18 +12,12 @@ namespace BizHawk.Emulation.Cores.Sega.GGHawkLink { public GGHawkLinkControllerDeck(string controller1Name, string controller2Name) { - if (!ValidControllerTypes.ContainsKey(controller1Name)) - { - throw new InvalidOperationException("Invalid controller type: " + controller1Name); - } - - if (!ValidControllerTypes.ContainsKey(controller2Name)) - { - throw new InvalidOperationException("Invalid controller type: " + controller2Name); - } - - Port1 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller1Name], 1); - Port2 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller2Name], 2); + Port1 = ControllerCtors.TryGetValue(controller1Name, out var ctor1) + ? ctor1(1) + : throw new InvalidOperationException($"Invalid controller type: {controller1Name}"); + Port2 = ControllerCtors.TryGetValue(controller2Name, out var ctor2) + ? ctor2(2) + : throw new InvalidOperationException($"Invalid controller type: {controller2Name}"); Definition = new ControllerDefinition { @@ -61,23 +55,13 @@ namespace BizHawk.Emulation.Cores.Sega.GGHawkLink private readonly IPort Port1; private readonly IPort Port2; - private static Dictionary _controllerTypes; + private static IReadOnlyDictionary> _controllerCtors; - public static Dictionary ValidControllerTypes - { - get + public static IReadOnlyDictionary> ControllerCtors => _controllerCtors + ??= new Dictionary> { - if (_controllerTypes == null) - { - _controllerTypes = Emulation.Cores.ReflectionCache.Types - .Where(t => typeof(IPort).IsAssignableFrom(t)) - .Where(t => !t.IsAbstract && !t.IsInterface) - .ToDictionary(tkey => tkey.DisplayName()); - } - - return _controllerTypes; - } - } + [typeof(StandardControls).DisplayName()] = portNum => new StandardControls(portNum) + }; public static string DefaultControllerName => typeof(StandardControls).DisplayName(); }