Simplify Hotkey parsing.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user