Skip to content

Commit 72bfd1d

Browse files
committed
Merge branch 'release/GDC2022' into sam/fix/some-cleanup-cherrypick
* release/GDC2022: feat: authentication failure message popups will tell the user to try direct ip connection option (#547) feat: replace guid with auth playerid for session management (#488) (#548) # Conflicts: # Assets/BossRoom/Scripts/Client/UI/UnityServicesUIHandler.cs
2 parents b9db4b9 + f3fad54 commit 72bfd1d

File tree

13 files changed

+161
-142
lines changed

13 files changed

+161
-142
lines changed

Assets/BossRoom/Scripts/Client/Game/State/ClientMainMenuState.cs

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
using BossRoom.Scripts.Shared.Net.UnityServices.Auth;
2+
using Unity.Multiplayer.Samples.BossRoom.Shared;
23
using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
34
using Unity.Multiplayer.Samples.BossRoom.Shared.Net.UnityServices.Lobbies;
45
using Unity.Multiplayer.Samples.BossRoom.Visual;
56
using Unity.Services.Authentication;
67
using Unity.Services.Core;
78
using UnityEngine;
89

9-
#if UNITY_EDITOR
10-
using ParrelSync;
11-
#endif
1210

1311
namespace Unity.Multiplayer.Samples.BossRoom.Client
1412
{
@@ -21,7 +19,6 @@ namespace Unity.Multiplayer.Samples.BossRoom.Client
2119
/// </remarks>
2220
public class ClientMainMenuState : GameStateBehaviour
2321
{
24-
public const string AuthProfileCommandLineArg = "-AuthProfile";
2522
public override GameState ActiveState { get { return GameState.MainMenu; } }
2623

2724
[SerializeField] GameObject[] m_GameObjectsThatWillBeInjectedAutomatically;
@@ -51,33 +48,11 @@ void InjectDependenciesAndInitialize(AuthenticationServiceFacade authServiceFaca
5148
m_Scope.BindInstanceAsSingle(m_IPUIMediator);
5249

5350
var unityAuthenticationInitOptions = new InitializationOptions();
54-
55-
#if UNITY_EDITOR
56-
//The code below makes it possible for the clone instance to log in as a different user profile in Authentication service.
57-
//This allows us to test services integration locally by utilising Parrelsync.
58-
if (ClonesManager.IsClone())
59-
{
60-
Debug.Log("This is a clone project.");
61-
var customArguments = ClonesManager.GetArgument().Split(',');
62-
63-
//second argument is our custom ID, but if it's not set we would just use some default.
64-
65-
var hardcodedProfileID = customArguments.Length > 1 ? customArguments[1] : "defaultCloneID";
66-
67-
unityAuthenticationInitOptions.SetProfile(hardcodedProfileID);
68-
}
69-
#else
70-
var arguments = System.Environment.GetCommandLineArgs();
71-
for (int i = 0; i < arguments.Length; i++)
51+
var profile = ProfileManager.Profile;
52+
if (profile.Length > 0)
7253
{
73-
if (arguments[i] == AuthProfileCommandLineArg)
74-
{
75-
var profileId = arguments[i + 1];
76-
unityAuthenticationInitOptions.SetProfile(profileId);
77-
break;
78-
}
54+
unityAuthenticationInitOptions.SetProfile(profile);
7955
}
80-
#endif
8156

8257
authServiceFacade.DoSignInAsync(OnAuthSignIn, OnSignInFailed, unityAuthenticationInitOptions);
8358

@@ -103,7 +78,6 @@ void OnAuthSignIn()
10378
void OnSignInFailed()
10479
{
10580
m_SignInSpinner.SetActive(false);
106-
PopupPanel.ShowPopupPanel("Authentication Error", "For some reason we can't authenticate the user anonymously - that typically means that project is not properly set up with Unity services.");
10781
}
10882
}
10983

Assets/BossRoom/Scripts/Client/UI/UnityServicesUIHandler.cs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System;
2-
using Unity.Multiplayer.Samples.BossRoom.Client;
2+
using Unity.Multiplayer.Samples.BossRoom.Shared;
33
using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
44
using Unity.Multiplayer.Samples.BossRoom.Shared.Net.UnityServices.Infrastructure;
55
using Unity.Services.Lobbies;
@@ -25,18 +25,33 @@ void Initialize(ISubscriber<UnityServiceErrorMessage> serviceError)
2525
void ServiceErrorHandler(UnityServiceErrorMessage error)
2626
{
2727
var errorMessage = error.Message;
28-
if (error.AffectedService == UnityServiceErrorMessage.Service.Lobby)
28+
switch (error.AffectedService)
2929
{
30-
if (error.OriginalException is LobbyServiceException {Reason: LobbyExceptionReason.LobbyConflict})
30+
case UnityServiceErrorMessage.Service.Lobby:
3131
{
32-
// LobbyConflict can have multiple causes. Let's add other solutions here if there's other situations that arise for this.
33-
errorMessage += "\nSee logs for possible causes and solution.";
34-
Debug.LogError($"Got service error {error.Message} with LobbyConflict. Possible conflict cause: Trying to play with two builds on the " +
35-
$"same machine. Please use command line arg '{ClientMainMenuState.AuthProfileCommandLineArg} someName' to set a different auth profile.\n");
32+
if ((error.OriginalException is LobbyServiceException {Reason: LobbyExceptionReason.LobbyConflict}))
33+
{
34+
// LobbyConflict can have multiple causes. Let's add other solutions here if there's other situations that arise for this.
35+
errorMessage += "\nSee logs for possible causes and solution.";
36+
Debug.LogError($"Got service error {error.Message} with LobbyConflict. Possible conflict cause: Trying to play with two builds on the " +
37+
$"same machine. Please use command line arg '{ProfileManager.AuthProfileCommandLineArg} someName' to set a different auth profile.\n");
38+
}
39+
PopupPanel.ShowPopupPanel("Service error", errorMessage);
40+
break;
41+
}
42+
case UnityServiceErrorMessage.Service.Authentication:
43+
{
44+
PopupPanel.ShowPopupPanel(
45+
"Authentication Error",
46+
$"{error.OriginalException.Message} \n tip: You can still use the Direct IP connection option.");
47+
break;
48+
}
49+
default:
50+
{
51+
PopupPanel.ShowPopupPanel("Service error: "+error.Title, errorMessage);
52+
break;
3653
}
3754
}
38-
39-
PopupPanel.ShowPopupPanel("Service error: "+error.Title, errorMessage);
4055
}
4156

4257
void OnDestroy()

Assets/BossRoom/Scripts/Client/com.unity.multiplayer.samples.bossroom.client.asmdef

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
"Photon Realtime Transport for Netcode for GameObjects",
1313
"Unity.Services.Relay",
1414
"Unity.Multiplayer.Samples.Utilities",
15-
"Unity.Services.Lobbies",
16-
"ParrelSync"
15+
"Unity.Services.Lobbies"
1716
],
1817
"includePlatforms": [],
1918
"excludePlatforms": [],
@@ -24,4 +23,4 @@
2423
"defineConstraints": [],
2524
"versionDefines": [],
2625
"noEngineReferences": false
27-
}
26+
}

Assets/BossRoom/Scripts/Shared/ClientPrefs.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ namespace Unity.Multiplayer.Samples.BossRoom.Client
77
/// (This is just a wrapper around the PlayerPrefs system,
88
/// so that all the calls are in the same place.)
99
/// </summary>
10-
public class ClientPrefs
10+
public static class ClientPrefs
1111
{
12-
private const float k_DefaultMasterVolume = 0.5f;
13-
private const float k_DefaultMusicVolume = 0.8f;
12+
const float k_DefaultMasterVolume = 0.5f;
13+
const float k_DefaultMusicVolume = 0.8f;
1414

1515
public static float GetMasterVolume()
1616
{

Assets/BossRoom/Scripts/Shared/Net/ConnectionManagement/ClientGameNetPortal.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,9 @@ async void ConnectClient(Action<string> onFailure)
274274
}
275275
}
276276

277-
var clientGuid = ClientPrefs.GetGuid();
278277
var payload = JsonUtility.ToJson(new ConnectionPayload()
279278
{
280-
clientGUID = clientGuid,
279+
playerId = m_Portal.GetPlayerId(),
281280
clientScene = SceneManager.GetActiveScene().buildIndex,
282281
playerName = m_Portal.PlayerName
283282
});

Assets/BossRoom/Scripts/Shared/Net/ConnectionManagement/GameNetPortal.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using System;
2-
using System.Collections;
32
using Unity.Multiplayer.Samples.BossRoom.Client;
43
using Unity.Multiplayer.Samples.BossRoom.Server;
4+
using Unity.Multiplayer.Samples.BossRoom.Shared;
55
using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
66
using Unity.Multiplayer.Samples.BossRoom.Shared.Net.UnityServices.Lobbies;
77
using Unity.Netcode;
88
using Unity.Netcode.Transports.UNET;
9+
using Unity.Services.Authentication;
910
using UnityEngine;
1011
using UnityEngine.SceneManagement;
1112

@@ -34,7 +35,7 @@ public enum OnlineMode
3435
[Serializable]
3536
public class ConnectionPayload
3637
{
37-
public string clientGUID;
38+
public string playerId;
3839
public int clientScene = -1;
3940
public string playerName;
4041
}
@@ -248,5 +249,10 @@ public void RequestDisconnect()
248249
m_ClientPortal.OnUserDisconnectRequest();
249250
m_ServerPortal.OnUserDisconnectRequest();
250251
}
252+
253+
public string GetPlayerId()
254+
{
255+
return AuthenticationService.Instance.IsSignedIn ? AuthenticationService.Instance.PlayerId : ClientPrefs.GetGuid() + ProfileManager.Profile;
256+
}
251257
}
252258
}

Assets/BossRoom/Scripts/Shared/Net/ConnectionManagement/ServerGameNetPortal.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ private void ApprovalCheck(byte[] connectionData, ulong clientId, NetworkManager
162162
// Approval check happens for Host too, but obviously we want it to be approved
163163
if (clientId == NetworkManager.Singleton.LocalClientId)
164164
{
165-
SessionManager<SessionPlayerData>.Instance.AddHostData(
165+
SessionManager<SessionPlayerData>.Instance.SetupConnectingPlayerSessionData(clientId, m_Portal.GetPlayerId(),
166166
new SessionPlayerData(clientId, m_Portal.PlayerName, m_Portal.AvatarRegistry.GetRandomAvatar().Guid.ToNetworkGuid(), 0, true));
167167

168168
connectionApprovedCallback(true, null, true, null, null);
@@ -192,9 +192,9 @@ private void ApprovalCheck(byte[] connectionData, ulong clientId, NetworkManager
192192

193193
int clientScene = connectionPayload.clientScene;
194194

195-
Debug.Log("Host ApprovalCheck: connecting client GUID: " + connectionPayload.clientGUID);
195+
Debug.Log("Host ApprovalCheck: connecting client with player ID: " + connectionPayload.playerId);
196196

197-
gameReturnStatus = SessionManager<SessionPlayerData>.Instance.SetupConnectingPlayerSessionData(clientId, connectionPayload.clientGUID,
197+
gameReturnStatus = SessionManager<SessionPlayerData>.Instance.SetupConnectingPlayerSessionData(clientId, connectionPayload.playerId,
198198
new SessionPlayerData(clientId, connectionPayload.playerName, m_Portal.AvatarRegistry.GetRandomAvatar().Guid.ToNetworkGuid(), 0, true))
199199
? ConnectStatus.Success
200200
: ConnectStatus.LoggedInAgain;
@@ -203,7 +203,7 @@ private void ApprovalCheck(byte[] connectionData, ulong clientId, NetworkManager
203203
if (gameReturnStatus == ConnectStatus.LoggedInAgain)
204204
{
205205
SessionPlayerData? sessionPlayerData =
206-
SessionManager<SessionPlayerData>.Instance.GetPlayerData(connectionPayload.clientGUID);
206+
SessionManager<SessionPlayerData>.Instance.GetPlayerData(connectionPayload.playerId);
207207

208208
ulong oldClientId = sessionPlayerData?.ClientID ?? 0;
209209
// kicking old client to leave only current

Assets/BossRoom/Scripts/Shared/Net/UnityServices/Auth/AuthenticationServiceFacade.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,17 @@ void InjectDependencies(IPublisher<UnityServiceErrorMessage> unityServiceErrorMe
1818
m_UnityServiceErrorMessagePublisher = unityServiceErrorMessagePublisher;
1919
}
2020

21-
void OnServiceException(AuthenticationException e)
21+
void OnServiceException(Exception e)
2222
{
2323
Debug.LogWarning(e.Message);
24-
25-
var reason = $"{e.Message} ({e.InnerException?.Message})"; // Lobby error type, then HTTP error type.
26-
24+
var reason = $"{e.Message} ({e.InnerException?.Message})";
2725
m_UnityServiceErrorMessagePublisher.Publish(new UnityServiceErrorMessage("Authentication Error", reason, UnityServiceErrorMessage.Service.Authentication, e));
2826
}
2927

3028
public void DoSignInAsync(Action onSigninComplete, Action onFailed, InitializationOptions initializationOptions)
3129
{
3230
var task = TrySignIn(initializationOptions);
33-
UnityServiceCallsTaskWrapper.RunTask<AuthenticationException>(task, onSigninComplete, onFailed, OnServiceException);
31+
UnityServiceCallsTaskWrapper.RunTask<Exception>(task, onSigninComplete, onFailed, OnServiceException);
3432
}
3533

3634
async Task TrySignIn(InitializationOptions initializationOptions)

Assets/BossRoom/Scripts/Shared/Net/UnityServices/Infrastructure/Messages/UnityServiceErrorMessage.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Unity.Services.Core;
1+
using System;
22

33
namespace Unity.Multiplayer.Samples.BossRoom.Shared.Net.UnityServices.Infrastructure
44
{
@@ -13,9 +13,9 @@ public enum Service
1313
public string Title;
1414
public string Message;
1515
public Service AffectedService;
16-
public RequestFailedException OriginalException;
16+
public Exception OriginalException;
1717

18-
public UnityServiceErrorMessage(string title, string message, Service service, RequestFailedException originalException = null)
18+
public UnityServiceErrorMessage(string title, string message, Service service, Exception originalException = null)
1919
{
2020
Title = title;
2121
Message = message;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using UnityEngine;
2+
3+
#if UNITY_EDITOR
4+
using ParrelSync;
5+
#endif
6+
7+
namespace Unity.Multiplayer.Samples.BossRoom.Shared
8+
{
9+
public static class ProfileManager
10+
{
11+
public const string AuthProfileCommandLineArg = "-AuthProfile";
12+
13+
static string s_Profile;
14+
15+
public static string Profile => s_Profile ??= GetProfile();
16+
17+
static string GetProfile()
18+
{
19+
#if UNITY_EDITOR
20+
21+
//The code below makes it possible for the clone instance to log in as a different user profile in Authentication service.
22+
//This allows us to test services integration locally by utilising Parrelsync.
23+
if (ClonesManager.IsClone())
24+
{
25+
Debug.Log("This is a clone project.");
26+
var customArguments = ClonesManager.GetArgument().Split(',');
27+
28+
//second argument is our custom ID, but if it's not set we would just use some default.
29+
30+
var hardcodedProfileID = customArguments.Length > 1 ? customArguments[1] : "defaultCloneID";
31+
32+
return hardcodedProfileID;
33+
}
34+
#else
35+
var arguments = System.Environment.GetCommandLineArgs();
36+
for (int i = 0; i < arguments.Length; i++)
37+
{
38+
if (arguments[i] == AuthProfileCommandLineArg)
39+
{
40+
var profileId = arguments[i + 1];
41+
return profileId;
42+
}
43+
}
44+
#endif
45+
return "";
46+
}
47+
}
48+
}

Assets/BossRoom/Scripts/Shared/ProfileManager.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/BossRoom/Scripts/Shared/com.unity.multiplayer.samples.bossroom.shared.asmdef

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"Unity.Collections",
1313
"Photon Realtime Transport for Netcode for GameObjects",
1414
"Unity.Multiplayer.Samples.Utilities",
15-
"Unity.Networking.Transport"
15+
"Unity.Networking.Transport",
16+
"ParrelSync"
1617
],
1718
"includePlatforms": [],
1819
"excludePlatforms": [],
@@ -23,4 +24,4 @@
2324
"defineConstraints": [],
2425
"versionDefines": [],
2526
"noEngineReferences": false
26-
}
27+
}

0 commit comments

Comments
 (0)