From ceb92fba4034ea0b5c2c4fb47b23898a52058716 Mon Sep 17 00:00:00 2001 From: walon Date: Wed, 28 May 2025 08:42:57 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=A6=E5=8F=B7=E6=B7=B7=E6=B7=86=E7=9A=84?= =?UTF-8?q?=E8=A7=84=E5=88=99=E6=96=87=E4=BB=B6=E4=B8=ADtype=E8=A7=84?= =?UTF-8?q?=E5=88=99=E6=96=B0=E5=A2=9EhasCustomAttributes=E5=B1=9E?= =?UTF-8?q?=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Policies/ConfigurableRenamePolicy.cs | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/Policies/ConfigurableRenamePolicy.cs b/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/Policies/ConfigurableRenamePolicy.cs index d8656cc..95ecb8e 100644 --- a/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/Policies/ConfigurableRenamePolicy.cs +++ b/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/Policies/ConfigurableRenamePolicy.cs @@ -59,6 +59,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies public ModifierType? modifierType; public ClassType? classType; public List inheritTypes; + public List hasCustomAttributes; public bool? obfuscateName; public ObfuzScope? applyToMembers; public bool applyToNestedTypes; @@ -199,7 +200,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies return scope; } - private List ParseInheritTypes(string inheritStr) + private List ParseTypes(string inheritStr) { if (string.IsNullOrWhiteSpace(inheritStr)) { @@ -227,7 +228,8 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies rule.applyToNestedTypes = ConfigUtil.ParseNullableBool(element.GetAttribute("applyToNestedTypes")) ?? true; rule.modifierType = ParseModifierType(element.GetAttribute("modifier")); rule.classType = ParseClassType(element.GetAttribute("classType")); - rule.inheritTypes = ParseInheritTypes(element.GetAttribute("inherit")); + rule.inheritTypes = ParseTypes(element.GetAttribute("inherit")); + rule.hasCustomAttributes = ParseTypes(element.GetAttribute("hasCustomAttributes")); //rule.nestTypeRuleSpecs = new List(); rule.fields = new List(); @@ -694,13 +696,33 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies return false; } + private bool MatchCustomAttributes(List customAttributes, TypeDef typeDef) + { + if (customAttributes == null || customAttributes.Count == 0) + { + return true; + } + foreach (string customAttributeName in customAttributes) + { + if (typeDef.CustomAttributes.Find(customAttributeName) != null) + { + return true; + } + } + return false; + } + private IEnumerable GetMatchTypes(ModuleDef mod, List types, TypeRuleSpec typeSpec) { if (typeSpec.nameMatcher.IsWildcardPattern) { foreach (var typeDef in types) { - if (!typeSpec.nameMatcher.IsMatch(typeDef.FullName) || !MatchModifier(typeSpec.modifierType, typeDef) || !MatchClassType(typeSpec.classType, typeDef) || !MatchInheritTypes(typeSpec.inheritTypes, typeDef)) + if (!typeSpec.nameMatcher.IsMatch(typeDef.FullName) + || !MatchModifier(typeSpec.modifierType, typeDef) + || !MatchClassType(typeSpec.classType, typeDef) + || !MatchInheritTypes(typeSpec.inheritTypes, typeDef) + || !MatchCustomAttributes(typeSpec.hasCustomAttributes, typeDef)) { continue; } @@ -710,7 +732,11 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies else { TypeDef typeDef = mod.FindNormal(typeSpec.nameMatcher.NameOrPattern); - if (typeDef != null && MatchModifier(typeSpec.modifierType, typeDef) && MatchClassType(typeSpec.classType, typeDef) && MatchInheritTypes(typeSpec.inheritTypes, typeDef)) + if (typeDef != null + && MatchModifier(typeSpec.modifierType, typeDef) + && MatchClassType(typeSpec.classType, typeDef) + && MatchInheritTypes(typeSpec.inheritTypes, typeDef) + && MatchCustomAttributes(typeSpec.hasCustomAttributes, typeDef)) { yield return typeDef; }