333 lines
10 KiB
C#
333 lines
10 KiB
C#
using UnityEngine;
|
|
using System;
|
|
|
|
namespace Tech3C
|
|
{
|
|
/// <summary>
|
|
/// Bridge component to receive callbacks from native Android/iOS SDK via Unity messaging
|
|
/// This GameObject receives UnitySendMessage calls from native code
|
|
/// </summary>
|
|
public class Tech3CSDKBridge : MonoBehaviour
|
|
{
|
|
private static Tech3CSDKBridge instance;
|
|
|
|
// Callback references
|
|
private static IAuthCallback authCallback;
|
|
private static ILogoutCallback logoutCallback;
|
|
private static IUserInfoCallback userInfoCallback;
|
|
|
|
#region Singleton
|
|
|
|
public static Tech3CSDKBridge Instance
|
|
{
|
|
get
|
|
{
|
|
if (instance == null)
|
|
{
|
|
GameObject go = new GameObject("Tech3CSDKBridge");
|
|
instance = go.AddComponent<Tech3CSDKBridge>();
|
|
DontDestroyOnLoad(go);
|
|
}
|
|
return instance;
|
|
}
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
if (instance == null)
|
|
{
|
|
instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
Debug.Log("[Tech3C Bridge] Bridge initialized");
|
|
}
|
|
else if (instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Set Callbacks
|
|
|
|
/// <summary>
|
|
/// Set the auth callback to be invoked when auth events occur
|
|
/// </summary>
|
|
public static void SetAuthCallback(IAuthCallback callback)
|
|
{
|
|
authCallback = callback;
|
|
Debug.Log("[Tech3C Bridge] Auth callback set");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set the logout callback to be invoked when logout events occur
|
|
/// </summary>
|
|
public static void SetLogoutCallback(ILogoutCallback callback)
|
|
{
|
|
logoutCallback = callback;
|
|
Debug.Log("[Tech3C Bridge] Logout callback set");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set the user info callback to be invoked when user info events occur
|
|
/// </summary>
|
|
public static void SetUserInfoCallback(IUserInfoCallback callback)
|
|
{
|
|
userInfoCallback = callback;
|
|
Debug.Log("[Tech3C Bridge] User info callback set");
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Auth Callbacks
|
|
|
|
/// <summary>
|
|
/// Called from native code when authentication is successful
|
|
/// Message format: "userId|password|accessToken|refreshToken|loginType|expiryTime"
|
|
/// </summary>
|
|
public void OnAuthSuccess(string message)
|
|
{
|
|
Debug.Log($"[Tech3C Bridge] OnAuthSuccess received: {message}");
|
|
|
|
if (authCallback == null)
|
|
{
|
|
Debug.LogWarning("[Tech3C Bridge] No auth callback registered");
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
string[] parts = message.Split('|');
|
|
if (parts.Length >= 6)
|
|
{
|
|
string userId = parts[0];
|
|
string password = parts[1];
|
|
string accessToken = parts[2];
|
|
string refreshToken = parts[3];
|
|
LoginType loginType = (LoginType)Enum.Parse(typeof(LoginType), parts[4]);
|
|
long expiryTime = long.Parse(parts[5]);
|
|
|
|
Debug.Log($"[Tech3C Bridge] Parsed auth success: userId={userId}, loginType={loginType}");
|
|
authCallback.OnAuthSuccess(userId, password, accessToken, refreshToken, loginType, expiryTime);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError($"[Tech3C Bridge] Invalid message format: {message}");
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError($"[Tech3C Bridge] Error parsing auth success message: {e.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called from native code when authentication is cancelled
|
|
/// </summary>
|
|
public void OnAuthCancelled(string message)
|
|
{
|
|
Debug.Log("[Tech3C Bridge] OnAuthCancelled received");
|
|
|
|
if (authCallback == null)
|
|
{
|
|
Debug.LogWarning("[Tech3C Bridge] No auth callback registered");
|
|
return;
|
|
}
|
|
|
|
authCallback.OnAuthCancelled();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called from native code when an authentication error occurs
|
|
/// Message format: "errorCode|errorMessage"
|
|
/// </summary>
|
|
public void OnAuthError(string message)
|
|
{
|
|
Debug.Log($"[Tech3C Bridge] OnAuthError received: {message}");
|
|
|
|
if (authCallback == null)
|
|
{
|
|
Debug.LogWarning("[Tech3C Bridge] No auth callback registered");
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
string[] parts = message.Split('|');
|
|
int errorCode = -1;
|
|
string errorMessage = "Unknown error";
|
|
|
|
if (parts.Length >= 1)
|
|
{
|
|
int.TryParse(parts[0], out errorCode);
|
|
}
|
|
if (parts.Length >= 2)
|
|
{
|
|
errorMessage = parts[1];
|
|
}
|
|
|
|
Debug.Log($"[Tech3C Bridge] Parsed auth error: code={errorCode}, message={errorMessage}");
|
|
authCallback.OnAuthError(errorCode, errorMessage);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError($"[Tech3C Bridge] Error parsing auth error message: {e.Message}");
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Logout Callbacks
|
|
|
|
/// <summary>
|
|
/// Called from native code when logout is successful
|
|
/// </summary>
|
|
public void OnLogoutSuccess(string message)
|
|
{
|
|
Debug.Log("[Tech3C Bridge] OnLogoutSuccess received");
|
|
|
|
if (logoutCallback == null)
|
|
{
|
|
Debug.LogWarning("[Tech3C Bridge] No logout callback registered");
|
|
return;
|
|
}
|
|
|
|
logoutCallback.OnLogoutSuccess();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called from native code when a logout error occurs
|
|
/// Message format: "errorCode|errorMessage"
|
|
/// </summary>
|
|
public void OnLogoutError(string message)
|
|
{
|
|
Debug.Log($"[Tech3C Bridge] OnLogoutError received: {message}");
|
|
|
|
if (logoutCallback == null)
|
|
{
|
|
Debug.LogWarning("[Tech3C Bridge] No logout callback registered");
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
string[] parts = message.Split('|');
|
|
int errorCode = -1;
|
|
string errorMessage = "Unknown error";
|
|
|
|
if (parts.Length >= 1)
|
|
{
|
|
int.TryParse(parts[0], out errorCode);
|
|
}
|
|
if (parts.Length >= 2)
|
|
{
|
|
errorMessage = parts[1];
|
|
}
|
|
|
|
logoutCallback.OnLogoutError(errorCode, errorMessage);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError($"[Tech3C Bridge] Error parsing logout error message: {e.Message}");
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region User Info Callbacks
|
|
|
|
/// <summary>
|
|
/// Called from native code when user info is received
|
|
/// Message format: "userId|username|email|phone"
|
|
/// </summary>
|
|
public void OnUserInfoReceived(string message)
|
|
{
|
|
Debug.Log($"[Tech3C Bridge] OnUserInfoReceived received: {message}");
|
|
|
|
if (userInfoCallback == null)
|
|
{
|
|
Debug.LogWarning("[Tech3C Bridge] No user info callback registered");
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
string[] parts = message.Split('|');
|
|
if (parts.Length >= 4)
|
|
{
|
|
string userId = parts[0];
|
|
string username = parts[1];
|
|
string email = parts[2];
|
|
string phone = parts[3];
|
|
|
|
Debug.Log($"[Tech3C Bridge] Parsed user info: userId={userId}, username={username}");
|
|
userInfoCallback.OnUserInfoReceived(userId, username, email, phone);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError($"[Tech3C Bridge] Invalid message format: {message}");
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError($"[Tech3C Bridge] Error parsing user info message: {e.Message}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called from native code when user info retrieval is cancelled
|
|
/// </summary>
|
|
public void OnUserInfoCancelled(string message)
|
|
{
|
|
Debug.Log("[Tech3C Bridge] OnUserInfoCancelled received");
|
|
|
|
if (userInfoCallback == null)
|
|
{
|
|
Debug.LogWarning("[Tech3C Bridge] No user info callback registered");
|
|
return;
|
|
}
|
|
|
|
userInfoCallback.OnUserInfoCancelled();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called from native code when a user info error occurs
|
|
/// Message format: "errorCode|errorMessage"
|
|
/// </summary>
|
|
public void OnUserInfoError(string message)
|
|
{
|
|
Debug.Log($"[Tech3C Bridge] OnUserInfoError received: {message}");
|
|
|
|
if (userInfoCallback == null)
|
|
{
|
|
Debug.LogWarning("[Tech3C Bridge] No user info callback registered");
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
string[] parts = message.Split('|');
|
|
int errorCode = -1;
|
|
string errorMessage = "Unknown error";
|
|
|
|
if (parts.Length >= 1)
|
|
{
|
|
int.TryParse(parts[0], out errorCode);
|
|
}
|
|
if (parts.Length >= 2)
|
|
{
|
|
errorMessage = parts[1];
|
|
}
|
|
|
|
userInfoCallback.OnUserInfoError(errorCode, errorMessage);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError($"[Tech3C Bridge] Error parsing user info error message: {e.Message}");
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|