Example #1
0
        static int Main(string[] args)
        {
            // https://github.com/natemcmaster/CommandLineUtils/
            // Older cmd line clients (for options reference): https://github.com/aerror2/ILSpy-For-MacOSX and https://github.com/andreif/ILSpyMono
            var app = new CommandLineApplication();

            app.LongVersionGetter = () => "ilspycmd " + typeof(FullTypeName).Assembly.GetName().Version.ToString();
            app.HelpOption("-h|--help");
            var inputAssemblyFileName = app.Argument("Assembly filename name", "The assembly that is being decompiled. This argument is mandatory.");
            var projectOption         = app.Option("-p|--project", "Decompile assembly as compilable project. This requires the output directory option.", CommandOptionType.NoValue);
            var outputOption          = app.Option("-o|--outputdir <directory>", "The output directory, if omitted decompiler output is written to standard out.", CommandOptionType.SingleValue);
            var typeOption            = app.Option("-t|--type <type-name>", "The fully qualified name of the type to decompile.", CommandOptionType.SingleValue);
            var listOption            = app.Option("-l|--list <entity-type(s)>", "Lists all entities of the specified type(s). Valid types: c(lass), i(interface), s(truct), d(elegate), e(num)", CommandOptionType.MultipleValue);

            app.ExtendedHelpText = Environment.NewLine + "-o is valid with every option and required when using -p.";

            app.ThrowOnUnexpectedArgument = false;             // Ignore invalid arguments / options

            app.OnExecute(() => {
                // HACK : the CommandLineUtils package does not allow us to specify an argument as mandatory.
                // Therefore we're implementing it as simple as possible.
                if (inputAssemblyFileName.Value == null)
                {
                    app.ShowVersion();
                    app.ShowHint();
                    return(-1);
                }
                if (!File.Exists(inputAssemblyFileName.Value))
                {
                    app.Error.WriteLine($"ERROR: Input file not found.");
                    return(-1);
                }
                if (!outputOption.HasValue() && projectOption.HasValue())
                {
                    app.Error.WriteLine($"ERROR: Output directory not speciified.");
                    return(-1);
                }
                if (outputOption.HasValue() && !Directory.Exists(outputOption.Value()))
                {
                    app.Error.WriteLine($"ERROR: Output directory '{outputOption.Value()}' does not exist.");
                    return(-1);
                }
                if (projectOption.HasValue())
                {
                    DecompileAsProject(inputAssemblyFileName.Value, outputOption.Value());
                }
                else if (listOption.HasValue())
                {
                    var values = listOption.Values.SelectMany(v => v.Split(',', ';')).ToArray();
                    HashSet <TypeKind> kinds = TypesParser.ParseSelection(values);
                    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) + ".list.txt");
                    }
                    ListContent(inputAssemblyFileName.Value, output, kinds);
                }
                else
                {
                    TextWriter output = System.Console.Out;
                    if (outputOption.HasValue())
                    {
                        string directory  = outputOption.Value();
                        string outputName = Path.GetFileNameWithoutExtension(inputAssemblyFileName.Value);
                        output            = File.CreateText(Path.Combine(directory, (typeOption.Value() ?? outputName) + ".decompiled.cs"));
                    }
                    Decompile(inputAssemblyFileName.Value, output, typeOption.Value());
                }
                return(0);
            });

            return(app.Execute(args));
        }
Example #2
0
        private int OnExecute(CommandLineApplication app)
        {
            TextWriter output = System.Console.Out;
            bool       outputDirectorySpecified = !String.IsNullOrEmpty(OutputDirectory);

            try {
                if (CreateCompilableProjectFlag)
                {
                    DecompileAsProject(InputAssemblyName, OutputDirectory, ReferencePaths);
                }
                else if (EntityTypes.Any())
                {
                    var values = EntityTypes.SelectMany(v => v.Split(',', ';')).ToArray();
                    HashSet <TypeKind> kinds = TypesParser.ParseSelection(values);
                    if (outputDirectorySpecified)
                    {
                        string outputName = Path.GetFileNameWithoutExtension(InputAssemblyName);
                        output = File.CreateText(Path.Combine(OutputDirectory, outputName) + ".list.txt");
                    }

                    ListContent(InputAssemblyName, output, kinds, ReferencePaths);
                }
                else if (ShowILCodeFlag)
                {
                    if (outputDirectorySpecified)
                    {
                        string outputName = Path.GetFileNameWithoutExtension(InputAssemblyName);
                        output = File.CreateText(Path.Combine(OutputDirectory, outputName) + ".il");
                    }

                    ShowIL(InputAssemblyName, output, ReferencePaths);
                }
                else if (CreateDebugInfoFlag)
                {
                    string pdbFileName = null;
                    if (outputDirectorySpecified)
                    {
                        string outputName = Path.GetFileNameWithoutExtension(InputAssemblyName);
                        pdbFileName = Path.Combine(OutputDirectory, outputName) + ".pdb";
                    }
                    else
                    {
                        pdbFileName = Path.ChangeExtension(InputAssemblyName, ".pdb");
                    }

                    return(GeneratePdbForAssembly(InputAssemblyName, pdbFileName, ReferencePaths, app));
                }
                else if (ShowVersion)
                {
                    string vInfo = "ilspycmd: " + typeof(ILSpyCmdProgram).Assembly.GetName().Version.ToString() +
                                   Environment.NewLine
                                   + "ICSharpCode.Decompiler: " +
                                   typeof(FullTypeName).Assembly.GetName().Version.ToString();
                    output.WriteLine(vInfo);
                }
                else
                {
                    if (outputDirectorySpecified)
                    {
                        string outputName = Path.GetFileNameWithoutExtension(InputAssemblyName);
                        output = File.CreateText(Path.Combine(OutputDirectory,
                                                              (String.IsNullOrEmpty(TypeName) ? outputName : TypeName) + ".decompiled.cs"));
                    }

                    Decompile(InputAssemblyName, output, ReferencePaths, TypeName);
                }
            } catch (Exception ex) {
                app.Error.WriteLine(ex.ToString());
                return(ProgramExitCodes.EX_SOFTWARE);
            } finally {
                output.Close();
            }

            return(0);
        }