public static List <ReferenceSegment> Decompile(TextEditor data, AssemblyLoader assemblyLoader, Func <CSharpDecompiler, SyntaxTree> decompile, DecompilerSettings settings = null, DecompileFlags flags = null) { settings = settings ?? GetDecompilerSettings(data, publicOnly: flags.PublicOnly); var csharpDecompiler = assemblyLoader.CSharpDecompiler; try { var syntaxTree = decompile(csharpDecompiler); if (!flags.MethodBodies) { MethodBodyRemoveVisitor.RemoveMethodBodies(syntaxTree); } var output = new ColoredCSharpFormatter(data); TokenWriter tokenWriter = new TextTokenWriter(output, settings, csharpDecompiler.TypeSystem) { FoldBraces = settings.FoldBraces }; var formattingPolicy = settings.CSharpFormattingOptions; syntaxTree.AcceptVisitor(new CSharpOutputVisitor(tokenWriter, formattingPolicy)); output.SetDocumentData(); return(output.ReferencedSegments); } catch (Exception e) { data.InsertText(data.Length, "/* decompilation failed: \n" + e + " */"); } return(null); }
void WriteCode(ITextOutput output, DecompilerSettings settings, SyntaxTree syntaxTree, IDecompilerTypeSystem typeSystem) { syntaxTree.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true }); TokenWriter tokenWriter = new TextTokenWriter(output, settings, typeSystem) { FoldBraces = settings.FoldBraces, /*ExpandMemberDefinitions = settings.ExpandMemberDefinitions */ }; syntaxTree.AcceptVisitor(new CSharpOutputVisitor(tokenWriter, settings.CSharpFormattingOptions)); }
public override void GenerateCode(ITextOutput output) { syntaxTree.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true }); TextTokenWriter outputFormatter = new TextTokenWriter(output, context) { FoldBraces = context.Settings.FoldBraces }; CSharpFormattingOptions formattingPolicy = context.Settings.CSharpFormattingOptions; syntaxTree.AcceptVisitor(new ClassFieldsOutputVisitor(outputFormatter, formattingPolicy)); }
void WriteCode(ITextOutput output, DecompilerSettings settings, SyntaxTree syntaxTree, IDecompilerTypeSystem typeSystem) { syntaxTree.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true }); var outputFormatter = new TextTokenWriter(output, settings, typeSystem) { FoldBraces = settings.FoldBraces }; var formattingPolicy = settings.CSharpFormattingOptions; syntaxTree.AcceptVisitor(new CSharpOutputVisitor(outputFormatter, formattingPolicy)); }
void WriteCode(ITextOutput output, DecompilerSettings settings, SyntaxTree syntaxTree, IDecompilerTypeSystem typeSystem) { syntaxTree.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true }); output.IndentationString = settings.CSharpFormattingOptions.IndentationString; TokenWriter tokenWriter = new TextTokenWriter(output, settings, typeSystem); if (output is ISmartTextOutput highlightingOutput) { tokenWriter = new CSharpHighlightingTokenWriter(tokenWriter, highlightingOutput); } syntaxTree.AcceptVisitor(new CSharpOutputVisitor(tokenWriter, settings.CSharpFormattingOptions)); }
public override void GenerateCode(ITextOutput output) { syntaxTree.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true }); TextTokenWriter outputFormatter = new TextTokenWriter(output, context) { FoldBraces = context.Settings.FoldBraces }; CSharpFormattingOptions formattingPolicy = context.Settings.CSharpFormattingOptions; MethodBodyOutputVisitor vis = new MethodBodyOutputVisitor(outputFormatter, formattingPolicy); vis.SetUpGenericSubstitition(this._rewrite); syntaxTree.AcceptVisitor(vis); }
public static Task <List <ReferenceSegment> > DecompileAsync(TextEditor data, AssemblyLoader assemblyLoader, Func <CSharpDecompiler, SyntaxTree> decompile, DecompilerSettings settings = null, DecompileFlags flags = null) { if (data == null) { throw new ArgumentNullException(nameof(data)); } if (assemblyLoader == null) { throw new ArgumentNullException(nameof(assemblyLoader)); } return(Task.Run(async delegate { settings = settings ?? GetDecompilerSettings(data, publicOnly: flags.PublicOnly); var csharpDecompiler = assemblyLoader.CSharpDecompiler; try { var syntaxTree = decompile(csharpDecompiler); if (!flags.MethodBodies) { MethodBodyRemoveVisitor.RemoveMethodBodies(syntaxTree); } return await Runtime.RunInMainThread(delegate { if (data.IsDisposed) { return new List <ReferenceSegment> (); } var output = new ColoredCSharpFormatter(data); TokenWriter tokenWriter = new TextTokenWriter(output, settings, csharpDecompiler.TypeSystem) { FoldBraces = settings.FoldBraces }; var formattingPolicy = settings.CSharpFormattingOptions; syntaxTree.AcceptVisitor(new CSharpOutputVisitor(tokenWriter, formattingPolicy)); output.SetDocumentData(); return output.ReferencedSegments; }); } catch (Exception e) { await Runtime.RunInMainThread(delegate { data.InsertText(data.Length, "/* decompilation failed: \n" + e + " */"); }); } return new List <ReferenceSegment> (); })); }
//private static EcsFile GenerateFile(TypeDefinition t, CSharpDecompiler codeDomBuilder, string attributeString, OutputMode mode, string ext) //{ // var sourceRes = t.CustomAttributes.SingleOrDefault(x => x.AttributeType.Name == "Custom" + attributeString + "File"); // var sourceAtt = t.CustomAttributes.SingleOrDefault(x => x.AttributeType.Name == "Custom" + attributeString); // if (sourceRes != null) { // var sourceName = t.Name + ext; // sourceName = (string)sourceRes.ConstructorArguments[0].Value; // var sourceContent = EmbeddedFileLoader.GetResourceFile(sourceName, t.Module); // return new EcsFile(sourceName, sourceContent); // } // if (sourceAtt != null) { // var sourceContent = (string)sourceAtt.ConstructorArguments[0].Value; // return new EcsFile(t.Name + ext, sourceContent); // } // return new EcsFile(t.Name + ext, WriteType(codeDomBuilder, mode)); //} string WriteCode(DecompilerSettings settings, SyntaxTree syntaxTree, IDecompilerTypeSystem typeSystem, OutputMode mode) { //syntaxTree.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true }); var ms = new MemoryStream(); using (StreamWriter w = new StreamWriter(ms)) { //codeDomBuilder.GenerateCode(new PlainTextOutput(w), mode); TokenWriter tokenWriter = new TextTokenWriter(new PlainTextOutput(w), settings, typeSystem) { FoldBraces = settings.FoldBraces, ExpandMemberDefinitions = settings.ExpandMemberDefinitions }; //syntaxTree.AcceptVisitor(new CSharpOutputVisitor(tokenWriter, settings.CSharpFormattingOptions)); syntaxTree.AcceptVisitor(new COutputVisitor(tokenWriter, settings.CSharpFormattingOptions, mode)); w.Flush(); ms.Position = 0; var sr = new StreamReader(ms); var myStr = sr.ReadToEnd(); return(myStr); } }
public ClassFieldsOutputVisitor(TextTokenWriter formatter, CSharpFormattingOptions formattingPolicy) : base(formatter, formattingPolicy) { }
public MethodParametersOutputVisitor(TextTokenWriter formatter, CSharpFormattingOptions formattingPolicy) : base(formatter, formattingPolicy) { }
public MethodSignatureOutputVisitor(TextTokenWriter formatter, CSharpFormattingOptions formattingPolicy) : base(formatter, formattingPolicy) { }
public CPlusPlusCLIOutputVisitor(TextTokenWriter formatter, CSharpFormattingOptions formattingPolicy) : base(formatter, formattingPolicy) { }
string decompileAssembly(string path) { Step("Reading IL"); var parameters = new ReaderParameters { AssemblyResolver = this, }; var asm = AssemblyDefinition.ReadAssembly(path, parameters); mscorlib = AssemblyDefinition.ReadAssembly(typeof(String).Assembly.Location, parameters); system = AssemblyDefinition.ReadAssembly(typeof(INotifyPropertyChanged).Assembly.Location, parameters); systemCore = AssemblyDefinition.ReadAssembly(typeof(Enumerable).Assembly.Location, parameters); systemDrawing = AssemblyDefinition.ReadAssembly(typeof(System.Drawing.Bitmap).Assembly.Location, parameters); Step("Decompiling IL to C#"); var context = new DecompilerContext(asm.MainModule); context.Settings.ForEachStatement = false; context.Settings.ObjectOrCollectionInitializers = false; context.Settings.UsingStatement = false; context.Settings.AsyncAwait = false; context.Settings.AutomaticProperties = true; context.Settings.AutomaticEvents = true; context.Settings.QueryExpressions = false; context.Settings.AlwaysGenerateExceptionVariableForCatchBlocks = true; context.Settings.UsingDeclarations = true; context.Settings.FullyQualifyAmbiguousTypeNames = true; context.Settings.YieldReturn = false; var builder = new AstBuilder(context); builder.AddAssembly(asm); foreach (var a in referencedAssemblies.Values) { if (a != null) { builder.AddAssembly(a); } } /*{ * var type = asm.MainModule.Types.ElementAt(16); * Console.WriteLine(type + "::"); * var astBuilder = new AstBuilder(new DecompilerContext(asm.MainModule) { CurrentType = type, Settings=context.Settings.Clone()}); * astBuilder.AddType(type); * astBuilder.RunTransformations(); * var op = new PlainTextOutput(); * astBuilder.GenerateCode(op); * Console.WriteLine(op.ToString()); * }*/ foreach (var transform in CreatePipeline(context)) { transform.Run(builder.SyntaxTree); } builder.SyntaxTree.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true }); var str = new StringWriter(); var outputFormatter = new TextTokenWriter(new PlainTextOutput(str), context) { FoldBraces = context.Settings.FoldBraces }; builder.SyntaxTree.AcceptVisitor(new CSharpOutputVisitor(outputFormatter, context.Settings.CSharpFormattingOptions)); return(str.GetStringBuilder().ToString()); }