From 73ed79e270d925cd371b7bce92e9b361d13c6871 Mon Sep 17 00:00:00 2001 From: adelikat Date: Tue, 24 Jun 2014 00:26:35 +0000 Subject: [PATCH] Virtualpads - remove system id switch and use reflection to find schemas with a matching SchemaAttribute of the current SystemId --- .../tools/VirtualPads/VirtualpadTool.cs | 46 ++++++------------- 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/BizHawk.Client.EmuHawk/tools/VirtualPads/VirtualpadTool.cs b/BizHawk.Client.EmuHawk/tools/VirtualPads/VirtualpadTool.cs index 417e268d8a..4d28afbc92 100644 --- a/BizHawk.Client.EmuHawk/tools/VirtualPads/VirtualpadTool.cs +++ b/BizHawk.Client.EmuHawk/tools/VirtualPads/VirtualpadTool.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Drawing; using System.Linq; +using System.Reflection; using System.Windows.Forms; using BizHawk.Client.Common; @@ -67,38 +68,21 @@ namespace BizHawk.Client.EmuHawk { ControllerBox.Controls.Clear(); - // TODO: be more clever than this - switch(Global.Emulator.SystemId) + var schemaType = Assembly + .GetExecutingAssembly() + .GetTypes() + .Where(t => typeof(IVirtualPadSchema) + .IsAssignableFrom(t) && t.GetCustomAttributes(false) + .OfType() + .Any()) + .FirstOrDefault(t => t.GetCustomAttributes(false) + .OfType() + .First().SystemId == Global.Emulator.SystemId); + + if (schemaType != null) { - case "NES": - ControllerBox.Controls.AddRange(new NesSchema().GetPads().ToArray()); - break; - case "N64": - ControllerBox.Controls.Add(new VirtualPad( - N64Schema.StandardController(1)) - { - Location = new Point(15, 15) - }); - break; - case "SMS": - case "SG": - case "GG": // TODO: test if all 3 of these are needed - ControllerBox.Controls.Add(new VirtualPad( - SmsSchema.StandardController(1)) - { - Location = new Point(15, 15) - }); - break; - case "PCE": - ControllerBox.Controls.AddRange(new PceSchema().GetPads().ToArray()); - break; - case "SNES": - ControllerBox.Controls.Add(new VirtualPad( - SnesSchema.StandardController(1)) - { - Location = new Point(15, 15) - }); - break; + var pads = (Activator.CreateInstance(schemaType) as IVirtualPadSchema).GetPads(); + ControllerBox.Controls.AddRange(pads.ToArray()); } }