diff --git a/OpenRA.FileFormats/Exts.cs b/OpenRA.FileFormats/Exts.cs index 55523c95d9..ae878cb495 100755 --- a/OpenRA.FileFormats/Exts.cs +++ b/OpenRA.FileFormats/Exts.cs @@ -262,5 +262,22 @@ namespace OpenRA { public static T Parse(string s) { return (T)Enum.Parse(typeof(T), s); } public static T[] GetValues() { return (T[])Enum.GetValues(typeof(T)); } + + public static bool TryParse(string s, bool ignoreCase, out T value) + { + // The string may be a comma delimited list of values + var names = ignoreCase ? Enum.GetNames(typeof(T)).Select(x => x.ToLowerInvariant()) : Enum.GetNames(typeof(T)); + var values = ignoreCase ? s.Split(',').Select(x => x.Trim().ToLowerInvariant()) : s.Split(',').Select(x => x.Trim()); + + if (values.Any(x => !names.Contains(x))) + { + value = default(T); + return false; + } + + value = (T)Enum.Parse(typeof(T), s, ignoreCase); + + return true; + } } } diff --git a/OpenRA.FileFormats/Hotkey.cs b/OpenRA.FileFormats/Hotkey.cs index 2bbced90f5..691a78829e 100755 --- a/OpenRA.FileFormats/Hotkey.cs +++ b/OpenRA.FileFormats/Hotkey.cs @@ -25,43 +25,22 @@ namespace OpenRA public static bool TryParse(string s, out Hotkey result) { result = Invalid; - var parts = s.Split(' '); + + Keycode key; + if (!Enum.TryParse(parts[0], true, out key)) + return false; + + var mods = Modifiers.None; if (parts.Length >= 2) { - if (!Enum.GetNames(typeof(Keycode)).Contains(parts[0])) - return false; - var modString = s.Substring(s.IndexOf(' ')); - var modParts = modString.Split(',').Select(x => x.Trim()); - if (modParts.Any(p => !Enum.GetNames(typeof(Modifiers)).Contains(p))) + if (!Enum.TryParse(modString, true, out mods)) return false; - - var key = (Keycode)Enum.Parse(typeof(Keycode), parts[0]); - var mods = (Modifiers)Enum.Parse(typeof(Modifiers), modString); - - result = new Hotkey(key, mods); - return true; } - if (parts.Length == 1) - { - // HACK: Try parsing as a legacy key name - // This is a stop-gap solution to keep backwards - // compatibility while outside code is converted - var i = 0; - for (; i < (int)Keycode.LAST; i++) - if (KeycodeExts.DisplayString((Keycode)i) == parts[0]) - break; - - if (i < (int)Keycode.LAST) - { - result = new Hotkey((Keycode)i, Modifiers.None); - return true; - } - } - - return false; + result = new Hotkey(key, mods); + return true; } public static Hotkey FromKeyInput(KeyInput ki)