Fixed relay loading

This commit is contained in:
2026-05-20 11:19:41 +01:00
parent 470f1de835
commit 8d3998945a
15 changed files with 367 additions and 250 deletions

View File

@@ -1,4 +1,3 @@
using UnityEngine.SceneManagement;
using UnityEngine;
namespace PashaBibko.PenguinChase.Core
@@ -8,15 +7,11 @@ namespace PashaBibko.PenguinChase.Core
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void LoadBoostrapScene()
{
#if UNITY_EDITOR
// Stops loading in on the bootstrap scene
if (SceneManager.GetActiveScene().name == "Bootstrap")
{
SceneManager.LoadScene("MainMenu", LoadSceneMode.Single);
}
#endif // UNITY_EDITOR
SceneManager.LoadScene("Bootstrap", LoadSceneMode.Additive);
GameObject go = Resources.Load<GameObject>("Bootstrap");
GameObject instance = Object.Instantiate(go);
Object.DontDestroyOnLoad(instance);
instance.name = "Bootstrap";
}
}
}

View File

@@ -1,26 +1,46 @@
using Unity.Services.Authentication;
using PashaBibko.PenguinChase.Extensions;
using Unity.Services.Authentication;
using Unity.Services.Core;
using System.Collections;
using UnityEngine;
namespace PashaBibko.PenguinChase.Core.Network
{
public static class Authenticator
{
public static bool IsAuthenticated { get; private set; }
private static bool sIsAuthenticating;
public static IEnumerator Authenticate()
{
// Early return if already authenticated
Debug.Log($"Authenticate called, [Authenticated: {IsAuthenticated}]");
if (IsAuthenticated)
{
// User is already authenticated
yield break;
}
// TODO: Sign in via current platform
yield return UnityServices.InitializeAsync();
yield return AuthenticationService.Instance.SignInAnonymouslyAsync();
// Stops multiple authentication attempts at the same time
if (sIsAuthenticating)
{
yield return new WaitUntil(() => IsAuthenticated || !sIsAuthenticating);
yield break; // User should be logged in from other attempt
}
sIsAuthenticating = true;
Debug.Log("Initializing Unity Services");
yield return UnityServices
.InitializeAsync()
.Await();
Debug.Log("Signing in anonymously");
yield return AuthenticationService.Instance
.SignInAnonymouslyAsync()
.Await();
Debug.Log("User has been authenticated");
sIsAuthenticating = false;
IsAuthenticated = true;
}
}
}
}

View File

@@ -11,7 +11,7 @@ namespace PashaBibko.PenguinChase.Core.Network
[SerializeField] private GameObject PrefabForEachClient;
public static GameObject ClientPrefab => sInstance?.PrefabForEachClient;
private void Start()
private void Awake()
{
// Stops overlapping instances
if (sInstance is not null)

View File

@@ -59,7 +59,7 @@ namespace PashaBibko.PenguinChase.Core.Network
}
}
private void Start()
private void Awake()
{
// Stops overlapping instances
if (sInstance is not null)
@@ -91,9 +91,7 @@ namespace PashaBibko.PenguinChase.Core.Network
// Then actually hosts the lobby
yield return sConnectionManager.Host(() =>
{
SceneManager.UnloadSceneAsync("MainMenu");
NetworkManager.Singleton.SceneManager.LoadScene("GameScene", LoadSceneMode.Additive);
NetworkManager.Singleton.SceneManager.LoadScene("GameScene", LoadSceneMode.Single);
Debug.Log("Hosted");
});
}

View File

@@ -10,7 +10,7 @@ namespace PashaBibko.PenguinChase.Core.Network
{
public class UnityRelayTransport : INetworkTransport
{
private const uint MAX_CONNECTIONS = 7;
private const int MAX_CONNECTIONS = 7;
public IEnumerator Join(string code, Action callback)
{
@@ -34,11 +34,13 @@ namespace PashaBibko.PenguinChase.Core.Network
public IEnumerator Host(Action callback)
{
yield return Authenticator.Authenticate();
Debug.Log("Authenticated");
Allocation allocation;
{
Result<Allocation> result = new();
yield return RelayService.Instance
.CreateAllocationAsync(7)
.CreateAllocationAsync(MAX_CONNECTIONS)
.Await(result);
allocation = result.Value;

View File

@@ -1,5 +1,6 @@
using System.Threading.Tasks;
using System.Collections;
using UnityEngine;
namespace PashaBibko.PenguinChase.Extensions
{
@@ -10,6 +11,20 @@ namespace PashaBibko.PenguinChase.Extensions
public static class TaskExtensions
{
public static IEnumerator Await(this Task task)
{
// Waits until the task is completed
while (!task.IsCompleted)
{
yield return null;
}
if (task.IsFaulted)
{
Debug.LogError($"Task failed: [{task.Exception?.InnerException?.Message}]");
}
}
public static IEnumerator Await<T>(this Task<T> task, Result<T> result)
where T : class
{
@@ -19,6 +34,11 @@ namespace PashaBibko.PenguinChase.Extensions
yield return null;
}
if (task.IsFaulted)
{
Debug.LogError($"Task failed: [{task.Exception?.InnerException?.Message}]");
}
// Has to return the value like this because of the wonders of C#
result.Value = task.Result;
}