AddLil2cppSourceCodeToXcodeproj2022OrNewer.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using HybridCLR.Editor.Installer;
  2. using HybridCLR.Editor.Settings;
  3. using System.IO;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using UnityEditor;
  7. using UnityEditor.Build;
  8. using UnityEditor.Callbacks;
  9. using UnityEngine;
  10. #if UNITY_2022 && (UNITY_IOS || UNITY_TVOS || UNITY_VISIONOS)
  11. namespace HybridCLR.Editor.BuildProcessors
  12. {
  13. public static class AddLil2cppSourceCodeToXcodeproj2022OrNewer
  14. {
  15. [PostProcessBuild]
  16. public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
  17. {
  18. if (!HybridCLRSettings.Instance.enable)
  19. return;
  20. string pbxprojFile = BuildProcessorUtil.GetXcodeProjectFile(pathToBuiltProject);
  21. RemoveExternalLibil2cppOption(pbxprojFile);
  22. CopyLibil2cppToXcodeProj(pathToBuiltProject);
  23. }
  24. private static string TryRemoveDunplicateShellScriptSegment(string pbxprojFile, string pbxprojContent)
  25. {
  26. // will appear duplicated Shell Script segment when append to existed xcode project.
  27. // This is unity bug.
  28. // we remove duplicated Shell Script to avoid build error.
  29. string copyFileComment = @"/\* CopyFiles \*/,\s+([A-Z0-9]{24}) /\* ShellScript \*/,\s+([A-Z0-9]{24}) /\* ShellScript \*/,";
  30. var m = Regex.Match(pbxprojContent, copyFileComment, RegexOptions.Multiline);
  31. if (!m.Success)
  32. {
  33. return pbxprojContent;
  34. }
  35. if (m.Groups[1].Value != m.Groups[2].Value)
  36. {
  37. throw new BuildFailedException($"find invalid /* ShellScript */ segment");
  38. }
  39. int startIndexOfDupShellScript = m.Groups[2].Index;
  40. int endIndexOfDupShellScript = pbxprojContent.IndexOf(",", startIndexOfDupShellScript);
  41. pbxprojContent = pbxprojContent.Remove(startIndexOfDupShellScript, endIndexOfDupShellScript + 1 - startIndexOfDupShellScript);
  42. Debug.LogWarning($"[AddLil2cppSourceCodeToXcodeproj] remove duplicated '/* ShellScript */' from file '{pbxprojFile}'");
  43. return pbxprojContent;
  44. }
  45. private static void RemoveExternalLibil2cppOption(string pbxprojFile)
  46. {
  47. string pbxprojContent = File.ReadAllText(pbxprojFile, Encoding.UTF8);
  48. string removeBuildOption = @"--external-lib-il2-cpp=\""$PROJECT_DIR/Libraries/libil2cpp.a\""";
  49. if (pbxprojContent.Contains(removeBuildOption))
  50. {
  51. pbxprojContent = pbxprojContent.Replace(removeBuildOption, "");
  52. Debug.Log($"[AddLil2cppSourceCodeToXcodeproj] remove il2cpp build option '{removeBuildOption}' from file '{pbxprojFile}'");
  53. }
  54. else
  55. {
  56. Debug.LogWarning($"[AddLil2cppSourceCodeToXcodeproj] project.pbxproj remove building option:'{removeBuildOption}' fail. This may occur when 'Append' to existing xcode project in building");
  57. }
  58. pbxprojContent = TryRemoveDunplicateShellScriptSegment(pbxprojFile, pbxprojContent);
  59. File.WriteAllText(pbxprojFile, pbxprojContent, Encoding.UTF8);
  60. }
  61. private static void CopyLibil2cppToXcodeProj(string pathToBuiltProject)
  62. {
  63. string srcLibil2cppDir = $"{SettingsUtil.LocalIl2CppDir}/libil2cpp";
  64. string destLibil2cppDir = $"{pathToBuiltProject}/Il2CppOutputProject/IL2CPP/libil2cpp";
  65. BashUtil.RemoveDir(destLibil2cppDir);
  66. BashUtil.CopyDir(srcLibil2cppDir, destLibil2cppDir, true);
  67. }
  68. }
  69. }
  70. #endif