diff --git a/Editor/NotObfuscatedMethodWhiteList.cs b/Editor/NotObfuscatedMethodWhiteList.cs index 612d293..98e5186 100644 --- a/Editor/NotObfuscatedMethodWhiteList.cs +++ b/Editor/NotObfuscatedMethodWhiteList.cs @@ -1,4 +1,5 @@ using dnlib.DotNet; +using Obfuz.Utils; using System; using System.Collections.Generic; using System.Linq; @@ -9,6 +10,10 @@ namespace Obfuz { public class NotObfuscatedMethodWhiteList { + private bool ShouldBeIgnoredByCustomAttribute(IHasCustomAttribute obj) + { + return MetaUtil.HasObfuzIgnoreAttribute(obj) || MetaUtil.HasCompilerGeneratedAttribute(obj); + } public bool IsInWhiteList(ModuleDef module) { @@ -17,6 +22,10 @@ namespace Obfuz { return true; } + if (ShouldBeIgnoredByCustomAttribute(module)) + { + return true; + } return false; } @@ -26,6 +35,10 @@ namespace Obfuz { return true; } + if (ShouldBeIgnoredByCustomAttribute(method)) + { + return true; + } return false; } @@ -39,6 +52,10 @@ namespace Obfuz { return true; } + if (ShouldBeIgnoredByCustomAttribute(type)) + { + return true; + } if (type.FullName == "Obfuz.EncryptionVM.GeneratedEncryptionVirtualMachine") { return true; diff --git a/Editor/ObfusPasses/FieldEncrypt/FieldEncryptPass.cs b/Editor/ObfusPasses/FieldEncrypt/FieldEncryptPass.cs index 2f2cbc6..04aece0 100644 --- a/Editor/ObfusPasses/FieldEncrypt/FieldEncryptPass.cs +++ b/Editor/ObfusPasses/FieldEncrypt/FieldEncryptPass.cs @@ -25,7 +25,7 @@ namespace Obfuz.ObfusPasses.FieldEncrypt _encryptionLevel = settings.encryptionLevel; } - protected override bool NeedProcessNotObfuscatedAssembly => true; + protected override bool ForceProcessAllAssembliesAndIgnoreAllPolicy => true; public override void Start() { diff --git a/Editor/ObfusPasses/InstructionObfuscationPassBase.cs b/Editor/ObfusPasses/InstructionObfuscationPassBase.cs index 0fb72ec..064f58b 100644 --- a/Editor/ObfusPasses/InstructionObfuscationPassBase.cs +++ b/Editor/ObfusPasses/InstructionObfuscationPassBase.cs @@ -7,33 +7,33 @@ namespace Obfuz.ObfusPasses { public abstract class InstructionObfuscationPassBase : ObfuscationPassBase { - protected virtual bool NeedProcessNotObfuscatedAssembly => false; + protected virtual bool ForceProcessAllAssembliesAndIgnoreAllPolicy => false; protected abstract bool NeedObfuscateMethod(MethodDef method); public override void Process() { var ctx = ObfuscationPassContext.Current; - var modules = NeedProcessNotObfuscatedAssembly ? ctx.obfuscatedAndNotObfuscatedModules : ctx.toObfuscatedModules; + var modules = ForceProcessAllAssembliesAndIgnoreAllPolicy ? ctx.obfuscatedAndNotObfuscatedModules : ctx.toObfuscatedModules; NotObfuscatedMethodWhiteList whiteList = ctx.whiteList; ConfigurablePassPolicy passPolicy = ctx.passPolicy; foreach (ModuleDef mod in modules) { - if (whiteList.IsInWhiteList(mod) || !Support(passPolicy.GetAssemblyObfuscationPasses(mod))) + if (!ForceProcessAllAssembliesAndIgnoreAllPolicy && (whiteList.IsInWhiteList(mod) || !Support(passPolicy.GetAssemblyObfuscationPasses(mod)))) { continue; } // ToArray to avoid modify list exception foreach (TypeDef type in mod.GetTypes().ToArray()) { - if (whiteList.IsInWhiteList(type) || !Support(passPolicy.GetTypeObfuscationPasses(type))) + if (!ForceProcessAllAssembliesAndIgnoreAllPolicy && (whiteList.IsInWhiteList(type) || !Support(passPolicy.GetTypeObfuscationPasses(type)))) { continue; } // ToArray to avoid modify list exception foreach (MethodDef method in type.Methods.ToArray()) { - if (!method.HasBody || ctx.whiteList.IsInWhiteList(method) || !Support(passPolicy.GetMethodObfuscationPasses(method)) || !NeedObfuscateMethod(method)) + if (!method.HasBody || (!ForceProcessAllAssembliesAndIgnoreAllPolicy && (ctx.whiteList.IsInWhiteList(method) || !Support(passPolicy.GetMethodObfuscationPasses(method)) || !NeedObfuscateMethod(method)))) { continue; } diff --git a/Editor/Utils/MetaUtil.cs b/Editor/Utils/MetaUtil.cs index f61ab97..c77ff86 100644 --- a/Editor/Utils/MetaUtil.cs +++ b/Editor/Utils/MetaUtil.cs @@ -778,5 +778,15 @@ namespace Obfuz.Utils result.Append(")"); return result.ToString(); } + + public static bool HasObfuzIgnoreAttribute(IHasCustomAttribute obj) + { + return obj.CustomAttributes.Any(ca => ca.AttributeType.FullName == "Obfuz.ObfuzIgnoreAttribute"); + } + + public static bool HasCompilerGeneratedAttribute(IHasCustomAttribute obj) + { + return obj.CustomAttributes.Any(ca => ca.AttributeType.FullName == "System.Runtime.CompilerServices.CompilerGeneratedAttribute"); + } } } diff --git a/Runtime/ObfuzIgnoreAttribute.cs b/Runtime/ObfuzIgnoreAttribute.cs new file mode 100644 index 0000000..90d2b17 --- /dev/null +++ b/Runtime/ObfuzIgnoreAttribute.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Obfuz +{ + [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)] + public class ObfuzIgnoreAttribute : Attribute + { + } +}