新增基于 WordSet的 NameMaker
parent
1d1b257640
commit
dcf7fd3ae5
|
@ -34,7 +34,8 @@ namespace Obfuz
|
||||||
_options = options;
|
_options = options;
|
||||||
_assemblyCache = new AssemblyCache(new PathAssemblyResolver(options.AssemblySearchDirs.ToArray()));
|
_assemblyCache = new AssemblyCache(new PathAssemblyResolver(options.AssemblySearchDirs.ToArray()));
|
||||||
_renamePolicy = new CombineRenamePolicy(new SystemRenamePolicy(), new UnityRenamePolicy(), new XmlConfigRenamePolicy());
|
_renamePolicy = new CombineRenamePolicy(new SystemRenamePolicy(), new UnityRenamePolicy(), new XmlConfigRenamePolicy());
|
||||||
_nameMaker = new TestNameMaker();
|
//_nameMaker = new TestNameMaker();
|
||||||
|
_nameMaker = NameMakerFactory.CreateNameMakerBaseASCIICharSet();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DoIt()
|
public void DoIt()
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
using dnlib.DotNet;
|
||||||
|
|
||||||
|
namespace Obfuz.Rename
|
||||||
|
{
|
||||||
|
public interface INameMaker
|
||||||
|
{
|
||||||
|
string GetNewName(ModuleDefMD mod, string originalName);
|
||||||
|
|
||||||
|
string GetNewName(TypeDef typeDef, string originalName);
|
||||||
|
|
||||||
|
string GetNewNamespace(TypeDef typeDef, string originalNamespace);
|
||||||
|
|
||||||
|
string GetNewName(MethodDef methodDef, string originalName);
|
||||||
|
|
||||||
|
string GetNewName(ParamDef param, string originalName);
|
||||||
|
|
||||||
|
string GetNewName(FieldDef fieldDef, string originalName);
|
||||||
|
|
||||||
|
string GetNewName(PropertyDef propertyDef, string originalName);
|
||||||
|
|
||||||
|
string GetNewName(EventDef eventDef, string originalName);
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,72 +7,90 @@ using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Obfuz.Rename
|
namespace Obfuz.Rename
|
||||||
{
|
{
|
||||||
public interface INameMaker
|
public class NameScope
|
||||||
{
|
{
|
||||||
string GetNewName(ModuleDefMD mod, string originalName);
|
private readonly List<string> _wordSet;
|
||||||
|
private int _nextIndex;
|
||||||
|
|
||||||
string GetNewName(TypeDef typeDef, string originalName);
|
public NameScope(List<string> wordSet)
|
||||||
|
{
|
||||||
|
_wordSet = wordSet;
|
||||||
|
_nextIndex = 0;
|
||||||
|
}
|
||||||
|
|
||||||
string GetNewNamespace(TypeDef typeDef, string originalNamespace);
|
public string GetNewName(string originalName)
|
||||||
|
{
|
||||||
string GetNewName(MethodDef methodDef, string originalName);
|
if (_nextIndex >= _wordSet.Count)
|
||||||
|
throw new InvalidOperationException("No more names available in the word set.");
|
||||||
string GetNewName(ParamDef param, string originalName);
|
string newName = _wordSet[_nextIndex++];
|
||||||
|
return newName;
|
||||||
string GetNewName(FieldDef fieldDef, string originalName);
|
}
|
||||||
|
|
||||||
string GetNewName(PropertyDef propertyDef, string originalName);
|
|
||||||
|
|
||||||
string GetNewName(EventDef eventDef, string originalName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class NameMaker : INameMaker
|
public class NameMaker : INameMaker
|
||||||
{
|
{
|
||||||
private int _nextIndex;
|
private readonly List<string> _wordSet;
|
||||||
|
|
||||||
private string GetDefaultNewName(string originName)
|
private readonly Dictionary<object, NameScope> _nameScopes = new Dictionary<object, NameScope>();
|
||||||
|
|
||||||
|
public NameMaker(List<string> wordSet)
|
||||||
{
|
{
|
||||||
return $"{originName}>{_nextIndex++}";
|
_wordSet = wordSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
private NameScope GetNameScope(object key)
|
||||||
|
{
|
||||||
|
if (!_nameScopes.TryGetValue(key, out var nameScope))
|
||||||
|
{
|
||||||
|
nameScope = new NameScope(_wordSet);
|
||||||
|
_nameScopes[key] = nameScope;
|
||||||
|
}
|
||||||
|
return nameScope;
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GetDefaultNewName(object scope, string originName)
|
||||||
|
{
|
||||||
|
return GetNameScope(scope).GetNewName(originName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetNewName(ModuleDefMD mod, string originalName)
|
public string GetNewName(ModuleDefMD mod, string originalName)
|
||||||
{
|
{
|
||||||
return GetDefaultNewName(originalName);
|
return GetDefaultNewName(this, originalName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetNewNamespace(TypeDef typeDef, string originalNamespace)
|
public string GetNewNamespace(TypeDef typeDef, string originalNamespace)
|
||||||
{
|
{
|
||||||
return GetDefaultNewName(originalNamespace);
|
return GetDefaultNewName(typeDef.Module, originalNamespace);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetNewName(TypeDef typeDef, string originalName)
|
public string GetNewName(TypeDef typeDef, string originalName)
|
||||||
{
|
{
|
||||||
return GetDefaultNewName(originalName);
|
return GetDefaultNewName(typeDef.Module, originalName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetNewName(MethodDef methodDef, string originalName)
|
public string GetNewName(MethodDef methodDef, string originalName)
|
||||||
{
|
{
|
||||||
return GetDefaultNewName(originalName);
|
return GetDefaultNewName(methodDef.DeclaringType, originalName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetNewName(ParamDef param, string originalName)
|
public string GetNewName(ParamDef param, string originalName)
|
||||||
{
|
{
|
||||||
return GetDefaultNewName(originalName);
|
return "1";
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetNewName(FieldDef fieldDef, string originalName)
|
public string GetNewName(FieldDef fieldDef, string originalName)
|
||||||
{
|
{
|
||||||
return GetDefaultNewName(originalName);
|
return GetDefaultNewName(fieldDef.DeclaringType, originalName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetNewName(PropertyDef propertyDef, string originalName)
|
public string GetNewName(PropertyDef propertyDef, string originalName)
|
||||||
{
|
{
|
||||||
return GetDefaultNewName(originalName);
|
return GetDefaultNewName(propertyDef.DeclaringType, originalName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetNewName(EventDef eventDef, string originalName)
|
public string GetNewName(EventDef eventDef, string originalName)
|
||||||
{
|
{
|
||||||
return GetDefaultNewName(originalName);
|
return GetDefaultNewName(eventDef.DeclaringType, originalName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Runtime.Remoting.Messaging;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Obfuz.Rename
|
||||||
|
{
|
||||||
|
public static class NameMakerFactory
|
||||||
|
{
|
||||||
|
public static INameMaker CreateNameMakerBaseASCIICharSet()
|
||||||
|
{
|
||||||
|
var words = new List<string>();
|
||||||
|
for (int i = 0; i < 26; i++)
|
||||||
|
{
|
||||||
|
words.Add(((char)('a' + i)).ToString());
|
||||||
|
words.Add(((char)('A' + i)).ToString());
|
||||||
|
}
|
||||||
|
return new NameMaker(words);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue