Skip to content

Commit

Permalink
Port more code
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfsck committed Oct 31, 2012
1 parent 2e0aa36 commit 8d87fa0
Show file tree
Hide file tree
Showing 14 changed files with 243 additions and 402 deletions.
4 changes: 4 additions & 0 deletions AssemblyData/AssemblyData.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@
<Project>{5C93C5E2-196F-4877-BF65-96FEBFCEFCA1}</Project>
<Name>de4dot.mdecrypt</Name>
</ProjectReference>
<ProjectReference Include="..\dot10\src\dot10.csproj">
<Project>{FDFC1237-143F-4919-8318-4926901F4639}</Project>
<Name>dot10</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
42 changes: 21 additions & 21 deletions AssemblyData/methodsrewriter/AssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ You should have received a copy of the GNU General Public License
using System;
using System.Collections.Generic;
using System.Reflection;
using Mono.Cecil;
using dot10.DotNet;
using de4dot.blocks;

namespace AssemblyData.methodsrewriter {
Expand Down Expand Up @@ -48,38 +48,38 @@ void initTypes() {
}
}

TypeResolver getTypeResolver(TypeReference typeReference) {
var key = typeReference.Namespace + "." + typeReference.Name;
TypeResolver getTypeResolver(ITypeDefOrRef typeRef) {
var key = typeRef.Namespace + "." + typeRef.Name;
List<TypeResolver> list;
if (!types.TryGetValue(key, out list))
return null;

if (typeReference is TypeDefinition) {
if (typeRef is TypeDef) {
foreach (var resolver in list) {
if (resolver.type.MetadataToken == typeReference.MetadataToken.ToInt32())
if (resolver.type.MetadataToken == typeRef.MDToken.Raw)
return resolver;
}
}

foreach (var resolver in list) {
if (ResolverUtils.compareTypes(resolver.type, typeReference))
if (ResolverUtils.compareTypes(resolver.type, typeRef))
return resolver;
}

return null;
}

public FieldInfo resolve(FieldReference fieldReference) {
var resolver = getTypeResolver(fieldReference.DeclaringType);
public FieldInfo resolve(IField fieldRef) {
var resolver = getTypeResolver(fieldRef.DeclaringType);
if (resolver != null)
return resolver.resolve(fieldReference);
return resolveGlobalField(fieldReference);
return resolver.resolve(fieldRef);
return resolveGlobalField(fieldRef);
}

FieldInfo resolveGlobalField(FieldReference fieldReference) {
FieldInfo resolveGlobalField(IField fieldRef) {
initGlobalFields();
foreach (var globalField in globalFields) {
if (ResolverUtils.compareFields(globalField, fieldReference))
if (ResolverUtils.compareFields(globalField, fieldRef))
return globalField;
}
return null;
Expand All @@ -97,17 +97,17 @@ void initGlobalFields() {
}
}

public MethodBase resolve(MethodReference methodReference) {
var resolver = getTypeResolver(methodReference.DeclaringType);
public MethodBase resolve(IMethod methodRef) {
var resolver = getTypeResolver(methodRef.DeclaringType);
if (resolver != null)
return resolver.resolve(methodReference);
return resolveGlobalMethod(methodReference);
return resolver.resolve(methodRef);
return resolveGlobalMethod(methodRef);
}

MethodBase resolveGlobalMethod(MethodReference methodReference) {
MethodBase resolveGlobalMethod(IMethod methodRef) {
initGlobalMethods();
foreach (var globalMethod in globalMethods) {
if (ResolverUtils.compareMethods(globalMethod, methodReference))
if (ResolverUtils.compareMethods(globalMethod, methodRef))
return globalMethod;
}
return null;
Expand All @@ -125,12 +125,12 @@ void initGlobalMethods() {
}
}

public Type resolve(TypeReference typeReference) {
var resolver = getTypeResolver(typeReference);
public Type resolve(ITypeDefOrRef typeRef) {
var resolver = getTypeResolver(typeRef);
if (resolver != null)
return resolver.type;

if (typeReference.IsGenericParameter)
if (typeRef.IsGenericParameter)
return typeof(MGenericParameter);

return null;
Expand Down
32 changes: 16 additions & 16 deletions AssemblyData/methodsrewriter/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ You should have received a copy of the GNU General Public License
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using Mono.Cecil;
using Mono.Cecil.Cil;
using dot10.DotNet.Emit;
using dot10.DotNet;
using de4dot.blocks;

using OpCode = Mono.Cecil.Cil.OpCode;
using OpCodes = Mono.Cecil.Cil.OpCodes;
using OperandType = Mono.Cecil.Cil.OperandType;
using OpCode = dot10.DotNet.Emit.OpCode;
using OpCodes = dot10.DotNet.Emit.OpCodes;
using OperandType = dot10.DotNet.Emit.OperandType;
using ROpCode = System.Reflection.Emit.OpCode;
using ROpCodes = System.Reflection.Emit.OpCodes;

Expand Down Expand Up @@ -94,7 +94,7 @@ public Delegate generate(IList<Instruction> allInstructions, IList<ExceptionHand

var dm = new DynamicMethod(methodName, methodReturnType, methodParameters, methodInfo.methodBase.Module, true);
var lastInstr = allInstructions[allInstructions.Count - 1];
ilg = dm.GetILGenerator(lastInstr.Offset + lastInstr.GetSize());
ilg = dm.GetILGenerator((int)lastInstr.Offset + lastInstr.GetSize());

initInstrToIndex();
initLocals();
Expand Down Expand Up @@ -134,7 +134,7 @@ void updateExceptionHandlers(Instruction instr) {
if (ex.FilterStart == instr) {
}
if (ex.HandlerStart == instr) {
if (ex.HandlerType == ExceptionHandlerType.Finally)
if (ex.HandlerType == ExceptionClause.Finally)
ilg.BeginFinallyBlock();
else
ilg.BeginCatchBlock(Resolver.getRtType(ex.CatchType));
Expand Down Expand Up @@ -178,7 +178,7 @@ void initInstrToIndex() {

void initLocals() {
locals = new List<LocalBuilder>();
foreach (var local in methodInfo.methodDefinition.Body.Variables)
foreach (var local in methodInfo.methodDef.CilBody.Variables)
locals.Add(ilg.DeclareLocal(Resolver.getRtType(local.VariableType), local.IsPinned));
tempObjLocal = ilg.DeclareLocal(typeof(object));
tempObjArrayLocal = ilg.DeclareLocal(typeof(object[]));
Expand Down Expand Up @@ -256,11 +256,11 @@ Label[] getLabels(Instruction[] targets) {
return labels;
}

int getArgIndex(ParameterDefinition arg) {
return arg.Sequence;
int getArgIndex(Parameter arg) {
return arg.Index;
}

int getLocalIndex(VariableDefinition local) {
int getLocalIndex(Local local) {
return local.Index;
}

Expand Down Expand Up @@ -311,7 +311,7 @@ void writeInstr(Instruction instr) {
case OperandType.InlineType:
case OperandType.InlineMethod:
case OperandType.InlineField:
var obj = Resolver.getRtObject((MemberReference)instr.Operand);
var obj = Resolver.getRtObject((ITokenOperand)instr.Operand);
if (obj is ConstructorInfo)
ilg.Emit(opcode, (ConstructorInfo)obj);
else if (obj is MethodInfo)
Expand All @@ -325,19 +325,19 @@ void writeInstr(Instruction instr) {
break;

case OperandType.InlineArg:
ilg.Emit(opcode, checked((short)getArgIndex((ParameterDefinition)instr.Operand)));
ilg.Emit(opcode, checked((short)getArgIndex((Parameter)instr.Operand)));
break;

case OperandType.ShortInlineArg:
ilg.Emit(opcode, checked((byte)getArgIndex((ParameterDefinition)instr.Operand)));
ilg.Emit(opcode, checked((byte)getArgIndex((Parameter)instr.Operand)));
break;

case OperandType.InlineVar:
ilg.Emit(opcode, checked((short)getLocalIndex((VariableDefinition)instr.Operand)));
ilg.Emit(opcode, checked((short)getLocalIndex((Local)instr.Operand)));
break;

case OperandType.ShortInlineVar:
ilg.Emit(opcode, checked((byte)getLocalIndex((VariableDefinition)instr.Operand)));
ilg.Emit(opcode, checked((byte)getLocalIndex((Local)instr.Operand)));
break;

case OperandType.InlineSig: //TODO:
Expand Down
1 change: 0 additions & 1 deletion AssemblyData/methodsrewriter/IMethodsRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ You should have received a copy of the GNU General Public License

using System;
using System.Reflection;
using Mono.Cecil;

namespace AssemblyData.methodsrewriter {
interface IMethodsRewriter {
Expand Down
10 changes: 5 additions & 5 deletions AssemblyData/methodsrewriter/MField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ You should have received a copy of the GNU General Public License
*/

using System.Reflection;
using Mono.Cecil;
using dot10.DotNet;

namespace AssemblyData.methodsrewriter {
class MField {
public FieldInfo fieldInfo;
public FieldDefinition fieldDefinition;
public FieldDef fieldDef;

public MField(FieldInfo fieldInfo, FieldDefinition fieldDefinition) {
public MField(FieldInfo fieldInfo, FieldDef fieldDef) {
this.fieldInfo = fieldInfo;
this.fieldDefinition = fieldDefinition;
this.fieldDef = fieldDef;
}

public override string ToString() {
return fieldDefinition.ToString();
return fieldDef.ToString();
}
}
}
12 changes: 6 additions & 6 deletions AssemblyData/methodsrewriter/MMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ You should have received a copy of the GNU General Public License
*/

using System.Reflection;
using Mono.Cecil;
using dot10.DotNet;

namespace AssemblyData.methodsrewriter {
class MMethod {
public MethodBase methodBase;
public MethodDefinition methodDefinition;
public MMethod(MethodBase methodBase, MethodDefinition methodDefinition) {
public MethodDef methodDef;
public MMethod(MethodBase methodBase, MethodDef methodDefinition) {
this.methodBase = methodBase;
this.methodDefinition = methodDefinition;
this.methodDef = methodDefinition;
}

public bool hasInstructions() {
return methodDefinition.Body != null && methodDefinition.Body.Instructions.Count != 0;
return methodDef.CilBody != null && methodDef.CilBody.Instructions.Count != 0;
}

public override string ToString() {
return methodDefinition.ToString();
return methodDef.ToString();
}
}
}
52 changes: 26 additions & 26 deletions AssemblyData/methodsrewriter/MModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,74 +20,74 @@ You should have received a copy of the GNU General Public License
using System;
using System.Collections.Generic;
using System.Reflection;
using Mono.Cecil;
using dot10.DotNet;
using de4dot.blocks;

namespace AssemblyData.methodsrewriter {
class MModule {
public Module module;
public ModuleDefinition moduleDefinition;
public ModuleDefMD moduleDef;
TypeDefinitionDict<MType> typeReferenceToType = new TypeDefinitionDict<MType>();
Dictionary<int, MType> tokenToType = new Dictionary<int, MType>();
Dictionary<int, MMethod> tokenToGlobalMethod;
Dictionary<int, MField> tokenToGlobalField;
TypeDefinition moduleType;
TypeDef moduleType;

public MModule(Module module, ModuleDefinition moduleDefinition) {
public MModule(Module module, ModuleDefMD moduleDefinition) {
this.module = module;
this.moduleDefinition = moduleDefinition;
this.moduleDef = moduleDefinition;
initTokenToType();
}

void initTokenToType() {
moduleType = DotNetUtils.getModuleType(moduleDefinition);
foreach (var typeDefinition in moduleDefinition.GetTypes()) {
int token = typeDefinition.MetadataToken.ToInt32();
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;
typeReferenceToType.add(typeDefinition, null);
typeReferenceToType.add(typeDef, null);
continue;
}
var mtype = new MType(type, typeDefinition);
var mtype = new MType(type, typeDef);
tokenToType[token] = mtype;
typeReferenceToType.add(typeDefinition, mtype);
typeReferenceToType.add(typeDef, mtype);
}
}

public MType getType(TypeReference typeReference) {
return typeReferenceToType.find(typeReference);
public MType getType(ITypeDefOrRef typeRef) {
return typeReferenceToType.find(typeRef);
}

public MMethod getMethod(MethodReference methodReference) {
var type = getType(methodReference.DeclaringType);
public MMethod getMethod(IMethod methodRef) {
var type = getType(methodRef.DeclaringType);
if (type != null)
return type.getMethod(methodReference);
if (!MemberReferenceHelper.compareTypes(moduleType, methodReference.DeclaringType))
return type.getMethod(methodRef);
if (!new SigComparer().Equals(moduleType, methodRef.DeclaringType))
return null;

initGlobalMethods();
foreach (var method in tokenToGlobalMethod.Values) {
if (MemberReferenceHelper.compareMethodReference(methodReference, method.methodDefinition))
if (new SigComparer().Equals(methodRef, method.methodDef))
return method;
}

return null;
}

public MField getField(FieldReference fieldReference) {
var type = getType(fieldReference.DeclaringType);
public MField getField(IField fieldRef) {
var type = getType(fieldRef.DeclaringType);
if (type != null)
return type.getField(fieldReference);
if (!MemberReferenceHelper.compareTypes(moduleType, fieldReference.DeclaringType))
return type.getField(fieldRef);
if (!new SigComparer().Equals(moduleType, fieldRef.DeclaringType))
return null;

initGlobalFields();
foreach (var field in tokenToGlobalField.Values) {
if (MemberReferenceHelper.compareFieldReference(fieldReference, field.fieldDefinition))
if (new SigComparer().Equals(fieldRef, field.fieldDef))
return field;
}

Expand Down Expand Up @@ -120,7 +120,7 @@ void initGlobalMethods() {
foreach (var m in moduleType.Methods) {
if (m.Name == ".cctor") //TODO: Use module.GetMethod(token) to get .cctor method
continue;
var token = m.MetadataToken.ToInt32();
var token = (int)m.MDToken.Raw;
tokenToGlobalMethod[token] = new MMethod(tmpTokenToGlobalMethod[token], m);
}
}
Expand All @@ -135,13 +135,13 @@ void initGlobalFields() {
foreach (var f in module.GetFields(flags))
tmpTokenToGlobalField[f.MetadataToken] = f;
foreach (var f in moduleType.Fields) {
var token = f.MetadataToken.ToInt32();
var token = (int)f.MDToken.Raw;
tokenToGlobalField[token] = new MField(tmpTokenToGlobalField[token], f);
}
}

public override string ToString() {
return moduleDefinition.FullyQualifiedName;
return moduleDef.Location;
}
}
}
Loading

0 comments on commit 8d87fa0

Please sign in to comment.