Client LuaCsForBarotrauma
CircuitBoxNetStructs.cs
1 #nullable enable
2 
3 using System;
4 using System.Collections.Immutable;
5 using System.Xml.Linq;
7 using Microsoft.Xna.Framework;
8 
9 namespace Barotrauma
10 {
11  [Flags]
12  internal enum CircuitBoxResizeDirection
13  {
14  None = 0,
15  Down = 1,
16  Right = 2,
17  Left = 4
18  }
19 
20  // TODO this needs to be refactored at some point for reasons:
21  // 1. We need to send 4 different ImmutableArray<short> for some network packets
22  // 2. We have 3 identical remove events that are identical in signature
23  // 3. We have 3 different events for selecting. nodes, wires, and server broadcast
24  public enum CircuitBoxOpcode
25  {
26  Error,
27  Cursor,
30  AddWire,
31  RemoveWire,
37  AddLabel,
42  }
43 
44  [NetworkSerialize]
45  internal readonly record struct NetCircuitBoxHeader(CircuitBoxOpcode Opcode, ushort ItemID, byte ComponentIndex) : INetSerializableStruct
46  {
47  public Option<CircuitBox> FindTarget() => CircuitBox.FindCircuitBox(ItemID, ComponentIndex);
48  }
49 
50  [NetworkSerialize]
51  internal readonly record struct CircuitBoxConnectorIdentifier(Identifier SignalConnection, Option<ushort> TargetId) : INetSerializableStruct
52  {
53  public static CircuitBoxConnectorIdentifier FromConnection(CircuitBoxConnection connection) =>
54  connection switch
55  {
56  (CircuitBoxInputConnection or CircuitBoxOutputConnection)
57  => new CircuitBoxConnectorIdentifier(connection.Name.ToIdentifier(), Option.None),
58 
59  CircuitBoxNodeConnection nodeConnection
60  => new CircuitBoxConnectorIdentifier(connection.Name.ToIdentifier(), Option.Some(nodeConnection.Component.ID)),
61 
62  _ => throw new ArgumentOutOfRangeException(nameof(connection))
63  };
64 
65  public Option<CircuitBoxConnection> FindConnection(CircuitBox circuitBox)
66  {
67  if (!TargetId.TryUnwrap(out var id))
68  {
69  return circuitBox.FindInputOutputConnection(SignalConnection);
70  }
71 
72  foreach (CircuitBoxComponent boxNode in circuitBox.Components)
73  {
74  if (boxNode.ID != id) { continue; }
75 
76  foreach (var conn in boxNode.Connectors)
77  {
78  if (conn.Name != SignalConnection) { continue; }
79 
80  return Option.Some(conn);
81  }
82  }
83 
84  return Option.None;
85  }
86 
87  public XElement Save(string name) => new XElement(name,
88  new XAttribute("name", SignalConnection),
89  new XAttribute("target", TargetId.TryUnwrap(out var value) ? value.ToString() : string.Empty));
90 
91  public static CircuitBoxConnectorIdentifier Load(ContentXElement element)
92  {
93  string? name = element.GetAttributeString("name", string.Empty);
94  string? target = element.GetAttributeString("target", string.Empty);
95 
96  Option<ushort> targetId = Option.None;
97  if (!string.IsNullOrWhiteSpace(target))
98  {
99  targetId = ushort.TryParse(target, out var value) ? Option.Some(value) : Option.None;
100  }
101 
102  return new CircuitBoxConnectorIdentifier(name.ToIdentifier(), targetId);
103  }
104 
105  public override string ToString()
106  => $"{{Name: {SignalConnection}, ID: {(TargetId.TryUnwrap(out var value) ? value.ToString() : "N/A")}}}";
107  }
108 
109  [NetworkSerialize]
110  internal readonly record struct CircuitBoxAddLabelEvent(Vector2 Position, Color Color, NetLimitedString Header, NetLimitedString Body) : INetSerializableStruct;
111 
112  [NetworkSerialize]
113  internal readonly record struct CircuitBoxServerAddLabelEvent(ushort ID, Vector2 Position, Vector2 Size, Color Color, NetLimitedString Header, NetLimitedString Body) : INetSerializableStruct;
114 
115  [NetworkSerialize]
116  internal readonly record struct CircuitBoxResizeLabelEvent(ushort ID, Vector2 Position, Vector2 Size) : INetSerializableStruct;
117 
118  [NetworkSerialize]
119  internal readonly record struct CircuitBoxRemoveLabelEvent(ImmutableArray<ushort> TargetIDs) : INetSerializableStruct;
120 
121  [NetworkSerialize]
122  internal readonly record struct CircuitBoxAddComponentEvent(UInt32 PrefabIdentifier, Vector2 Position) : INetSerializableStruct;
123 
124  [NetworkSerialize]
125  internal readonly record struct CircuitBoxServerCreateComponentEvent(ushort BackingItemId, UInt32 UsedResource, ushort ComponentId, Vector2 Position) : INetSerializableStruct;
126 
127  [NetworkSerialize]
128  internal readonly record struct CircuitBoxRemoveComponentEvent(ImmutableArray<ushort> TargetIDs) : INetSerializableStruct;
129 
130  [NetworkSerialize]
131  internal readonly record struct CircuitBoxMoveComponentEvent(ImmutableArray<ushort> TargetIDs, ImmutableArray<CircuitBoxInputOutputNode.Type> IOs, ImmutableArray<ushort> LabelIDs, Vector2 MoveAmount) : INetSerializableStruct;
132 
133  [NetworkSerialize]
134  internal readonly record struct CircuitBoxSelectNodesEvent(ImmutableArray<ushort> TargetIDs, ImmutableArray<CircuitBoxInputOutputNode.Type> IOs, ImmutableArray<ushort> LabelIDs, bool Overwrite, ushort CharacterID) : INetSerializableStruct;
135 
136  [NetworkSerialize]
137  internal readonly record struct CircuitBoxServerUpdateSelection(ImmutableArray<CircuitBoxIdSelectionPair> ComponentIds, ImmutableArray<CircuitBoxIdSelectionPair> WireIds, ImmutableArray<CircuitBoxTypeSelectionPair> InputOutputs, ImmutableArray<CircuitBoxIdSelectionPair> LabelIds) : INetSerializableStruct;
138 
139  [NetworkSerialize]
140  internal readonly record struct CircuitBoxIdSelectionPair(ushort ID, Option<ushort> SelectedBy) : INetSerializableStruct;
141 
142  [NetworkSerialize]
143  internal readonly record struct CircuitBoxTypeSelectionPair(CircuitBoxInputOutputNode.Type Type, Option<ushort> SelectedBy) : INetSerializableStruct;
144 
145  [NetworkSerialize]
146  internal readonly record struct CircuitBoxSelectWiresEvent(ImmutableArray<ushort> TargetIDs, bool Overwrite, ushort CharacterID) : INetSerializableStruct;
147 
148  [NetworkSerialize]
149  internal readonly record struct CircuitBoxClientAddWireEvent(Color Color, CircuitBoxConnectorIdentifier Start, CircuitBoxConnectorIdentifier End, UInt32 SelectedWirePrefabIdentifier) : INetSerializableStruct;
150 
151  [NetworkSerialize]
152  internal readonly record struct CircuitBoxServerCreateWireEvent(CircuitBoxClientAddWireEvent Request, ushort WireId, Option<ushort> BackingItemId) : INetSerializableStruct;
153 
154  [NetworkSerialize]
155  internal readonly record struct CircuitBoxRemoveWireEvent(ImmutableArray<ushort> TargetIDs) : INetSerializableStruct;
156 
157  [NetworkSerialize]
158  internal readonly record struct CircuitBoxRenameLabelEvent(ushort LabelId, Color Color, NetLimitedString NewHeader, NetLimitedString NewBody) : INetSerializableStruct;
159 
160  [NetworkSerialize]
161  internal readonly record struct CircuitBoxRenameConnectionLabelsEvent(CircuitBoxInputOutputNode.Type Type, NetDictionary<string, string> Override) : INetSerializableStruct;
162 
163 
164  [NetworkSerialize]
165  internal readonly record struct CircuitBoxErrorEvent(string Message) : INetSerializableStruct;
166 
167  [NetworkSerialize]
168  internal readonly record struct CircuitBoxInitializeStateFromServerEvent(
169  ImmutableArray<CircuitBoxServerCreateComponentEvent> Components,
170  ImmutableArray<CircuitBoxServerCreateWireEvent> Wires,
171  ImmutableArray<CircuitBoxServerAddLabelEvent> Labels,
172  ImmutableArray<CircuitBoxRenameConnectionLabelsEvent> LabelOverrides,
173  Vector2 InputPos,
174  Vector2 OutputPos) : INetSerializableStruct;
175 
176  internal readonly record struct CircuitBoxEventData(INetSerializableStruct Data) : ItemComponent.IEventData
177  {
178  public CircuitBoxOpcode Opcode =>
179  Data switch
180  {
181  (CircuitBoxAddComponentEvent or CircuitBoxServerCreateComponentEvent)
182  => CircuitBoxOpcode.AddComponent,
183  CircuitBoxRemoveComponentEvent
184  => CircuitBoxOpcode.DeleteComponent,
185  CircuitBoxMoveComponentEvent
186  => CircuitBoxOpcode.MoveComponent,
187  CircuitBoxSelectNodesEvent
188  => CircuitBoxOpcode.SelectComponents,
189  CircuitBoxSelectWiresEvent
190  => CircuitBoxOpcode.SelectWires,
191  CircuitBoxServerUpdateSelection
192  => CircuitBoxOpcode.UpdateSelection,
193  (CircuitBoxClientAddWireEvent or CircuitBoxServerCreateWireEvent)
194  => CircuitBoxOpcode.AddWire,
195  CircuitBoxRemoveWireEvent
196  => CircuitBoxOpcode.RemoveWire,
197  CircuitBoxInitializeStateFromServerEvent
198  => CircuitBoxOpcode.ServerInitialize,
199  CircuitBoxRenameLabelEvent
200  => CircuitBoxOpcode.RenameLabel,
201  (CircuitBoxAddLabelEvent or CircuitBoxServerAddLabelEvent)
202  => CircuitBoxOpcode.AddLabel,
203  CircuitBoxRemoveLabelEvent
204  => CircuitBoxOpcode.RemoveLabel,
205  CircuitBoxResizeLabelEvent
206  => CircuitBoxOpcode.ResizeLabel,
207  CircuitBoxRenameConnectionLabelsEvent
208  => CircuitBoxOpcode.RenameConnections,
209  _ => throw new ArgumentOutOfRangeException(nameof(Data))
210  };
211  }
212 }
The base class for components holding the different functionalities of the item