From 967c18aa34ca18f99262a67b8f452c23e70e2fdc Mon Sep 17 00:00:00 2001 From: nattthebear Date: Thu, 27 Apr 2017 18:28:55 -0400 Subject: [PATCH] linq linq baby --- .../ControllerDefinition.cs | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/BizHawk.Emulation.Common/Base Implementations/ControllerDefinition.cs b/BizHawk.Emulation.Common/Base Implementations/ControllerDefinition.cs index 5ad465de88..96fcc2e28b 100644 --- a/BizHawk.Emulation.Common/Base Implementations/ControllerDefinition.cs +++ b/BizHawk.Emulation.Common/Base Implementations/ControllerDefinition.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text.RegularExpressions; namespace BizHawk.Emulation.Common { @@ -190,23 +191,28 @@ namespace BizHawk.Emulation.Common return player; } + private static readonly Regex PlayerRegex = new Regex("^P(\\d) "); + // TODO: a more respectable logic here, and possibly per core implementation public int PlayerCount { get { - var list = FloatControls.Union(BoolButtons).ToList(); - if (list.Any(b => b.StartsWith("P8"))) { return 8; } - if (list.Any(b => b.StartsWith("P7"))) { return 7; } - if (list.Any(b => b.StartsWith("P6"))) { return 6; } - if (list.Any(b => b.StartsWith("P5"))) { return 5; } - if (list.Any(b => b.StartsWith("P4"))) { return 4; } - if (list.Any(b => b.StartsWith("P3"))) { return 3; } - if (list.Any(b => b.StartsWith("P2"))) { return 2; } - if (list.Any(b => b.StartsWith("P1"))) { return 1; } + var allNames = FloatControls.Concat(BoolButtons).ToList(); + var player = allNames + .Select(s => PlayerRegex.Match(s).Groups[1]) + .Where(group => group.Success) + .Select(group => group.Value[0] - '0') + .DefaultIfEmpty(0) + .Max(); + + if (player > 0) + { + return player; + } // Hack for things like gameboy/ti-83 as opposed to genesis with no controllers plugged in - if (list.Any(b => b.StartsWith("Up"))) + if (allNames.Any(b => b.StartsWith("Up"))) { return 1; }