From f45dcf0e62cb430e7508a3001448b55a91c0caf2 Mon Sep 17 00:00:00 2001 From: kmatyaszek Date: Mon, 24 Sep 2018 12:03:57 -0700 Subject: [PATCH] #1284: Add new option "Show IL code" to ICSharpCode.Decompiler.Console --- ICSharpCode.Decompiler.Console/Program.cs | 29 ++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/ICSharpCode.Decompiler.Console/Program.cs b/ICSharpCode.Decompiler.Console/Program.cs index ca8f96cf9..a85184490 100644 --- a/ICSharpCode.Decompiler.Console/Program.cs +++ b/ICSharpCode.Decompiler.Console/Program.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -6,6 +6,9 @@ using McMaster.Extensions.CommandLineUtils; using ICSharpCode.Decompiler.CSharp; using ICSharpCode.Decompiler.TypeSystem; using ICSharpCode.Decompiler.Metadata; +using ICSharpCode.Decompiler.Disassembler; +using System.Threading; +using System.Reflection.Metadata; namespace ICSharpCode.Decompiler.Console { @@ -24,6 +27,7 @@ namespace ICSharpCode.Decompiler.Console var outputOption = app.Option("-o|--outputdir ", "The output directory, if omitted decompiler output is written to standard out.", CommandOptionType.SingleValue); var typeOption = app.Option("-t|--type ", "The fully qualified name of the type to decompile.", CommandOptionType.SingleValue); var listOption = app.Option("-l|--list ", "Lists all entities of the specified type(s). Valid types: c(lass), i(interface), s(truct), d(elegate), e(num)", CommandOptionType.MultipleValue); + var ilViewerOption = app.Option("-il|--ilcode", "Show IL code.", CommandOptionType.NoValue); app.ExtendedHelpText = Environment.NewLine + "-o is valid with every option and required when using -p."; app.ThrowOnUnexpectedArgument = false; // Ignore invalid arguments / options @@ -60,6 +64,14 @@ namespace ICSharpCode.Decompiler.Console output = File.CreateText(Path.Combine(directory, outputName) + ".list.txt"); } ListContent(inputAssemblyFileName.Value, output, kinds); + } else if (ilViewerOption.HasValue()) { + TextWriter output = System.Console.Out; + if (outputOption.HasValue()) { + string directory = outputOption.Value(); + string outputName = Path.GetFileNameWithoutExtension(inputAssemblyFileName.Value); + output = File.CreateText(Path.Combine(directory, outputName) + ".ILCode.txt"); + } + ShowIL(inputAssemblyFileName.Value, output); } else { TextWriter output = System.Console.Out; if (outputOption.HasValue()) { @@ -91,6 +103,21 @@ namespace ICSharpCode.Decompiler.Console } } + static void ShowIL(string assemblyFileName, TextWriter output) + { + CSharpDecompiler decompiler = GetDecompiler(assemblyFileName); + ITextOutput textOutput = new PlainTextOutput(); + ReflectionDisassembler disassembler = new ReflectionDisassembler(textOutput, CancellationToken.None); + + disassembler.DisassembleNamespace(decompiler.TypeSystem.MainModule.RootNamespace.Name, + decompiler.TypeSystem.MainModule.PEFile, + decompiler.TypeSystem.MainModule.TypeDefinitions.Select(x => (TypeDefinitionHandle)x.MetadataToken)); + + output.WriteLine($"IL code: {decompiler.TypeSystem.MainModule.AssemblyName}"); + output.WriteLine(textOutput.ToString()); + output.Flush(); + } + static void DecompileAsProject(string assemblyFileName, string outputDirectory) { WholeProjectDecompiler decompiler = new WholeProjectDecompiler();