Skip to content

Commit 84cb5d3

Browse files
cherrypick fix: null refs when quitting (#563) (#572)
* Adding try/catch to LeaveBeforeQuit to make sure that we quit even if exceptions happen (#563) (cherry picked from commit eecfe7e) * Removed unneeded ForceLeaveLobbyAttempt and unneeded return types
1 parent 107af0f commit 84cb5d3

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

Assets/BossRoom/Scripts/Shared/ApplicationController.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
using System;
12
using System.Collections;
23
using BossRoom.Scripts.Shared.Net.UnityServices.Auth;
34
using Unity.Multiplayer.Samples.BossRoom.Client;
45
using Unity.Multiplayer.Samples.BossRoom.Server;
56
using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
67
using Unity.Multiplayer.Samples.BossRoom.Shared.Net.UnityServices.Infrastructure;
78
using Unity.Multiplayer.Samples.BossRoom.Shared.Net.UnityServices.Lobbies;
8-
using Unity.Netcode;
99
using UnityEngine;
1010
using UnityEngine.SceneManagement;
1111

@@ -77,7 +77,7 @@ private void Start()
7777

7878
private void OnDestroy()
7979
{
80-
m_LobbyServiceFacade.ForceLeaveLobbyAttempt();
80+
m_LobbyServiceFacade.EndTracking();
8181
DIScope.RootScope.Dispose();
8282
}
8383

@@ -87,21 +87,32 @@ private void OnDestroy()
8787
/// </summary>
8888
private IEnumerator LeaveBeforeQuit()
8989
{
90-
m_LobbyServiceFacade.ForceLeaveLobbyAttempt();
90+
// We want to quit anyways, so if anything happens while trying to leave the Lobby, log the exception then carry on
91+
try
92+
{
93+
m_LobbyServiceFacade.EndTracking();
94+
}
95+
catch (Exception e)
96+
{
97+
Debug.LogError(e.Message);
98+
}
9199
yield return null;
92100
Application.Quit();
93101
}
94102

95103
private bool OnWantToQuit()
96104
{
97105
var canQuit = string.IsNullOrEmpty(m_LocalLobby?.LobbyID);
98-
StartCoroutine(LeaveBeforeQuit());
106+
if (canQuit)
107+
{
108+
StartCoroutine(LeaveBeforeQuit());
109+
}
99110
return canQuit;
100111
}
101112

102113
public void LeaveSession()
103114
{
104-
m_LobbyServiceFacade.ForceLeaveLobbyAttempt();
115+
m_LobbyServiceFacade.EndTracking();
105116

106117
// first disconnect then return to menu
107118
var gameNetPortal = GameNetPortal.Instance;

Assets/BossRoom/Scripts/Shared/Net/UnityServices/Lobbies/LobbyAPIInterface.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,18 @@ public void DeleteLobbyAsync(string lobbyId, Action onComplete, Action onFailed)
8787
RunTask(task, onComplete, onFailed);
8888
}
8989

90-
public Task JoinLobbyAsync_ByCode(string requesterUasId, string lobbyCode, Dictionary<string, PlayerDataObject> localUserData, Action<Lobby> onComplete, Action onFailed)
90+
public void JoinLobbyAsync_ByCode(string requesterUasId, string lobbyCode, Dictionary<string, PlayerDataObject> localUserData, Action<Lobby> onComplete, Action onFailed)
9191
{
9292
JoinLobbyByCodeOptions joinOptions = new JoinLobbyByCodeOptions { Player = new Player(id: requesterUasId, data: localUserData) };
9393
var task = Lobbies.Instance.JoinLobbyByCodeAsync(lobbyCode, joinOptions);
9494
RunTask(task, onComplete, onFailed);
95-
return task;
9695
}
9796

98-
public Task JoinLobbyAsync_ById(string requesterUasId, string lobbyId, Dictionary<string, PlayerDataObject> localUserData, Action<Lobby> onComplete, Action onFailed)
97+
public void JoinLobbyAsync_ById(string requesterUasId, string lobbyId, Dictionary<string, PlayerDataObject> localUserData, Action<Lobby> onComplete, Action onFailed)
9998
{
10099
JoinLobbyByIdOptions joinOptions = new JoinLobbyByIdOptions { Player = new Player(id: requesterUasId, data: localUserData) };
101100
var task = Lobbies.Instance.JoinLobbyByIdAsync(lobbyId, joinOptions);
102101
RunTask(task, onComplete, onFailed);
103-
return task;
104102
}
105103

106104
public void QuickJoinLobbyAsync(string requesterUasId, Dictionary<string, PlayerDataObject> localUserData, Action<Lobby> onComplete, Action onFailed)
@@ -115,11 +113,10 @@ public void QuickJoinLobbyAsync(string requesterUasId, Dictionary<string, Player
115113
RunTask(task, onComplete, onFailed);
116114
}
117115

118-
public Task LeaveLobbyAsync(string requesterUasId, string lobbyId, Action onComplete, Action onFailed)
116+
public void LeaveLobbyAsync(string requesterUasId, string lobbyId, Action onComplete, Action onFailed)
119117
{
120118
var task = Lobbies.Instance.RemovePlayerAsync(lobbyId, requesterUasId);
121119
RunTask(task, onComplete, onFailed);
122-
return task;
123120
}
124121

125122
public void QueryAllLobbiesAsync(Action<QueryResponse> onComplete, Action onFailed)

Assets/BossRoom/Scripts/Shared/Net/UnityServices/Lobbies/LobbyServiceFacade.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,15 @@ public void BeginTracking()
9292
}
9393
}
9494

95-
public Task EndTracking()
95+
public void EndTracking()
9696
{
97-
var task = Task.CompletedTask;
9897
if (CurrentUnityLobby != null)
9998
{
10099
CurrentUnityLobby = null;
101100

102101
if (!string.IsNullOrEmpty(m_LocalLobby?.LobbyID))
103102
{
104-
task = LeaveLobbyAsync(m_LocalLobby?.LobbyID, null, null);
103+
LeaveLobbyAsync(m_LocalLobby?.LobbyID, null, null);
105104
}
106105

107106
m_LocalUser.ResetState();
@@ -115,7 +114,6 @@ public Task EndTracking()
115114
m_HeartbeatTime = 0;
116115
m_JoinedLobbyContentHeartbeat.EndTracking();
117116
}
118-
return task;
119117
}
120118

121119
void UpdateLobby(float unused)
@@ -138,7 +136,7 @@ void OnSuccess(Lobby lobby)
138136
}
139137
}
140138
m_UnityServiceErrorMessagePub.Publish(new UnityServiceErrorMessage("Host left the lobby","Disconnecting.", UnityServiceErrorMessage.Service.Lobby));
141-
ForceLeaveLobbyAttempt();
139+
EndTracking();
142140
// no need to disconnect Netcode, it should already be handled by Netcode's callback to disconnect
143141
}
144142
}
@@ -169,24 +167,24 @@ public void CreateLobbyAsync(string lobbyName, int maxPlayers, bool isPrivate, O
169167
/// <summary>
170168
/// Attempt to join an existing lobby. Will try to join via code, if code is null - will try to join via ID.
171169
/// </summary>
172-
public Task JoinLobbyAsync(string lobbyId, string lobbyCode, Action<Lobby> onSuccess, Action onFailure)
170+
public void JoinLobbyAsync(string lobbyId, string lobbyCode, Action<Lobby> onSuccess, Action onFailure)
173171
{
174172
if (!m_RateLimitJoin.CanCall ||
175173
(lobbyId == null && lobbyCode == null))
176174
{
177175
onFailure?.Invoke();
178176
UnityEngine.Debug.LogWarning("Join Lobby hit the rate limit.");
179-
return Task.CompletedTask;
177+
return;
180178
}
181179
m_RateLimitJoin.PutOnCooldown();
182180

183181
if (!string.IsNullOrEmpty(lobbyCode))
184182
{
185-
return m_LobbyApiInterface.JoinLobbyAsync_ByCode(AuthenticationService.Instance.PlayerId, lobbyCode, m_LocalUser.GetDataForUnityServices(), onSuccess, onFailure);
183+
m_LobbyApiInterface.JoinLobbyAsync_ByCode(AuthenticationService.Instance.PlayerId, lobbyCode, m_LocalUser.GetDataForUnityServices(), onSuccess, onFailure);
186184
}
187185
else
188186
{
189-
return m_LobbyApiInterface.JoinLobbyAsync_ById(AuthenticationService.Instance.PlayerId, lobbyId, m_LocalUser.GetDataForUnityServices(), onSuccess, onFailure);
187+
m_LobbyApiInterface.JoinLobbyAsync_ById(AuthenticationService.Instance.PlayerId, lobbyId, m_LocalUser.GetDataForUnityServices(), onSuccess, onFailure);
190188
}
191189
}
192190

@@ -243,10 +241,10 @@ void RetrieveLobbyAsync(string lobbyId, Action<Lobby> onSuccess, Action onFailur
243241
/// <summary>
244242
/// Attempt to leave a lobby, and then delete it if no players remain.
245243
/// </summary>
246-
public Task LeaveLobbyAsync(string lobbyId, Action onSuccess, Action onFailure)
244+
public void LeaveLobbyAsync(string lobbyId, Action onSuccess, Action onFailure)
247245
{
248246
string uasId = AuthenticationService.Instance.PlayerId;
249-
return m_LobbyApiInterface.LeaveLobbyAsync(uasId, lobbyId, onSuccess, onFailure);
247+
m_LobbyApiInterface.LeaveLobbyAsync(uasId, lobbyId, onSuccess, onFailure);
250248
}
251249

252250
public void RemovePlayerFromLobbyAsync(string uasId, string lobbyId, Action onSuccess, Action onFailure)
@@ -406,10 +404,5 @@ public void DoLobbyHeartbeat(float dt)
406404
m_LobbyApiInterface.HeartbeatPlayerAsync(CurrentUnityLobby.Id);
407405
}
408406
}
409-
410-
public void ForceLeaveLobbyAttempt()
411-
{
412-
EndTracking();
413-
}
414407
}
415408
}

0 commit comments

Comments
 (0)