using Tech3C; using BrewMonster; using System; using UnityEngine; namespace BrewMonster.Scripts { public enum LOGIN_ERROR_CODE { SUCCESS = 0, CANCELLED = -1 } public class Tech3CSDKWrapper : Singleton { public string clientId = "tghh"; public string clientSecret = "1nBnWnUJadlqzlcd7x7uibXZwW9Bxx9h"; private AuthCallback authCallback; private LogoutCallback logoutCallback; /// /// After Tech3C SDK returns result, this callback will be called.
/// (errorCode, userId, password) ///
public Action onLoginCallback; public Action 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 EnsureInitialized(); BMLogger.Log("[SDK] Tech3C SDK Started"); } public bool EnsureInitialized() { if (Tech3CSDK.Instance.IsInitialized) { isInitialized = true; return true; } if(string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(clientSecret)) { BMLogger.LogError("[SDK] Client ID and/or Client Secret not set. Please configure in Inspector."); isInitialized = false; return false; } 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"); } isInitialized = Tech3CSDK.Instance.IsInitialized; return isInitialized; } 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 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 callback) { if (callback != null) { BMLogger.LogWarning("[SDK] Logout callback is already set, it will be overridden"); } onLogOutCallback = callback; } public void RemoveLogoutCallback() { onLogOutCallback = null; } /// /// Show Tech3C SDK login screen. /// Callback will be called after login success or failed. /// public bool Login() { if (!isInitialized) { BMLogger.LogError("[SDK] [Login] SDK is not initialized, please call Initialize() first"); return false; } if (isProcessing) { BMLogger.LogError("[SDK] [Login] SDK is already processing a request, please wait for the current request to complete"); return false; } if (onLoginCallback == null) { BMLogger.LogError("[SDK] [Login] 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] [Logout] SDK is not initialized, please call Initialize() first"); return false; } if (isProcessing) { BMLogger.LogError("[SDK] [Logout] SDK is already processing a request, please wait for the current request to complete"); return false; } if (onLogOutCallback == null) { BMLogger.LogError("[SDK] [Logout] Logout callback is not set, please set it using SetLogoutCallback"); return false; } //TODO: Check again after 3C resolve the callback issue. // isProcessing = true; Tech3CSDK.Instance.Logout(logoutCallback); return true; } #endregion } }