2011-11-18 23:55:54 +08:00
|
|
|
|
/*
|
2012-01-10 06:02:47 +08:00
|
|
|
|
Copyright (C) 2011-2012 de4dot@gmail.com
|
2011-11-18 23:55:54 +08:00
|
|
|
|
|
|
|
|
|
This file is part of de4dot.
|
|
|
|
|
|
|
|
|
|
de4dot is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
de4dot is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using Mono.Cecil;
|
|
|
|
|
|
2011-12-09 16:02:06 +08:00
|
|
|
|
namespace de4dot.code.renamer {
|
2011-11-18 23:55:54 +08:00
|
|
|
|
class VariableNameState {
|
2012-01-27 12:39:25 +08:00
|
|
|
|
ExistingNames existingVariableNames;
|
|
|
|
|
ExistingNames existingMethodNames;
|
|
|
|
|
ExistingNames existingPropertyNames;
|
|
|
|
|
ExistingNames existingEventNames;
|
|
|
|
|
TypeNames variableNameCreator; // For fields and method args
|
|
|
|
|
TypeNames propertyNameCreator;
|
|
|
|
|
NameCreator eventNameCreator;
|
|
|
|
|
NameCreator genericPropertyNameCreator;
|
|
|
|
|
public NameCreator staticMethodNameCreator;
|
|
|
|
|
public NameCreator instanceMethodNameCreator;
|
|
|
|
|
|
|
|
|
|
public static VariableNameState create() {
|
|
|
|
|
var vns = new VariableNameState();
|
|
|
|
|
vns.existingVariableNames = new ExistingNames();
|
|
|
|
|
vns.existingMethodNames = new ExistingNames();
|
|
|
|
|
vns.existingPropertyNames = new ExistingNames();
|
|
|
|
|
vns.existingEventNames = new ExistingNames();
|
|
|
|
|
vns.variableNameCreator = new VariableNameCreator();
|
|
|
|
|
vns.propertyNameCreator = new PropertyNameCreator();
|
|
|
|
|
vns.eventNameCreator = new NameCreator("Event_");
|
|
|
|
|
vns.genericPropertyNameCreator = new NameCreator("Prop_");
|
|
|
|
|
vns.staticMethodNameCreator = new NameCreator("smethod_");
|
|
|
|
|
vns.instanceMethodNameCreator = new NameCreator("method_");
|
|
|
|
|
return vns;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VariableNameState() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Cloning only params will speed up the method param renaming code
|
|
|
|
|
public VariableNameState cloneParamsOnly() {
|
|
|
|
|
var vns = new VariableNameState();
|
|
|
|
|
vns.existingVariableNames = new ExistingNames();
|
2012-01-28 05:01:20 +08:00
|
|
|
|
vns.variableNameCreator = new VariableNameCreator();
|
2012-01-27 12:39:25 +08:00
|
|
|
|
vns.existingVariableNames.merge(existingVariableNames);
|
|
|
|
|
vns.variableNameCreator.merge(variableNameCreator);
|
|
|
|
|
return vns;
|
2011-11-18 23:55:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-22 15:14:34 +08:00
|
|
|
|
public VariableNameState merge(VariableNameState other) {
|
|
|
|
|
existingVariableNames.merge(other.existingVariableNames);
|
|
|
|
|
existingMethodNames.merge(other.existingMethodNames);
|
|
|
|
|
existingPropertyNames.merge(other.existingPropertyNames);
|
|
|
|
|
existingEventNames.merge(other.existingEventNames);
|
|
|
|
|
variableNameCreator.merge(other.variableNameCreator);
|
|
|
|
|
propertyNameCreator.merge(other.propertyNameCreator);
|
|
|
|
|
eventNameCreator.merge(other.eventNameCreator);
|
|
|
|
|
genericPropertyNameCreator.merge(other.genericPropertyNameCreator);
|
|
|
|
|
staticMethodNameCreator.merge(other.staticMethodNameCreator);
|
|
|
|
|
instanceMethodNameCreator.merge(other.instanceMethodNameCreator);
|
|
|
|
|
return this;
|
2011-11-21 17:36:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-22 22:56:48 +08:00
|
|
|
|
public void mergeMethods(VariableNameState other) {
|
|
|
|
|
existingMethodNames.merge(other.existingMethodNames);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void mergeProperties(VariableNameState other) {
|
|
|
|
|
existingPropertyNames.merge(other.existingPropertyNames);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void mergeEvents(VariableNameState other) {
|
|
|
|
|
existingEventNames.merge(other.existingEventNames);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-21 17:36:23 +08:00
|
|
|
|
public string getNewPropertyName(PropertyDefinition propertyDefinition) {
|
|
|
|
|
var propType = propertyDefinition.PropertyType;
|
|
|
|
|
string newName;
|
|
|
|
|
if (propType is GenericParameter)
|
2012-01-04 16:42:01 +08:00
|
|
|
|
newName = existingPropertyNames.getName(propertyDefinition.Name, genericPropertyNameCreator);
|
2011-11-21 17:36:23 +08:00
|
|
|
|
else
|
2012-01-04 16:42:01 +08:00
|
|
|
|
newName = existingPropertyNames.getName(propertyDefinition.Name, () => propertyNameCreator.create(propType));
|
2011-11-21 17:36:23 +08:00
|
|
|
|
addPropertyName(newName);
|
|
|
|
|
return newName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string getNewEventName(EventDefinition eventDefinition) {
|
|
|
|
|
string newName = eventNameCreator.create();
|
|
|
|
|
addEventName(newName);
|
|
|
|
|
return newName;
|
2011-11-18 23:55:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void addFieldName(string fieldName) {
|
|
|
|
|
existingVariableNames.add(fieldName);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-21 17:36:23 +08:00
|
|
|
|
public void addParamName(string paramName) {
|
|
|
|
|
existingVariableNames.add(paramName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void addMethodName(string methodName) {
|
|
|
|
|
existingMethodNames.add(methodName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void addPropertyName(string propName) {
|
|
|
|
|
existingPropertyNames.add(propName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void addEventName(string eventName) {
|
|
|
|
|
existingEventNames.add(eventName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool isMethodNameUsed(string methodName) {
|
|
|
|
|
return existingMethodNames.exists(methodName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool isPropertyNameUsed(string propName) {
|
|
|
|
|
return existingPropertyNames.exists(propName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool isEventNameUsed(string eventName) {
|
|
|
|
|
return existingEventNames.exists(eventName);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-18 23:55:54 +08:00
|
|
|
|
public string getNewFieldName(FieldDefinition field) {
|
|
|
|
|
return existingVariableNames.getName(field.Name, () => variableNameCreator.create(field.FieldType));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string getNewFieldName(string oldName, INameCreator nameCreator) {
|
|
|
|
|
return existingVariableNames.getName(oldName, () => nameCreator.create());
|
|
|
|
|
}
|
2011-11-21 17:36:23 +08:00
|
|
|
|
|
|
|
|
|
public string getNewParamName(string oldName, ParameterDefinition param) {
|
|
|
|
|
return existingVariableNames.getName(oldName, () => variableNameCreator.create(param.ParameterType));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string getNewMethodName(string oldName, INameCreator nameCreator) {
|
|
|
|
|
return existingMethodNames.getName(oldName, nameCreator);
|
|
|
|
|
}
|
2011-11-18 23:55:54 +08:00
|
|
|
|
}
|
|
|
|
|
}
|