2025-09-10 11:15:05 +08:00
using dnlib.DotNet.Writer ;
using Obfuz.EncryptionVM ;
2025-05-21 09:23:29 +08:00
using Obfuz.ObfusPasses ;
using Obfuz.ObfusPasses.CallObfus ;
using Obfuz.ObfusPasses.ConstEncrypt ;
2025-06-22 10:39:31 +08:00
using Obfuz.ObfusPasses.ControlFlowObfus ;
2025-06-21 10:59:39 +08:00
using Obfuz.ObfusPasses.EvalStackObfus ;
2025-05-21 09:23:29 +08:00
using Obfuz.ObfusPasses.ExprObfus ;
using Obfuz.ObfusPasses.FieldEncrypt ;
2025-08-01 09:19:24 +08:00
using Obfuz.ObfusPasses.RemoveConstField ;
2025-05-21 09:23:29 +08:00
using Obfuz.ObfusPasses.SymbolObfus ;
2025-08-02 21:50:26 +08:00
using Obfuz.ObfusPasses.Watermark ;
2025-05-21 09:23:29 +08:00
using Obfuz.Settings ;
2025-05-23 12:47:57 +08:00
using Obfuz.Utils ;
2025-05-21 09:23:29 +08:00
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using UnityEditor ;
2025-09-10 11:15:05 +08:00
using UnityEngine ;
2025-05-21 09:23:29 +08:00
namespace Obfuz
{
2025-05-21 21:29:26 +08:00
2025-05-23 12:47:57 +08:00
public class CoreSettingsFacade
{
public BuildTarget buildTarget ;
2025-09-10 11:15:05 +08:00
public RuntimeType targetRuntime ;
2025-05-23 12:47:57 +08:00
public byte [ ] defaultStaticSecretKey ;
public byte [ ] defaultDynamicSecretKey ;
public List < string > assembliesUsingDynamicSecretKeys ;
public int randomSeed ;
public string encryptionVmGenerationSecretKey ;
public int encryptionVmOpCodeCount ;
public string encryptionVmCodeFile ;
public List < string > assembliesToObfuscate ;
public List < string > nonObfuscatedButReferencingObfuscatedAssemblies ;
public List < string > assemblySearchPaths ;
public string obfuscatedAssemblyOutputPath ;
public string obfuscatedAssemblyTempOutputPath ;
public ObfuscationPassType enabledObfuscationPasses ;
public List < string > obfuscationPassRuleConfigFiles ;
public List < IObfuscationPass > obfuscationPasses ;
}
2025-05-21 09:23:29 +08:00
2025-05-23 12:47:57 +08:00
public class ObfuscatorBuilder
{
private CoreSettingsFacade _coreSettingsFacade ;
2025-05-21 09:23:29 +08:00
2025-05-23 12:47:57 +08:00
public CoreSettingsFacade CoreSettingsFacade = > _coreSettingsFacade ;
2025-05-21 09:23:29 +08:00
public void InsertTopPriorityAssemblySearchPaths ( List < string > assemblySearchPaths )
{
2025-05-23 12:47:57 +08:00
_coreSettingsFacade . assemblySearchPaths . InsertRange ( 0 , assemblySearchPaths ) ;
2025-05-21 09:23:29 +08:00
}
public ObfuscatorBuilder AddPass ( IObfuscationPass pass )
{
2025-05-23 12:47:57 +08:00
_coreSettingsFacade . obfuscationPasses . Add ( pass ) ;
2025-05-21 09:23:29 +08:00
return this ;
}
public Obfuscator Build ( )
{
return new Obfuscator ( this ) ;
}
2025-06-28 20:51:54 +08:00
public static List < string > BuildUnityAssemblySearchPaths ( bool searchPathIncludeUnityEditorDll = false )
2025-05-21 09:23:29 +08:00
{
string applicationContentsPath = EditorApplication . applicationContentsPath ;
2025-05-23 10:18:37 +08:00
var searchPaths = new List < string >
2025-05-21 09:23:29 +08:00
{
#if UNITY_2021_1_OR_NEWER
2025-05-23 10:18:37 +08:00
#if UNITY_STANDALONE_WIN || (UNITY_EDITOR_WIN && UNITY_SERVER) || UNITY_WSA || UNITY_LUMIN
"MonoBleedingEdge/lib/mono/unityaot-win32" ,
"MonoBleedingEdge/lib/mono/unityaot-win32/Facades" ,
#elif UNITY_STANDALONE_OSX || (UNITY_EDITOR_OSX && UNITY_SERVER) || UNITY_IOS || UNITY_TVOS
"MonoBleedingEdge/lib/mono/unityaot-macos" ,
"MonoBleedingEdge/lib/mono/unityaot-macos/Facades" ,
2025-05-21 09:23:29 +08:00
# else
2025-05-23 10:18:37 +08:00
"MonoBleedingEdge/lib/mono/unityaot-linux" ,
"MonoBleedingEdge/lib/mono/unityaot-linux/Facades" ,
2025-05-21 09:23:29 +08:00
# endif
2025-05-23 10:18:37 +08:00
# else
"MonoBleedingEdge/lib/mono/unityaot" ,
"MonoBleedingEdge/lib/mono/unityaot/Facades" ,
# endif
#if UNITY_STANDALONE_WIN || (UNITY_EDITOR_WIN && UNITY_SERVER)
2025-06-06 15:39:56 +08:00
"PlaybackEngines/windowsstandalonesupport/Variations/il2cpp/Managed" ,
2025-05-23 10:18:37 +08:00
#elif UNITY_STANDALONE_OSX || (UNITY_EDITOR_OSX && UNITY_SERVER)
2025-06-06 15:39:56 +08:00
"PlaybackEngines/MacStandaloneSupport/Variations/il2cpp/Managed" ,
2025-05-23 10:18:37 +08:00
#elif UNITY_STANDALONE_LINUX || (UNITY_EDITOR_LINUX && UNITY_SERVER)
2025-06-06 15:39:56 +08:00
"PlaybackEngines/LinuxStandaloneSupport/Variations/il2cpp/Managed" ,
2025-05-23 10:18:37 +08:00
#elif UNITY_ANDROID
2025-06-06 15:39:56 +08:00
"PlaybackEngines/AndroidPlayer/Variations/il2cpp/Managed" ,
2025-05-23 10:18:37 +08:00
#elif UNITY_IOS
2025-06-06 15:39:56 +08:00
"PlaybackEngines/iOSSupport/Variations/il2cpp/Managed" ,
2025-05-23 10:18:37 +08:00
#elif UNITY_MINIGAME || UNITY_WEIXINMINIGAME
2025-06-09 15:10:05 +08:00
#if TUANJIE_1_1_OR_NEWER
"PlaybackEngines/WeixinMiniGameSupport/Variations/il2cpp/nondevelopment/Data/Managed" ,
# else
"PlaybackEngines/WeixinMiniGameSupport/Variations/nondevelopment/Data/Managed" ,
# endif
2025-05-23 10:18:37 +08:00
#elif UNITY_OPENHARMONY
2025-06-06 15:39:56 +08:00
"PlaybackEngines/OpenHarmonyPlayer/Variations/il2cpp/Managed" ,
2025-06-09 12:45:46 +08:00
#elif UNITY_WEBGL
"PlaybackEngines/WebGLSupport/Variations/nondevelopment/Data/Managed" ,
2025-05-23 10:18:37 +08:00
#elif UNITY_TVOS
2025-06-09 08:53:12 +08:00
"PlaybackEngines/AppleTVSupport/Variations/il2cpp/Managed" ,
2025-05-23 10:18:37 +08:00
#elif UNITY_WSA
2025-06-06 15:39:56 +08:00
"PlaybackEngines/WSASupport/Variations/il2cpp/Managed" ,
2025-05-23 10:18:37 +08:00
#elif UNITY_LUMIN
2025-06-06 15:39:56 +08:00
"PlaybackEngines/LuminSupport/Variations/il2cpp/Managed" ,
2025-05-23 10:18:37 +08:00
# else
#error "Unsupported platform, please report to us"
# endif
2025-05-21 09:23:29 +08:00
} ;
2025-06-28 20:51:54 +08:00
if ( searchPathIncludeUnityEditorDll )
{
searchPaths . Add ( "Managed/UnityEngine" ) ;
}
2025-08-03 12:37:44 +08:00
var resultPaths = new List < string > ( ) ;
2025-06-06 15:39:56 +08:00
foreach ( var path in searchPaths )
{
string candidatePath1 = Path . Combine ( applicationContentsPath , path ) ;
if ( Directory . Exists ( candidatePath1 ) )
{
resultPaths . Add ( candidatePath1 ) ;
}
if ( path . StartsWith ( "PlaybackEngines" ) )
{
string candidatePath2 = Path . Combine ( Path . GetDirectoryName ( Path . GetDirectoryName ( applicationContentsPath ) ) , path ) ;
if ( Directory . Exists ( candidatePath2 ) )
{
resultPaths . Add ( candidatePath2 ) ;
}
}
}
return resultPaths ;
2025-05-21 09:23:29 +08:00
}
2025-06-28 20:51:54 +08:00
public static ObfuscatorBuilder FromObfuzSettings ( ObfuzSettings settings , BuildTarget target , bool searchPathIncludeUnityEditorInstallLocation , bool searchPathIncludeUnityEditorDll = false )
2025-05-21 09:23:29 +08:00
{
List < string > searchPaths = searchPathIncludeUnityEditorInstallLocation ?
2025-06-28 20:51:54 +08:00
BuildUnityAssemblySearchPaths ( searchPathIncludeUnityEditorDll ) . Concat ( settings . assemblySettings . additionalAssemblySearchPaths ) . ToList ( )
2025-05-21 09:23:29 +08:00
: settings . assemblySettings . additionalAssemblySearchPaths . ToList ( ) ;
2025-06-06 15:39:56 +08:00
foreach ( var path in searchPaths )
{
bool exists = Directory . Exists ( path ) ;
UnityEngine . Debug . Log ( $"search path:{path} exists:{exists}" ) ;
}
2025-09-10 11:15:05 +08:00
RuntimeType targetRuntime = settings . compatibilitySettings . targetRuntime ! = RuntimeType . ActivatedScriptingBackend ?
settings . compatibilitySettings . targetRuntime :
( PlatformUtil . IsMonoBackend ( ) ? RuntimeType . Mono : RuntimeType . IL2CPP ) ;
if ( searchPathIncludeUnityEditorDll & & targetRuntime ! = RuntimeType . Mono )
{
Debug . LogError ( $"obfuscate dll for editor, but ObfuzSettings.CompatibilitySettings.targetRuntime isn't RuntimeType.Mono. Use RuntimeType.Mono for targetRuntime automatically." ) ;
targetRuntime = RuntimeType . Mono ;
}
2025-05-21 09:23:29 +08:00
var builder = new ObfuscatorBuilder
{
2025-05-23 12:47:57 +08:00
_coreSettingsFacade = new CoreSettingsFacade ( )
{
buildTarget = target ,
2025-09-10 11:15:05 +08:00
targetRuntime = targetRuntime ,
2025-05-23 12:47:57 +08:00
defaultStaticSecretKey = KeyGenerator . GenerateKey ( settings . secretSettings . defaultStaticSecretKey , VirtualMachine . SecretKeyLength ) ,
defaultDynamicSecretKey = KeyGenerator . GenerateKey ( settings . secretSettings . defaultDynamicSecretKey , VirtualMachine . SecretKeyLength ) ,
assembliesUsingDynamicSecretKeys = settings . secretSettings . assembliesUsingDynamicSecretKeys . ToList ( ) ,
randomSeed = settings . secretSettings . randomSeed ,
encryptionVmGenerationSecretKey = settings . encryptionVMSettings . codeGenerationSecretKey ,
encryptionVmOpCodeCount = settings . encryptionVMSettings . encryptionOpCodeCount ,
encryptionVmCodeFile = settings . encryptionVMSettings . codeOutputPath ,
assembliesToObfuscate = settings . assemblySettings . GetAssembliesToObfuscate ( ) ,
nonObfuscatedButReferencingObfuscatedAssemblies = settings . assemblySettings . nonObfuscatedButReferencingObfuscatedAssemblies . ToList ( ) ,
assemblySearchPaths = searchPaths ,
obfuscatedAssemblyOutputPath = settings . GetObfuscatedAssemblyOutputPath ( target ) ,
obfuscatedAssemblyTempOutputPath = settings . GetObfuscatedAssemblyTempOutputPath ( target ) ,
enabledObfuscationPasses = settings . obfuscationPassSettings . enabledPasses ,
2025-06-21 08:27:45 +08:00
obfuscationPassRuleConfigFiles = settings . obfuscationPassSettings . ruleFiles ? . ToList ( ) ? ? new List < string > ( ) ,
2025-05-23 12:47:57 +08:00
obfuscationPasses = new List < IObfuscationPass > ( ) ,
} ,
2025-05-21 09:23:29 +08:00
} ;
ObfuscationPassType obfuscationPasses = settings . obfuscationPassSettings . enabledPasses ;
2025-08-27 20:15:20 +08:00
if ( obfuscationPasses . HasFlag ( ObfuscationPassType . SymbolObfus ) & & settings . symbolObfusSettings . detectReflectionCompatibility )
{
builder . AddPass ( new ReflectionCompatibilityDetectionPass ( settings . symbolObfusSettings . ToFacade ( ) ) ) ;
}
2025-05-21 09:23:29 +08:00
if ( obfuscationPasses . HasFlag ( ObfuscationPassType . ConstEncrypt ) )
{
2025-05-23 12:47:57 +08:00
builder . AddPass ( new ConstEncryptPass ( settings . constEncryptSettings . ToFacade ( ) ) ) ;
2025-05-21 09:23:29 +08:00
}
2025-08-01 09:19:24 +08:00
if ( obfuscationPasses . HasFlag ( ObfuscationPassType . RemoveConstField ) )
{
builder . AddPass ( new RemoveConstFieldPass ( settings . removeConstFieldSettings . ToFacade ( ) ) ) ;
}
2025-06-21 11:43:04 +08:00
if ( obfuscationPasses . HasFlag ( ObfuscationPassType . ExprObfus ) )
{
builder . AddPass ( new ExprObfusPass ( settings . exprObfusSettings . ToFacade ( ) ) ) ;
}
2025-08-07 21:32:00 +08:00
//if (obfuscationPasses.HasFlag(ObfuscationPassType.EvalStackObfus))
//{
// builder.AddPass(new EvalStackObfusPass(settings.evalStackObfusSettings.ToFacade()));
//}
2025-05-21 09:23:29 +08:00
if ( obfuscationPasses . HasFlag ( ObfuscationPassType . FieldEncrypt ) )
{
2025-05-23 12:47:57 +08:00
builder . AddPass ( new FieldEncryptPass ( settings . fieldEncryptSettings . ToFacade ( ) ) ) ;
2025-05-21 09:23:29 +08:00
}
2025-06-17 20:21:28 +08:00
if ( obfuscationPasses . HasFlag ( ObfuscationPassType . CallObfus ) )
2025-05-21 09:23:29 +08:00
{
2025-06-17 20:21:28 +08:00
builder . AddPass ( new CallObfusPass ( settings . callObfusSettings . ToFacade ( ) ) ) ;
2025-05-21 09:23:29 +08:00
}
2025-06-22 10:39:31 +08:00
if ( obfuscationPasses . HasFlag ( ObfuscationPassType . ControlFlowObfus ) )
{
2025-06-28 19:01:57 +08:00
builder . AddPass ( new ControlFlowObfusPass ( settings . controlFlowObfusSettings . ToFacade ( ) ) ) ;
2025-06-22 10:39:31 +08:00
}
2025-08-02 21:50:26 +08:00
if ( obfuscationPasses . HasFlag ( ObfuscationPassType . WaterMark ) )
{
builder . AddPass ( new WatermarkPass ( settings . watermarkSettings . ToFacade ( ) ) ) ;
}
2025-05-21 09:23:29 +08:00
if ( obfuscationPasses . HasFlag ( ObfuscationPassType . SymbolObfus ) )
{
2025-05-23 12:47:57 +08:00
builder . AddPass ( new SymbolObfusPass ( settings . symbolObfusSettings . ToFacade ( ) ) ) ;
2025-05-21 09:23:29 +08:00
}
return builder ;
}
}
}