de4dot-cex/AssemblyData/methodsrewriter/MModule.cs

148 lines
4.5 KiB
C#
Raw Permalink Normal View History

2011-09-27 07:48:47 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 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;
using dnlib.DotNet;
2011-09-27 07:48:47 +08:00
using de4dot.blocks;
namespace AssemblyData.methodsrewriter {
class MModule {
public Module module;
2012-11-01 05:44:54 +08:00
public ModuleDefMD moduleDef;
2012-11-22 16:14:51 +08:00
TypeDefDict<MType> typeRefToType = new TypeDefDict<MType>();
2011-09-27 07:48:47 +08:00
Dictionary<int, MType> tokenToType = new Dictionary<int, MType>();
Dictionary<int, MMethod> tokenToGlobalMethod;
Dictionary<int, MField> tokenToGlobalField;
2012-11-01 05:44:54 +08:00
TypeDef moduleType;
2011-09-27 07:48:47 +08:00
2012-11-22 16:14:51 +08:00
public MModule(Module module, ModuleDefMD moduleDef) {
2011-09-27 07:48:47 +08:00
this.module = module;
2012-11-22 16:14:51 +08:00
this.moduleDef = moduleDef;
2013-01-19 20:03:57 +08:00
InitTokenToType();
2011-09-27 07:48:47 +08:00
}
2013-01-19 20:03:57 +08:00
void InitTokenToType() {
2012-11-01 05:44:54 +08:00
moduleType = moduleDef.Types[0];
foreach (var typeDef in moduleDef.GetTypes()) {
int token = (int)typeDef.MDToken.Raw;
Type type;
try {
type = module.ResolveType(token);
}
catch {
tokenToType[token] = null;
2013-01-19 20:03:57 +08:00
typeRefToType.Add(typeDef, null);
continue;
}
2012-11-01 05:44:54 +08:00
var mtype = new MType(type, typeDef);
2011-09-27 07:48:47 +08:00
tokenToType[token] = mtype;
2013-01-19 20:03:57 +08:00
typeRefToType.Add(typeDef, mtype);
2011-09-27 07:48:47 +08:00
}
}
2013-01-19 20:03:57 +08:00
public MType GetType(IType typeRef) {
return typeRefToType.Find(typeRef);
2011-09-27 07:48:47 +08:00
}
2013-01-19 20:03:57 +08:00
public MMethod GetMethod(IMethod methodRef) {
var type = GetType(methodRef.DeclaringType);
2011-09-27 07:48:47 +08:00
if (type != null)
2013-01-19 20:03:57 +08:00
return type.GetMethod(methodRef);
2012-11-01 05:44:54 +08:00
if (!new SigComparer().Equals(moduleType, methodRef.DeclaringType))
2011-09-27 07:48:47 +08:00
return null;
2013-01-19 20:03:57 +08:00
InitGlobalMethods();
2011-09-27 07:48:47 +08:00
foreach (var method in tokenToGlobalMethod.Values) {
2012-11-01 05:44:54 +08:00
if (new SigComparer().Equals(methodRef, method.methodDef))
2011-09-27 07:48:47 +08:00
return method;
}
return null;
}
2013-01-19 20:03:57 +08:00
public MField GetField(IField fieldRef) {
var type = GetType(fieldRef.DeclaringType);
2011-09-27 07:48:47 +08:00
if (type != null)
2013-01-19 20:03:57 +08:00
return type.GetField(fieldRef);
2012-11-01 05:44:54 +08:00
if (!new SigComparer().Equals(moduleType, fieldRef.DeclaringType))
2011-09-27 07:48:47 +08:00
return null;
2013-01-19 20:03:57 +08:00
InitGlobalFields();
2011-09-27 07:48:47 +08:00
foreach (var field in tokenToGlobalField.Values) {
2012-11-01 05:44:54 +08:00
if (new SigComparer().Equals(fieldRef, field.fieldDef))
2011-09-27 07:48:47 +08:00
return field;
}
return null;
}
2013-01-19 20:03:57 +08:00
public MMethod GetMethod(MethodBase method) {
2011-09-27 07:48:47 +08:00
if (method.Module != module)
throw new ApplicationException("Not our module");
if (method.DeclaringType == null)
2013-01-19 20:03:57 +08:00
return GetGlobalMethod(method);
2011-09-27 07:48:47 +08:00
var type = tokenToType[method.DeclaringType.MetadataToken];
2013-01-19 20:03:57 +08:00
return type.GetMethod(method.MetadataToken);
2011-09-27 07:48:47 +08:00
}
2013-01-19 20:03:57 +08:00
public MMethod GetGlobalMethod(MethodBase method) {
InitGlobalMethods();
2011-09-27 07:48:47 +08:00
return tokenToGlobalMethod[method.MetadataToken];
}
2013-01-19 20:03:57 +08:00
void InitGlobalMethods() {
2011-09-27 07:48:47 +08:00
if (tokenToGlobalMethod != null)
return;
tokenToGlobalMethod = new Dictionary<int, MMethod>();
var tmpTokenToGlobalMethod = new Dictionary<int, MethodInfo>();
var flags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
foreach (var m in module.GetMethods(flags))
tmpTokenToGlobalMethod[m.MetadataToken] = m;
foreach (var m in moduleType.Methods) {
if (m.Name == ".cctor") //TODO: Use module.GetMethod(token) to get .cctor method
continue;
2012-11-01 05:44:54 +08:00
var token = (int)m.MDToken.Raw;
2011-09-27 07:48:47 +08:00
tokenToGlobalMethod[token] = new MMethod(tmpTokenToGlobalMethod[token], m);
}
}
2013-01-19 20:03:57 +08:00
void InitGlobalFields() {
2011-09-27 07:48:47 +08:00
if (tokenToGlobalField != null)
return;
tokenToGlobalField = new Dictionary<int, MField>();
var tmpTokenToGlobalField = new Dictionary<int, FieldInfo>();
var flags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
foreach (var f in module.GetFields(flags))
tmpTokenToGlobalField[f.MetadataToken] = f;
foreach (var f in moduleType.Fields) {
2012-11-01 05:44:54 +08:00
var token = (int)f.MDToken.Raw;
2011-09-27 07:48:47 +08:00
tokenToGlobalField[token] = new MField(tmpTokenToGlobalField[token], f);
}
}
public override string ToString() {
2012-11-01 05:44:54 +08:00
return moduleDef.Location;
2011-09-27 07:48:47 +08:00
}
}
}