Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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