AppLovinPluginMigrationHelper.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEngine;
  6. #if UNITY_2019_2_OR_NEWER
  7. namespace AppLovinMax.Scripts.IntegrationManager.Editor
  8. {
  9. /// <summary>
  10. /// Moves our SDK Unity Plugin from under the Assets folder to the Unity Package Manager.
  11. /// </summary>
  12. public static class AppLovinPluginMigrationHelper
  13. {
  14. private const string ApplovinRegistryName = "AppLovin MAX Unity";
  15. private const string ApplovinRegistryUrl = "https://unity.packages.applovin.com/";
  16. private static readonly List<string> AppLovinRegistryScopes = new List<string>() {"com.applovin.mediation.ads", "com.applovin.mediation.adapters", "com.applovin.mediation.dsp"};
  17. private const string OpenUpmRegistryName = "package.openupm.com";
  18. private const string OpenUpmRegistryUrl = "https://package.openupm.com";
  19. private static readonly List<string> OpenUpmRegistryScopes = new List<string>() {"com.google.external-dependency-manager"};
  20. private static List<string> betaNetworkPluginFilePaths = new List<string>();
  21. /// <summary>
  22. /// Attempts to move the Unity plugin to UPM by adding the AppLovin scoped registry and dependencies to the manifest.
  23. /// </summary>
  24. /// <param name="pluginData">The Unity Plugin data for our sdk and mediation adapters.</param>
  25. /// <param name="deleteExternalDependencyManager">Whether to delete the EDM folder under "Assets"</param>
  26. internal static void MigrateToUnityPackageManager(PluginData pluginData, bool deleteExternalDependencyManager)
  27. {
  28. MaxSdkLogger.UserDebug("Moving AppLovin Unity Plugin to package manager");
  29. if (deleteExternalDependencyManager)
  30. {
  31. DeleteExternalDependencyManager();
  32. }
  33. var appLovinManifest = AppLovinUpmManifest.Load();
  34. MigrateAdapters(pluginData, appLovinManifest);
  35. MigratePlugin(pluginData, appLovinManifest);
  36. appLovinManifest.Save();
  37. AppLovinUpmPackageManager.ResolvePackageManager();
  38. DeletePluginFiles();
  39. }
  40. /// <summary>
  41. /// Add all currently installed networks to the manifest.
  42. /// </summary>
  43. internal static void MigrateAdapters(PluginData pluginData, AppLovinUpmManifest appLovinManifest)
  44. {
  45. var allNetworks = pluginData.MediatedNetworks.Concat(pluginData.PartnerMicroSdks).ToArray();
  46. betaNetworkPluginFilePaths.Clear();
  47. // Add every currently installed network and separate it by android and iOS.
  48. foreach (var network in allNetworks)
  49. {
  50. var currentVersion = network.CurrentVersions != null ? network.CurrentVersions.Unity : "";
  51. if (string.IsNullOrEmpty(currentVersion)) continue;
  52. if (currentVersion.Contains("beta"))
  53. {
  54. betaNetworkPluginFilePaths.AddRange(network.PluginFilePaths);
  55. continue;
  56. }
  57. AppLovinUpmPackageManager.AddPackages(network, appLovinManifest);
  58. }
  59. }
  60. /// <summary>
  61. /// Add the AppLovin scoped registry to the manifest if it doesn't exist. Otherwise update it.
  62. /// </summary>
  63. private static void MigratePlugin(PluginData pluginData, AppLovinUpmManifest appLovinManifest)
  64. {
  65. appLovinManifest.AddOrUpdateRegistry(ApplovinRegistryName, ApplovinRegistryUrl, AppLovinRegistryScopes);
  66. appLovinManifest.AddOrUpdateRegistry(OpenUpmRegistryName, OpenUpmRegistryUrl, OpenUpmRegistryScopes);
  67. var appLovinVersion = pluginData.AppLovinMax.LatestVersions.Unity;
  68. appLovinManifest.AddPackageDependency(AppLovinUpmPackageManager.PackageNamePrefixAppLovin, appLovinVersion);
  69. }
  70. #region Utility
  71. /// <summary>
  72. /// Delete the external dependency manager folder from the project.
  73. /// </summary>
  74. private static void DeleteExternalDependencyManager()
  75. {
  76. var externalDependencyManagerPath = Path.Combine(Application.dataPath, "ExternalDependencyManager");
  77. FileUtil.DeleteFileOrDirectory(externalDependencyManagerPath);
  78. FileUtil.DeleteFileOrDirectory(externalDependencyManagerPath + ".meta");
  79. }
  80. /// <summary>
  81. /// Deletes all the files in the plugin directory except the AppLovinSettings.asset file.
  82. /// </summary>
  83. private static void DeletePluginFiles()
  84. {
  85. if (AppLovinIntegrationManager.IsPluginInPackageManager) return;
  86. var pluginPath = Path.Combine(AppLovinIntegrationManager.PluginParentDirectory, "MaxSdk");
  87. var appLovinSettingsPath = Path.Combine(pluginPath, "Resources/AppLovinSettings.asset");
  88. var appLovinResourcesDirectory = Path.Combine(pluginPath, "Resources");
  89. var appLovinSettingsTempPath = Path.Combine(Path.GetTempPath(), "AppLovinSettings.asset");
  90. var appLovinMediationTempPath = Path.Combine(Path.GetTempPath(), "Mediation");
  91. // Ensure there aren't any errors when moving the files due to the directories/files already existing.
  92. FileUtil.DeleteFileOrDirectory(appLovinSettingsTempPath);
  93. FileUtil.DeleteFileOrDirectory(appLovinMediationTempPath);
  94. var mediationAssetsDir = Path.Combine(AppLovinIntegrationManager.PluginParentDirectory, "MaxSdk/Mediation");
  95. // Move the AppLovinSettings.asset file and any beta adapters to a temporary directory.
  96. File.Move(appLovinSettingsPath, appLovinSettingsTempPath);
  97. var adapterSaved = MoveBetaAdaptersIfNeeded(mediationAssetsDir, appLovinMediationTempPath);
  98. // Move the meta file if the adapter was saved to save the asset labels
  99. if (adapterSaved)
  100. {
  101. File.Move(mediationAssetsDir, appLovinMediationTempPath + ".meta");
  102. }
  103. // Delete the plugin directory and then move the AppLovinSettings.asset and beta adapters back.
  104. FileUtil.DeleteFileOrDirectory(pluginPath);
  105. Directory.CreateDirectory(appLovinResourcesDirectory);
  106. File.Move(appLovinSettingsTempPath, appLovinSettingsPath);
  107. MoveBetaAdaptersIfNeeded(appLovinMediationTempPath, mediationAssetsDir);
  108. if (adapterSaved)
  109. {
  110. File.Move(appLovinMediationTempPath + ".meta", mediationAssetsDir);
  111. }
  112. FileUtil.DeleteFileOrDirectory(appLovinMediationTempPath);
  113. }
  114. /// <summary>
  115. /// Moves the beta adapters from a source mediation directory to a destination mediation directory.
  116. /// </summary>
  117. /// <param name="sourceMediationDirectory">The directory containing the beta adapters to be moved.</param>
  118. /// <param name="destinationMediationDirectory">The target directory where the beta adapters should be moved.</param>
  119. private static bool MoveBetaAdaptersIfNeeded(string sourceMediationDirectory, string destinationMediationDirectory)
  120. {
  121. if (betaNetworkPluginFilePaths.Count == 0 || !Directory.Exists(sourceMediationDirectory)) return false;
  122. var movedAdapter = false;
  123. Directory.CreateDirectory(destinationMediationDirectory);
  124. foreach (var pluginFilePath in betaNetworkPluginFilePaths)
  125. {
  126. var sourceDirectory = Path.Combine(sourceMediationDirectory, Path.GetFileName(pluginFilePath));
  127. var destinationDirectory = Path.Combine(destinationMediationDirectory, Path.GetFileName(pluginFilePath));
  128. if (Directory.Exists(sourceDirectory))
  129. {
  130. Directory.Move(sourceDirectory, destinationDirectory);
  131. movedAdapter = true;
  132. }
  133. }
  134. return movedAdapter;
  135. }
  136. #endregion
  137. }
  138. }
  139. #endif