AppLovinPreProcess.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // AppLovinPreProcess.cs
  3. // AppLovin MAX Unity Plugin
  4. //
  5. // Created by Jonathan Liu on 10/19/2023.
  6. // Copyright © 2023 AppLovin. All rights reserved.
  7. //
  8. using System;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Xml;
  12. using System.Xml.Linq;
  13. namespace AppLovinMax.Scripts.IntegrationManager.Editor
  14. {
  15. public abstract class AppLovinPreProcess
  16. {
  17. private const string AppLovinDependenciesFileExportPath = "MaxSdk/AppLovin/Editor/Dependencies.xml";
  18. private const string ElementNameDependencies = "dependencies";
  19. private static readonly XmlWriterSettings DependenciesFileXmlWriterSettings = new XmlWriterSettings
  20. {
  21. Indent = true,
  22. IndentChars = " ",
  23. NewLineChars = "\n",
  24. NewLineHandling = NewLineHandling.Replace
  25. };
  26. protected static string AppLovinDependenciesFilePath
  27. {
  28. get { return AppLovinIntegrationManager.IsPluginInPackageManager ? Path.Combine("Assets", AppLovinDependenciesFileExportPath) : MaxSdkUtils.GetAssetPathForExportPath(AppLovinDependenciesFileExportPath); }
  29. }
  30. /// <summary>
  31. /// Gets the AppLovin Dependencies.xml file. If `createIfNotExists` is true, a new file will be created if one does not exist.
  32. /// </summary>
  33. /// <param name="path">The path to the AppLovin Dependencies.xml file</param>
  34. /// <param name="createIfNotExists">Whether to create a new Dependencies.xml file if one does not exist</param>
  35. /// <returns></returns>
  36. protected static XDocument GetAppLovinDependenciesFile(string path, bool createIfNotExists = false)
  37. {
  38. try
  39. {
  40. if (File.Exists(path))
  41. {
  42. return XDocument.Load(path);
  43. }
  44. if (createIfNotExists)
  45. {
  46. return new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
  47. new XElement(ElementNameDependencies));
  48. }
  49. }
  50. catch (Exception exception)
  51. {
  52. MaxSdkLogger.E("Unable to load Dependencies file due to exception: " + exception.Message);
  53. }
  54. return null;
  55. }
  56. /// <summary>
  57. /// Updates a dependency if it exists, otherwise adds a new dependency.
  58. /// </summary>
  59. /// <param name="dependenciesDocument">The dependencies document we are writing to</param>
  60. /// <param name="parentTag">The parent tag that we want to search for the dependency. For example, to add a new dependency to Android, pass in "androidPackages"</param>
  61. /// <param name="elementTag">The element we are looking to update/add. For example, to add a new dependency to Android, pass in "androidPackage"</param>
  62. /// <param name="matchAttribute">The attribute name we want in the dependency. For example, to add something to the spec attribute, pass in "spec" </param>
  63. /// <param name="matchValuePrefix">The attribute value prefix we are looking to replace. For example, "com.google.android.ump:user-messaging-platform"</param>
  64. /// <param name="newDependency">The new dependency we want to add.</param>
  65. protected static void AddOrUpdateDependency(
  66. XDocument dependenciesDocument,
  67. string parentTag,
  68. string elementTag,
  69. string matchAttribute,
  70. string matchValuePrefix,
  71. XElement newDependency)
  72. {
  73. var parentElement = dependenciesDocument.Root.Element(parentTag);
  74. if (parentElement == null)
  75. {
  76. parentElement = new XElement(parentTag);
  77. dependenciesDocument.Root.Add(parentElement);
  78. }
  79. // Check if a dependency exists that matches the attributes name and value
  80. var existingElement = parentElement.Elements(elementTag)
  81. .FirstOrDefault(element =>
  82. {
  83. var attr = element.Attribute(matchAttribute);
  84. return attr != null && attr.Value.StartsWith(matchValuePrefix, StringComparison.OrdinalIgnoreCase);
  85. });
  86. if (existingElement != null)
  87. {
  88. foreach (var attr in newDependency.Attributes())
  89. {
  90. existingElement.SetAttributeValue(attr.Name, attr.Value);
  91. }
  92. }
  93. else
  94. {
  95. parentElement.Add(newDependency);
  96. }
  97. }
  98. /// <summary>
  99. /// Removes a dependency from an xml file.
  100. /// </summary>
  101. /// <param name="doc">The xml file to remove a dependency from</param>
  102. /// <param name="parentTag">The parent tag that we want to search for the dependency to remove. For example: "androidPackages"</param>
  103. /// <param name="elementTag">The element we are looking to remove. For example: "androidPackage"</param>
  104. /// <param name="matchAttribute">The attribute name we want to remove. For example: "spec" </param>
  105. /// <param name="matchValuePrefix">The attribute value prefix we are looking to replace. For example: "com.google.android.ump:user-messaging-platform"</param>
  106. /// <returns>True if the dependency was removed successfully, otherwise return false.</returns>
  107. protected static bool RemoveDependency(
  108. XDocument doc,
  109. string parentTag,
  110. string elementTag,
  111. string matchAttribute,
  112. string matchValuePrefix)
  113. {
  114. var root = doc.Root;
  115. if (root == null) return false;
  116. var parentElement = root.Element(parentTag);
  117. if (parentElement == null) return false;
  118. XElement toRemove = null;
  119. foreach (var e in parentElement.Elements(elementTag))
  120. {
  121. var attr = e.Attribute(matchAttribute);
  122. if (attr != null && attr.Value.StartsWith(matchValuePrefix))
  123. {
  124. toRemove = e;
  125. break;
  126. }
  127. }
  128. if (toRemove == null) return false;
  129. toRemove.Remove();
  130. return true;
  131. }
  132. /// <summary>
  133. /// Saves an xml file.
  134. /// </summary>
  135. /// <param name="doc">The document to save</param>
  136. /// <param name="path">The path to the document to save</param>
  137. /// <returns>Returns true if the file was saved successfully</returns>
  138. protected static bool SaveDependenciesFile(XDocument doc, string path)
  139. {
  140. try
  141. {
  142. using (var xmlWriter = XmlWriter.Create(path, DependenciesFileXmlWriterSettings))
  143. {
  144. doc.Save(xmlWriter);
  145. return true;
  146. }
  147. }
  148. catch (Exception exception)
  149. {
  150. MaxSdkLogger.E("Unable to save Dependencies file due to exception: " + exception.Message);
  151. }
  152. return false;
  153. }
  154. }
  155. }