Browse Source

Load the assembly and create a skeleton for each type (just 'public class' now)

pull/1/head^2
David Srbecký 18 years ago
parent
commit
d43bf783ba
  1. 1
      Decompiler.csproj
  2. 69
      src/CodeDomBuilder.cs
  3. 5
      src/MainForm.Designer.cs
  4. 11
      src/Program.cs

1
Decompiler.csproj

@ -34,6 +34,7 @@ @@ -34,6 +34,7 @@
<ItemGroup>
<Folder Include="src" />
<Compile Include="src\AssemblyInfo.cs" />
<Compile Include="src\CodeDomBuilder.cs" />
<Compile Include="src\MainForm.cs" />
<Compile Include="src\MainForm.Designer.cs" />
<Compile Include="src\Program.cs" />

69
src/CodeDomBuilder.cs

@ -0,0 +1,69 @@ @@ -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<string, CodeNamespace> codeNamespaces = new Dictionary<string, CodeNamespace>();
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);
}
}
}

5
src/MainForm.Designer.cs generated

@ -43,14 +43,15 @@ namespace Decompiler @@ -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";

11
src/Program.cs

@ -6,6 +6,8 @@ @@ -6,6 +6,8 @@
using System;
using System.Windows.Forms;
using Mono.Cecil;
namespace Decompiler
{
/// <summary>
@ -19,7 +21,7 @@ namespace Decompiler @@ -19,7 +21,7 @@ namespace Decompiler
[STAThread]
private static void Main(string[] args)
{
string sourceCode = "<Source code>";
string sourceCode = Decompile(@"..\..\tests\ClassStructure\bin\Debug\ClassStructure.dll");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
@ -28,5 +30,12 @@ namespace Decompiler @@ -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();
}
}
}

Loading…
Cancel
Save