From 2dc1dd4efcda99d93993a34b308a7010cfa032e7 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Wed, 2 Oct 2019 20:10:17 +0200 Subject: [PATCH] Refactor IlspyCmdProgram to use non-static methods. --- .../IlspyCmdProgram.cs | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/ICSharpCode.Decompiler.Console/IlspyCmdProgram.cs b/ICSharpCode.Decompiler.Console/IlspyCmdProgram.cs index 3adfe47c0..9cfb7f62b 100644 --- a/ICSharpCode.Decompiler.Console/IlspyCmdProgram.cs +++ b/ICSharpCode.Decompiler.Console/IlspyCmdProgram.cs @@ -61,11 +61,11 @@ Remarks: private int OnExecute(CommandLineApplication app) { TextWriter output = System.Console.Out; - bool outputDirectorySpecified = !String.IsNullOrEmpty(OutputDirectory); + bool outputDirectorySpecified = !string.IsNullOrEmpty(OutputDirectory); try { if (CreateCompilableProjectFlag) { - DecompileAsProject(InputAssemblyName, OutputDirectory, ReferencePaths); + return DecompileAsProject(InputAssemblyName, OutputDirectory); } else if (EntityTypes.Any()) { var values = EntityTypes.SelectMany(v => v.Split(',', ';')).ToArray(); HashSet kinds = TypesParser.ParseSelection(values); @@ -74,14 +74,14 @@ Remarks: output = File.CreateText(Path.Combine(OutputDirectory, outputName) + ".list.txt"); } - ListContent(InputAssemblyName, output, kinds, ReferencePaths); + return ListContent(InputAssemblyName, output, kinds); } else if (ShowILCodeFlag) { if (outputDirectorySpecified) { string outputName = Path.GetFileNameWithoutExtension(InputAssemblyName); output = File.CreateText(Path.Combine(OutputDirectory, outputName) + ".il"); } - ShowIL(InputAssemblyName, output, ReferencePaths); + return ShowIL(InputAssemblyName, output); } else if (CreateDebugInfoFlag) { string pdbFileName = null; if (outputDirectorySpecified) { @@ -91,7 +91,7 @@ Remarks: pdbFileName = Path.ChangeExtension(InputAssemblyName, ".pdb"); } - return GeneratePdbForAssembly(InputAssemblyName, pdbFileName, ReferencePaths, app); + return GeneratePdbForAssembly(InputAssemblyName, pdbFileName, app); } else if (ShowVersion) { string vInfo = "ilspycmd: " + typeof(ILSpyCmdProgram).Assembly.GetName().Version.ToString() + Environment.NewLine @@ -102,10 +102,10 @@ Remarks: if (outputDirectorySpecified) { string outputName = Path.GetFileNameWithoutExtension(InputAssemblyName); output = File.CreateText(Path.Combine(OutputDirectory, - (String.IsNullOrEmpty(TypeName) ? outputName : TypeName) + ".decompiled.cs")); + (string.IsNullOrEmpty(TypeName) ? outputName : TypeName) + ".decompiled.cs")); } - Decompile(InputAssemblyName, output, ReferencePaths, TypeName); + return Decompile(InputAssemblyName, output, TypeName); } } catch (Exception ex) { app.Error.WriteLine(ex.ToString()); @@ -117,28 +117,29 @@ Remarks: return 0; } - static CSharpDecompiler GetDecompiler(string assemblyFileName, string[] referencePaths) + CSharpDecompiler GetDecompiler(string assemblyFileName) { var module = new PEFile(assemblyFileName); var resolver = new UniversalAssemblyResolver(assemblyFileName, false, module.Reader.DetectTargetFrameworkId()); - foreach (var path in referencePaths) { + foreach (var path in ReferencePaths) { resolver.AddSearchDirectory(path); } return new CSharpDecompiler(assemblyFileName, resolver, new DecompilerSettings()); } - static void ListContent(string assemblyFileName, TextWriter output, ISet kinds, string[] referencePaths) + int ListContent(string assemblyFileName, TextWriter output, ISet kinds) { - CSharpDecompiler decompiler = GetDecompiler(assemblyFileName, referencePaths); + CSharpDecompiler decompiler = GetDecompiler(assemblyFileName); foreach (var type in decompiler.TypeSystem.MainModule.TypeDefinitions) { if (!kinds.Contains(type.Kind)) continue; output.WriteLine($"{type.Kind} {type.FullName}"); } + return 0; } - static void ShowIL(string assemblyFileName, TextWriter output, string[] referencePaths) + int ShowIL(string assemblyFileName, TextWriter output) { CSharpDecompiler decompiler = GetDecompiler(assemblyFileName, referencePaths); ITextOutput textOutput = new PlainTextOutput(); @@ -150,23 +151,25 @@ Remarks: output.WriteLine($"// IL code: {decompiler.TypeSystem.MainModule.AssemblyName}"); output.WriteLine(textOutput.ToString()); + return 0; } - static void DecompileAsProject(string assemblyFileName, string outputDirectory, string[] referencePaths) + int DecompileAsProject(string assemblyFileName, string outputDirectory) { var decompiler = new WholeProjectDecompiler(); var module = new PEFile(assemblyFileName); var resolver = new UniversalAssemblyResolver(assemblyFileName, false, module.Reader.DetectTargetFrameworkId()); - foreach (var path in referencePaths) { + foreach (var path in ReferencePaths) { resolver.AddSearchDirectory(path); } decompiler.AssemblyResolver = resolver; decompiler.DecompileProject(module, outputDirectory); + return 0; } - static void Decompile(string assemblyFileName, TextWriter output, string[] referencePaths, string typeName = null) + int Decompile(string assemblyFileName, TextWriter output, string typeName = null) { - CSharpDecompiler decompiler = GetDecompiler(assemblyFileName, referencePaths); + CSharpDecompiler decompiler = GetDecompiler(assemblyFileName); if (typeName == null) { output.Write(decompiler.DecompileWholeModuleAsString()); @@ -174,9 +177,10 @@ Remarks: var name = new FullTypeName(typeName); output.Write(decompiler.DecompileTypeAsString(name)); } + return 0; } - static int GeneratePdbForAssembly(string assemblyFileName, string pdbFileName, string[] referencePaths, CommandLineApplication app) + int GeneratePdbForAssembly(string assemblyFileName, string pdbFileName, CommandLineApplication app) { var module = new PEFile(assemblyFileName, new FileStream(assemblyFileName, FileMode.Open, FileAccess.Read), @@ -189,7 +193,7 @@ Remarks: } using (FileStream stream = new FileStream(pdbFileName, FileMode.OpenOrCreate, FileAccess.Write)) { - var decompiler = GetDecompiler(assemblyFileName, referencePaths); + var decompiler = GetDecompiler(assemblyFileName); PortablePdbWriter.WritePdb(module, decompiler, new DecompilerSettings() { ThrowOnAssemblyResolveErrors = false }, stream); }