Skip to content

cherrypick feat: replace guid with auth playerid for session management (#488) #548

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
Show file tree
Hide file tree
Changes from all commits
Commits
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
@@ -1,14 +1,12 @@
using BossRoom.Scripts.Shared.Net.UnityServices.Auth;
using Unity.Multiplayer.Samples.BossRoom.Shared;
using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
using Unity.Multiplayer.Samples.BossRoom.Shared.Net.UnityServices.Lobbies;
using Unity.Multiplayer.Samples.BossRoom.Visual;
using Unity.Services.Authentication;
using Unity.Services.Core;
using UnityEngine;

#if UNITY_EDITOR
using ParrelSync;
#endif

namespace Unity.Multiplayer.Samples.BossRoom.Client
{
Expand All @@ -21,7 +19,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 +48,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
@@ -1,5 +1,5 @@
using System;
using Unity.Multiplayer.Samples.BossRoom.Client;
using Unity.Multiplayer.Samples.BossRoom.Shared;
using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
using Unity.Multiplayer.Samples.BossRoom.Shared.Net.UnityServices.Infrastructure;
using Unity.Services.Lobbies;
Expand Down Expand Up @@ -32,7 +32,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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
"Photon Realtime Transport for Netcode for GameObjects",
"Unity.Services.Relay",
"Unity.Multiplayer.Samples.Utilities",
"Unity.Services.Lobbies",
"ParrelSync"
"Unity.Services.Lobbies"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand All @@ -24,4 +23,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
Expand Up @@ -274,10 +274,9 @@ async void ConnectClient(Action<string> onFailure)
}
}

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
@@ -1,11 +1,12 @@
using System;
using System.Collections;
using Unity.Multiplayer.Samples.BossRoom.Client;
using Unity.Multiplayer.Samples.BossRoom.Server;
using Unity.Multiplayer.Samples.BossRoom.Shared;
using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
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 Down Expand Up @@ -34,7 +35,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 @@ -248,5 +249,10 @@ public void RequestDisconnect()
m_ClientPortal.OnUserDisconnectRequest();
m_ServerPortal.OnUserDisconnectRequest();
}

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 @@ -162,7 +162,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 @@ -192,9 +192,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 @@ -203,7 +203,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 @@ -12,7 +12,8 @@
"Unity.Collections",
"Photon Realtime Transport for Netcode for GameObjects",
"Unity.Multiplayer.Samples.Utilities",
"Unity.Networking.Transport"
"Unity.Networking.Transport",
"ParrelSync"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand All @@ -23,4 +24,4 @@
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
}
Loading