207 lines
6.7 KiB
C#
207 lines
6.7 KiB
C#
using Tech3C;
|
|
using BrewMonster;
|
|
using System;
|
|
using UnityEngine;
|
|
|
|
namespace BrewMonster.Scripts
|
|
{
|
|
public enum LOGIN_ERROR_CODE
|
|
{
|
|
SUCCESS = 0,
|
|
CANCELLED = -1
|
|
}
|
|
|
|
public class Tech3CSDKWrapper : Singleton<Tech3CSDKWrapper>
|
|
{
|
|
public string clientId = "";
|
|
public string clientSecret = "";
|
|
|
|
private AuthCallback authCallback;
|
|
private LogoutCallback logoutCallback;
|
|
|
|
/// <summary>
|
|
/// After Tech3C SDK returns result, this callback will be called. <br/>
|
|
/// (errorCode, userId, password)
|
|
/// </summary>
|
|
public Action<int, string, string> onLoginCallback;
|
|
public Action<int, string> onLogOutCallback;
|
|
|
|
public bool isInitialized = false;
|
|
public bool isProcessing = false; // true if the SDK is processing a request (login or logout)
|
|
|
|
#region private functions
|
|
protected override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
|
|
SetupCallbacks();
|
|
|
|
// Auto-initialize SDK
|
|
if (!string.IsNullOrEmpty(clientId) && !string.IsNullOrEmpty(clientSecret))
|
|
{
|
|
BMLogger.Log($"Initializing Tech3C SDK...\n Client ID: {clientId}");
|
|
Tech3CSDK.Instance.Initialize(clientId, clientSecret);
|
|
|
|
if (Tech3CSDK.Instance.IsInitialized)
|
|
{
|
|
BMLogger.Log("[SDK] SDK Initialized Successfully!");
|
|
}
|
|
else
|
|
{
|
|
BMLogger.Log("[SDK] Failed to initialize SDK");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
BMLogger.Log("[SDK] Client ID and/or Client Secret not set. Please configure in Inspector.");
|
|
}
|
|
|
|
BMLogger.Log("[SDK] Tech3C SDK Simple Demo Started");
|
|
|
|
isInitialized = true;
|
|
}
|
|
|
|
private void SetupCallbacks()
|
|
{
|
|
// Auth callback
|
|
authCallback = new AuthCallback();
|
|
authCallback.OnAuthSuccessEvent += OnAuthSuccessEvent;
|
|
authCallback.OnAuthCancelledEvent += OnAuthCancelledEvent;
|
|
authCallback.OnAuthErrorEvent += OnAuthErrorEvent;
|
|
|
|
// Logout callback
|
|
logoutCallback = new LogoutCallback();
|
|
logoutCallback.OnLogoutSuccessEvent += OnLogoutSuccessEvent;
|
|
logoutCallback.OnLogoutErrorEvent += OnLogoutErrorEvent;
|
|
}
|
|
#endregion
|
|
|
|
#region SDK Callbacks
|
|
private void OnAuthSuccessEvent(string userId, string password, string accessToken, string refreshToken, LoginType loginType, long expiryTime)
|
|
{
|
|
isProcessing = false;
|
|
BMLogger.Log($"[SDK] Auth Success!\n User ID: {userId}\n Password: {password}\n Login Type: {loginType}\n Token: {accessToken?.Substring(0, Mathf.Min(20, accessToken?.Length ?? 0))}...");
|
|
onLoginCallback?.Invoke((byte)LOGIN_ERROR_CODE.SUCCESS, userId, password);
|
|
}
|
|
|
|
private void OnAuthCancelledEvent()
|
|
{
|
|
isProcessing = false;
|
|
BMLogger.Log("[SDK] Auth Cancelled by user");
|
|
onLoginCallback?.Invoke((int)LOGIN_ERROR_CODE.CANCELLED, "", "");
|
|
}
|
|
|
|
private void OnAuthErrorEvent(int errorCode, string errorMessage)
|
|
{
|
|
isProcessing = false;
|
|
BMLogger.Log($"[SDK] Auth Error [{errorCode}]: {errorMessage}");
|
|
onLoginCallback?.Invoke(errorCode, errorMessage, string.Empty);
|
|
}
|
|
|
|
private void OnLogoutSuccessEvent()
|
|
{
|
|
isProcessing = false;
|
|
BMLogger.Log("[SDK] Logout Successful");
|
|
onLogOutCallback?.Invoke((int)LOGIN_ERROR_CODE.SUCCESS, "");
|
|
}
|
|
|
|
private void OnLogoutErrorEvent(int errorCode, string errorMessage)
|
|
{
|
|
isProcessing = false;
|
|
BMLogger.Log($"[SDK] Logout Error [{errorCode}]: {errorMessage}");
|
|
onLogOutCallback?.Invoke(errorCode, errorMessage);
|
|
}
|
|
#endregion
|
|
|
|
|
|
#region public functions
|
|
|
|
public void SetLoginCallback(Action<int, string, string> callback)
|
|
{
|
|
if (callback != null)
|
|
{
|
|
BMLogger.LogWarning("[SDK] Login callback is already set, it will be overridden");
|
|
}
|
|
onLoginCallback = callback;
|
|
}
|
|
|
|
public void RemoveLoginCallback()
|
|
{
|
|
onLoginCallback = null;
|
|
}
|
|
|
|
public void SetLogoutCallback(Action<int, string> callback)
|
|
{
|
|
if (callback != null)
|
|
{
|
|
BMLogger.LogWarning("[SDK] Logout callback is already set, it will be overridden");
|
|
}
|
|
onLogOutCallback = callback;
|
|
}
|
|
|
|
public void RemoveLogoutCallback()
|
|
{
|
|
onLogOutCallback = null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Show Tech3C SDK login screen.
|
|
/// Callback will be called after login success or failed.
|
|
/// </summary>
|
|
public bool Login()
|
|
{
|
|
if (!isInitialized)
|
|
{
|
|
BMLogger.LogError("[SDK] SDK is not initialized, please call Initialize() first");
|
|
return false;
|
|
}
|
|
|
|
if (isProcessing)
|
|
{
|
|
BMLogger.LogError("[SDK] SDK is already processing a request, please wait for the current request to complete");
|
|
return false;
|
|
}
|
|
|
|
if (onLoginCallback == null)
|
|
{
|
|
BMLogger.LogError("[SDK] Login callback is not set, please set it using SetLoginCallback");
|
|
return false;
|
|
}
|
|
|
|
isProcessing = true;
|
|
|
|
Tech3CSDK.Instance.ShowAuth(authCallback);
|
|
return true;
|
|
}
|
|
|
|
public bool Logout()
|
|
{
|
|
if (!isInitialized)
|
|
{
|
|
BMLogger.LogError("[SDK] SDK is not initialized, please call Initialize() first");
|
|
return false;
|
|
}
|
|
|
|
if (isProcessing)
|
|
{
|
|
BMLogger.LogError("[SDK] SDK is already processing a request, please wait for the current request to complete");
|
|
return false;
|
|
}
|
|
|
|
|
|
if (onLogOutCallback == null)
|
|
{
|
|
BMLogger.LogError("[SDK] Logout callback is not set, please set it using SetLogoutCallback");
|
|
return false;
|
|
}
|
|
|
|
isProcessing = true;
|
|
|
|
Tech3CSDK.Instance.Logout(logoutCallback);
|
|
return true;
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
} |