474 lines
13 KiB
C#
474 lines
13 KiB
C#
using UnityEngine;
|
|
using System;
|
|
|
|
namespace Tech3C
|
|
{
|
|
/// <summary>
|
|
/// Main API wrapper for Tech3C SDK
|
|
/// This is the main entry point for using the SDK in Unity
|
|
/// </summary>
|
|
public class Tech3CSDK : MonoBehaviour
|
|
{
|
|
private static Tech3CSDK instance;
|
|
private ITech3CNativeBridge nativeBridge;
|
|
private Tech3CConfig config;
|
|
private bool isInitialized = false;
|
|
|
|
#region Singleton
|
|
|
|
/// <summary>
|
|
/// Get the singleton instance of Tech3CSDK
|
|
/// </summary>
|
|
public static Tech3CSDK Instance
|
|
{
|
|
get
|
|
{
|
|
if (instance == null)
|
|
{
|
|
GameObject go = new GameObject("Tech3CSDK");
|
|
instance = go.AddComponent<Tech3CSDK>();
|
|
DontDestroyOnLoad(go);
|
|
}
|
|
return instance;
|
|
}
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
if (instance == null)
|
|
{
|
|
instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
else if (instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Initialization
|
|
|
|
/// <summary>
|
|
/// Initialize the Tech3C SDK with configuration
|
|
/// </summary>
|
|
/// <param name="config">The configuration object</param>
|
|
public void Initialize(Tech3CConfig config)
|
|
{
|
|
if (config == null)
|
|
{
|
|
Debug.LogError("[Tech3C] Config cannot be null");
|
|
return;
|
|
}
|
|
|
|
if (!config.IsValid())
|
|
{
|
|
Debug.LogError("[Tech3C] Invalid config: Client ID and Client Secret are required");
|
|
return;
|
|
}
|
|
|
|
this.config = config;
|
|
|
|
// Ensure Tech3CSDKBridge is created to receive callbacks from native code
|
|
var bridge = Tech3CSDKBridge.Instance;
|
|
Debug.Log("[Tech3C] Tech3CSDKBridge ensured for callbacks");
|
|
|
|
// Create platform-specific bridge
|
|
CreateNativeBridge();
|
|
|
|
if (nativeBridge != null)
|
|
{
|
|
nativeBridge.Initialize(config.clientId, config.clientSecret, config);
|
|
isInitialized = true;
|
|
Debug.Log("[Tech3C] Initialization completed successfully");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("[Tech3C] Failed to create native bridge");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initialize the Tech3C SDK with client credentials
|
|
/// </summary>
|
|
/// <param name="clientId">The client ID</param>
|
|
/// <param name="clientSecret">The client secret</param>
|
|
public void Initialize(string clientId, string clientSecret)
|
|
{
|
|
if (string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(clientSecret))
|
|
{
|
|
Debug.LogError("[Tech3C] Client ID and Client Secret are required");
|
|
return;
|
|
}
|
|
|
|
config = Tech3CConfig.CreateDefault();
|
|
config.clientId = clientId;
|
|
config.clientSecret = clientSecret;
|
|
|
|
Initialize(config);
|
|
}
|
|
|
|
private void CreateNativeBridge()
|
|
{
|
|
#if UNITY_ANDROID && !UNITY_EDITOR
|
|
nativeBridge = new Tech3CAndroidBridge();
|
|
#elif UNITY_IOS && !UNITY_EDITOR
|
|
nativeBridge = new Tech3CiOSBridge();
|
|
#else
|
|
#if UNITY_ANDROID
|
|
nativeBridge = new Tech3CAndroidBridge();
|
|
#elif UNITY_IOS
|
|
nativeBridge = new Tech3CiOSBridge();
|
|
#else
|
|
Debug.LogWarning("[Tech3C] Running in Editor or unsupported platform. Using mock implementation.");
|
|
nativeBridge = new MockBridge();
|
|
#endif
|
|
#endif
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Authentication
|
|
|
|
/// <summary>
|
|
/// Show authentication screen (login/register/forgot password)
|
|
/// </summary>
|
|
/// <param name="callback">The callback for authentication result</param>
|
|
public void ShowAuth(IAuthCallback callback)
|
|
{
|
|
if (!isInitialized)
|
|
{
|
|
Debug.LogError("[Tech3C] SDK not initialized. Call Initialize() first.");
|
|
callback?.OnAuthError(-1, "SDK not initialized");
|
|
return;
|
|
}
|
|
|
|
nativeBridge?.ShowAuth(callback);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Show authentication screen without callback
|
|
/// </summary>
|
|
public void ShowAuth()
|
|
{
|
|
ShowAuth(null);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region User Management
|
|
|
|
/// <summary>
|
|
/// Logout current user
|
|
/// </summary>
|
|
/// <param name="callback">The callback for logout result</param>
|
|
public void Logout(ILogoutCallback callback)
|
|
{
|
|
if (!isInitialized)
|
|
{
|
|
Debug.LogError("[Tech3C] SDK not initialized. Call Initialize() first.");
|
|
callback?.OnLogoutError(-1, "SDK not initialized");
|
|
return;
|
|
}
|
|
|
|
nativeBridge?.Logout(callback);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Logout current user without callback
|
|
/// </summary>
|
|
public void Logout()
|
|
{
|
|
Logout(null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get user information
|
|
/// </summary>
|
|
/// <param name="callback">The callback for user info result</param>
|
|
public void GetUserInfo(IUserInfoCallback callback)
|
|
{
|
|
if (!isInitialized)
|
|
{
|
|
Debug.LogError("[Tech3C] SDK not initialized. Call Initialize() first.");
|
|
callback?.OnUserInfoError(-1, "SDK not initialized");
|
|
return;
|
|
}
|
|
|
|
nativeBridge?.GetUserInfo(callback);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get user information without callback
|
|
/// </summary>
|
|
public void GetUserInfo()
|
|
{
|
|
GetUserInfo(null);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region State Checkers
|
|
|
|
/// <summary>
|
|
/// Check if user is logged in
|
|
/// </summary>
|
|
public bool IsLoggedIn()
|
|
{
|
|
if (!isInitialized)
|
|
{
|
|
Debug.LogWarning("[Tech3C] SDK not initialized");
|
|
return false;
|
|
}
|
|
|
|
return nativeBridge?.IsLoggedIn() ?? false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if access token is expired
|
|
/// </summary>
|
|
public bool IsTokenExpired()
|
|
{
|
|
if (!isInitialized)
|
|
{
|
|
Debug.LogWarning("[Tech3C] SDK not initialized");
|
|
return true;
|
|
}
|
|
|
|
return nativeBridge?.IsTokenExpired() ?? true;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Token Management
|
|
|
|
/// <summary>
|
|
/// Get access token
|
|
/// </summary>
|
|
public string GetAccessToken()
|
|
{
|
|
if (!isInitialized)
|
|
{
|
|
Debug.LogWarning("[Tech3C] SDK not initialized");
|
|
return null;
|
|
}
|
|
|
|
return nativeBridge?.GetAccessToken();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get refresh token
|
|
/// </summary>
|
|
public string GetRefreshToken()
|
|
{
|
|
if (!isInitialized)
|
|
{
|
|
Debug.LogWarning("[Tech3C] SDK not initialized");
|
|
return null;
|
|
}
|
|
|
|
return nativeBridge?.GetRefreshToken();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region User Information
|
|
|
|
/// <summary>
|
|
/// Get user ID
|
|
/// </summary>
|
|
public string GetUserId()
|
|
{
|
|
if (!isInitialized)
|
|
{
|
|
Debug.LogWarning("[Tech3C] SDK not initialized");
|
|
return null;
|
|
}
|
|
|
|
return nativeBridge?.GetUserId();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get device ID
|
|
/// </summary>
|
|
public string GetDeviceId()
|
|
{
|
|
if (!isInitialized)
|
|
{
|
|
Debug.LogWarning("[Tech3C] SDK not initialized");
|
|
return null;
|
|
}
|
|
|
|
return nativeBridge?.GetDeviceId();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get login timestamp
|
|
/// </summary>
|
|
public long GetLoginTime()
|
|
{
|
|
if (!isInitialized)
|
|
{
|
|
Debug.LogWarning("[Tech3C] SDK not initialized");
|
|
return 0;
|
|
}
|
|
|
|
return nativeBridge?.GetLoginTime() ?? 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get token expiry timestamp
|
|
/// </summary>
|
|
public long GetTokenExpiry()
|
|
{
|
|
if (!isInitialized)
|
|
{
|
|
Debug.LogWarning("[Tech3C] SDK not initialized");
|
|
return 0;
|
|
}
|
|
|
|
return nativeBridge?.GetTokenExpiry() ?? 0;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Configuration
|
|
|
|
/// <summary>
|
|
/// Set display language
|
|
/// </summary>
|
|
/// <param name="language">The language to set</param>
|
|
public void SetLanguage(Language language)
|
|
{
|
|
if (!isInitialized)
|
|
{
|
|
Debug.LogWarning("[Tech3C] SDK not initialized");
|
|
return;
|
|
}
|
|
|
|
nativeBridge?.SetLanguage(language);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set debug mode
|
|
/// </summary>
|
|
/// <param name="debug">Enable or disable debug mode</param>
|
|
public void SetDebug(bool debug)
|
|
{
|
|
if (!isInitialized)
|
|
{
|
|
Debug.LogWarning("[Tech3C] SDK not initialized");
|
|
return;
|
|
}
|
|
|
|
nativeBridge?.SetDebug(debug);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Cleanup
|
|
|
|
/// <summary>
|
|
/// Cleanup SDK resources
|
|
/// </summary>
|
|
public void Cleanup()
|
|
{
|
|
nativeBridge?.Cleanup();
|
|
nativeBridge = null;
|
|
config = null;
|
|
isInitialized = false;
|
|
Debug.Log("[Tech3C] Cleanup completed");
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
Cleanup();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
/// <summary>
|
|
/// Get the current configuration
|
|
/// </summary>
|
|
public Tech3CConfig Config => config;
|
|
|
|
/// <summary>
|
|
/// Check if SDK is initialized
|
|
/// </summary>
|
|
public bool IsInitialized => isInitialized;
|
|
|
|
#endregion
|
|
}
|
|
|
|
/// <summary>
|
|
/// Mock bridge for testing in editor or unsupported platforms
|
|
/// </summary>
|
|
internal class MockBridge : ITech3CNativeBridge
|
|
{
|
|
private bool isLoggedIn = false;
|
|
private string accessToken = "mock_access_token";
|
|
private string refreshToken = "mock_refresh_token";
|
|
private string userId = "mock_user_id";
|
|
|
|
public void Initialize(string clientId, string clientSecret, Tech3CConfig config)
|
|
{
|
|
Debug.Log($"[Tech3C Mock] Initialized with Client ID: {clientId}");
|
|
}
|
|
|
|
public void ShowAuth(IAuthCallback callback)
|
|
{
|
|
Debug.Log("[Mock3C Mock] ShowAuth called");
|
|
isLoggedIn = true;
|
|
string password = "mock_password";
|
|
callback?.OnAuthSuccess(userId, password, accessToken, refreshToken, LoginType.ACCOUNT, DateTime.Now.Ticks + 3600000);
|
|
}
|
|
|
|
public void Logout(ILogoutCallback callback)
|
|
{
|
|
Debug.Log("[Tech3C Mock] Logout called");
|
|
isLoggedIn = false;
|
|
callback?.OnLogoutSuccess();
|
|
}
|
|
|
|
public void GetUserInfo(IUserInfoCallback callback)
|
|
{
|
|
Debug.Log("[Tech3C Mock] GetUserInfo called");
|
|
callback?.OnUserInfoReceived(userId, "mock_username", "mock@example.com", "1234567890");
|
|
}
|
|
|
|
public bool IsLoggedIn() => isLoggedIn;
|
|
|
|
public string GetAccessToken() => isLoggedIn ? accessToken : null;
|
|
|
|
public string GetRefreshToken() => isLoggedIn ? refreshToken : null;
|
|
|
|
public string GetUserId() => isLoggedIn ? userId : null;
|
|
|
|
public string GetDeviceId() => "mock_device_id";
|
|
|
|
public long GetLoginTime() => isLoggedIn ? DateTime.Now.Ticks : 0;
|
|
|
|
public long GetTokenExpiry() => isLoggedIn ? DateTime.Now.Ticks + 3600000 : 0;
|
|
|
|
public bool IsTokenExpired() => false;
|
|
|
|
public void SetLanguage(Language language)
|
|
{
|
|
Debug.Log($"[Tech3C Mock] Language set to {language}");
|
|
}
|
|
|
|
public void SetDebug(bool debug)
|
|
{
|
|
Debug.Log($"[Tech3C Mock] Debug mode set to {debug}");
|
|
}
|
|
|
|
public void Cleanup()
|
|
{
|
|
Debug.Log("[Tech3C Mock] Cleanup completed");
|
|
}
|
|
}
|
|
}
|