2011-09-27 07:48:47 +08:00
|
|
|
|
/*
|
2012-01-10 06:02:47 +08:00
|
|
|
|
Copyright (C) 2011-2012 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 MGenericParameter {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class AssemblyResolver {
|
|
|
|
|
Dictionary<string, List<TypeResolver>> types = new Dictionary<string, List<TypeResolver>>(StringComparer.Ordinal);
|
|
|
|
|
List<MethodBase> globalMethods;
|
|
|
|
|
List<FieldInfo> globalFields;
|
|
|
|
|
Assembly assembly;
|
|
|
|
|
|
|
|
|
|
public AssemblyResolver(string asmName) {
|
|
|
|
|
assembly = Assembly.Load(new AssemblyName(asmName));
|
|
|
|
|
initTypes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void initTypes() {
|
|
|
|
|
foreach (var type in assembly.GetTypes()) {
|
|
|
|
|
string key = (type.Namespace ?? "") + "." + type.Name;
|
|
|
|
|
List<TypeResolver> list;
|
|
|
|
|
if (!types.TryGetValue(key, out list))
|
|
|
|
|
types[key] = list = new List<TypeResolver>();
|
|
|
|
|
list.Add(new TypeResolver(type));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 05:44:54 +08:00
|
|
|
|
TypeResolver getTypeResolver(ITypeDefOrRef typeRef) {
|
2012-11-06 02:21:33 +08:00
|
|
|
|
if (typeRef == null)
|
|
|
|
|
return null;
|
|
|
|
|
var scopeType = typeRef.ScopeType;
|
|
|
|
|
var key = scopeType.Namespace + "." + scopeType.TypeName;
|
2011-09-27 07:48:47 +08:00
|
|
|
|
List<TypeResolver> list;
|
|
|
|
|
if (!types.TryGetValue(key, out list))
|
|
|
|
|
return null;
|
|
|
|
|
|
2012-11-06 02:21:33 +08:00
|
|
|
|
if (scopeType is TypeDef) {
|
2011-12-17 01:41:36 +08:00
|
|
|
|
foreach (var resolver in list) {
|
2012-11-06 02:21:33 +08:00
|
|
|
|
if (resolver.type.MetadataToken == scopeType.MDToken.Raw)
|
2011-12-17 01:41:36 +08:00
|
|
|
|
return resolver;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-27 07:48:47 +08:00
|
|
|
|
foreach (var resolver in list) {
|
2012-11-06 02:21:33 +08:00
|
|
|
|
if (ResolverUtils.compareTypes(resolver.type, scopeType))
|
2011-09-27 07:48:47 +08:00
|
|
|
|
return resolver;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 05:44:54 +08:00
|
|
|
|
public FieldInfo resolve(IField fieldRef) {
|
|
|
|
|
var resolver = getTypeResolver(fieldRef.DeclaringType);
|
2011-09-27 07:48:47 +08:00
|
|
|
|
if (resolver != null)
|
2012-11-01 05:44:54 +08:00
|
|
|
|
return resolver.resolve(fieldRef);
|
|
|
|
|
return resolveGlobalField(fieldRef);
|
2011-09-27 07:48:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 05:44:54 +08:00
|
|
|
|
FieldInfo resolveGlobalField(IField fieldRef) {
|
2011-09-27 07:48:47 +08:00
|
|
|
|
initGlobalFields();
|
|
|
|
|
foreach (var globalField in globalFields) {
|
2012-11-01 05:44:54 +08:00
|
|
|
|
if (ResolverUtils.compareFields(globalField, fieldRef))
|
2011-09-27 07:48:47 +08:00
|
|
|
|
return globalField;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void initGlobalFields() {
|
|
|
|
|
if (globalFields != null)
|
|
|
|
|
return;
|
|
|
|
|
globalFields = new List<FieldInfo>();
|
|
|
|
|
|
|
|
|
|
var flags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
|
|
|
|
|
foreach (var module in assembly.GetModules(true)) {
|
|
|
|
|
foreach (var method in module.GetFields(flags))
|
|
|
|
|
globalFields.Add(method);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 05:44:54 +08:00
|
|
|
|
public MethodBase resolve(IMethod methodRef) {
|
|
|
|
|
var resolver = getTypeResolver(methodRef.DeclaringType);
|
2011-09-27 07:48:47 +08:00
|
|
|
|
if (resolver != null)
|
2012-11-01 05:44:54 +08:00
|
|
|
|
return resolver.resolve(methodRef);
|
|
|
|
|
return resolveGlobalMethod(methodRef);
|
2011-09-27 07:48:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 05:44:54 +08:00
|
|
|
|
MethodBase resolveGlobalMethod(IMethod methodRef) {
|
2011-09-27 07:48:47 +08:00
|
|
|
|
initGlobalMethods();
|
|
|
|
|
foreach (var globalMethod in globalMethods) {
|
2012-11-01 05:44:54 +08:00
|
|
|
|
if (ResolverUtils.compareMethods(globalMethod, methodRef))
|
2011-09-27 07:48:47 +08:00
|
|
|
|
return globalMethod;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void initGlobalMethods() {
|
|
|
|
|
if (globalMethods != null)
|
|
|
|
|
return;
|
|
|
|
|
globalMethods = new List<MethodBase>();
|
|
|
|
|
|
|
|
|
|
var flags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
|
|
|
|
|
foreach (var module in assembly.GetModules(true)) {
|
|
|
|
|
foreach (var method in module.GetMethods(flags))
|
|
|
|
|
globalMethods.Add(method);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 05:44:54 +08:00
|
|
|
|
public Type resolve(ITypeDefOrRef typeRef) {
|
|
|
|
|
var resolver = getTypeResolver(typeRef);
|
2011-09-27 07:48:47 +08:00
|
|
|
|
if (resolver != null)
|
|
|
|
|
return resolver.type;
|
|
|
|
|
|
2012-11-01 14:34:40 +08:00
|
|
|
|
var ts = typeRef as TypeSpec;
|
|
|
|
|
if (ts != null && ts.TypeSig is GenericSig)
|
2011-09-27 07:48:47 +08:00
|
|
|
|
return typeof(MGenericParameter);
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString() {
|
|
|
|
|
return assembly.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|