diff --git a/Assets/PerfectWorld/Scripts/Common/Singleton.cs b/Assets/PerfectWorld/Scripts/Common/Singleton.cs index 5b16cdb2e1..8a84813091 100644 --- a/Assets/PerfectWorld/Scripts/Common/Singleton.cs +++ b/Assets/PerfectWorld/Scripts/Common/Singleton.cs @@ -17,5 +17,10 @@ namespace BrewMonster if (_instance.IsValueCreated) throw new InvalidOperationException($"Singleton<{typeof(T).Name}> already created!"); } + + protected virtual void Initialize() + { + + } } } diff --git a/Assets/PerfectWorld/Scripts/Tech3CSDK.meta b/Assets/PerfectWorld/Scripts/Tech3CSDK.meta new file mode 100644 index 0000000000..06775562c2 --- /dev/null +++ b/Assets/PerfectWorld/Scripts/Tech3CSDK.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 9cfabbaaddd233a4a9d4f07a3577d3a2 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/PerfectWorld/Scripts/Tech3CSDK/Tech3CSDKWrapper.cs b/Assets/PerfectWorld/Scripts/Tech3CSDK/Tech3CSDKWrapper.cs new file mode 100644 index 0000000000..7b931f18ed --- /dev/null +++ b/Assets/PerfectWorld/Scripts/Tech3CSDK/Tech3CSDKWrapper.cs @@ -0,0 +1,207 @@ +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 = ""; + public string clientSecret = ""; + + 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 + 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 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] 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 + + } +} \ No newline at end of file diff --git a/Assets/PerfectWorld/Scripts/Tech3CSDK/Tech3CSDKWrapper.cs.meta b/Assets/PerfectWorld/Scripts/Tech3CSDK/Tech3CSDKWrapper.cs.meta new file mode 100644 index 0000000000..07834551d0 --- /dev/null +++ b/Assets/PerfectWorld/Scripts/Tech3CSDK/Tech3CSDKWrapper.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 61795692226b05849aec56bf168c905a \ No newline at end of file diff --git a/Assets/PerfectWorld/Scripts/UI/Login/LoginScreenUI.cs b/Assets/PerfectWorld/Scripts/UI/Login/LoginScreenUI.cs index 559b51cac9..e346a83d27 100644 --- a/Assets/PerfectWorld/Scripts/UI/Login/LoginScreenUI.cs +++ b/Assets/PerfectWorld/Scripts/UI/Login/LoginScreenUI.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using BrewMonster.Network; +using BrewMonster.Scripts; using CSNetwork.Protocols; using CSNetwork.Protocols.RPCData; using TMPro; @@ -33,6 +34,12 @@ namespace BrewMonster.UI bool isDoneNPCRender = false; private SynchronizationContext context; public AudioClip loginBGM; + + void Awake() + { + var tech3CWrapper = Tech3CSDKWrapper.Instance; + } + void Start() { AudioManager.Instance.PlayBGM(loginBGM, 1.5f); @@ -41,7 +48,9 @@ namespace BrewMonster.UI _usernameInputField.text = PlayerPrefs.GetString("username", ""); _passwordInputField.text = PlayerPrefs.GetString("password", ""); - + + Tech3CSDKWrapper.Instance.SetLoginCallback(OnLoginCallback); + Tech3CSDKWrapper.Instance.SetLogoutCallback(OnLogoutCallback); } // Update is called once per frame @@ -69,18 +78,16 @@ namespace BrewMonster.UI #endif } + + private void OnDisable() + { + Tech3CSDKWrapper.Instance.RemoveLoginCallback(); + Tech3CSDKWrapper.Instance.RemoveLogoutCallback(); + } + public async void OnLoginButtonClicked() { - BMLogger.Log("OnLoginButtonClicked"); - string username = _usernameInputField.text; - string password = _passwordInputField.text; - // UnityGameSession.SetConnectionInfo("103.182.22.52", 29000); - UnityGameSession.SetConnectionInfo("103.51.120.195", 29000); - PlayerPrefs.SetString("username", username); - PlayerPrefs.SetString("password", password); - PlayerPrefs.Save(); - await UnityGameSession.Login(username, password, OnLoginComplete); - _selectCharacterScreen.gameObject.SetActive(true); + Tech3CSDKWrapper.Instance.Login(); } /// @@ -278,5 +285,37 @@ namespace BrewMonster.UI } } #endif + + private async void OnLoginCallback(int errorCode, string userId, string password) + { + if (errorCode == 0) + { + BMLogger.Log($"Login success -- userId: {userId} - {password}"); + // UnityGameSession.SetConnectionInfo("103.182.22.52", 29000); + UnityGameSession.SetConnectionInfo("103.51.120.195", 29000); + PlayerPrefs.SetString("username", userId); + PlayerPrefs.SetString("password", password); + PlayerPrefs.Save(); + await UnityGameSession.Login(userId, password, OnLoginComplete); + _selectCharacterScreen.gameObject.SetActive(true); + } + else + { + // if it failed, the userId will be the error message + BMLogger.LogError($"Login failed -- errorCode: {errorCode}: {userId}"); + } + } + + private void OnLogoutCallback(int errorCode, string errorMessage) + { + if (errorCode == 0) + { + BMLogger.Log("Logout success"); + } + else + { + BMLogger.LogError($"Logout failed -- errorCode: {errorCode}: {errorMessage}"); + } + } } } \ No newline at end of file diff --git a/Assets/Plugins/Android.meta b/Assets/Plugins/Android.meta new file mode 100644 index 0000000000..ba484d202e --- /dev/null +++ b/Assets/Plugins/Android.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: f45408c4c89289d498475d0e1e1e5b7e +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/Android/AndroidManifest.xml b/Assets/Plugins/Android/AndroidManifest.xml new file mode 100644 index 0000000000..783069970e --- /dev/null +++ b/Assets/Plugins/Android/AndroidManifest.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/Assets/Plugins/Android/AndroidManifest.xml.meta b/Assets/Plugins/Android/AndroidManifest.xml.meta new file mode 100644 index 0000000000..bfa9b92c0a --- /dev/null +++ b/Assets/Plugins/Android/AndroidManifest.xml.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 11f5d7426047d6048a47d330b02103d9 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/Android/baseProjectTemplate.gradle.DISABLED b/Assets/Plugins/Android/baseProjectTemplate.gradle.DISABLED new file mode 100644 index 0000000000..fd43460978 --- /dev/null +++ b/Assets/Plugins/Android/baseProjectTemplate.gradle.DISABLED @@ -0,0 +1,13 @@ +plugins { + // If you are changing the Android Gradle Plugin version, make sure it is compatible with the Gradle version preinstalled with Unity + // See which Gradle version is preinstalled with Unity here https://docs.unity3d.com/Manual/android-gradle-overview.html + // See official Gradle and Android Gradle Plugin compatibility table here https://developer.android.com/studio/releases/gradle-plugin#updating-gradle + // To specify a custom Gradle version in Unity, go do "Preferences > External Tools", uncheck "Gradle Installed with Unity (recommended)" and specify a path to a custom Gradle version + id 'com.android.application' version '7.4.2' apply false + id 'com.android.library' version '7.4.2' apply false + **BUILD_SCRIPT_DEPS** +} + +task clean(type: Delete) { + delete rootProject.buildDir +} diff --git a/Assets/Plugins/Android/baseProjectTemplate.gradle.DISABLED.meta b/Assets/Plugins/Android/baseProjectTemplate.gradle.DISABLED.meta new file mode 100644 index 0000000000..f58f05f816 --- /dev/null +++ b/Assets/Plugins/Android/baseProjectTemplate.gradle.DISABLED.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 7736ab544ec5cab4a89af84ac45b5a75 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/Android/mainTemplate.gradle b/Assets/Plugins/Android/mainTemplate.gradle new file mode 100644 index 0000000000..323c9d1038 --- /dev/null +++ b/Assets/Plugins/Android/mainTemplate.gradle @@ -0,0 +1,51 @@ +apply plugin: 'com.android.library' +apply from: '../shared/keepUnitySymbols.gradle' +**APPLY_PLUGINS** + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation "androidx.appcompat:appcompat:1.6.1" + implementation "com.google.android.material:material:1.11.0" +**DEPS**} + +android { + ndkVersion "**NDKVERSION**" + namespace "com.unity3d.player" + ndkPath "**NDKPATH**" + compileSdk **APIVERSION** + buildToolsVersion '**BUILDTOOLS**' + + compileOptions { + sourceCompatibility JavaVersion.VERSION_11 + targetCompatibility JavaVersion.VERSION_11 + } + + buildFeatures { + viewBinding true + } + + defaultConfig { +**DEFAULT_CONFIG_SETUP** + minSdk **MINSDK** + targetSdk **TARGETSDK** + ndk { + debugSymbolLevel **DEBUGSYMBOLLEVEL** + abiFilters **ABIFILTERS** + } + versionCode **VERSIONCODE** + versionName '**VERSIONNAME**' + consumerProguardFiles 'proguard-unity.txt'**USER_PROGUARD** + } + + lint { + abortOnError false + } + + androidResources { + noCompress = **BUILTIN_NOCOMPRESS** + unityStreamingAssets.tokenize(', ') + ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:!CVS:!thumbs.db:!picasa.ini:!*~" + }**PACKAGING** +} +**IL_CPP_BUILD_SETUP** +**SOURCE_BUILD_SETUP** +**EXTERNAL_SOURCES** diff --git a/Assets/Plugins/Android/mainTemplate.gradle.backup b/Assets/Plugins/Android/mainTemplate.gradle.backup new file mode 100644 index 0000000000..c5203ee0d7 --- /dev/null +++ b/Assets/Plugins/Android/mainTemplate.gradle.backup @@ -0,0 +1,47 @@ +apply plugin: 'com.android.library' +**APPLY_PLUGINS** + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation "androidx.appcompat:appcompat:1.6.1" + implementation "com.google.android.material:material:1.11.0" +**DEPS**} + +android { + namespace "com.unity3d.player" + ndkPath "**NDKPATH**" + compileSdkVersion **APIVERSION** + buildToolsVersion '**BUILDTOOLS**' + + compileOptions { + sourceCompatibility JavaVersion.VERSION_11 + targetCompatibility JavaVersion.VERSION_11 + } + + buildFeatures { + viewBinding true + } + + defaultConfig { + minSdkVersion **MINSDKVERSION** + targetSdkVersion **TARGETSDKVERSION** + ndk { + abiFilters **ABIFILTERS** + } + versionCode **VERSIONCODE** + versionName '**VERSIONNAME**' + consumerProguardFiles 'proguard-unity.txt'**USER_PROGUARD** + } + + lintOptions { + abortOnError false + } + + aaptOptions { + noCompress = **BUILTIN_NOCOMPRESS** + unityStreamingAssets.tokenize(', ') + ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:!CVS:!thumbs.db:!picasa.ini:!*~" + }**PACKAGING_OPTIONS** +} +**IL_CPP_BUILD_SETUP** +**SOURCE_BUILD_SETUP** +**EXTERNAL_SOURCES** diff --git a/Assets/Plugins/Android/mainTemplate.gradle.backup.meta b/Assets/Plugins/Android/mainTemplate.gradle.backup.meta new file mode 100644 index 0000000000..db9977c019 --- /dev/null +++ b/Assets/Plugins/Android/mainTemplate.gradle.backup.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: f3bedb9070cd041a2bb8a27d9751aada +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/Android/mainTemplate.gradle.meta b/Assets/Plugins/Android/mainTemplate.gradle.meta new file mode 100644 index 0000000000..ff480e90c1 --- /dev/null +++ b/Assets/Plugins/Android/mainTemplate.gradle.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 950fdc575e5b3b74ea1d3f1c60322d4f +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tech3C.meta b/Assets/Tech3C.meta new file mode 100644 index 0000000000..b197cdb0af --- /dev/null +++ b/Assets/Tech3C.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 86378052e9798924e8ee14f4fa780f0f +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tech3C/Plugins.meta b/Assets/Tech3C/Plugins.meta new file mode 100644 index 0000000000..9c87ae31f2 --- /dev/null +++ b/Assets/Tech3C/Plugins.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 4c935d070fff35741adebc5424c4e02b +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tech3C/Plugins/Android.meta b/Assets/Tech3C/Plugins/Android.meta new file mode 100644 index 0000000000..f279ada1af --- /dev/null +++ b/Assets/Tech3C/Plugins/Android.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 2111256da5c2b2a469d98ce0f103cdb7 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tech3C/Plugins/Android/Tech3CUnityBridge.java b/Assets/Tech3C/Plugins/Android/Tech3CUnityBridge.java new file mode 100644 index 0000000000..6dd99c4ee8 --- /dev/null +++ b/Assets/Tech3C/Plugins/Android/Tech3CUnityBridge.java @@ -0,0 +1,184 @@ +package vn.tech3c.sdk.unity; + +import android.util.Log; + +import vn.tech3c.sdk.auth.callback.OnAuthCallback; +import vn.tech3c.sdk.auth.controller.Tech3CIdController; +import vn.tech3c.sdk.auth.entities.enums.LoginType; +import vn.tech3c.sdk.auth.exceptions.Tech3CIdException; + +import com.unity3d.player.UnityPlayer; + +/** + * Unity Bridge for Tech3C SDK + * This class receives callbacks from Tech3C SDK and sends them to Unity via UnitySendMessage + */ +public class Tech3CUnityBridge implements OnAuthCallback { + private static final String TAG = "Tech3CUnityBridge"; + private static final String UNITY_GAME_OBJECT = "Tech3CSDKBridge"; + + private static Tech3CUnityBridge instance; + + private OnUnityAuthCallback unityAuthCallback; + private OnUnityLogoutCallback unityLogoutCallback; + private OnUnityUserInfoCallback unityUserInfoCallback; + + // Singleton + public static synchronized Tech3CUnityBridge getInstance() { + if (instance == null) { + instance = new Tech3CUnityBridge(); + } + return instance; + } + + // Interfaces for Unity callbacks + public interface OnUnityAuthCallback { + void onAuthSuccess(String userId, String password, String accessToken, String refreshToken, LoginType loginType, long expiryTime); + void onAuthCancelled(); + void onAuthError(int errorCode, String errorMessage); + } + + public interface OnUnityLogoutCallback { + void onLogoutSuccess(); + void onLogoutError(int errorCode, String errorMessage); + } + + public interface OnUnityUserInfoCallback { + void onUserInfoReceived(String userId, String username, String email, String phone); + void onUserInfoCancelled(); + void onUserInfoError(int errorCode, String errorMessage); + } + + // Set callbacks + public void setUnityAuthCallback(OnUnityAuthCallback callback) { + this.unityAuthCallback = callback; + } + + public void setUnityLogoutCallback(OnUnityLogoutCallback callback) { + this.unityLogoutCallback = callback; + } + + public void setUnityUserInfoCallback(OnUnityUserInfoCallback callback) { + this.unityUserInfoCallback = callback; + } + + // Send message to Unity + private void sendToUnity(String methodName, String message) { + try { + UnityPlayer.UnitySendMessage(UNITY_GAME_OBJECT, methodName, message); + Log.d(TAG, "Sent to Unity: " + methodName + " - " + message); + } catch (Exception e) { + Log.e(TAG, "Error sending to Unity: " + e.getMessage()); + } + } + + // ========== OnAuthCallback Implementation ========== + + @Override + public void onLoginSuccess(String userId, String password, String accessToken, String refreshToken, LoginType loginType, long expiryTime) { + Log.d(TAG, "onLoginSuccess: " + userId); + + // Send to Unity + String message = userId + "|" + password + "|" + accessToken + "|" + refreshToken + "|" + loginType.name() + "|" + expiryTime; + sendToUnity("OnAuthSuccess", message); + + // Also call Unity callback if set + if (unityAuthCallback != null) { + unityAuthCallback.onAuthSuccess(userId, password, accessToken, refreshToken, loginType, expiryTime); + } + } + + @Override + public void onRegisterSuccess(String accessToken, String refreshToken, String userId, long expiryTime) { + Log.d(TAG, "onRegisterSuccess: " + userId); + + // Treat register success as auth success (password is empty for register) + String message = userId + "|" + "" + "|" + accessToken + "|" + refreshToken + "|REGISTER|" + expiryTime; + sendToUnity("OnAuthSuccess", message); + + if (unityAuthCallback != null) { + unityAuthCallback.onAuthSuccess(userId, "", accessToken, refreshToken, LoginType.ACCOUNT, expiryTime); + } + } + + @Override + public void onAuthCancelled() { + Log.d(TAG, "onAuthCancelled"); + sendToUnity("OnAuthCancelled", ""); + + if (unityAuthCallback != null) { + unityAuthCallback.onAuthCancelled(); + } + } + + @Override + public void onAuthScreenOpened() { + Log.d(TAG, "onAuthScreenOpened"); + // Optional: Send to Unity if needed + } + + @Override + public void onError(Tech3CIdException exception) { + Log.d(TAG, "onError: " + exception.getMessage()); + + String message = exception.getErrorCode() + "|" + exception.getMessage(); + sendToUnity("OnAuthError", message); + + if (unityAuthCallback != null) { + unityAuthCallback.onAuthError(exception.getErrorCode(), exception.getMessage()); + } + } + + // ========== Additional Callback Methods ========== + + public void onLogoutSuccess() { + Log.d(TAG, "onLogoutSuccess"); + sendToUnity("OnLogoutSuccess", ""); + + if (unityLogoutCallback != null) { + unityLogoutCallback.onLogoutSuccess(); + } + } + + public void onLogoutError(int errorCode, String errorMessage) { + Log.d(TAG, "onLogoutError: " + errorMessage); + + String message = errorCode + "|" + errorMessage; + sendToUnity("OnLogoutError", message); + + if (unityLogoutCallback != null) { + unityLogoutCallback.onLogoutError(errorCode, errorMessage); + } + } + + public void onUserInfoReceived(String userId, String username, String email, String phone) { + Log.d(TAG, "onUserInfoReceived: " + userId); + + String message = userId + "|" + username + "|" + email + "|" + phone; + sendToUnity("OnUserInfoReceived", message); + + if (unityUserInfoCallback != null) { + unityUserInfoCallback.onUserInfoReceived(userId, username, email, phone); + } + } + + public void onUserInfoCancelled() { + Log.d(TAG, "onUserInfoCancelled"); + sendToUnity("OnUserInfoCancelled", ""); + + if (unityUserInfoCallback != null) { + unityUserInfoCallback.onUserInfoCancelled(); + } + } + + public void onUserInfoError(int errorCode, String errorMessage) { + Log.d(TAG, "onUserInfoError: " + errorMessage); + + String message = errorCode + "|" + errorMessage; + sendToUnity("OnUserInfoError", message); + + if (unityUserInfoCallback != null) { + unityUserInfoCallback.onUserInfoError(errorCode, errorMessage); + } + } +} diff --git a/Assets/Tech3C/Plugins/Android/Tech3CUnityBridge.java.meta b/Assets/Tech3C/Plugins/Android/Tech3CUnityBridge.java.meta new file mode 100644 index 0000000000..6f929e7398 --- /dev/null +++ b/Assets/Tech3C/Plugins/Android/Tech3CUnityBridge.java.meta @@ -0,0 +1,32 @@ +fileFormatVersion: 2 +guid: e32ed25f4a2155a4f8106c56c82bb7fc +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + defineConstraints: [] + isPreloaded: 0 + isOverridable: 0 + isExplicitlyReferenced: 0 + validateReferences: 1 + platformData: + - first: + Android: Android + second: + enabled: 1 + settings: {} + - first: + Any: + second: + enabled: 0 + settings: {} + - first: + Editor: Editor + second: + enabled: 0 + settings: + DefaultValueInitialized: true + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tech3C/Plugins/Android/auth-1.2.1.aar b/Assets/Tech3C/Plugins/Android/auth-1.2.1.aar new file mode 100644 index 0000000000..b972ff9123 Binary files /dev/null and b/Assets/Tech3C/Plugins/Android/auth-1.2.1.aar differ diff --git a/Assets/Tech3C/Plugins/Android/auth-1.2.1.aar.meta b/Assets/Tech3C/Plugins/Android/auth-1.2.1.aar.meta new file mode 100644 index 0000000000..26ca9658d7 --- /dev/null +++ b/Assets/Tech3C/Plugins/Android/auth-1.2.1.aar.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 3385cda9ff7dcd74ebc47f50d2e8196c \ No newline at end of file diff --git a/Assets/Tech3C/Plugins/Android/gradle.properties b/Assets/Tech3C/Plugins/Android/gradle.properties new file mode 100644 index 0000000000..646c51b977 --- /dev/null +++ b/Assets/Tech3C/Plugins/Android/gradle.properties @@ -0,0 +1,2 @@ +android.useAndroidX=true +android.enableJetifier=true diff --git a/Assets/Tech3C/Plugins/Android/gradle.properties.meta b/Assets/Tech3C/Plugins/Android/gradle.properties.meta new file mode 100644 index 0000000000..235ab6ba4b --- /dev/null +++ b/Assets/Tech3C/Plugins/Android/gradle.properties.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 5b51400b9b30374459987374d5fb2a0a +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tech3C/Plugins/Android/mainTemplate.gradle b/Assets/Tech3C/Plugins/Android/mainTemplate.gradle new file mode 100644 index 0000000000..d5267dce20 --- /dev/null +++ b/Assets/Tech3C/Plugins/Android/mainTemplate.gradle @@ -0,0 +1,43 @@ +apply plugin: 'com.android.library' +**APPLY_PLUGINS** + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation "androidx.appcompat:appcompat:1.6.1" + implementation "com.google.android.material:material:1.11.0" +**DEPS**} + +android { + namespace "com.unity3d.player" + ndkPath "**NDKPATH**" + compileSdkVersion **APIVERSION** + buildToolsVersion '**BUILDTOOLS**' + + compileOptions { + sourceCompatibility JavaVersion.VERSION_11 + targetCompatibility JavaVersion.VERSION_11 + } + + defaultConfig { + minSdkVersion **MINSDKVERSION** + targetSdkVersion **TARGETSDKVERSION** + ndk { + abiFilters **ABIFILTERS** + } + versionCode **VERSIONCODE** + versionName '**VERSIONNAME**' + consumerProguardFiles 'proguard-unity.txt'**USER_PROGUARD** + } + + lintOptions { + abortOnError false + } + + aaptOptions { + noCompress = **BUILTIN_NOCOMPRESS** + unityStreamingAssets.tokenize(', ') + ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:!CVS:!thumbs.db:!picasa.ini:!*~" + }**PACKAGING_OPTIONS** +} +**IL_CPP_BUILD_SETUP** +**SOURCE_BUILD_SETUP** +**EXTERNAL_SOURCES** diff --git a/Assets/Tech3C/Plugins/Android/mainTemplate.gradle.meta b/Assets/Tech3C/Plugins/Android/mainTemplate.gradle.meta new file mode 100644 index 0000000000..8ce6936aca --- /dev/null +++ b/Assets/Tech3C/Plugins/Android/mainTemplate.gradle.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 68ef4c077c6bfee47ad1aa16645a896d +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tech3C/Plugins/Android/proguard-user.txt b/Assets/Tech3C/Plugins/Android/proguard-user.txt new file mode 100644 index 0000000000..6b42f31f02 --- /dev/null +++ b/Assets/Tech3C/Plugins/Android/proguard-user.txt @@ -0,0 +1,30 @@ +#### Proguard class tech3c +-keep public class vn.tech3c.sdk.auth.entities.* { + public *; +} +-keep public class vn.tech3c.sdk.auth.entities.enums.* { + public *; +} +-keep public class vn.tech3c.sdk.auth.entities.response.* { + public *; +} +-keep public class vn.tech3c.sdk.auth.entities.request.* { + public *; +} + +-keep public interface vn.tech3c.sdk.auth.** { *; } + +-keep public class vn.tech3c.sdk.auth.controller.Tech3CIdController { public *;} +-keep public class vn.tech3c.sdk.auth.exceptions.Tech3CIdException { public *;} + +-keep public class vn.tech3c.sdk.auth.Tech3CIdAuthentication { + public *; +} + +-keep public class vn.tech3c.sdk.auth.config.Tech3CIdConfig { + public *; +} + +-keep public class vn.tech3c.sdk.example.Tech3CUnityBridge { public *;} +-keep public class vn.tech3c.sdk.example.Tech3CUnityBridge$** { *; } + diff --git a/Assets/Tech3C/Plugins/Android/proguard-user.txt.meta b/Assets/Tech3C/Plugins/Android/proguard-user.txt.meta new file mode 100644 index 0000000000..6b05d1673c --- /dev/null +++ b/Assets/Tech3C/Plugins/Android/proguard-user.txt.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 0a1d44356c1df074d8cd78bee53cbafa +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tech3C/Runtime.meta b/Assets/Tech3C/Runtime.meta new file mode 100644 index 0000000000..8c1c38fee8 --- /dev/null +++ b/Assets/Tech3C/Runtime.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: c5469f93c69421146854ce74deba7931 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tech3C/Runtime/Tech3CAndroidBridge.cs b/Assets/Tech3C/Runtime/Tech3CAndroidBridge.cs new file mode 100644 index 0000000000..c781c13e3c --- /dev/null +++ b/Assets/Tech3C/Runtime/Tech3CAndroidBridge.cs @@ -0,0 +1,452 @@ +using UnityEngine; +using System; + +namespace Tech3C +{ + /// + /// Bridge class for Android platform to communicate with Tech3C native SDK + /// + public class Tech3CAndroidBridge : ITech3CNativeBridge + { + private const string CLASS_NAME = "vn.tech3c.sdk.auth.controller.Tech3CIdController"; + private const string BRIDGE_CLASS = "vn.tech3c.sdk.unity.Tech3CUnityBridge"; + + private AndroidJavaObject controller; + private AndroidJavaObject unityBridge; + + /// + /// Initialize the Tech3C SDK + /// + public void Initialize(string clientId, string clientSecret, Tech3CConfig config) + { + try + { + Debug.Log("[Tech3C] Bridge GameObject initialized"); + + // Initialize Unity Bridge for callbacks first + using (AndroidJavaClass bridgeClass = new AndroidJavaClass(BRIDGE_CLASS)) + { + unityBridge = bridgeClass.CallStatic("getInstance"); + } + + // Initialize controller with method chaining to set callback + if (controller == null) + { + using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) + using (AndroidJavaObject activity = unityPlayer.GetStatic("currentActivity")) + using (AndroidJavaClass controllerClass = new AndroidJavaClass(CLASS_NAME)) + { + // Chain methods like MainActivity: initialize() -> setDebug() -> setOnAuthCallback() + controller = controllerClass.CallStatic( + "initialize", + activity, + clientId, + clientSecret + ) + // .Call("setDebug", true) + // .Call("setDisableExitLogin", false) + // .Call("setEnableMaintenanceCheck", true) + .Call("setOnAuthCallback", unityBridge); + + Debug.Log("[Tech3C] Unity Bridge registered with native SDK"); + } + } + + if (controller != null) + { + // Apply configuration + ApplyConfig(config); + Debug.Log("[Tech3C] Initialized successfully"); + } + else + { + Debug.LogError("[Tech3C] Failed to initialize controller"); + } + } + catch (Exception e) + { + Debug.LogError($"[Tech3C] Initialize error: {e.Message}"); + } + } + + /// + /// Apply configuration settings to the SDK + /// + private void ApplyConfig(Tech3CConfig config) + { + if (controller == null || config == null) return; + + try + { + // Set debug mode (returns Tech3CIdController for chaining) + controller = controller.Call("setDebug", config.debugMode); + + // Set language - pass Java Language enum + using (AndroidJavaClass languageEnumClass = new AndroidJavaClass("vn.tech3c.sdk.auth.entities.enums.Language")) + { + string javaEnumValue = config.language == Language.Vietnamese ? "VIETNAMESE" : "ENGLISH"; + AndroidJavaObject javaLanguage = languageEnumClass.GetStatic(javaEnumValue); + controller = controller.Call("setLanguageDisplay", javaLanguage); + } + + // Set UI mode - pass Java UiMode enum + using (AndroidJavaClass uiModeEnumClass = new AndroidJavaClass("vn.tech3c.sdk.auth.entities.enums.UiMode")) + { + string javaEnumValue = config.uiMode == UiMode.Fullscreen ? "FULLSCREEN" : "DIALOG"; + AndroidJavaObject javaUiMode = uiModeEnumClass.GetStatic(javaEnumValue); + controller = controller.Call("setUiMode", javaUiMode); + } + + // Set orientation - pass Java OrientationMode enum + using (AndroidJavaClass orientationEnumClass = new AndroidJavaClass("vn.tech3c.sdk.auth.entities.enums.OrientationMode")) + { + string javaEnumValue = config.screenOrientation.ToString().ToUpper(); + AndroidJavaObject javaOrientation = orientationEnumClass.GetStatic(javaEnumValue); + controller = controller.Call("setOrientation", javaOrientation); + } + + // Set guest login + controller = controller.Call("setEnableGuestLogin", config.enableGuestLogin); + + // Set OTP requirement + controller = controller.Call("setRequireOtp", config.requireOtp); + + // Set disable exit login + controller = controller.Call("setDisableExitLogin", config.disableExitLogin); + + // Set maintenance check + controller = controller.Call("setEnableMaintenanceCheck", config.enableMaintenanceCheck); + + if (!string.IsNullOrEmpty(config.maintenanceCheckUrl)) + { + controller = controller.Call("setMaintenanceCheckUrl", config.maintenanceCheckUrl); + } + + if (!string.IsNullOrEmpty(config.serverIp)) + { + controller = controller.Call("setIpMaintenanceCheck", config.serverIp); + } + + Debug.Log("[Tech3C] Configuration applied successfully"); + } + catch (Exception e) + { + Debug.LogError($"[Tech3C] ApplyConfig error: {e.Message}"); + } + } + + /// + /// Show authentication screen + /// + public void ShowAuth(IAuthCallback callback) + { + try + { + if (controller == null) + { + Debug.LogError("[Tech3C] Controller not initialized"); + callback?.OnAuthError(-1, "SDK not initialized"); + return; + } + + // Register callback with bridge + Tech3CSDKBridge.SetAuthCallback(callback); + + using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) + using (AndroidJavaObject activity = unityPlayer.GetStatic("currentActivity")) + { + controller.Call("showAuth", activity); + Debug.Log("[Tech3C] Auth screen shown"); + } + } + catch (Exception e) + { + Debug.LogError($"[Tech3C] ShowAuth error: {e.Message}"); + callback?.OnAuthError(-1, e.Message); + } + } + + /// + /// Logout current user + /// + public void Logout(ILogoutCallback callback) + { + try + { + if (controller == null) + { + Debug.LogError("[Tech3C] Controller not initialized"); + callback?.OnLogoutError(-1, "SDK not initialized"); + return; + } + + // Register callback with bridge + Tech3CSDKBridge.SetLogoutCallback(callback); + + controller.Call("logout"); + Debug.Log("[Tech3C] Logout initiated"); + } + catch (Exception e) + { + Debug.LogError($"[Tech3C] Logout error: {e.Message}"); + callback?.OnLogoutError(-1, e.Message); + } + } + + /// + /// Get user information + /// + public void GetUserInfo(IUserInfoCallback callback) + { + try + { + if (controller == null) + { + Debug.LogError("[Tech3C] Controller not initialized"); + callback?.OnUserInfoError(-1, "SDK not initialized"); + return; + } + + // Register callback with bridge + Tech3CSDKBridge.SetUserInfoCallback(callback); + + controller.Call("getUserInfo"); + Debug.Log("[Tech3C] GetUserInfo called"); + } + catch (Exception e) + { + Debug.LogError($"[Tech3C] GetUserInfo error: {e.Message}"); + callback?.OnUserInfoError(-1, e.Message); + } + } + + /// + /// Check if user is logged in + /// + public bool IsLoggedIn() + { + try + { + if (controller == null) return false; + + return controller.Call("isLogin"); + } + catch (Exception e) + { + Debug.LogError($"[Tech3C] IsLoggedIn error: {e.Message}"); + return false; + } + } + + /// + /// Get access token + /// + public string GetAccessToken() + { + try + { + if (controller == null) return null; + + return controller.Call("getAccessToken"); + } + catch (Exception e) + { + Debug.LogError($"[Tech3C] GetAccessToken error: {e.Message}"); + return null; + } + } + + /// + /// Get refresh token + /// + public string GetRefreshToken() + { + try + { + if (controller == null) return null; + + return controller.Call("getRefreshToken"); + } + catch (Exception e) + { + Debug.LogError($"[Tech3C] GetRefreshToken error: {e.Message}"); + return null; + } + } + + /// + /// Get user ID + /// + public string GetUserId() + { + try + { + if (controller == null) return null; + + return controller.Call("getUserId"); + } + catch (Exception e) + { + Debug.LogError($"[Tech3C] GetUserId error: {e.Message}"); + return null; + } + } + + /// + /// Get device ID + /// + public string GetDeviceId() + { + try + { + if (controller == null) return null; + + return controller.Call("getDeviceId"); + } + catch (Exception e) + { + Debug.LogError($"[Tech3C] GetDeviceId error: {e.Message}"); + return null; + } + } + + /// + /// Get login time + /// + public long GetLoginTime() + { + try + { + if (controller == null) return 0; + + return controller.Call("getLoginTime"); + } + catch (Exception e) + { + Debug.LogError($"[Tech3C] GetLoginTime error: {e.Message}"); + return 0; + } + } + + /// + /// Get token expiry time + /// + public long GetTokenExpiry() + { + try + { + if (controller == null) return 0; + + return controller.Call("getTokenExpiry"); + } + catch (Exception e) + { + Debug.LogError($"[Tech3C] GetTokenExpiry error: {e.Message}"); + return 0; + } + } + + /// + /// Check if token is expired + /// + public bool IsTokenExpired() + { + try + { + if (controller == null) return false; + + return controller.Call("isTokenExpired"); + } + catch (Exception e) + { + Debug.LogError($"[Tech3C] IsTokenExpired error: {e.Message}"); + return false; + } + } + + /// + /// Set language + /// + public void SetLanguage(Language language) + { + try + { + if (controller == null) return; + + // Set language - pass Java Language enum + using (AndroidJavaClass languageEnumClass = new AndroidJavaClass("vn.tech3c.sdk.auth.entities.enums.Language")) + { + string javaEnumValue = language == Language.Vietnamese ? "VIETNAMESE" : "ENGLISH"; + AndroidJavaObject javaLanguage = languageEnumClass.GetStatic(javaEnumValue); + controller = controller.Call("setLanguageDisplay", javaLanguage); + } + Debug.Log($"[Tech3C] Language set to {language}"); + } + catch (Exception e) + { + Debug.LogError($"[Tech3C] SetLanguage error: {e.Message}"); + } + } + + /// + /// Set debug mode + /// + public void SetDebug(bool debug) + { + try + { + if (controller == null) return; + + controller = controller.Call("setDebug", debug); + Debug.Log($"[Tech3C] Debug mode set to {debug}"); + } + catch (Exception e) + { + Debug.LogError($"[Tech3C] SetDebug error: {e.Message}"); + } + } + + /// + /// Cleanup resources + /// + public void Cleanup() + { + try + { + if (controller != null) + { + controller.Call("cleanup"); + controller.Dispose(); + controller = null; + Debug.Log("[Tech3C] Cleanup completed"); + } + } + catch (Exception e) + { + Debug.LogError($"[Tech3C] Cleanup error: {e.Message}"); + } + } + } + + /// + /// Interface for native bridge implementations + /// + public interface ITech3CNativeBridge + { + void Initialize(string clientId, string clientSecret, Tech3CConfig config); + void ShowAuth(IAuthCallback callback); + void Logout(ILogoutCallback callback); + void GetUserInfo(IUserInfoCallback callback); + bool IsLoggedIn(); + string GetAccessToken(); + string GetRefreshToken(); + string GetUserId(); + string GetDeviceId(); + long GetLoginTime(); + long GetTokenExpiry(); + bool IsTokenExpired(); + void SetLanguage(Language language); + void SetDebug(bool debug); + void Cleanup(); + } +} diff --git a/Assets/Tech3C/Runtime/Tech3CAndroidBridge.cs.meta b/Assets/Tech3C/Runtime/Tech3CAndroidBridge.cs.meta new file mode 100644 index 0000000000..937d79ca0b --- /dev/null +++ b/Assets/Tech3C/Runtime/Tech3CAndroidBridge.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 50744a202f3e9e24a80e10aecf427dac +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tech3C/Runtime/Tech3CCallbacks.cs b/Assets/Tech3C/Runtime/Tech3CCallbacks.cs new file mode 100644 index 0000000000..7f40b826a3 --- /dev/null +++ b/Assets/Tech3C/Runtime/Tech3CCallbacks.cs @@ -0,0 +1,150 @@ +using System; + +namespace Tech3C +{ + /// + /// Callback for authentication operations (login, register, etc.) + /// + public interface IAuthCallback + { + /// + /// Called when authentication is successful + /// + /// The authenticated user ID + /// The user password + /// The access token + /// The refresh token + /// The type of login used + /// Token expiry timestamp + void OnAuthSuccess(string userId, string password, string accessToken, string refreshToken, LoginType loginType, long expiryTime); + + /// + /// Called when authentication is cancelled by the user + /// + void OnAuthCancelled(); + + /// + /// Called when an error occurs during authentication + /// + /// The error code + /// The error message + void OnAuthError(int errorCode, string errorMessage); + } + + /// + /// Callback for logout operations + /// + public interface ILogoutCallback + { + /// + /// Called when logout is successful + /// + void OnLogoutSuccess(); + + /// + /// Called when an error occurs during logout + /// + /// The error code + /// The error message + void OnLogoutError(int errorCode, string errorMessage); + } + + /// + /// Callback for user info operations + /// + public interface IUserInfoCallback + { + /// + /// Called when user info is successfully retrieved + /// + /// The user ID + /// The username + /// The email address + /// The phone number + void OnUserInfoReceived(string userId, string username, string email, string phone); + + /// + /// Called when user info retrieval is cancelled + /// + void OnUserInfoCancelled(); + + /// + /// Called when an error occurs while retrieving user info + /// + /// The error code + /// The error message + void OnUserInfoError(int errorCode, string errorMessage); + } + + /// + /// Default implementation of IAuthCallback using Unity events + /// + [Serializable] + public class AuthCallback : IAuthCallback + { + public event Action OnAuthSuccessEvent; + public event Action OnAuthCancelledEvent; + public event Action OnAuthErrorEvent; + + public void OnAuthSuccess(string userId, string password, string accessToken, string refreshToken, LoginType loginType, long expiryTime) + { + OnAuthSuccessEvent?.Invoke(userId, password, accessToken, refreshToken, loginType, expiryTime); + } + + public void OnAuthCancelled() + { + OnAuthCancelledEvent?.Invoke(); + } + + public void OnAuthError(int errorCode, string errorMessage) + { + OnAuthErrorEvent?.Invoke(errorCode, errorMessage); + } + } + + /// + /// Default implementation of ILogoutCallback using Unity events + /// + [Serializable] + public class LogoutCallback : ILogoutCallback + { + public event Action OnLogoutSuccessEvent; + public event Action OnLogoutErrorEvent; + + public void OnLogoutSuccess() + { + OnLogoutSuccessEvent?.Invoke(); + } + + public void OnLogoutError(int errorCode, string errorMessage) + { + OnLogoutErrorEvent?.Invoke(errorCode, errorMessage); + } + } + + /// + /// Default implementation of IUserInfoCallback using Unity events + /// + [Serializable] + public class UserInfoCallback : IUserInfoCallback + { + public event Action OnUserInfoReceivedEvent; + public event Action OnUserInfoCancelledEvent; + public event Action OnUserInfoErrorEvent; + + public void OnUserInfoReceived(string userId, string username, string email, string phone) + { + OnUserInfoReceivedEvent?.Invoke(userId, username, email, phone); + } + + public void OnUserInfoCancelled() + { + OnUserInfoCancelledEvent?.Invoke(); + } + + public void OnUserInfoError(int errorCode, string errorMessage) + { + OnUserInfoErrorEvent?.Invoke(errorCode, errorMessage); + } + } +} diff --git a/Assets/Tech3C/Runtime/Tech3CCallbacks.cs.meta b/Assets/Tech3C/Runtime/Tech3CCallbacks.cs.meta new file mode 100644 index 0000000000..b7e5fd3738 --- /dev/null +++ b/Assets/Tech3C/Runtime/Tech3CCallbacks.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ed5aa8a6da34849438530838098c8ca0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tech3C/Runtime/Tech3CConfig.cs b/Assets/Tech3C/Runtime/Tech3CConfig.cs new file mode 100644 index 0000000000..6d31bb34b2 --- /dev/null +++ b/Assets/Tech3C/Runtime/Tech3CConfig.cs @@ -0,0 +1,114 @@ +using UnityEngine; + +namespace Tech3C +{ + /// + /// Configuration class for Tech3C SDK + /// + [CreateAssetMenu(fileName = "Tech3CConfig", menuName = "Tech3C SDK/Configuration", order = 1)] + public class Tech3CConfig : ScriptableObject + { + [Header("Client Credentials")] + [Tooltip("Client ID for Tech3C authentication")] + public string clientId; + + [Tooltip("Client Secret for Tech3C authentication")] + public string clientSecret; + + [Header("General Settings")] + [Tooltip("Enable debug mode for detailed logging")] + public bool debugMode = false; + + [Tooltip("Display language for the SDK")] + public Language language = Language.English; + + [Tooltip("Environment for the SDK")] + public Environment environment = Environment.Production; + + [Header("UI Settings")] + [Tooltip("UI mode for authentication (Fullscreen or Dialog)")] + public UiMode uiMode = UiMode.Fullscreen; + + [Tooltip("Dialog size when UI mode is Dialog")] + public DialogSize dialogSize = DialogSize.Medium; + + [Tooltip("Screen orientation for authentication")] + public OrientationMode screenOrientation = OrientationMode.Auto; + + [Tooltip("Disable exit button on login screen")] + public bool disableExitLogin = false; + + [Header("Authentication Settings")] + [Tooltip("Enable guest login")] + public bool enableGuestLogin = false; + + [Tooltip("Require OTP for authentication")] + public bool requireOtp = false; + + [Tooltip("Enable Google login")] + public bool enableGoogleLogin = false; + + [Tooltip("Enable Facebook login")] + public bool enableFacebookLogin = false; + + [Tooltip("Enable Apple login")] + public bool enableAppleLogin = false; + + [Header("Maintenance Settings")] + [Tooltip("Enable maintenance check")] + public bool enableMaintenanceCheck = false; + + [Tooltip("Maintenance check URL")] + public string maintenanceCheckUrl; + + [Tooltip("Server IP for maintenance check")] + public string serverIp; + + [Header("Network Settings")] + [Tooltip("Timeout in seconds for network requests")] + public int timeoutSeconds = 30; + + /// + /// Creates a default Tech3CConfig asset + /// + public static Tech3CConfig CreateDefault() + { + var config = CreateInstance(); + config.clientId = ""; + config.clientSecret = ""; + config.debugMode = false; + config.language = Language.English; + config.environment = Environment.Production; + config.uiMode = UiMode.Fullscreen; + config.dialogSize = DialogSize.Medium; + config.screenOrientation = OrientationMode.Auto; + config.disableExitLogin = false; + config.enableGuestLogin = false; + config.requireOtp = false; + config.enableGoogleLogin = false; + config.enableFacebookLogin = false; + config.enableAppleLogin = false; + config.enableMaintenanceCheck = false; + config.maintenanceCheckUrl = ""; + config.serverIp = ""; + config.timeoutSeconds = 30; + return config; + } + + /// + /// Validates the configuration + /// + public bool IsValid() + { + return !string.IsNullOrEmpty(clientId) && !string.IsNullOrEmpty(clientSecret); + } + + /// + /// Gets the language code string + /// + public string GetLanguageCode() + { + return language == Language.Vietnamese ? "vi" : "en"; + } + } +} diff --git a/Assets/Tech3C/Runtime/Tech3CConfig.cs.meta b/Assets/Tech3C/Runtime/Tech3CConfig.cs.meta new file mode 100644 index 0000000000..39fd2c7efd --- /dev/null +++ b/Assets/Tech3C/Runtime/Tech3CConfig.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 95a442ac7dacf8b4484a10035308bc31 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tech3C/Runtime/Tech3CEnums.cs b/Assets/Tech3C/Runtime/Tech3CEnums.cs new file mode 100644 index 0000000000..9107769385 --- /dev/null +++ b/Assets/Tech3C/Runtime/Tech3CEnums.cs @@ -0,0 +1,64 @@ +using System; + +namespace Tech3C +{ + /// + /// Language options for the SDK + /// + public enum Language + { + Vietnamese, + English + } + + /// + /// UI mode for authentication dialog + /// + public enum UiMode + { + Fullscreen, + Dialog + } + + /// + /// Screen orientation for authentication + /// + public enum OrientationMode + { + Auto, + Portrait, + Landscape + } + + /// + /// Login type for authentication + /// Matches the native SDK's LoginType enum + /// + public enum LoginType + { + GUEST, + ACCOUNT, + SOCIAL, + REGISTER // For register success handling + } + + /// + /// Environment for the SDK + /// + public enum Environment + { + Production, + Staging, + Development + } + + /// + /// Dialog size for UI mode + /// + public enum DialogSize + { + Small, + Medium, + Large + } +} diff --git a/Assets/Tech3C/Runtime/Tech3CEnums.cs.meta b/Assets/Tech3C/Runtime/Tech3CEnums.cs.meta new file mode 100644 index 0000000000..0b58d42a40 --- /dev/null +++ b/Assets/Tech3C/Runtime/Tech3CEnums.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c35f3ea36736dfe439eb6f4b8a314dde +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tech3C/Runtime/Tech3CSDK.cs b/Assets/Tech3C/Runtime/Tech3CSDK.cs new file mode 100644 index 0000000000..dd9111009a --- /dev/null +++ b/Assets/Tech3C/Runtime/Tech3CSDK.cs @@ -0,0 +1,473 @@ +using UnityEngine; +using System; + +namespace Tech3C +{ + /// + /// Main API wrapper for Tech3C SDK + /// This is the main entry point for using the SDK in Unity + /// + public class Tech3CSDK : MonoBehaviour + { + private static Tech3CSDK instance; + private ITech3CNativeBridge nativeBridge; + private Tech3CConfig config; + private bool isInitialized = false; + + #region Singleton + + /// + /// Get the singleton instance of Tech3CSDK + /// + public static Tech3CSDK Instance + { + get + { + if (instance == null) + { + GameObject go = new GameObject("Tech3CSDK"); + instance = go.AddComponent(); + DontDestroyOnLoad(go); + } + return instance; + } + } + + private void Awake() + { + if (instance == null) + { + instance = this; + DontDestroyOnLoad(gameObject); + } + else if (instance != this) + { + Destroy(gameObject); + } + } + + #endregion + + #region Initialization + + /// + /// Initialize the Tech3C SDK with configuration + /// + /// The configuration object + 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"); + } + } + + /// + /// Initialize the Tech3C SDK with client credentials + /// + /// The client ID + /// The client secret + 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 + + /// + /// Show authentication screen (login/register/forgot password) + /// + /// The callback for authentication result + 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); + } + + /// + /// Show authentication screen without callback + /// + public void ShowAuth() + { + ShowAuth(null); + } + + #endregion + + #region User Management + + /// + /// Logout current user + /// + /// The callback for logout result + 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); + } + + /// + /// Logout current user without callback + /// + public void Logout() + { + Logout(null); + } + + /// + /// Get user information + /// + /// The callback for user info result + 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); + } + + /// + /// Get user information without callback + /// + public void GetUserInfo() + { + GetUserInfo(null); + } + + #endregion + + #region State Checkers + + /// + /// Check if user is logged in + /// + public bool IsLoggedIn() + { + if (!isInitialized) + { + Debug.LogWarning("[Tech3C] SDK not initialized"); + return false; + } + + return nativeBridge?.IsLoggedIn() ?? false; + } + + /// + /// Check if access token is expired + /// + public bool IsTokenExpired() + { + if (!isInitialized) + { + Debug.LogWarning("[Tech3C] SDK not initialized"); + return true; + } + + return nativeBridge?.IsTokenExpired() ?? true; + } + + #endregion + + #region Token Management + + /// + /// Get access token + /// + public string GetAccessToken() + { + if (!isInitialized) + { + Debug.LogWarning("[Tech3C] SDK not initialized"); + return null; + } + + return nativeBridge?.GetAccessToken(); + } + + /// + /// Get refresh token + /// + public string GetRefreshToken() + { + if (!isInitialized) + { + Debug.LogWarning("[Tech3C] SDK not initialized"); + return null; + } + + return nativeBridge?.GetRefreshToken(); + } + + #endregion + + #region User Information + + /// + /// Get user ID + /// + public string GetUserId() + { + if (!isInitialized) + { + Debug.LogWarning("[Tech3C] SDK not initialized"); + return null; + } + + return nativeBridge?.GetUserId(); + } + + /// + /// Get device ID + /// + public string GetDeviceId() + { + if (!isInitialized) + { + Debug.LogWarning("[Tech3C] SDK not initialized"); + return null; + } + + return nativeBridge?.GetDeviceId(); + } + + /// + /// Get login timestamp + /// + public long GetLoginTime() + { + if (!isInitialized) + { + Debug.LogWarning("[Tech3C] SDK not initialized"); + return 0; + } + + return nativeBridge?.GetLoginTime() ?? 0; + } + + /// + /// Get token expiry timestamp + /// + public long GetTokenExpiry() + { + if (!isInitialized) + { + Debug.LogWarning("[Tech3C] SDK not initialized"); + return 0; + } + + return nativeBridge?.GetTokenExpiry() ?? 0; + } + + #endregion + + #region Configuration + + /// + /// Set display language + /// + /// The language to set + public void SetLanguage(Language language) + { + if (!isInitialized) + { + Debug.LogWarning("[Tech3C] SDK not initialized"); + return; + } + + nativeBridge?.SetLanguage(language); + } + + /// + /// Set debug mode + /// + /// Enable or disable debug mode + public void SetDebug(bool debug) + { + if (!isInitialized) + { + Debug.LogWarning("[Tech3C] SDK not initialized"); + return; + } + + nativeBridge?.SetDebug(debug); + } + + #endregion + + #region Cleanup + + /// + /// Cleanup SDK resources + /// + public void Cleanup() + { + nativeBridge?.Cleanup(); + nativeBridge = null; + config = null; + isInitialized = false; + Debug.Log("[Tech3C] Cleanup completed"); + } + + private void OnDestroy() + { + Cleanup(); + } + + #endregion + + #region Properties + + /// + /// Get the current configuration + /// + public Tech3CConfig Config => config; + + /// + /// Check if SDK is initialized + /// + public bool IsInitialized => isInitialized; + + #endregion + } + + /// + /// Mock bridge for testing in editor or unsupported platforms + /// + 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"); + } + } +} diff --git a/Assets/Tech3C/Runtime/Tech3CSDK.cs.meta b/Assets/Tech3C/Runtime/Tech3CSDK.cs.meta new file mode 100644 index 0000000000..72d6fb5d15 --- /dev/null +++ b/Assets/Tech3C/Runtime/Tech3CSDK.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3afb6189c673cea45a34e6e75c1f5ccb +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tech3C/Runtime/Tech3CSDKBridge.cs b/Assets/Tech3C/Runtime/Tech3CSDKBridge.cs new file mode 100644 index 0000000000..4c0b34086e --- /dev/null +++ b/Assets/Tech3C/Runtime/Tech3CSDKBridge.cs @@ -0,0 +1,332 @@ +using UnityEngine; +using System; + +namespace Tech3C +{ + /// + /// Bridge component to receive callbacks from native Android/iOS SDK via Unity messaging + /// This GameObject receives UnitySendMessage calls from native code + /// + 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(); + 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 + + /// + /// Set the auth callback to be invoked when auth events occur + /// + public static void SetAuthCallback(IAuthCallback callback) + { + authCallback = callback; + Debug.Log("[Tech3C Bridge] Auth callback set"); + } + + /// + /// Set the logout callback to be invoked when logout events occur + /// + public static void SetLogoutCallback(ILogoutCallback callback) + { + logoutCallback = callback; + Debug.Log("[Tech3C Bridge] Logout callback set"); + } + + /// + /// Set the user info callback to be invoked when user info events occur + /// + public static void SetUserInfoCallback(IUserInfoCallback callback) + { + userInfoCallback = callback; + Debug.Log("[Tech3C Bridge] User info callback set"); + } + + #endregion + + #region Auth Callbacks + + /// + /// Called from native code when authentication is successful + /// Message format: "userId|password|accessToken|refreshToken|loginType|expiryTime" + /// + 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}"); + } + } + + /// + /// Called from native code when authentication is cancelled + /// + 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(); + } + + /// + /// Called from native code when an authentication error occurs + /// Message format: "errorCode|errorMessage" + /// + 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 + + /// + /// Called from native code when logout is successful + /// + 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(); + } + + /// + /// Called from native code when a logout error occurs + /// Message format: "errorCode|errorMessage" + /// + 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 + + /// + /// Called from native code when user info is received + /// Message format: "userId|username|email|phone" + /// + 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}"); + } + } + + /// + /// Called from native code when user info retrieval is cancelled + /// + 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(); + } + + /// + /// Called from native code when a user info error occurs + /// Message format: "errorCode|errorMessage" + /// + 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 + } +} diff --git a/Assets/Tech3C/Runtime/Tech3CSDKBridge.cs.meta b/Assets/Tech3C/Runtime/Tech3CSDKBridge.cs.meta new file mode 100644 index 0000000000..353dcb291b --- /dev/null +++ b/Assets/Tech3C/Runtime/Tech3CSDKBridge.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 46cf26ff28457b04890e322b5b161a67 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tech3C/Runtime/Tech3CiOSBridge.cs b/Assets/Tech3C/Runtime/Tech3CiOSBridge.cs new file mode 100644 index 0000000000..2aa3a12b95 --- /dev/null +++ b/Assets/Tech3C/Runtime/Tech3CiOSBridge.cs @@ -0,0 +1,147 @@ +using UnityEngine; +using System; + +namespace Tech3C +{ + /// + /// Bridge class for iOS platform to communicate with Tech3C native SDK + /// Note: iOS implementation requires native framework. This is a placeholder. + /// + public class Tech3CiOSBridge : ITech3CNativeBridge + { + private bool initialized = false; + + /// + /// Initialize the Tech3C SDK + /// + public void Initialize(string clientId, string clientSecret, Tech3CConfig config) + { + Debug.LogWarning("[Tech3C] iOS platform is not yet supported. Please provide the native iOS framework."); + initialized = true; + } + + /// + /// Show authentication screen + /// + public void ShowAuth(IAuthCallback callback) + { + Debug.LogWarning("[Tech3C] ShowAuth is not implemented for iOS platform"); + callback?.OnAuthError(-1, "iOS platform not supported"); + } + + /// + /// Logout current user + /// + public void Logout(ILogoutCallback callback) + { + Debug.LogWarning("[Tech3C] Logout is not implemented for iOS platform"); + callback?.OnLogoutError(-1, "iOS platform not supported"); + } + + /// + /// Get user information + /// + public void GetUserInfo(IUserInfoCallback callback) + { + Debug.LogWarning("[Tech3C] GetUserInfo is not implemented for iOS platform"); + callback?.OnUserInfoError(-1, "iOS platform not supported"); + } + + /// + /// Check if user is logged in + /// + public bool IsLoggedIn() + { + Debug.LogWarning("[Tech3C] IsLoggedIn is not implemented for iOS platform"); + return false; + } + + /// + /// Get access token + /// + public string GetAccessToken() + { + Debug.LogWarning("[Tech3C] GetAccessToken is not implemented for iOS platform"); + return null; + } + + /// + /// Get refresh token + /// + public string GetRefreshToken() + { + Debug.LogWarning("[Tech3C] GetRefreshToken is not implemented for iOS platform"); + return null; + } + + /// + /// Get user ID + /// + public string GetUserId() + { + Debug.LogWarning("[Tech3C] GetUserId is not implemented for iOS platform"); + return null; + } + + /// + /// Get device ID + /// + public string GetDeviceId() + { + Debug.LogWarning("[Tech3C] GetDeviceId is not implemented for iOS platform"); + return null; + } + + /// + /// Get login time + /// + public long GetLoginTime() + { + Debug.LogWarning("[Tech3C] GetLoginTime is not implemented for iOS platform"); + return 0; + } + + /// + /// Get token expiry time + /// + public long GetTokenExpiry() + { + Debug.LogWarning("[Tech3C] GetTokenExpiry is not implemented for iOS platform"); + return 0; + } + + /// + /// Check if token is expired + /// + public bool IsTokenExpired() + { + Debug.LogWarning("[Tech3C] IsTokenExpired is not implemented for iOS platform"); + return false; + } + + /// + /// Set language + /// + public void SetLanguage(Language language) + { + Debug.LogWarning("[Tech3C] SetLanguage is not implemented for iOS platform"); + } + + /// + /// Set debug mode + /// + public void SetDebug(bool debug) + { + Debug.LogWarning("[Tech3C] SetDebug is not implemented for iOS platform"); + } + + /// + /// Cleanup resources + /// + public void Cleanup() + { + Debug.Log("[Tech3C] iOS bridge cleanup completed"); + initialized = false; + } + } +} diff --git a/Assets/Tech3C/Runtime/Tech3CiOSBridge.cs.meta b/Assets/Tech3C/Runtime/Tech3CiOSBridge.cs.meta new file mode 100644 index 0000000000..327d3972f3 --- /dev/null +++ b/Assets/Tech3C/Runtime/Tech3CiOSBridge.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7199bd7af3b75294c84f9d6451df86d4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tech3C/Samples.meta b/Assets/Tech3C/Samples.meta new file mode 100644 index 0000000000..a7dd7ed56a --- /dev/null +++ b/Assets/Tech3C/Samples.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: a947977eda82ae34b85413ea17ef265a +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Tech3C/Samples/Tech3CSimpleDemo.cs b/Assets/Tech3C/Samples/Tech3CSimpleDemo.cs new file mode 100644 index 0000000000..0c609186bb --- /dev/null +++ b/Assets/Tech3C/Samples/Tech3CSimpleDemo.cs @@ -0,0 +1,473 @@ +using UnityEngine; +using UnityEngine.UI; +using UnityEngine.EventSystems; +using System; + +namespace Tech3C +{ + /// + /// Simple demo that creates UI programmatically + /// Just attach this script to any GameObject in your scene + /// + public class Tech3CSimpleDemo : MonoBehaviour + { + [Header("Configuration")] + public string clientId = "your_client_id"; + public string clientSecret = "your_client_secret"; + + private Canvas canvas; + private Text logText; + private AuthCallback authCallback; + private LogoutCallback logoutCallback; + + private GameObject dialogPanel; + private Text dialogText; + private Button dialogCloseButton; + + private void Start() + { + EnsureEventSystem(); + CreateUI(); + SetupCallbacks(); + + // Auto-initialize SDK + if (!string.IsNullOrEmpty(clientId) && !string.IsNullOrEmpty(clientSecret)) + { + Log($"Initializing Tech3C SDK...\n Client ID: {clientId}"); + Tech3CSDK.Instance.Initialize(clientId, clientSecret); + + if (Tech3CSDK.Instance.IsInitialized) + { + Log("✓ SDK Initialized Successfully!"); + } + else + { + Log("✗ Failed to initialize SDK"); + } + } + else + { + Log("⚠ Client ID and/or Client Secret not set. Please configure in Inspector."); + } + + Debug.Log("[Tech3C Demo] Tech3C SDK Simple Demo Started"); + } + + private void EnsureEventSystem() + { + if (EventSystem.current == null) + { + GameObject eventSystemGO = new GameObject("EventSystem"); + eventSystemGO.AddComponent(); + eventSystemGO.AddComponent(); + Debug.Log("[Tech3C Demo] EventSystem created"); + } + } + + private void CreateUI() + { + // Create Canvas + GameObject canvasGO = new GameObject("Tech3CDemoCanvas"); + canvasGO.transform.SetParent(transform); + canvas = canvasGO.AddComponent(); + canvas.renderMode = RenderMode.ScreenSpaceOverlay; + + // Configure CanvasScaler for responsive 16:9 ratio + CanvasScaler canvasScaler = canvasGO.AddComponent(); + canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; + canvasScaler.referenceResolution = new Vector2(1920, 1080); // 16:9 ratio + canvasScaler.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight; + canvasScaler.matchWidthOrHeight = 0.5f; // Balance between width and height + + canvasGO.AddComponent(); + + // Create Scroll Panel + GameObject scrollPanel = new GameObject("ScrollPanel"); + scrollPanel.transform.SetParent(canvasGO.transform, false); + RectTransform scrollPanelRect = scrollPanel.AddComponent(); + scrollPanelRect.anchorMin = Vector2.zero; + scrollPanelRect.anchorMax = Vector2.one; + scrollPanelRect.sizeDelta = Vector2.zero; + Image scrollPanelImage = scrollPanel.AddComponent(); + scrollPanelImage.color = new Color(0.2f, 0.2f, 0.2f, 1f); + + // Create ScrollRect + ScrollRect scrollRect = scrollPanel.AddComponent(); + scrollRect.horizontal = false; + scrollRect.vertical = true; + scrollRect.scrollSensitivity = 50f; + + // Create Content Panel + GameObject panel = new GameObject("Content"); + panel.transform.SetParent(scrollPanel.transform, false); + RectTransform panelRect = panel.AddComponent(); + panelRect.anchorMin = new Vector2(0, 1); + panelRect.anchorMax = new Vector2(1, 1); + panelRect.pivot = new Vector2(0.5f, 1f); + panelRect.sizeDelta = new Vector2(0, 0); + scrollRect.content = panelRect; + + // Create Vertical Layout Group + VerticalLayoutGroup layoutGroup = panel.AddComponent(); + layoutGroup.childControlHeight = false; + layoutGroup.childControlWidth = true; + layoutGroup.childForceExpandWidth = true; + layoutGroup.childForceExpandHeight = false; + layoutGroup.spacing = 15; + layoutGroup.padding = new RectOffset(40, 40, 40, 40); + ContentSizeFitter contentSizeFitter = panel.AddComponent(); + contentSizeFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize; + + // Create Title + CreateTextResponsive(panel.transform, "Tech3C SDK Demo", 28, TextAnchor.MiddleCenter, 60); + + // Create Separator + CreateTextResponsive(panel.transform, "--- Authentication ---", 18, TextAnchor.MiddleCenter, 30); + + // Create Show Auth Button + Button showAuthButton = CreateButtonResponsive(panel.transform, "Show Auth Screen", 70); + showAuthButton.onClick.AddListener(OnShowAuthClick); + + // Create Logout Button + Button logoutButton = CreateButtonResponsive(panel.transform, "Logout", 70); + logoutButton.onClick.AddListener(OnLogoutClick); + + // Create Log Text + CreateTextResponsive(panel.transform, "Logs:", 16, TextAnchor.MiddleLeft, 35); + logText = CreateTextResponsive(panel.transform, "Ready...", 16, TextAnchor.MiddleLeft, 400); + logText.color = Color.green; + + // Reposition panel + panelRect.anchoredPosition = Vector2.zero; + } + + private void SetupCallbacks() + { + // Auth callback + authCallback = new AuthCallback(); + authCallback.OnAuthSuccessEvent += (userId, password, accessToken, refreshToken, loginType, expiryTime) => + { + Debug.Log($"[Tech3C Demo] === AUTH SUCCESS CALLBACK FIRED ==="); + Debug.Log($"[Tech3C Demo] UserId: {userId}"); + Debug.Log($"[Tech3C Demo] Password: {password?.Substring(0, Math.Min(3, password?.Length ?? 0))}..."); + Debug.Log($"[Tech3C Demo] LoginType: {loginType}"); + Debug.Log($"[Tech3C Demo] AccessToken: {accessToken?.Substring(0, Math.Min(20, accessToken?.Length ?? 0))}..."); + Debug.Log($"[Tech3C Demo] RefreshToken: {refreshToken?.Substring(0, Math.Min(20, refreshToken?.Length ?? 0))}..."); + Debug.Log($"[Tech3C Demo] ExpiryTime: {expiryTime}"); + + Log($"✓ Auth Success!\n User ID: {userId}\n Password: {password}\n Login Type: {loginType}\n Token: {accessToken?.Substring(0, Math.Min(20, accessToken?.Length ?? 0))}..."); + + ShowAuthSuccessDialog(userId, password, loginType.ToString()); + }; + authCallback.OnAuthCancelledEvent += () => + { + Debug.Log($"[Tech3C Demo] === AUTH CANCELLED CALLBACK FIRED ==="); + Log("✗ Auth Cancelled by user"); + }; + authCallback.OnAuthErrorEvent += (errorCode, errorMessage) => + { + Debug.Log($"[Tech3C Demo] === AUTH ERROR CALLBACK FIRED ==="); + Debug.Log($"[Tech3C Demo] ErrorCode: {errorCode}"); + Debug.Log($"[Tech3C Demo] ErrorMessage: {errorMessage}"); + Log($"✗ Auth Error [{errorCode}]: {errorMessage}"); + }; + + // Logout callback + logoutCallback = new LogoutCallback(); + logoutCallback.OnLogoutSuccessEvent += () => + { + Debug.Log($"[Tech3C Demo] === LOGOUT SUCCESS CALLBACK FIRED ==="); + Log("✓ Logout Successful"); + }; + logoutCallback.OnLogoutErrorEvent += (errorCode, errorMessage) => + { + Debug.Log($"[Tech3C Demo] === LOGOUT ERROR CALLBACK FIRED ==="); + Debug.Log($"[Tech3C Demo] ErrorCode: {errorCode}"); + Debug.Log($"[Tech3C Demo] ErrorMessage: {errorMessage}"); + Log($"✗ Logout Error [{errorCode}]: {errorMessage}"); + }; + } + + #region Button Handlers + + private void OnInitializeClick(string newClientId, string newClientSecret) + { + if (string.IsNullOrEmpty(newClientId) || string.IsNullOrEmpty(newClientSecret)) + { + Log("✗ Client ID and Client Secret are required!"); + return; + } + + clientId = newClientId; + clientSecret = newClientSecret; + + Log($"Initializing Tech3C SDK...\n Client ID: {clientId}"); + Tech3CSDK.Instance.Initialize(clientId, clientSecret); + + if (Tech3CSDK.Instance.IsInitialized) + { + Log("✓ SDK Initialized Successfully!"); + } + else + { + Log("✗ Failed to initialize SDK"); + } + } + + private void OnShowAuthClick() + { + if (!Tech3CSDK.Instance.IsInitialized) + { + Log("✗ SDK not initialized. Click Initialize first!"); + return; + } + + Log("Opening Auth Screen..."); + Tech3CSDK.Instance.ShowAuth(authCallback); + } + + private void OnLogoutClick() + { + if (!Tech3CSDK.Instance.IsInitialized) + { + Log("✗ SDK not initialized."); + return; + } + + Log("Logging out..."); + Tech3CSDK.Instance.Logout(logoutCallback); + } + + #endregion + + #region UI Helper Methods + + private Button CreateButton(Transform parent, string text, float width, float height) + { + GameObject buttonGO = new GameObject(text.Replace(" ", "_")); + buttonGO.transform.SetParent(parent, false); + + RectTransform rect = buttonGO.AddComponent(); + rect.sizeDelta = new Vector2(width, height); + + Image image = buttonGO.AddComponent(); + image.color = new Color(0.3f, 0.5f, 0.8f, 1f); + + Button button = buttonGO.AddComponent