2026-10-08 21:01:52 +08:00
// Copyright 2026 Code Philosophy
/ /
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
/ /
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
/ /
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
2025-12-13 12:48:22 +08:00
#if UNITY_6000_3_OR_NEWER && UNITY_EDITOR_OSX
#define NEW_IL2CPP_PATH
# endif
2026-10-08 21:01:52 +08:00
using HybridCLR.Settings ;
using HybridCLR.Utils ;
2022-09-22 08:56:07 +08:00
using System ;
using System.Collections.Generic ;
using System.IO ;
2026-10-08 21:01:52 +08:00
using System.Linq ;
2022-09-22 08:56:07 +08:00
using System.Text ;
using UnityEditor ;
using UnityEngine ;
using Debug = UnityEngine . Debug ;
2026-10-08 21:01:52 +08:00
namespace HybridCLR.Installer
2022-09-22 08:56:07 +08:00
{
2023-01-26 13:11:38 +08:00
public class InstallerController
2022-09-22 08:56:07 +08:00
{
2022-10-25 17:26:21 +08:00
private const string hybridclr_repo_path = "hybridclr_repo" ;
2022-12-06 11:52:47 +08:00
2022-10-25 17:26:21 +08:00
private const string il2cpp_plus_repo_path = "il2cpp_plus_repo" ;
2022-09-22 08:56:07 +08:00
2026-10-08 21:01:52 +08:00
public int MajorVersion = > _curVersion . major ;
2023-01-26 13:11:38 +08:00
private readonly UnityVersion _curVersion ;
private readonly HybridclrVersionInfo _curDefaultVersion ;
2022-09-22 08:56:07 +08:00
2023-06-16 18:18:41 +08:00
public string PackageVersion { get ; private set ; }
2023-10-15 12:44:22 +08:00
public string InstalledLibil2cppVersion { get ; private set ; }
2022-09-22 08:56:07 +08:00
public InstallerController ( )
{
2026-10-08 21:01:52 +08:00
_curVersion = UnityVersion . ParseUnityVersion ( Application . unityVersion ) ;
HybridclrVersionManifest versionManifest = GetHybridCLRVersionManifest ( ) ;
_curDefaultVersion = FindMatchedVersion ( versionManifest . versions , _curVersion ) ;
2023-06-16 18:18:41 +08:00
PackageVersion = LoadPackageInfo ( ) . version ;
2023-10-15 12:44:22 +08:00
InstalledLibil2cppVersion = ReadLocalVersion ( ) ;
2023-01-26 13:11:38 +08:00
}
private HybridclrVersionManifest GetHybridCLRVersionManifest ( )
{
string versionFile = $"{SettingsUtil.ProjectDir}/{SettingsUtil.HybridCLRDataPathInPackage}/hybridclr_version.json" ;
return JsonUtility . FromJson < HybridclrVersionManifest > ( File . ReadAllText ( versionFile , Encoding . UTF8 ) ) ;
}
2023-06-16 18:18:41 +08:00
private PackageInfo LoadPackageInfo ( )
{
string packageJson = $"{SettingsUtil.ProjectDir}/Packages/{SettingsUtil.PackageName}/package.json" ;
return JsonUtility . FromJson < PackageInfo > ( File . ReadAllText ( packageJson , Encoding . UTF8 ) ) ;
}
2026-10-08 21:01:52 +08:00
private HybridclrVersionInfo FindMatchedVersion ( List < HybridclrVersionInfo > versions , UnityVersion curVer )
{
HybridclrVersionInfo bestMatch = null ;
foreach ( var v in versions )
{
string [ ] versionWithEngineParts = v . unity_version . Split ( '-' ) ;
bool isTuanjieVersion = versionWithEngineParts . Length > = 2 & & versionWithEngineParts [ 1 ] = = "tuanjie" ;
if ( curVer . isTuanjieEngine ! = isTuanjieVersion )
{
continue ;
}
string [ ] versionParts = versionWithEngineParts [ 0 ] . Split ( '.' ) ;
if ( int . Parse ( versionParts [ 0 ] ) = = curVer . major & & ( versionParts . Length < 2 | | int . Parse ( versionParts [ 1 ] ) = = curVer . minor1 ) )
{
bestMatch = v ;
}
}
if ( bestMatch = = null )
{
string compatibleVersions = string . Join ( ", " , versions . Select ( v = > v . unity_version ) ) ;
throw new NotSupportedException ( $"No compatible version found for Unity {curVer.major}.{curVer.minor1}.{curVer.minor2}, please use compatible version of Unity: {compatibleVersions}" ) ;
}
UnityEngine . Debug . Log ( $"bestMatch: {bestMatch.unity_version} {bestMatch.il2cpp_plus.tag} {bestMatch.hybridclr.tag} " ) ;
return bestMatch ;
}
2023-06-16 18:18:41 +08:00
[Serializable]
class PackageInfo
{
public string name ;
public string version ;
}
2023-01-26 13:11:38 +08:00
[Serializable]
class VersionDesc
{
2026-10-08 21:01:52 +08:00
public string tag ;
2023-01-26 13:11:38 +08:00
2023-02-22 10:43:30 +08:00
//public string hash;
2023-01-26 13:11:38 +08:00
}
[Serializable]
class HybridclrVersionInfo
{
public string unity_version ;
public VersionDesc hybridclr ;
public VersionDesc il2cpp_plus ;
2022-12-06 11:52:47 +08:00
}
2023-01-26 13:11:38 +08:00
[Serializable]
class HybridclrVersionManifest
{
public List < HybridclrVersionInfo > versions ;
}
2022-09-22 08:56:07 +08:00
2026-10-08 21:01:52 +08:00
public string HybridclrLocalVersion = > _curDefaultVersion ? . hybridclr ? . tag ;
2023-09-17 23:30:09 +08:00
2026-10-08 21:01:52 +08:00
public string Il2cppPlusLocalVersion = > _curDefaultVersion ? . il2cpp_plus ? . tag ;
2022-12-06 18:57:48 +08:00
2025-12-13 12:48:22 +08:00
public string ApplicationIl2cppPath
2022-12-06 11:52:47 +08:00
{
2025-12-13 12:48:22 +08:00
get
{
Debug . Log ( $"application path:{EditorApplication.applicationPath} {EditorApplication.applicationContentsPath}" ) ;
#if NEW_IL2CPP_PATH
#if UNITY_IOS
string platformDirName = "iOSSupport" ;
#elif UNITY_TVOS
string platformDirName = "AppleTVSupport" ;
#elif UNITY_VISIONOS
string platformDirName = "VisionOSPlayer" ;
# else
string platformDirName = "iOSSupport" ;
# endif
return $"{EditorApplication.applicationContentsPath}/../../PlaybackEngines/{platformDirName}/il2cpp" ;
# else
return $"{EditorApplication.applicationContentsPath}/il2cpp" ;
2026-10-08 21:01:52 +08:00
# endif
2025-12-13 12:48:22 +08:00
}
2022-09-22 08:56:07 +08:00
}
2023-10-15 12:44:22 +08:00
public string LocalVersionFile = > $"{SettingsUtil.LocalIl2CppDir}/libil2cpp/hybridclr/generated/libil2cpp-version.txt" ;
private string ReadLocalVersion ( )
{
if ( ! File . Exists ( LocalVersionFile ) )
{
return null ;
}
return File . ReadAllText ( LocalVersionFile , Encoding . UTF8 ) ;
}
public void WriteLocalVersion ( )
{
InstalledLibil2cppVersion = PackageVersion ;
2026-10-08 21:01:52 +08:00
Directory . CreateDirectory ( Path . GetDirectoryName ( LocalVersionFile ) ) ;
2023-10-15 12:44:22 +08:00
File . WriteAllText ( LocalVersionFile , PackageVersion , Encoding . UTF8 ) ;
2023-11-28 21:01:33 +08:00
Debug . Log ( $"Write installed version:'{PackageVersion}' to {LocalVersionFile}" ) ;
2023-10-15 12:44:22 +08:00
}
2023-01-26 13:11:38 +08:00
public void InstallDefaultHybridCLR ( )
2022-09-22 08:56:07 +08:00
{
2023-04-29 12:41:47 +08:00
InstallFromLocal ( PrepareLibil2cppWithHybridclrFromGitRepo ( ) ) ;
2022-09-22 08:56:07 +08:00
}
2022-12-06 11:52:47 +08:00
public bool HasInstalledHybridCLR ( )
2022-09-22 08:56:07 +08:00
{
2022-12-06 11:52:47 +08:00
return Directory . Exists ( $"{SettingsUtil.LocalIl2CppDir}/libil2cpp/hybridclr" ) ;
2022-09-22 08:56:07 +08:00
}
2023-02-22 10:43:30 +08:00
void CloneBranch ( string workDir , string repoUrl , string branch , string repoDir )
2023-01-26 13:11:38 +08:00
{
2026-10-08 21:01:52 +08:00
DirectoryUtil . RemoveDir ( repoDir ) ;
BashUtil . RunCommand ( workDir , "git" , new string [ ] { "clone" , "-b" , branch , "--depth" , "1" , repoUrl , repoDir } ) ;
2023-01-26 13:11:38 +08:00
}
2023-04-29 12:11:16 +08:00
private string PrepareLibil2cppWithHybridclrFromGitRepo ( )
2022-09-22 08:56:07 +08:00
{
string workDir = SettingsUtil . HybridCLRDataDir ;
Directory . CreateDirectory ( workDir ) ;
//BashUtil.RecreateDir(workDir);
// clone hybridclr
2026-10-08 21:01:52 +08:00
string hybridclrRepoURL = HybridCLRSettings . instance . hybridclrRepoURL ;
2022-10-25 17:26:21 +08:00
string hybridclrRepoDir = $"{workDir}/{hybridclr_repo_path}" ;
2026-10-08 21:01:52 +08:00
CloneBranch ( workDir , hybridclrRepoURL , _curDefaultVersion . hybridclr . tag , hybridclrRepoDir ) ;
2022-09-22 08:56:07 +08:00
2023-04-29 12:11:16 +08:00
if ( ! Directory . Exists ( hybridclrRepoDir ) )
{
throw new Exception ( $"clone hybridclr fail. url: {hybridclrRepoURL}" ) ;
}
2022-09-22 08:56:07 +08:00
// clone il2cpp_plus
2026-10-08 21:01:52 +08:00
string il2cppPlusRepoURL = HybridCLRSettings . instance . il2cppPlusRepoURL ;
2022-10-25 17:26:21 +08:00
string il2cppPlusRepoDir = $"{workDir}/{il2cpp_plus_repo_path}" ;
2026-10-08 21:01:52 +08:00
CloneBranch ( workDir , il2cppPlusRepoURL , _curDefaultVersion . il2cpp_plus . tag , il2cppPlusRepoDir ) ;
2022-09-22 08:56:07 +08:00
2023-04-29 12:11:16 +08:00
if ( ! Directory . Exists ( il2cppPlusRepoDir ) )
{
throw new Exception ( $"clone il2cpp_plus fail. url: {il2cppPlusRepoDir}" ) ;
}
Directory . Move ( $"{hybridclrRepoDir}/hybridclr" , $"{il2cppPlusRepoDir}/libil2cpp/hybridclr" ) ;
return $"{il2cppPlusRepoDir}/libil2cpp" ;
}
2023-04-29 12:41:47 +08:00
public void InstallFromLocal ( string libil2cppWithHybridclrSourceDir )
{
2026-10-08 21:01:52 +08:00
RunInitLocalIl2CppData ( ApplicationIl2cppPath , libil2cppWithHybridclrSourceDir ) ;
2023-04-29 12:41:47 +08:00
}
2026-10-08 21:01:52 +08:00
private void RunInitLocalIl2CppData ( string editorIl2cppPath , string libil2cppWithHybridclrSourceDir )
2023-04-29 12:11:16 +08:00
{
string workDir = SettingsUtil . HybridCLRDataDir ;
Directory . CreateDirectory ( workDir ) ;
2022-09-22 08:56:07 +08:00
// create LocalIl2Cpp
string localUnityDataDir = SettingsUtil . LocalUnityDataDir ;
2026-10-08 21:01:52 +08:00
DirectoryUtil . RecreateDir ( localUnityDataDir ) ;
2025-12-13 12:48:22 +08:00
#if !NEW_IL2CPP_PATH
2022-09-22 08:56:07 +08:00
// copy MonoBleedingEdge
2026-10-08 21:01:52 +08:00
DirectoryUtil . CopyDir ( $"{Directory.GetParent(editorIl2cppPath)}/MonoBleedingEdge" , $"{localUnityDataDir}/MonoBleedingEdge" , true ) ;
2025-12-13 12:48:22 +08:00
# endif
2022-09-22 08:56:07 +08:00
// copy il2cpp
2026-10-08 21:01:52 +08:00
DirectoryUtil . CopyDir ( editorIl2cppPath , SettingsUtil . LocalIl2CppDir , true ) ;
2025-12-13 12:48:22 +08:00
#if NEW_IL2CPP_PATH
string buildDir = $"{SettingsUtil.LocalIl2CppDir}/build" ;
if ( RuntimeInformation . ProcessArchitecture = = Architecture . Arm | | RuntimeInformation . ProcessArchitecture = = Architecture . Arm64 )
{
2026-10-08 21:01:52 +08:00
DirectoryUtil . CopyDir ( $"{buildDir}/deploy_arm64" , $"{buildDir}/deploy" , false ) ;
2025-12-13 12:48:22 +08:00
}
else
{
2026-10-08 21:01:52 +08:00
DirectoryUtil . CopyDir ( $"{buildDir}/deploy_x86_64" , $"{buildDir}/deploy" , false ) ;
2025-12-13 12:48:22 +08:00
}
# endif
2022-09-22 08:56:07 +08:00
// replace libil2cpp
string dstLibil2cppDir = $"{SettingsUtil.LocalIl2CppDir}/libil2cpp" ;
2026-10-08 21:01:52 +08:00
DirectoryUtil . CopyDir ( $"{libil2cppWithHybridclrSourceDir}" , dstLibil2cppDir , true ) ;
2022-09-22 08:56:07 +08:00
// clean Il2cppBuildCache
2026-10-08 21:01:52 +08:00
DirectoryUtil . RemoveDir ( $"{SettingsUtil.ProjectDir}/Library/Il2cppBuildCache" , true ) ;
2022-09-22 08:56:07 +08:00
if ( HasInstalledHybridCLR ( ) )
{
2023-10-15 12:44:22 +08:00
WriteLocalVersion ( ) ;
2023-07-11 12:27:11 +08:00
Debug . Log ( "Install Sucessfully" ) ;
2022-09-22 08:56:07 +08:00
}
else
{
2023-07-11 12:27:11 +08:00
Debug . LogError ( "Installation failed!" ) ;
2022-09-22 08:56:07 +08:00
}
}
}
}