Client LuaCsForBarotrauma
Lobby.cs
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Threading.Tasks;
6 
7 namespace Barotrauma.Steam
8 {
9  static partial class SteamManager
10  {
11  private enum LobbyState
12  {
13  NotConnected,
14  Creating,
15  Owner,
16  Joining,
17  Joined
18  }
19  private static UInt64 lobbyID = 0;
20  private static LobbyState lobbyState = LobbyState.NotConnected;
21  private static Steamworks.Data.Lobby? currentLobby;
22  public static UInt64 CurrentLobbyID
23  {
24  get { return currentLobby?.Id ?? 0; }
25  }
26 
27  public static void CreateLobby(ServerSettings serverSettings)
28  {
29  if (!SteamManager.IsInitialized) { return; }
30  if (lobbyState != LobbyState.NotConnected) { return; }
31  lobbyState = LobbyState.Creating;
32  TaskPool.Add("CreateLobbyAsync", Steamworks.SteamMatchmaking.CreateLobbyAsync(serverSettings.MaxPlayers + 10),
33  (lobby) =>
34  {
35  if (lobbyState != LobbyState.Creating)
36  {
37  LeaveLobby();
38  return;
39  }
40 
41  currentLobby = ((Task<Steamworks.Data.Lobby?>)lobby).Result;
42 
43  if (currentLobby == null)
44  {
45  DebugConsole.ThrowError("Failed to create Steam lobby");
46  lobbyState = LobbyState.NotConnected;
47  return;
48  }
49 
50  DebugConsole.NewMessage("Lobby created!", Microsoft.Xna.Framework.Color.Lime);
51 
52  lobbyState = LobbyState.Owner;
53  lobbyID = (currentLobby?.Id).Value;
54 
55  SetLobbyPublic(serverSettings.IsPublic);
56  currentLobby?.SetJoinable(true);
57 
58  UpdateLobby(serverSettings);
59  });
60  }
61 
62  public static void SetLobbyPublic(bool isPublic)
63  {
64  if (isPublic)
65  {
66  currentLobby?.SetPublic();
67  }
68  else
69  {
70  currentLobby?.SetFriendsOnly();
71  }
72  }
73 
74  public static void UpdateLobby(ServerSettings serverSettings)
75  {
76  if (GameMain.Client == null)
77  {
78  LeaveLobby();
79  return;
80  }
81 
82  if (lobbyState == LobbyState.NotConnected)
83  {
84  CreateLobby(serverSettings);
85  }
86 
87  if (lobbyState != LobbyState.Owner)
88  {
89  return;
90  }
91 
92  serverSettings.UpdateServerListInfo(SetServerListInfo);
93 
94  currentLobby?.SetData("lobbyowner", GetSteamId().TryUnwrap(out var steamId)
95  ? steamId.StringRepresentation
96  : throw new InvalidOperationException("Steamworks not initialized"));
97 
98  if (EosInterface.IdQueries.GetLoggedInPuids() is { Length: > 0 } puids)
99  {
100  currentLobby?.SetData("EosEndpoint", puids[0].Value);
101  }
102 
103  DebugConsole.Log("Lobby updated!");
104  }
105 
106  private static void SetServerListInfo(Identifier key, object value)
107  {
108  switch (value)
109  {
110  case IEnumerable<ContentPackage> contentPackages:
111  currentLobby?.SetData("contentpackage", contentPackages.Select(p => p.Name).JoinEscaped(','));
112  currentLobby?.SetData("contentpackagehash", contentPackages.Select(p => p.Hash.StringRepresentation).JoinEscaped(','));
113  currentLobby?.SetData("contentpackageid", contentPackages
114  .Select(p => p.UgcId.Select(ugcId => ugcId.StringRepresentation).Fallback(""))
115  .JoinEscaped(','));
116  return;
117  }
118 
119  currentLobby?.SetData(key.Value.ToLowerInvariant(), value.ToString());
120  }
121 
122  public static void LeaveLobby()
123  {
124  if (lobbyState != LobbyState.NotConnected)
125  {
126  currentLobby?.Leave(); currentLobby = null;
127  lobbyState = LobbyState.NotConnected;
128 
129  lobbyID = 0;
130 
131  Steamworks.SteamMatchmaking.ResetActions();
132  }
133  }
134  public static void JoinLobby(UInt64 id, bool joinServer)
135  {
136  if (currentLobby.HasValue && currentLobby.Value.Id == id) { return; }
137  if (lobbyID == id) { return; }
138  lobbyState = LobbyState.Joining;
139  lobbyID = id;
140 
141  TaskPool.Add("JoinLobbyAsync", Steamworks.SteamMatchmaking.JoinLobbyAsync(lobbyID),
142  (lobby) =>
143  {
144  currentLobby = ((Task<Steamworks.Data.Lobby?>)lobby).Result;
145  lobbyState = LobbyState.Joined;
146  lobbyID = (currentLobby?.Id).Value;
147  if (joinServer)
148  {
149  GameMain.Instance.ConnectCommand = Option<ConnectCommand>.Some(
150  new ConnectCommand(
151  currentLobby?.GetData("servername") ?? "Server",
152  new SteamP2PEndpoint(new SteamId(currentLobby?.Owner.Id ?? 0))));
153  }
154  });
155  }
156  }
157 }