Client LuaCsForBarotrauma
NetworkMember.cs
2 using Microsoft.Xna.Framework;
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 
7 namespace Barotrauma.Networking
8 {
9  public enum ClientPacketHeader
10  {
11  UPDATE_LOBBY, //update state in lobby
12  UPDATE_INGAME, //update state ingame
13 
14  SERVER_SETTINGS, //change server settings
15 
17 
18  FILE_REQUEST, //request a (submarine) file from the server
19 
20  VOICE,
21 
23 
24  RESPONSE_STARTGAME, //tell the server whether you're ready to start
25  SERVER_COMMAND, //tell the server to end a round or kick/ban someone (special permissions required)
26 
28 
29  REQUEST_STARTGAMEFINALIZE, //tell the server you're ready to finalize round initialization
30 
32 
33  ERROR, //tell the server that an error occurred
34  CREW, //hiring UI
35  MEDICAL, //medical clinic
36  TRANSFER_MONEY, // wallet transfers
37  REWARD_DISTRIBUTION, // wallet reward distribution
39  CIRCUITBOX,
44  }
45 
47  {
48  SyncIds, //ids of the last changes the client knows about
49  ChatMessage, //also self-explanatory
50  Vote, //you get the idea
54  }
55 
57  {
58  MISSING_EVENT, //client was expecting a previous event
59  MISSING_ENTITY //client can't find an entity of a certain ID
60  }
61 
62  public enum ServerPacketHeader
63  {
64  AUTH_RESPONSE, //tell the player if they require a password to log in
65  AUTH_FAILURE, //the server won't authorize player yet, however connection is still alive
66  UPDATE_LOBBY, //update state in lobby (votes and chat messages)
67  UPDATE_INGAME, //update state ingame (character input and chat messages)
68 
69  PERMISSIONS, //tell the client which special permissions they have (if any)
70  ACHIEVEMENT, //give the client a steam achievement
71  ACHIEVEMENT_STAT, //increment stat for an achievement
72  CHEATS_ENABLED, //tell the clients whether cheats are on or off
73 
75 
77 
78  VOICE,
80 
81  PING_REQUEST, //ping the client
82  CLIENT_PINGS, //tell the client the pings of all other clients
83 
84  QUERY_STARTGAME, //ask the clients whether they're ready to start
85  STARTGAME, //start a new round
86  STARTGAMEFINALIZE, //finalize round initialization
87  ENDGAME,
88 
89  MISSION,
92  CREW, //anything related to managing bots in multiplayer
93  MEDICAL, //medical clinic
94  CIRCUITBOX,
95  MONEY,
96  READY_CHECK, //start, end and update a ready check
97 
99  }
101  {
102  SyncIds,
103  ChatMessage,
104  Vote,
105  ClientList,
107  EntityEvent,
109  }
110 
112  readonly record struct EntityPositionHeader(
113  bool IsItem,
114  UInt32 PrefabUintIdentifier,
115  UInt16 EntityId) : INetSerializableStruct
116  {
117  public static EntityPositionHeader FromEntity(Entity entity)
118  => new (
119  IsItem: entity is Item,
120  PrefabUintIdentifier: entity is MapEntity me ? me.Prefab.UintIdentifier : 0,
121  EntityId: entity.ID);
122  }
123 
124  enum VoteType
125  {
126  Unknown,
127  Sub,
128  Mode,
129  EndRound,
130  Kick,
131  StartRound,
132  PurchaseAndSwitchSub,
133  PurchaseSub,
134  SwitchSub,
135  TransferMoney,
136  Traitor,
137  }
138 
139  public enum ReadyCheckState
140  {
141  Start,
142  Update,
143  End
144  }
145 
146  enum DisconnectReason
147  {
148  //do not attempt reconnecting with these reasons
149  Unknown,
150  Disconnected,
151  Banned,
152  Kicked,
153  ServerShutdown,
154  ServerCrashed,
155  ServerFull,
156  AuthenticationRequired,
157  AuthenticationFailed,
158  SessionTaken,
159  TooManyFailedLogins,
160  InvalidName,
161  NameTaken,
162  InvalidVersion,
163  SteamP2PError,
164  MalformedData,
165 
166  //attempt reconnecting with these reasons
167  Timeout,
168  ExcessiveDesyncOldEvent,
169  ExcessiveDesyncRemovedEvent,
170  SyncTimeout,
171  SteamP2PTimeOut
172  }
173 
174  abstract partial class NetworkMember
175  {
176  public UInt16 LastClientListUpdateID
177  {
178  get;
179  set;
180  }
181 
182  public abstract bool IsServer { get; }
183 
184  public abstract bool IsClient { get; }
185 
186  public abstract void CreateEntityEvent(INetSerializable entity, NetEntityEvent.IData extraData = null);
187 
188  public abstract Voting Voting { get; }
189 
190  protected DateTime updateTimer;
191 
192  public bool ShowNetStats;
193 
194  public float SimulatedRandomLatency, SimulatedMinimumLatency;
195  public float SimulatedLoss;
196  public float SimulatedDuplicatesChance;
197 
198  public KarmaManager KarmaManager
199  {
200  get;
201  private set;
202  } = new KarmaManager();
203 
204  public bool GameStarted { get; protected set; }
205 
206  public abstract IReadOnlyList<Client> ConnectedClients { get; }
207 
208  public RespawnManager RespawnManager { get; protected set; }
209 
210  public ServerSettings ServerSettings { get; protected set; }
211 
212  public TimeSpan UpdateInterval => new TimeSpan(0, 0, 0, 0, MathHelper.Clamp(1000 / ServerSettings.TickRate, 1, 500));
213 
214  public void AddChatMessage(string message, ChatMessageType type, string senderName = "", Client senderClient = null, Entity senderEntity = null, PlayerConnectionChangeType changeType = PlayerConnectionChangeType.None, Color? textColor = null)
215  {
216  AddChatMessage(ChatMessage.Create(senderName, message, type, senderEntity, senderClient, changeType: changeType, textColor: textColor));
217  }
218 
219  public abstract void AddChatMessage(ChatMessage message);
220 
221  public static string ClientLogName(Client client, string name = null)
222  {
223  if (client == null) { return name; }
224  string retVal = "‖";
225  if (client.Karma < 40.0f)
226  {
227  retVal += "color:#ff9900;";
228  }
229  retVal += "metadata:" + (client.AccountId.TryUnwrap(out var accountId) ? accountId.ToString() : client.SessionId.ToString())
230  + "‖" + (name ?? client.Name).Replace("‖", "") + "‖end‖";
231  return retVal;
232  }
233 
234  public abstract void KickPlayer(string kickedName, string reason);
235 
236  public abstract void BanPlayer(string kickedName, string reason, TimeSpan? duration = null);
237 
238  public abstract void UnbanPlayer(string playerName);
239 
240  public abstract void UnbanPlayer(Endpoint endpoint);
241 
245  public static bool IsCompatible(Version myVersion, Version remoteVersion)
246  {
247  //major.minor.build.revision
248  //revision number is ignored, other values have to match
249  return
250  myVersion.Major == remoteVersion.Major &&
251  myVersion.Minor == remoteVersion.Minor &&
252  myVersion.Build == remoteVersion.Build;
253  }
254  }
255 }
Marks fields and properties as to be serialized and deserialized by INetSerializableStruct....
static ChatMessage Create(string senderName, string text, ChatMessageType type, Entity sender, Client client=null, PlayerConnectionChangeType changeType=PlayerConnectionChangeType.None, Color? textColor=null)
Option< AccountId > AccountId
The ID of the account used to authenticate this session. This value can be used as a persistent value...
readonly byte SessionId
An ID for this client for the current session. THIS IS NOT A PERSISTENT VALUE. DO NOT STORE THIS LONG...