4 using System.Collections.Generic;
5 using System.Collections.Immutable;
12 readonly record
struct ConnectCommand(
13 Option<ConnectCommand.NameAndP2PEndpoints> NameAndP2PEndpointsOption,
14 Option<ConnectCommand.NameAndLidgrenEndpoint> NameAndLidgrenEndpointOption,
15 Option<ConnectCommand.SteamLobbyId> SteamLobbyIdOption)
17 public bool IsClientConnectedToEndpoint()
19 if (GameMain.Client?.ClientPeer ==
null) {
return false; }
20 if (NameAndP2PEndpointsOption.TryUnwrap(out var nameAndP2PEndpoints))
22 if (nameAndP2PEndpoints.Endpoints.Any(e => e.Equals(GameMain.Client.ClientPeer.ServerEndpoint))) {
return true; }
24 if (NameAndLidgrenEndpointOption.TryUnwrap(out var nameAndLidgrenEndpoint))
26 if (nameAndLidgrenEndpoint.Endpoint.Equals(GameMain.Client.ClientPeer.ServerEndpoint)) {
return true; }
28 if (SteamLobbyIdOption.TryUnwrap(out var steamLobbyId))
30 if (SteamManager.CurrentLobbyID == steamLobbyId.Value) {
return true; }
35 public readonly record
struct NameAndP2PEndpoints(
39 public readonly record
struct NameAndLidgrenEndpoint(
43 public readonly record
struct SteamLobbyId(ulong Value);
45 public ConnectCommand(
string serverName, Endpoint endpoint)
47 NameAndP2PEndpointsOption: endpoint is
P2PEndpoint p2pEndpoint
48 ? Option.Some(new NameAndP2PEndpoints(ServerName: serverName, p2pEndpoint.ToEnumerable().ToImmutableArray()))
50 NameAndLidgrenEndpointOption: endpoint is LidgrenEndpoint lidgrenEndpoint
51 ? Option.Some(new NameAndLidgrenEndpoint(ServerName: serverName, lidgrenEndpoint))
53 SteamLobbyIdOption: Option.
None) { }
55 public ConnectCommand(
string serverName, ImmutableArray<P2PEndpoint> endpoints)
57 NameAndP2PEndpointsOption: Option.Some(new NameAndP2PEndpoints(ServerName: serverName, Endpoints: endpoints)),
58 NameAndLidgrenEndpointOption: Option.
None,
59 SteamLobbyIdOption: Option.
None) { }
61 public ConnectCommand(
string serverName, LidgrenEndpoint endpoint)
63 NameAndP2PEndpointsOption: Option.
None,
64 NameAndLidgrenEndpointOption: Option.Some(new NameAndLidgrenEndpoint(ServerName: serverName,
Endpoint: endpoint)),
65 SteamLobbyIdOption: Option.
None) { }
67 public ConnectCommand(SteamLobbyId lobbyId)
69 NameAndP2PEndpointsOption: Option.
None,
70 NameAndLidgrenEndpointOption: Option.
None,
71 SteamLobbyIdOption: Option.Some(lobbyId)) { }
73 public static Option<ConnectCommand> Parse(
string str)
74 => Parse(ToolBox.SplitCommand(str));
76 public static Option<ConnectCommand> Parse(IReadOnlyList<string>? args)
78 if (args ==
null || args.Count < 2) {
return Option.None; }
80 if (args[0].Equals(
"-connect", StringComparison.OrdinalIgnoreCase))
82 if (args.Count < 3) {
return Option.None; }
84 var serverName = args[1];
86 var endpointStrs = args[2].Split(
",");
87 var endpoints = endpointStrs.Select(
Endpoint.Parse).NotNone().ToImmutableArray();
88 if (endpoints.Length != endpointStrs.Length) {
return Option.None; }
93 new ConnectCommand(serverName, endpoints.Cast<
P2PEndpoint>().ToImmutableArray()));
95 else if (endpoints.Length == 1 && endpoints[0] is LidgrenEndpoint lidgrenEndpoint)
98 new ConnectCommand(serverName, lidgrenEndpoint));
103 else if (args[0].Equals(
"+connect_lobby", StringComparison.OrdinalIgnoreCase))
105 return UInt64.TryParse(args[1], out var lobbyId)
106 ? Option.Some(
new ConnectCommand(
new SteamLobbyId(lobbyId)))
112 public override string ToString()
114 if (SteamLobbyIdOption.TryUnwrap(out var steamLobbyId))
116 return $
"+connect_lobby {steamLobbyId.Value}";
119 if (NameAndP2PEndpointsOption.TryUnwrap(out var nameAndP2PEndpoints))
121 var escapedName = nameAndP2PEndpoints.ServerName.Replace(
"\"",
"\\\"");
122 var escapedEndpoints = nameAndP2PEndpoints.Endpoints.Select(e => e.StringRepresentation).JoinEscaped(
',');
123 return $
"-connect \"{escapedName}\" {escapedEndpoints}";
126 if (NameAndLidgrenEndpointOption.TryUnwrap(out var nameAndLidgrenEndpoint))
128 var escapedName = nameAndLidgrenEndpoint.ServerName.Replace(
"\"",
"\\\"");
129 var endpoint = nameAndLidgrenEndpoint.Endpoint.StringRepresentation;
130 return $
"-connect \"{escapedName}\" {endpoint}";