forked from ncruces/Unity-DLL-Patcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
177 lines (154 loc) · 6.5 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using Microsoft.Build.Utilities;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace PatchDlls
{
class Program
{
static void Main(string[] args)
{
var path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
var res = Path.ChangeExtension(path, "res");
var il = Path.ChangeExtension(path, "il");
var assembly = args[0];
Ildasm(assembly, il);
var source = File.ReadAllText(il);
var debug = GetDebuggableAttribute(source);
source = PatchIEnumConstraint(source);
source = PatchDelegateConstraint(source);
source = PatchArrayConstraint(source);
source = PatchInterlockedExchange(source);
source = PatchInterlockedCompareExchange(source);
if (args.Length > 1)
{
var builder = new StringBuilder();
foreach (var file in Directory.EnumerateFiles(args[1], "*.il", SearchOption.AllDirectories))
{
var patch = File.ReadAllLines(file);
var header = patch.First();
var footer = patch.Last();
var start = source.IndexOf(header);
var end = source.LastIndexOf(footer) + footer.Length;
builder.Clear();
builder.Append(source, 0, start);
foreach (var line in patch) builder.AppendLine(line);
builder.Append(source, end, source.Length - end);
source = builder.ToString();
}
}
File.WriteAllText(il, source);
Ilasm(il, res, assembly, debug);
File.Delete(il);
File.Delete(res);
}
// Assembler
static void Ilasm(string il, string res, string assembly, DebuggableAttribute debug)
{
var ilasm = ToolLocationHelper.GetPathToDotNetFrameworkFile("ilasm.exe", TargetDotNetFrameworkVersion.Version20);
// DLL or EXE
var args = Path.GetExtension(assembly).Replace('.', '/');
// Debugging
if (File.Exists(Path.ChangeExtension(assembly, "pdb")))
{
if (debug.IsJITOptimizerDisabled)
{
if (debug.DebuggingFlags.HasFlag(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints))
{
args += " /debug=impl";
}
else
{
args += " /debug";
}
}
else
{
if (debug.DebuggingFlags.HasFlag(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints))
{
args += " /debug=opt";
}
else
{
args += " /pdb";
}
}
}
// Optimization
if (!debug.DebuggingFlags.HasFlag(DebuggableAttribute.DebuggingModes.DisableOptimizations))
{
args += " /optimize /fold";
}
Run(ilasm, "\"{0}\" /noautoinherit /resource=\"{1}\" /out=\"{2}\" {3}", il, res, assembly, args);
}
// Disassembler
static void Ildasm(string assembly, string il)
{
var ildasm = ToolLocationHelper.GetPathToDotNetFrameworkSdkFile("ildasm.exe", TargetDotNetFrameworkVersion.VersionLatest);
Run(ildasm, "\"{0}\" /linenum /typelist /nobar /out=\"{1}\"", assembly, il);
}
static void Run(string fileName, string format, params object[] args)
{
var proc = Process.Start(new ProcessStartInfo
{
FileName = fileName,
Arguments = string.Format(format, args),
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
});
proc.WaitForExit();
if (proc.ExitCode != 0) Environment.Exit(proc.ExitCode);
}
static DebuggableAttribute GetDebuggableAttribute(string source)
{
var flags = DebuggableAttribute.DebuggingModes.None;
var match = Regex.Match(source,
@".custom instance void \[mscorlib\]System.Diagnostics.DebuggableAttribute::.ctor\(valuetype \[mscorlib\]System.Diagnostics.DebuggableAttribute/DebuggingModes\) = " +
@"\( (..) (..) (..) (..) (..) (..) (..) (..) \)");
if (match.Success)
{
var number = string.Join(string.Empty, match.Groups.Cast<Group>().Skip(1).Reverse());
flags = (DebuggableAttribute.DebuggingModes)Convert.ToInt64(number, 16);
}
return new DebuggableAttribute(flags);
}
static string PatchIEnumConstraint(string source)
{
return Regex.Replace(source,
@"valuetype \.ctor \(\[mscorlib\]System\.ValueType, (\[[^]]+\])?System\.IEnumConstraint\)",
"valuetype .ctor ([mscorlib]System.Enum)",
RegexOptions.Singleline);
}
static string PatchDelegateConstraint(string source)
{
return Regex.Replace(source,
@"\((\[[^]]+\])?System\.DelegateConstraint\)",
"([mscorlib]System.Delegate)",
RegexOptions.Singleline);
}
static string PatchArrayConstraint(string source)
{
return Regex.Replace(source,
@"\((\[[^]]+\])?System\.ArrayConstraint\)",
"([mscorlib]System.Array)",
RegexOptions.Singleline);
}
static string PatchInterlockedExchange(string source)
{
return Regex.Replace(source,
@"!!0 \[mscorlib\]System\.Threading\.Interlocked::Exchange<[^()]+>\([^()]+\)",
"object [mscorlib]System.Threading.Interlocked::Exchange(object&, object)",
RegexOptions.Singleline);
}
static string PatchInterlockedCompareExchange(string source)
{
return Regex.Replace(source,
@"!!0 \[mscorlib\]System\.Threading\.Interlocked::CompareExchange<[^()]+>\([^()]+\)",
"object [mscorlib]System.Threading.Interlocked::CompareExchange(object&, object, object)",
RegexOptions.Singleline);
}
}
}