public static List <ReferenceSegment> Decompile(TextEditorData data, ModuleDefinition module, TypeDefinition currentType, Action <AstBuilder> setData, DecompilerSettings settings)
        {
            try {
                var context = new DecompilerContext(module);
                var source  = new CancellationTokenSource();

                context.CancellationToken = source.Token;
                context.CurrentType       = currentType;

                context.Settings = settings;

                AstBuilder astBuilder = new AstBuilder(context);

                setData(astBuilder);

                astBuilder.RunTransformations(o => false);
                GeneratedCodeSettings.Default.Apply(astBuilder.SyntaxTree);
                var output = new ColoredCSharpFormatter(data.Document);
                astBuilder.GenerateCode(output);
                output.SetDocumentData();
                return(output.ReferencedSegments);
            } catch (Exception e) {
                data.Text = "Decompilation failed: \n" + e;
            }
            return(null);
        }
예제 #2
0
        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);
        }
        public static string Decompile(DomCecilMethod method, bool markup)
        {
            if (method.MethodDefinition.IsPInvokeImpl)
            {
                return(GettextCatalog.GetString("Method is P/Invoke"));
            }
            if (method.MethodDefinition.Body == null)
            {
                IType type = method.DeclaringType;
                return(type == null ||  type.ClassType == ClassType.Interface ? GettextCatalog.GetString("Interface method") : GettextCatalog.GetString("Abstract method"));
            }

            StringBuilder result = new StringBuilder();

            try {
                //ControlFlowGraph controlFlowGraph = ControlFlowGraph.Create (method.MethodDefinition);
                ILanguage lang = CSharp.GetLanguage(CSharpVersion.V3);
                ColoredCSharpFormatter formatter  = new ColoredCSharpFormatter();
                ILanguageWriter        langWriter = lang.GetWriter(formatter);
                langWriter.Write(method.MethodDefinition);
                result.Append(formatter.Text);
            } catch (Exception e) {
                result.Append("Decompilation failed: \n" + e);
            }
            return(result.ToString());
        }
예제 #4
0
        public static List <ReferenceSegment> Decompile(TextEditorData data, ModuleDefinition module, TypeDefinition currentType, Action <AstBuilder> setData, DecompilerSettings settings)
        {
            try {
                var types      = DesktopService.GetMimeTypeInheritanceChain(data.Document.MimeType);
                var codePolicy = MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy <MonoDevelop.CSharp.Formatting.CSharpFormattingPolicy> (types);

                var context = new DecompilerContext(module);
                var source  = new CancellationTokenSource();

                context.CancellationToken = source.Token;
                context.CurrentType       = currentType;

                context.Settings = settings;

                AstBuilder astBuilder = new AstBuilder(context);

                setData(astBuilder);

                astBuilder.RunTransformations(o => false);
                GeneratedCodeSettings.Default.Apply(astBuilder.CompilationUnit);
                var output = new ColoredCSharpFormatter(data.Document);
                CSharpFormattingOptions options = codePolicy.CreateOptions();
                astBuilder.GenerateCode(output, options);
                output.SetDocumentData();
                return(output.ReferencedSegments);
            } catch (Exception e) {
                data.Text = "Decompilation failed: \n" + e;
            }
            return(null);
        }
예제 #5
0
        public static List <ReferenceSegment> Decompile(TextEditorData data, ModuleDefinition module, TypeDefinition currentType, Action <AstBuilder> setData, DecompilerSettings settings)
        {
            var context = new DecompilerContext(module);
            var source  = new CancellationTokenSource();

            context.CancellationToken = source.Token;
            context.CurrentType       = currentType;
            context.Settings          = settings;
            try {
                var astBuilder = new AstBuilder(context);
                setData(astBuilder);
                astBuilder.RunTransformations(o => false);
                GeneratedCodeSettings.Default.Apply(astBuilder.SyntaxTree);
                var output = new ColoredCSharpFormatter(data.Document);
                astBuilder.GenerateCode(output);
                output.SetDocumentData();
                return(output.ReferencedSegments);
            } catch (Exception e) {
                // exception  -> try to decompile without method bodies
                try {
                    var astBuilder = new AstBuilder(context);
                    astBuilder.DecompileMethodBodies = false;
                    setData(astBuilder);
                    astBuilder.RunTransformations(o => false);
                    GeneratedCodeSettings.Default.Apply(astBuilder.SyntaxTree);
                    var output = new ColoredCSharpFormatter(data.Document);
                    astBuilder.GenerateCode(output);
                    output.SetDocumentData();
                    data.Document.Insert(data.Document.TextLength, "/* body decompilation failed: \n" + e + " */");
                } catch (Exception e2) {
                    data.Text = "/* fallback decompilation failed: \n" + e2 + "*/";
                }
            }
            return(null);
        }
예제 #6
0
        public static List <ReferenceSegment> Disassemble(TextEditor data, Action <ReflectionDisassembler> setData)
        {
            var source       = new CancellationTokenSource();
            var output       = new ColoredCSharpFormatter(data);
            var disassembler = new ReflectionDisassembler(output, source.Token);

            setData(disassembler);
            output.SetDocumentData();
            return(output.ReferencedSegments);
        }
        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> ();
            }));
        }
		public static List<ReferenceSegment> Disassemble (TextEditorData data, Action<ReflectionDisassembler> setData)
		{
			var source = new CancellationTokenSource ();
			var output = new ColoredCSharpFormatter (data.Document);
			var disassembler = new ReflectionDisassembler (output, true, source.Token);
			setData (disassembler);
			output.SetDocumentData ();
			return output.ReferencedSegments;
		}
		public static List<ReferenceSegment> Decompile (TextEditorData data, ModuleDefinition module, TypeDefinition currentType, Action<AstBuilder> setData, DecompilerSettings settings)
		{
			try {

				var context = new DecompilerContext (module);
				var source = new CancellationTokenSource ();
				
				context.CancellationToken = source.Token;
				context.CurrentType = currentType;
				
				context.Settings = settings;
				
				AstBuilder astBuilder = new AstBuilder (context);
				
				setData (astBuilder);
				
				astBuilder.RunTransformations (o => false);
				GeneratedCodeSettings.Default.Apply (astBuilder.CompilationUnit);
				var output = new ColoredCSharpFormatter (data.Document);
				astBuilder.GenerateCode (output);
				output.SetDocumentData ();
				return output.ReferencedSegments;
			} catch (Exception e) {
				data.Text = "Decompilation failed: \n" + e;
			}
			return null;
		}
		public static List<ReferenceSegment> Decompile (TextEditor data, ModuleDefinition module, TypeDefinition currentType, Action<AstBuilder> setData, DecompilerSettings settings)
		{
			var context = new DecompilerContext (module);
			var source = new CancellationTokenSource ();
			context.CancellationToken = source.Token;
			context.CurrentType = currentType;
			context.Settings = settings;
			try {
				var astBuilder = new AstBuilder (context);
				setData (astBuilder);
				astBuilder.RunTransformations (o => false);
				GeneratedCodeSettings.Default.Apply (astBuilder.SyntaxTree);
				var output = new ColoredCSharpFormatter (data);
				astBuilder.GenerateCode (output);
				output.SetDocumentData ();
				return output.ReferencedSegments;
			} catch (Exception e) {
				// exception  -> try to decompile without method bodies
				try {
					var astBuilder = new AstBuilder (context);
					astBuilder.DecompileMethodBodies = false;
					setData (astBuilder);
					astBuilder.RunTransformations (o => false);
					GeneratedCodeSettings.Default.Apply (astBuilder.SyntaxTree);
					var output = new ColoredCSharpFormatter (data);
					astBuilder.GenerateCode (output);
					output.SetDocumentData ();
					data.InsertText (data.Length, "/* body decompilation failed: \n" + e + " */"); 
				} catch (Exception e2) {
					data.Text = "/* fallback decompilation failed: \n" + e2 +"*/";
				}
			}
			return null;
		}
예제 #11
0
		public static List<ReferenceSegment> Decompile (TextEditorData data, ModuleDefinition module, TypeDefinition currentType, Action<AstBuilder> setData)
		{
			try {
				var types = DesktopService.GetMimeTypeInheritanceChain (data.Document.MimeType);
				var codePolicy = MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy<MonoDevelop.CSharp.Formatting.CSharpFormattingPolicy> (types);
				
				var context = new DecompilerContext (module);
				var source = new CancellationTokenSource ();
				
				context.CancellationToken = source.Token;
				context.CurrentType = currentType;
				
				context.Settings = new DecompilerSettings () {
					AnonymousMethods = true,
					AutomaticEvents  = true,
					AutomaticProperties = true,
					ForEachStatement = true,
					LockStatement = true
				};
				
				AstBuilder astBuilder = new AstBuilder (context);
				
				setData (astBuilder);
				
				astBuilder.RunTransformations (o => false);
				var output = new ColoredCSharpFormatter (data.Document);
				astBuilder.GenerateCode (output, codePolicy.CreateOptions ());
				output.SetDocumentData ();
				return output.ReferencedSegments;
			} catch (Exception e) {
				data.Text = "Decompilation failed: \n" + e;
			}
			return null;
		}
		public static string Decompile (DomCecilMethod method, bool markup)
		{
			if (method.MethodDefinition.IsPInvokeImpl)
				return GettextCatalog.GetString ("Method is P/Invoke");
			if (method.MethodDefinition.Body == null) {
				IType type = method.DeclaringType;
				return type == null || type.ClassType == ClassType.Interface ? GettextCatalog.GetString ("Interface method") : GettextCatalog.GetString ("Abstract method");
			}
			
			StringBuilder result = new StringBuilder ();
			try {
				//ControlFlowGraph controlFlowGraph = ControlFlowGraph.Create (method.MethodDefinition);
				ILanguage lang = CSharp.GetLanguage (CSharpVersion.V3);
				ColoredCSharpFormatter formatter = new ColoredCSharpFormatter ();
				ILanguageWriter langWriter = lang.GetWriter (formatter);
				langWriter.Write (method.MethodDefinition);
				result.Append (formatter.Text);
			} catch (Exception e) {
				result.Append ("Decompilation failed: \n" + e);
			}
			return result.ToString ();
		}