public void DecompileOnDemand(TypeDefinition type) { if (type == null) return; if (CheckMappings(type.MetadataToken.ToInt32())) return; try { DecompilerContext context = new DecompilerContext(type.Module); AstBuilder astBuilder = new AstBuilder(context); astBuilder.AddType(type); astBuilder.GenerateCode(new PlainTextOutput()); int token = type.MetadataToken.ToInt32(); var info = new DecompileInformation { CodeMappings = astBuilder.CodeMappings, LocalVariables = astBuilder.LocalVariables, DecompiledMemberReferences = astBuilder.DecompiledMemberReferences }; // save the data DebugInformation.AddOrUpdate(token, info, (k, v) => info); } catch { return; } }
AstBuilder CreateBuilder(IDnlibDef item, CancellationToken token) { ModuleDef moduleDef; DecompilerContext ctx; AstBuilder builder; if (item is ModuleDef) { var def = (ModuleDef)item; moduleDef = def; builder = new AstBuilder(ctx = new DecompilerContext(moduleDef) { CancellationToken = token }); builder.AddAssembly(def, true); } else if (item is TypeDef) { var def = (TypeDef)item; moduleDef = def.Module; builder = new AstBuilder(ctx = new DecompilerContext(moduleDef) { CancellationToken = token }); builder.DecompileMethodBodies = false; ctx.CurrentType = def; builder.AddType(def); } else if (item is MethodDef) { var def = (MethodDef)item; moduleDef = def.Module; builder = new AstBuilder(ctx = new DecompilerContext(moduleDef) { CancellationToken = token }); ctx.CurrentType = def.DeclaringType; builder.AddMethod(def); } else if (item is FieldDef) { var def = (FieldDef)item; moduleDef = def.Module; builder = new AstBuilder(ctx = new DecompilerContext(moduleDef) { CancellationToken = token }); ctx.CurrentType = def.DeclaringType; builder.AddField(def); } else if (item is PropertyDef) { var def = (PropertyDef)item; moduleDef = def.Module; builder = new AstBuilder(ctx = new DecompilerContext(moduleDef) { CancellationToken = token }); ctx.CurrentType = def.DeclaringType; builder.AddProperty(def); } else if (item is EventDef) { var def = (EventDef)item; moduleDef = def.Module; builder = new AstBuilder(ctx = new DecompilerContext(moduleDef) { CancellationToken = token }); ctx.CurrentType = def.DeclaringType; builder.AddEvent(def); } else return null; ctx.Settings = new DecompilerSettings { UsingDeclarations = false }; return builder; }
private void DecompileTypeFromModule(string typeName, string moduleFileName) { var assemblyDef = AssemblyDefinition.ReadAssembly(moduleFileName); var typeDef = TypeDefFromAssemblyDef(typeName, assemblyDef); AstBuilder decompiler = new AstBuilder( new DecompilerContext(typeDef.Module)); decompiler.AddType(typeDef); GenerateCode(decompiler); }
private List<MethodDefinition> GetConstructors(TypeDefinition type, bool isBaseInitializer) { List<MethodDefinition> constructors = new List<MethodDefinition>(); var visitor = new ObservableAstVisitor(); visitor.EnterConstructorDeclaration += exp => { var constructor = exp.Annotation<MethodReference>(); if(!isBaseInitializer || exp.Initializer.ConstructorInitializerType == ConstructorInitializerType.Base){ constructors.Add(constructor.Resolve()); } }; ProcessNewInstructions(); var d = new DecompilerContext(type.Module) {CurrentType = type}; var astBuilder = new AstBuilder(d); astBuilder.AddType(type); astBuilder.SyntaxTree.AcceptVisitor(visitor); return constructors; }
private void FixGeneratedType() { var d = new DecompilerContext(DestinationType.Module); d.CurrentType = DestinationType; var visitor = new ObservableAstVisitor(); visitor.EnterInvocationExpression += exp => { var target = exp.Target; var methodInvoke = exp.Annotation<MethodReference>(); if(methodInvoke==null) return; for(var i = 0; i< exp.Arguments.Count; i++){ var arg = exp.Arguments.ToList()[i]; var argType = arg.Annotation<TypeInformation>().InferredType; var param = methodInvoke.Parameters[i]; var paramType = param.ParameterType; if(argType.IsValueType && !paramType.IsValueType){ var argILRange = arg.Annotation<List<ILRange>>(); var insts = GetInstructions(arg); var inst = GetInstruction(insts, argILRange[0].To); if(inst.Previous.OpCode.Code != Code.Box){ var box = Instruction.Create(OpCodes.Box,argType); insts.Insert(insts.IndexOf(inst),box); Console.WriteLine("Box inserted! Method Arg "+ param.Name + " " +argType + " to " + paramType); } } } }; ProcessNewInstructions(); var astBuilder = new AstBuilder(d); astBuilder.AddType(DestinationType); // astBuilder.RunTransformations(); astBuilder.SyntaxTree.AcceptVisitor(visitor); }
private void CopyConstructorInitInstructions() { var constructors = GetConstructors(DestinationType, true); var visitor = new ObservableAstVisitor(); bool isNewConstructor = false; visitor.EnterConstructorDeclaration += exp => { var constructor = exp.Annotation<MethodReference>(); if(!copier.CopyMap.ContainsKey(constructor)){ isNewConstructor = true; } }; visitor.LeaveConstructorDeclaration += exp => { isNewConstructor = false; }; visitor.EnterConstructorInitializer += exp => { isNewConstructor = false; }; visitor.EnterAssignmentExpression += exp => { var member = exp.Left.Annotation<MemberReference>(); if(isNewConstructor){ if(copier.CopyMap.ContainsKey(member)){ var argILRange = exp.Annotation<List<ILRange>>(); var newInstructions = GetInstructions(exp); foreach(var constructor in constructors){ var newInstruction = GetInstruction(newInstructions, argILRange[0].To); newInstruction = newInstruction.Previous; while(newInstruction != null && newInstruction.Offset >= argILRange[0].From){ constructor.Body.Instructions.Insert(0, copier.Copy(newInstruction)); newInstruction = newInstruction.Previous; } } Console.WriteLine("Copied to "+constructors.Count+" constructor assigment of "+member); } } }; ProcessNewInstructions(); var d = new DecompilerContext(OriginType.Module) {CurrentType = OriginType}; var astBuilder = new AstBuilder(d); astBuilder.AddType(OriginType); astBuilder.SyntaxTree.AcceptVisitor(visitor); }
void RunDecompiler(string assemblyFile, string fullTypeName, DebuggerTextOutput textOutput, CancellationToken cancellationToken) { ReaderParameters readerParameters = new ReaderParameters(); // Use new assembly resolver instance so that the AssemblyDefinitions can be garbage-collected // once the code is decompiled. readerParameters.AssemblyResolver = new ILSpyAssemblyResolver(Path.GetDirectoryName(assemblyFile)); ModuleDefinition module = ModuleDefinition.ReadModule(assemblyFile, readerParameters); TypeDefinition typeDefinition = module.GetType(fullTypeName); if (typeDefinition == null) throw new InvalidOperationException("Could not find type"); DecompilerContext context = new DecompilerContext(module); context.CancellationToken = cancellationToken; AstBuilder astBuilder = new AstBuilder(context); astBuilder.AddType(typeDefinition); astBuilder.GenerateCode(textOutput); // save decompilation data decompiledType = typeDefinition; memberLocations = textOutput.MemberLocations; }
void RunDecompiler(string assemblyFile, string fullTypeName, ITextOutput textOutput, CancellationToken cancellationToken) { ReaderParameters readerParameters = new ReaderParameters(); // Use new assembly resolver instance so that the AssemblyDefinitions can be garbage-collected // once the code is decompiled. readerParameters.AssemblyResolver = new ILSpyAssemblyResolver(Path.GetDirectoryName(assemblyFile)); ModuleDefinition module = ModuleDefinition.ReadModule(assemblyFile, readerParameters); TypeDefinition typeDefinition = module.GetType(fullTypeName); if (typeDefinition == null) throw new InvalidOperationException("Could not find type"); DecompilerContext context = new DecompilerContext(module); context.CancellationToken = cancellationToken; AstBuilder astBuilder = new AstBuilder(context); astBuilder.AddType(typeDefinition); astBuilder.GenerateCode(textOutput); // save decompilation data decompiledType = typeDefinition; /* int token = decompiledType.MetadataToken.ToInt32(); var info = new DecompileInformation { CodeMappings = astBuilder.CodeMappings, LocalVariables = astBuilder.LocalVariables, DecompiledMemberReferences = astBuilder.DecompiledMemberReferences }; // save the data DebuggerDecompilerService.DebugInformation.AddOrUpdate(token, info, (k, v) => info); */ }
public bool Compile() { LogLine(1, "Compiling Xaml"); LogLine(1, "\nAssembly: {0}", Assembly); if (!string.IsNullOrEmpty(DependencyPaths)) LogLine(1, "DependencyPaths: \t{0}", DependencyPaths); if (!string.IsNullOrEmpty(ReferencePath)) LogLine(1, "ReferencePath: \t{0}", ReferencePath.Replace("//", "/")); LogLine(3, "DebugSymbols:\"{0}\"", DebugSymbols); var skipassembly = true; //change this to false to enable XamlC by default bool success = true; if (!File.Exists(Assembly)) { LogLine(1, "Assembly file not found. Skipping XamlC."); return true; } var resolver = new XamlCAssemblyResolver(); if (!string.IsNullOrEmpty(DependencyPaths)) { foreach (var dep in DependencyPaths.Split(';')) { LogLine(3, "Adding searchpath {0}", dep); resolver.AddSearchDirectory(dep); } } if (!string.IsNullOrEmpty(ReferencePath)) { var paths = ReferencePath.Replace("//", "/").Split(';'); foreach (var p in paths) { var searchpath = Path.GetDirectoryName(p); LogLine(3, "Adding searchpath {0}", searchpath); resolver.AddSearchDirectory(searchpath); // LogLine (3, "Referencing {0}", p); // resolver.AddAssembly (p); } } var assemblyDefinition = AssemblyDefinition.ReadAssembly(Path.GetFullPath(Assembly), new ReaderParameters { AssemblyResolver = resolver, ReadSymbols = DebugSymbols }); CustomAttribute xamlcAttr; if (assemblyDefinition.HasCustomAttributes && (xamlcAttr = assemblyDefinition.CustomAttributes.FirstOrDefault( ca => ca.AttributeType.FullName == "Xamarin.Forms.Xaml.XamlCompilationAttribute")) != null) { var options = (XamlCompilationOptions)xamlcAttr.ConstructorArguments[0].Value; if ((options & XamlCompilationOptions.Skip) == XamlCompilationOptions.Skip) skipassembly = true; if ((options & XamlCompilationOptions.Compile) == XamlCompilationOptions.Compile) skipassembly = false; } foreach (var module in assemblyDefinition.Modules) { var skipmodule = skipassembly; if (module.HasCustomAttributes && (xamlcAttr = module.CustomAttributes.FirstOrDefault( ca => ca.AttributeType.FullName == "Xamarin.Forms.Xaml.XamlCompilationAttribute")) != null) { var options = (XamlCompilationOptions)xamlcAttr.ConstructorArguments[0].Value; if ((options & XamlCompilationOptions.Skip) == XamlCompilationOptions.Skip) skipmodule = true; if ((options & XamlCompilationOptions.Compile) == XamlCompilationOptions.Compile) skipmodule = false; } LogLine(2, " Module: {0}", module.Name); var resourcesToPrune = new List<EmbeddedResource>(); foreach (var resource in module.Resources.OfType<EmbeddedResource>()) { Log(2, " Resource: {0}... ", resource.Name); string classname; if (!resource.IsXaml(out classname)) { LogLine(2, "skipped."); continue; } TypeDefinition typeDef = module.GetType(classname); if (typeDef == null) { LogLine(2, "no type found... skipped."); continue; } var skiptype = skipmodule; if (typeDef.HasCustomAttributes && (xamlcAttr = typeDef.CustomAttributes.FirstOrDefault( ca => ca.AttributeType.FullName == "Xamarin.Forms.Xaml.XamlCompilationAttribute")) != null) { var options = (XamlCompilationOptions)xamlcAttr.ConstructorArguments[0].Value; if ((options & XamlCompilationOptions.Skip) == XamlCompilationOptions.Skip) skiptype = true; if ((options & XamlCompilationOptions.Compile) == XamlCompilationOptions.Compile) skiptype = false; } if (skiptype) { LogLine(2, "Has XamlCompilationAttribute set to Skip and not Compile... skipped"); continue; } var initComp = typeDef.Methods.FirstOrDefault(md => md.Name == "InitializeComponent"); if (initComp == null) { LogLine(2, "no InitializeComponent found... skipped."); continue; } LogLine(2, ""); Log(2, " Parsing Xaml... "); var rootnode = ParseXaml(resource.GetResourceStream(), typeDef); if (rootnode == null) { LogLine(2, "failed."); continue; } LogLine(2, "done."); hasCompiledXamlResources = true; try { Log(2, " Replacing {0}.InitializeComponent ()... ", typeDef.Name); var body = new MethodBody(initComp); var il = body.GetILProcessor(); il.Emit(OpCodes.Nop); var visitorContext = new ILContext(il, body); rootnode.Accept(new XamlNodeVisitor((node, parent) => node.Parent = parent), null); rootnode.Accept(new ExpandMarkupsVisitor(visitorContext), null); rootnode.Accept(new CreateObjectVisitor(visitorContext), null); rootnode.Accept(new SetNamescopesAndRegisterNamesVisitor(visitorContext), null); rootnode.Accept(new SetFieldVisitor(visitorContext), null); rootnode.Accept(new SetResourcesVisitor(visitorContext), null); rootnode.Accept(new SetPropertiesVisitor(visitorContext, true), null); il.Emit(OpCodes.Ret); initComp.Body = body; } catch (XamlParseException xpe) { LogLine(2, "failed."); LogError(null, null, null, resource.Name, xpe.XmlInfo.LineNumber, xpe.XmlInfo.LinePosition, 0, 0, xpe.Message, xpe.HelpLink, xpe.Source); LogLine(4, xpe.StackTrace); success = false; continue; } catch (XmlException xe) { LogLine(2, "failed."); LogError(null, null, null, resource.Name, xe.LineNumber, xe.LinePosition, 0, 0, xe.Message, xe.HelpLink, xe.Source); LogLine(4, xe.StackTrace); success = false; continue; } catch (Exception e) { LogLine(2, "failed."); LogError(null, null, null, resource.Name, 0, 0, 0, 0, e.Message, e.HelpLink, e.Source); LogLine(4, e.StackTrace); success = false; continue; } LogLine(2, "done."); if (OptimizeIL) { Log(2, " Optimizing IL... "); initComp.Body.OptimizeMacros(); LogLine(2, "done"); } if (OutputGeneratedILAsCode) { var filepath = Path.Combine(Path.GetDirectoryName(Assembly), typeDef.FullName + ".decompiled.cs"); Log(2, " Decompiling {0} into {1}...", typeDef.FullName, filepath); var decompilerContext = new DecompilerContext(module); using (var writer = new StreamWriter(filepath)) { var output = new PlainTextOutput(writer); var codeDomBuilder = new AstBuilder(decompilerContext); codeDomBuilder.AddType(typeDef); codeDomBuilder.GenerateCode(output); } LogLine(2, "done"); } resourcesToPrune.Add(resource); } if (!KeepXamlResources) { if (resourcesToPrune.Any()) LogLine(2, " Removing compiled xaml resources"); foreach (var resource in resourcesToPrune) { Log(2, " Removing {0}... ", resource.Name); module.Resources.Remove(resource); LogLine(2, "done"); } } LogLine(2, ""); } if (!hasCompiledXamlResources) { LogLine(1, "No compiled resources. Skipping writing assembly."); return success; } Log(1, "Writing the assembly... "); try { assemblyDefinition.Write(Assembly, new WriterParameters { WriteSymbols = DebugSymbols }); LogLine(1, "done."); } catch (Exception e) { LogLine(1, "failed."); LogError(null, null, null, null, 0, 0, 0, 0, e.Message, e.HelpLink, e.Source); LogLine(4, e.StackTrace); success = false; } return success; }
public ICSharpCode.AvalonEdit.Document.TextDocument Decompile(object obj) { AvalonEditTextOutput aeto = new AvalonEditTextOutput(); AstBuilder ast = new AstBuilder(new DecompilerContext(ModuleDefinition.CreateModule("ash", ModuleKind.NetModule))); switch (obj.GetType().Name) { case "AssemblyDefinition": ast = new AstBuilder(new DecompilerContext((obj as AssemblyDefinition).MainModule) { Settings = new DecompilerSettings() }); try { ast.AddAssembly(obj as AssemblyDefinition); } catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.FullName); } break; case "TypeDefinition": ast = CreateAstBuilder((obj as TypeDefinition), true); try { ast.AddType(obj as TypeDefinition); } catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.FullName); } break; case "MethodDefinition": MethodDefinition method = (obj as MethodDefinition); ast = CreateAstBuilder(method.DeclaringType, true); if (method.IsConstructor && !method.IsStatic && !method.DeclaringType.IsValueType) { foreach (var field in method.DeclaringType.Fields) if (field.IsStatic == method.IsStatic) { try { ast.AddField(field); } catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.Name); } } foreach (var ctor in method.DeclaringType.Methods) if (ctor.IsConstructor && ctor.IsStatic == method.IsStatic) { try { ast.AddMethod(ctor); } catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.Name); } } } else { try { ast.AddMethod(obj as MethodDefinition); } catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.Name); } } break; case "PropertyDefinition": ast = CreateAstBuilder((obj as PropertyDefinition).DeclaringType, true); try { ast.AddProperty(obj as PropertyDefinition); } catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.Name); } break; case "FieldDefinition": ast = CreateAstBuilder((obj as FieldDefinition).DeclaringType, true); try { ast.AddField(obj as FieldDefinition); } catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.Name); } break; case "EventDefinition": ast = CreateAstBuilder((obj as EventDefinition).DeclaringType, true); try { ast.AddEvent(obj as EventDefinition); } catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly " + e.AssemblyReference.Name); } break; default: return new ICSharpCode.AvalonEdit.Document.TextDocument(); } try { ast.GenerateCode(aeto); } catch (AssemblyResolutionException e) { MessageBox.Show("Could not load assembly upon code generation:\r" + e.AssemblyReference.FullName); } return aeto.GetDocument(); }
void RunDecompiler(string assemblyFile, string fullTypeName, ITextOutput textOutput, CancellationToken cancellationToken) { ReaderParameters readerParameters = new ReaderParameters(); // Use new assembly resolver instance so that the AssemblyDefinitions can be garbage-collected // once the code is decompiled. readerParameters.AssemblyResolver = new DefaultAssemblyResolver(); ModuleDefinition module = ModuleDefinition.ReadModule(assemblyFile, readerParameters); TypeDefinition typeDefinition = module.GetType(fullTypeName); if (typeDefinition == null) throw new InvalidOperationException("Could not find type"); DecompilerContext context = new DecompilerContext(module); context.CancellationToken = cancellationToken; AstBuilder astBuilder = new AstBuilder(context); astBuilder.AddType(typeDefinition); astBuilder.GenerateCode(textOutput); // save decompilation data var nodes = TreeTraversal .PreOrder((AstNode)astBuilder.CompilationUnit, n => n.Children) .Where(n => n is AttributedNode && n.Annotation<Tuple<int, int>>() != null); MemberReference = typeDefinition; int token = MemberReference.MetadataToken.ToInt32(); var info = new DecompileInformation { CodeMappings = astBuilder.CodeMappings, LocalVariables = astBuilder.LocalVariables, DecompiledMemberReferences = astBuilder.DecompiledMemberReferences, AstNodes = nodes }; // save the data DebuggerDecompilerService.DebugInformation.AddOrUpdate(token, info, (k, v) => info); }
public string GetClass(TypeIdentity identity) { // Before we attempt to fetch it just try a decompilation. GetAssembly(identity.AssemblyPath); ModuleDefinition moduleDef; if (!this.loadedModules.TryGetValue(identity.AssemblyPath, out moduleDef)) { // Can't find the assembly, just return nothing. return string.Empty; } TypeDefinition typeDef = moduleDef.GetType(identity.FullyQualifiedName); if (typeDef == null) { // If we can't find our type just return as well. return string.Empty; } DecompilerContext context = new DecompilerContext(moduleDef); AstBuilder astBuilder = new AstBuilder(context); astBuilder.AddType(typeDef); PlainTextOutput textOutput = new PlainTextOutput(); astBuilder.GenerateCode(textOutput); return textOutput.ToString(); }