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  SERVER_SETTINGS_PERKS, //change disembark perks (has different permissions from the rest of server settings)
16 
18 
19  FILE_REQUEST, //request a (submarine) file from the server
20 
21  VOICE,
22 
24 
25  RESPONSE_CANCEL_STARTGAME, //tell the server you do not wish to start with the given warnings active
26 
27  RESPONSE_STARTGAME, //tell the server whether you're ready to start
28  SERVER_COMMAND, //tell the server to end a round or kick/ban someone (special permissions required)
29 
31 
32  REQUEST_STARTGAMEFINALIZE, //tell the server you're ready to finalize round initialization
33 
35 
36  ERROR, //tell the server that an error occurred
37  CREW, //hiring UI
38  MEDICAL, //medical clinic
39  TRANSFER_MONEY, // wallet transfers
40  REWARD_DISTRIBUTION, // wallet reward distribution
42  CIRCUITBOX,
46 
47  REQUEST_BACKUP_INDICES, // client wants a list of available backups for a save file
49  }
50 
52  {
53  SyncIds, //ids of the last changes the client knows about
54  ChatMessage, //also self-explanatory
55  Vote, //you get the idea
59  }
60 
62  {
63  MISSING_EVENT, //client was expecting a previous event
64  MISSING_ENTITY //client can't find an entity of a certain ID
65  }
66 
67  public enum ServerPacketHeader
68  {
69  AUTH_RESPONSE, //tell the player if they require a password to log in
70  AUTH_FAILURE, //the server won't authorize player yet, however connection is still alive
71  UPDATE_LOBBY, //update state in lobby (votes and chat messages)
72  UPDATE_INGAME, //update state ingame (character input and chat messages)
73 
74  PERMISSIONS, //tell the client which special permissions they have (if any)
75  ACHIEVEMENT, //give the client a steam achievement
76  ACHIEVEMENT_STAT, //increment stat for an achievement
77  CHEATS_ENABLED, //tell the clients whether cheats are on or off
78 
80 
82 
83  VOICE,
85 
86  PING_REQUEST, //ping the client
87  CLIENT_PINGS, //tell the client the pings of all other clients
88 
89  QUERY_STARTGAME, //ask the clients whether they're ready to start
90  WARN_STARTGAME, //round is about to start with invalid (perk) settings, warn the clients before starting
91  CANCEL_STARTGAME, //someone requested the round start to be cancelled due to invalid settings, tell the other clients
92  STARTGAME, //start a new round
93  STARTGAMEFINALIZE, //finalize round initialization
94  ENDGAME,
95 
96  MISSION,
99  CREW, //anything related to managing bots in multiplayer
100  MEDICAL, //medical clinic
101  CIRCUITBOX,
102  MONEY,
103  READY_CHECK, //start, end and update a ready check
104  SEND_BACKUP_INDICES, // the server sends a list of available backups for a save file
106  }
108  {
109  SyncIds,
110  ChatMessage,
111  Vote,
112  ClientList,
114  EntityEvent,
116  }
117 
119  readonly record struct EntityPositionHeader(
120  bool IsItem,
121  UInt32 PrefabUintIdentifier,
122  UInt16 EntityId) : INetSerializableStruct
123  {
124  public static EntityPositionHeader FromEntity(Entity entity)
125  => new (
126  IsItem: entity is Item,
127  PrefabUintIdentifier: entity is MapEntity me ? me.Prefab.UintIdentifier : 0,
128  EntityId: entity.ID);
129  }
130 
131  enum VoteType
132  {
133  Unknown,
134  Sub,
135  Mode,
136  EndRound,
137  Kick,
138  StartRound,
139  PurchaseAndSwitchSub,
140  PurchaseSub,
141  SwitchSub,
142  TransferMoney,
143  Traitor,
144  }
145 
146  public enum ReadyCheckState
147  {
148  Start,
149  Update,
150  End
151  }
152 
153  enum DisconnectReason
154  {
155  //do not attempt reconnecting with these reasons
156  Unknown,
157  Disconnected,
158  Banned,
159  Kicked,
160  ServerShutdown,
161  ServerCrashed,
162  ServerFull,
163  AuthenticationRequired,
164  AuthenticationFailed,
165  SessionTaken,
166  TooManyFailedLogins,
167  InvalidName,
168  NameTaken,
169  InvalidVersion,
170  SteamP2PError,
171  MalformedData,
172 
173  //attempt reconnecting with these reasons
174  Timeout,
175  ExcessiveDesyncOldEvent,
176  ExcessiveDesyncRemovedEvent,
177  SyncTimeout,
178  SteamP2PTimeOut
179  }
180 
181  abstract partial class NetworkMember
182  {
183  public UInt16 LastClientListUpdateID
184  {
185  get;
186  set;
187  }
188 
189  public abstract bool IsServer { get; }
190 
191  public abstract bool IsClient { get; }
192 
193  public abstract void CreateEntityEvent(INetSerializable entity, NetEntityEvent.IData extraData = null);
194 
195  public abstract Voting Voting { get; }
196 
197  protected DateTime updateTimer;
198 
199  public bool ShowNetStats;
200 
201  public float SimulatedRandomLatency, SimulatedMinimumLatency;
202  public float SimulatedLoss;
203  public float SimulatedDuplicatesChance;
204 
205  public KarmaManager KarmaManager
206  {
207  get;
208  private set;
209  } = new KarmaManager();
210 
211  public bool GameStarted { get; protected set; }
212 
213  public abstract IReadOnlyList<Client> ConnectedClients { get; }
214 
215  public RespawnManager RespawnManager { get; protected set; }
216 
217  public ServerSettings ServerSettings { get; protected set; }
218 
219  public TimeSpan UpdateInterval => new TimeSpan(0, 0, 0, 0, MathHelper.Clamp(1000 / ServerSettings.TickRate, 1, 500));
220 
221  public void AddChatMessage(string message, ChatMessageType type, string senderName = "", Client senderClient = null, Entity senderEntity = null, PlayerConnectionChangeType changeType = PlayerConnectionChangeType.None, Color? textColor = null)
222  {
223  AddChatMessage(ChatMessage.Create(senderName, message, type, senderEntity, senderClient, changeType: changeType, textColor: textColor));
224  }
225 
226  public abstract void AddChatMessage(ChatMessage message);
227 
228  public static string ClientLogName(Client client, string name = null)
229  {
230  if (client == null) { return name; }
231  string retVal = "‖";
232  if (client.Karma < 40.0f)
233  {
234  retVal += "color:#ff9900;";
235  }
236  retVal += "metadata:" + (client.AccountId.TryUnwrap(out var accountId) ? accountId.ToString() : client.SessionId.ToString())
237  + "‖" + (name ?? client.Name).Replace("‖", "") + "‖end‖";
238  return retVal;
239  }
240 
241  public abstract void KickPlayer(string kickedName, string reason);
242 
243  public abstract void BanPlayer(string kickedName, string reason, TimeSpan? duration = null);
244 
245  public abstract void UnbanPlayer(string playerName);
246 
247  public abstract void UnbanPlayer(Endpoint endpoint);
248 
252  public static bool IsCompatible(Version myVersion, Version remoteVersion)
253  {
254  //major.minor.build.revision
255  //revision number is ignored, other values have to match
256  return
257  myVersion.Major == remoteVersion.Major &&
258  myVersion.Minor == remoteVersion.Minor &&
259  myVersion.Build == remoteVersion.Build;
260  }
261  }
262 }
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...