命名空间复用
parent
def8fb168c
commit
6e8ef517cd
|
@ -12,13 +12,15 @@ namespace Obfuz.Rename
|
||||||
private readonly List<string> _wordSet;
|
private readonly List<string> _wordSet;
|
||||||
private int _nextIndex;
|
private int _nextIndex;
|
||||||
|
|
||||||
|
private readonly Dictionary<string, string> _nameMap = new Dictionary<string, string>();
|
||||||
|
|
||||||
public NameScope(List<string> wordSet)
|
public NameScope(List<string> wordSet)
|
||||||
{
|
{
|
||||||
_wordSet = wordSet;
|
_wordSet = wordSet;
|
||||||
_nextIndex = 0;
|
_nextIndex = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetNewName(string originalName)
|
private string CreateNewName()
|
||||||
{
|
{
|
||||||
var nameBuilder = new StringBuilder();
|
var nameBuilder = new StringBuilder();
|
||||||
for (int i = _nextIndex++; ;)
|
for (int i = _nextIndex++; ;)
|
||||||
|
@ -32,6 +34,22 @@ namespace Obfuz.Rename
|
||||||
}
|
}
|
||||||
return nameBuilder.ToString();
|
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
|
public class NameMaker : INameMaker
|
||||||
|
@ -40,6 +58,8 @@ namespace Obfuz.Rename
|
||||||
|
|
||||||
private readonly Dictionary<object, NameScope> _nameScopes = new Dictionary<object, NameScope>();
|
private readonly Dictionary<object, NameScope> _nameScopes = new Dictionary<object, NameScope>();
|
||||||
|
|
||||||
|
private readonly object _namespaceScope = new object();
|
||||||
|
|
||||||
public NameMaker(List<string> wordSet)
|
public NameMaker(List<string> wordSet)
|
||||||
{
|
{
|
||||||
_wordSet = wordSet;
|
_wordSet = wordSet;
|
||||||
|
@ -55,19 +75,23 @@ namespace Obfuz.Rename
|
||||||
return 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(this, originalName);
|
return GetDefaultNewName(this, originalName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string GetDefaultNewName(object scope, string originName)
|
||||||
|
{
|
||||||
|
return GetNameScope(scope).GetNewName();
|
||||||
|
}
|
||||||
|
|
||||||
public string GetNewNamespace(TypeDef typeDef, string originalNamespace)
|
public string GetNewNamespace(TypeDef typeDef, string originalNamespace)
|
||||||
{
|
{
|
||||||
return GetDefaultNewName(typeDef.Module, originalNamespace);
|
if (string.IsNullOrEmpty(originalNamespace))
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
return GetNameScope(_namespaceScope).GetNewName0(originalNamespace);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetNewName(TypeDef typeDef, string originalName)
|
public string GetNewName(TypeDef typeDef, string originalName)
|
||||||
|
|
|
@ -11,6 +11,8 @@ namespace Obfuz.Rename
|
||||||
{
|
{
|
||||||
private int _nextIndex;
|
private int _nextIndex;
|
||||||
|
|
||||||
|
private readonly Dictionary<string, string> _nameMap = new Dictionary<string, string>();
|
||||||
|
|
||||||
private string GetDefaultNewName(string originName)
|
private string GetDefaultNewName(string originName)
|
||||||
{
|
{
|
||||||
return $"{originName}>{_nextIndex++}";
|
return $"{originName}>{_nextIndex++}";
|
||||||
|
@ -23,7 +25,17 @@ namespace Obfuz.Rename
|
||||||
|
|
||||||
public string GetNewNamespace(TypeDef typeDef, string originalNamespace)
|
public string GetNewNamespace(TypeDef typeDef, string originalNamespace)
|
||||||
{
|
{
|
||||||
return GetDefaultNewName(originalNamespace);
|
if (string.IsNullOrEmpty(originalNamespace))
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
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)
|
public string GetNewName(TypeDef typeDef, string originalName)
|
||||||
|
|
Loading…
Reference in New Issue