From b85f3f54a0e3c965fc827fd0457ac84f82a35504 Mon Sep 17 00:00:00 2001 From: walon Date: Wed, 28 May 2025 10:11:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96UnityRenamePolicy=EF=BC=8C?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E8=AE=A1=E7=AE=97=E7=BB=93=E6=9E=9C=EF=BC=8C?= =?UTF-8?q?=E5=B0=86=E6=95=B4=E4=BD=93=E6=B7=B7=E6=B7=86=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E5=87=8F=E5=B0=91=E4=BA=86=E4=B8=80=E5=8D=8A=E5=B7=A6=E5=8F=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SymbolObfus/Policies/UnityRenamePolicy.cs | 42 ++++++++----------- .../Editor/Utils/CachedDictionary.cs | 29 +++++++++++++ .../Editor/Utils/CachedDictionary.cs.meta | 11 +++++ 3 files changed, 58 insertions(+), 24 deletions(-) create mode 100644 com.code-philosophy.obfuz/Editor/Utils/CachedDictionary.cs create mode 100644 com.code-philosophy.obfuz/Editor/Utils/CachedDictionary.cs.meta diff --git a/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/Policies/UnityRenamePolicy.cs b/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/Policies/UnityRenamePolicy.cs index a99052e..12091cf 100644 --- a/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/Policies/UnityRenamePolicy.cs +++ b/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/Policies/UnityRenamePolicy.cs @@ -126,9 +126,18 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies "OnCancel", }; - private readonly Dictionary _unitySourceGeneratedComputeCache = new Dictionary(); + private readonly CachedDictionary _computeDeclaringTypeDisableAllMemberRenamingCache; + private readonly CachedDictionary _isInheritScriptCache; + private readonly CachedDictionary _isInheritFromMonoBehaviourCache; - private bool ComputeIsUnitySourceGeneratedAssemblyType(TypeDef typeDef) + public UnityRenamePolicy() + { + _computeDeclaringTypeDisableAllMemberRenamingCache = new CachedDictionary(ComputeDeclaringTypeDisableAllMemberRenaming); + _isInheritScriptCache = new CachedDictionary(MetaUtil.IsScriptType); + _isInheritFromMonoBehaviourCache = new CachedDictionary(MetaUtil.IsInheritFromMonoBehaviour); + } + + private bool IsUnitySourceGeneratedAssemblyType(TypeDef typeDef) { if (typeDef.Name.StartsWith("UnitySourceGeneratedAssemblyMonoScriptTypes_")) { @@ -157,18 +166,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies return false; } - private bool IsUnitySourceGeneratedAssemblyType(TypeDef typeDef) - { - if (_unitySourceGeneratedComputeCache.TryGetValue(typeDef, out var result)) - { - return result; - } - result = ComputeIsUnitySourceGeneratedAssemblyType(typeDef); - _unitySourceGeneratedComputeCache.Add(typeDef, result); - return result; - } - - private bool DoesDeclaringTypeDisableAllMemberRenaming(TypeDef typeDef) + private bool ComputeDeclaringTypeDisableAllMemberRenaming(TypeDef typeDef) { if (typeDef.IsEnum && MetaUtil.HasBlackboardEnumAttribute(typeDef)) { @@ -182,20 +180,16 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies { return true; } - if (IsUnitySourceGeneratedAssemblyType(typeDef)) - { - return true; - } return false; } public override bool NeedRename(TypeDef typeDef) { - if (MetaUtil.IsScriptType(typeDef)) + if (_isInheritScriptCache.GetValue(typeDef)) { return false; } - if (DoesDeclaringTypeDisableAllMemberRenaming(typeDef)) + if (_computeDeclaringTypeDisableAllMemberRenamingCache.GetValue(typeDef)) { return false; } @@ -209,11 +203,11 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies public override bool NeedRename(MethodDef methodDef) { TypeDef typeDef = methodDef.DeclaringType; - if (MetaUtil.IsInheritFromMonoBehaviour(typeDef) && s_monoBehaviourEvents.Contains(methodDef.Name)) + if (s_monoBehaviourEvents.Contains(methodDef.Name) && _isInheritFromMonoBehaviourCache.GetValue(typeDef)) { return false; } - if (DoesDeclaringTypeDisableAllMemberRenaming(typeDef)) + if (_computeDeclaringTypeDisableAllMemberRenamingCache.GetValue(typeDef)) { return false; } @@ -235,11 +229,11 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies { return !MetaUtil.IsSerializableField(fieldDef); } - if (DoesDeclaringTypeDisableAllMemberRenaming(typeDef)) + if (_computeDeclaringTypeDisableAllMemberRenamingCache.GetValue(typeDef)) { return false; } - if (MetaUtil.HasBurstCompileAttribute(fieldDef) || MetaUtil.HasDOTSCompilerGeneratedAttribute(fieldDef)) + if (MetaUtil.HasBurstCompileAttribute(fieldDef)) { return false; } diff --git a/com.code-philosophy.obfuz/Editor/Utils/CachedDictionary.cs b/com.code-philosophy.obfuz/Editor/Utils/CachedDictionary.cs new file mode 100644 index 0000000..82f5837 --- /dev/null +++ b/com.code-philosophy.obfuz/Editor/Utils/CachedDictionary.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Obfuz.Utils +{ + public class CachedDictionary + { + private readonly Func _valueFactory; + private readonly Dictionary _cache = new Dictionary(); + + public CachedDictionary(Func valueFactory) + { + _valueFactory = valueFactory; + } + + public V GetValue(K key) + { + if (!_cache.TryGetValue(key, out var value)) + { + value = _valueFactory(key); + _cache[key] = value; + } + return value; + } + } +} diff --git a/com.code-philosophy.obfuz/Editor/Utils/CachedDictionary.cs.meta b/com.code-philosophy.obfuz/Editor/Utils/CachedDictionary.cs.meta new file mode 100644 index 0000000..115dde0 --- /dev/null +++ b/com.code-philosophy.obfuz/Editor/Utils/CachedDictionary.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 66494da674feeb741b889590cb663d4e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: