Skip to content
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
66 changes: 6 additions & 60 deletions Assets/Talo Game Services/Talo/Runtime/APIs/PlayersAPI.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Threading.Tasks;
using UnityEngine;
using System.IO;

namespace TaloGameServices
{
Expand All @@ -23,8 +22,6 @@ public enum DebouncedOperation
public event Action OnIdentityCleared;
public event Action<RejectedProp[]> OnPropsRejected;

public static readonly string offlineDataPath = Application.persistentDataPath + "/ta.bin";

public PlayersAPI() : base("v1/players")
{
Talo.OnConnectionRestored += OnConnectionRestored;
Expand Down Expand Up @@ -211,13 +208,13 @@ private async Task<Player> IdentifyOffline(string service, string identifier)
PlayerAlias offlineAlias;
try
{
offlineAlias = GetOfflineAlias();
offlineAlias = PlayerAlias.GetOfflineAlias();
}
catch
{
DeleteOfflineAlias();
PlayerAlias.DeleteOfflineAlias();
OnIdentificationFailed?.Invoke();
throw new Exception("Failed to parse offline player alias.");
throw new Exception("Failed to parse offline player alias");
}

if (offlineAlias != null && offlineAlias.MatchesIdentifyRequest(service, identifier))
Expand All @@ -226,66 +223,15 @@ private async Task<Player> IdentifyOffline(string service, string identifier)
}

OnIdentificationFailed?.Invoke();
throw new Exception("No offline player alias found.");
}

public bool HasOfflineAlias()
{
return Talo.Settings.cachePlayerOnIdentify && File.Exists(offlineDataPath);
}

private PlayerAlias GetOfflineAlias()
{
if (!HasOfflineAlias())
{
return null;
}

return JsonUtility.FromJson<PlayerAlias>(Talo.Crypto.ReadFileContent(offlineDataPath));
}

private void DeleteOfflineAlias()
{
if (File.Exists(offlineDataPath))
{
try
{
File.Delete(offlineDataPath);
}
catch (Exception ex)
{
Debug.LogWarning($"Failed to delete offline player data: {ex.Message}");
}
}
throw new Exception("No offline player alias found");
}

public async Task ClearIdentity()
{
Talo.IdentityCheck();

try
if (await Talo.PlayerAuth.SessionManager.ClearSession())
{
DeleteOfflineAlias();
OnIdentityCleared?.Invoke();
}
catch (Exception ex)
{
Debug.LogWarning($"Error deleting offline alias: {ex.Message}");
}

try
{
// clears the alias and resets the socket (doesn't require auth)
await Talo.PlayerAuth.SessionManager.ClearSession();
}
catch (Exception ex)
{
Debug.LogWarning($"Error clearing session: {ex.Message}");
}

Talo.Events.ClearQueue();
Talo.Continuity.ClearRequests();

OnIdentityCleared?.Invoke();
}

public async Task<PlayersSearchResponse> Search(string query)
Expand Down
37 changes: 35 additions & 2 deletions Assets/Talo Game Services/Talo/Runtime/Entities/PlayerAlias.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using UnityEngine;
using System.IO;
using UnityEngine;

namespace TaloGameServices
{
[System.Serializable]
public class PlayerAlias
{
private static readonly string offlineDataPath = Application.persistentDataPath + "/ta.bin";

public int id;
public string service, identifier, displayName;
public Player player;
Expand All @@ -23,7 +26,37 @@ public void WriteOfflineAlias()
}

var content = JsonUtility.ToJson(this);
Talo.Crypto.WriteFileContent(PlayersAPI.offlineDataPath, content);
Talo.Crypto.WriteFileContent(offlineDataPath, content);
}

public static bool HasOfflineAlias()
{
return Talo.Settings.cachePlayerOnIdentify && File.Exists(offlineDataPath);
}

public static PlayerAlias GetOfflineAlias()
{
if (!HasOfflineAlias())
{
return null;
}

return JsonUtility.FromJson<PlayerAlias>(Talo.Crypto.ReadFileContent(offlineDataPath));
}

public static void DeleteOfflineAlias()
{
if (File.Exists(offlineDataPath))
{
try
{
File.Delete(offlineDataPath);
}
catch (System.Exception ex)
{
Debug.LogWarning($"Failed to delete offline player data: {ex.Message}");
}
}
}
}
}
29 changes: 25 additions & 4 deletions Assets/Talo Game Services/Talo/Runtime/Utils/SessionManager.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Threading.Tasks;
using UnityEngine;

Expand Down Expand Up @@ -39,15 +40,35 @@ private void SaveSession(string sessionToken, string refreshToken)
SetIdentifierPlayerPref();
}

public async Task ClearSession(bool resetSocket = true)
public async Task<bool> ClearSession(bool resetSocket = true)
{
Talo.CurrentAlias = null;
if (!Talo.HasIdentity())
{
return false;
}

_sessionToken = null;
Talo.CurrentAlias = null;
PlayerAlias.DeleteOfflineAlias();

PlayerPrefs.DeleteKey("TaloRefreshToken");

Talo.Events.ClearQueue();
Talo.Continuity.ClearRequests();

if (resetSocket)
{
await Talo.Socket.ResetConnection();
try
{
await Talo.Socket.ResetConnection();
}
catch (Exception ex)
{
Debug.LogWarning($"Error resetting socket: {ex.Message}");
}
}

return true;
}

public string GetSessionToken()
Expand All @@ -74,7 +95,7 @@ public async Task<bool> CheckForSession()

if (Talo.IsOffline())
{
return Talo.Players.HasOfflineAlias();
return PlayerAlias.HasOfflineAlias();
}

if (!string.IsNullOrEmpty(GetRefreshToken()))
Expand Down
Loading