|
- using System;
- using System.Diagnostics;
- using CIG.Extensions;
- using GoogleMobileAds.Api;
- using SUISS.Core;
- using SUISSEngine;
- using UnityEngine;
-
- namespace CIG
- {
- public sealed class AdMobInterstitialAdsProvider : Singleton<AdMobInterstitialAdsProvider>, IAdProvider
- {
- public AdMobInterstitialAdsProvider()
- {
- GameObject gameObject = new GameObject("AdMobInterstitialAdsProvider");
- UnityEngine.Object.DontDestroyOnLoad(gameObject);
- gameObject.hideFlags = HideFlags.HideAndDontSave;
- this._providerMonoBehaviour = gameObject.AddComponent<AdProviderMonoBehaviour>();
- }
-
- //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
- public event AdAvailabilityChangedEventHandler AvailabilityChangedEvent;
-
- private void FireAvailabilityChangedEvent()
- {
- if (this.AvailabilityChangedEvent != null)
- {
- this.AvailabilityChangedEvent(this);
- }
- }
-
- public void StartCaching()
- {
- AdProviderState providerState = this._providerState;
- if (providerState != AdProviderState.None)
- {
- if (providerState == AdProviderState.Initialized)
- {
- AdState adState = this._adState;
- if (adState == AdState.None)
- {
- if (SingletonMonobehaviour<CIGWebService>.IsAvailable)
- {
- this.LoadAd();
- }
- else
- {
- this.TrySchedulingDelayedCachingRoutine(false);
- }
- }
- }
- }
- else
- {
- try
- {
- this._providerState = AdProviderState.Initializing;
- MobileAds.Initialize(this.AppId);
- this._defaultAdRequest = new AdRequest.Builder().TagForChildDirectedTreatment(this.TagForChildDirectedTreatmentEnabled).Build();
- this._providerState = AdProviderState.Initialized;
- this.LoadAd();
- }
- catch (Exception ex)
- {
- this._providerState = AdProviderState.None;
- UnityEngine.Debug.LogErrorFormat("[AdMobInterstitialAdsProvider] Failed to Initialize AdMob. Error: {0}\r\nStacktrace:\r\n{1}", new object[]
- {
- ex.Message,
- ex.StackTrace
- });
- }
- }
- }
-
- public bool IsReady
- {
- get
- {
- return this._providerState == AdProviderState.Initialized && this._adState == AdState.Available && this._interstitialAd != null && this._interstitialAd.IsLoaded();
- }
- }
-
- public bool ShowAd(Action<bool, bool> callback)
- {
- if (this.IsReady)
- {
- this._callback = callback;
- this._adState = AdState.Showing;
- SingletonMonobehaviour<CIGGameStats>.Instance.AddScreenViewed("interstitial_admob");
- try
- {
- this._interstitialAd.Show();
- }
- catch (Exception ex)
- {
- this.DestroyAd();
- UnityEngine.Debug.LogErrorFormat("[AdMobInterstitialAdsProvider] InterstitialAd.Show() failed. Error: {0}\r\nStacktrace:\r\n{1}", new object[]
- {
- ex.Message,
- ex.StackTrace
- });
- return false;
- }
- this.FireAvailabilityChangedEvent();
- return true;
- }
- return false;
- }
-
- public AdType AdType
- {
- get
- {
- return AdType.Interstitial;
- }
- }
-
- public AdProviderType AdProviderType
- {
- get
- {
- return AdProviderType.Admob;
- }
- }
-
- private void LoadAd()
- {
- if (this._adState == AdState.None && this._interstitialAd == null)
- {
- try
- {
- this._interstitialAd = new InterstitialAd(this.AdId);
- }
- catch (Exception ex)
- {
- this._interstitialAd = null;
- UnityEngine.Debug.LogErrorFormat("[AdMobInterstitialAdsProvider] Failed to instantiate InterstitialAd. Error: {0}\r\nStacktrace:\r\n{1}", new object[]
- {
- ex.Message,
- ex.StackTrace
- });
- }
- if (this._interstitialAd != null)
- {
- this._interstitialAd.OnAdClosed += this.OnAdClosed;
- this._interstitialAd.OnAdLoaded += this.OnAdLoaded;
- this._interstitialAd.OnAdFailedToLoad += this.OnAdFailedToLoad;
- try
- {
- this._interstitialAd.LoadAd(this._defaultAdRequest);
- this._adState = AdState.Requesting;
- }
- catch (Exception ex2)
- {
- this._interstitialAd.OnAdClosed -= this.OnAdClosed;
- this._interstitialAd.OnAdLoaded -= this.OnAdLoaded;
- this._interstitialAd.OnAdFailedToLoad -= this.OnAdFailedToLoad;
- this._interstitialAd = null;
- UnityEngine.Debug.LogErrorFormat("[AdMobInterstitialAdsProvider] InterstitalAd.LoadAd() failed. Error: {0}\r\nStacktrace:\r\n{1}", new object[]
- {
- ex2.Message,
- ex2.StackTrace
- });
- }
- }
- }
- }
-
- private void DestroyAd()
- {
- if (this._interstitialAd != null)
- {
- this._interstitialAd.OnAdClosed -= this.OnAdClosed;
- this._interstitialAd.OnAdLoaded -= this.OnAdLoaded;
- this._interstitialAd.OnAdFailedToLoad -= this.OnAdFailedToLoad;
- try
- {
- this._interstitialAd.Destroy();
- }
- catch (Exception ex)
- {
- UnityEngine.Debug.LogErrorFormat("[AdMobInterstitialAdsProvider] Failed to Destroy InterstitialAd. Error: {0}\r\nStacktrace:\r\n{1}", new object[]
- {
- ex.Message,
- ex.StackTrace
- });
- }
- this._interstitialAd = null;
- }
- this._adState = AdState.None;
- }
-
- private void TrySchedulingDelayedCachingRoutine(bool applyBackOff)
- {
- if (this._providerMonoBehaviour.IsInvoking(new Action(this.StartCaching)))
- {
- return;
- }
- float num = 5f;
- if (applyBackOff)
- {
- this._backOffSeconds = Mathf.Min(this._backOffSeconds + 5f, 120f);
- num += this._backOffSeconds;
- }
- this._providerMonoBehaviour.InvokeOnMainThread(new Action(this.StartCaching), num);
- UnityEngine.Debug.LogWarningFormat("[AdMobInterstitialAdsProvider] Ad Failed To Load. Retrying in {0} seconds", new object[]
- {
- num
- });
- }
-
- private void OnAdLoaded(object sender, EventArgs e)
- {
- this._backOffSeconds = 0f;
- this._adState = AdState.Available;
- this.FireAvailabilityChangedEvent();
- }
-
- private void OnAdFailedToLoad(object sender, AdFailedToLoadEventArgs adFailedToLoadEventArgs)
- {
- this.DestroyAd();
- this.FireAvailabilityChangedEvent();
- this.TrySchedulingDelayedCachingRoutine(true);
- }
-
- private void OnAdClosed(object sender, EventArgs args)
- {
- Action<bool, bool> callback = this._callback;
- this.DestroyAd();
- this._callback = null;
- if (callback != null)
- {
- callback(true, false);
- }
- this.FireAvailabilityChangedEvent();
- this.StartCaching();
- }
-
- private bool IsPayingUser
- {
- get
- {
- return SingletonMonobehaviour<CIGWebService>.Instance.Properties.GetBoolean("is_paying_user", false);
- }
- }
-
- private string AppId
- {
- get
- {
- return "ca-app-pub-2218023806663419~8272615086";
- }
- }
-
- private string AdId
- {
- get
- {
- if (this.IsPayingUser)
- {
- return "ca-app-pub-2218023806663419/7447947481";
- }
- return "ca-app-pub-2218023806663419/5179547886";
- }
- }
-
- private string TestAdId
- {
- get
- {
- return "ca-app-pub-3940256099942544/1033173712";
- }
- }
-
- private bool TagForChildDirectedTreatmentEnabled
- {
- get
- {
- return true;
- }
- }
-
- private const string AndroidTestAdId = "ca-app-pub-3940256099942544/1033173712";
-
- public const string AndroidAppId = "ca-app-pub-2218023806663419~8272615086";
-
- private const string AndroidNonPayerAdId = "ca-app-pub-2218023806663419/5179547886";
-
- private const string AndroidPayerAdId = "ca-app-pub-2218023806663419/7447947481";
-
- private const string iOSTestAdId = "ca-app-pub-3940256099942544/4411468910";
-
- public const string iOSAppId = "ca-app-pub-2218023806663419~7853812687";
-
- private const string iOSNonPayerAdId = "ca-app-pub-2218023806663419/9051344285";
-
- private const string iOSPayerAdId = "ca-app-pub-2218023806663419/2878147084";
-
- private const string IsPayingUserServerKey = "is_paying_user";
-
- private const float MaxBackOffSeconds = 120f;
-
- private AdProviderState _providerState;
-
- private AdState _adState;
-
- private Action<bool, bool> _callback;
-
- private InterstitialAd _interstitialAd;
-
- private AdRequest _defaultAdRequest;
-
- private float _backOffSeconds;
-
- private AdProviderMonoBehaviour _providerMonoBehaviour;
- }
- }
|