From 4cb3b88d24fd65faa7a5569f7d599976493ea37d Mon Sep 17 00:00:00 2001 From: walon Date: Wed, 16 Apr 2025 18:05:21 +0800 Subject: [PATCH] =?UTF-8?q?Rename=E6=97=B6=E4=B8=8D=E4=BC=9A=E4=BD=BF?= =?UTF-8?q?=E7=94=A8dll=E5=AD=98=E5=9C=A8=E7=9A=84=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=E5=8F=8Amapping.xml=E4=B8=AD=E8=AE=B0=E5=BD=95=E7=9A=84?= =?UTF-8?q?=E6=98=A0=E5=B0=84=E8=BF=87=E7=9A=84=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Editor/CollectionExtensions.cs | 5 ++ Editor/MetaUtil.cs | 10 +++ Editor/Rename/INameMaker.cs | 18 ++++- Editor/Rename/INameScope.cs | 9 +++ Editor/Rename/NameMaker.cs | 111 ++-------------------------- Editor/Rename/NameMakerBase.cs | 119 +++++++++++++++++++++++++++++++ Editor/Rename/NameScope.cs | 32 +++++++++ Editor/Rename/NameScopeBase.cs | 55 ++++++++++++++ Editor/Rename/RenameRecordMap.cs | 67 ++++++++++++++--- Editor/Rename/SymbolRename.cs | 18 ++--- Editor/Rename/TestNameMaker.cs | 65 +++-------------- 11 files changed, 326 insertions(+), 183 deletions(-) create mode 100644 Editor/Rename/INameScope.cs create mode 100644 Editor/Rename/NameMakerBase.cs create mode 100644 Editor/Rename/NameScope.cs create mode 100644 Editor/Rename/NameScopeBase.cs diff --git a/Editor/CollectionExtensions.cs b/Editor/CollectionExtensions.cs index b03df01..49f949b 100644 --- a/Editor/CollectionExtensions.cs +++ b/Editor/CollectionExtensions.cs @@ -15,5 +15,10 @@ namespace Obfuz values.Add(value); } } + + public static V GetValueOrDefault(IDictionary dic, K key) + { + return dic.TryGetValue(key, out V v) ? v : default(V); + } } } diff --git a/Editor/MetaUtil.cs b/Editor/MetaUtil.cs index 9e0fae4..39e5ae1 100644 --- a/Editor/MetaUtil.cs +++ b/Editor/MetaUtil.cs @@ -15,6 +15,16 @@ namespace Obfuz return Path.GetFileNameWithoutExtension(moduleName); } + public static (string, string) SplitNamespaceAndName(string fullName) + { + int index = fullName.LastIndexOf('/'); + if (index == -1) + { + int index2 = fullName.IndexOf('.'); + return index2 >= 0 ? (fullName.Substring(0, index2), fullName.Substring(index2 + 1)) : ("", fullName); + } + return ("", fullName.Substring(index + 1)); + } public static TypeDef GetBaseTypeDef(TypeDef type) diff --git a/Editor/Rename/INameMaker.cs b/Editor/Rename/INameMaker.cs index e806d43..c02d3d5 100644 --- a/Editor/Rename/INameMaker.cs +++ b/Editor/Rename/INameMaker.cs @@ -4,11 +4,27 @@ namespace Obfuz.Rename { public interface INameMaker { + void AddPreservedName(ModuleDefMD mod, string name); + + void AddPreservedName(TypeDef typeDef, string name); + + void AddPreservedNamespace(TypeDef typeDef, string name); + + void AddPreservedName(MethodDef methodDef, string name); + + void AddPreservedName(ParamDef paramDef, string name); + + void AddPreservedName(FieldDef fieldDef, string name); + + void AddPreservedName(PropertyDef propertyDef, string name); + + void AddPreservedName(EventDef eventDef, string name); + string GetNewName(ModuleDefMD mod, string originalName); string GetNewName(TypeDef typeDef, string originalName); - string GetNewNamespace(TypeDef typeDef, string originalNamespace); + string GetNewNamespace(TypeDef typeDef, string originalNamespace, bool reuse); string GetNewName(MethodDef methodDef, string originalName); diff --git a/Editor/Rename/INameScope.cs b/Editor/Rename/INameScope.cs new file mode 100644 index 0000000..6246bd3 --- /dev/null +++ b/Editor/Rename/INameScope.cs @@ -0,0 +1,9 @@ +namespace Obfuz.Rename +{ + public interface INameScope + { + void AddPreservedName(string name); + + string GetNewName(string originalName, bool reuse); + } +} diff --git a/Editor/Rename/NameMaker.cs b/Editor/Rename/NameMaker.cs index d1305f7..e960299 100644 --- a/Editor/Rename/NameMaker.cs +++ b/Editor/Rename/NameMaker.cs @@ -2,126 +2,23 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; using System.Threading.Tasks; namespace Obfuz.Rename { - public class NameScope + + public class NameMaker : NameMakerBase { private readonly List _wordSet; - private int _nextIndex; - - private readonly Dictionary _nameMap = new Dictionary(); - - public NameScope(List wordSet) - { - _wordSet = wordSet; - _nextIndex = 0; - } - - private string CreateNewName() - { - var nameBuilder = new StringBuilder(); - for (int i = _nextIndex++; ;) - { - nameBuilder.Append(_wordSet[i % _wordSet.Count]); - i = i / _wordSet.Count; - if (i == 0) - { - break; - } - } - return nameBuilder.ToString(); - } - - public string GetNewName() - { - return CreateNewName(); - } - - public string GetNewName0(string originalName) - { - if (_nameMap.TryGetValue(originalName, out var newName)) - { - return newName; - } - newName = CreateNewName(); - _nameMap[originalName] = newName; - return newName; - } - } - - public class NameMaker : INameMaker - { - private readonly List _wordSet; - - private readonly Dictionary _nameScopes = new Dictionary(); - - private readonly object _namespaceScope = new object(); public NameMaker(List wordSet) { _wordSet = wordSet; } - private NameScope GetNameScope(object key) + protected override INameScope CreateNameScope() { - if (!_nameScopes.TryGetValue(key, out var nameScope)) - { - nameScope = new NameScope(_wordSet); - _nameScopes[key] = nameScope; - } - return nameScope; - } - - public string GetNewName(ModuleDefMD mod, string originalName) - { - return GetDefaultNewName(this, originalName); - } - - private string GetDefaultNewName(object scope, string originName) - { - return GetNameScope(scope).GetNewName(); - } - - public string GetNewNamespace(TypeDef typeDef, string originalNamespace) - { - if (string.IsNullOrEmpty(originalNamespace)) - { - return string.Empty; - } - return GetNameScope(_namespaceScope).GetNewName0(originalNamespace); - } - - public string GetNewName(TypeDef typeDef, string originalName) - { - return GetDefaultNewName(typeDef.Module, originalName); - } - - public string GetNewName(MethodDef methodDef, string originalName) - { - return GetDefaultNewName(methodDef.DeclaringType, originalName); - } - - public string GetNewName(ParamDef param, string originalName) - { - return "1"; - } - - public string GetNewName(FieldDef fieldDef, string originalName) - { - return GetDefaultNewName(fieldDef.DeclaringType, originalName); - } - - public string GetNewName(PropertyDef propertyDef, string originalName) - { - return GetDefaultNewName(propertyDef.DeclaringType, originalName); - } - - public string GetNewName(EventDef eventDef, string originalName) - { - return GetDefaultNewName(eventDef.DeclaringType, originalName); + return new NameScope(_wordSet); } } } diff --git a/Editor/Rename/NameMakerBase.cs b/Editor/Rename/NameMakerBase.cs new file mode 100644 index 0000000..914fc17 --- /dev/null +++ b/Editor/Rename/NameMakerBase.cs @@ -0,0 +1,119 @@ +using dnlib.DotNet; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Obfuz.Rename +{ + public abstract class NameMakerBase : INameMaker + { + + private readonly Dictionary _nameScopes = new Dictionary(); + + private readonly object _namespaceScope = new object(); + + protected abstract INameScope CreateNameScope(); + + protected INameScope GetNameScope(object key) + { + if (!_nameScopes.TryGetValue(key, out var nameScope)) + { + nameScope = CreateNameScope(); + _nameScopes[key] = nameScope; + } + return nameScope; + } + + public void AddPreservedName(ModuleDefMD mod, string name) + { + GetNameScope(mod).AddPreservedName(name); + } + + public void AddPreservedName(TypeDef typeDef, string name) + { + GetNameScope(typeDef.Module).AddPreservedName(name); + } + + public void AddPreservedName(MethodDef methodDef, string name) + { + GetNameScope(methodDef.DeclaringType).AddPreservedName(name); + } + + public void AddPreservedName(ParamDef paramDef, string name) + { + + } + + public void AddPreservedName(FieldDef fieldDef, string name) + { + GetNameScope(fieldDef.DeclaringType).AddPreservedName(name); + } + + public void AddPreservedName(PropertyDef propertyDef, string name) + { + GetNameScope(propertyDef.DeclaringType).AddPreservedName(name); + } + + public void AddPreservedName(EventDef eventDef, string name) + { + GetNameScope(eventDef.DeclaringType).AddPreservedName(name); + } + + public void AddPreservedNamespace(TypeDef typeDef, string name) + { + GetNameScope(_namespaceScope).AddPreservedName(name); + } + + + public string GetNewName(ModuleDefMD mod, string originalName) + { + return GetDefaultNewName(this, originalName); + } + + private string GetDefaultNewName(object scope, string originName) + { + return GetNameScope(scope).GetNewName(originName, false); + } + + public string GetNewNamespace(TypeDef typeDef, string originalNamespace, bool reuse) + { + if (string.IsNullOrEmpty(originalNamespace)) + { + return string.Empty; + } + return GetNameScope(_namespaceScope).GetNewName(originalNamespace, reuse); + } + + public string GetNewName(TypeDef typeDef, string originalName) + { + return GetDefaultNewName(typeDef.Module, originalName); + } + + public string GetNewName(MethodDef methodDef, string originalName) + { + return GetDefaultNewName(methodDef.DeclaringType, originalName); + } + + public string GetNewName(ParamDef param, string originalName) + { + return "1"; + } + + public string GetNewName(FieldDef fieldDef, string originalName) + { + return GetDefaultNewName(fieldDef.DeclaringType, originalName); + } + + public string GetNewName(PropertyDef propertyDef, string originalName) + { + return GetDefaultNewName(propertyDef.DeclaringType, originalName); + } + + public string GetNewName(EventDef eventDef, string originalName) + { + return GetDefaultNewName(eventDef.DeclaringType, originalName); + } + } +} diff --git a/Editor/Rename/NameScope.cs b/Editor/Rename/NameScope.cs new file mode 100644 index 0000000..15e098c --- /dev/null +++ b/Editor/Rename/NameScope.cs @@ -0,0 +1,32 @@ +using Microsoft.SqlServer.Server; +using System.Collections.Generic; +using System.Text; + +namespace Obfuz.Rename +{ + + public class NameScope : NameScopeBase + { + private readonly List _wordSet; + private int _nextIndex; + + public NameScope(List wordSet) + { + _wordSet = wordSet; + _nextIndex = 0; + } + + protected override void BuildNewName(StringBuilder nameBuilder, string originalName) + { + for (int i = _nextIndex++; ;) + { + nameBuilder.Append(_wordSet[i % _wordSet.Count]); + i = i / _wordSet.Count; + if (i == 0) + { + break; + } + } + } + } +} diff --git a/Editor/Rename/NameScopeBase.cs b/Editor/Rename/NameScopeBase.cs new file mode 100644 index 0000000..46ca32c --- /dev/null +++ b/Editor/Rename/NameScopeBase.cs @@ -0,0 +1,55 @@ +using System.Collections.Generic; +using System.Text; + +namespace Obfuz.Rename +{ + public abstract class NameScopeBase : INameScope + { + + private readonly Dictionary _nameMap = new Dictionary(); + + private readonly HashSet _preservedNames = new HashSet(); + + + public void AddPreservedName(string name) + { + if (!string.IsNullOrEmpty(name)) + { + _preservedNames.Add(name); + } + } + + + protected abstract void BuildNewName(StringBuilder nameBuilder, string originalName); + + private string CreateNewName(string originalName) + { + var nameBuilder = new StringBuilder(); + while (true) + { + nameBuilder.Clear(); + BuildNewName(nameBuilder, originalName); + string newName = nameBuilder.ToString(); + if (_preservedNames.Add(newName)) + { + return newName; + } + } + } + + public string GetNewName(string originalName, bool reuse) + { + if (!reuse) + { + return CreateNewName(originalName); + } + if (_nameMap.TryGetValue(originalName, out var newName)) + { + return newName; + } + newName = CreateNewName(originalName); + _nameMap[originalName] = newName; + return newName; + } + } +} diff --git a/Editor/Rename/RenameRecordMap.cs b/Editor/Rename/RenameRecordMap.cs index f2e5852..22001a7 100644 --- a/Editor/Rename/RenameRecordMap.cs +++ b/Editor/Rename/RenameRecordMap.cs @@ -101,16 +101,17 @@ namespace Obfuz public RenameRecordMap(string mappingFile) { _mappingFile = mappingFile; - LoadXmlMappingFile(mappingFile); } - public void Init(List assemblies) + public void Init(List assemblies, INameMaker nameMaker) { + LoadXmlMappingFile(_mappingFile); foreach (var ObfuzAssemblyInfo in assemblies) { ModuleDefMD mod = ObfuzAssemblyInfo.module; string name = mod.Assembly.Name; + nameMaker.AddPreservedName(mod, name); _modRenames.Add(mod, new RenameRecord { status = RenameStatus.NotRenamed, @@ -118,21 +119,48 @@ namespace Obfuz oldName = name, newName = null, }); + + RenameMappingAssembly rma = _assemblies.GetValueOrDefault(name); + if (rma != null && rma.status == RenameStatus.Renamed) + { + nameMaker.AddPreservedName(mod, rma.newAssName); + } + foreach (TypeDef type in mod.GetTypes()) { + nameMaker.AddPreservedName(type, name); + nameMaker.AddPreservedNamespace(type, type.Namespace); + string fullTypeName = type.FullName; + RenameMappingType rmt = rma?.types.GetValueOrDefault(fullTypeName); + if (rmt != null) + { + var (newNamespace, newName) = MetaUtil.SplitNamespaceAndName(rmt.newFullName); + nameMaker.AddPreservedNamespace(type, newNamespace); + nameMaker.AddPreservedName(type, newName); + } + _typeRenames.Add(type, new RenameRecord { status = RenameStatus.NotRenamed, - signature = type.FullName, - oldName = type.FullName, + signature = fullTypeName, + oldName = fullTypeName, newName = null, }); foreach (MethodDef method in type.Methods) { + nameMaker.AddPreservedName(method, method.Name); + string methodSig = TypeSigUtil.ComputeMethodDefSignature(method); + nameMaker.AddPreservedName(method, method.Name); + + RenameMappingMethod rmm = rmt?.methods.GetValueOrDefault(methodSig); + if (rmm != null) + { + nameMaker.AddPreservedName(method, rmm.newName); + } _methodRenames.Add(method, new RenameRecord { status = RenameStatus.NotRenamed, - signature = TypeSigUtil.ComputeMethodDefSignature(method), + signature = methodSig, oldName = method.Name, newName = null, }); @@ -152,30 +180,51 @@ namespace Obfuz } foreach (FieldDef field in type.Fields) { + nameMaker.AddPreservedName(field, field.Name); + string fieldSig = TypeSigUtil.ComputeFieldDefSignature(field); + RenameMappingField rmf = rmt?.fields.GetValueOrDefault(fieldSig); + if (rmf != null) + { + nameMaker.AddPreservedName(field, rmf.newName); + } _fieldRenames.Add(field, new RenameRecord { status = RenameStatus.NotRenamed, - signature = TypeSigUtil.ComputeFieldDefSignature(field), + signature = fieldSig, oldName = field.Name, newName = null, }); } foreach (PropertyDef property in type.Properties) { + nameMaker.AddPreservedName(property, property.Name); + string propertySig = TypeSigUtil.ComputePropertyDefSignature(property); + RenameMappingProperty rmp = rmt?.properties.GetValueOrDefault(propertySig); + if (rmp != null) + { + nameMaker.AddPreservedName(property, rmp.newName); + } _propertyRenames.Add(property, new RenameRecord { status = RenameStatus.NotRenamed, - signature = TypeSigUtil.ComputePropertyDefSignature(property), + signature = propertySig, oldName = property.Name, newName = null, }); } foreach (EventDef eventDef in type.Events) { + nameMaker.AddPreservedName(eventDef, eventDef.Name); + string eventSig = TypeSigUtil.ComputeEventDefSignature(eventDef); + RenameMappingEvent rme = rmt?.events.GetValueOrDefault(eventSig); + if (rme != null) + { + nameMaker.AddPreservedName(eventDef, rme.newName); + } _eventRenames.Add(eventDef, new RenameRecord { status = RenameStatus.NotRenamed, - signature = TypeSigUtil.ComputeEventDefSignature(eventDef), + signature = eventSig, oldName = eventDef.Name, newName = null, }); @@ -203,8 +252,6 @@ namespace Obfuz } } - - private void LoadAssemblyMapping(XmlElement ele) { if (ele.Name != "assembly") diff --git a/Editor/Rename/SymbolRename.cs b/Editor/Rename/SymbolRename.cs index b033d40..69a4437 100644 --- a/Editor/Rename/SymbolRename.cs +++ b/Editor/Rename/SymbolRename.cs @@ -118,7 +118,7 @@ namespace Obfuz public void Process() { - _renameRecordMap.Init(_obfuzAssemblies); + _renameRecordMap.Init(_obfuzAssemblies, _nameMaker); RenameModules(); RenameTypes(); RenameFields(); @@ -236,7 +236,7 @@ namespace Obfuz { if (_renamePolicy.NeedRename(type)) { - Rename(type, _refTypeRefMetasMap.TryGetValue(type, out var typeDefMetas) ? typeDefMetas : null); + Rename(type, _refTypeRefMetasMap.GetValueOrDefault(type)); } } } @@ -342,7 +342,7 @@ namespace Obfuz { if (_renamePolicy.NeedRename(field)) { - Rename(field, refFieldMetasMap.TryGetValue(field, out var fieldMetas) ? fieldMetas : null); + Rename(field, refFieldMetasMap.GetValueOrDefault(field)); } } } @@ -410,7 +410,7 @@ namespace Obfuz } if (_renamePolicy.NeedRename(method)) { - Rename(method, refMethodMetasMap.TryGetValue(method, out var refMethodMetas) ? refMethodMetas : null); + Rename(method, refMethodMetasMap.GetValueOrDefault(method)); } } } @@ -443,7 +443,7 @@ namespace Obfuz } if (_renameRecordMap.TryGetRename(group, out var newName)) { - Rename(method, refMethodMetasMap.TryGetValue(method, out var refMethodMetas) ? refMethodMetas : null, newName); + Rename(method, refMethodMetasMap.GetValueOrDefault(method), newName); } else { @@ -516,7 +516,7 @@ namespace Obfuz { if (_renamePolicy.NeedRename(property)) { - Rename(property, refPropertyMetasMap.TryGetValue(property, out var refPropertyMeta) ? refPropertyMeta : null); + Rename(property, refPropertyMetasMap.GetValueOrDefault(property)); } } } @@ -545,9 +545,10 @@ namespace Obfuz private void Rename(ModuleDefMD mod) { - string oldName = MetaUtil.GetModuleNameWithoutExt(mod.Name); + string oldName = mod.Assembly.Name; string newName = _nameMaker.GetNewName(mod, oldName); _renameRecordMap.AddRename(mod, newName); + mod.Assembly.Name = newName; mod.Name = $"{newName}.dll"; //Debug.Log($"rename module. oldName:{oldName} newName:{newName}"); foreach (ObfuzAssemblyInfo ass in GetReferenceMeAssemblies(mod)) @@ -556,6 +557,7 @@ namespace Obfuz { if (assRef.Name == oldName) { + _renameRecordMap.AddRename(mod, newName); assRef.Name = newName; // Debug.Log($"rename assembly:{ass.name} ref oldName:{oldName} newName:{newName}"); } @@ -575,7 +577,7 @@ namespace Obfuz } else { - newNamespace = _nameMaker.GetNewNamespace(type, oldNamespace); + newNamespace = _nameMaker.GetNewNamespace(type, oldNamespace, true); type.Namespace = newNamespace; } diff --git a/Editor/Rename/TestNameMaker.cs b/Editor/Rename/TestNameMaker.cs index 7b2452c..d2472a7 100644 --- a/Editor/Rename/TestNameMaker.cs +++ b/Editor/Rename/TestNameMaker.cs @@ -1,71 +1,22 @@ -using dnlib.DotNet; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Text; namespace Obfuz.Rename { - public class TestNameMaker : INameMaker + public class TestNameMaker : NameMakerBase { - private int _nextIndex; - - private readonly Dictionary _nameMap = new Dictionary(); - - private string GetDefaultNewName(string originName) + private class TestNameScope : NameScopeBase { - return $"{originName}>{_nextIndex++}"; - } - - public string GetNewName(ModuleDefMD mod, string originalName) - { - return GetDefaultNewName(originalName); - } - - public string GetNewNamespace(TypeDef typeDef, string originalNamespace) - { - if (string.IsNullOrEmpty(originalNamespace)) + private int _nextIndex; + protected override void BuildNewName(StringBuilder nameBuilder, string originalName) { - return string.Empty; + nameBuilder.Append($"{originalName}>{_nextIndex++}"); } - if (_nameMap.TryGetValue(originalNamespace, out var newName)) - { - return newName; - } - newName = GetDefaultNewName(originalNamespace); - _nameMap.Add(originalNamespace, newName); - return newName; } - public string GetNewName(TypeDef typeDef, string originalName) + protected override INameScope CreateNameScope() { - return GetDefaultNewName(originalName); + return new TestNameScope(); } - public string GetNewName(MethodDef methodDef, string originalName) - { - return GetDefaultNewName(originalName); - } - - public string GetNewName(ParamDef param, string originalName) - { - return GetDefaultNewName(originalName); - } - - public string GetNewName(FieldDef fieldDef, string originalName) - { - return GetDefaultNewName(originalName); - } - - public string GetNewName(PropertyDef propertyDef, string originalName) - { - return GetDefaultNewName(originalName); - } - - public string GetNewName(EventDef eventDef, string originalName) - { - return GetDefaultNewName(originalName); - } } }