Update the code that renames virtual methods, props, events
This commit is contained in:
parent
a4747aaeef
commit
45cd6bf211
|
@ -52,11 +52,9 @@ namespace de4dot.renamer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ExistingNames clone() {
|
public void merge(ExistingNames other) {
|
||||||
var cn = new ExistingNames();
|
foreach (var key in other.allNames.Keys)
|
||||||
foreach (var key in allNames.Keys)
|
allNames[key] = true;
|
||||||
cn.allNames[key] = true;
|
|
||||||
return cn;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,6 @@ using de4dot.blocks;
|
||||||
|
|
||||||
namespace de4dot.renamer {
|
namespace de4dot.renamer {
|
||||||
interface INameCreator {
|
interface INameCreator {
|
||||||
INameCreator clone();
|
|
||||||
string create();
|
string create();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,70 +33,53 @@ namespace de4dot.renamer {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public INameCreator clone() {
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string create() {
|
public string create() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class GlobalNameCreator : INameCreator {
|
abstract class NameCreatorCounter : INameCreator {
|
||||||
INameCreator other;
|
protected int num;
|
||||||
|
|
||||||
public GlobalNameCreator(INameCreator other) {
|
public abstract string create();
|
||||||
this.other = other;
|
|
||||||
}
|
|
||||||
|
|
||||||
public INameCreator clone() {
|
public NameCreatorCounter merge(NameCreatorCounter other) {
|
||||||
|
if (num < other.num)
|
||||||
|
num = other.num;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string create() {
|
|
||||||
return other.create();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class GenericParamNameCreator : INameCreator {
|
class GenericParamNameCreator : NameCreatorCounter {
|
||||||
static string[] names = new string[] { "T", "U", "V", "W", "X", "Y", "Z" };
|
static string[] names = new string[] { "T", "U", "V", "W", "X", "Y", "Z" };
|
||||||
int index = 0;
|
|
||||||
|
|
||||||
public string create() {
|
public override string create() {
|
||||||
if (index < names.Length)
|
if (num < names.Length)
|
||||||
return names[index++];
|
return names[num++];
|
||||||
return string.Format("T{0}", index++);
|
return string.Format("T{0}", num++);
|
||||||
}
|
|
||||||
|
|
||||||
public INameCreator clone() {
|
|
||||||
var rv = new GenericParamNameCreator();
|
|
||||||
rv.index = index;
|
|
||||||
return rv;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class NameCreator : INameCreator {
|
class NameCreator : NameCreatorCounter {
|
||||||
string prefix;
|
string prefix;
|
||||||
int num;
|
|
||||||
|
|
||||||
public NameCreator(string prefix, int num = 0) {
|
public NameCreator(string prefix, int num = 0) {
|
||||||
this.prefix = prefix;
|
this.prefix = prefix;
|
||||||
this.num = num;
|
this.num = num;
|
||||||
}
|
}
|
||||||
|
|
||||||
public INameCreator clone() {
|
public NameCreator clone() {
|
||||||
return new NameCreator(prefix, num);
|
return new NameCreator(prefix, num);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string create() {
|
public override string create() {
|
||||||
return prefix + num++;
|
return prefix + num++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Like NameCreator but don't add the counter the first time
|
// Like NameCreator but don't add the counter the first time
|
||||||
class NameCreator2 : INameCreator {
|
class NameCreator2 : NameCreatorCounter {
|
||||||
string prefix;
|
string prefix;
|
||||||
int num;
|
|
||||||
const string separator = "_";
|
const string separator = "_";
|
||||||
|
|
||||||
public NameCreator2(string prefix, int num = 0) {
|
public NameCreator2(string prefix, int num = 0) {
|
||||||
|
@ -105,11 +87,7 @@ namespace de4dot.renamer {
|
||||||
this.num = num;
|
this.num = num;
|
||||||
}
|
}
|
||||||
|
|
||||||
public INameCreator clone() {
|
public override string create() {
|
||||||
return new NameCreator2(prefix, num);
|
|
||||||
}
|
|
||||||
|
|
||||||
public string create() {
|
|
||||||
string rv;
|
string rv;
|
||||||
if (num == 0)
|
if (num == 0)
|
||||||
rv = prefix;
|
rv = prefix;
|
||||||
|
@ -129,18 +107,18 @@ namespace de4dot.renamer {
|
||||||
|
|
||||||
class NameInfo {
|
class NameInfo {
|
||||||
public string name;
|
public string name;
|
||||||
public INameCreator nameCreator;
|
public NameCreator nameCreator;
|
||||||
public NameInfo(string name, INameCreator nameCreator) {
|
public NameInfo(string name, NameCreator nameCreator) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.nameCreator = nameCreator;
|
this.nameCreator = nameCreator;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void add(string name, INameCreator nameCreator) {
|
public void add(string name, NameCreator nameCreator) {
|
||||||
nameInfos.Add(new NameInfo(name, nameCreator));
|
nameInfos.Add(new NameInfo(name, nameCreator));
|
||||||
}
|
}
|
||||||
|
|
||||||
public INameCreator find(string typeName) {
|
public NameCreator find(string typeName) {
|
||||||
foreach (var nameInfo in nameInfos) {
|
foreach (var nameInfo in nameInfos) {
|
||||||
if (typeName.Contains(nameInfo.name))
|
if (typeName.Contains(nameInfo.name))
|
||||||
return nameInfo.nameCreator;
|
return nameInfo.nameCreator;
|
||||||
|
@ -152,12 +130,12 @@ namespace de4dot.renamer {
|
||||||
|
|
||||||
class TypeNameCreator : ITypeNameCreator {
|
class TypeNameCreator : ITypeNameCreator {
|
||||||
ExistingNames existingNames;
|
ExistingNames existingNames;
|
||||||
INameCreator createUnknownTypeName;
|
NameCreator createUnknownTypeName;
|
||||||
INameCreator createEnumName;
|
NameCreator createEnumName;
|
||||||
INameCreator createStructName;
|
NameCreator createStructName;
|
||||||
INameCreator createDelegateName;
|
NameCreator createDelegateName;
|
||||||
INameCreator createClassName;
|
NameCreator createClassName;
|
||||||
INameCreator createInterfaceName;
|
NameCreator createInterfaceName;
|
||||||
NameInfos nameInfos = new NameInfos();
|
NameInfos nameInfos = new NameInfos();
|
||||||
|
|
||||||
public TypeNameCreator(ExistingNames existingNames) {
|
public TypeNameCreator(ExistingNames existingNames) {
|
||||||
|
@ -181,7 +159,7 @@ namespace de4dot.renamer {
|
||||||
nameInfos.add(name, createNameCreator(name));
|
nameInfos.add(name, createNameCreator(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual INameCreator createNameCreator(string prefix) {
|
protected virtual NameCreator createNameCreator(string prefix) {
|
||||||
return new NameCreator(prefix);
|
return new NameCreator(prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,7 +168,7 @@ namespace de4dot.renamer {
|
||||||
return existingNames.getName(typeDefinition.Name, nameCreator);
|
return existingNames.getName(typeDefinition.Name, nameCreator);
|
||||||
}
|
}
|
||||||
|
|
||||||
INameCreator getNameCreator(TypeDefinition typeDefinition, string newBaseTypeName) {
|
NameCreator getNameCreator(TypeDefinition typeDefinition, string newBaseTypeName) {
|
||||||
var nameCreator = createUnknownTypeName;
|
var nameCreator = createUnknownTypeName;
|
||||||
if (typeDefinition.IsEnum)
|
if (typeDefinition.IsEnum)
|
||||||
nameCreator = createEnumName;
|
nameCreator = createEnumName;
|
||||||
|
@ -222,8 +200,8 @@ namespace de4dot.renamer {
|
||||||
: base(existingNames) {
|
: base(existingNames) {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override INameCreator createNameCreator(string prefix) {
|
protected override NameCreator createNameCreator(string prefix) {
|
||||||
return new GlobalNameCreator(base.createNameCreator("G" + prefix));
|
return base.createNameCreator("G" + prefix);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -604,9 +604,6 @@ namespace de4dot.renamer {
|
||||||
|
|
||||||
prepareRenameEntryPoints();
|
prepareRenameEntryPoints();
|
||||||
|
|
||||||
foreach (var typeDef in modules.BaseTypes)
|
|
||||||
memberInfos.type(typeDef).variableNameState = new VariableNameState();
|
|
||||||
|
|
||||||
foreach (var typeDef in modules.AllTypes)
|
foreach (var typeDef in modules.AllTypes)
|
||||||
prepareRenameMembers(typeDef);
|
prepareRenameMembers(typeDef);
|
||||||
|
|
||||||
|
@ -638,68 +635,92 @@ namespace de4dot.renamer {
|
||||||
return allScopes;
|
return allScopes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class ScopeHelper {
|
||||||
|
MemberInfos memberInfos;
|
||||||
|
Dictionary<TypeDef, bool> visited = new Dictionary<TypeDef, bool>();
|
||||||
|
Dictionary<MethodDef, MethodNameScope> methodToScope;
|
||||||
|
List<MethodNameScope> scopes = new List<MethodNameScope>();
|
||||||
|
IEnumerable<TypeDef> allTypes;
|
||||||
|
Action<MethodNameScope> func;
|
||||||
|
|
||||||
|
public ScopeHelper(MemberInfos memberInfos, IEnumerable<TypeDef> allTypes) {
|
||||||
|
this.memberInfos = memberInfos;
|
||||||
|
this.allTypes = allTypes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add(MethodNameScope scope) {
|
||||||
|
scopes.Add(scope);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void visitAll(Action<MethodNameScope> func) {
|
||||||
|
this.func = func;
|
||||||
|
visited.Clear();
|
||||||
|
|
||||||
|
methodToScope = new Dictionary<MethodDef, MethodNameScope>();
|
||||||
|
foreach (var scope in scopes) {
|
||||||
|
foreach (var method in scope.Methods)
|
||||||
|
methodToScope[method] = scope;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var type in allTypes)
|
||||||
|
visit(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
void visit(TypeDef type) {
|
||||||
|
if (visited.ContainsKey(type))
|
||||||
|
return;
|
||||||
|
visited[type] = true;
|
||||||
|
|
||||||
|
foreach (var ifaceInfo in type.interfaces)
|
||||||
|
visit(ifaceInfo.typeDef);
|
||||||
|
if (type.baseType != null)
|
||||||
|
visit(type.baseType.typeDef);
|
||||||
|
|
||||||
|
TypeInfo info;
|
||||||
|
if (!memberInfos.tryGetType(type, out info))
|
||||||
|
return;
|
||||||
|
info.mergeState();
|
||||||
|
|
||||||
|
foreach (var method in type.AllMethodsSorted) {
|
||||||
|
MethodNameScope scope;
|
||||||
|
if (!methodToScope.TryGetValue(method, out scope))
|
||||||
|
continue;
|
||||||
|
foreach (var m in scope.Methods)
|
||||||
|
methodToScope.Remove(m);
|
||||||
|
func(scope);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void prepareRenameVirtualMethods(MethodNameScopes scopes) {
|
void prepareRenameVirtualMethods(MethodNameScopes scopes) {
|
||||||
var allScopes = getSorted(scopes);
|
var allScopes = getSorted(scopes);
|
||||||
|
|
||||||
var virtualMethods = new List<MethodNameScope>();
|
var virtualMethods = new ScopeHelper(memberInfos, modules.AllTypes);
|
||||||
var ifaceMethods = new List<MethodNameScope>();
|
var ifaceMethods = new ScopeHelper(memberInfos, modules.AllTypes);
|
||||||
var propMethods = new List<MethodNameScope>();
|
var propMethods = new ScopeHelper(memberInfos, modules.AllTypes);
|
||||||
var eventMethods = new List<MethodNameScope>();
|
var eventMethods = new ScopeHelper(memberInfos, modules.AllTypes);
|
||||||
foreach (var scope in allScopes) {
|
foreach (var scope in allScopes) {
|
||||||
if (scope.hasNonRenamableMethod())
|
if (scope.hasNonRenamableMethod())
|
||||||
continue;
|
continue;
|
||||||
else if (scope.hasPropertyMethod() && getPropertyMethodType(scope.Methods[0]) != PropertyMethodType.Other)
|
else if (scope.hasPropertyMethod() && getPropertyMethodType(scope.Methods[0]) != PropertyMethodType.Other)
|
||||||
propMethods.Add(scope);
|
propMethods.add(scope);
|
||||||
else if (scope.hasEventMethod())
|
else if (scope.hasEventMethod())
|
||||||
eventMethods.Add(scope);
|
eventMethods.add(scope);
|
||||||
else if (scope.hasInterfaceMethod())
|
else if (scope.hasInterfaceMethod())
|
||||||
ifaceMethods.Add(scope);
|
ifaceMethods.add(scope);
|
||||||
else
|
else
|
||||||
virtualMethods.Add(scope);
|
virtualMethods.add(scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
prepareRenameVirtualProperties(propMethods);
|
propMethods.visitAll((scope) => prepareRenameProperty(scope, false));
|
||||||
prepareRenameVirtualEvents(eventMethods);
|
eventMethods.visitAll((scope) => prepareRenameEvent(scope, false));
|
||||||
prepareRenameVirtualMethods(virtualMethods, "vmethod_", false);
|
virtualMethods.visitAll((scope) => prepareRenameVirtualMethods(scope, "vmethod_", false));
|
||||||
prepareRenameVirtualMethods(ifaceMethods, "imethod_", false);
|
ifaceMethods.visitAll((scope) => prepareRenameVirtualMethods(scope, "imethod_", false));
|
||||||
prepareRenameVirtualMethods(virtualMethods, "vmethod_", true);
|
|
||||||
prepareRenameVirtualMethods(ifaceMethods, "imethod_", true);
|
|
||||||
}
|
|
||||||
|
|
||||||
Dictionary<TypeDef, int> numBaseClassesDict = new Dictionary<TypeDef, int>();
|
propMethods.visitAll((scope) => prepareRenameProperty(scope, true));
|
||||||
int getNumberOfBaseClasses(TypeDef type) {
|
eventMethods.visitAll((scope) => prepareRenameEvent(scope, true));
|
||||||
int numBaseClasses;
|
virtualMethods.visitAll((scope) => prepareRenameVirtualMethods(scope, "vmethod_", true));
|
||||||
if (numBaseClassesDict.TryGetValue(type, out numBaseClasses))
|
ifaceMethods.visitAll((scope) => prepareRenameVirtualMethods(scope, "imethod_", true));
|
||||||
return numBaseClasses;
|
|
||||||
return numBaseClassesDict[type] = getNumberOfBaseClassesInternal(type);
|
|
||||||
}
|
|
||||||
|
|
||||||
int getNumberOfBaseClassesInternal(TypeDef type) {
|
|
||||||
if (type.baseType == null)
|
|
||||||
return 0;
|
|
||||||
return getNumberOfBaseClasses(type.baseType.typeDef) + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int compareTypes(TypeDef a, TypeDef b) {
|
|
||||||
int ac = getNumberOfBaseClasses(a);
|
|
||||||
int bc = getNumberOfBaseClasses(b);
|
|
||||||
if (ac < bc) return -1;
|
|
||||||
if (ac > bc) return 1;
|
|
||||||
return Utils.compareInt32(a.Index, b.Index);
|
|
||||||
}
|
|
||||||
|
|
||||||
void sortScopes(List<MethodNameScope> scopes) {
|
|
||||||
var scopeToType = new Dictionary<MethodNameScope, TypeDef>(scopes.Count);
|
|
||||||
foreach (var scope in scopes) {
|
|
||||||
TypeDef type = null;
|
|
||||||
foreach (var method in scope.Methods) {
|
|
||||||
var owner = method.Owner;
|
|
||||||
if (type == null || compareTypes(owner, type) < 0)
|
|
||||||
type = owner;
|
|
||||||
}
|
|
||||||
scopeToType[scope] = type;
|
|
||||||
}
|
|
||||||
scopes.Sort((a, b) => compareTypes(scopeToType[a], scopeToType[b]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static readonly Regex removeGenericsArityRegex = new Regex(@"`[0-9]+");
|
static readonly Regex removeGenericsArityRegex = new Regex(@"`[0-9]+");
|
||||||
|
@ -719,15 +740,6 @@ namespace de4dot.renamer {
|
||||||
return name.Substring(index + 1);
|
return name.Substring(index + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void prepareRenameVirtualEvents(List<MethodNameScope> scopes) {
|
|
||||||
sortScopes(scopes);
|
|
||||||
|
|
||||||
foreach (var scope in scopes)
|
|
||||||
prepareRenameEvent(scope, false);
|
|
||||||
foreach (var scope in scopes)
|
|
||||||
prepareRenameEvent(scope, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
void prepareRenameEvent(MethodNameScope scope, bool renameOverrides) {
|
void prepareRenameEvent(MethodNameScope scope, bool renameOverrides) {
|
||||||
string methodPrefix, overridePrefix;
|
string methodPrefix, overridePrefix;
|
||||||
var eventName = prepareRenameEvent(scope, renameOverrides, out overridePrefix, out methodPrefix);
|
var eventName = prepareRenameEvent(scope, renameOverrides, out overridePrefix, out methodPrefix);
|
||||||
|
@ -808,15 +820,6 @@ namespace de4dot.renamer {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
void prepareRenameVirtualProperties(List<MethodNameScope> scopes) {
|
|
||||||
sortScopes(scopes);
|
|
||||||
|
|
||||||
foreach (var scope in scopes)
|
|
||||||
prepareRenameProperty(scope, false);
|
|
||||||
foreach (var scope in scopes)
|
|
||||||
prepareRenameProperty(scope, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
void prepareRenameProperty(MethodNameScope scope, bool renameOverrides) {
|
void prepareRenameProperty(MethodNameScope scope, bool renameOverrides) {
|
||||||
string overridePrefix;
|
string overridePrefix;
|
||||||
var propName = prepareRenameProperty(scope, renameOverrides, out overridePrefix);
|
var propName = prepareRenameProperty(scope, renameOverrides, out overridePrefix);
|
||||||
|
@ -918,16 +921,15 @@ namespace de4dot.renamer {
|
||||||
const string defaultVal = "Prop_";
|
const string defaultVal = "Prop_";
|
||||||
|
|
||||||
var propType = getPropertyType(scope);
|
var propType = getPropertyType(scope);
|
||||||
if (propType == null)
|
if (propType == null || propType is GenericInstanceType)
|
||||||
return defaultVal;
|
return defaultVal;
|
||||||
|
|
||||||
string name = propType.Name;
|
string name = propType.Name;
|
||||||
int i = name.IndexOf('`');
|
int i;
|
||||||
if (i >= 0)
|
if ((i = name.IndexOf('`')) >= 0)
|
||||||
name = name.Substring(0, i);
|
|
||||||
i = name.IndexOf('.');
|
|
||||||
if (i >= 0)
|
|
||||||
name = name.Substring(0, i);
|
name = name.Substring(0, i);
|
||||||
|
if ((i = name.LastIndexOf('.')) >= 0)
|
||||||
|
name = name.Substring(i + 1);
|
||||||
if (name == "")
|
if (name == "")
|
||||||
return defaultVal;
|
return defaultVal;
|
||||||
return name + "_";
|
return name + "_";
|
||||||
|
@ -968,13 +970,6 @@ namespace de4dot.renamer {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
void prepareRenameVirtualMethods(List<MethodNameScope> scopes, string namePrefix, bool renameOverrides) {
|
|
||||||
sortScopes(scopes);
|
|
||||||
|
|
||||||
foreach (var scope in scopes)
|
|
||||||
prepareRenameVirtualMethods(scope, namePrefix, renameOverrides);
|
|
||||||
}
|
|
||||||
|
|
||||||
void prepareRenameVirtualMethods(MethodNameScope scope, string namePrefix, bool renameOverrides) {
|
void prepareRenameVirtualMethods(MethodNameScope scope, string namePrefix, bool renameOverrides) {
|
||||||
if (!hasInvalidMethodName(scope))
|
if (!hasInvalidMethodName(scope))
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -29,7 +29,7 @@ namespace de4dot.renamer {
|
||||||
class TypeInfo : MemberInfo {
|
class TypeInfo : MemberInfo {
|
||||||
public string oldNamespace;
|
public string oldNamespace;
|
||||||
public string newNamespace;
|
public string newNamespace;
|
||||||
public VariableNameState variableNameState;
|
public VariableNameState variableNameState = new VariableNameState();
|
||||||
public TypeDef type;
|
public TypeDef type;
|
||||||
MemberInfos memberInfos;
|
MemberInfos memberInfos;
|
||||||
|
|
||||||
|
@ -111,9 +111,24 @@ namespace de4dot.renamer {
|
||||||
prepareRenameGenericParams(type.GenericParams, checker);
|
prepareRenameGenericParams(type.GenericParams, checker);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void mergeState() {
|
||||||
|
foreach (var ifaceInfo in type.interfaces)
|
||||||
|
mergeState(ifaceInfo.typeDef);
|
||||||
|
if (type.baseType != null)
|
||||||
|
mergeState(type.baseType.typeDef);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mergeState(TypeDef other) {
|
||||||
|
if (other == null)
|
||||||
|
return;
|
||||||
|
TypeInfo otherInfo;
|
||||||
|
if (!memberInfos.tryGetType(other, out otherInfo))
|
||||||
|
return;
|
||||||
|
variableNameState.merge(otherInfo.variableNameState);
|
||||||
|
}
|
||||||
|
|
||||||
public void prepareRenameMembers() {
|
public void prepareRenameMembers() {
|
||||||
if (variableNameState == null)
|
mergeState();
|
||||||
variableNameState = memberInfos.type(type.baseType.typeDef).variableNameState.clone();
|
|
||||||
|
|
||||||
foreach (var fieldDef in type.AllFields)
|
foreach (var fieldDef in type.AllFields)
|
||||||
variableNameState.addFieldName(field(fieldDef).oldName);
|
variableNameState.addFieldName(field(fieldDef).oldName);
|
||||||
|
|
|
@ -23,10 +23,56 @@ using Mono.Cecil;
|
||||||
|
|
||||||
namespace de4dot.renamer {
|
namespace de4dot.renamer {
|
||||||
abstract class TypeNames {
|
abstract class TypeNames {
|
||||||
protected IDictionary<string, INameCreator> typeNames = new Dictionary<string, INameCreator>(StringComparer.Ordinal);
|
protected Dictionary<string, NameCreator> typeNames = new Dictionary<string, NameCreator>(StringComparer.Ordinal);
|
||||||
protected INameCreator genericParamNameCreator = new NameCreator("gparam_");
|
protected NameCreator genericParamNameCreator = new NameCreator("gparam_");
|
||||||
|
|
||||||
public TypeNames() {
|
public string create(TypeReference typeRef) {
|
||||||
|
var elementType = typeRef.GetElementType();
|
||||||
|
if (elementType is GenericParameter)
|
||||||
|
return genericParamNameCreator.create();
|
||||||
|
|
||||||
|
var name = elementType.FullName;
|
||||||
|
NameCreator nc;
|
||||||
|
if (typeNames.TryGetValue(name, out nc))
|
||||||
|
return nc.create();
|
||||||
|
|
||||||
|
var parts = name.Replace('/', '.').Split(new char[] { '.' });
|
||||||
|
var newName = parts[parts.Length - 1];
|
||||||
|
int tickIndex = newName.LastIndexOf('`');
|
||||||
|
if (tickIndex > 0)
|
||||||
|
newName = newName.Substring(0, tickIndex);
|
||||||
|
|
||||||
|
return addTypeName(name, newName).create();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected INameCreator addTypeName(string fullName, string newName) {
|
||||||
|
newName = fixName(newName);
|
||||||
|
|
||||||
|
var name2 = " " + newName;
|
||||||
|
NameCreator nc;
|
||||||
|
if (!typeNames.TryGetValue(name2, out nc))
|
||||||
|
typeNames[name2] = nc = new NameCreator(newName + "_");
|
||||||
|
|
||||||
|
typeNames[fullName] = nc;
|
||||||
|
return nc;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract string fixName(string name);
|
||||||
|
|
||||||
|
public virtual TypeNames merge(TypeNames other) {
|
||||||
|
foreach (var pair in other.typeNames) {
|
||||||
|
if (typeNames.ContainsKey(pair.Key))
|
||||||
|
typeNames[pair.Key].merge(pair.Value);
|
||||||
|
else
|
||||||
|
typeNames[pair.Key] = pair.Value.clone();
|
||||||
|
}
|
||||||
|
genericParamNameCreator.merge(other.genericParamNameCreator);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class VariableNameCreator : TypeNames {
|
||||||
|
public VariableNameCreator() {
|
||||||
addTypeName("System.Boolean", "bool");
|
addTypeName("System.Boolean", "bool");
|
||||||
addTypeName("System.Byte", "byte");
|
addTypeName("System.Byte", "byte");
|
||||||
addTypeName("System.Char", "char");
|
addTypeName("System.Char", "char");
|
||||||
|
@ -45,49 +91,6 @@ namespace de4dot.renamer {
|
||||||
addTypeName("System.Decimal", "decimal");
|
addTypeName("System.Decimal", "decimal");
|
||||||
}
|
}
|
||||||
|
|
||||||
public string create(TypeReference typeRef) {
|
|
||||||
var elementType = typeRef.GetElementType();
|
|
||||||
if (elementType is GenericParameter)
|
|
||||||
return genericParamNameCreator.create();
|
|
||||||
|
|
||||||
var name = elementType.FullName;
|
|
||||||
INameCreator nc;
|
|
||||||
if (typeNames.TryGetValue(name, out nc))
|
|
||||||
return nc.create();
|
|
||||||
|
|
||||||
var parts = name.Replace('/', '.').Split(new char[] { '.' });
|
|
||||||
var newName = parts[parts.Length - 1];
|
|
||||||
int tickIndex = newName.LastIndexOf('`');
|
|
||||||
if (tickIndex > 0)
|
|
||||||
newName = newName.Substring(0, tickIndex);
|
|
||||||
|
|
||||||
return addTypeName(name, newName).create();
|
|
||||||
}
|
|
||||||
|
|
||||||
INameCreator addTypeName(string fullName, string newName) {
|
|
||||||
newName = fixName(newName);
|
|
||||||
|
|
||||||
var name2 = " " + newName;
|
|
||||||
INameCreator nc;
|
|
||||||
if (!typeNames.TryGetValue(name2, out nc))
|
|
||||||
typeNames[name2] = nc = new NameCreator(newName + "_");
|
|
||||||
|
|
||||||
typeNames[fullName] = nc;
|
|
||||||
return nc;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected abstract string fixName(string name);
|
|
||||||
public abstract TypeNames clone();
|
|
||||||
|
|
||||||
protected IDictionary<string, INameCreator> cloneDict() {
|
|
||||||
var rv = new Dictionary<string, INameCreator>(StringComparer.Ordinal);
|
|
||||||
foreach (var key in typeNames.Keys)
|
|
||||||
rv[key] = typeNames[key].clone();
|
|
||||||
return rv;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class VariableNameCreator : TypeNames {
|
|
||||||
protected override string fixName(string name) {
|
protected override string fixName(string name) {
|
||||||
// Make all leading upper case chars lower case
|
// Make all leading upper case chars lower case
|
||||||
var s = "";
|
var s = "";
|
||||||
|
@ -99,25 +102,11 @@ namespace de4dot.renamer {
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override TypeNames clone() {
|
|
||||||
var rv = new VariableNameCreator();
|
|
||||||
rv.typeNames = cloneDict();
|
|
||||||
rv.genericParamNameCreator = genericParamNameCreator.clone();
|
|
||||||
return rv;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class PropertyNameCreator : TypeNames {
|
class PropertyNameCreator : TypeNames {
|
||||||
protected override string fixName(string name) {
|
protected override string fixName(string name) {
|
||||||
return name.Substring(0, 1).ToUpperInvariant() + name.Substring(1);
|
return name.Substring(0, 1).ToUpperInvariant() + name.Substring(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override TypeNames clone() {
|
|
||||||
var rv = new PropertyNameCreator();
|
|
||||||
rv.typeNames = cloneDict();
|
|
||||||
rv.genericParamNameCreator = genericParamNameCreator.clone();
|
|
||||||
return rv;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,14 +24,14 @@ namespace de4dot.renamer {
|
||||||
class TypeRenamerState {
|
class TypeRenamerState {
|
||||||
ExistingNames existingNames;
|
ExistingNames existingNames;
|
||||||
Dictionary<string, string> namespaceToNewName;
|
Dictionary<string, string> namespaceToNewName;
|
||||||
INameCreator createNamespaceName;
|
NameCreator createNamespaceName;
|
||||||
public ITypeNameCreator globalTypeNameCreator;
|
public ITypeNameCreator globalTypeNameCreator;
|
||||||
public ITypeNameCreator internalTypeNameCreator;
|
public ITypeNameCreator internalTypeNameCreator;
|
||||||
|
|
||||||
public TypeRenamerState() {
|
public TypeRenamerState() {
|
||||||
existingNames = new ExistingNames();
|
existingNames = new ExistingNames();
|
||||||
namespaceToNewName = new Dictionary<string, string>(StringComparer.Ordinal);
|
namespaceToNewName = new Dictionary<string, string>(StringComparer.Ordinal);
|
||||||
createNamespaceName = new GlobalNameCreator(new NameCreator("ns"));
|
createNamespaceName = new NameCreator("ns");
|
||||||
globalTypeNameCreator = new GlobalTypeNameCreator(existingNames);
|
globalTypeNameCreator = new GlobalTypeNameCreator(existingNames);
|
||||||
internalTypeNameCreator = new TypeNameCreator(existingNames);
|
internalTypeNameCreator = new TypeNameCreator(existingNames);
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,28 +27,27 @@ namespace de4dot.renamer {
|
||||||
ExistingNames existingEventNames = new ExistingNames();
|
ExistingNames existingEventNames = new ExistingNames();
|
||||||
TypeNames variableNameCreator = new VariableNameCreator(); // For fields and method args
|
TypeNames variableNameCreator = new VariableNameCreator(); // For fields and method args
|
||||||
TypeNames propertyNameCreator = new PropertyNameCreator();
|
TypeNames propertyNameCreator = new PropertyNameCreator();
|
||||||
INameCreator eventNameCreator = new NameCreator("Event_");
|
NameCreator eventNameCreator = new NameCreator("Event_");
|
||||||
INameCreator genericPropertyNameCreator = new NameCreator("Prop_");
|
NameCreator genericPropertyNameCreator = new NameCreator("Prop_");
|
||||||
public INameCreator staticMethodNameCreator = new NameCreator("smethod_");
|
public NameCreator staticMethodNameCreator = new NameCreator("smethod_");
|
||||||
public INameCreator instanceMethodNameCreator = new NameCreator("method_");
|
public NameCreator instanceMethodNameCreator = new NameCreator("method_");
|
||||||
|
|
||||||
public virtual VariableNameState clone() {
|
public VariableNameState clone() {
|
||||||
var rv = new VariableNameState();
|
return new VariableNameState().merge(this);
|
||||||
cloneInit(rv);
|
|
||||||
return rv;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void cloneInit(VariableNameState variableNameState) {
|
public VariableNameState merge(VariableNameState other) {
|
||||||
variableNameState.existingVariableNames = existingVariableNames.clone();
|
existingVariableNames.merge(other.existingVariableNames);
|
||||||
variableNameState.existingMethodNames = existingMethodNames.clone();
|
existingMethodNames.merge(other.existingMethodNames);
|
||||||
variableNameState.existingPropertyNames = existingPropertyNames.clone();
|
existingPropertyNames.merge(other.existingPropertyNames);
|
||||||
variableNameState.existingEventNames = existingEventNames.clone();
|
existingEventNames.merge(other.existingEventNames);
|
||||||
variableNameState.variableNameCreator = variableNameCreator.clone();
|
variableNameCreator.merge(other.variableNameCreator);
|
||||||
variableNameState.propertyNameCreator = propertyNameCreator.clone();
|
propertyNameCreator.merge(other.propertyNameCreator);
|
||||||
variableNameState.eventNameCreator = eventNameCreator.clone();
|
eventNameCreator.merge(other.eventNameCreator);
|
||||||
variableNameState.genericPropertyNameCreator = genericPropertyNameCreator.clone();
|
genericPropertyNameCreator.merge(other.genericPropertyNameCreator);
|
||||||
variableNameState.staticMethodNameCreator = staticMethodNameCreator.clone();
|
staticMethodNameCreator.merge(other.staticMethodNameCreator);
|
||||||
variableNameState.instanceMethodNameCreator = instanceMethodNameCreator.clone();
|
instanceMethodNameCreator.merge(other.instanceMethodNameCreator);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string getNewPropertyName(PropertyDefinition propertyDefinition) {
|
public string getNewPropertyName(PropertyDefinition propertyDefinition) {
|
||||||
|
|
|
@ -68,8 +68,11 @@ namespace de4dot.renamer.asmmodules {
|
||||||
|
|
||||||
public bool hasEventMethod() {
|
public bool hasEventMethod() {
|
||||||
foreach (var method in methods) {
|
foreach (var method in methods) {
|
||||||
if (method.Event != null)
|
if (method.Event != null) {
|
||||||
return true;
|
var evt = method.Event;
|
||||||
|
if (method == evt.AddMethod || method == evt.RemoveMethod || method == evt.RaiseMethod)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -82,14 +85,6 @@ namespace de4dot.renamer.asmmodules {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool hasEvent() {
|
|
||||||
foreach (var method in methods) {
|
|
||||||
if (method.Event != null)
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() {
|
public override string ToString() {
|
||||||
return string.Format("{0} -- {1}", methods.Count, methods.Count > 0 ? methods[0].ToString() : "");
|
return string.Format("{0} -- {1}", methods.Count, methods.Count > 0 ? methods[0].ToString() : "");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user