Client LuaCsForBarotrauma
BarotraumaShared/SharedSource/Networking/Client.cs
1 using Microsoft.Xna.Framework;
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 
6 namespace Barotrauma.Networking
7 {
8  [NetworkSerialize]
9  struct TempClient : INetSerializableStruct
10  {
11  public string Name;
12  public Identifier PreferredJob;
14  public UInt16 NameId;
16  public byte SessionId;
17  public UInt16 CharacterId;
18  public float Karma;
19  public bool Muted;
20  public bool InGame;
21  public bool HasPermissions;
22  public bool IsOwner;
23  public bool IsDownloading;
24  }
25 
26  partial class Client : IDisposable
27  {
28  public const int MaxNameLength = 32;
29 
30  public string Name; public UInt16 NameId;
31 
37  public readonly byte SessionId;
38 
40 
46  public Option<AccountId> AccountId => AccountInfo.AccountId;
47 
49 
50  public UInt16 Ping;
51 
52  public Identifier PreferredJob;
53 
55 
57 
58  private Character character;
60  {
61  get
62  {
63  if (GameMain.NetworkMember != null && GameMain.NetworkMember.IsClient && (character?.ID ?? 0) != CharacterID)
64  {
66  }
67  return character;
68  }
69  set
70  {
71  if (GameMain.NetworkMember != null && GameMain.NetworkMember.IsServer)
72  {
73  GameMain.NetworkMember.LastClientListUpdateID++;
74  if (value != null)
75  {
76  CharacterID = value.ID;
77  }
78  }
79  else
80  {
81  if (value != null)
82  {
83  DebugConsole.NewMessage(value.Name, Color.Yellow);
84  }
85  }
86  character = value;
87  if (character != null)
88  {
89  HasSpawned = true;
90  UsingFreeCam = false;
91 #if CLIENT
92  GameMain.GameSession?.CrewManager?.SetPlayerVoiceIconState(this, muted, mutedLocally);
93 #endif
94  }
95  }
96  }
97 
101  public bool UsingFreeCam;
102 
103  public UInt16 CharacterID;
104 
105  private Vector2 spectatePos;
106  public Vector2? SpectatePos
107  {
108  get
109  {
110  if (character == null || character.IsDead)
111  {
112  return spectatePos;
113  }
114  else
115  {
116  return null;
117  }
118  }
119 
120  set
121  {
122  spectatePos = value.Value;
123  }
124  }
125 
126  public bool Spectating
127  {
128  get
129  {
130  return inGame && character == null;
131  }
132  }
133 
134  private bool muted;
135  public bool Muted
136  {
137  get { return muted; }
138  set
139  {
140  if (muted == value) { return; }
141  muted = value;
142 #if CLIENT
143  GameMain.NetLobbyScreen.SetPlayerVoiceIconState(this, muted, mutedLocally);
144  GameMain.GameSession?.CrewManager?.SetPlayerVoiceIconState(this, muted, mutedLocally);
145 #endif
146  if (GameMain.NetworkMember != null && GameMain.NetworkMember.IsServer)
147  {
148  GameMain.NetworkMember.LastClientListUpdateID++;
149  }
150  }
151  }
152 
154 
156  {
157  get;
158  private set;
159  }
160 
161  private bool inGame;
162  public bool InGame
163  {
164  get
165  {
166  return inGame;
167  }
168  set
169  {
170  if (GameMain.NetworkMember != null && GameMain.NetworkMember.IsServer)
171  {
172  GameMain.NetworkMember.LastClientListUpdateID++;
173  }
174  inGame = value;
175  }
176  }
177  public bool HasSpawned; //has the client spawned as a character during the current round
178 
179  public HashSet<Identifier> GivenAchievements = new HashSet<Identifier>();
180 
182  public readonly HashSet<DebugConsole.Command> PermittedConsoleCommands = new HashSet<DebugConsole.Command>();
183 
184  private readonly object[] votes;
185 
186  partial void InitProjSpecific();
187  partial void DisposeProjSpecific();
188  public Client(string name, byte sessionId)
189  {
190  this.Name = name;
191  this.SessionId = sessionId;
192 
193  votes = new object[Enum.GetNames(typeof(VoteType)).Length];
194 
195  InitProjSpecific();
196  }
197 
198  public T GetVote<T>(VoteType voteType)
199  {
200  return (votes[(int)voteType] is T t) ? t : default;
201  }
202 
203  public void SetVote(VoteType voteType, object value)
204  {
205  votes[(int)voteType] = value;
206  }
207 
208  public bool SessionOrAccountIdMatches(string userId)
209  => (AccountId.IsSome() && Networking.AccountId.Parse(userId) == AccountId)
210  || (byte.TryParse(userId, out byte sessionId) && SessionId == sessionId);
211 
213  {
214  msg.WriteByte(SessionId);
215  msg.WriteRangedInteger((int)Permissions, 0, (int)ClientPermissions.All);
216  if (HasPermission(ClientPermissions.ConsoleCommands))
217  {
218  msg.WriteUInt16((UInt16)PermittedConsoleCommands.Count);
219  foreach (DebugConsole.Command command in PermittedConsoleCommands)
220  {
221  msg.WriteIdentifier(command.Names[0]);
222  }
223  }
224  }
225  public static void ReadPermissions(IReadMessage inc, out ClientPermissions permissions, out List<DebugConsole.Command> permittedCommands)
226  {
227  int permissionsInt = inc.ReadRangedInteger(0, (int)ClientPermissions.All);
228  permissions = ClientPermissions.None;
229  permittedCommands = new List<DebugConsole.Command>();
230  try
231  {
232  permissions = (ClientPermissions)permissionsInt;
233  }
234  catch (InvalidCastException)
235  {
236  return;
237  }
238  if (permissions.HasFlag(ClientPermissions.ConsoleCommands))
239  {
240  UInt16 commandCount = inc.ReadUInt16();
241  for (int i = 0; i < commandCount; i++)
242  {
243  Identifier commandName = inc.ReadIdentifier();
244  var consoleCommand = DebugConsole.Commands.Find(c => c.Names.Contains(commandName));
245  if (consoleCommand != null)
246  {
247  permittedCommands.Add(consoleCommand);
248  }
249  }
250  }
251  }
252 
253  public void ReadPermissions(IReadMessage inc)
254  {
255  ReadPermissions(inc, out ClientPermissions permissions, out List<DebugConsole.Command> permittedCommands);
256  SetPermissions(permissions, permittedCommands);
257  }
258 
259  public static string SanitizeName(string name)
260  {
261  name = name.Trim();
262  if (name.Length > MaxNameLength)
263  {
264  name = name.Substring(0, MaxNameLength);
265  }
266  string rName = "";
267  for (int i = 0; i < name.Length; i++)
268  {
269  rName += name[i] < 32 ? '?' : name[i];
270  }
271  return rName;
272  }
273 
274  public void Dispose()
275  {
276  DisposeProjSpecific();
277  }
278  }
279 }
void SetPlayerVoiceIconState(Client client, bool muted, bool mutedLocally)
readonly ushort ID
Unique, but non-persistent identifier. Stays the same if the entities are created in the exactly same...
Definition: Entity.cs:43
static Entity FindEntityByID(ushort ID)
Find an entity based on the ID
Definition: Entity.cs:204
static GameSession?? GameSession
Definition: GameMain.cs:88
static NetLobbyScreen NetLobbyScreen
Definition: GameMain.cs:55
static NetworkMember NetworkMember
Definition: GameMain.cs:190
void SetPlayerVoiceIconState(Client client, bool muted, bool mutedLocally)
readonly HashSet< DebugConsole.Command > PermittedConsoleCommands
static void ReadPermissions(IReadMessage inc, out ClientPermissions permissions, out List< DebugConsole.Command > permittedCommands)
void SetPermissions(ClientPermissions permissions, IEnumerable< Identifier > permittedConsoleCommands)
Option< AccountId > AccountId
The ID of the account used to authenticate this session. This value can be used as a persistent value...
bool SessionOrAccountIdMatches(string userId)
readonly byte SessionId
An ID for this client for the current session. THIS IS NOT A PERSISTENT VALUE. DO NOT STORE THIS LONG...
bool UsingFreeCam
Is the client using the 'freecam' console command?
int ReadRangedInteger(int min, int max)
void WriteRangedInteger(int val, int min, int max)
void WriteIdentifier(Identifier val)
readonly Option< AccountId > AccountId
The primary ID for a given user
Definition: AccountInfo.cs:15