From 7ebea32be9989048abf999231be7c9d8560d3eee Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sat, 18 Nov 2017 17:53:11 +0000 Subject: [PATCH] Remove hardcoded settings backend. --- OpenRA.Game/HotkeyManager.cs | 40 ++++++---- OpenRA.Game/Settings.cs | 145 ++++------------------------------- 2 files changed, 41 insertions(+), 144 deletions(-) diff --git a/OpenRA.Game/HotkeyManager.cs b/OpenRA.Game/HotkeyManager.cs index 421fec1aeb..fcea3afc30 100644 --- a/OpenRA.Game/HotkeyManager.cs +++ b/OpenRA.Game/HotkeyManager.cs @@ -17,29 +17,33 @@ namespace OpenRA { public sealed class HotkeyManager { - readonly KeySettings settings; - readonly Dictionary keys = new Dictionary(); + readonly Dictionary settings; + readonly Dictionary definitions = new Dictionary(); + readonly Dictionary keys = new Dictionary(); - public HotkeyManager(IReadOnlyFileSystem fileSystem, KeySettings settings, Manifest manifest) + public HotkeyManager(IReadOnlyFileSystem fileSystem, Dictionary settings, Manifest manifest) { this.settings = settings; var keyDefinitions = MiniYaml.Load(fileSystem, manifest.Hotkeys, null); foreach (var kd in keyDefinitions) - keys[kd.Key] = new HotkeyDefinition(kd.Key, kd.Value); + { + var definition = new HotkeyDefinition(kd.Key, kd.Value); + definitions[kd.Key] = definition; + keys[kd.Key] = definition.Default; + } + + foreach (var kv in settings) + keys[kv.Key] = kv.Value; } internal Func GetHotkeyReference(string name) { - var ret = settings.GetHotkeyReference(name); - if (ret != null) - return ret; + // Is this a mod-defined hotkey? + if (keys.ContainsKey(name)) + return () => keys[name]; - HotkeyDefinition keyDefinition; - if (keys.TryGetValue(name, out keyDefinition)) - return () => keyDefinition.Default; - - // Not a mod-defined hotkey, so try and parse as a hardcoded definition + // Try and parse as a hardcoded definition Hotkey key; if (!Hotkey.TryParse(name, out key)) key = Hotkey.Invalid; @@ -49,11 +53,15 @@ namespace OpenRA public void Set(string name, Hotkey value) { - var field = settings.GetType().GetField(name + "Key"); - if (field == null) + HotkeyDefinition definition; + if (!definitions.TryGetValue(name, out definition)) return; - field.SetValue(settings, value); + keys[name] = value; + if (value != definition.Default) + settings[name] = value; + else + settings.Remove(name); } public HotkeyReference this[string name] @@ -64,6 +72,6 @@ namespace OpenRA } } - public IEnumerable Definitions { get { return keys.Values; } } + public IEnumerable Definitions { get { return definitions.Values; } } } } diff --git a/OpenRA.Game/Settings.cs b/OpenRA.Game/Settings.cs index c3e07128d8..b72d8df0b4 100644 --- a/OpenRA.Game/Settings.cs +++ b/OpenRA.Game/Settings.cs @@ -194,132 +194,6 @@ namespace OpenRA public MPGameFilters MPGameFilters = MPGameFilters.Waiting | MPGameFilters.Empty | MPGameFilters.Protected | MPGameFilters.Started; } - public class KeySettings - { - public Hotkey CycleBaseKey = new Hotkey(Keycode.H, Modifiers.None); - public Hotkey ToLastEventKey = new Hotkey(Keycode.SPACE, Modifiers.None); - public Hotkey ToSelectionKey = new Hotkey(Keycode.HOME, Modifiers.None); - public Hotkey SelectAllUnitsKey = new Hotkey(Keycode.Q, Modifiers.None); - public Hotkey SelectUnitsByTypeKey = new Hotkey(Keycode.W, Modifiers.None); - - public Hotkey MapScrollUpKey = new Hotkey(Keycode.UP, Modifiers.None); - public Hotkey MapScrollDownKey = new Hotkey(Keycode.DOWN, Modifiers.None); - public Hotkey MapScrollLeftKey = new Hotkey(Keycode.LEFT, Modifiers.None); - public Hotkey MapScrollRightKey = new Hotkey(Keycode.RIGHT, Modifiers.None); - - public Hotkey MapJumpToTopEdgeKey = new Hotkey(Keycode.UP, Modifiers.Alt); - public Hotkey MapJumpToBottomEdgeKey = new Hotkey(Keycode.DOWN, Modifiers.Alt); - public Hotkey MapJumpToLeftEdgeKey = new Hotkey(Keycode.LEFT, Modifiers.Alt); - public Hotkey MapJumpToRightEdgeKey = new Hotkey(Keycode.RIGHT, Modifiers.Alt); - - public Hotkey MapBookmarkSave01Key = new Hotkey(Keycode.Q, Modifiers.Ctrl); - public Hotkey MapBookmarkSave02Key = new Hotkey(Keycode.W, Modifiers.Ctrl); - public Hotkey MapBookmarkSave03Key = new Hotkey(Keycode.E, Modifiers.Ctrl); - public Hotkey MapBookmarkSave04Key = new Hotkey(Keycode.R, Modifiers.Ctrl); - - public Hotkey MapBookmarkRestore01Key = new Hotkey(Keycode.Q, Modifiers.Alt); - public Hotkey MapBookmarkRestore02Key = new Hotkey(Keycode.W, Modifiers.Alt); - public Hotkey MapBookmarkRestore03Key = new Hotkey(Keycode.E, Modifiers.Alt); - public Hotkey MapBookmarkRestore04Key = new Hotkey(Keycode.R, Modifiers.Alt); - - public Hotkey PauseKey = new Hotkey(Keycode.PAUSE, Modifiers.None); - public Hotkey PlaceBeaconKey = new Hotkey(Keycode.B, Modifiers.None); - public Hotkey SellKey = new Hotkey(Keycode.Z, Modifiers.None); - public Hotkey PowerDownKey = new Hotkey(Keycode.X, Modifiers.None); - public Hotkey RepairKey = new Hotkey(Keycode.C, Modifiers.None); - - public Hotkey NextProductionTabKey = new Hotkey(Keycode.PAGEDOWN, Modifiers.None); - public Hotkey PreviousProductionTabKey = new Hotkey(Keycode.PAGEUP, Modifiers.None); - public Hotkey CycleProductionBuildingsKey = new Hotkey(Keycode.TAB, Modifiers.None); - - public Hotkey AttackMoveKey = new Hotkey(Keycode.A, Modifiers.None); - public Hotkey StopKey = new Hotkey(Keycode.S, Modifiers.None); - public Hotkey ScatterKey = new Hotkey(Keycode.X, Modifiers.Ctrl); - public Hotkey DeployKey = new Hotkey(Keycode.F, Modifiers.None); - public Hotkey StanceHoldFireKey = new Hotkey(Keycode.F, Modifiers.Alt); - public Hotkey StanceReturnFireKey = new Hotkey(Keycode.D, Modifiers.Alt); - public Hotkey StanceDefendKey = new Hotkey(Keycode.S, Modifiers.Alt); - public Hotkey StanceAttackAnythingKey = new Hotkey(Keycode.A, Modifiers.Alt); - public Hotkey GuardKey = new Hotkey(Keycode.D, Modifiers.None); - - public Hotkey ObserverCombinedViewKey = new Hotkey(Keycode.MINUS, Modifiers.None); - public Hotkey ObserverWorldViewKey = new Hotkey(Keycode.EQUALS, Modifiers.None); - public Hotkey StatisticsBasicKey = new Hotkey(Keycode.F1, Modifiers.None); - public Hotkey StatisticsEconomyKey = new Hotkey(Keycode.F2, Modifiers.None); - public Hotkey StatisticsProductionKey = new Hotkey(Keycode.F3, Modifiers.None); - public Hotkey StatisticsCombatKey = new Hotkey(Keycode.F4, Modifiers.None); - public Hotkey StatisticsGraphKey = new Hotkey(Keycode.UNKNOWN, Modifiers.None); - - public Hotkey CycleStatusBarsKey = new Hotkey(Keycode.COMMA, Modifiers.None); - public Hotkey TogglePixelDoubleKey = new Hotkey(Keycode.PERIOD, Modifiers.None); - public Hotkey TogglePlayerStanceColorKey = new Hotkey(Keycode.COMMA, Modifiers.Ctrl); - - public Hotkey TakeScreenshotKey = new Hotkey(Keycode.P, Modifiers.Ctrl); - public Hotkey ToggleMuteKey = new Hotkey(Keycode.M, Modifiers.None); - - public Hotkey Production01Key = new Hotkey(Keycode.F1, Modifiers.None); - public Hotkey Production02Key = new Hotkey(Keycode.F2, Modifiers.None); - public Hotkey Production03Key = new Hotkey(Keycode.F3, Modifiers.None); - public Hotkey Production04Key = new Hotkey(Keycode.F4, Modifiers.None); - public Hotkey Production05Key = new Hotkey(Keycode.F5, Modifiers.None); - public Hotkey Production06Key = new Hotkey(Keycode.F6, Modifiers.None); - public Hotkey Production07Key = new Hotkey(Keycode.F7, Modifiers.None); - public Hotkey Production08Key = new Hotkey(Keycode.F8, Modifiers.None); - public Hotkey Production09Key = new Hotkey(Keycode.F9, Modifiers.None); - public Hotkey Production10Key = new Hotkey(Keycode.F10, Modifiers.None); - public Hotkey Production11Key = new Hotkey(Keycode.F11, Modifiers.None); - public Hotkey Production12Key = new Hotkey(Keycode.F12, Modifiers.None); - - public Hotkey Production13Key = new Hotkey(Keycode.F1, Modifiers.Ctrl); - public Hotkey Production14Key = new Hotkey(Keycode.F2, Modifiers.Ctrl); - public Hotkey Production15Key = new Hotkey(Keycode.F3, Modifiers.Ctrl); - public Hotkey Production16Key = new Hotkey(Keycode.F4, Modifiers.Ctrl); - public Hotkey Production17Key = new Hotkey(Keycode.F5, Modifiers.Ctrl); - public Hotkey Production18Key = new Hotkey(Keycode.F6, Modifiers.Ctrl); - public Hotkey Production19Key = new Hotkey(Keycode.F7, Modifiers.Ctrl); - public Hotkey Production20Key = new Hotkey(Keycode.F8, Modifiers.Ctrl); - public Hotkey Production21Key = new Hotkey(Keycode.F9, Modifiers.Ctrl); - public Hotkey Production22Key = new Hotkey(Keycode.F10, Modifiers.Ctrl); - public Hotkey Production23Key = new Hotkey(Keycode.F11, Modifiers.Ctrl); - public Hotkey Production24Key = new Hotkey(Keycode.F12, Modifiers.Ctrl); - - public Hotkey ProductionTypeBuildingKey = new Hotkey(Keycode.E, Modifiers.None); - public Hotkey ProductionTypeDefenseKey = new Hotkey(Keycode.R, Modifiers.None); - public Hotkey ProductionTypeInfantryKey = new Hotkey(Keycode.T, Modifiers.None); - public Hotkey ProductionTypeVehicleKey = new Hotkey(Keycode.Y, Modifiers.None); - public Hotkey ProductionTypeAircraftKey = new Hotkey(Keycode.U, Modifiers.None); - public Hotkey ProductionTypeNavalKey = new Hotkey(Keycode.I, Modifiers.None); - public Hotkey ProductionTypeTankKey = new Hotkey(Keycode.I, Modifiers.None); - public Hotkey ProductionTypeMerchantKey = new Hotkey(Keycode.O, Modifiers.None); - public Hotkey ProductionTypeUpgradeKey = new Hotkey(Keycode.R, Modifiers.None); - - public Hotkey SupportPower01Key = new Hotkey(Keycode.UNKNOWN, Modifiers.None); - public Hotkey SupportPower02Key = new Hotkey(Keycode.UNKNOWN, Modifiers.None); - public Hotkey SupportPower03Key = new Hotkey(Keycode.UNKNOWN, Modifiers.None); - public Hotkey SupportPower04Key = new Hotkey(Keycode.UNKNOWN, Modifiers.None); - public Hotkey SupportPower05Key = new Hotkey(Keycode.UNKNOWN, Modifiers.None); - public Hotkey SupportPower06Key = new Hotkey(Keycode.UNKNOWN, Modifiers.None); - - public Hotkey ReplaySpeedSlowKey = new Hotkey(Keycode.F5, Modifiers.None); - public Hotkey ReplaySpeedRegularKey = new Hotkey(Keycode.F6, Modifiers.None); - public Hotkey ReplaySpeedFastKey = new Hotkey(Keycode.F7, Modifiers.None); - public Hotkey ReplaySpeedMaxKey = new Hotkey(Keycode.F8, Modifiers.None); - - public Hotkey StopMusicKey = new Hotkey(Keycode.AUDIOSTOP, Modifiers.None); - public Hotkey PauseMusicKey = new Hotkey(Keycode.AUDIOPLAY, Modifiers.None); - public Hotkey PrevMusicKey = new Hotkey(Keycode.AUDIOPREV, Modifiers.None); - public Hotkey NextMusicKey = new Hotkey(Keycode.AUDIONEXT, Modifiers.None); - - internal Func GetHotkeyReference(string name) - { - var field = typeof(KeySettings).GetField(name + "Key"); - if (field == null) - return null; - - return () => (Hotkey)field.GetValue(this); - } - } - public class ChatSettings { public string Hostname = "irc.openra.net"; @@ -340,7 +214,7 @@ namespace OpenRA public readonly GraphicSettings Graphics = new GraphicSettings(); public readonly ServerSettings Server = new ServerSettings(); public readonly DebugSettings Debug = new DebugSettings(); - public readonly KeySettings Keys = new KeySettings(); + internal Dictionary Keys = new Dictionary(); public readonly ChatSettings Chat = new ChatSettings(); public readonly Dictionary Sections; @@ -361,7 +235,6 @@ namespace OpenRA { "Graphics", Graphics }, { "Server", Server }, { "Debug", Debug }, - { "Keys", Keys }, { "Chat", Chat } }; @@ -381,6 +254,11 @@ namespace OpenRA if (Sections.TryGetValue(yamlSection.Key, out settingsSection)) LoadSectionYaml(yamlSection.Value, settingsSection); } + + var keysNode = yamlCache.FirstOrDefault(n => n.Key == "Keys"); + if (keysNode != null) + foreach (var node in keysNode.Value.Nodes) + Keys[node.Key] = FieldLoader.GetValue(node.Key, node.Value.Value); } // Override with commandline args @@ -430,6 +308,17 @@ namespace OpenRA } } + var keysYaml = yamlCache.FirstOrDefault(x => x.Key == "Keys"); + if (keysYaml == null) + { + keysYaml = new MiniYamlNode("Keys", new MiniYaml("")); + yamlCache.Add(keysYaml); + } + + keysYaml.Value.Nodes.Clear(); + foreach (var kv in Keys) + keysYaml.Value.Nodes.Add(new MiniYamlNode(kv.Key, FieldSaver.FormatValue(kv.Value))); + yamlCache.WriteToFile(settingsFile); }