Client LuaCsForBarotrauma
ConnectCommand.cs
1 #nullable enable
2 
3 using System;
4 using System.Collections.Generic;
5 using System.Collections.Immutable;
6 using System.Linq;
8 using Barotrauma.Steam;
9 
10 namespace Barotrauma.Networking;
11 
12 readonly record struct ConnectCommand(
13  Option<ConnectCommand.NameAndP2PEndpoints> NameAndP2PEndpointsOption,
14  Option<ConnectCommand.NameAndLidgrenEndpoint> NameAndLidgrenEndpointOption,
15  Option<ConnectCommand.SteamLobbyId> SteamLobbyIdOption)
16 {
17  public bool IsClientConnectedToEndpoint()
18  {
19  if (GameMain.Client?.ClientPeer == null) { return false; }
20  if (NameAndP2PEndpointsOption.TryUnwrap(out var nameAndP2PEndpoints))
21  {
22  if (nameAndP2PEndpoints.Endpoints.Any(e => e.Equals(GameMain.Client.ClientPeer.ServerEndpoint))) { return true; }
23  }
24  if (NameAndLidgrenEndpointOption.TryUnwrap(out var nameAndLidgrenEndpoint))
25  {
26  if (nameAndLidgrenEndpoint.Endpoint.Equals(GameMain.Client.ClientPeer.ServerEndpoint)) { return true; }
27  }
28  if (SteamLobbyIdOption.TryUnwrap(out var steamLobbyId))
29  {
30  if (SteamManager.CurrentLobbyID == steamLobbyId.Value) { return true; }
31  }
32  return false;
33  }
34 
35  public readonly record struct NameAndP2PEndpoints(
36  string ServerName,
37  ImmutableArray<P2PEndpoint> Endpoints);
38 
39  public readonly record struct NameAndLidgrenEndpoint(
40  string ServerName,
41  LidgrenEndpoint Endpoint);
42 
43  public readonly record struct SteamLobbyId(ulong Value);
44 
45  public ConnectCommand(string serverName, Endpoint endpoint)
46  : this(
47  NameAndP2PEndpointsOption: endpoint is P2PEndpoint p2pEndpoint
48  ? Option.Some(new NameAndP2PEndpoints(ServerName: serverName, p2pEndpoint.ToEnumerable().ToImmutableArray()))
49  : Option.None,
50  NameAndLidgrenEndpointOption: endpoint is LidgrenEndpoint lidgrenEndpoint
51  ? Option.Some(new NameAndLidgrenEndpoint(ServerName: serverName, lidgrenEndpoint))
52  : Option.None,
53  SteamLobbyIdOption: Option.None) { }
54 
55  public ConnectCommand(string serverName, ImmutableArray<P2PEndpoint> endpoints)
56  : this(
57  NameAndP2PEndpointsOption: Option.Some(new NameAndP2PEndpoints(ServerName: serverName, Endpoints: endpoints)),
58  NameAndLidgrenEndpointOption: Option.None,
59  SteamLobbyIdOption: Option.None) { }
60 
61  public ConnectCommand(string serverName, LidgrenEndpoint endpoint)
62  : this(
63  NameAndP2PEndpointsOption: Option.None,
64  NameAndLidgrenEndpointOption: Option.Some(new NameAndLidgrenEndpoint(ServerName: serverName, Endpoint: endpoint)),
65  SteamLobbyIdOption: Option.None) { }
66 
67  public ConnectCommand(SteamLobbyId lobbyId)
68  : this(
69  NameAndP2PEndpointsOption: Option.None,
70  NameAndLidgrenEndpointOption: Option.None,
71  SteamLobbyIdOption: Option.Some(lobbyId)) { }
72 
73  public static Option<ConnectCommand> Parse(string str)
74  => Parse(ToolBox.SplitCommand(str));
75 
76  public static Option<ConnectCommand> Parse(IReadOnlyList<string>? args)
77  {
78  if (args == null || args.Count < 2) { return Option.None; }
79 
80  if (args[0].Equals("-connect", StringComparison.OrdinalIgnoreCase))
81  {
82  if (args.Count < 3) { return Option.None; }
83 
84  var serverName = args[1];
85 
86  var endpointStrs = args[2].Split(",");
87  var endpoints = endpointStrs.Select(Endpoint.Parse).NotNone().ToImmutableArray();
88  if (endpoints.Length != endpointStrs.Length) { return Option.None; }
89 
90  if (endpoints.All(e => e is P2PEndpoint))
91  {
92  return Option.Some(
93  new ConnectCommand(serverName, endpoints.Cast<P2PEndpoint>().ToImmutableArray()));
94  }
95  else if (endpoints.Length == 1 && endpoints[0] is LidgrenEndpoint lidgrenEndpoint)
96  {
97  return Option.Some(
98  new ConnectCommand(serverName, lidgrenEndpoint));
99  }
100 
101  return Option.None;
102  }
103  else if (args[0].Equals("+connect_lobby", StringComparison.OrdinalIgnoreCase))
104  {
105  return UInt64.TryParse(args[1], out var lobbyId)
106  ? Option.Some(new ConnectCommand(new SteamLobbyId(lobbyId)))
107  : Option.None;
108  }
109  return Option.None;
110  }
111 
112  public override string ToString()
113  {
114  if (SteamLobbyIdOption.TryUnwrap(out var steamLobbyId))
115  {
116  return $"+connect_lobby {steamLobbyId.Value}";
117  }
118 
119  if (NameAndP2PEndpointsOption.TryUnwrap(out var nameAndP2PEndpoints))
120  {
121  var escapedName = nameAndP2PEndpoints.ServerName.Replace("\"", "\\\"");
122  var escapedEndpoints = nameAndP2PEndpoints.Endpoints.Select(e => e.StringRepresentation).JoinEscaped(',');
123  return $"-connect \"{escapedName}\" {escapedEndpoints}";
124  }
125 
126  if (NameAndLidgrenEndpointOption.TryUnwrap(out var nameAndLidgrenEndpoint))
127  {
128  var escapedName = nameAndLidgrenEndpoint.ServerName.Replace("\"", "\\\"");
129  var endpoint = nameAndLidgrenEndpoint.Endpoint.StringRepresentation;
130  return $"-connect \"{escapedName}\" {endpoint}";
131  }
132 
133  return "";
134  }
135 }