using System; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace WZ { public class TransferManager : D_MonoSingleton { private AdInitState currentState = AdInitState.Idle; private bool isFirebaseActivated = false; private Coroutine waitingCoroutine = null; private int admobRetryCount = 0; private const int MAX_ADMOB_RETRY_COUNT = 5; private const float ADMOB_RETRY_INTERVAL = 30f; private Coroutine admobRetryCoroutine = null; private bool isRetryingAdMob = false; private bool _afterLoading = false; private int _level = 0; // 状态枚举 public enum AdInitState { Idle, // 空闲 WaitingForAdjustCallback, // 等待Adjust回调 WaitingForFirebase, // 等待Firebase激活 CheckingPromotion, // 检查推广状态 InitializingTransfer, // 初始化Transfer SDK CheckingVpnPermission, // 检查VPN权限 RequestingVpnPermission, // 请求VPN权限 CheckingAdMobAvailability, // 检查AdMob可用性 InitializingAdMob, // 初始化AdMob InitializingTopOn, // 初始化TopOn Completed, // 完成 Failed, // 失败 RetryingAdMobCheck } public void StartAdInitialization(bool afterLoading,int level) { Debug.Log("收到Adjust回调,开始处理广告初始化"); _afterLoading = afterLoading; _level = level; // 开始广告初始化流程 StartAdInitialization(); } private void StartAdInitialization() { if (currentState != AdInitState.WaitingForAdjustCallback) { Debug.LogWarning($"广告初始化状态不正确,当前状态: {currentState}"); return; } // 检查Firebase状态 if (isFirebaseActivated) { Debug.Log("Firebase已激活,直接执行广告初始化"); ExecuteAdInitialization(); } else { Debug.Log("Firebase未激活,等待激活后再执行广告初始化"); ChangeState(AdInitState.WaitingForFirebase, "等待Firebase激活"); // 开始等待Firebase激活 waitingCoroutine = StartCoroutine(WaitForFirebaseAndInitialize()); } } /// /// 等待Firebase激活并初始化广告 /// private IEnumerator WaitForFirebaseAndInitialize() { Debug.Log("开始等待Firebase激活..."); float startTime = Time.time; const float timeout = 360f; // 等待Firebase激活 while (!isFirebaseActivated && Time.time - startTime < timeout) { yield return new WaitForSeconds(0.5f); // 检查Firebase状态(可能是外部初始化的) CheckFirebaseStatus(); if (Time.time - startTime >= 5f && !isFirebaseActivated) { Debug.Log($"已等待{Time.time - startTime:F1}秒,Firebase仍未激活"); } } if (isFirebaseActivated) { Debug.Log($"Firebase激活成功,等待时间: {Time.time - startTime:F1}秒"); ExecuteAdInitialization(); } else { Debug.LogWarning($"Firebase激活等待超时({timeout}秒),继续初始化广告"); ExecuteAdInitialization(); } waitingCoroutine = null; } /// /// 执行广告初始化流程 /// private void ExecuteAdInitialization() { Debug.Log("开始执行广告初始化流程"); StartCoroutine(AdInitializationCoroutine()); } /// /// 广告初始化协程 /// private IEnumerator AdInitializationCoroutine() { // 状态1: 检查是否为推广用户 ChangeState(AdInitState.CheckingPromotion, "检查推广用户状态"); if (!IsPromoteUser()) { Debug.Log("非推广用户,直接初始化TopOn广告"); ChangeState(AdInitState.InitializingTopOn, "初始化TopOn广告"); InitializeTopOnAds(); yield break; } Debug.Log("推广用户,开始VPN和AdMob检查流程"); // 状态2: 初始化Transfer SDK ChangeState(AdInitState.InitializingTransfer, "初始化Transfer SDK"); TransferAndroidClass.Init(); // 状态3: 检查VPN权限 ChangeState(AdInitState.CheckingVpnPermission, "检查VPN权限"); if (TransferData.HasVpnPermission) { Debug.Log("已有VPN权限,检查AdMob可用性"); yield return StartCoroutine(CheckAndInitializeAdsWithVpnPermission()); } else { Debug.Log("无VPN权限,检查是否已请求过权限"); yield return StartCoroutine(HandleNoVpnPermission()); } } public static float checkAdmobStart; /// /// 已有VPN权限时的广告初始化流程 /// private IEnumerator CheckAndInitializeAdsWithVpnPermission() { ChangeState(AdInitState.CheckingAdMobAvailability, "检查AdMob可用性"); bool checkCompleted = false; bool isAdMobAllowed = false; string message = ""; checkAdmobStart = Time.time; RushSDKManager.Instance.LogEvent("isAllowAdMob_check_start"); TransferAndroidClass.IsAllowAdMob((res, msg) => { isAdMobAllowed = res; message = msg; checkCompleted = true; RushSDKManager.Instance.LogEvent(string.Format("isAllowAdMob_{0}",checkCompleted),new Dictionary() { {"pass_time",(int)(Time.time-checkAdmobStart)}, {"reason",msg} }); }); // 等待检查完成 yield return new WaitUntil(() => checkCompleted); if (isAdMobAllowed) { Debug.Log($"AdMob可用: {message},初始化AdMob广告"); ChangeState(AdInitState.InitializingAdMob, "初始化AdMob广告"); InitializeAdMobAds(); } else { Debug.Log($"AdMob不可用: {message},初始化TopOn广告"); ChangeState(AdInitState.InitializingTopOn, "初始化TopOn广告"); InitializeTopOnAds(); if (TransferData.HasVpnPermission) { Debug.Log("权限引导流程已完成且AdMob检查失败,启动重试机制"); StartAdMobRetry(); } } } /// /// 处理无VPN权限的情况 /// private IEnumerator HandleNoVpnPermission() { // 已请求过VPN权限但用户拒绝 if (TransferData.HasShowedReqVpnPermissionView) { Debug.Log("已请求过VPN权限但用户拒绝,初始化TopOn广告"); ChangeState(AdInitState.InitializingTopOn, "初始化TopOn广告"); InitializeTopOnAds(); yield break; } // 首次请求VPN权限 Debug.Log("首次请求VPN权限,弹出引导"); ChangeState(AdInitState.RequestingVpnPermission, "请求VPN权限"); bool permissionRequestCompleted = false; bool permissionGranted = false; var vpnGuide = new VPNGuideManager(); vpnGuide.ShowConnectionRequest(_afterLoading, _level, (res, msg) => { permissionGranted = res; permissionRequestCompleted = true; Debug.Log($"VPN权限请求结果: {res}, 消息: {msg}"); }); // 等待权限请求完成 yield return new WaitUntil(() => permissionRequestCompleted); if (permissionGranted) { Debug.Log("VPN权限已授予,更新权限状态并检查AdMob"); TransferData.HasVpnPermission = true; yield return StartCoroutine(CheckAndInitializeAdsWithVpnPermission()); } else { Debug.Log("VPN权限被拒绝,初始化TopOn广告"); ChangeState(AdInitState.InitializingTopOn, "初始化TopOn广告"); InitializeTopOnAds(); } } private void StartAdMobRetry() { if (isRetryingAdMob) { Debug.Log("AdMob重试已在运行中"); return; } if (admobRetryCount >= MAX_ADMOB_RETRY_COUNT) { Debug.Log($"已达到最大重试次数({MAX_ADMOB_RETRY_COUNT}),停止重试"); return; } Debug.Log("启动AdMob重试机制"); isRetryingAdMob = true; admobRetryCoroutine = StartCoroutine(AdMobRetryCoroutine()); } private IEnumerator AdMobRetryCoroutine() { // 第一次立即检查 admobRetryCount = 0; while (isRetryingAdMob && admobRetryCount < MAX_ADMOB_RETRY_COUNT) { Debug.Log($"第{admobRetryCount + 1}次AdMob检查"); ChangeState(AdInitState.RetryingAdMobCheck, $"第{admobRetryCount + 1}次检查AdMob"); bool checkCompleted = false; bool isAdMobAllowed = false; string message = ""; RushSDKManager.Instance.LogEvent("isAllowAdMob_check_start"); checkAdmobStart = Time.time; TransferAndroidClass.IsAllowAdMob((res, msg) => { isAdMobAllowed = res; message = msg; checkCompleted = true; RushSDKManager.Instance.LogEvent(string.Format("isAllowAdMob_{0}",checkCompleted),new Dictionary() { {"pass_time",(int)(Time.time-checkAdmobStart)}, {"reason",msg} }); }); // 等待检查完成 yield return new WaitUntil(() => checkCompleted); if (isAdMobAllowed) { Debug.Log($"第{admobRetryCount + 1}次AdMob检查成功: {message}"); StopAdMobRetry(); InitializeAdMobAds(); break; } else { Debug.Log($"第{admobRetryCount + 1}次AdMob检查失败: {message}"); admobRetryCount++; // 达到最大重试次数 if (admobRetryCount >= MAX_ADMOB_RETRY_COUNT) { Debug.Log($"达到最大重试次数({MAX_ADMOB_RETRY_COUNT}),停止重试"); StopAdMobRetry(); break; } // 等待30秒后再次尝试 Debug.Log($"等待{ADMOB_RETRY_INTERVAL}秒后重试AdMob检查"); yield return new WaitForSeconds(ADMOB_RETRY_INTERVAL); } } } private void StopAdMobRetry() { isRetryingAdMob = false; if (admobRetryCoroutine != null) { StopCoroutine(admobRetryCoroutine); admobRetryCoroutine = null; } } private void InitializeAdMobAds() { StopAdMobRetry(); AdmobAdsManager.Instance.Initialize(); } private void InitializeTopOnAds() { TpnAdsManager.Instance.Initialize(); } private void ChangeState(AdInitState newState, string message) { var oldState = currentState; currentState = newState; Debug.Log($"广告初始化状态: {oldState} -> {newState} ({message})"); } private void CheckFirebaseStatus() { if(FireBaseRemoteConfigManager.Instance.IsInitialized) { Debug.Log("Firebase激活完成事件收到"); isFirebaseActivated = true; // 如果正在等待Firebase,继续广告初始化 if (currentState == AdInitState.WaitingForFirebase && waitingCoroutine != null) { // 停止等待协程,直接继续 if (waitingCoroutine != null) { StopCoroutine(waitingCoroutine); waitingCoroutine = null; } Debug.Log("从Firebase等待状态恢复,继续广告初始化"); ExecuteAdInitialization(); } } } public bool IsPromoteUser() { return !AdjustManager.Instance.IsOrganic(AdjustNetwork.GetNetwork()); } } }