using System; using System.Runtime.InteropServices; using UnityEngine; public static class AkinorevSDK { public const string Tag = "[AKINOREV SDK]"; public const int DeviceUnknown = 0; public const int DeviceMobile = 1; public const int DeviceDesktop = 2; [Serializable] public class Account { public string id = ""; public string nickname = ""; public string avatar = ""; } [Serializable] public class Session { public string url = ""; public string key = ""; public string room = ""; } #if UNITY_WEBGL && !UNITY_EDITOR [DllImport("__Internal")] private static extern void AkinorevSaveSet(string key, string value); [DllImport("__Internal")] private static extern IntPtr AkinorevSaveGet(string key); [DllImport("__Internal")] private static extern void AkinorevSaveRemove(string key); [DllImport("__Internal")] private static extern void AkinorevSaveClear(); [DllImport("__Internal")] private static extern void AkinorevFree(IntPtr pointer); [DllImport("__Internal")] private static extern int AkinorevGetDevice(); [DllImport("__Internal")] private static extern IntPtr AkinorevGetLocale(); [DllImport("__Internal")] private static extern void AkinorevSetRoom(string room); [DllImport("__Internal")] private static extern void AkinorevClearRoom(); [DllImport("__Internal")] private static extern void AkinorevGetAccount(string target); [DllImport("__Internal")] private static extern void AkinorevGetSession(string target); #endif public static bool IsAvailable { get { try { #if UNITY_WEBGL && !UNITY_EDITOR return true; #else return false; #endif } catch (Exception error) { Debug.LogError($"{Tag} IsAvailable failed: {error}"); return false; } } } public static void SaveSet(string key, string value) { try { if (string.IsNullOrEmpty(key)) throw new ArgumentException("save key is required"); #if UNITY_WEBGL && !UNITY_EDITOR AkinorevSaveSet(key, value ?? string.Empty); #else Debug.LogWarning($"{Tag} SaveSet works only in WebGL builds"); #endif } catch (Exception error) { Debug.LogError($"{Tag} SaveSet('{key}') failed: {error}"); } } public static string SaveGet(string key) { try { if (string.IsNullOrEmpty(key)) throw new ArgumentException("save key is required"); #if UNITY_WEBGL && !UNITY_EDITOR return ReadString(AkinorevSaveGet(key)); #else Debug.LogWarning($"{Tag} SaveGet works only in WebGL builds"); return null; #endif } catch (Exception error) { Debug.LogError($"{Tag} SaveGet('{key}') failed: {error}"); return null; } } public static void SaveRemove(string key) { try { if (string.IsNullOrEmpty(key)) throw new ArgumentException("save key is required"); #if UNITY_WEBGL && !UNITY_EDITOR AkinorevSaveRemove(key); #else Debug.LogWarning($"{Tag} SaveRemove works only in WebGL builds"); #endif } catch (Exception error) { Debug.LogError($"{Tag} SaveRemove('{key}') failed: {error}"); } } public static void SaveClear() { try { #if UNITY_WEBGL && !UNITY_EDITOR AkinorevSaveClear(); #else Debug.LogWarning($"{Tag} SaveClear works only in WebGL builds"); #endif } catch (Exception error) { Debug.LogError($"{Tag} SaveClear failed: {error}"); } } public static int Device() { try { #if UNITY_WEBGL && !UNITY_EDITOR return AkinorevGetDevice(); #else return DeviceUnknown; #endif } catch (Exception error) { Debug.LogError($"{Tag} Device failed: {error}"); return DeviceUnknown; } } public static bool IsMobile() { return Device() == DeviceMobile; } public static string Locale() { try { #if UNITY_WEBGL && !UNITY_EDITOR return ReadString(AkinorevGetLocale()) ?? ""; #else return ""; #endif } catch (Exception error) { Debug.LogError($"{Tag} Locale failed: {error}"); return ""; } } public static void SetRoom(string room) { try { if (string.IsNullOrEmpty(room)) throw new ArgumentException("room code is required"); #if UNITY_WEBGL && !UNITY_EDITOR AkinorevSetRoom(room); #else Debug.LogWarning($"{Tag} SetRoom works only in WebGL builds"); #endif } catch (Exception error) { Debug.LogError($"{Tag} SetRoom('{room}') failed: {error}"); } } public static void ClearRoom() { try { #if UNITY_WEBGL && !UNITY_EDITOR AkinorevClearRoom(); #else Debug.LogWarning($"{Tag} ClearRoom works only in WebGL builds"); #endif } catch (Exception error) { Debug.LogError($"{Tag} ClearRoom failed: {error}"); } } public static void GetAccount(Action onResult) { try { #if UNITY_WEBGL && !UNITY_EDITOR AkinorevSDKBridge.AccountHandler = json => DeliverAccount(json, onResult); AkinorevGetAccount(AkinorevSDKBridge.Ensure()); #else Debug.LogWarning($"{Tag} GetAccount works only in WebGL builds"); onResult?.Invoke(null); #endif } catch (Exception error) { Debug.LogError($"{Tag} GetAccount failed: {error}"); onResult?.Invoke(null); } } public static void GetSession(Action onResult) { try { #if UNITY_WEBGL && !UNITY_EDITOR AkinorevSDKBridge.SessionHandler = json => DeliverSession(json, onResult); AkinorevGetSession(AkinorevSDKBridge.Ensure()); #else Debug.LogWarning($"{Tag} GetSession works only in WebGL builds"); onResult?.Invoke(null); #endif } catch (Exception error) { Debug.LogError($"{Tag} GetSession failed: {error}"); onResult?.Invoke(null); } } private static void DeliverAccount(string json, Action onResult) { try { Account account = string.IsNullOrEmpty(json) ? null : JsonUtility.FromJson(json); onResult?.Invoke(account); } catch (Exception error) { Debug.LogError($"{Tag} account parsing failed: {error}"); onResult?.Invoke(null); } } private static void DeliverSession(string json, Action onResult) { try { Session session = string.IsNullOrEmpty(json) ? null : JsonUtility.FromJson(json); onResult?.Invoke(session); } catch (Exception error) { Debug.LogError($"{Tag} session parsing failed: {error}"); onResult?.Invoke(null); } } private static string ReadString(IntPtr pointer) { if (pointer == IntPtr.Zero) return null; try { return Marshal.PtrToStringUTF8(pointer); } finally { #if UNITY_WEBGL && !UNITY_EDITOR try { AkinorevFree(pointer); } catch (Exception error) { Debug.LogError($"{Tag} memory release failed: {error}"); } #endif } } } public sealed class AkinorevSDKBridge : MonoBehaviour { private static AkinorevSDKBridge instance; internal static Action AccountHandler; internal static Action SessionHandler; internal static string Ensure() { try { if (instance != null && instance.gameObject != null) { return instance.gameObject.name; } GameObject host = new GameObject("AkinorevSDKBridge"); DontDestroyOnLoad(host); instance = host.AddComponent(); return host.name; } catch (Exception error) { Debug.LogError($"{AkinorevSDK.Tag} bridge creation failed: {error}"); return ""; } } public void OnAkinorevAccount(string json) { try { AccountHandler?.Invoke(json); } catch (Exception error) { Debug.LogError($"{AkinorevSDK.Tag} account callback failed: {error}"); } } public void OnAkinorevSession(string json) { try { SessionHandler?.Invoke(json); } catch (Exception error) { Debug.LogError($"{AkinorevSDK.Tag} session callback failed: {error}"); } } }