Server LuaCsForBarotrauma
CircuitBoxInputOutputNode.cs
1 #nullable enable
2 
3 using System.Collections.Generic;
4 using System.Collections.Immutable;
5 using System.Xml.Linq;
7 using Microsoft.Xna.Framework;
8 
9 namespace Barotrauma
10 {
11  internal sealed partial class CircuitBoxInputOutputNode : CircuitBoxNode
12  {
13  public enum Type
14  {
15  Invalid,
16  Input,
17  Output
18  }
19 
20  public readonly Type NodeType;
21 
22  private const int MaxConnectionLabelLength = 32;
23  private const string ConnectionLabelOverrideElementName = "ConnectionLabelOverride";
24 
25  public Dictionary<string, string> ConnectionLabelOverrides = new();
26 
27  public CircuitBoxInputOutputNode(IReadOnlyList<CircuitBoxConnection> conns, Vector2 initialPosition, Type type, CircuitBox circuitBox): base(circuitBox)
28  {
29  InitSize(conns);
30  Connectors = conns.ToImmutableArray();
31  Position = initialPosition;
32  NodeType = type;
33  UpdatePositions();
34  }
35 
36  public void ReplaceAllConnectionLabelOverrides(Dictionary<string, string> replace)
37  {
38  foreach (var (_, value) in replace)
39  {
40  if (value.Length > MaxConnectionLabelLength)
41  {
42  DebugConsole.ThrowError($"Label override value \"{value}\" is too long (max {MaxConnectionLabelLength} characters)");
43  return;
44  }
45  }
46 
47  foreach (var (name, value) in replace)
48  {
49  if (string.IsNullOrWhiteSpace(value))
50  {
51  ConnectionLabelOverrides.Remove(name);
52  }
53  else
54  {
55  ConnectionLabelOverrides[name] = value;
56  }
57  }
58 
59  InitSize(Connectors);
60  UpdatePositions();
61  }
62 
63  private void InitSize(IReadOnlyList<CircuitBoxConnection> conns)
64  {
65 #if CLIENT
66  foreach (CircuitBoxConnection conn in conns)
67  {
68  if (ConnectionLabelOverrides.TryGetValue(conn.Name, out string? value))
69  {
70  LocalizedString newLabel =
71  string.IsNullOrWhiteSpace(value)
72  ? conn.Connection.DisplayName
73  : TextManager.Get(value).Fallback(value);
74 
75  conn.SetLabel(newLabel, this);
76  }
77  else
78  {
79  conn.SetLabel(conn.Connection.DisplayName, this);
80  }
81  }
82 #endif
83  Size = CalculateSize(conns);
84  }
85 
86  public XElement Save()
87  {
88  XElement element = new XElement($"{NodeType}Node", new XAttribute("pos", XMLExtensions.Vector2ToString(Position)));
89 
90  foreach (var (name, value) in ConnectionLabelOverrides)
91  {
92  element.Add(new XElement(ConnectionLabelOverrideElementName,
93  new XAttribute("name", name),
94  new XAttribute("value", value)));
95  }
96 
97  return element;
98  }
99 
100  public void Load(ContentXElement element)
101  {
102  Position = element.GetAttributeVector2("pos", Vector2.Zero);
103 
104  Dictionary<string, string> loadedOverrides = new();
105  foreach (var subElement in element.Elements())
106  {
107  if (subElement.Name != ConnectionLabelOverrideElementName) { continue; }
108 
109  string name = subElement.GetAttributeString("name", string.Empty);
110  string value = subElement.GetAttributeString("value", string.Empty);
111 
112  loadedOverrides[name] = value;
113  }
114 
115  ConnectionLabelOverrides = loadedOverrides;
116  InitSize(Connectors);
117  UpdatePositions();
118  }
119  }
120 }