Browse Source

add simple assembly decompilation support

pull/728/head
Siegfried Pammer 11 years ago
parent
commit
cdef3c2081
  1. 53
      ILSpy/Languages/CSharpLanguage.cs

53
ILSpy/Languages/CSharpLanguage.cs

@ -292,7 +292,7 @@ namespace ICSharpCode.ILSpy
} }
return null; return null;
} }
/*
public override void DecompileAssembly(LoadedAssembly assembly, ITextOutput output, DecompilationOptions options) public override void DecompileAssembly(LoadedAssembly assembly, ITextOutput output, DecompilationOptions options)
{ {
if (options.FullDecompilation && options.SaveAsProjectDirectory != null) { if (options.FullDecompilation && options.SaveAsProjectDirectory != null) {
@ -326,14 +326,19 @@ namespace ICSharpCode.ILSpy
// 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 (options.FullDecompilation ? null : LoadedAssembly.DisableAssemblyLoad()) { using (options.FullDecompilation ? null : LoadedAssembly.DisableAssemblyLoad()) {
AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: assembly.ModuleDefinition); CSharpDecompiler decompiler = new CSharpDecompiler(assembly.ModuleDefinition);
codeDomBuilder.AddAssembly(assembly.ModuleDefinition, onlyAssemblyLevel: !options.FullDecompilation); if (options.FullDecompilation) {
codeDomBuilder.RunTransformations(transformAbortCondition); SyntaxTree st = decompiler.DecompileWholeModuleAsSingleFile();
codeDomBuilder.GenerateCode(output); output.WriteLine(st.ToString());
} }
} // AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: assembly.ModuleDefinition);
// codeDomBuilder.AddAssembly(assembly.ModuleDefinition, onlyAssemblyLevel: !options.FullDecompilation);
// codeDomBuilder.RunTransformations(transformAbortCondition);
// codeDomBuilder.GenerateCode(output);
}
}
} }
*/
#region WriteProjectFile #region WriteProjectFile
void WriteProjectFile(TextWriter writer, IEnumerable<Tuple<string, string>> files, ModuleDefinition module) void WriteProjectFile(TextWriter writer, IEnumerable<Tuple<string, string>> files, ModuleDefinition module)
{ {
@ -460,13 +465,12 @@ namespace ICSharpCode.ILSpy
#endregion #endregion
#region WriteCodeFilesInProject #region WriteCodeFilesInProject
/*
bool IncludeTypeWhenDecompilingProject(TypeDefinition type, DecompilationOptions options) bool IncludeTypeWhenDecompilingProject(TypeDefinition type, DecompilationOptions options)
{ {
if (type.Name == "<Module>" || AstBuilder.MemberIsHidden(type, options.DecompilerSettings)) // if (type.Name == "<Module>" || CSharpDecompiler.MemberIsHidden(type, options.DecompilerSettings))
return false; // return false;
if (type.Namespace == "XamlGeneratedNamespace" && type.Name == "GeneratedInternalTypeHelper") // if (type.Namespace == "XamlGeneratedNamespace" && type.Name == "GeneratedInternalTypeHelper")
return false; // return false;
return true; return true;
} }
@ -475,16 +479,16 @@ namespace ICSharpCode.ILSpy
// 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); // AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: module);
codeDomBuilder.AddAssembly(module, onlyAssemblyLevel: true); // codeDomBuilder.AddAssembly(module, onlyAssemblyLevel: true);
codeDomBuilder.RunTransformations(transformAbortCondition); // 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)); // codeDomBuilder.GenerateCode(new PlainTextOutput(w));
return new Tuple<string, string>[] { Tuple.Create("Compile", assemblyInfo) }; return new Tuple<string, string>[] { Tuple.Create("Compile", assemblyInfo) };
} }
} }
@ -503,23 +507,22 @@ namespace ICSharpCode.ILSpy
return Path.Combine(dir, file); return Path.Combine(dir, file);
} }
}, StringComparer.OrdinalIgnoreCase).ToList(); }, StringComparer.OrdinalIgnoreCase).ToList();
AstMethodBodyBuilder.ClearUnhandledOpcodes(); DecompilerTypeSystem ts = new DecompilerTypeSystem(module);
// AstMethodBodyBuilder.ClearUnhandledOpcodes();
Parallel.ForEach( Parallel.ForEach(
files, files,
new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount },
delegate(IGrouping<string, TypeDefinition> file) { delegate(IGrouping<string, TypeDefinition> file) {
using (StreamWriter w = new StreamWriter(Path.Combine(options.SaveAsProjectDirectory, file.Key))) { using (StreamWriter w = new StreamWriter(Path.Combine(options.SaveAsProjectDirectory, file.Key))) {
AstBuilder codeDomBuilder = CreateAstBuilder(options, currentModule: module); CSharpDecompiler decompiler = new CSharpDecompiler(ts);
foreach (TypeDefinition type in file) { foreach (TypeDefinition type in file) {
codeDomBuilder.AddType(type); w.WriteLine(decompiler.Decompile(type).ToString());
} }
codeDomBuilder.RunTransformations(transformAbortCondition);
codeDomBuilder.GenerateCode(new PlainTextOutput(w));
} }
}); });
AstMethodBodyBuilder.PrintNumberOfUnhandledOpcodes(); // AstMethodBodyBuilder.PrintNumberOfUnhandledOpcodes();
return files.Select(f => Tuple.Create("Compile", f.Key)).Concat(WriteAssemblyInfo(module, options, directories)); return files.Select(f => Tuple.Create("Compile", f.Key)).Concat(WriteAssemblyInfo(module, options, directories));
}*/ }
#endregion #endregion
#region WriteResourceFilesInProject #region WriteResourceFilesInProject

Loading…
Cancel
Save