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.
2026-10-12 12:08:19 +08:00
using HybridCLR.Settings ;
2023-10-09 15:39:32 +08:00
using System ;
2024-05-24 18:32:44 +08:00
using System.IO ;
using System.Text.RegularExpressions ;
2023-05-06 12:28:03 +08:00
using UnityEditor ;
2022-09-22 08:56:07 +08:00
using UnityEditor.Build ;
using UnityEditor.Build.Reporting ;
using UnityEngine ;
2026-10-08 21:01:52 +08:00
namespace HybridCLR.BuildProcessors
2022-09-22 08:56:07 +08:00
{
internal class CheckSettings : IPreprocessBuildWithReport
{
public int callbackOrder = > 0 ;
2024-05-24 18:32:44 +08:00
public static bool DisableMethodBridgeDevelopmentFlagChecking { get ; set ; }
2022-09-22 08:56:07 +08:00
public void OnPreprocessBuild ( BuildReport report )
{
2022-10-09 20:53:13 +08:00
HybridCLRSettings globalSettings = SettingsUtil . HybridCLRSettings ;
if ( ! globalSettings . enable | | globalSettings . useGlobalIl2cpp )
2022-09-22 08:56:07 +08:00
{
string oldIl2cppPath = Environment . GetEnvironmentVariable ( "UNITY_IL2CPP_PATH" ) ;
if ( ! string . IsNullOrEmpty ( oldIl2cppPath ) )
{
Environment . SetEnvironmentVariable ( "UNITY_IL2CPP_PATH" , "" ) ;
2023-08-30 11:07:12 +08:00
Debug . Log ( $"[CheckSettings] clean process environment variable: UNITY_IL2CPP_PATH, old vlaue:'{oldIl2cppPath}'" ) ;
2022-09-22 08:56:07 +08:00
}
}
else
{
string curIl2cppPath = Environment . GetEnvironmentVariable ( "UNITY_IL2CPP_PATH" ) ;
if ( curIl2cppPath ! = SettingsUtil . LocalIl2CppDir )
{
Environment . SetEnvironmentVariable ( "UNITY_IL2CPP_PATH" , SettingsUtil . LocalIl2CppDir ) ;
2023-08-30 11:07:12 +08:00
Debug . Log ( $"[CheckSettings] UNITY_IL2CPP_PATH old value:'{curIl2cppPath}', new value:'{SettingsUtil.LocalIl2CppDir}'" ) ;
2022-09-22 08:56:07 +08:00
}
}
2022-10-09 16:13:23 +08:00
if ( ! globalSettings . enable )
2022-09-22 08:56:07 +08:00
{
return ;
}
2023-09-05 16:47:28 +08:00
BuildTargetGroup buildTargetGroup = BuildPipeline . GetBuildTargetGroup ( EditorUserBuildSettings . activeBuildTarget ) ;
2025-12-13 14:33:22 +08:00
#if UNITY_6000_0_OR_NEWER
NamedBuildTarget namedBuildTarget = NamedBuildTarget . FromBuildTargetGroup ( buildTargetGroup ) ;
ScriptingImplementation curScriptingImplementation = PlayerSettings . GetScriptingBackend ( namedBuildTarget ) ;
# else
2023-05-06 12:28:03 +08:00
ScriptingImplementation curScriptingImplementation = PlayerSettings . GetScriptingBackend ( buildTargetGroup ) ;
2025-12-13 14:33:22 +08:00
# endif
2023-05-06 12:28:03 +08:00
ScriptingImplementation targetScriptingImplementation = ScriptingImplementation . IL2CPP ;
if ( curScriptingImplementation ! = targetScriptingImplementation )
{
2023-08-30 11:07:12 +08:00
Debug . LogError ( $"[CheckSettings] current ScriptingBackend:{curScriptingImplementation}, have been switched to:{targetScriptingImplementation} automatically" ) ;
2025-12-13 14:33:22 +08:00
#if UNITY_6000_0_OR_NEWER
PlayerSettings . SetScriptingBackend ( namedBuildTarget , targetScriptingImplementation ) ;
# else
2023-05-06 12:28:03 +08:00
PlayerSettings . SetScriptingBackend ( buildTargetGroup , targetScriptingImplementation ) ;
2025-12-13 14:33:22 +08:00
# endif
2023-05-06 12:28:03 +08:00
}
2022-09-22 08:56:07 +08:00
var installer = new Installer . InstallerController ( ) ;
if ( ! installer . HasInstalledHybridCLR ( ) )
{
2023-08-30 11:07:12 +08:00
throw new BuildFailedException ( $"You have not initialized HybridCLR, please install it via menu 'HybridCLR/Installer'" ) ;
2022-09-22 08:56:07 +08:00
}
2023-10-15 12:44:22 +08:00
if ( installer . PackageVersion ! = installer . InstalledLibil2cppVersion )
{
throw new BuildFailedException ( $"You must run `HybridCLR/Installer` after upgrading package" ) ;
}
2022-10-09 20:53:13 +08:00
HybridCLRSettings gs = SettingsUtil . HybridCLRSettings ;
2022-09-22 08:56:07 +08:00
if ( ( ( gs . hotUpdateAssemblies ? . Length + gs . hotUpdateAssemblyDefinitions ? . Length ) ? ? 0 ) = = 0 )
{
2023-08-30 11:07:12 +08:00
Debug . LogWarning ( "[CheckSettings] No hot update modules configured in HybridCLRSettings" ) ;
2022-09-22 08:56:07 +08:00
}
2024-05-24 18:32:44 +08:00
if ( ! DisableMethodBridgeDevelopmentFlagChecking )
{
2026-10-08 21:01:52 +08:00
string methodBridgeFile = $"{SettingsUtil.GeneratedCppDir}/MethodBridge.inc" ;
2024-05-24 18:32:44 +08:00
var match = Regex . Match ( File . ReadAllText ( methodBridgeFile ) , @"// DEVELOPMENT=(\d)" ) ;
if ( match . Success )
{
int developmentFlagInMethodBridge = int . Parse ( match . Groups [ 1 ] . Value ) ;
int developmentFlagInEditorSettings = EditorUserBuildSettings . development ? 1 : 0 ;
if ( developmentFlagInMethodBridge ! = developmentFlagInEditorSettings )
{
2026-10-08 21:01:52 +08:00
Debug . LogError ( $"[CheckSettings] MethodBridge.inc DEVELOPMENT flag:{developmentFlagInMethodBridge} is inconsistent with EditorUserBuildSettings.development:{developmentFlagInEditorSettings}. Please run 'HybridCLR/Generate/All' before building." ) ;
2024-05-24 18:32:44 +08:00
}
}
else
{
2026-10-08 21:01:52 +08:00
Debug . LogError ( "[CheckSettings] MethodBridge.inc DEVELOPMENT flag not found. Please run 'HybridCLR/Generate/All' before building." ) ;
2024-05-24 18:32:44 +08:00
}
}
2022-09-22 08:56:07 +08:00
}
}
}