Client LuaCsForBarotrauma
DebugConsoleMapping.cs
1 #nullable enable
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6 using System.Xml.Linq;
8 
10 {
11  public class DebugConsoleMapping
12  {
13  private readonly Dictionary<KeyOrMouse, string> bindings = new Dictionary<KeyOrMouse, string>();
14  public IReadOnlyDictionary<KeyOrMouse, string> Bindings => bindings;
15 
16  private DebugConsoleMapping() { }
17 
18  private DebugConsoleMapping(XElement element)
19  {
20  var bindings = new Dictionary<KeyOrMouse, string>();
21  foreach (var subElement in element.Elements())
22  {
23  KeyOrMouse keyOrMouse = subElement.GetAttributeKeyOrMouse("key", MouseButton.None);
24  if (keyOrMouse == MouseButton.None) { continue; }
25  string command = subElement.GetAttributeString("command", "");
26  if (command.IsNullOrWhiteSpace()) { continue; }
27  bindings[keyOrMouse] = command;
28  }
29 
30  this.bindings = bindings;
31  }
32 
33  public static void Init(XElement? element)
34  {
35  if (element is null) { return; }
36 
37  Instance = new DebugConsoleMapping(element);
38  }
39 
40  public void SaveTo(XElement element)
41  {
42  Bindings
43  .ForEach(kvp => element.Add(
44  new XElement("Keybind",
45  new XAttribute("key", kvp.Key),
46  new XAttribute("command", kvp.Value))));
47  }
48 
49  public void Set(KeyOrMouse key, string command)
50  => bindings[key] = command;
51 
52  public void Remove(KeyOrMouse key)
53  => bindings.Remove(key);
54 
55  public static DebugConsoleMapping Instance { get; private set; } = new DebugConsoleMapping();
56  }
57 
58 }
void Set(KeyOrMouse key, string command)
IReadOnlyDictionary< KeyOrMouse, string > Bindings