2023-06-15 09:57:15 +08:00
using HybridCLR.Editor.Installer ;
2023-10-11 19:10:20 +08:00
using HybridCLR.Editor.Settings ;
2023-06-15 09:57:15 +08:00
using System.IO ;
using System.Text ;
using UnityEditor ;
using UnityEditor.Build ;
using UnityEditor.Callbacks ;
using UnityEngine ;
2024-06-11 00:16:39 +08:00
#if UNITY_2022 && (UNITY_IOS || UNITY_TVOS || UNITY_VISIONOS)
2023-06-15 09:57:15 +08:00
2023-10-09 15:39:32 +08:00
namespace HybridCLR.Editor.BuildProcessors
2023-06-15 09:57:15 +08:00
{
public static class AddLil2cppSourceCodeToXcodeproj2022OrNewer
{
[PostProcessBuild]
public static void OnPostProcessBuild ( BuildTarget target , string pathToBuiltProject )
{
2024-04-21 13:45:47 +08:00
if ( ! HybridCLRSettings . Instance . enable )
2023-06-15 09:57:15 +08:00
return ;
2024-05-29 14:48:05 +08:00
string pbxprojFile = BuildProcessorUtil . GetXcodeProjectFile ( pathToBuiltProject ) ;
2023-06-15 09:57:15 +08:00
RemoveExternalLibil2cppOption ( pbxprojFile ) ;
CopyLibil2cppToXcodeProj ( pathToBuiltProject ) ;
}
private static void RemoveExternalLibil2cppOption ( string pbxprojFile )
{
string pbxprojContent = File . ReadAllText ( pbxprojFile , Encoding . UTF8 ) ;
string removeBuildOption = @"--external-lib-il2-cpp=\""$PROJECT_DIR/Libraries/libil2cpp.a\""" ;
2024-05-14 21:59:56 +08:00
if ( pbxprojContent . Contains ( removeBuildOption ) )
2023-06-15 09:57:15 +08:00
{
2024-05-14 21:59:56 +08:00
pbxprojContent = pbxprojContent . Replace ( removeBuildOption , "" ) ;
Debug . Log ( $"[AddLil2cppSourceCodeToXcodeproj] remove il2cpp build option '{removeBuildOption}' from file '{pbxprojFile}'" ) ;
}
else
{
Debug . LogWarning ( $"[AddLil2cppSourceCodeToXcodeproj] project.pbxproj remove building option:'{removeBuildOption}' fail. This may occur when 'Append' to existing xcode project in building" ) ;
2023-06-15 09:57:15 +08:00
}
2024-05-14 21:59:56 +08:00
int strShellScriptIndex1 = pbxprojContent . IndexOf ( "/* ShellScript */," ) ;
int strShellScriptIndex2 = pbxprojContent . IndexOf ( "/* ShellScript */," , strShellScriptIndex1 + 10 ) ;
if ( strShellScriptIndex2 > = 0 )
{
pbxprojContent = pbxprojContent . Remove ( strShellScriptIndex1 , strShellScriptIndex2 - strShellScriptIndex1 ) ;
Debug . LogWarning ( $"[AddLil2cppSourceCodeToXcodeproj] remove duplicated '/* ShellScript */' from file '{pbxprojFile}'" ) ;
}
2023-06-15 09:57:15 +08:00
File . WriteAllText ( pbxprojFile , pbxprojContent , Encoding . UTF8 ) ;
}
private static void CopyLibil2cppToXcodeProj ( string pathToBuiltProject )
{
string srcLibil2cppDir = $"{SettingsUtil.LocalIl2CppDir}/libil2cpp" ;
string destLibil2cppDir = $"{pathToBuiltProject}/Il2CppOutputProject/IL2CPP/libil2cpp" ;
BashUtil . RemoveDir ( destLibil2cppDir ) ;
BashUtil . CopyDir ( srcLibil2cppDir , destLibil2cppDir , true ) ;
}
}
}
# endif