Skip to content

Commit 41790b7

Browse files
committed
Adding back GUID with IP connections
When not using UGS, player id is now defined as GUID + profile string
1 parent 0d34e9c commit 41790b7

File tree

7 files changed

+115
-56
lines changed

7 files changed

+115
-56
lines changed

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

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
using Unity.Services.Core;
77
using UnityEngine;
88

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

1310
namespace Unity.Multiplayer.Samples.BossRoom.Client
1411
{
@@ -21,7 +18,6 @@ namespace Unity.Multiplayer.Samples.BossRoom.Client
2118
/// </remarks>
2219
public class ClientMainMenuState : GameStateBehaviour
2320
{
24-
public const string AuthProfileCommandLineArg = "-AuthProfile";
2521
public override GameState ActiveState { get { return GameState.MainMenu; } }
2622

2723
[SerializeField] GameObject[] m_GameObjectsThatWillBeInjectedAutomatically;
@@ -49,33 +45,11 @@ void InjectDependenciesAndInitialize(AuthenticationServiceFacade authServiceFaca
4945
m_Scope.BindInstanceAsSingle(m_LobbyUIMediator);
5046

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

8054
authServiceFacade.DoSignInAsync(OnAuthSignIn, OnSignInFailed, unityAuthenticationInitOptions);
8155

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void ServiceErrorHandler(UnityServiceErrorMessage error)
3232
// LobbyConflict can have multiple causes. Let's add other solutions here if there's other situations that arise for this.
3333
errorMessage += "\nSee logs for possible causes and solution.";
3434
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");
35+
$"same machine. Please use command line arg '{ProfileManager.AuthProfileCommandLineArg} someName' to set a different auth profile.\n");
3636
}
3737
}
3838

@@ -44,4 +44,4 @@ void OnDestroy()
4444
m_Subscriptions.Dispose();
4545
}
4646
}
47-
}
47+
}

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/Net/ConnectionManagement/ClientGameNetPortal.cs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using Unity.Multiplayer.Samples.BossRoom.Shared.Infrastructure;
3-
using Unity.Multiplayer.Samples.BossRoom.Shared.Net.UnityServices.Infrastructure;
43
using Unity.Multiplayer.Samples.BossRoom.Shared.Net.UnityServices.Lobbies;
54
using UnityEngine;
65
using UnityEngine.SceneManagement;
@@ -19,6 +18,9 @@ public class ClientGameNetPortal : MonoBehaviour
1918
public static ClientGameNetPortal Instance;
2019
private GameNetPortal m_Portal;
2120

21+
string m_JoinCode;
22+
OnlineMode m_OnlineMode;
23+
2224
/// <summary>
2325
/// If a disconnect occurred this will be populated with any contextual information that was available to explain why.
2426
/// </summary>
@@ -156,11 +158,11 @@ private void OnDisconnectOrTimeout(ulong clientID)
156158
/// 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
157159
/// yet initialized its client and server GNP-Logic objects yet (which it does in OnNetworkSpawn, based on the role that the current player is performing).
158160
/// </remarks>
159-
/// <param name="portal"> </param>
160161
/// <param name="ipaddress">the IP address of the host to connect to. (currently IPV4 only)</param>
161162
/// <param name="port">The port of the host to connect to. </param>
162163
public void StartClient(string ipaddress, int port)
163164
{
165+
m_OnlineMode = OnlineMode.IpHost;
164166
var chosenTransport = NetworkManager.Singleton.gameObject.GetComponent<TransportPicker>().IpHostTransport;
165167
NetworkManager.Singleton.NetworkConfig.NetworkTransport = chosenTransport;
166168

@@ -178,39 +180,52 @@ public void StartClient(string ipaddress, int port)
178180
throw new ArgumentOutOfRangeException(nameof(chosenTransport));
179181
}
180182

181-
ConnectClient();
183+
ConnectClient(null);
182184
}
183185

184-
public async void StartClientUnityRelayModeAsync(string joinCode, Action<string> onFailure)
186+
public void StartClientUnityRelayModeAsync(string joinCode, Action<string> onFailure)
185187
{
186-
var utp = (UnityTransport)NetworkManager.Singleton.gameObject.GetComponent<TransportPicker>().UnityRelayTransport;
188+
m_OnlineMode = OnlineMode.UnityRelay;
189+
m_JoinCode = joinCode;
190+
var utp = NetworkManager.Singleton.gameObject.GetComponent<TransportPicker>().UnityRelayTransport;
187191
NetworkManager.Singleton.NetworkConfig.NetworkTransport = utp;
188192

189193
Debug.Log($"Setting Unity Relay client with join code {joinCode}");
190194

191-
try
195+
ConnectClient(onFailure);
196+
}
197+
198+
async void ConnectClient(Action<string> onFailure)
199+
{
200+
string playerId;
201+
if (m_OnlineMode == OnlineMode.UnityRelay)
192202
{
193-
var clientRelayUtilityTask = UnityRelayUtilities.JoinRelayServerFromJoinCode(joinCode);
194-
await clientRelayUtilityTask;
195-
var (ipv4Address, port, allocationIdBytes, connectionData, hostConnectionData, key) = clientRelayUtilityTask.Result;
203+
try
204+
{
205+
var clientRelayUtilityTask = UnityRelayUtilities.JoinRelayServerFromJoinCode(m_JoinCode);
206+
await clientRelayUtilityTask;
207+
var (ipv4Address, port, allocationIdBytes, connectionData, hostConnectionData, key) = clientRelayUtilityTask.Result;
196208

197-
m_LobbyServiceFacade.UpdatePlayerRelayInfoAsync(allocationIdBytes.ToString(), joinCode, null, null);
198-
utp.SetClientRelayData(ipv4Address, port, allocationIdBytes, key, connectionData, hostConnectionData, isSecure: true);
209+
m_LobbyServiceFacade.UpdatePlayerRelayInfoAsync(allocationIdBytes.ToString(), m_JoinCode, null, null);
210+
var utp = (UnityTransport) NetworkManager.Singleton.NetworkConfig.NetworkTransport;
211+
utp.SetClientRelayData(ipv4Address, port, allocationIdBytes, key, connectionData, hostConnectionData, isSecure: true);
212+
}
213+
catch (Exception e)
214+
{
215+
onFailure?.Invoke(e.Message);
216+
return;//not re-throwing, but still not allowing to connect
217+
}
218+
219+
playerId = AuthenticationService.Instance.PlayerId;
199220
}
200-
catch (Exception e)
221+
else
201222
{
202-
onFailure?.Invoke(e.Message);
203-
return;//not re-throwing, but still not allowing to connect
223+
playerId = ClientPrefs.GetGuid() + ProfileManager.Profile;
204224
}
205225

206-
ConnectClient();
207-
}
208-
209-
private void ConnectClient()
210-
{
211226
var payload = JsonUtility.ToJson(new ConnectionPayload()
212227
{
213-
playerId = AuthenticationService.Instance.PlayerId,
228+
playerId = playerId,
214229
clientScene = SceneManager.GetActiveScene().buildIndex,
215230
playerName = m_Portal.PlayerName
216231
});
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using UnityEngine;
2+
3+
#if UNITY_EDITOR
4+
using ParrelSync;
5+
#endif
6+
7+
public static class ProfileManager
8+
{
9+
public const string AuthProfileCommandLineArg = "-AuthProfile";
10+
11+
static bool s_IsProfileSet = false;
12+
13+
static string s_Profile = "";
14+
15+
public static string Profile
16+
{
17+
get
18+
{
19+
if (!s_IsProfileSet)
20+
{
21+
s_Profile = GetProfile();
22+
s_IsProfileSet = true;
23+
}
24+
25+
return s_Profile;
26+
}
27+
private set => s_Profile = value;
28+
}
29+
30+
static string GetProfile()
31+
{
32+
#if UNITY_EDITOR
33+
//The code below makes it possible for the clone instance to log in as a different user profile in Authentication service.
34+
//This allows us to test services integration locally by utilising Parrelsync.
35+
if (ClonesManager.IsClone())
36+
{
37+
Debug.Log("This is a clone project.");
38+
var customArguments = ClonesManager.GetArgument().Split(',');
39+
40+
//second argument is our custom ID, but if it's not set we would just use some default.
41+
42+
var hardcodedProfileID = customArguments.Length > 1 ? customArguments[1] : "defaultCloneID";
43+
44+
return hardcodedProfileID;
45+
}
46+
#else
47+
var arguments = System.Environment.GetCommandLineArgs();
48+
for (int i = 0; i < arguments.Length; i++)
49+
{
50+
if (arguments[i] == AuthProfileCommandLineArg)
51+
{
52+
var profileId = arguments[i + 1];
53+
return profileId;
54+
}
55+
}
56+
#endif
57+
return "";
58+
}
59+
}

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)