From df53a0bd1b54e8b8fb82cdf01f9dd4bf87fc5faf Mon Sep 17 00:00:00 2001 From: walon Date: Sat, 14 Jun 2025 09:16:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20ReflectionCompatibilityDet?= =?UTF-8?q?ector=E6=A3=80=E6=9F=A5Enum.Parse=20TryParse=20GetName=20GetNam?= =?UTF-8?q?es=E6=97=B6=E7=9A=84=E5=88=A4=E5=AE=9A=E6=9D=A1=E4=BB=B6?= =?UTF-8?q?=E4=B8=BA=E6=9E=9A=E4=B8=BE=E7=B1=BB=E5=9E=8B=E5=90=8D=E8=A2=AB?= =?UTF-8?q?=E6=B7=B7=E6=B7=86=E7=9A=84=E9=94=99=E8=AF=AF=EF=BC=8C=E5=BA=94?= =?UTF-8?q?=E8=AF=A5=E6=98=AF=E6=9E=9A=E4=B8=BE=E9=A1=B9=E8=A2=AB=E6=B7=B7?= =?UTF-8?q?=E6=B7=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ReflectionCompatibilityDetector.cs | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/Editor/ObfusPasses/SymbolObfus/ReflectionCompatibilityDetector.cs b/Editor/ObfusPasses/SymbolObfus/ReflectionCompatibilityDetector.cs index 97a1cda..a5bb68b 100644 --- a/Editor/ObfusPasses/SymbolObfus/ReflectionCompatibilityDetector.cs +++ b/Editor/ObfusPasses/SymbolObfus/ReflectionCompatibilityDetector.cs @@ -134,6 +134,12 @@ namespace Obfuz.ObfusPasses.SymbolObfus } } + + private bool IsAnyEnumItemRenamed(TypeDef typeDef) + { + return typeDef.Fields.Any(f => _renamePolicy.NeedRename(f)); + } + private void AnalyzeCallvir(IMethod calledMethod, ITypeDefOrRef constrainedType) { TypeDef callType = calledMethod.DeclaringType.ResolveTypeDef(); @@ -151,7 +157,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus if (constrainedType != null) { TypeDef enumTypeDef = constrainedType.ResolveTypeDef(); - if (enumTypeDef != null && enumTypeDef.IsEnum && enumTypeDef.Fields.Any(f => _renamePolicy.NeedRename(f))) + if (enumTypeDef != null && enumTypeDef.IsEnum && IsAnyEnumItemRenamed(enumTypeDef)) { Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: {enumTypeDef.FullName}.ToString() the enum members are renamed."); } @@ -190,22 +196,22 @@ namespace Obfuz.ObfusPasses.SymbolObfus if (parseType != null) { // Enum.Parse(string name) or Enum.Parse(string name, bool caseInsensitive) - if (_renamePolicy.NeedRename(parseType)) + if (IsAnyEnumItemRenamed(parseType)) { - Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.Parse T:{parseType.FullName} is renamed."); + Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.Parse field of T:{parseType.FullName} is renamed."); } } else { // Enum.Parse(Type type, string name) or Enum.Parse(Type type, string name, bool ignoreCase) TypeDef enumType = FindLatestTypeOf(method.GetParamCount() + extraSearchInstructionCount)?.ResolveTypeDef(); - if (enumType != null && enumType.IsEnum && _renamePolicy.NeedRename(enumType)) + if (enumType != null && enumType.IsEnum && IsAnyEnumItemRenamed(enumType)) { - Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.Parse argument type:{enumType.FullName} is renamed."); + Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.Parse field of argument type:{enumType.FullName} is renamed."); } else { - Debug.LogWarning($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.Parse argument `type` should not be renamed."); + Debug.LogWarning($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.Parse field of argument `type` should not be renamed."); } } break; @@ -215,9 +221,9 @@ namespace Obfuz.ObfusPasses.SymbolObfus if (parseType != null) { // Enum.TryParse(string name, out T result) or Enum.TryParse(string name, bool ignoreCase, out T result) - if (_renamePolicy.NeedRename(parseType)) + if (IsAnyEnumItemRenamed(parseType)) { - Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.TryParse T:{parseType.FullName} is renamed."); + Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.TryParse field of T:{parseType.FullName} is renamed."); } } else @@ -230,7 +236,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus { // Enum.GetName(Type type, object value) TypeDef enumType = FindLatestTypeOf(method.GetParamCount() + extraSearchInstructionCount)?.ResolveTypeDef(); - if (enumType != null && enumType.IsEnum && enumType.Fields.Any(f => _renamePolicy.NeedRename(f))) + if (enumType != null && enumType.IsEnum && IsAnyEnumItemRenamed(enumType)) { Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.GetName field of type:{enumType.FullName} is renamed."); } @@ -244,9 +250,9 @@ namespace Obfuz.ObfusPasses.SymbolObfus { // Enum.GetNames(Type type) TypeDef enumType = FindLatestTypeOf(method.GetParamCount() + extraSearchInstructionCount)?.ResolveTypeDef(); - if (enumType != null && enumType.IsEnum && enumType.Fields.Any(f => _renamePolicy.NeedRename(f))) + if (enumType != null && enumType.IsEnum && IsAnyEnumItemRenamed(enumType)) { - Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.GetNams field of type:{enumType.FullName} is renamed."); + Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.GetNames field of type:{enumType.FullName} is renamed."); } else {