Skip to content

feat: replace guid with auth playerid for session management #488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
97c2ee1
Replaced custom guid with playerid for session management
LPLafontaineB Feb 24, 2022
4ce6534
Removed code handling duplicate connections in debug builds
LPLafontaineB Feb 24, 2022
5958c9b
Removed AddHostData method
LPLafontaineB Feb 24, 2022
3d9ebfa
Removed guid from ClientPrefs
LPLafontaineB Feb 24, 2022
62d5ea7
Made ClientPrefs class static
LPLafontaineB Feb 24, 2022
47b02ba
cleaning up
LPLafontaineB Feb 24, 2022
e6bb5d5
Merge develop into feature/replace-guid-with-auth-playerid
netcode-ci-service Mar 8, 2022
020cce1
Merge develop into feature/replace-guid-with-auth-playerid
netcode-ci-service Mar 8, 2022
0d874b5
Merge develop into feature/replace-guid-with-auth-playerid
netcode-ci-service Mar 8, 2022
77b939f
Merge develop into feature/replace-guid-with-auth-playerid
netcode-ci-service Mar 8, 2022
b974e90
Merge develop into feature/replace-guid-with-auth-playerid
netcode-ci-service Mar 8, 2022
2d83fcf
Merge develop into feature/replace-guid-with-auth-playerid
netcode-ci-service Mar 9, 2022
5296213
Merge develop into feature/replace-guid-with-auth-playerid
netcode-ci-service Mar 9, 2022
1babc3f
Merge develop into feature/replace-guid-with-auth-playerid
netcode-ci-service Mar 9, 2022
8975d19
Merge develop into feature/replace-guid-with-auth-playerid
netcode-ci-service Mar 9, 2022
40d2f89
Merge develop into feature/replace-guid-with-auth-playerid
netcode-ci-service Mar 9, 2022
f13fd7a
Merge develop into feature/replace-guid-with-auth-playerid
netcode-ci-service Mar 9, 2022
6f27732
Merge develop into feature/replace-guid-with-auth-playerid
netcode-ci-service Mar 9, 2022
0d34e9c
Revert "Removed guid from ClientPrefs"
LPLafontaineB Mar 9, 2022
4063c42
Merge develop into feature/replace-guid-with-auth-playerid
netcode-ci-service Mar 9, 2022
41790b7
Adding back GUID with IP connections
LPLafontaineB Mar 9, 2022
9008d77
Merge branch 'feature/replace-guid-with-auth-playerid' of https://git…
LPLafontaineB Mar 9, 2022
f479872
Exctracted GetPlayerId to GameNetPortal
LPLafontaineB Mar 10, 2022
d164192
refactored GetPlayerId to use AuthService's playerId when signed in o…
LPLafontaineB Mar 10, 2022
d119ffb
cleanup using directives
LPLafontaineB Mar 10, 2022
788eb23
Merge develop into feature/replace-guid-with-auth-playerid
netcode-ci-service Mar 10, 2022
2973861
reverted unneeded change
LPLafontaineB Mar 11, 2022
346bb7f
Simplified ProfileManager property
LPLafontaineB Mar 11, 2022
d66f779
Removed OnlineMode responsability from GameNetPortal
LPLafontaineB Mar 11, 2022
a44aeaa
Merge branch 'develop' into feature/replace-guid-with-auth-playerid
LPLafontaineB Mar 11, 2022
b525df7
Adding namespace
LPLafontaineB Mar 11, 2022
eabe77c
Merge branch 'develop' into feature/replace-guid-with-auth-playerid
LPLafontaineB Mar 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
using Unity.Services.Core;
using UnityEngine;

#if UNITY_EDITOR
using ParrelSync;
#endif

namespace Unity.Multiplayer.Samples.BossRoom.Client
{
Expand All @@ -21,7 +18,6 @@ namespace Unity.Multiplayer.Samples.BossRoom.Client
/// </remarks>
public class ClientMainMenuState : GameStateBehaviour
{
public const string AuthProfileCommandLineArg = "-AuthProfile";
public override GameState ActiveState { get { return GameState.MainMenu; } }

[SerializeField] GameObject[] m_GameObjectsThatWillBeInjectedAutomatically;
Expand Down Expand Up @@ -51,33 +47,11 @@ void InjectDependenciesAndInitialize(AuthenticationServiceFacade authServiceFaca
m_Scope.BindInstanceAsSingle(m_IPUIMediator);

var unityAuthenticationInitOptions = new InitializationOptions();

#if UNITY_EDITOR
//The code below makes it possible for the clone instance to log in as a different user profile in Authentication service.
//This allows us to test services integration locally by utilising Parrelsync.
if (ClonesManager.IsClone())
{
Debug.Log("This is a clone project.");
var customArguments = ClonesManager.GetArgument().Split(',');

//second argument is our custom ID, but if it's not set we would just use some default.

var hardcodedProfileID = customArguments.Length > 1 ? customArguments[1] : "defaultCloneID";

unityAuthenticationInitOptions.SetProfile(hardcodedProfileID);
}
#else
var arguments = System.Environment.GetCommandLineArgs();
for (int i = 0; i < arguments.Length; i++)
var profile = ProfileManager.Profile;
if (profile.Length > 0)
{
if (arguments[i] == AuthProfileCommandLineArg)
{
var profileId = arguments[i + 1];
unityAuthenticationInitOptions.SetProfile(profileId);
break;
}
unityAuthenticationInitOptions.SetProfile(profile);
}
#endif

authServiceFacade.DoSignInAsync(OnAuthSignIn, OnSignInFailed, unityAuthenticationInitOptions);

Expand Down
4 changes: 2 additions & 2 deletions Assets/BossRoom/Scripts/Client/UI/UnityServicesUIHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void ServiceErrorHandler(UnityServiceErrorMessage error)
// LobbyConflict can have multiple causes. Let's add other solutions here if there's other situations that arise for this.
errorMessage += "\nSee logs for possible causes and solution.";
Debug.LogError($"Got service error {error.Message} with LobbyConflict. Possible conflict cause: Trying to play with two builds on the " +
$"same machine. Please use command line arg '{ClientMainMenuState.AuthProfileCommandLineArg} someName' to set a different auth profile.\n");
$"same machine. Please use command line arg '{ProfileManager.AuthProfileCommandLineArg} someName' to set a different auth profile.\n");
}
}
}
Expand All @@ -47,4 +47,4 @@ void OnDestroy()
m_Subscriptions.Dispose();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"Unity.Netcode.Adapter.UTP",
"Unity.Services.Relay",
"Unity.Multiplayer.Samples.Utilities",
"Unity.Services.Lobbies",
"ParrelSync"
"Unity.Services.Lobbies"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand All @@ -23,4 +22,4 @@
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
}
6 changes: 3 additions & 3 deletions Assets/BossRoom/Scripts/Shared/ClientPrefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ namespace Unity.Multiplayer.Samples.BossRoom.Client
/// (This is just a wrapper around the PlayerPrefs system,
/// so that all the calls are in the same place.)
/// </summary>
public class ClientPrefs
public static class ClientPrefs
{
private const float k_DefaultMasterVolume = 0.5f;
private const float k_DefaultMusicVolume = 0.8f;
const float k_DefaultMasterVolume = 0.5f;
const float k_DefaultMusicVolume = 0.8f;

public static float GetMasterVolume()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
using Unity.Multiplayer.Samples.BossRoom.Shared.Net.UnityServices.Infrastructure;
using Unity.Multiplayer.Samples.BossRoom.Shared.Net.UnityServices.Lobbies;
using UnityEngine;
using UnityEngine.SceneManagement;
Expand Down Expand Up @@ -155,7 +154,6 @@ private void OnDisconnectOrTimeout(ulong clientID)
/// This method must be static because, when it is invoked, the client still doesn't know it's a client yet, and in particular, GameNetPortal hasn't
/// yet initialized its client and server GameNetPortal objects yet (which it does in OnNetworkSpawn, based on the role that the current player is performing).
/// </remarks>
/// <param name="portal"> </param>
/// <param name="ipaddress">the IP address of the host to connect to. (currently IPV4 only)</param>
/// <param name="port">The port of the host to connect to. </param>
public void StartClient(string ipaddress, int port)
Expand Down Expand Up @@ -202,15 +200,16 @@ public async void StartClientUnityRelayModeAsync(string joinCode, Action<string>
return;//not re-throwing, but still not allowing to connect
}


ConnectClient();
}

private void ConnectClient()
void ConnectClient()
{
var clientGuid = ClientPrefs.GetGuid();

var payload = JsonUtility.ToJson(new ConnectionPayload()
{
clientGUID = clientGuid,
playerId = m_Portal.GetPlayerId(),
clientScene = SceneManager.GetActiveScene().buildIndex,
playerName = m_Portal.PlayerName
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Unity.Multiplayer.Samples.BossRoom.Shared.Net.UnityServices.Lobbies;
using Unity.Netcode;
using Unity.Netcode.Transports.UNET;
using Unity.Services.Authentication;
using UnityEngine;
using UnityEngine.SceneManagement;

Expand All @@ -31,7 +32,7 @@ public enum OnlineMode
[Serializable]
public class ConnectionPayload
{
public string clientGUID;
public string playerId;
public int clientScene = -1;
public string playerName;
}
Expand Down Expand Up @@ -245,5 +246,10 @@ public void RequestDisconnect()
SessionManager<SessionPlayerData>.Instance.OnUserDisconnectRequest();
NetManager.Shutdown();
}

public string GetPlayerId()
{
return AuthenticationService.Instance.IsSignedIn ? AuthenticationService.Instance.PlayerId : ClientPrefs.GetGuid() + ProfileManager.Profile;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private void ApprovalCheck(byte[] connectionData, ulong clientId, NetworkManager
// Approval check happens for Host too, but obviously we want it to be approved
if (clientId == NetworkManager.Singleton.LocalClientId)
{
SessionManager<SessionPlayerData>.Instance.AddHostData(
SessionManager<SessionPlayerData>.Instance.SetupConnectingPlayerSessionData(clientId, m_Portal.GetPlayerId(),
new SessionPlayerData(clientId, m_Portal.PlayerName, m_Portal.AvatarRegistry.GetRandomAvatar().Guid.ToNetworkGuid(), 0, true));

connectionApprovedCallback(true, null, true, null, null);
Expand Down Expand Up @@ -176,9 +176,9 @@ private void ApprovalCheck(byte[] connectionData, ulong clientId, NetworkManager

int clientScene = connectionPayload.clientScene;

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

gameReturnStatus = SessionManager<SessionPlayerData>.Instance.SetupConnectingPlayerSessionData(clientId, connectionPayload.clientGUID,
gameReturnStatus = SessionManager<SessionPlayerData>.Instance.SetupConnectingPlayerSessionData(clientId, connectionPayload.playerId,
new SessionPlayerData(clientId, connectionPayload.playerName, m_Portal.AvatarRegistry.GetRandomAvatar().Guid.ToNetworkGuid(), 0, true))
? ConnectStatus.Success
: ConnectStatus.LoggedInAgain;
Expand All @@ -187,7 +187,7 @@ private void ApprovalCheck(byte[] connectionData, ulong clientId, NetworkManager
if (gameReturnStatus == ConnectStatus.LoggedInAgain)
{
SessionPlayerData? sessionPlayerData =
SessionManager<SessionPlayerData>.Instance.GetPlayerData(connectionPayload.clientGUID);
SessionManager<SessionPlayerData>.Instance.GetPlayerData(connectionPayload.playerId);

ulong oldClientId = sessionPlayerData?.ClientID ?? 0;
// kicking old client to leave only current
Expand Down
48 changes: 48 additions & 0 deletions Assets/BossRoom/Scripts/Shared/ProfileManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using UnityEngine;

#if UNITY_EDITOR
using ParrelSync;
#endif

namespace Unity.Multiplayer.Samples.BossRoom.Shared
{
public static class ProfileManager
{
public const string AuthProfileCommandLineArg = "-AuthProfile";

static string s_Profile;

public static string Profile => s_Profile ??= GetProfile();

static string GetProfile()
{
#if UNITY_EDITOR

//The code below makes it possible for the clone instance to log in as a different user profile in Authentication service.
//This allows us to test services integration locally by utilising Parrelsync.
if (ClonesManager.IsClone())
{
Debug.Log("This is a clone project.");
var customArguments = ClonesManager.GetArgument().Split(',');

//second argument is our custom ID, but if it's not set we would just use some default.

var hardcodedProfileID = customArguments.Length > 1 ? customArguments[1] : "defaultCloneID";

return hardcodedProfileID;
}
#else
var arguments = System.Environment.GetCommandLineArgs();
for (int i = 0; i < arguments.Length; i++)
{
if (arguments[i] == AuthProfileCommandLineArg)
{
var profileId = arguments[i + 1];
return profileId;
}
}
#endif
return "";
}
}
}
11 changes: 11 additions & 0 deletions Assets/BossRoom/Scripts/Shared/ProfileManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"Unity.Netcode.Adapter.UTP",
"Unity.Collections",
"Unity.Multiplayer.Samples.Utilities",
"Unity.Networking.Transport"
"Unity.Networking.Transport",
"ParrelSync"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand All @@ -22,4 +23,4 @@
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
}
Loading