using System; using Dirichlet.Mediation; using Runtime.ADAggregator; using UnityEngine; public sealed class TapadnSplashPlayer : ADPlayer, IDirichletSplashAutoAdListener { private DirichletAdNative _adNative; private DirichletSplashAd _loadedAd; private bool _showSettled; public override int MaxLoadAttempts => TapadnAdController.CurrentOptions?.SplashMaxLoadAttempts ?? base.MaxLoadAttempts; public override float LoadRetryDelaySeconds => Math.Max(0f, (TapadnAdController.CurrentOptions?.SplashLoadRetryDelayMs ?? 500) / 1000f); public override float ShowPendingTimeoutSeconds => Math.Max(1f, (TapadnAdController.CurrentOptions?.SplashShowTimeoutMs ?? 20000) / 1000f); public override bool AutoPreloadOnInit => TapadnAdController.CurrentOptions?.SplashPrewarmOnInit ?? false; public override void OnInit() { _adNative = DirichletAdManager.CreateAdNative(); } public override bool IsReadly() { if (UseAutoLoad()) { return TapadnAdRequestFactory.TryParseSlotId(Key, out _); } if (_loadedAd != null && _loadedAd.IsLoaded && _loadedAd.IsValid) { curState = 2; return true; } return false; } public override void LoadAD() { if (!TapadnAdRequestFactory.TryParseSlotId(Key, out _)) { Debug.LogError($"[TapADN] Invalid splash slot id: {Key}"); curState = 0; return; } if (UseAutoLoad()) { curState = 2; return; } if (curState == 1 || IsReadly()) { return; } curState = 1; TapadnSmartLoadOrchestrator.OnLoadStarted(AD_Type.Splash, AdScene, Key); _adNative.LoadSplashAd( TapadnAdRequestFactory.BuildSplash(Key, TapadnAdController.CurrentOptions), ad => { TapadnSmartLoadOrchestrator.OnLoadResult(AD_Type.Splash, AdScene, true, Key); _loadedAd?.Destroy(); _loadedAd = ad; _loadedAd.Shown += OnManualShown; _loadedAd.Clicked += OnManualClicked; _loadedAd.ShowFailed += OnManualShowFailed; _loadedAd.Closed += OnManualClosed; curState = 2; Debug.Log($"[TapADN] Splash loaded. slot={Key}"); }, error => { TapadnSmartLoadOrchestrator.OnLoadResult(AD_Type.Splash, AdScene, false, Key); curState = 0; Debug.LogError($"[TapADN] Splash load failed. code={error.Code}, message={error.Message}"); }); } public override void ShowAD(Action onClose, Action onVideoComplete) { adListener.onClose = onClose; adListener.onVideoComplete = onVideoComplete; _showSettled = false; curState = 0; if (UseAutoLoad()) { _adNative.ShowSplashAutoAd(TapadnAdRequestFactory.BuildSplash(Key, TapadnAdController.CurrentOptions), this); return; } if (_loadedAd == null || !_loadedAd.Show()) { OnError(new DirichletError("show_failed", "ShowSplashAd returned false")); } } public void OnError(DirichletError error) { if (_showSettled) { return; } _showSettled = true; TapadnSmartLoadOrchestrator.OnShowError(AD_Type.Splash, AdScene, Key); curState = 0; Debug.LogError($"[TapADN] Splash show failed. code={error?.Code}, message={error?.Message}"); adListener.OnShowError(); } public void OnAdShow() { TapadnSmartLoadOrchestrator.OnShowStart(AD_Type.Splash, AdScene, Key); NotifyShowStarted(); } public void OnAdClose() { if (_showSettled) { return; } _showSettled = true; adListener.OnAdClose(); adListener.OnShowComplete(); } public void OnAdClick() { } public override void OnPlayRequestStarted() { TapadnSmartLoadOrchestrator.OnPlayRequestStarted(AD_Type.Splash, AdScene, !UseAutoLoad() && IsReadly(), Key); } public override void EnterAdScenario(string scenario) { AdScene = string.IsNullOrWhiteSpace(scenario) ? "__default__" : scenario.Trim(); TapadnSmartLoadOrchestrator.OnEnterAdScenario(AD_Type.Splash, AdScene, Key); } private bool UseAutoLoad() { return TapadnAdController.CurrentOptions?.SplashAutoLoad ?? true; } private void OnManualShown() { TapadnSmartLoadOrchestrator.OnShowStart(AD_Type.Splash, AdScene, Key); NotifyShowStarted(); } private void OnManualClicked() { } private void OnManualShowFailed(DirichletError error) { OnError(error); } private void OnManualClosed() { _loadedAd?.Destroy(); _loadedAd = null; OnAdClose(); } }