You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

318 lines
8.0 KiB

  1. using System;
  2. using System.Diagnostics;
  3. using CIG.Extensions;
  4. using GoogleMobileAds.Api;
  5. using SUISS.Core;
  6. using SUISSEngine;
  7. using UnityEngine;
  8. namespace CIG
  9. {
  10. public sealed class AdMobInterstitialAdsProvider : Singleton<AdMobInterstitialAdsProvider>, IAdProvider
  11. {
  12. public AdMobInterstitialAdsProvider()
  13. {
  14. GameObject gameObject = new GameObject("AdMobInterstitialAdsProvider");
  15. UnityEngine.Object.DontDestroyOnLoad(gameObject);
  16. gameObject.hideFlags = HideFlags.HideAndDontSave;
  17. this._providerMonoBehaviour = gameObject.AddComponent<AdProviderMonoBehaviour>();
  18. }
  19. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  20. public event AdAvailabilityChangedEventHandler AvailabilityChangedEvent;
  21. private void FireAvailabilityChangedEvent()
  22. {
  23. if (this.AvailabilityChangedEvent != null)
  24. {
  25. this.AvailabilityChangedEvent(this);
  26. }
  27. }
  28. public void StartCaching()
  29. {
  30. AdProviderState providerState = this._providerState;
  31. if (providerState != AdProviderState.None)
  32. {
  33. if (providerState == AdProviderState.Initialized)
  34. {
  35. AdState adState = this._adState;
  36. if (adState == AdState.None)
  37. {
  38. if (SingletonMonobehaviour<CIGWebService>.IsAvailable)
  39. {
  40. this.LoadAd();
  41. }
  42. else
  43. {
  44. this.TrySchedulingDelayedCachingRoutine(false);
  45. }
  46. }
  47. }
  48. }
  49. else
  50. {
  51. try
  52. {
  53. this._providerState = AdProviderState.Initializing;
  54. MobileAds.Initialize(this.AppId);
  55. this._defaultAdRequest = new AdRequest.Builder().TagForChildDirectedTreatment(this.TagForChildDirectedTreatmentEnabled).Build();
  56. this._providerState = AdProviderState.Initialized;
  57. this.LoadAd();
  58. }
  59. catch (Exception ex)
  60. {
  61. this._providerState = AdProviderState.None;
  62. UnityEngine.Debug.LogErrorFormat("[AdMobInterstitialAdsProvider] Failed to Initialize AdMob. Error: {0}\r\nStacktrace:\r\n{1}", new object[]
  63. {
  64. ex.Message,
  65. ex.StackTrace
  66. });
  67. }
  68. }
  69. }
  70. public bool IsReady
  71. {
  72. get
  73. {
  74. return this._providerState == AdProviderState.Initialized && this._adState == AdState.Available && this._interstitialAd != null && this._interstitialAd.IsLoaded();
  75. }
  76. }
  77. public bool ShowAd(Action<bool, bool> callback)
  78. {
  79. if (this.IsReady)
  80. {
  81. this._callback = callback;
  82. this._adState = AdState.Showing;
  83. SingletonMonobehaviour<CIGGameStats>.Instance.AddScreenViewed("interstitial_admob");
  84. try
  85. {
  86. this._interstitialAd.Show();
  87. }
  88. catch (Exception ex)
  89. {
  90. this.DestroyAd();
  91. UnityEngine.Debug.LogErrorFormat("[AdMobInterstitialAdsProvider] InterstitialAd.Show() failed. Error: {0}\r\nStacktrace:\r\n{1}", new object[]
  92. {
  93. ex.Message,
  94. ex.StackTrace
  95. });
  96. return false;
  97. }
  98. this.FireAvailabilityChangedEvent();
  99. return true;
  100. }
  101. return false;
  102. }
  103. public AdType AdType
  104. {
  105. get
  106. {
  107. return AdType.Interstitial;
  108. }
  109. }
  110. public AdProviderType AdProviderType
  111. {
  112. get
  113. {
  114. return AdProviderType.Admob;
  115. }
  116. }
  117. private void LoadAd()
  118. {
  119. if (this._adState == AdState.None && this._interstitialAd == null)
  120. {
  121. try
  122. {
  123. this._interstitialAd = new InterstitialAd(this.AdId);
  124. }
  125. catch (Exception ex)
  126. {
  127. this._interstitialAd = null;
  128. UnityEngine.Debug.LogErrorFormat("[AdMobInterstitialAdsProvider] Failed to instantiate InterstitialAd. Error: {0}\r\nStacktrace:\r\n{1}", new object[]
  129. {
  130. ex.Message,
  131. ex.StackTrace
  132. });
  133. }
  134. if (this._interstitialAd != null)
  135. {
  136. this._interstitialAd.OnAdClosed += this.OnAdClosed;
  137. this._interstitialAd.OnAdLoaded += this.OnAdLoaded;
  138. this._interstitialAd.OnAdFailedToLoad += this.OnAdFailedToLoad;
  139. try
  140. {
  141. this._interstitialAd.LoadAd(this._defaultAdRequest);
  142. this._adState = AdState.Requesting;
  143. }
  144. catch (Exception ex2)
  145. {
  146. this._interstitialAd.OnAdClosed -= this.OnAdClosed;
  147. this._interstitialAd.OnAdLoaded -= this.OnAdLoaded;
  148. this._interstitialAd.OnAdFailedToLoad -= this.OnAdFailedToLoad;
  149. this._interstitialAd = null;
  150. UnityEngine.Debug.LogErrorFormat("[AdMobInterstitialAdsProvider] InterstitalAd.LoadAd() failed. Error: {0}\r\nStacktrace:\r\n{1}", new object[]
  151. {
  152. ex2.Message,
  153. ex2.StackTrace
  154. });
  155. }
  156. }
  157. }
  158. }
  159. private void DestroyAd()
  160. {
  161. if (this._interstitialAd != null)
  162. {
  163. this._interstitialAd.OnAdClosed -= this.OnAdClosed;
  164. this._interstitialAd.OnAdLoaded -= this.OnAdLoaded;
  165. this._interstitialAd.OnAdFailedToLoad -= this.OnAdFailedToLoad;
  166. try
  167. {
  168. this._interstitialAd.Destroy();
  169. }
  170. catch (Exception ex)
  171. {
  172. UnityEngine.Debug.LogErrorFormat("[AdMobInterstitialAdsProvider] Failed to Destroy InterstitialAd. Error: {0}\r\nStacktrace:\r\n{1}", new object[]
  173. {
  174. ex.Message,
  175. ex.StackTrace
  176. });
  177. }
  178. this._interstitialAd = null;
  179. }
  180. this._adState = AdState.None;
  181. }
  182. private void TrySchedulingDelayedCachingRoutine(bool applyBackOff)
  183. {
  184. if (this._providerMonoBehaviour.IsInvoking(new Action(this.StartCaching)))
  185. {
  186. return;
  187. }
  188. float num = 5f;
  189. if (applyBackOff)
  190. {
  191. this._backOffSeconds = Mathf.Min(this._backOffSeconds + 5f, 120f);
  192. num += this._backOffSeconds;
  193. }
  194. this._providerMonoBehaviour.InvokeOnMainThread(new Action(this.StartCaching), num);
  195. UnityEngine.Debug.LogWarningFormat("[AdMobInterstitialAdsProvider] Ad Failed To Load. Retrying in {0} seconds", new object[]
  196. {
  197. num
  198. });
  199. }
  200. private void OnAdLoaded(object sender, EventArgs e)
  201. {
  202. this._backOffSeconds = 0f;
  203. this._adState = AdState.Available;
  204. this.FireAvailabilityChangedEvent();
  205. }
  206. private void OnAdFailedToLoad(object sender, AdFailedToLoadEventArgs adFailedToLoadEventArgs)
  207. {
  208. this.DestroyAd();
  209. this.FireAvailabilityChangedEvent();
  210. this.TrySchedulingDelayedCachingRoutine(true);
  211. }
  212. private void OnAdClosed(object sender, EventArgs args)
  213. {
  214. Action<bool, bool> callback = this._callback;
  215. this.DestroyAd();
  216. this._callback = null;
  217. if (callback != null)
  218. {
  219. callback(true, false);
  220. }
  221. this.FireAvailabilityChangedEvent();
  222. this.StartCaching();
  223. }
  224. private bool IsPayingUser
  225. {
  226. get
  227. {
  228. return SingletonMonobehaviour<CIGWebService>.Instance.Properties.GetBoolean("is_paying_user", false);
  229. }
  230. }
  231. private string AppId
  232. {
  233. get
  234. {
  235. return "ca-app-pub-2218023806663419~8272615086";
  236. }
  237. }
  238. private string AdId
  239. {
  240. get
  241. {
  242. if (this.IsPayingUser)
  243. {
  244. return "ca-app-pub-2218023806663419/7447947481";
  245. }
  246. return "ca-app-pub-2218023806663419/5179547886";
  247. }
  248. }
  249. private string TestAdId
  250. {
  251. get
  252. {
  253. return "ca-app-pub-3940256099942544/1033173712";
  254. }
  255. }
  256. private bool TagForChildDirectedTreatmentEnabled
  257. {
  258. get
  259. {
  260. return true;
  261. }
  262. }
  263. private const string AndroidTestAdId = "ca-app-pub-3940256099942544/1033173712";
  264. public const string AndroidAppId = "ca-app-pub-2218023806663419~8272615086";
  265. private const string AndroidNonPayerAdId = "ca-app-pub-2218023806663419/5179547886";
  266. private const string AndroidPayerAdId = "ca-app-pub-2218023806663419/7447947481";
  267. private const string iOSTestAdId = "ca-app-pub-3940256099942544/4411468910";
  268. public const string iOSAppId = "ca-app-pub-2218023806663419~7853812687";
  269. private const string iOSNonPayerAdId = "ca-app-pub-2218023806663419/9051344285";
  270. private const string iOSPayerAdId = "ca-app-pub-2218023806663419/2878147084";
  271. private const string IsPayingUserServerKey = "is_paying_user";
  272. private const float MaxBackOffSeconds = 120f;
  273. private AdProviderState _providerState;
  274. private AdState _adState;
  275. private Action<bool, bool> _callback;
  276. private InterstitialAd _interstitialAd;
  277. private AdRequest _defaultAdRequest;
  278. private float _backOffSeconds;
  279. private AdProviderMonoBehaviour _providerMonoBehaviour;
  280. }
  281. }