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)
This commit is contained in:
YoshiRulz 2020-09-08 00:01:24 +10:00
parent 329a7de8b8
commit d170972e3c
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
17 changed files with 147 additions and 329 deletions

View File

@ -1,5 +1,4 @@
using System; using System;
using System.Linq;
using System.Windows.Forms; using System.Windows.Forms;
using BizHawk.Emulation.Cores.Atari.A7800Hawk; using BizHawk.Emulation.Cores.Atari.A7800Hawk;
@ -22,9 +21,7 @@ namespace BizHawk.Client.EmuHawk
private void IntvControllerSettings_Load(object sender, EventArgs e) private void IntvControllerSettings_Load(object sender, EventArgs e)
{ {
var possibleControllers = A7800HawkControllerDeck.ValidControllerTypes.Select(t => t.Key); foreach (var val in A7800HawkControllerDeck.ControllerCtors.Keys)
foreach (var val in possibleControllers)
{ {
Port1ComboBox.Items.Add(val); Port1ComboBox.Items.Add(val);
Port2ComboBox.Items.Add(val); Port2ComboBox.Items.Add(val);

View File

@ -1,5 +1,4 @@
using System; using System;
using System.Linq;
using System.Windows.Forms; using System.Windows.Forms;
using BizHawk.Emulation.Cores.ColecoVision; using BizHawk.Emulation.Cores.ColecoVision;
@ -22,9 +21,7 @@ namespace BizHawk.Client.EmuHawk
private void ColecoControllerSettings_Load(object sender, EventArgs e) private void ColecoControllerSettings_Load(object sender, EventArgs e)
{ {
var possibleControllers = ColecoVisionControllerDeck.ValidControllerTypes.Select(t => t.Key); foreach (var val in ColecoVisionControllerDeck.ControllerCtors.Keys)
foreach (var val in possibleControllers)
{ {
Port1ComboBox.Items.Add(val); Port1ComboBox.Items.Add(val);
Port2ComboBox.Items.Add(val); Port2ComboBox.Items.Add(val);

View File

@ -1,5 +1,4 @@
using System; using System;
using System.Linq;
using System.Windows.Forms; using System.Windows.Forms;
using BizHawk.Emulation.Cores.Intellivision; using BizHawk.Emulation.Cores.Intellivision;
@ -22,9 +21,7 @@ namespace BizHawk.Client.EmuHawk
private void IntvControllerSettings_Load(object sender, EventArgs e) private void IntvControllerSettings_Load(object sender, EventArgs e)
{ {
var possibleControllers = IntellivisionControllerDeck.ValidControllerTypes.Select(t => t.Key); foreach (var val in IntellivisionControllerDeck.ControllerCtors.Keys)
foreach (var val in possibleControllers)
{ {
Port1ComboBox.Items.Add(val); Port1ComboBox.Items.Add(val);
Port2ComboBox.Items.Add(val); Port2ComboBox.Items.Add(val);

View File

@ -3,27 +3,17 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using BizHawk.Common; using BizHawk.Common;
using BizHawk.Common.ReflectionExtensions;
using BizHawk.Emulation.Common; using BizHawk.Emulation.Common;
namespace BizHawk.Emulation.Cores.Atari.Atari2600 namespace BizHawk.Emulation.Cores.Atari.Atari2600
{ {
public class Atari2600ControllerDeck 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) public Atari2600ControllerDeck(Atari2600ControllerTypes controller1, Atari2600ControllerTypes controller2)
{ {
Port1 = (IPort)Activator.CreateInstance(Implementors[(int)controller1], 1); Port1 = ControllerCtors[controller1](1);
Port2 = (IPort)Activator.CreateInstance(Implementors[(int)controller2], 2); Port2 = ControllerCtors[controller1](2);
Definition = new ControllerDefinition Definition = new ControllerDefinition
{ {
@ -77,24 +67,17 @@ namespace BizHawk.Emulation.Cores.Atari.Atari2600
private readonly IPort Port1; private readonly IPort Port1;
private readonly IPort Port2; private readonly IPort Port2;
private static Dictionary<string, Type> _controllerTypes; private static IReadOnlyDictionary<Atari2600ControllerTypes, Func<int, IPort>> _controllerCtors;
public static Dictionary<string, Type> ValidControllerTypes public static IReadOnlyDictionary<Atari2600ControllerTypes, Func<int, IPort>> ControllerCtors => _controllerCtors
??= new Dictionary<Atari2600ControllerTypes, Func<int, IPort>>
{ {
get [Atari2600ControllerTypes.Unplugged] = portNum => new UnpluggedController(portNum),
{ [Atari2600ControllerTypes.Joystick] = portNum => new StandardController(portNum),
if (_controllerTypes == null) [Atari2600ControllerTypes.Paddle] = portNum => new PaddleController(portNum),
{ [Atari2600ControllerTypes.BoostGrip] = portNum => new BoostGripController(portNum),
_controllerTypes = Emulation.Cores.ReflectionCache.Types [Atari2600ControllerTypes.Driving] = portNum => new DrivingController(portNum),
.Where(t => typeof(IPort).IsAssignableFrom(t)) [Atari2600ControllerTypes.Keyboard] = portNum => new KeyboardController(portNum)
.Where(t => !t.IsAbstract && !t.IsInterface) };
.ToDictionary(tkey => tkey.DisplayName());
}
return _controllerTypes;
}
}
public static string DefaultControllerName => typeof(StandardController).DisplayName();
} }
} }

View File

@ -66,7 +66,7 @@ namespace BizHawk.Emulation.Cores.Atari.A7800Hawk
get => _port1; get => _port1;
set set
{ {
if (!A7800HawkControllerDeck.ValidControllerTypes.ContainsKey(value)) if (!A7800HawkControllerDeck.ControllerCtors.ContainsKey(value))
{ {
throw new InvalidOperationException("Invalid controller type: " + value); throw new InvalidOperationException("Invalid controller type: " + value);
} }
@ -81,7 +81,7 @@ namespace BizHawk.Emulation.Cores.Atari.A7800Hawk
get => _port2; get => _port2;
set set
{ {
if (!A7800HawkControllerDeck.ValidControllerTypes.ContainsKey(value)) if (!A7800HawkControllerDeck.ControllerCtors.ContainsKey(value))
{ {
throw new InvalidOperationException("Invalid controller type: " + value); throw new InvalidOperationException("Invalid controller type: " + value);
} }

View File

@ -12,18 +12,12 @@ namespace BizHawk.Emulation.Cores.Atari.A7800Hawk
{ {
public A7800HawkControllerDeck(string controller1Name, string controller2Name) public A7800HawkControllerDeck(string controller1Name, string controller2Name)
{ {
if (!ValidControllerTypes.ContainsKey(controller1Name)) Port1 = ControllerCtors.TryGetValue(controller1Name, out var ctor1)
{ ? ctor1(1)
throw new InvalidOperationException("Invalid controller type: " + controller1Name); : throw new InvalidOperationException($"Invalid controller type: {controller1Name}");
} Port2 = ControllerCtors.TryGetValue(controller2Name, out var ctor2)
? ctor2(2)
if (!ValidControllerTypes.ContainsKey(controller2Name)) : throw new InvalidOperationException($"Invalid controller type: {controller2Name}");
{
throw new InvalidOperationException("Invalid controller type: " + controller2Name);
}
Port1 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller1Name], 1);
Port2 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller2Name], 2);
Definition = new ControllerDefinition Definition = new ControllerDefinition
{ {
@ -113,23 +107,16 @@ namespace BizHawk.Emulation.Cores.Atari.A7800Hawk
private readonly IPort Port1; private readonly IPort Port1;
private readonly IPort Port2; private readonly IPort Port2;
private static Dictionary<string, Type> _controllerTypes; private static IReadOnlyDictionary<string, Func<int, IPort>> _controllerCtors;
public static Dictionary<string, Type> ValidControllerTypes public static IReadOnlyDictionary<string, Func<int, IPort>> ControllerCtors => _controllerCtors
??= new Dictionary<string, Func<int, IPort>>
{ {
get [typeof(UnpluggedController).DisplayName()] = portNum => new UnpluggedController(portNum),
{ [typeof(StandardController).DisplayName()] = portNum => new StandardController(portNum),
if (_controllerTypes == null) [typeof(ProLineController).DisplayName()] = portNum => new ProLineController(portNum),
{ [typeof(LightGunController).DisplayName()] = portNum => new LightGunController(portNum)
_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(); public static string DefaultControllerName => typeof(StandardController).DisplayName();
} }

View File

@ -12,18 +12,12 @@ namespace BizHawk.Emulation.Cores.ColecoVision
{ {
public ColecoVisionControllerDeck(string controller1Name, string controller2Name) public ColecoVisionControllerDeck(string controller1Name, string controller2Name)
{ {
if (!ValidControllerTypes.ContainsKey(controller1Name)) Port1 = ControllerCtors.TryGetValue(controller1Name, out var ctor1)
{ ? ctor1(1)
throw new InvalidOperationException("Invalid controller type: " + controller1Name); : throw new InvalidOperationException($"Invalid controller type: {controller1Name}");
} Port2 = ControllerCtors.TryGetValue(controller2Name, out var ctor2)
? ctor2(2)
if (!ValidControllerTypes.ContainsKey(controller2Name)) : throw new InvalidOperationException($"Invalid controller type: {controller2Name}");
{
throw new InvalidOperationException("Invalid controller type: " + controller2Name);
}
Port1 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller1Name], 1);
Port2 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller2Name], 2);
Definition = new ControllerDefinition Definition = new ControllerDefinition
{ {
@ -79,23 +73,16 @@ namespace BizHawk.Emulation.Cores.ColecoVision
public IPort Port1 { get; } public IPort Port1 { get; }
public IPort Port2 { get; } public IPort Port2 { get; }
private static Dictionary<string, Type> _controllerTypes = null; private static IReadOnlyDictionary<string, Func<int, IPort>> _controllerCtors;
public static Dictionary<string, Type> ValidControllerTypes public static IReadOnlyDictionary<string, Func<int, IPort>> ControllerCtors => _controllerCtors
??= new Dictionary<string, Func<int, IPort>>
{ {
get [typeof(UnpluggedController).DisplayName()] = portNum => new UnpluggedController(portNum),
{ [typeof(StandardController).DisplayName()] = portNum => new StandardController(portNum),
if (_controllerTypes == null) [typeof(ColecoTurboController).DisplayName()] = portNum => new ColecoTurboController(portNum),
{ [typeof(ColecoSuperActionController).DisplayName()] = portNum => new ColecoSuperActionController(portNum)
_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(); public static string DefaultControllerName => typeof(StandardController).DisplayName();
} }

View File

@ -57,7 +57,7 @@ namespace BizHawk.Emulation.Cores.ColecoVision
get => _port1; get => _port1;
set set
{ {
if (!ColecoVisionControllerDeck.ValidControllerTypes.ContainsKey(value)) if (!ColecoVisionControllerDeck.ControllerCtors.ContainsKey(value))
{ {
throw new InvalidOperationException("Invalid controller type: " + value); throw new InvalidOperationException("Invalid controller type: " + value);
} }
@ -72,7 +72,7 @@ namespace BizHawk.Emulation.Cores.ColecoVision
get => _port2; get => _port2;
set set
{ {
if (!ColecoVisionControllerDeck.ValidControllerTypes.ContainsKey(value)) if (!ColecoVisionControllerDeck.ControllerCtors.ContainsKey(value))
{ {
throw new InvalidOperationException("Invalid controller type: " + value); throw new InvalidOperationException("Invalid controller type: " + value);
} }

View File

@ -12,18 +12,12 @@ namespace BizHawk.Emulation.Cores.Consoles.Vectrex
{ {
public VectrexHawkControllerDeck(string controller1Name, string controller2Name) public VectrexHawkControllerDeck(string controller1Name, string controller2Name)
{ {
if (!ValidControllerTypes.ContainsKey(controller1Name)) Port1 = ControllerCtors.TryGetValue(controller1Name, out var ctor1)
{ ? ctor1(1)
throw new InvalidOperationException("Invalid controller type: " + controller1Name); : throw new InvalidOperationException($"Invalid controller type: {controller1Name}");
} Port2 = ControllerCtors.TryGetValue(controller2Name, out var ctor2)
? ctor2(2)
if (!ValidControllerTypes.ContainsKey(controller2Name)) : throw new InvalidOperationException($"Invalid controller type: {controller2Name}");
{
throw new InvalidOperationException("Invalid controller type: " + controller2Name);
}
Port1 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller1Name], 1);
Port2 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller2Name], 2);
Definition = new ControllerDefinition Definition = new ControllerDefinition
{ {
@ -68,23 +62,14 @@ namespace BizHawk.Emulation.Cores.Consoles.Vectrex
private readonly IPort Port1; private readonly IPort Port1;
private readonly IPort Port2; private readonly IPort Port2;
private static Dictionary<string, Type> _controllerTypes; private static IReadOnlyDictionary<string, Func<int, IPort>> _controllerCtors;
public static Dictionary<string, Type> ValidControllerTypes public static IReadOnlyDictionary<string, Func<int, IPort>> ControllerCtors => _controllerCtors
??= new Dictionary<string, Func<int, IPort>>
{ {
get [typeof(StandardControls).DisplayName()] = portNum => new StandardControls(portNum),
{ [typeof(AnalogControls).DisplayName()] = portNum => new AnalogControls(portNum)
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(StandardControls).DisplayName(); public static string DefaultControllerName => typeof(StandardControls).DisplayName();
} }

View File

@ -12,18 +12,12 @@ namespace BizHawk.Emulation.Cores.Intellivision
{ {
public IntellivisionControllerDeck(string controller1Name, string controller2Name) public IntellivisionControllerDeck(string controller1Name, string controller2Name)
{ {
if (!ValidControllerTypes.ContainsKey(controller1Name)) Port1 = ControllerCtors.TryGetValue(controller1Name, out var ctor1)
{ ? ctor1(1)
throw new InvalidOperationException("Invalid controller type: " + controller1Name); : throw new InvalidOperationException($"Invalid controller type: {controller1Name}");
} Port2 = ControllerCtors.TryGetValue(controller2Name, out var ctor2)
? ctor2(2)
if (!ValidControllerTypes.ContainsKey(controller2Name)) : throw new InvalidOperationException($"Invalid controller type: {controller2Name}");
{
throw new InvalidOperationException("Invalid controller type: " + controller2Name);
}
Port1 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller1Name], 1);
Port2 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller2Name], 2);
Definition = new ControllerDefinition Definition = new ControllerDefinition
{ {
@ -68,23 +62,15 @@ namespace BizHawk.Emulation.Cores.Intellivision
private readonly IPort Port1; private readonly IPort Port1;
private readonly IPort Port2; private readonly IPort Port2;
private static Dictionary<string, Type> _controllerTypes; private static IReadOnlyDictionary<string, Func<int, IPort>> _controllerCtors;
public static Dictionary<string, Type> ValidControllerTypes public static IReadOnlyDictionary<string, Func<int, IPort>> ControllerCtors => _controllerCtors
??= new Dictionary<string, Func<int, IPort>>
{ {
get [typeof(UnpluggedController).DisplayName()] = portNum => new UnpluggedController(portNum),
{ [typeof(StandardController).DisplayName()] = portNum => new StandardController(portNum),
if (_controllerTypes == null) [typeof(FakeAnalogController).DisplayName()] = portNum => new FakeAnalogController(portNum)
{ };
_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(FakeAnalogController).DisplayName(); public static string DefaultControllerName => typeof(FakeAnalogController).DisplayName();
} }

View File

@ -53,7 +53,7 @@ namespace BizHawk.Emulation.Cores.Intellivision
get => _port1; get => _port1;
set set
{ {
if (!IntellivisionControllerDeck.ValidControllerTypes.ContainsKey(value)) if (!IntellivisionControllerDeck.ControllerCtors.ContainsKey(value))
{ {
throw new InvalidOperationException("Invalid controller type: " + value); throw new InvalidOperationException("Invalid controller type: " + value);
} }
@ -68,7 +68,7 @@ namespace BizHawk.Emulation.Cores.Intellivision
get => _port2; get => _port2;
set set
{ {
if (!IntellivisionControllerDeck.ValidControllerTypes.ContainsKey(value)) if (!IntellivisionControllerDeck.ControllerCtors.ContainsKey(value))
{ {
throw new InvalidOperationException("Invalid controller type: " + value); throw new InvalidOperationException("Invalid controller type: " + value);
} }

View File

@ -12,18 +12,12 @@ namespace BizHawk.Emulation.Cores.Consoles.O2Hawk
{ {
public O2HawkControllerDeck(string controller1Name, string controller2Name, bool is_G7400) public O2HawkControllerDeck(string controller1Name, string controller2Name, bool is_G7400)
{ {
if (!ValidControllerTypes.ContainsKey(controller1Name)) Port1 = ControllerCtors.TryGetValue(controller1Name, out var ctor1)
{ ? ctor1(1)
throw new InvalidOperationException("Invalid controller type: " + controller1Name); : throw new InvalidOperationException($"Invalid controller type: {controller1Name}");
} Port2 = ControllerCtors.TryGetValue(controller2Name, out var ctor2)
? ctor2(2)
if (!ValidControllerTypes.ContainsKey(controller2Name)) : throw new InvalidOperationException($"Invalid controller type: {controller2Name}");
{
throw new InvalidOperationException("Invalid controller type: " + controller2Name);
}
Port1 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller1Name], 1);
Port2 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller2Name], 2);
if (is_G7400) if (is_G7400)
{ {
@ -94,23 +88,13 @@ namespace BizHawk.Emulation.Cores.Consoles.O2Hawk
private readonly IPort Port1, Port2; private readonly IPort Port1, Port2;
private static Dictionary<string, Type> _controllerTypes; private static IReadOnlyDictionary<string, Func<int, IPort>> _controllerCtors;
public static Dictionary<string, Type> ValidControllerTypes public static IReadOnlyDictionary<string, Func<int, IPort>> ControllerCtors => _controllerCtors
??= new Dictionary<string, Func<int, IPort>>
{ {
get [typeof(StandardControls).DisplayName()] = portNum => new StandardControls(portNum)
{ };
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(StandardControls).DisplayName(); public static string DefaultControllerName => typeof(StandardControls).DisplayName();
} }

View File

@ -12,12 +12,9 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk
{ {
public GBHawkControllerDeck(string controller1Name) public GBHawkControllerDeck(string controller1Name)
{ {
if (!ValidControllerTypes.ContainsKey(controller1Name)) Port1 = ControllerCtors.TryGetValue(controller1Name, out var ctor1)
{ ? ctor1(1)
throw new InvalidOperationException("Invalid controller type: " + controller1Name); : throw new InvalidOperationException($"Invalid controller type: {controller1Name}");
}
Port1 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller1Name], 1);
Definition = new ControllerDefinition Definition = new ControllerDefinition
{ {
@ -55,23 +52,14 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawk
private readonly IPort Port1; private readonly IPort Port1;
private static Dictionary<string, Type> _controllerTypes; private static IReadOnlyDictionary<string, Func<int, IPort>> _controllerCtors;
public static Dictionary<string, Type> ValidControllerTypes public static IReadOnlyDictionary<string, Func<int, IPort>> ControllerCtors => _controllerCtors
??= new Dictionary<string, Func<int, IPort>>
{ {
get [typeof(StandardControls).DisplayName()] = portNum => new StandardControls(portNum),
{ [typeof(StandardTilt).DisplayName()] = portNum => new StandardTilt(portNum)
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(StandardControls).DisplayName(); public static string DefaultControllerName => typeof(StandardControls).DisplayName();
} }

View File

@ -12,18 +12,12 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawkLink
{ {
public GBHawkLinkControllerDeck(string controller1Name, string controller2Name) public GBHawkLinkControllerDeck(string controller1Name, string controller2Name)
{ {
if (!ValidControllerTypes.ContainsKey(controller1Name)) Port1 = ControllerCtors.TryGetValue(controller1Name, out var ctor1)
{ ? ctor1(1)
throw new InvalidOperationException("Invalid controller type: " + controller1Name); : throw new InvalidOperationException($"Invalid controller type: {controller1Name}");
} Port2 = ControllerCtors.TryGetValue(controller2Name, out var ctor2)
? ctor2(2)
if (!ValidControllerTypes.ContainsKey(controller2Name)) : throw new InvalidOperationException($"Invalid controller type: {controller2Name}");
{
throw new InvalidOperationException("Invalid controller type: " + controller2Name);
}
Port1 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller1Name], 1);
Port2 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller2Name], 2);
Definition = new ControllerDefinition Definition = new ControllerDefinition
{ {
@ -61,23 +55,13 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawkLink
private readonly IPort Port1; private readonly IPort Port1;
private readonly IPort Port2; private readonly IPort Port2;
private static Dictionary<string, Type> _controllerTypes; private static IReadOnlyDictionary<string, Func<int, IPort>> _controllerCtors;
public static Dictionary<string, Type> ValidControllerTypes public static IReadOnlyDictionary<string, Func<int, IPort>> ControllerCtors => _controllerCtors
??= new Dictionary<string, Func<int, IPort>>
{ {
get [typeof(StandardControls).DisplayName()] = portNum => new StandardControls(portNum)
{ };
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(StandardControls).DisplayName(); public static string DefaultControllerName => typeof(StandardControls).DisplayName();
} }

View File

@ -12,24 +12,15 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawkLink3x
{ {
public GBHawkLink3xControllerDeck(string controller1Name, string controller2Name, string controller3Name) public GBHawkLink3xControllerDeck(string controller1Name, string controller2Name, string controller3Name)
{ {
if (!ValidControllerTypes.ContainsKey(controller1Name)) Port1 = ControllerCtors.TryGetValue(controller1Name, out var ctor1)
{ ? ctor1(1)
throw new InvalidOperationException("Invalid controller type: " + controller1Name); : throw new InvalidOperationException($"Invalid controller type: {controller1Name}");
} Port2 = ControllerCtors.TryGetValue(controller2Name, out var ctor2)
? ctor2(2)
if (!ValidControllerTypes.ContainsKey(controller2Name)) : throw new InvalidOperationException($"Invalid controller type: {controller2Name}");
{ Port3 = ControllerCtors.TryGetValue(controller3Name, out var ctor3)
throw new InvalidOperationException("Invalid controller type: " + controller2Name); ? ctor3(3)
} : throw new InvalidOperationException($"Invalid controller type: {controller3Name}");
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);
Definition = new ControllerDefinition Definition = new ControllerDefinition
{ {
@ -80,23 +71,13 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawkLink3x
private readonly IPort Port2; private readonly IPort Port2;
private readonly IPort Port3; private readonly IPort Port3;
private static Dictionary<string, Type> _controllerTypes; private static IReadOnlyDictionary<string, Func<int, IPort>> _controllerCtors;
public static Dictionary<string, Type> ValidControllerTypes public static IReadOnlyDictionary<string, Func<int, IPort>> ControllerCtors => _controllerCtors
??= new Dictionary<string, Func<int, IPort>>
{ {
get [typeof(StandardControls).DisplayName()] = portNum => new StandardControls(portNum)
{ };
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(StandardControls).DisplayName(); public static string DefaultControllerName => typeof(StandardControls).DisplayName();
} }

View File

@ -12,30 +12,18 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawkLink4x
{ {
public GBHawkLink4xControllerDeck(string controller1Name, string controller2Name, string controller3Name, string controller4Name) public GBHawkLink4xControllerDeck(string controller1Name, string controller2Name, string controller3Name, string controller4Name)
{ {
if (!ValidControllerTypes.ContainsKey(controller1Name)) Port1 = ControllerCtors.TryGetValue(controller1Name, out var ctor1)
{ ? ctor1(1)
throw new InvalidOperationException("Invalid controller type: " + controller1Name); : throw new InvalidOperationException($"Invalid controller type: {controller1Name}");
} Port2 = ControllerCtors.TryGetValue(controller2Name, out var ctor2)
? ctor2(2)
if (!ValidControllerTypes.ContainsKey(controller2Name)) : throw new InvalidOperationException($"Invalid controller type: {controller2Name}");
{ Port3 = ControllerCtors.TryGetValue(controller3Name, out var ctor3)
throw new InvalidOperationException("Invalid controller type: " + controller2Name); ? ctor3(3)
} : throw new InvalidOperationException($"Invalid controller type: {controller3Name}");
Port4 = ControllerCtors.TryGetValue(controller4Name, out var ctor4)
if (!ValidControllerTypes.ContainsKey(controller3Name)) ? ctor4(4)
{ : throw new InvalidOperationException($"Invalid controller type: {controller4Name}");
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);
Definition = new ControllerDefinition Definition = new ControllerDefinition
{ {
@ -98,23 +86,13 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBHawkLink4x
private readonly IPort Port3; private readonly IPort Port3;
private readonly IPort Port4; private readonly IPort Port4;
private static Dictionary<string, Type> _controllerTypes; private static IReadOnlyDictionary<string, Func<int, IPort>> _controllerCtors;
public static Dictionary<string, Type> ValidControllerTypes public static IReadOnlyDictionary<string, Func<int, IPort>> ControllerCtors => _controllerCtors
??= new Dictionary<string, Func<int, IPort>>
{ {
get [typeof(StandardControls).DisplayName()] = portNum => new StandardControls(portNum)
{ };
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(StandardControls).DisplayName(); public static string DefaultControllerName => typeof(StandardControls).DisplayName();
} }

View File

@ -12,18 +12,12 @@ namespace BizHawk.Emulation.Cores.Sega.GGHawkLink
{ {
public GGHawkLinkControllerDeck(string controller1Name, string controller2Name) public GGHawkLinkControllerDeck(string controller1Name, string controller2Name)
{ {
if (!ValidControllerTypes.ContainsKey(controller1Name)) Port1 = ControllerCtors.TryGetValue(controller1Name, out var ctor1)
{ ? ctor1(1)
throw new InvalidOperationException("Invalid controller type: " + controller1Name); : throw new InvalidOperationException($"Invalid controller type: {controller1Name}");
} Port2 = ControllerCtors.TryGetValue(controller2Name, out var ctor2)
? ctor2(2)
if (!ValidControllerTypes.ContainsKey(controller2Name)) : throw new InvalidOperationException($"Invalid controller type: {controller2Name}");
{
throw new InvalidOperationException("Invalid controller type: " + controller2Name);
}
Port1 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller1Name], 1);
Port2 = (IPort)Activator.CreateInstance(ValidControllerTypes[controller2Name], 2);
Definition = new ControllerDefinition Definition = new ControllerDefinition
{ {
@ -61,23 +55,13 @@ namespace BizHawk.Emulation.Cores.Sega.GGHawkLink
private readonly IPort Port1; private readonly IPort Port1;
private readonly IPort Port2; private readonly IPort Port2;
private static Dictionary<string, Type> _controllerTypes; private static IReadOnlyDictionary<string, Func<int, IPort>> _controllerCtors;
public static Dictionary<string, Type> ValidControllerTypes public static IReadOnlyDictionary<string, Func<int, IPort>> ControllerCtors => _controllerCtors
??= new Dictionary<string, Func<int, IPort>>
{ {
get [typeof(StandardControls).DisplayName()] = portNum => new StandardControls(portNum)
{ };
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(StandardControls).DisplayName(); public static string DefaultControllerName => typeof(StandardControls).DisplayName();
} }