1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
| using Netcode.Transports.Facepunch;
using Steamworks;
using Steamworks.Data;
using System.Collections.Generic;
using System.Threading.Tasks;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameNetworkManager : MonoBehaviour
{
public static GameNetworkManager Instance { get; private set; } = null;
private FacepunchTransport transport;
public Lobby? CurrentLobby { get; private set; } = null;
public List<Lobby> Lobbies { get; private set; } = new List<Lobby>(capacity: 100);
private void Awake()
{
if (Instance == null)
Instance = this;
else
{
Destroy(gameObject);
return;
}
DontDestroyOnLoad(gameObject);
}
private void Start()
{
#if UNITY_EDITOR
Debug.unityLogger.logEnabled = true;
#else
Debug.unityLogger.logEnabled = Debug.isDebugBuild;
#endif
transport = NetworkManager.Singleton.GetComponent<FacepunchTransport>();
SteamMatchmaking.OnLobbyCreated += OnLobbyCreated;
SteamMatchmaking.OnLobbyEntered += OnLobbyEntered;
SteamMatchmaking.OnLobbyMemberJoined += OnLobbyMemberJoined;
SteamMatchmaking.OnLobbyMemberLeave += OnLobbyMemberLeave;
SteamMatchmaking.OnLobbyInvite += OnLobbyInvite;
SteamFriends.OnGameLobbyJoinRequested += OnGameLobbyJoinRequested;
}
private void OnDestroy()
{
SteamMatchmaking.OnLobbyCreated -= OnLobbyCreated;
SteamMatchmaking.OnLobbyEntered -= OnLobbyEntered;
SteamMatchmaking.OnLobbyMemberJoined -= OnLobbyMemberJoined;
SteamMatchmaking.OnLobbyMemberLeave -= OnLobbyMemberLeave;
SteamMatchmaking.OnLobbyInvite -= OnLobbyInvite;
SteamFriends.OnGameLobbyJoinRequested -= OnGameLobbyJoinRequested;
if (NetworkManager.Singleton == null)
return;
NetworkManager.Singleton.OnClientConnectedCallback -= OnClientConnectedCallback;
NetworkManager.Singleton.OnClientDisconnectCallback -= OnClientDisconnectCallback;
NetworkManager.Singleton.OnServerStarted -= OnServerStarted;
}
private void OnApplicationQuit() => Disconnect();
public async void StartHost(uint maxMembers)
{
NetworkManager.Singleton.OnClientConnectedCallback += OnClientConnectedCallback;
NetworkManager.Singleton.OnClientDisconnectCallback += OnClientDisconnectCallback;
NetworkManager.Singleton.OnServerStarted += OnServerStarted;
NetworkManager.Singleton.StartHost();
CurrentLobby = await SteamMatchmaking.CreateLobbyAsync((int)maxMembers);
}
public void StartClient(SteamId id)
{
NetworkManager.Singleton.OnClientConnectedCallback += ClientConnected;
NetworkManager.Singleton.OnClientDisconnectCallback += ClientDisconnected;
transport.targetSteamId = id;
Debug.Log($"Joining room hosted by {transport.targetSteamId}", this);
if (NetworkManager.Singleton.StartClient())
Debug.Log("Client has joined!", this);
}
public void Disconnect()
{
CurrentLobby?.Leave();
if (NetworkManager.Singleton == null)
return;
NetworkManager.Singleton.Shutdown();
}
public async Task<bool> RefreshLobbies(int maxResults = 20)
{
try
{
Lobbies.Clear();
var lobbies = await SteamMatchmaking.LobbyList
.FilterDistanceClose()
.WithMaxResults(maxResults)
.RequestAsync();
if (lobbies != null)
{
for (int i = 0; i < lobbies.Length; i++)
Lobbies.Add(lobbies[i]);
}
return true;
}
catch (System.Exception ex)
{
Debug.Log("Error fetching lobbies", this);
Debug.LogException(ex, this);
return false;
}
}
private Steamworks.ServerList.Internet GetInternetRequest()
{
var request = new Steamworks.ServerList.Internet();
//request.AddFilter("secure", "1");
//request.AddFilter("and", "1");
//request.AddFilter("gametype", "1");
return request;
}
#region Steam Callbacks
private void OnGameLobbyJoinRequested(Lobby lobby, SteamId id)
{
bool isSame = lobby.Owner.Id.Equals(id);
Debug.Log($"Owner: {lobby.Owner}");
Debug.Log($"Id: {id}");
Debug.Log($"IsSame: {isSame}", this);
StartClient(id);
}
private void OnLobbyInvite(Friend friend, Lobby lobby) => Debug.Log($"You got a invite from {friend.Name}", this);
private void OnLobbyMemberLeave(Lobby lobby, Friend friend) { }
private void OnLobbyMemberJoined(Lobby lobby, Friend friend) { }
private void OnLobbyEntered(Lobby lobby)
{
Debug.Log($"You have entered in lobby, clientId={NetworkManager.Singleton.LocalClientId}", this);
if (NetworkManager.Singleton.IsHost)
return;
StartClient(lobby.Owner.Id);
}
private void OnLobbyCreated(Result result, Lobby lobby)
{
if (result != Result.OK)
{
Debug.LogError($"Lobby couldn't be created!, {result}", this);
return;
}
lobby.SetFriendsOnly(); // Set to friends only!
lobby.SetData("name", "Random Cool Lobby");
lobby.SetJoinable(true);
var status = NetworkManager.Singleton.SceneManager.LoadScene("Save1", LoadSceneMode.Single);
if (status != SceneEventProgressStatus.Started)
{
Debug.LogWarning($"Failed to load Save1 with a {nameof(SceneEventProgressStatus)}: {status}");
}
Debug.Log("Lobby has been created!");
}
#endregion
#region Network Callbacks
private void ClientConnected(ulong clientId) => Debug.Log($"I'm connected, clientId={clientId}");
private void ClientDisconnected(ulong clientId)
{
Debug.Log($"I'm disconnected, clientId={clientId}");
NetworkManager.Singleton.OnClientDisconnectCallback -= ClientDisconnected;
NetworkManager.Singleton.OnClientConnectedCallback -= ClientConnected;
}
private void OnServerStarted() { }
private void OnClientConnectedCallback(ulong clientId) => Debug.Log($"Client connected, clientId={clientId}", this);
private void OnClientDisconnectCallback(ulong clientId) => Debug.Log($"Client disconnected, clientId={clientId}", this);
#endregion
} |