109 lines
3.3 KiB
C#
109 lines
3.3 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace IchniOnline.Online.Logic
|
|
{
|
|
public class IchniProtocolHandler : MonoBehaviour
|
|
{
|
|
private static IchniProtocolHandler _instance;
|
|
public static IchniProtocolHandler Instance => _instance;
|
|
|
|
private void Awake()
|
|
{
|
|
if (_instance != null)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
_instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
|
|
Application.deepLinkActivated += OnDeepLinkActivated;
|
|
|
|
if (!string.IsNullOrEmpty(Application.absoluteURL))
|
|
{
|
|
OnDeepLinkActivated(Application.absoluteURL);
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (_instance == this)
|
|
{
|
|
Application.deepLinkActivated -= OnDeepLinkActivated;
|
|
}
|
|
}
|
|
|
|
private void OnDeepLinkActivated(string url)
|
|
{
|
|
if (string.IsNullOrEmpty(url))
|
|
{
|
|
Debug.LogWarning("[IchniProtocolHandler] Deep link URL is empty");
|
|
return;
|
|
}
|
|
|
|
Debug.Log($"[IchniProtocolHandler] Deep link activated: {url}");
|
|
|
|
if (url.StartsWith("ichni://auth"))
|
|
{
|
|
HandleAuthCallback(url);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"[IchniProtocolHandler] Unknown deep link: {url}");
|
|
}
|
|
}
|
|
|
|
private void HandleAuthCallback(string url)
|
|
{
|
|
try
|
|
{
|
|
var uri = new Uri(url);
|
|
var query = uri.Query.TrimStart('?');
|
|
var queryParams = ParseQueryString(query);
|
|
var token = queryParams.ContainsKey("token") ? queryParams["token"] : null;
|
|
|
|
if (!string.IsNullOrEmpty(token))
|
|
{
|
|
Debug.Log("[IchniProtocolHandler] Auth callback received, completing login");
|
|
IchniOnlineAuthService.HandleIchniProtocolCallback(token);
|
|
}
|
|
else
|
|
{
|
|
var error = queryParams.ContainsKey("error") ? queryParams["error"] : null;
|
|
if (!string.IsNullOrEmpty(error))
|
|
{
|
|
Debug.LogError($"[IchniProtocolHandler] Auth callback error: {error}");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("[IchniProtocolHandler] Auth callback missing token");
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.LogError($"[IchniProtocolHandler] Failed to parse deep link: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private static System.Collections.Generic.Dictionary<string, string> ParseQueryString(string query)
|
|
{
|
|
var result = new System.Collections.Generic.Dictionary<string, string>();
|
|
if (string.IsNullOrEmpty(query)) return result;
|
|
|
|
var pairs = query.Split('&');
|
|
foreach (var pair in pairs)
|
|
{
|
|
var parts = pair.Split('=');
|
|
if (parts.Length == 2)
|
|
{
|
|
result[Uri.UnescapeDataString(parts[0])] = Uri.UnescapeDataString(parts[1]);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
}
|