AddLil2cppSourceCodeToXcodeproj2022OrNewer.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using HybridCLR.Editor.Installer;
  2. using HybridCLR.Editor.Settings;
  3. using System.IO;
  4. using System.Text;
  5. using UnityEditor;
  6. using UnityEditor.Build;
  7. using UnityEditor.Callbacks;
  8. using UnityEngine;
  9. #if UNITY_2022_2_OR_NEWER && UNITY_IOS
  10. namespace HybridCLR.Editor.BuildProcessors
  11. {
  12. public static class AddLil2cppSourceCodeToXcodeproj2022OrNewer
  13. {
  14. //[MenuItem("HybridCLR/Modfiyxcode")]
  15. //public static void Modify()
  16. //{
  17. // OnPostProcessBuild(BuildTarget.iOS, $"{SettingsUtil.ProjectDir}/Build-iOS");
  18. //}
  19. [PostProcessBuild]
  20. public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
  21. {
  22. if (target != BuildTarget.iOS || !HybridCLRSettings.Instance.enable)
  23. return;
  24. string pbxprojFile = $"{pathToBuiltProject}/Unity-iPhone.xcodeproj/project.pbxproj";
  25. RemoveExternalLibil2cppOption(pbxprojFile);
  26. CopyLibil2cppToXcodeProj(pathToBuiltProject);
  27. }
  28. private static void RemoveExternalLibil2cppOption(string pbxprojFile)
  29. {
  30. string pbxprojContent = File.ReadAllText(pbxprojFile, Encoding.UTF8);
  31. string removeBuildOption = @"--external-lib-il2-cpp=\""$PROJECT_DIR/Libraries/libil2cpp.a\""";
  32. if (!pbxprojContent.Contains(removeBuildOption))
  33. {
  34. //throw new BuildFailedException("modified project.pbxproj fail");
  35. Debug.LogError("[AddLil2cppSourceCodeToXcodeproj] modified project.pbxproj fail");
  36. return;
  37. }
  38. pbxprojContent = pbxprojContent.Replace(removeBuildOption, "");
  39. File.WriteAllText(pbxprojFile, pbxprojContent, Encoding.UTF8);
  40. Debug.Log($"[AddLil2cppSourceCodeToXcodeproj] remove il2cpp build option '{removeBuildOption}' from file '{pbxprojFile}'");
  41. }
  42. private static void CopyLibil2cppToXcodeProj(string pathToBuiltProject)
  43. {
  44. string srcLibil2cppDir = $"{SettingsUtil.LocalIl2CppDir}/libil2cpp";
  45. string destLibil2cppDir = $"{pathToBuiltProject}/Il2CppOutputProject/IL2CPP/libil2cpp";
  46. BashUtil.RemoveDir(destLibil2cppDir);
  47. BashUtil.CopyDir(srcLibil2cppDir, destLibil2cppDir, true);
  48. }
  49. }
  50. }
  51. #endif