2011-09-27 07:48:47 +08:00
|
|
|
|
/*
|
2013-01-02 00:03:16 +08:00
|
|
|
|
Copyright (C) 2011-2013 de4dot@gmail.com
|
2011-09-27 07:48:47 +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;
|
|
|
|
|
using System.Reflection;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
2011-09-27 07:48:47 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace AssemblyData.methodsrewriter {
|
|
|
|
|
class MType {
|
|
|
|
|
public Type type;
|
2012-11-01 05:44:54 +08:00
|
|
|
|
public TypeDef typeDef;
|
2011-09-27 07:48:47 +08:00
|
|
|
|
Dictionary<int, MMethod> tokenToMethod;
|
2012-11-22 16:14:51 +08:00
|
|
|
|
MethodDefDict<MMethod> methodRefToMethod;
|
2011-09-27 07:48:47 +08:00
|
|
|
|
Dictionary<int, MField> tokenToField;
|
2012-11-22 16:14:51 +08:00
|
|
|
|
FieldDefDict<MField> fieldRefToField;
|
2011-09-27 07:48:47 +08:00
|
|
|
|
|
2012-11-22 16:14:51 +08:00
|
|
|
|
public MType(Type type, TypeDef typeDef) {
|
2011-09-27 07:48:47 +08:00
|
|
|
|
this.type = type;
|
2012-11-22 16:14:51 +08:00
|
|
|
|
this.typeDef = typeDef;
|
2011-09-27 07:48:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 05:44:54 +08:00
|
|
|
|
public MMethod getMethod(IMethod methodRef) {
|
2011-09-27 07:48:47 +08:00
|
|
|
|
initMethods();
|
2012-11-22 16:14:51 +08:00
|
|
|
|
return methodRefToMethod.find(methodRef);
|
2011-09-27 07:48:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 05:44:54 +08:00
|
|
|
|
public MField getField(IField fieldRef) {
|
2011-09-27 07:48:47 +08:00
|
|
|
|
initFields();
|
2012-11-22 16:14:51 +08:00
|
|
|
|
return fieldRefToField.find(fieldRef);
|
2011-09-27 07:48:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MMethod getMethod(int token) {
|
|
|
|
|
initMethods();
|
|
|
|
|
return tokenToMethod[token];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MField getField(int token) {
|
|
|
|
|
initFields();
|
|
|
|
|
return tokenToField[token];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void initMethods() {
|
|
|
|
|
if (tokenToMethod != null)
|
|
|
|
|
return;
|
2012-11-01 05:44:54 +08:00
|
|
|
|
tokenToMethod = new Dictionary<int, MMethod>(typeDef.Methods.Count);
|
2012-11-22 16:14:51 +08:00
|
|
|
|
methodRefToMethod = new MethodDefDict<MMethod>();
|
2011-09-27 07:48:47 +08:00
|
|
|
|
|
|
|
|
|
var tmpTokenToMethod = new Dictionary<int, MethodBase>();
|
|
|
|
|
var flags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
|
|
|
|
|
foreach (var m in ResolverUtils.getMethodBases(type, flags))
|
|
|
|
|
tmpTokenToMethod[m.MetadataToken] = m;
|
2012-11-01 05:44:54 +08:00
|
|
|
|
foreach (var m in typeDef.Methods) {
|
|
|
|
|
var token = (int)m.MDToken.Raw;
|
2011-09-27 07:48:47 +08:00
|
|
|
|
var method = new MMethod(tmpTokenToMethod[token], m);
|
|
|
|
|
tokenToMethod[token] = method;
|
2012-11-22 16:14:51 +08:00
|
|
|
|
methodRefToMethod.add(method.methodDef, method);
|
2011-09-27 07:48:47 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void initFields() {
|
|
|
|
|
if (tokenToField != null)
|
|
|
|
|
return;
|
2012-11-01 05:44:54 +08:00
|
|
|
|
tokenToField = new Dictionary<int, MField>(typeDef.Fields.Count);
|
2012-11-22 16:14:51 +08:00
|
|
|
|
fieldRefToField = new FieldDefDict<MField>();
|
2011-09-27 07:48:47 +08:00
|
|
|
|
|
|
|
|
|
var tmpTokenToField = new Dictionary<int, FieldInfo>();
|
|
|
|
|
var flags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
|
|
|
|
|
foreach (var f in type.GetFields(flags))
|
|
|
|
|
tmpTokenToField[f.MetadataToken] = f;
|
2012-11-01 05:44:54 +08:00
|
|
|
|
foreach (var f in typeDef.Fields) {
|
|
|
|
|
var token = (int)f.MDToken.Raw;
|
2011-09-27 07:48:47 +08:00
|
|
|
|
var field = new MField(tmpTokenToField[token], f);
|
|
|
|
|
tokenToField[token] = field;
|
2012-11-22 16:14:51 +08:00
|
|
|
|
fieldRefToField.add(field.fieldDef, field);
|
2011-09-27 07:48:47 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString() {
|
2012-11-01 05:44:54 +08:00
|
|
|
|
return string.Format("{0:X8} - {1}", typeDef.MDToken.Raw, typeDef.FullName);
|
2011-09-27 07:48:47 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|