obfuz/Editor/Rename/NameMaker.cs

97 lines
2.7 KiB
C#
Raw Normal View History

2025-04-05 21:47:28 +08:00
using dnlib.DotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Obfuz.Rename
{
2025-04-15 18:30:17 +08:00
public class NameScope
2025-04-05 21:47:28 +08:00
{
2025-04-15 18:30:17 +08:00
private readonly List<string> _wordSet;
private int _nextIndex;
2025-04-05 21:47:28 +08:00
2025-04-15 18:30:17 +08:00
public NameScope(List<string> wordSet)
{
_wordSet = wordSet;
_nextIndex = 0;
}
2025-04-05 21:47:28 +08:00
2025-04-15 18:30:17 +08:00
public string GetNewName(string originalName)
{
if (_nextIndex >= _wordSet.Count)
throw new InvalidOperationException("No more names available in the word set.");
string newName = _wordSet[_nextIndex++];
return newName;
}
2025-04-05 21:47:28 +08:00
}
public class NameMaker : INameMaker
{
2025-04-15 18:30:17 +08:00
private readonly List<string> _wordSet;
private readonly Dictionary<object, NameScope> _nameScopes = new Dictionary<object, NameScope>();
public NameMaker(List<string> wordSet)
{
_wordSet = wordSet;
}
private NameScope GetNameScope(object key)
{
if (!_nameScopes.TryGetValue(key, out var nameScope))
{
nameScope = new NameScope(_wordSet);
_nameScopes[key] = nameScope;
}
return nameScope;
}
2025-04-13 22:41:09 +08:00
2025-04-15 18:30:17 +08:00
private string GetDefaultNewName(object scope, string originName)
2025-04-05 21:47:28 +08:00
{
2025-04-15 18:30:17 +08:00
return GetNameScope(scope).GetNewName(originName);
2025-04-05 21:47:28 +08:00
}
public string GetNewName(ModuleDefMD mod, string originalName)
{
2025-04-15 18:30:17 +08:00
return GetDefaultNewName(this, originalName);
2025-04-05 21:47:28 +08:00
}
2025-04-05 23:21:24 +08:00
public string GetNewNamespace(TypeDef typeDef, string originalNamespace)
{
2025-04-15 18:30:17 +08:00
return GetDefaultNewName(typeDef.Module, originalNamespace);
2025-04-05 23:21:24 +08:00
}
2025-04-05 21:47:28 +08:00
public string GetNewName(TypeDef typeDef, string originalName)
{
2025-04-15 18:30:17 +08:00
return GetDefaultNewName(typeDef.Module, originalName);
2025-04-05 21:47:28 +08:00
}
public string GetNewName(MethodDef methodDef, string originalName)
{
2025-04-15 18:30:17 +08:00
return GetDefaultNewName(methodDef.DeclaringType, originalName);
2025-04-05 21:47:28 +08:00
}
2025-04-07 09:02:43 +08:00
public string GetNewName(ParamDef param, string originalName)
{
2025-04-15 18:30:17 +08:00
return "1";
2025-04-07 09:02:43 +08:00
}
2025-04-05 21:47:28 +08:00
public string GetNewName(FieldDef fieldDef, string originalName)
{
2025-04-15 18:30:17 +08:00
return GetDefaultNewName(fieldDef.DeclaringType, originalName);
2025-04-05 21:47:28 +08:00
}
public string GetNewName(PropertyDef propertyDef, string originalName)
{
2025-04-15 18:30:17 +08:00
return GetDefaultNewName(propertyDef.DeclaringType, originalName);
2025-04-05 21:47:28 +08:00
}
public string GetNewName(EventDef eventDef, string originalName)
{
2025-04-15 18:30:17 +08:00
return GetDefaultNewName(eventDef.DeclaringType, originalName);
2025-04-05 21:47:28 +08:00
}
}
}