180 lines
5.0 KiB
C#
180 lines
5.0 KiB
C#
using System.Text.RegularExpressions;
|
||
|
||
namespace Guru
|
||
{
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using Firebase;
|
||
using Firebase.Analytics;
|
||
using Firebase.Extensions;
|
||
using Firebase.RemoteConfig;
|
||
using Random = UnityEngine.Random;
|
||
using UnityEngine;
|
||
|
||
public static partial class FirebaseUtil
|
||
{
|
||
private static readonly string LOG_TAG = "Firebase";
|
||
private static bool _isDebug = false;
|
||
private static bool _isReady = false;
|
||
public static bool IsReady => _isReady && IsFirebaseInitialized;
|
||
|
||
public static DependencyStatus DependencyStatus = DependencyStatus.UnavailableOther;
|
||
public static bool IsFirebaseInitialized => DependencyStatus == DependencyStatus.Available;
|
||
public static Action<bool> onInitComplete;
|
||
|
||
public static Action<bool> OnFirebaseAuthResult;
|
||
public static Action<bool> OnUserAuthResult;
|
||
|
||
|
||
/// <summary>
|
||
/// 初始化 Firebase 组件
|
||
/// </summary>
|
||
/// <param name="callback"></param>
|
||
/// <param name="isDebug"></param>
|
||
public static void InitFirebase(Action callback, bool isDebug = false)
|
||
{
|
||
_isReady = false;
|
||
_isDebug = isDebug;
|
||
|
||
// 初始化 Fireabse 依赖
|
||
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => {
|
||
DependencyStatus = task.Result;
|
||
if (DependencyStatus == DependencyStatus.Available)
|
||
{
|
||
_isReady = true;
|
||
InitializeFirebaseComp();
|
||
callback?.Invoke();
|
||
}
|
||
else
|
||
{
|
||
Log.E(LOG_TAG, "Could not resolve all Firebase dependencies: " + DependencyStatus);
|
||
}
|
||
onInitComplete?.Invoke(_isReady);
|
||
});
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 初始化 Firebase 组件
|
||
/// </summary>
|
||
private static void InitializeFirebaseComp()
|
||
{
|
||
InitCrashlytics(); // 老项目沿用此逻辑
|
||
InitRemoteConfig(); // 老项目沿用此逻辑
|
||
InitAssetByFirebaseIdAsync(); // 获取到 FirebaseID 后异步执行逻辑
|
||
|
||
if (IPMConfig.IPM_UID.IsNullOrEmpty())
|
||
{
|
||
Log.I(LOG_TAG, "没有存储UID时,从中台获取匿名认证授权");
|
||
//没有存储UID时,从中台获取匿名认证授权
|
||
new AuthUserRequest()
|
||
.SetRetryTimes(-1)
|
||
.SetSuccessCallBack(() =>
|
||
{
|
||
OnUserAuthResult?.Invoke(true);
|
||
AuthUser(InitializeMessage);
|
||
}).SetFailCallBack(() =>
|
||
{
|
||
OnUserAuthResult?.Invoke(false);
|
||
})
|
||
.Send();
|
||
}
|
||
else
|
||
{
|
||
InitializeMessage();
|
||
int currentTimeStamp = TimeUtil.GetCurrentTimeStampSecond();
|
||
if(currentTimeStamp - IPMConfig.IPM_TOKEN_TIME >= IPMConfig.TOKEN_VALID_TIME)
|
||
{
|
||
//中台Token失效,从中台重新获取Token
|
||
new RefreshTokenRequest().SetRetryTimes(-1).Send();
|
||
}
|
||
|
||
if(currentTimeStamp - IPMConfig.IPM_FIREBASE_TOKEN_TIME >= IPMConfig.FIREBASE_TOKEN_VALID_TIME)
|
||
{
|
||
//中台firebaseToken失效,从中台重新获取firebaseToken
|
||
new RefreshFirebaseTokenRequest()
|
||
.SetRetryTimes(-1)
|
||
.SetSuccessCallBack(()=> AuthUser(InitializeMessage))
|
||
.Send();
|
||
}
|
||
else
|
||
{
|
||
AuthUser(InitializeMessage);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
private static void InitAssetByFirebaseIdAsync()
|
||
{
|
||
|
||
Debug.Log($"[SDK] --- InitAssetByFirebaseIdAsync");
|
||
|
||
// 异步获取 FirebaseID
|
||
FirebaseAnalytics.GetAnalyticsInstanceIdAsync()
|
||
.ContinueWithOnMainThread(task =>
|
||
{
|
||
string fid = task.Result;
|
||
if (task.IsCompleted && !string.IsNullOrEmpty(fid))
|
||
{
|
||
// 保存本地ID备份
|
||
IPMConfig.FIREBASE_ID = fid; // 保存FirebaseID
|
||
Debug.Log($"[SDK] --- Get FirebaseID: {fid}");
|
||
GuruAnalytics.SetFirebaseId(fid);
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError($"[SDK] --- Fetch FirebaseID failed on start!");
|
||
}
|
||
|
||
//--- 结束后启动相关的服务 ---
|
||
InitAdjustService(); // 启动 AdjustService
|
||
InitAnalytics(); // 初始化打点逻辑和实现
|
||
});
|
||
}
|
||
|
||
|
||
#region 启动 Adjust 服务
|
||
|
||
/// <summary>
|
||
/// 启动 Adjust 服务
|
||
/// </summary>
|
||
private static void InitAdjustService()
|
||
{
|
||
// 启动 AdjustService
|
||
string appToken = GuruSettings.Instance.AdjustSetting?.GetAppToken() ?? "";
|
||
string fbAppId = GuruSettings.Instance.IPMSetting.FacebookAppId;
|
||
AdjustService.StartService(appToken, fbAppId);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region Android 自打点特殊启动方式
|
||
|
||
// Firebase 初始化才能调用此方法
|
||
private static void InitAnalytics()
|
||
{
|
||
#if UNITY_ANDROID
|
||
// Android 平台开始
|
||
var fid = IPMConfig.FIREBASE_ID;// 获取 FirebaseID
|
||
var enableErrorLog = true; // 开启日志上报
|
||
|
||
// 随机抽取本地分组获取数据
|
||
GuruAnalyticsExp.GetGuruAnalyticsExpParams(out string groupId, out string baseUrl, out string[] uploadIpAddress, out bool isEnable);
|
||
|
||
// 初始化打点和自打点
|
||
Analytics.InitAnalytics(baseUrl, uploadIpAddress, isEnable, enableErrorLog, fid); // 打点提前初始化
|
||
Analytics.SetUserProperty(GuruAnalyticsExp.KEY_GURU_ANALYTICS_EXP, groupId); // 上报分组数据
|
||
#else
|
||
|
||
// iOS 不参与分组
|
||
Analytics.InitAnalytics();
|
||
Analytics.SetUserProperty(GuruAnalyticsExp.KEY_GURU_ANALYTICS_EXP, "none"); // 上报分组数据
|
||
#endif
|
||
|
||
}
|
||
|
||
#endregion
|
||
|
||
}
|
||
} |