PListProcessor.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright (C) 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #if UNITY_IPHONE || UNITY_IOS
  15. using System;
  16. using System.Collections.Generic;
  17. using System.IO;
  18. using System.Xml;
  19. using UnityEditor;
  20. using UnityEditor.Callbacks;
  21. using UnityEditor.iOS.Xcode;
  22. using UnityEngine;
  23. namespace GoogleMobileAds.Editor {
  24. public static class PListProcessor {
  25. private const string KEY_SK_ADNETWORK_ITEMS = "SKAdNetworkItems";
  26. private const string KEY_SK_ADNETWORK_ID = "SKAdNetworkIdentifier";
  27. private const string SKADNETWORKS_RELATIVE_PATH =
  28. "GoogleMobileAds/Editor/GoogleMobileAdsSKAdNetworkItems.xml";
  29. private const string SKADNETWORKS_FILE_NAME = "GoogleMobileAdsSKAdNetworkItems.xml";
  30. [PostProcessBuild]
  31. public static void OnPostProcessBuild(BuildTarget buildTarget, string path) {
  32. string plistPath = Path.Combine(path, "Info.plist");
  33. PlistDocument plist = new PlistDocument();
  34. plist.ReadFromFile(plistPath);
  35. GoogleMobileAdsSettings instance = GoogleMobileAdsSettings.LoadInstance();
  36. string appId = instance.GoogleMobileAdsIOSAppId.Trim();
  37. if (appId.Length == 0) {
  38. NotifyBuildFailure(
  39. "iOS Google Mobile Ads app ID is empty. Please enter a valid app ID to run ads properly.");
  40. } else {
  41. plist.root.SetString("GADApplicationIdentifier", appId);
  42. }
  43. string userTrackingDescription = instance.UserTrackingUsageDescription;
  44. if (!string.IsNullOrEmpty(userTrackingDescription)) {
  45. plist.root.SetString("NSUserTrackingUsageDescription", userTrackingDescription);
  46. }
  47. List<string> skNetworkIds = ReadSKAdNetworkIdentifiersFromXML();
  48. if (skNetworkIds.Count > 0) {
  49. AddSKAdNetworkIdentifier(plist, skNetworkIds);
  50. }
  51. string unityVersion = Application.unityVersion;
  52. if (!string.IsNullOrEmpty(unityVersion)) {
  53. plist.root.SetString("GADUUnityVersion", unityVersion);
  54. }
  55. File.WriteAllText(plistPath, plist.WriteToString());
  56. }
  57. private static PlistElementArray GetSKAdNetworkItemsArray(PlistDocument document) {
  58. PlistElementArray array;
  59. if (document.root.values.ContainsKey(KEY_SK_ADNETWORK_ITEMS)) {
  60. try {
  61. PlistElement element;
  62. document.root.values.TryGetValue(KEY_SK_ADNETWORK_ITEMS, out element);
  63. array = element.AsArray();
  64. }
  65. #pragma warning disable 0168
  66. catch (Exception e)
  67. #pragma warning restore 0168
  68. {
  69. // The element is not an array type.
  70. array = null;
  71. }
  72. } else {
  73. array = document.root.CreateArray(KEY_SK_ADNETWORK_ITEMS);
  74. }
  75. return array;
  76. }
  77. private static List<string> ReadSKAdNetworkIdentifiersFromXML() {
  78. List<string> skAdNetworkItems = new List<string>();
  79. string path = Path.Combine(Application.dataPath, SKADNETWORKS_RELATIVE_PATH);
  80. /*
  81. * Handle importing GMA via Unity Package Manager.
  82. */
  83. EditorPathUtils pathUtils = ScriptableObject.CreateInstance<EditorPathUtils>();
  84. if (pathUtils.IsPackageRootPath()) {
  85. string parentDirectoryPath = pathUtils.GetDirectoryAssetPath();
  86. path = Path.Combine(parentDirectoryPath, SKADNETWORKS_FILE_NAME);
  87. }
  88. try {
  89. if (!File.Exists(path)) {
  90. throw new FileNotFoundException();
  91. }
  92. using (FileStream fs = File.OpenRead(path)) {
  93. XmlDocument document = new XmlDocument();
  94. document.Load(fs);
  95. XmlNode root = document.FirstChild;
  96. XmlNodeList nodes = root.SelectNodes(KEY_SK_ADNETWORK_ID);
  97. foreach (XmlNode node in nodes) {
  98. skAdNetworkItems.Add(node.InnerText);
  99. }
  100. }
  101. }
  102. #pragma warning disable 0168
  103. catch (FileNotFoundException e)
  104. #pragma warning restore 0168
  105. {
  106. NotifyBuildFailure("GoogleMobileAdsSKAdNetworkItems.xml not found", false);
  107. } catch (IOException e) {
  108. NotifyBuildFailure("Failed to read GoogleMobileAdsSKAdNetworkIds.xml: " + e.Message, false);
  109. }
  110. return skAdNetworkItems;
  111. }
  112. private static void AddSKAdNetworkIdentifier(PlistDocument document,
  113. List<string> skAdNetworkIds) {
  114. PlistElementArray array = GetSKAdNetworkItemsArray(document);
  115. if (array != null) {
  116. foreach (string id in skAdNetworkIds) {
  117. if (!ContainsSKAdNetworkIdentifier(array, id)) {
  118. PlistElementDict added = array.AddDict();
  119. added.SetString(KEY_SK_ADNETWORK_ID, id);
  120. }
  121. }
  122. } else {
  123. NotifyBuildFailure(
  124. "SKAdNetworkItems element already exists in Info.plist, but is not an array.", false);
  125. }
  126. }
  127. private static bool ContainsSKAdNetworkIdentifier(PlistElementArray skAdNetworkItemsArray,
  128. string id) {
  129. foreach (PlistElement elem in skAdNetworkItemsArray.values) {
  130. try {
  131. PlistElementDict elemInDict = elem.AsDict();
  132. PlistElement value;
  133. bool identifierExists = elemInDict.values.TryGetValue(KEY_SK_ADNETWORK_ID, out value);
  134. if (identifierExists && value.AsString().Equals(id)) {
  135. return true;
  136. }
  137. }
  138. #pragma warning disable 0168
  139. catch (Exception e)
  140. #pragma warning restore 0168
  141. {
  142. // Do nothing
  143. }
  144. }
  145. return false;
  146. }
  147. private static void NotifyBuildFailure(string message, bool showOpenSettingsButton = true) {
  148. string dialogTitle = "Google Mobile Ads";
  149. string dialogMessage = "Error: " + message;
  150. if (showOpenSettingsButton) {
  151. bool openSettings =
  152. EditorUtility.DisplayDialog(dialogTitle, dialogMessage, "Open Settings", "Close");
  153. if (openSettings) {
  154. GoogleMobileAdsSettingsEditor.OpenInspector();
  155. }
  156. } else {
  157. EditorUtility.DisplayDialog(dialogTitle, dialogMessage, "Close");
  158. }
  159. ThrowBuildException("[GoogleMobileAds] " + message);
  160. }
  161. private static void ThrowBuildException(string message) {
  162. #if UNITY_2017_1_OR_NEWER
  163. throw new BuildPlayerWindow.BuildMethodException(message);
  164. #else
  165. throw new OperationCanceledException(message);
  166. #endif
  167. }
  168. }
  169. }
  170. #endif