2011-11-17 06:08:27 +08:00
|
|
|
|
/*
|
2014-03-12 05:15:43 +08:00
|
|
|
|
Copyright (C) 2011-2014 de4dot@gmail.com
|
2011-11-17 06:08:27 +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 System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
2011-12-09 16:02:06 +08:00
|
|
|
|
namespace de4dot.code.renamer.asmmodules {
|
2015-05-17 19:29:48 +08:00
|
|
|
|
public class MethodNameGroup {
|
2012-11-02 22:57:11 +08:00
|
|
|
|
List<MMethodDef> methods = new List<MMethodDef>();
|
2011-11-17 06:08:27 +08:00
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public List<MMethodDef> Methods {
|
2011-11-17 06:08:27 +08:00
|
|
|
|
get { return methods; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Count {
|
|
|
|
|
get { return methods.Count; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Add(MMethodDef method) {
|
2011-11-17 06:08:27 +08:00
|
|
|
|
methods.Add(method);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Merge(MethodNameGroup other) {
|
2011-11-17 06:08:27 +08:00
|
|
|
|
if (this == other)
|
|
|
|
|
return;
|
|
|
|
|
methods.AddRange(other.methods);
|
|
|
|
|
}
|
2011-11-18 23:55:54 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public bool HasNonRenamableMethod() {
|
2011-11-21 17:36:23 +08:00
|
|
|
|
foreach (var method in methods) {
|
|
|
|
|
if (!method.Owner.HasModule)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public bool HasInterfaceMethod() {
|
2011-11-21 17:36:23 +08:00
|
|
|
|
foreach (var method in methods) {
|
2012-11-02 22:57:11 +08:00
|
|
|
|
if (method.Owner.TypeDef.IsInterface)
|
2011-11-21 17:36:23 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public bool HasGetterOrSetterPropertyMethod() {
|
2011-11-21 17:36:23 +08:00
|
|
|
|
foreach (var method in methods) {
|
2011-11-23 11:42:13 +08:00
|
|
|
|
if (method.Property == null)
|
|
|
|
|
continue;
|
|
|
|
|
var prop = method.Property;
|
|
|
|
|
if (method == prop.GetMethod || method == prop.SetMethod)
|
2011-11-21 17:36:23 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public bool HasAddRemoveOrRaiseEventMethod() {
|
2011-11-21 17:36:23 +08:00
|
|
|
|
foreach (var method in methods) {
|
2011-11-23 11:42:13 +08:00
|
|
|
|
if (method.Event == null)
|
|
|
|
|
continue;
|
|
|
|
|
var evt = method.Event;
|
|
|
|
|
if (method == evt.AddMethod || method == evt.RemoveMethod || method == evt.RaiseMethod)
|
|
|
|
|
return true;
|
2011-11-21 17:36:23 +08:00
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public bool HasProperty() {
|
2011-11-21 17:36:23 +08:00
|
|
|
|
foreach (var method in methods) {
|
|
|
|
|
if (method.Property != null)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public bool HasEvent() {
|
2011-11-23 13:41:28 +08:00
|
|
|
|
foreach (var method in methods) {
|
|
|
|
|
if (method.Event != null)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-18 23:55:54 +08:00
|
|
|
|
public override string ToString() {
|
|
|
|
|
return string.Format("{0} -- {1}", methods.Count, methods.Count > 0 ? methods[0].ToString() : "");
|
|
|
|
|
}
|
2011-11-17 06:08:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-17 19:29:48 +08:00
|
|
|
|
public class MethodNameGroups {
|
2012-11-02 22:57:11 +08:00
|
|
|
|
Dictionary<MMethodDef, MethodNameGroup> methodGroups = new Dictionary<MMethodDef, MethodNameGroup>();
|
2011-11-17 06:08:27 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Same(MMethodDef a, MMethodDef b) {
|
|
|
|
|
Merge(Get(a), Get(b));
|
2011-11-17 06:08:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Add(MMethodDef methodDef) {
|
|
|
|
|
Get(methodDef);
|
2011-11-17 06:08:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public MethodNameGroup Get(MMethodDef method) {
|
|
|
|
|
if (!method.IsVirtual())
|
2011-11-17 06:08:27 +08:00
|
|
|
|
throw new ApplicationException("Not a virtual method");
|
2012-03-08 20:23:01 +08:00
|
|
|
|
MethodNameGroup group;
|
|
|
|
|
if (!methodGroups.TryGetValue(method, out group)) {
|
|
|
|
|
methodGroups[method] = group = new MethodNameGroup();
|
2013-01-19 20:03:57 +08:00
|
|
|
|
group.Add(method);
|
2011-11-17 06:08:27 +08:00
|
|
|
|
}
|
2012-03-08 20:23:01 +08:00
|
|
|
|
return group;
|
2011-11-17 06:08:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void Merge(MethodNameGroup a, MethodNameGroup b) {
|
2011-11-17 06:08:27 +08:00
|
|
|
|
if (a == b)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (a.Count < b.Count) {
|
2012-03-08 20:23:01 +08:00
|
|
|
|
MethodNameGroup tmp = a;
|
2011-11-17 06:08:27 +08:00
|
|
|
|
a = b;
|
|
|
|
|
b = tmp;
|
|
|
|
|
}
|
2013-01-19 20:03:57 +08:00
|
|
|
|
a.Merge(b);
|
2011-11-17 06:08:27 +08:00
|
|
|
|
foreach (var methodDef in b.Methods)
|
2012-03-08 20:23:01 +08:00
|
|
|
|
methodGroups[methodDef] = a;
|
2011-11-17 06:08:27 +08:00
|
|
|
|
}
|
2011-11-18 23:55:54 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public IEnumerable<MethodNameGroup> GetAllGroups() {
|
|
|
|
|
return Utils.Unique(methodGroups.Values);
|
2011-11-18 23:55:54 +08:00
|
|
|
|
}
|
2011-11-17 06:08:27 +08:00
|
|
|
|
}
|
|
|
|
|
}
|