diff --git a/OpenRA.Game/Settings.cs b/OpenRA.Game/Settings.cs index daa6fca16a..59e1963d3a 100644 --- a/OpenRA.Game/Settings.cs +++ b/OpenRA.Game/Settings.cs @@ -13,6 +13,7 @@ using System.Collections.Generic; using System.Drawing.Imaging; using System.IO; using System.Linq; +using System.Linq.Expressions; using OpenRA.Graphics; using OpenRA.Traits; @@ -227,22 +228,32 @@ namespace OpenRA public Hotkey SupportPower05Key = new Hotkey(Keycode.UNKNOWN, Modifiers.None); public Hotkey SupportPower06Key = new Hotkey(Keycode.UNKNOWN, Modifiers.None); + static readonly Func[] ProductionKeys = GetKeys(24, "Production"); + static readonly Func[] SupportPowerKeys = GetKeys(6, "SupportPower"); + + static Func[] GetKeys(int count, string prefix) + { + var keySettings = Expression.Parameter(typeof(KeySettings), "keySettings"); + return Exts.MakeArray(count, i => Expression.Lambda>( + Expression.Field(keySettings, "{0}{1:D2}Key".F(prefix, i + 1)), keySettings).Compile()); + } + public Hotkey GetProductionHotkey(int index) { - var field = GetType().GetField("Production{0:D2}Key".F(index + 1)); - if (field == null) - return Hotkey.Invalid; - - return (Hotkey)field.GetValue(this); + return GetKey(ProductionKeys, index); } public Hotkey GetSupportPowerHotkey(int index) { - var field = GetType().GetField("SupportPower{0:D2}Key".F(index + 1)); - if (field == null) + return GetKey(SupportPowerKeys, index); + } + + Hotkey GetKey(Func[] keys, int index) + { + if (index < 0 || index >= keys.Length) return Hotkey.Invalid; - return (Hotkey)field.GetValue(this); + return keys[index](this); } }