Last active
March 3, 2018 01:59
-
-
Save brandonprry/5726268 to your computer and use it in GitHub Desktop.
Dirty C# decompiler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using ICSharpCode.Decompiler.Ast; | |
using System.IO; | |
using Mono.Cecil; | |
using ICSharpCode.Decompiler; | |
using System.Collections.Generic; | |
namespace Decompiler { | |
class MainClass { | |
public static void Main (string[] args) { | |
if (args.Length != 2) { | |
Console.WriteLine("Dirty C# decompiler requires two arguments."); | |
Console.WriteLine("decompiler.exe <path to assembly> <path to write-out directory>"); | |
} | |
IEnumerable<AssemblyClass> klasses = GenerateAssemblyMethodSource (args[0]); | |
foreach (AssemblyClass klass in klasses) { | |
if (!Directory.Exists(args[1] + Path.DirectorySeparatorChar + klass.namespase)) | |
Directory.CreateDirectory(args[1] + Path.DirectorySeparatorChar + klass.namespase); | |
File.WriteAllText(args[1] + Path.DirectorySeparatorChar + klass.namespase + Path.DirectorySeparatorChar + klass.name + ".cs", klass.source); | |
} | |
} | |
private static IEnumerable<AssemblyClass> GenerateAssemblyMethodSource (string assemblyPath) { | |
AssemblyDefinition assemblyDefinition = AssemblyDefinition.ReadAssembly (assemblyPath, new ReaderParameters (ReadingMode.Deferred) { ReadSymbols = true }); | |
AstBuilder astBuilder = null; | |
foreach (var defmod in assemblyDefinition.Modules) { | |
foreach (var typeInAssembly in defmod.Types) { | |
AssemblyClass klass = new AssemblyClass (); | |
klass.name = typeInAssembly.Name; | |
klass.namespase = typeInAssembly.Namespace; | |
astBuilder = new AstBuilder (new DecompilerContext (assemblyDefinition.MainModule) { CurrentType = typeInAssembly }); | |
astBuilder.AddType (typeInAssembly); | |
StringWriter output = new StringWriter (); | |
astBuilder.GenerateCode (new PlainTextOutput (output)); | |
klass.source = output.ToString (); | |
yield return klass; | |
} | |
} | |
} | |
} | |
public class AssemblyClass { | |
public string namespase; | |
public string name; | |
public string source; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment