驱蚊app
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

106 行
4.5 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using UnityEditor;
  7. using UnityEngine;
  8. namespace AppleAuth.Editor
  9. {
  10. public static class AppleAuthMacosPostprocessorHelper
  11. {
  12. /// <summary>
  13. /// Use this script to change the bundle identifier of the plugin's library bundle to replace it with a personalized one for your product.
  14. /// This should avoid CFBundleIdentifier Collision errors when uploading the app to the macOS App Store
  15. /// </summary>
  16. /// <remarks>Basically this should replace the plugin's bundle identifier from "com.lupidan.MacOSAppleAuthManager" to "{your.project.application.identifier}.MacOSAppleAuthManager"</remarks>
  17. /// <param name="target">The current build target, so it's only executed when building for MacOS</param>
  18. /// <param name="path">The path of the built .app file</param>
  19. public static void FixManagerBundleIdentifier(BuildTarget target, string path)
  20. {
  21. if (target != BuildTarget.StandaloneOSX)
  22. {
  23. throw new Exception(GetMessage("FixManagerBundleIdentifier should only be called when building for macOS"));
  24. }
  25. try
  26. {
  27. var macosAppleAuthManagerInfoPlistPath = GetInfoPlistPath(path);
  28. var macosAppleAuthManagerInfoPlist = File.ReadAllText(macosAppleAuthManagerInfoPlistPath);
  29. var regex = new Regex(@"\<key\>CFBundleIdentifier\<\/key\>\s*\<string\>(com\.lupidan)\.MacOSAppleAuthManager\<\/string\>");
  30. var match = regex.Match(macosAppleAuthManagerInfoPlist);
  31. if (!match.Success)
  32. {
  33. throw new Exception(GetMessage("Can't locate CFBundleIdentifier in MacOSAppleAuthManager's Info.plist"));
  34. }
  35. var modifiedMacosAppleAuthManagerInfoPlist = macosAppleAuthManagerInfoPlist
  36. .Remove(match.Groups[1].Index, match.Groups[1].Length)
  37. .Insert(match.Groups[1].Index, PlayerSettings.applicationIdentifier);
  38. File.WriteAllText(macosAppleAuthManagerInfoPlistPath, modifiedMacosAppleAuthManagerInfoPlist);
  39. Debug.Log(GetMessage($"Renamed MacOSAppleAuthManager.bundle bundle identifier from \"com.lupidan.MacOSAppleAuthManager\" -> \"{PlayerSettings.applicationIdentifier}.MacOSAppleAuthManager\""));
  40. }
  41. catch (Exception exception)
  42. {
  43. throw new Exception(GetMessage(
  44. $"Error while fixing MacOSAppleAuthManager.bundle bundle identifier :: {exception.Message}"));
  45. }
  46. }
  47. private static string GetMessage(string message) => $"{nameof(AppleAuthMacosPostprocessorHelper)}: {message}";
  48. private static string GetInfoPlistPath(string path)
  49. {
  50. const string bundleName = "MacOSAppleAuthManager.bundle";
  51. var possibleRootPaths = new List<string>();
  52. if (Directory.Exists(path))
  53. {
  54. possibleRootPaths.Add(path);
  55. }
  56. if (Directory.Exists($"{path}.app"))
  57. {
  58. possibleRootPaths.Add($"{path}.app");
  59. }
  60. var bundleDirectories = possibleRootPaths
  61. .SelectMany(possibleRootPath => Directory.GetDirectories(
  62. possibleRootPath,
  63. bundleName,
  64. SearchOption.AllDirectories))
  65. .ToArray();
  66. if (bundleDirectories.Length == 0)
  67. {
  68. throw new Exception(GetMessage($"Can't locate any {bundleName}"));
  69. }
  70. if (bundleDirectories.Length > 1)
  71. {
  72. var allPaths = string.Join("\n", bundleDirectories);
  73. throw new Exception(GetMessage($"Located multiple {bundleName}!\n{allPaths}"));
  74. }
  75. var bundlePath = bundleDirectories[0];
  76. Debug.Log(GetMessage($"Located {bundleName} at {bundlePath}"));
  77. var infoPlistPath = Path.Combine(
  78. bundlePath,
  79. "Contents",
  80. "Info.plist");
  81. if (!File.Exists(infoPlistPath))
  82. {
  83. throw new Exception(GetMessage("Can't locate MacOSAppleAuthManager's Info.plist"));
  84. }
  85. Debug.Log(GetMessage($"Located Info.plist at {infoPlistPath}"));
  86. return infoPlistPath;
  87. }
  88. }
  89. }