Remove special-case hotkey parsing.

This commit is contained in:
Paul Chote
2025-12-10 17:04:33 +00:00
committed by Gustas Kažukauskas
parent 575f6624a2
commit 98f7fe0db7
4 changed files with 19 additions and 32 deletions

View File

@@ -17,13 +17,16 @@ namespace OpenRA
{
public sealed class HotkeyManager
{
readonly Dictionary<string, Hotkey> settings;
[YamlNode("Keys", shared: true)]
sealed class HotkeySettings : SettingsModule { }
readonly Dictionary<string, HotkeyDefinition> definitions = [];
readonly Dictionary<string, Hotkey> keys = [];
readonly HotkeySettings hotkeySettings;
public HotkeyManager(IReadOnlyFileSystem fileSystem, Dictionary<string, Hotkey> settings, Manifest manifest)
public HotkeyManager(IReadOnlyFileSystem fileSystem, ObjectCreator objectCreator, Manifest manifest)
{
this.settings = settings;
hotkeySettings = Game.Settings.GetOrCreate<HotkeySettings>(objectCreator, manifest.Id);
var keyDefinitions = MiniYaml.Load(fileSystem, manifest.Hotkeys, null);
foreach (var kd in keyDefinitions)
@@ -33,11 +36,9 @@ namespace OpenRA
keys[kd.Key] = definition.Default;
}
foreach (var kv in settings)
{
if (definitions.TryGetValue(kv.Key, out var definition) && !definition.Readonly)
keys[kv.Key] = kv.Value;
}
foreach (var node in hotkeySettings.Yaml.Nodes)
if (definitions.TryGetValue(node.Key, out var definition) && !definition.Readonly)
keys[node.Key] = FieldLoader.GetValue<Hotkey>(node.Key, node.Value.Value);
foreach (var hd in definitions)
hd.Value.HasDuplicates = GetFirstDuplicate(hd.Value, this[hd.Value.Name].GetValue()) != null;
@@ -68,10 +69,9 @@ namespace OpenRA
return;
keys[name] = value;
hotkeySettings.Yaml.Nodes.RemoveAll(n => n.Key == name);
if (value != definition.Default)
settings[name] = value;
else
settings.Remove(name);
hotkeySettings.Yaml.Nodes.Add(new MiniYamlNodeBuilder(name, FieldSaver.FormatValue(value)));
var hadDuplicates = definition.HasDuplicates;
definition.HasDuplicates = GetFirstDuplicate(definition, this[definition.Name].GetValue()) != null;
@@ -108,5 +108,10 @@ namespace OpenRA
public HotkeyReference this[string name] => new(GetHotkeyReference(name));
public IEnumerable<HotkeyDefinition> Definitions => definitions.Values;
public void Save()
{
hotkeySettings.Save();
}
}
}

View File

@@ -107,7 +107,7 @@ namespace OpenRA
SpriteLoaders = ObjectCreator.GetLoaders<ISpriteLoader>(Manifest.SpriteFormats, "sprite");
VideoLoaders = ObjectCreator.GetLoaders<IVideoLoader>(Manifest.VideoFormats, "video");
SpriteSequenceLoader = ObjectCreator.GetLoader<ISpriteSequenceLoader>(Manifest.SpriteSequenceFormat, "sequence");
Hotkeys = new HotkeyManager(ModFiles, Game.Settings.Keys, Manifest);
Hotkeys = new HotkeyManager(ModFiles, ObjectCreator, Manifest);
Cursors = ParseCursors(Manifest, DefaultFileSystem);
defaultRules = Exts.Lazy(() => Ruleset.LoadDefaults(this));

View File

@@ -383,7 +383,6 @@ namespace OpenRA
public readonly ServerSettings Server;
public readonly DebugSettings Debug;
public readonly SinglePlayerGameSettings SinglePlayerSettings;
internal readonly Dictionary<string, Hotkey> Keys = [];
readonly Arguments args;
readonly TypeDictionary modules = [];
@@ -409,12 +408,6 @@ namespace OpenRA
Server = GetOrCreate<ServerSettings>(null);
Debug = GetOrCreate<DebugSettings>(null);
SinglePlayerSettings = GetOrCreate<SinglePlayerGameSettings>(null);
var keysNode = yaml.FirstOrDefault(n => n.Key == "Keys");
if (keysNode != null)
foreach (var node in keysNode.Value.Nodes)
if (node.Key != null)
Keys[node.Key] = FieldLoader.GetValue<Hotkey>(node.Key, node.Value.Value);
}
public T GetOrCreate<T>(ObjectCreator objectCreator, string mod = null) where T : SettingsModule
@@ -488,17 +481,6 @@ namespace OpenRA
foreach (var m in modules)
((SettingsModule)m).Commit();
var keysNode = yaml.FirstOrDefault(n => n.Key == "Keys");
if (keysNode == null)
{
keysNode = new MiniYamlNodeBuilder("Keys", "");
yaml.Add(keysNode);
}
keysNode.Value.Nodes.Clear();
foreach (var kv in Keys)
keysNode.Value.Nodes.Add(new MiniYamlNodeBuilder(kv.Key, FieldSaver.FormatValue(kv.Value)));
// Filter out modules with no fields and force a newline between each module
var container = new[] { null, new MiniYamlNodeBuilder("", "") };
IEnumerable<MiniYamlNodeBuilder> AddSpacer(MiniYamlNodeBuilder n)

View File

@@ -317,7 +317,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
WidgetUtils.TruncateButtonToTooltip(selectedHotkeyButton, hotkeyEntryWidget.Key.DisplayString());
modData.Hotkeys.Set(selectedHotkeyDefinition.Name, hotkeyEntryWidget.Key);
Game.Settings.Save();
modData.Hotkeys.Save();
}
void ResetHotkey()
@@ -338,7 +338,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
if (duplicateHotkeyButton != null)
WidgetUtils.TruncateButtonToTooltip(duplicateHotkeyButton, Hotkey.Invalid.DisplayString());
modData.Hotkeys.Set(duplicateHotkeyDefinition.Name, Hotkey.Invalid);
Game.Settings.Save();
modData.Hotkeys.Save();
hotkeyEntryWidget.YieldKeyboardFocus();
}