-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
95 lines (80 loc) · 2.6 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.IO;
using System.Collections.Generic;
using ICSharpCode.Decompiler;
using ICSharpCode.ILSpy;
namespace ILSpyMono
{
class MainClass
{
public static void Main (string[] args)
{
AssemblyList assemblyList = new AssemblyList ("name");
if (args.Length != 3) {
Exit (0, "Usage: ILSpyMono.exe list-of-dependencies.txt-or-dir path/to/library.dll path/to/output/dir");
}
string DepsFileOrDir = args [0], DllFile = args [1], OutPath = args [2];
if (!File.Exists (DllFile)) {
Error ("Assembly file not found: " + DllFile);
}
if (Directory.Exists (OutPath)) {
Console.Out.WriteLine ("WARNING: Output path already exists and will be overwritten: " + OutPath);
} else {
Directory.CreateDirectory (OutPath);
}
if (DepsFileOrDir != "-") {
foreach (string path in LoadDeps(DepsFileOrDir)) {
assemblyList.OpenAssembly (path, true);
}
}
LoadedAssembly asm = assemblyList.OpenAssembly (DllFile, true);
if (asm.HasLoadError) {
Error ("Failed loading assembly");
}
using (var writer = new System.IO.StreamWriter(OutPath + "/" + asm.ShortName + ".csproj")) {
try {
new CSharpLanguage().DecompileAssembly(asm,
new PlainTextOutput(writer),
new DecompilationOptions { FullDecompilation = true, SaveAsProjectDirectory = OutPath });
} catch (Exception ex) {
Error ("Exception: " + ex.ToString());
}
}
Console.Out.WriteLine ("Done.");
}
public static void Exit(int code, string message) {
Console.Out.WriteLine (message);
Environment.Exit (code);
}
public static void Error(string message) {
Exit (1, "Error: " + message);
}
public static string[] LoadDeps(string path) {
List<String> result = new List<String> ();
if (Directory.Exists (path)) {
return Directory.GetFiles (path, "*.dll", SearchOption.AllDirectories);
} else if (!File.Exists (path)) {
Error ("Dependencies list not found: " + path);
}
try
{ // Open the text file using a stream reader.
using (StreamReader sr = new StreamReader(path))
{ // Read the stream to a string, and write the string to the console.
String line = sr.ReadToEnd().Trim();
if (line.Length > 0 && !line.StartsWith("#")) {
if (!File.Exists(line)) {
Error ("One of dependencies not found: " + line);
}
if (!line.EndsWith(".dll")) {
Error ("List of dependencies should contain only dlls: " + line);
}
result.Add(line);
}
}
} catch (Exception e) {
Error ("The dependencies file could not be read: " + e.Message);
}
return result.ToArray ();
}
}
}