From d43bf783bae10749dd29d631a15b3bec3fd2a1c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Srbeck=C3=BD?= Date: Sun, 28 Oct 2007 13:56:15 +0000 Subject: [PATCH] Load the assembly and create a skeleton for each type (just 'public class' now) --- Decompiler.csproj | 1 + src/CodeDomBuilder.cs | 69 ++++++++++++++++++++++++++++++++++++++++ src/MainForm.Designer.cs | 5 +-- src/Program.cs | 11 ++++++- 4 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 src/CodeDomBuilder.cs diff --git a/Decompiler.csproj b/Decompiler.csproj index 811a5b4a9..a7ad13525 100644 --- a/Decompiler.csproj +++ b/Decompiler.csproj @@ -34,6 +34,7 @@ + diff --git a/src/CodeDomBuilder.cs b/src/CodeDomBuilder.cs new file mode 100644 index 000000000..79c72fb1f --- /dev/null +++ b/src/CodeDomBuilder.cs @@ -0,0 +1,69 @@ +using System; +using System.CodeDom; +using System.CodeDom.Compiler; +using System.Collections.Generic; +using System.IO; +using System.Windows.Forms; +using Microsoft.CSharp; + +using Mono.Cecil; + +namespace Decompiler +{ + public class CodeDomBuilder + { + CodeCompileUnit codeCompileUnit = new CodeCompileUnit(); + Dictionary codeNamespaces = new Dictionary(); + + public string GenerateCode() + { + CSharpCodeProvider provider = new CSharpCodeProvider(); + + TextWriter stringWriter = new StringWriter(); + + CodeGeneratorOptions options = new CodeGeneratorOptions(); + options.BlankLinesBetweenMembers = true; + options.BracingStyle = "C"; + options.ElseOnClosing = true; + options.IndentString = "\t"; + options.VerbatimOrder = true; + + provider.GenerateCodeFromCompileUnit(codeCompileUnit, stringWriter, options); + + // Remove the generated comments at the start + string code = stringWriter.ToString(); + while(code.StartsWith("//") || code.StartsWith("\r\n")) { + code = code.Remove(0, code.IndexOf("\r\n") + "\r\n".Length); + } + + return code; + } + + public void AddAssembly(AssemblyDefinition assemblyDefinition) + { + foreach(TypeDefinition typeDef in assemblyDefinition.MainModule.Types) { + AddType(typeDef); + } + } + + CodeNamespace GetCodeNamespace(string name) + { + if (codeNamespaces.ContainsKey(name)) { + return codeNamespaces[name]; + } else { + // Create the namespace + CodeNamespace codeNamespace = new CodeNamespace(name); + codeCompileUnit.Namespaces.Add(codeNamespace); + codeNamespaces[name] = codeNamespace; + return codeNamespace; + } + } + + public void AddType(TypeDefinition typeDef) + { + CodeTypeDeclaration codeType = new CodeTypeDeclaration(); + codeType.Name = typeDef.Name; + GetCodeNamespace(typeDef.Namespace).Types.Add(codeType); + } + } +} diff --git a/src/MainForm.Designer.cs b/src/MainForm.Designer.cs index 3e1ba8640..af33459d4 100644 --- a/src/MainForm.Designer.cs +++ b/src/MainForm.Designer.cs @@ -43,14 +43,15 @@ namespace Decompiler this.sourceCode.Location = new System.Drawing.Point(12, 12); this.sourceCode.Multiline = true; this.sourceCode.Name = "sourceCode"; - this.sourceCode.Size = new System.Drawing.Size(266, 223); + this.sourceCode.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; + this.sourceCode.Size = new System.Drawing.Size(736, 665); this.sourceCode.TabIndex = 0; // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(290, 247); + this.ClientSize = new System.Drawing.Size(760, 689); this.Controls.Add(this.sourceCode); this.Name = "MainForm"; this.Text = "Decompiler"; diff --git a/src/Program.cs b/src/Program.cs index 7dac20d8b..ab9d4cf14 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -6,6 +6,8 @@ using System; using System.Windows.Forms; +using Mono.Cecil; + namespace Decompiler { /// @@ -19,7 +21,7 @@ namespace Decompiler [STAThread] private static void Main(string[] args) { - string sourceCode = ""; + string sourceCode = Decompile(@"..\..\tests\ClassStructure\bin\Debug\ClassStructure.dll"); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); @@ -28,5 +30,12 @@ namespace Decompiler Application.Run(mainForm); } + static string Decompile(string filename) + { + AssemblyDefinition assembly = AssemblyFactory.GetAssembly(filename); + CodeDomBuilder codeDomBuilder = new CodeDomBuilder(); + codeDomBuilder.AddAssembly(assembly); + return codeDomBuilder.GenerateCode(); + } } }