Geen omschrijving
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.

PListProcessor.cs 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. using GoogleMobileAds.Editor;
  24. public static class PListProcessor
  25. {
  26. private const string KEY_SK_ADNETWORK_ITEMS = "SKAdNetworkItems";
  27. private const string KEY_SK_ADNETWORK_ID = "SKAdNetworkIdentifier";
  28. private const string SKADNETWORKS_RELATIVE_PATH = "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. {
  33. string plistPath = Path.Combine(path, "Info.plist");
  34. PlistDocument plist = new PlistDocument();
  35. plist.ReadFromFile(plistPath);
  36. GoogleMobileAdsSettings instance = GoogleMobileAdsSettings.LoadInstance();
  37. string appId = instance.GoogleMobileAdsIOSAppId;
  38. if (appId.Length == 0)
  39. {
  40. NotifyBuildFailure(
  41. "iOS Google Mobile Ads app ID is empty. Please enter a valid app ID to run ads properly.");
  42. }
  43. else
  44. {
  45. plist.root.SetString("GADApplicationIdentifier", appId);
  46. }
  47. string userTrackingDescription = instance.UserTrackingUsageDescription;
  48. if (!string.IsNullOrEmpty(userTrackingDescription))
  49. {
  50. plist.root.SetString("NSUserTrackingUsageDescription", userTrackingDescription);
  51. }
  52. List<string> skNetworkIds = ReadSKAdNetworkIdentifiersFromXML();
  53. if (skNetworkIds.Count > 0)
  54. {
  55. AddSKAdNetworkIdentifier(plist, skNetworkIds);
  56. }
  57. string unityVersion = Application.unityVersion;
  58. if (!string.IsNullOrEmpty(unityVersion))
  59. {
  60. plist.root.SetString("GADUUnityVersion", unityVersion);
  61. }
  62. File.WriteAllText(plistPath, plist.WriteToString());
  63. }
  64. private static PlistElementArray GetSKAdNetworkItemsArray(PlistDocument document)
  65. {
  66. PlistElementArray array;
  67. if (document.root.values.ContainsKey(KEY_SK_ADNETWORK_ITEMS))
  68. {
  69. try
  70. {
  71. PlistElement element;
  72. document.root.values.TryGetValue(KEY_SK_ADNETWORK_ITEMS, out element);
  73. array = element.AsArray();
  74. }
  75. #pragma warning disable 0168
  76. catch (Exception e)
  77. #pragma warning restore 0168
  78. {
  79. // The element is not an array type.
  80. array = null;
  81. }
  82. }
  83. else
  84. {
  85. array = document.root.CreateArray(KEY_SK_ADNETWORK_ITEMS);
  86. }
  87. return array;
  88. }
  89. private static List<string> ReadSKAdNetworkIdentifiersFromXML()
  90. {
  91. List<string> skAdNetworkItems = new List<string>();
  92. string path = Path.Combine(Application.dataPath, SKADNETWORKS_RELATIVE_PATH);
  93. /*
  94. * Handle importing GMA via Unity Package Manager.
  95. */
  96. EditorPathUtils pathUtils = ScriptableObject.CreateInstance<EditorPathUtils>();
  97. if (pathUtils.IsPackageRootPath())
  98. {
  99. string parentDirectoryPath = pathUtils.GetDirectoryAssetPath();
  100. path = Path.Combine(parentDirectoryPath, SKADNETWORKS_FILE_NAME);
  101. }
  102. try
  103. {
  104. if (!File.Exists(path))
  105. {
  106. throw new FileNotFoundException();
  107. }
  108. using (FileStream fs = File.OpenRead(path))
  109. {
  110. XmlDocument document = new XmlDocument();
  111. document.Load(fs);
  112. XmlNode root = document.FirstChild;
  113. XmlNodeList nodes = root.SelectNodes(KEY_SK_ADNETWORK_ID);
  114. foreach (XmlNode node in nodes)
  115. {
  116. skAdNetworkItems.Add(node.InnerText);
  117. }
  118. }
  119. }
  120. #pragma warning disable 0168
  121. catch (FileNotFoundException e)
  122. #pragma warning restore 0168
  123. {
  124. NotifyBuildFailure("GoogleMobileAdsSKAdNetworkItems.xml not found", false);
  125. }
  126. catch (IOException e)
  127. {
  128. NotifyBuildFailure("Failed to read GoogleMobileAdsSKAdNetworkIds.xml: " + e.Message, false);
  129. }
  130. return skAdNetworkItems;
  131. }
  132. private static void AddSKAdNetworkIdentifier(PlistDocument document, List<string> skAdNetworkIds)
  133. {
  134. PlistElementArray array = GetSKAdNetworkItemsArray(document);
  135. if (array != null)
  136. {
  137. foreach (string id in skAdNetworkIds)
  138. {
  139. if (!ContainsSKAdNetworkIdentifier(array, id))
  140. {
  141. PlistElementDict added = array.AddDict();
  142. added.SetString(KEY_SK_ADNETWORK_ID, id);
  143. }
  144. }
  145. }
  146. else
  147. {
  148. NotifyBuildFailure("SKAdNetworkItems element already exists in Info.plist, but is not an array.", false);
  149. }
  150. }
  151. private static bool ContainsSKAdNetworkIdentifier(PlistElementArray skAdNetworkItemsArray, string id)
  152. {
  153. foreach (PlistElement elem in skAdNetworkItemsArray.values)
  154. {
  155. try
  156. {
  157. PlistElementDict elemInDict = elem.AsDict();
  158. PlistElement value;
  159. bool identifierExists = elemInDict.values.TryGetValue(KEY_SK_ADNETWORK_ID, out value);
  160. if (identifierExists && value.AsString().Equals(id))
  161. {
  162. return true;
  163. }
  164. }
  165. #pragma warning disable 0168
  166. catch (Exception e)
  167. #pragma warning restore 0168
  168. {
  169. // Do nothing
  170. }
  171. }
  172. return false;
  173. }
  174. private static void NotifyBuildFailure(string message, bool showOpenSettingsButton = true)
  175. {
  176. string dialogTitle = "Google Mobile Ads";
  177. string dialogMessage = "Error: " + message;
  178. if (showOpenSettingsButton)
  179. {
  180. bool openSettings = EditorUtility.DisplayDialog(
  181. dialogTitle, dialogMessage, "Open Settings", "Close");
  182. if (openSettings)
  183. {
  184. GoogleMobileAdsSettingsEditor.OpenInspector();
  185. }
  186. }
  187. else
  188. {
  189. EditorUtility.DisplayDialog(dialogTitle, dialogMessage, "Close");
  190. }
  191. ThrowBuildException("[GoogleMobileAds] " + message);
  192. }
  193. private static void ThrowBuildException(string message)
  194. {
  195. #if UNITY_2017_1_OR_NEWER
  196. throw new BuildPlayerWindow.BuildMethodException(message);
  197. #else
  198. throw new OperationCanceledException(message);
  199. #endif
  200. }
  201. }
  202. #endif