From d2587382625339e04e262c4a091ebd6dcb1d5f37 Mon Sep 17 00:00:00 2001 From: walon Date: Wed, 21 May 2025 13:33:03 +0800 Subject: [PATCH] temp --- .../Editor/ObfuscationMethodWhitelist.cs | 13 ++-- .../Editor/Utils/MetaUtil.cs | 63 ++++++++++++++----- .../Runtime/ObfuzIgnoreAttribute.cs | 40 ++++++++++++ 3 files changed, 95 insertions(+), 21 deletions(-) diff --git a/com.code-philosophy.obfuz/Editor/ObfuscationMethodWhitelist.cs b/com.code-philosophy.obfuz/Editor/ObfuscationMethodWhitelist.cs index 87ca94f..a712604 100644 --- a/com.code-philosophy.obfuz/Editor/ObfuscationMethodWhitelist.cs +++ b/com.code-philosophy.obfuz/Editor/ObfuscationMethodWhitelist.cs @@ -11,9 +11,14 @@ namespace Obfuz { public class ObfuscationMethodWhitelist { - private bool ShouldBeIgnoredByCustomAttribute(IHasCustomAttribute obj) + private bool HasObfuzIgnoreScope(IHasCustomAttribute obj, ObfuzScope targetScope) { - return MetaUtil.HasObfuzIgnoreAttribute(obj); + ObfuzScope? objScope = MetaUtil.GetObfuzIgnoreScope(obj); + if (objScope == null) + { + return false; + } + return (objScope & targetScope) != 0; } public bool IsInWhiteList(ModuleDef module) @@ -23,7 +28,7 @@ namespace Obfuz { return true; } - if (ShouldBeIgnoredByCustomAttribute(module)) + if (HasObfuzIgnoreScope(module, ObfuzScope.Self)) { return true; } @@ -40,7 +45,7 @@ namespace Obfuz { return true; } - if (ShouldBeIgnoredByCustomAttribute(method)) + if (HasObfuzIgnoreScope(method, ObfuzScope.Self)) { return true; } diff --git a/com.code-philosophy.obfuz/Editor/Utils/MetaUtil.cs b/com.code-philosophy.obfuz/Editor/Utils/MetaUtil.cs index 711e957..154f714 100644 --- a/com.code-philosophy.obfuz/Editor/Utils/MetaUtil.cs +++ b/com.code-philosophy.obfuz/Editor/Utils/MetaUtil.cs @@ -779,11 +779,40 @@ namespace Obfuz.Utils return result.ToString(); } - public static bool HasObfuzIgnoreAttribute(IHasCustomAttribute obj) + public static ObfuzScope? GetObfuzIgnoreScope(IHasCustomAttribute obj) { - return obj.CustomAttributes.Any(ca => ca.AttributeType.FullName == "Obfuz.ObfuzIgnoreAttribute"); + var ca = obj.CustomAttributes.FirstOrDefault(ca => ca.AttributeType.FullName == "Obfuz.ObfuzIgnoreAttribute"); + if (ca == null) + { + return null; + } + var scope = (ObfuzScope)ca.ConstructorArguments[0].Value; + return scope; } + public static ObfuzScope? GetObfuzIgnoreScopeOfSelfOrDeclaringType(TypeDef typeDef) + { + TypeDef cur = typeDef; + while (true) + { + ObfuzScope? scope = GetObfuzIgnoreScope(cur); + if (scope != null) + { + return scope; + } + cur = cur.DeclaringType; + if (cur == null) + { + return null; + } + } + } + + //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"); @@ -794,20 +823,20 @@ namespace Obfuz.Utils return obj.CustomAttributes.Any(ca => ca.AttributeType.FullName == "Obfuz.EncryptFieldAttribute"); } - public static bool HasObfuzIgnoreAttributeInSelfOrParent(TypeDef typeDef) - { - while (true) - { - if (HasObfuzIgnoreAttribute(typeDef)) - { - return true; - } - typeDef = typeDef.DeclaringType; - if (typeDef == null) - { - return false; - } - } - } + //public static bool HasObfuzIgnoreAttributeInSelfOrParent(TypeDef typeDef) + //{ + // while (true) + // { + // if (HasObfuzIgnoreAttribute(typeDef)) + // { + // return true; + // } + // typeDef = typeDef.DeclaringType; + // if (typeDef == null) + // { + // return false; + // } + // } + //} } } diff --git a/com.code-philosophy.obfuz/Runtime/ObfuzIgnoreAttribute.cs b/com.code-philosophy.obfuz/Runtime/ObfuzIgnoreAttribute.cs index 90d2b17..4736ce2 100644 --- a/com.code-philosophy.obfuz/Runtime/ObfuzIgnoreAttribute.cs +++ b/com.code-philosophy.obfuz/Runtime/ObfuzIgnoreAttribute.cs @@ -6,8 +6,48 @@ using System.Threading.Tasks; namespace Obfuz { + public enum ObfuzScope + { + None = 0x0, + Self = 0x1, + Field = 0x2, + MethodName = 0x4, + MethodParameter = 0x8, + MethodBody = 0x10, + Method = MethodName | MethodParameter | MethodBody, + PropertyName = 020, + PropertyGetter = 0x40, + PropertySetter = 0x80, + Property = PropertyName | PropertyGetter | PropertySetter, + EventName = 0x100, + EventAdd = 0x200, + EventRemove = 0x400, + EventFire = 0x800, + Event = EventName | EventAdd | EventRemove, + + NestedTypeSelf = 0x1000, + NestedTypeField = 0x2000, + NestedTypeMethod = 0x4000, + NestedTypeProperty = 0x8000, + NestedTypeEvent = 0x10000, + + NestedTypeAll = NestedTypeSelf | NestedTypeField | NestedTypeMethod | NestedTypeProperty | NestedTypeEvent, + + Member = Field | Method | Property | Event, + MemberAndNestedTypeSelf = Member | NestedTypeSelf, + MemberAndNestedTypeAll = Member | NestedTypeAll, + SelfAndNestedTypeSelf = Self | NestedTypeSelf, + All = Self | MemberAndNestedTypeAll, + } + [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)] public class ObfuzIgnoreAttribute : Attribute { + public ObfuzScope Scope { get; set; } + + public ObfuzIgnoreAttribute(ObfuzScope scope = ObfuzScope.All) + { + this.Scope = scope; + } } }