@ -15,11 +15,11 @@ using ICSharpCode.Decompiler.CSharp.ProjectDecompiler;
@@ -15,11 +15,11 @@ using ICSharpCode.Decompiler.CSharp.ProjectDecompiler;
using ICSharpCode.Decompiler.DebugInfo ;
using ICSharpCode.Decompiler.Disassembler ;
using ICSharpCode.Decompiler.Metadata ;
using ICSharpCode.Decompiler.Solution ;
using ICSharpCode.Decompiler.TypeSystem ;
using ICSharpCode.ILSpyX.PdbProvider ;
using McMaster.Extensions.CommandLineUtils ;
// ReSharper disable All
namespace ICSharpCode.ILSpyCmd
{
@ -36,12 +36,11 @@ Remarks:
@@ -36,12 +36,11 @@ Remarks:
{
public static int Main ( string [ ] args ) = > CommandLineApplication . Execute < ILSpyCmdProgram > ( args ) ;
[FileExists ]
[Files Exist]
[Required]
[Argument(0, "Assembly file name", "The assembly that is being decompiled. This argument is mandatory.")]
public string InputAssemblyName { get ; }
[Argument(0, "Assembly file name(s)", "The list of assemblies that is being decompiled. This argument is mandatory.")]
public string [ ] InputAssemblyNames { get ; }
[DirectoryExists]
[Option("-o|--outputdir <directory>", "The output directory, if omitted decompiler output is written to standard out.", CommandOptionType.SingleValue)]
public string OutputDirectory { get ; }
@ -98,74 +97,108 @@ Remarks:
@@ -98,74 +97,108 @@ Remarks:
TextWriter output = System . Console . Out ;
bool outputDirectorySpecified = ! string . IsNullOrEmpty ( OutputDirectory ) ;
if ( outputDirectorySpecified )
{
Directory . CreateDirectory ( OutputDirectory ) ;
}
try
{
if ( CreateCompilableProjectFlag )
{
return DecompileAsProject ( InputAssemblyName , OutputDirectory ) ;
if ( InputAssemblyNames . Length = = 1 )
{
string projectFileName = Path . Combine ( Environment . CurrentDirectory , OutputDirectory , Path . GetFileNameWithoutExtension ( InputAssemblyNames [ 0 ] ) + ".csproj" ) ;
DecompileAsProject ( InputAssemblyNames [ 0 ] , projectFileName ) ;
return 0 ;
}
var projects = new List < ProjectItem > ( ) ;
foreach ( var file in InputAssemblyNames )
{
string projectFileName = Path . Combine ( Environment . CurrentDirectory , OutputDirectory , Path . GetFileNameWithoutExtension ( file ) , Path . GetFileNameWithoutExtension ( file ) + ".csproj" ) ;
Directory . CreateDirectory ( Path . GetDirectoryName ( projectFileName ) ) ;
ProjectId projectId = DecompileAsProject ( file , projectFileName ) ;
projects . Add ( new ProjectItem ( projectFileName , projectId . PlatformName , projectId . Guid , projectId . TypeGuid ) ) ;
}
SolutionCreator . WriteSolutionFile ( Path . Combine ( Environment . CurrentDirectory , OutputDirectory , Path . GetFileNameWithoutExtension ( OutputDirectory ) + ".sln" ) , projects ) ;
return 0 ;
}
else if ( EntityTypes . Any ( ) )
else
{
foreach ( var file in InputAssemblyNames )
{
int result = PerformPerFileAction ( file ) ;
if ( result ! = 0 )
return result ;
}
return 0 ;
}
}
catch ( Exception ex )
{
app . Error . WriteLine ( ex . ToString ( ) ) ;
return ProgramExitCodes . EX_SOFTWARE ;
}
finally
{
output . Close ( ) ;
}
int PerformPerFileAction ( string fileName )
{
if ( EntityTypes . Any ( ) )
{
var values = EntityTypes . SelectMany ( v = > v . Split ( ',' , ';' ) ) . ToArray ( ) ;
HashSet < TypeKind > kinds = TypesParser . ParseSelection ( values ) ;
if ( outputDirectorySpecified )
{
string outputName = Path . GetFileNameWithoutExtension ( InputAssemblyName ) ;
string outputName = Path . GetFileNameWithoutExtension ( file Name) ;
output = File . CreateText ( Path . Combine ( OutputDirectory , outputName ) + ".list.txt" ) ;
}
return ListContent ( InputAssemblyName , output , kinds ) ;
return ListContent ( file Name, output , kinds ) ;
}
else if ( ShowILCodeFlag | | ShowILSequencePointsFlag )
{
if ( outputDirectorySpecified )
{
string outputName = Path . GetFileNameWithoutExtension ( InputAssemblyName ) ;
string outputName = Path . GetFileNameWithoutExtension ( file Name) ;
output = File . CreateText ( Path . Combine ( OutputDirectory , outputName ) + ".il" ) ;
}
return ShowIL ( InputAssemblyName , output ) ;
return ShowIL ( file Name, output ) ;
}
else if ( CreateDebugInfoFlag )
{
string pdbFileName = null ;
if ( outputDirectorySpecified )
{
string outputName = Path . GetFileNameWithoutExtension ( InputAssembly Name) ;
string outputName = Path . GetFileNameWithoutExtension ( file Name) ;
pdbFileName = Path . Combine ( OutputDirectory , outputName ) + ".pdb" ;
}
else
{
pdbFileName = Path . ChangeExtension ( InputAssembly Name, ".pdb" ) ;
pdbFileName = Path . ChangeExtension ( file Name, ".pdb" ) ;
}
return GeneratePdbForAssembly ( InputAssembly Name, pdbFileName , app ) ;
return GeneratePdbForAssembly ( file Name, pdbFileName , app ) ;
}
else if ( DumpPackageFlag )
{
return DumpPackageAssemblies ( InputAssembly Name, OutputDirectory , app ) ;
return DumpPackageAssemblies ( file Name, OutputDirectory , app ) ;
}
else
{
if ( outputDirectorySpecified )
{
string outputName = Path . GetFileNameWithoutExtension ( InputAssembly Name) ;
string outputName = Path . GetFileNameWithoutExtension ( file Name) ;
output = File . CreateText ( Path . Combine ( OutputDirectory ,
( string . IsNullOrEmpty ( TypeName ) ? outputName : TypeName ) + ".decompiled.cs" ) ) ;
}
return Decompile ( InputAssembly Name, output , TypeName ) ;
return Decompile ( file Name, output , TypeName ) ;
}
}
catch ( Exception ex )
{
app . Error . WriteLine ( ex . ToString ( ) ) ;
return ProgramExitCodes . EX_SOFTWARE ;
}
finally
{
output . Close ( ) ;
}
}
DecompilerSettings GetSettings ( PEFile module )
@ -217,7 +250,7 @@ Remarks:
@@ -217,7 +250,7 @@ Remarks:
return 0 ;
}
int DecompileAsProject ( string assemblyFileName , string outputDirectory )
ProjectId DecompileAsProject ( string assemblyFileName , string projectFileName )
{
var module = new PEFile ( assemblyFileName ) ;
var resolver = new UniversalAssemblyResolver ( assemblyFileName , false , module . Metadata . DetectTargetFrameworkId ( ) ) ;
@ -226,8 +259,8 @@ Remarks:
@@ -226,8 +259,8 @@ Remarks:
resolver . AddSearchDirectory ( path ) ;
}
var decompiler = new WholeProjectDecompiler ( GetSettings ( module ) , resolver , resolver , TryLoadPDB ( module ) ) ;
decompiler . DecompileProject ( module , outputDirectory ) ;
return 0 ;
using ( var projectFileWriter = new StreamWriter ( File . OpenWrite ( projectFileName ) ) )
return decompiler . DecompileProject ( module , Path . GetDirectoryName ( projectFileName ) , projectFileWriter ) ;
}
int Decompile ( string assemblyFileName , TextWriter output , string typeName = null )