Browse Source

'decompile as project' improvements

pull/728/head
Daniel Grunwald 10 years ago
parent
commit
a00dfeb061
  1. 38
      ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs
  2. 26
      ILSpy/Languages/CSharpLanguage.cs

38
ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs

@ -116,17 +116,33 @@ namespace ICSharpCode.Decompiler.CSharp
rootNode.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true }); rootNode.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true });
} }
public SyntaxTree DecompileWholeModuleAsSingleFile() /// <summary>
/// Decompile assembly and module attributes.
/// </summary>
public SyntaxTree DecompileModuleAndAssemblyAttributes()
{ {
var decompilationContext = new SimpleTypeResolveContext(typeSystem.MainAssembly); var decompilationContext = new SimpleTypeResolveContext(typeSystem.MainAssembly);
SyntaxTree syntaxTree = new SyntaxTree(); SyntaxTree syntaxTree = new SyntaxTree();
foreach (var a in typeSystem.Compilation.MainAssembly.AssemblyAttributes) DoDecompileModuleAndAssemblyAttributes(decompilationContext, syntaxTree);
RunTransforms(syntaxTree, decompilationContext);
return syntaxTree;
}
void DoDecompileModuleAndAssemblyAttributes(ITypeResolveContext decompilationContext, SyntaxTree syntaxTree)
{ {
foreach (var a in typeSystem.Compilation.MainAssembly.AssemblyAttributes) {
var astBuilder = CreateAstBuilder(decompilationContext); var astBuilder = CreateAstBuilder(decompilationContext);
var attrSection = new AttributeSection(astBuilder.ConvertAttribute(a)); var attrSection = new AttributeSection(astBuilder.ConvertAttribute(a));
attrSection.AttributeTarget = "assembly"; attrSection.AttributeTarget = "assembly";
syntaxTree.AddChild(attrSection, SyntaxTree.MemberRole); syntaxTree.AddChild(attrSection, SyntaxTree.MemberRole);
} }
}
public SyntaxTree DecompileWholeModuleAsSingleFile()
{
var decompilationContext = new SimpleTypeResolveContext(typeSystem.MainAssembly);
SyntaxTree syntaxTree = new SyntaxTree();
DoDecompileModuleAndAssemblyAttributes(decompilationContext, syntaxTree);
string currentNamespace = null; string currentNamespace = null;
AstNode groupNode = null; AstNode groupNode = null;
foreach (var cecilType in typeSystem.ModuleDefinition.Types) { foreach (var cecilType in typeSystem.ModuleDefinition.Types) {
@ -150,7 +166,7 @@ namespace ICSharpCode.Decompiler.CSharp
return syntaxTree; return syntaxTree;
} }
public EntityDeclaration Decompile(TypeDefinition typeDefinition) public SyntaxTree Decompile(TypeDefinition typeDefinition)
{ {
if (typeDefinition == null) if (typeDefinition == null)
throw new ArgumentNullException("typeDefinition"); throw new ArgumentNullException("typeDefinition");
@ -158,9 +174,10 @@ namespace ICSharpCode.Decompiler.CSharp
if (typeDef == null) if (typeDef == null)
throw new InvalidOperationException("Could not find type definition in NR type system"); throw new InvalidOperationException("Could not find type definition in NR type system");
var decompilationContext = new SimpleTypeResolveContext(typeDef); var decompilationContext = new SimpleTypeResolveContext(typeDef);
var decl = DoDecompile(typeDef, decompilationContext); var syntaxTree = new SyntaxTree();
RunTransforms(decl, decompilationContext); syntaxTree.Members.Add(DoDecompile(typeDef, decompilationContext));
return decl; RunTransforms(syntaxTree, decompilationContext);
return syntaxTree;
} }
EntityDeclaration DoDecompile(ITypeDefinition typeDef, ITypeResolveContext decompilationContext) EntityDeclaration DoDecompile(ITypeDefinition typeDef, ITypeResolveContext decompilationContext)
@ -221,7 +238,7 @@ namespace ICSharpCode.Decompiler.CSharp
return typeDecl; return typeDecl;
} }
public EntityDeclaration Decompile(MethodDefinition methodDefinition) public SyntaxTree Decompile(MethodDefinition methodDefinition)
{ {
if (methodDefinition == null) if (methodDefinition == null)
throw new ArgumentNullException("methodDefinition"); throw new ArgumentNullException("methodDefinition");
@ -229,9 +246,10 @@ namespace ICSharpCode.Decompiler.CSharp
if (method == null) if (method == null)
throw new InvalidOperationException("Could not find method in NR type system"); throw new InvalidOperationException("Could not find method in NR type system");
var decompilationContext = new SimpleTypeResolveContext(method); var decompilationContext = new SimpleTypeResolveContext(method);
var decl = DoDecompile(methodDefinition, method, decompilationContext); var syntaxTree = new SyntaxTree();
RunTransforms(decl, decompilationContext); syntaxTree.Members.Add(DoDecompile(methodDefinition, method, decompilationContext));
return decl; RunTransforms(syntaxTree, decompilationContext);
return syntaxTree;
} }
EntityDeclaration DoDecompile(MethodDefinition methodDefinition, IMethod method, ITypeResolveContext decompilationContext) EntityDeclaration DoDecompile(MethodDefinition methodDefinition, IMethod method, ITypeResolveContext decompilationContext)

26
ILSpy/Languages/CSharpLanguage.cs

@ -330,11 +330,10 @@ namespace ICSharpCode.ILSpy
if (options.FullDecompilation) { if (options.FullDecompilation) {
SyntaxTree st = decompiler.DecompileWholeModuleAsSingleFile(); SyntaxTree st = decompiler.DecompileWholeModuleAsSingleFile();
output.WriteLine(st.ToString()); output.WriteLine(st.ToString());
} else {
SyntaxTree st = decompiler.DecompileModuleAndAssemblyAttributes();
output.WriteLine(st.ToString());
} }
// AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: assembly.ModuleDefinition);
// codeDomBuilder.AddAssembly(assembly.ModuleDefinition, onlyAssemblyLevel: !options.FullDecompilation);
// codeDomBuilder.RunTransformations(transformAbortCondition);
// codeDomBuilder.GenerateCode(output);
} }
} }
} }
@ -474,21 +473,21 @@ namespace ICSharpCode.ILSpy
return true; return true;
} }
IEnumerable<Tuple<string, string>> WriteAssemblyInfo(ModuleDefinition module, DecompilationOptions options, HashSet<string> directories) IEnumerable<Tuple<string, string>> WriteAssemblyInfo(DecompilerTypeSystem ts, DecompilationOptions options, HashSet<string> directories)
{ {
// don't automatically load additional assemblies when an assembly node is selected in the tree view // don't automatically load additional assemblies when an assembly node is selected in the tree view
using (LoadedAssembly.DisableAssemblyLoad()) using (LoadedAssembly.DisableAssemblyLoad())
{ {
// AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: module); CSharpDecompiler decompiler = new CSharpDecompiler(ts);
// codeDomBuilder.AddAssembly(module, onlyAssemblyLevel: true); SyntaxTree syntaxTree = decompiler.DecompileModuleAndAssemblyAttributes();
// codeDomBuilder.RunTransformations(transformAbortCondition);
string prop = "Properties"; string prop = "Properties";
if (directories.Add("Properties")) if (directories.Add("Properties"))
Directory.CreateDirectory(Path.Combine(options.SaveAsProjectDirectory, prop)); Directory.CreateDirectory(Path.Combine(options.SaveAsProjectDirectory, prop));
string assemblyInfo = Path.Combine(prop, "AssemblyInfo" + this.FileExtension); string assemblyInfo = Path.Combine(prop, "AssemblyInfo" + this.FileExtension);
// using (StreamWriter w = new StreamWriter(Path.Combine(options.SaveAsProjectDirectory, assemblyInfo))) using (StreamWriter w = new StreamWriter(Path.Combine(options.SaveAsProjectDirectory, assemblyInfo))) {
// codeDomBuilder.GenerateCode(new PlainTextOutput(w)); syntaxTree.AcceptVisitor(new CSharpOutputVisitor(w, options.DecompilerSettings.CSharpFormattingOptions));
}
return new Tuple<string, string>[] { Tuple.Create("Compile", assemblyInfo) }; return new Tuple<string, string>[] { Tuple.Create("Compile", assemblyInfo) };
} }
} }
@ -508,7 +507,6 @@ namespace ICSharpCode.ILSpy
} }
}, StringComparer.OrdinalIgnoreCase).ToList(); }, StringComparer.OrdinalIgnoreCase).ToList();
DecompilerTypeSystem ts = new DecompilerTypeSystem(module); DecompilerTypeSystem ts = new DecompilerTypeSystem(module);
// AstMethodBodyBuilder.ClearUnhandledOpcodes();
Parallel.ForEach( Parallel.ForEach(
files, files,
new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount },
@ -516,12 +514,12 @@ namespace ICSharpCode.ILSpy
using (StreamWriter w = new StreamWriter(Path.Combine(options.SaveAsProjectDirectory, file.Key))) { using (StreamWriter w = new StreamWriter(Path.Combine(options.SaveAsProjectDirectory, file.Key))) {
CSharpDecompiler decompiler = new CSharpDecompiler(ts); CSharpDecompiler decompiler = new CSharpDecompiler(ts);
foreach (TypeDefinition type in file) { foreach (TypeDefinition type in file) {
w.WriteLine(decompiler.Decompile(type).ToString()); var syntaxTree = decompiler.Decompile(type);
syntaxTree.AcceptVisitor(new CSharpOutputVisitor(w, options.DecompilerSettings.CSharpFormattingOptions));
} }
} }
}); });
// AstMethodBodyBuilder.PrintNumberOfUnhandledOpcodes(); return files.Select(f => Tuple.Create("Compile", f.Key)).Concat(WriteAssemblyInfo(ts, options, directories));
return files.Select(f => Tuple.Create("Compile", f.Key)).Concat(WriteAssemblyInfo(module, options, directories));
} }
#endregion #endregion

Loading…
Cancel
Save