AndroidManifestHelper.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Android NetworkDiscovery Multicast fix
  2. // https://github.com/vis2k/Mirror/pull/2887
  3. using UnityEditor;
  4. using UnityEngine;
  5. using UnityEditor.Build;
  6. using UnityEditor.Build.Reporting;
  7. using System.Xml;
  8. using System.IO;
  9. #if UNITY_ANDROID
  10. using UnityEditor.Android;
  11. #endif
  12. [InitializeOnLoad]
  13. public class AndroidManifestHelper : IPreprocessBuildWithReport, IPostprocessBuildWithReport
  14. #if UNITY_ANDROID
  15. , IPostGenerateGradleAndroidProject
  16. #endif
  17. {
  18. public int callbackOrder { get { return 99999; } }
  19. #if UNITY_ANDROID
  20. public void OnPostGenerateGradleAndroidProject(string path)
  21. {
  22. string manifestFolder = Path.Combine(path, "src/main");
  23. string sourceFile = manifestFolder + "/AndroidManifest.xml";
  24. // Load android manifest file
  25. XmlDocument doc = new XmlDocument();
  26. doc.Load(sourceFile);
  27. string androidNamepsaceURI;
  28. XmlElement element = (XmlElement)doc.SelectSingleNode("/manifest");
  29. if (element == null)
  30. {
  31. UnityEngine.Debug.LogError("Could not find manifest tag in android manifest.");
  32. return;
  33. }
  34. // Get android namespace URI from the manifest
  35. androidNamepsaceURI = element.GetAttribute("xmlns:android");
  36. if (string.IsNullOrEmpty(androidNamepsaceURI))
  37. {
  38. UnityEngine.Debug.LogError("Could not find Android Namespace in manifest.");
  39. return;
  40. }
  41. AddOrRemoveTag(doc,
  42. androidNamepsaceURI,
  43. "/manifest",
  44. "uses-permission",
  45. "android.permission.CHANGE_WIFI_MULTICAST_STATE",
  46. true,
  47. false);
  48. AddOrRemoveTag(doc,
  49. androidNamepsaceURI,
  50. "/manifest",
  51. "uses-permission",
  52. "android.permission.INTERNET",
  53. true,
  54. false);
  55. doc.Save(sourceFile);
  56. }
  57. #endif
  58. static void AddOrRemoveTag(XmlDocument doc, string @namespace, string path, string elementName, string name, bool required, bool modifyIfFound, params string[] attrs) // name, value pairs
  59. {
  60. var nodes = doc.SelectNodes(path + "/" + elementName);
  61. XmlElement element = null;
  62. foreach (XmlElement e in nodes)
  63. {
  64. if (name == null || name == e.GetAttribute("name", @namespace))
  65. {
  66. element = e;
  67. break;
  68. }
  69. }
  70. if (required)
  71. {
  72. if (element == null)
  73. {
  74. var parent = doc.SelectSingleNode(path);
  75. element = doc.CreateElement(elementName);
  76. element.SetAttribute("name", @namespace, name);
  77. parent.AppendChild(element);
  78. }
  79. for (int i = 0; i < attrs.Length; i += 2)
  80. {
  81. if (modifyIfFound || string.IsNullOrEmpty(element.GetAttribute(attrs[i], @namespace)))
  82. {
  83. if (attrs[i + 1] != null)
  84. {
  85. element.SetAttribute(attrs[i], @namespace, attrs[i + 1]);
  86. }
  87. else
  88. {
  89. element.RemoveAttribute(attrs[i], @namespace);
  90. }
  91. }
  92. }
  93. }
  94. else
  95. {
  96. if (element != null && modifyIfFound)
  97. {
  98. element.ParentNode.RemoveChild(element);
  99. }
  100. }
  101. }
  102. public void OnPostprocessBuild(BuildReport report) {}
  103. public void OnPreprocessBuild(BuildReport report) {}
  104. }