3 using System.Collections.Generic;
5 using System.Threading.Tasks;
9 static partial class SteamManager
11 private enum LobbyState
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
24 get {
return currentLobby?.Id ?? 0; }
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),
35 if (lobbyState != LobbyState.Creating)
41 currentLobby = ((Task<Steamworks.Data.Lobby?>)lobby).Result;
43 if (currentLobby ==
null)
45 DebugConsole.ThrowError(
"Failed to create Steam lobby");
46 lobbyState = LobbyState.NotConnected;
50 DebugConsole.NewMessage(
"Lobby created!", Microsoft.Xna.Framework.Color.Lime);
52 lobbyState = LobbyState.Owner;
53 lobbyID = (currentLobby?.Id).Value;
55 SetLobbyPublic(serverSettings.
IsPublic);
56 currentLobby?.SetJoinable(
true);
58 UpdateLobby(serverSettings);
62 public static void SetLobbyPublic(
bool isPublic)
66 currentLobby?.SetPublic();
70 currentLobby?.SetFriendsOnly();
76 if (GameMain.Client ==
null)
82 if (lobbyState == LobbyState.NotConnected)
84 CreateLobby(serverSettings);
87 if (lobbyState != LobbyState.Owner)
94 currentLobby?.SetData(
"lobbyowner", GetSteamId().TryUnwrap(out var steamId)
95 ? steamId.StringRepresentation
96 :
throw new InvalidOperationException(
"Steamworks not initialized"));
98 if (EosInterface.IdQueries.GetLoggedInPuids() is { Length: > 0 } puids)
100 currentLobby?.SetData(
"EosEndpoint", puids[0].Value);
103 DebugConsole.Log(
"Lobby updated!");
106 private static void SetServerListInfo(Identifier key,
object value)
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(
""))
119 currentLobby?.SetData(key.Value.ToLowerInvariant(), value.ToString());
122 public static void LeaveLobby()
124 if (lobbyState != LobbyState.NotConnected)
126 currentLobby?.Leave(); currentLobby =
null;
127 lobbyState = LobbyState.NotConnected;
131 Steamworks.SteamMatchmaking.ResetActions();
134 public static void JoinLobby(UInt64
id,
bool joinServer)
136 if (currentLobby.HasValue && currentLobby.Value.Id ==
id) {
return; }
137 if (lobbyID ==
id) {
return; }
138 lobbyState = LobbyState.Joining;
141 TaskPool.Add(
"JoinLobbyAsync", Steamworks.SteamMatchmaking.JoinLobbyAsync(lobbyID),
144 currentLobby = ((Task<Steamworks.Data.Lobby?>)lobby).Result;
145 lobbyState = LobbyState.Joined;
146 lobbyID = (currentLobby?.Id).Value;
149 GameMain.Instance.ConnectCommand = Option<ConnectCommand>.Some(
151 currentLobby?.GetData(
"servername") ??
"Server",
152 new SteamP2PEndpoint(new SteamId(currentLobby?.Owner.Id ?? 0))));
void UpdateServerListInfo(Action< Identifier, object > setter)