AppLovinAutoUpdater.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // AppLovinAutoUpdater.cs
  3. // AppLovin MAX Unity Plugin
  4. //
  5. // Created by Santosh Bagadi on 1/27/20.
  6. // Copyright © 2020 AppLovin. All rights reserved.
  7. //
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using UnityEditor;
  12. namespace AppLovinMax.Scripts.IntegrationManager.Editor
  13. {
  14. /// <summary>
  15. /// Handles auto updates for AppLovin MAX plugin.
  16. /// </summary>
  17. public static class AppLovinAutoUpdater
  18. {
  19. public const string KeyAutoUpdateEnabled = "com.applovin.auto_update_enabled";
  20. private const string KeyLastUpdateCheckTime = "com.applovin.last_update_check_time_v2"; // Updated to v2 to force adapter version checks in plugin version 3.1.10.
  21. private static readonly DateTime EpochTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  22. private static readonly int SecondsInADay = (int) TimeSpan.FromDays(1).TotalSeconds;
  23. // TODO: Make this list dynamic.
  24. public static readonly Dictionary<string, string> MinAdapterVersions = new Dictionary<string, string>()
  25. {
  26. {"ADMOB_NETWORK", "android_23.3.0.1_ios_11.9.0.1"},
  27. {"BIDMACHINE_NETWORK", "android_3.0.1.1_ios_3.0.0.0.1"},
  28. {"CHARTBOOST_NETWORK", "android_9.7.0.3_ios_9.7.0.2"},
  29. {"FACEBOOK_MEDIATE", "android_6.17.0.1_ios_6.15.2.1"},
  30. {"FYBER_NETWORK", "android_8.3.1.1_ios_8.3.2.1"},
  31. {"GOOGLE_AD_MANAGER_NETWORK", "android_23.3.0.1_ios_11.9.0.1"},
  32. {"HYPRMX_NETWORK", "android_6.4.2.1_ios_6.4.1.0.1"},
  33. {"INMOBI_NETWORK", "android_10.7.7.1_ios_10.7.5.1"},
  34. {"IRONSOURCE_NETWORK", "android_8.3.0.0.2_ios_8.3.0.0.1"},
  35. {"LINE_NETWORK", "android_2024.8.27.1_ios_2.8.20240827.1"},
  36. {"MINTEGRAL_NETWORK", "android_16.8.51.1_ios_7.7.2.0.1"},
  37. {"MOBILEFUSE_NETWORK", "android_1.7.6.1_ios_1.7.6.1"},
  38. {"MOLOCO_NETWORK", "android_3.1.0.1_ios_3.1.3.1"},
  39. {"MYTARGET_NETWORK", "android_5.22.1.1_ios_5.21.7.1"},
  40. {"PUBMATIC_NETWORK", "android_3.9.0.2_ios_3.9.0.2"},
  41. {"SMAATO_NETWORK", "android_22.7.0.1_ios_22.8.4.1"},
  42. {"TIKTOK_NETWORK", "android_6.2.0.5.2_ios_6.2.0.7.2"},
  43. {"UNITY_NETWORK", "android_4.12.2.1_ios_4.12.2.1"},
  44. {"VERVE_NETWORK", "android_3.0.4.1_ios_3.0.4.1"},
  45. {"VUNGLE_NETWORK", "android_7.4.1.1_ios_7.4.1.1"},
  46. {"YANDEX_NETWORK", "android_7.4.0.1_ios_2.18.0.1"},
  47. };
  48. /// <summary>
  49. /// Checks if a new version of the plugin is available and prompts the user to update if one is available.
  50. /// </summary>
  51. public static void Update()
  52. {
  53. var now = (int) (DateTime.UtcNow - EpochTime).TotalSeconds;
  54. if (EditorPrefs.HasKey(KeyLastUpdateCheckTime))
  55. {
  56. var elapsedTime = now - EditorPrefs.GetInt(KeyLastUpdateCheckTime);
  57. // Check if we have checked for a new version in the last 24 hrs and skip update if we have.
  58. if (elapsedTime < SecondsInADay) return;
  59. }
  60. // Update last checked time.
  61. EditorPrefs.SetInt(KeyLastUpdateCheckTime, now);
  62. // Load the plugin data
  63. AppLovinEditorCoroutine.StartCoroutine(AppLovinIntegrationManager.Instance.LoadPluginData(data =>
  64. {
  65. if (data == null) return;
  66. ShowPluginUpdateDialogIfNeeded(data);
  67. ShowNetworkAdaptersUpdateDialogIfNeeded(data.MediatedNetworks);
  68. ShowGoogleNetworkAdaptersUpdateDialogIfNeeded(data.MediatedNetworks);
  69. }));
  70. }
  71. private static void ShowPluginUpdateDialogIfNeeded(PluginData data)
  72. {
  73. // Check if publisher has disabled auto update.
  74. if (!EditorPrefs.GetBool(KeyAutoUpdateEnabled, true)) return;
  75. // Check if the current and latest version are the same or if the publisher is on a newer version (on beta). If so, skip update.
  76. var comparison = data.AppLovinMax.CurrentToLatestVersionComparisonResult;
  77. if (comparison == MaxSdkUtils.VersionComparisonResult.Equal || comparison == MaxSdkUtils.VersionComparisonResult.Greater) return;
  78. // A new version of the plugin is available. Show a dialog to the publisher.
  79. var option = EditorUtility.DisplayDialogComplex(
  80. "AppLovin MAX Plugin Update",
  81. "A new version of AppLovin MAX plugin is available for download. Update now?",
  82. "Download",
  83. "Not Now",
  84. "Don't Ask Again");
  85. if (option == 0) // Download
  86. {
  87. MaxSdkLogger.UserDebug("Downloading plugin...");
  88. AppLovinIntegrationManager.OnDownloadPluginProgressCallback = AppLovinIntegrationManagerWindow.OnDownloadPluginProgress;
  89. AppLovinEditorCoroutine.StartCoroutine(AppLovinIntegrationManager.Instance.DownloadPlugin(data.AppLovinMax));
  90. }
  91. else if (option == 1) // Not Now
  92. {
  93. // Do nothing
  94. MaxSdkLogger.UserDebug("Update postponed.");
  95. }
  96. else if (option == 2) // Don't Ask Again
  97. {
  98. MaxSdkLogger.UserDebug("Auto Update disabled. You can enable it again from the AppLovin Integration Manager");
  99. EditorPrefs.SetBool(KeyAutoUpdateEnabled, false);
  100. }
  101. }
  102. private static void ShowNetworkAdaptersUpdateDialogIfNeeded(Network[] networks)
  103. {
  104. var networksToUpdate = networks.Where(network => network.RequiresUpdate).ToList();
  105. // If all networks are above the required version, do nothing.
  106. if (networksToUpdate.Count <= 0) return;
  107. // We found a few adapters that are not compatible with the current SDK, show alert.
  108. var message = "The following network adapters are not compatible with the current version of AppLovin MAX Plugin:\n";
  109. foreach (var networkName in networksToUpdate)
  110. {
  111. message += "\n- ";
  112. message += networkName.DisplayName + " (Requires " + MinAdapterVersions[networkName.Name] + " or newer)";
  113. }
  114. message += "\n\nPlease update them to the latest versions to avoid any issues.";
  115. AppLovinIntegrationManager.ShowBuildFailureDialog(message);
  116. }
  117. private static void ShowGoogleNetworkAdaptersUpdateDialogIfNeeded(Network[] networks)
  118. {
  119. // AdMob and GAM use the same SDKs so their adapters should use the same underlying SDK version.
  120. var googleNetwork = networks.FirstOrDefault(network => network.Name.Equals("ADMOB_NETWORK"));
  121. var googleAdManagerNetwork = networks.FirstOrDefault(network => network.Name.Equals("GOOGLE_AD_MANAGER_NETWORK"));
  122. // If both AdMob and GAM are not integrated, do nothing.
  123. if (googleNetwork == null || string.IsNullOrEmpty(googleNetwork.CurrentVersions.Unity) ||
  124. googleAdManagerNetwork == null || string.IsNullOrEmpty(googleAdManagerNetwork.CurrentVersions.Unity)) return;
  125. var isAndroidVersionCompatible = GoogleNetworkAdaptersCompatible(googleNetwork.CurrentVersions.Android, googleAdManagerNetwork.CurrentVersions.Android, "19.8.0.0");
  126. var isIosVersionCompatible = GoogleNetworkAdaptersCompatible(googleNetwork.CurrentVersions.Ios, googleAdManagerNetwork.CurrentVersions.Ios, "8.0.0.0");
  127. if (isAndroidVersionCompatible && isIosVersionCompatible) return;
  128. var message = "You may see unexpected errors if you use different versions of the AdMob and Google Ad Manager adapter SDKs. " +
  129. "AdMob and Google Ad Manager share the same SDKs.\n\n" +
  130. "You can be sure that you are using the same SDK for both if the first three numbers in each adapter version match.";
  131. AppLovinIntegrationManager.ShowBuildFailureDialog(message);
  132. }
  133. private static bool GoogleNetworkAdaptersCompatible(string googleVersion, string googleAdManagerVersion, string breakingVersion)
  134. {
  135. var googleResult = MaxSdkUtils.CompareVersions(googleVersion, breakingVersion);
  136. var googleAdManagerResult = MaxSdkUtils.CompareVersions(googleAdManagerVersion, breakingVersion);
  137. // If one is less than the breaking version and the other is not, they are not compatible.
  138. if (googleResult == MaxSdkUtils.VersionComparisonResult.Lesser &&
  139. googleAdManagerResult != MaxSdkUtils.VersionComparisonResult.Lesser) return false;
  140. if (googleAdManagerResult == MaxSdkUtils.VersionComparisonResult.Lesser &&
  141. googleResult != MaxSdkUtils.VersionComparisonResult.Lesser) return false;
  142. return true;
  143. }
  144. }
  145. }