diff --git a/OpenRA.Game/Exts.cs b/OpenRA.Game/Exts.cs index b8bcb8528a..4046a91a3e 100644 --- a/OpenRA.Game/Exts.cs +++ b/OpenRA.Game/Exts.cs @@ -676,27 +676,4 @@ namespace OpenRA public ReadOnlySpan Current { get; private set; } } - - public static class Enum - { - 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; - return false; - } - - value = (T)Enum.Parse(typeof(T), s, ignoreCase); - - return true; - } - } } diff --git a/OpenRA.Game/Input/Hotkey.cs b/OpenRA.Game/Input/Hotkey.cs index 63d60d9460..efa4cf13af 100644 --- a/OpenRA.Game/Input/Hotkey.cs +++ b/OpenRA.Game/Input/Hotkey.cs @@ -27,25 +27,21 @@ namespace OpenRA public static bool TryParse(string s, out Hotkey result) { result = Invalid; - if (string.IsNullOrWhiteSpace(s)) + if (s == null) return false; - var parts = s.Split(' '); + Span ranges = stackalloc Range[2]; + var span = s.AsSpan(); + var count = span.Split(ranges, ' '); + if (count == 0) + return false; - if (!Enum.TryParse(parts[0], true, out var key)) - { - if (!int.TryParse(parts[0], out var c)) - return false; - key = (Keycode)c; - } + if (!Enum.TryParse(span[ranges[0]], true, out Keycode key)) + return false; var mods = Modifiers.None; - if (parts.Length >= 2) - { - var modString = s[s.IndexOf(' ')..]; - if (!Enum.TryParse(modString, true, out mods)) - return false; - } + if (count == 2 && !Enum.TryParse(span[ranges[1]], true, out mods)) + return false; result = new Hotkey(key, mods); return true; diff --git a/OpenRA.Game/Map/MapCache.cs b/OpenRA.Game/Map/MapCache.cs index 05905642ed..404f25ff73 100644 --- a/OpenRA.Game/Map/MapCache.cs +++ b/OpenRA.Game/Map/MapCache.cs @@ -93,7 +93,7 @@ namespace OpenRA { var name = kv.Key; var classification = string.IsNullOrEmpty(kv.Value) - ? MapClassification.Unknown : Enum.Parse(kv.Value); + ? MapClassification.Unknown : Enum.Parse(kv.Value); IReadOnlyPackage package; var optional = name.StartsWith('~'); diff --git a/OpenRA.Game/Support/VariableExpression.cs b/OpenRA.Game/Support/VariableExpression.cs index bbb3240915..02cf2de9db 100644 --- a/OpenRA.Game/Support/VariableExpression.cs +++ b/OpenRA.Game/Support/VariableExpression.cs @@ -212,9 +212,9 @@ namespace OpenRA.Support static IEnumerable CreateTokenTypeInfoEnumeration() { - for (var i = 0; i <= (int)TokenType.Invalid; i++) + for (var tt = TokenType.False; tt <= TokenType.Invalid; tt++) { - switch ((TokenType)i) + switch (tt) { case TokenType.Invalid: yield return new TokenTypeInfo("()", Precedence.Invalid); @@ -287,7 +287,7 @@ namespace OpenRA.Support continue; } - throw new InvalidProgramException($"CreateTokenTypeInfoEnumeration is missing a TokenTypeInfo entry for TokenType.{Enum.GetValues()[i]}"); + throw new InvalidProgramException($"CreateTokenTypeInfoEnumeration is missing a TokenTypeInfo entry for TokenType.{tt}"); } } @@ -741,8 +741,8 @@ namespace OpenRA.Support throw new InvalidProgramException( "Unable to convert " + - $"ExpressionType.{Enum.GetValues()[(int)fromType]} to " + - $"ExpressionType.{Enum.GetValues()[(int)toType]}"); + $"ExpressionType.{fromType} to " + + $"ExpressionType.{toType}"); } public Expression Pop(ExpressionType type) @@ -941,7 +941,7 @@ namespace OpenRA.Support default: throw new InvalidProgramException( "ConditionExpression.Compiler.Compile() is missing an expression builder for " + - $"TokenType.{Enum.GetValues()[(int)t.Type]}"); + $"TokenType.{t.Type}"); } } diff --git a/OpenRA.Mods.Common/Scripting/Properties/GeneralProperties.cs b/OpenRA.Mods.Common/Scripting/Properties/GeneralProperties.cs index ce6b2817c7..c22b06213e 100644 --- a/OpenRA.Mods.Common/Scripting/Properties/GeneralProperties.cs +++ b/OpenRA.Mods.Common/Scripting/Properties/GeneralProperties.cs @@ -9,6 +9,7 @@ */ #endregion +using System; using System.Linq; using Eluant; using OpenRA.Mods.Common.Activities; @@ -153,7 +154,7 @@ namespace OpenRA.Mods.Common.Scripting if (autotarget == null) return; - if (!Enum.TryParse(value, true, out var stance)) + if (!Enum.TryParse(value, true, out var stance)) throw new LuaException($"Unknown stance type '{value}'"); autotarget.SetStance(Self, stance); diff --git a/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs b/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs index 2e2191a0c6..c6495219bc 100644 --- a/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs +++ b/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs @@ -298,7 +298,7 @@ namespace OpenRA.Mods.Common.Server { lock (server.LobbyInfo) { - if (!Enum.TryParse(s, false, out var state)) + if (!Enum.TryParse(s, out var state)) { server.SendFluentMessageTo(conn, MalformedCommand, ["command", "state"]);