forked from MattRix/UnityDecompiled
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIL2CPPBuilder.cs
More file actions
238 lines (238 loc) · 8.71 KB
/
Copy pathIL2CPPBuilder.cs
File metadata and controls
238 lines (238 loc) · 8.71 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEditor.Scripting.Compilers;
namespace UnityEditorInternal
{
internal class IL2CPPBuilder
{
private readonly string m_TempFolder;
private readonly string m_StagingAreaData;
private readonly IIl2CppPlatformProvider m_PlatformProvider;
private readonly Action<string> m_ModifyOutputBeforeCompile;
private readonly RuntimeClassRegistry m_RuntimeClassRegistry;
private string[] Il2CppBlacklistPaths
{
get
{
return new string[]
{
Path.Combine("..", "platform_native_link.xml")
};
}
}
public IL2CPPBuilder(string tempFolder, string stagingAreaData, IIl2CppPlatformProvider platformProvider, Action<string> modifyOutputBeforeCompile, RuntimeClassRegistry runtimeClassRegistry)
{
this.m_TempFolder = tempFolder;
this.m_StagingAreaData = stagingAreaData;
this.m_PlatformProvider = platformProvider;
this.m_ModifyOutputBeforeCompile = modifyOutputBeforeCompile;
this.m_RuntimeClassRegistry = runtimeClassRegistry;
}
public void Run()
{
string cppOutputDirectoryInStagingArea = this.GetCppOutputDirectoryInStagingArea();
string fullPath = Path.GetFullPath(Path.Combine(this.m_StagingAreaData, "Managed"));
string[] files = Directory.GetFiles(fullPath);
for (int i = 0; i < files.Length; i++)
{
string fileName = files[i];
FileInfo fileInfo = new FileInfo(fileName);
fileInfo.IsReadOnly = false;
}
this.PatchAssemblies();
this.StripAssemblies(this.GetUserAssemblies(fullPath), fullPath);
this.ConvertPlayerDlltoCpp(this.GetUserAssembliesOrUnityEngine(fullPath), cppOutputDirectoryInStagingArea, fullPath);
if (this.m_ModifyOutputBeforeCompile != null)
{
this.m_ModifyOutputBeforeCompile(cppOutputDirectoryInStagingArea);
}
INativeCompiler nativeCompiler = this.m_PlatformProvider.CreateNativeCompiler();
if (nativeCompiler != null)
{
string text = Path.Combine(this.m_StagingAreaData, "Native");
Directory.CreateDirectory(text);
text = Path.Combine(text, this.m_PlatformProvider.nativeLibraryFileName);
List<string> list = new List<string>(this.m_PlatformProvider.includePaths);
list.Add(cppOutputDirectoryInStagingArea);
this.m_PlatformProvider.CreateNativeCompiler().CompileDynamicLibrary(text, NativeCompiler.AllSourceFilesIn(cppOutputDirectoryInStagingArea), list, this.m_PlatformProvider.libraryPaths, new string[0]);
}
}
private string[] GetUserAssembliesOrUnityEngine(string managedDir)
{
string[] array = this.GetUserAssemblies(managedDir);
if (array.Length == 0)
{
array = Directory.GetFiles(managedDir, "UnityEngine.dll", SearchOption.TopDirectoryOnly);
}
return array;
}
private bool HasStrippingInformation()
{
return this.m_RuntimeClassRegistry != null && this.m_RuntimeClassRegistry.UsedTypePerUserAssembly != null;
}
private bool ShouldAddUGUIAsUserAssembly()
{
return !this.HasStrippingInformation() || this.m_RuntimeClassRegistry.IsUGUIUsed();
}
private string[] GetUserAssemblies(string managedDir)
{
return (
from s in this.m_RuntimeClassRegistry.GetUserAssemblies()
select Path.Combine(managedDir, s)).ToArray<string>();
}
private IEnumerable<string> GetAssembliesInDirectory(string assemblyName, string managedDir)
{
return Directory.GetFiles(managedDir, assemblyName, SearchOption.TopDirectoryOnly);
}
public string GetCppOutputDirectoryInStagingArea()
{
return Path.Combine(this.m_TempFolder, "il2cppOutput");
}
private void PatchAssemblies()
{
string fullPath = Path.GetFullPath(this.m_StagingAreaData + "/Managed");
string text = fullPath + "/mscorlib.dll";
string text2 = fullPath + "/mscorlib.unpatched.dll";
File.Move(text, text2);
File.Copy(this.m_PlatformProvider.il2CppFolder + "/replacements.dll", fullPath + "/replacements.dll");
Runner.RunManagedProgram(this.m_PlatformProvider.il2CppFolder + "/AssemblyPatcher/AssemblyPatcher.exe", string.Format("-a \"{0}\" -o \"{1}\" -c \"{2}\" --log-config \"{3}\" -s \"{4}\"", new object[]
{
text2,
text,
this.m_PlatformProvider.il2CppFolder + "/assemblypatcher_config.txt",
this.m_PlatformProvider.il2CppFolder + "/AssemblyPatcher.Log.Config.xml",
Path.GetFullPath(this.m_StagingAreaData + "/Managed")
}));
File.Delete(text2);
}
private void ConvertPlayerDlltoCpp(ICollection<string> userAssemblies, string outputDirectory, string workingDirectory)
{
if (userAssemblies.Count == 0)
{
Directory.CreateDirectory(outputDirectory);
return;
}
string il2CppExe = this.GetIl2CppExe();
List<string> list = new List<string>();
list.Add("--copy-level=None");
list.Add("--enable-generic-sharing");
list.Add("--enable-unity-event-support");
if (this.m_PlatformProvider.usePrecompiledHeader)
{
list.Add("--use-precompiled-header");
}
if (this.m_PlatformProvider.emitNullChecks)
{
list.Add("--emit-null-checks");
}
if (this.m_PlatformProvider.enableStackTraces)
{
list.Add("--enable-stacktrace");
}
if (this.m_PlatformProvider.enableArrayBoundsCheck)
{
list.Add("--enable-array-bounds-check");
}
if (this.m_PlatformProvider.compactMode)
{
list.Add("--output-format=Compact");
}
if (this.m_PlatformProvider.loadSymbols)
{
list.Add("--enable-symbol-loading");
}
string empty = string.Empty;
if (PlayerSettings.GetPropertyOptionalString("additionalIl2CppArgs", ref empty))
{
list.Add(empty);
}
List<string> source = new List<string>(userAssemblies)
{
outputDirectory
};
list.AddRange(
from arg in source
select "\"" + Path.GetFullPath(arg) + "\"");
string text = list.Aggregate(string.Empty, (string current, string arg) => current + arg + " ");
Console.WriteLine("Invoking il2cpp with arguments: " + text);
Runner.RunManagedProgram(il2CppExe, text, workingDirectory, new Il2CppOutputParser());
}
private void StripAssemblies(string[] assemblies, string managedAssemblyFolderPath)
{
List<string> list = new List<string>();
list.AddRange(assemblies);
string[] assembliesToStrip = list.ToArray();
string[] searchDirs = new string[]
{
managedAssemblyFolderPath
};
this.RunAssemblyStripper(assemblies, managedAssemblyFolderPath, assembliesToStrip, searchDirs, AssemblyStripper.MonoLinker2Path);
}
private void RunAssemblyStripper(IEnumerable assemblies, string managedAssemblyFolderPath, string[] assembliesToStrip, string[] searchDirs, string monoLinkerPath)
{
IEnumerable<string> enumerable = this.Il2CppBlacklistPaths;
if (this.m_RuntimeClassRegistry != null)
{
enumerable = enumerable.Concat(new string[]
{
this.WriteMethodsToPreserveBlackList()
});
}
string text;
string text2;
if (AssemblyStripper.Strip(assembliesToStrip, searchDirs, managedAssemblyFolderPath, managedAssemblyFolderPath, out text, out text2, monoLinkerPath, Path.Combine(this.m_PlatformProvider.il2CppFolder, "LinkerDescriptors"), enumerable))
{
return;
}
throw new Exception(string.Concat(new object[]
{
"Error in stripping assemblies: ",
assemblies,
", ",
text2
}));
}
private string WriteMethodsToPreserveBlackList()
{
string text = Directory.GetCurrentDirectory() + "/" + this.m_StagingAreaData + "/methods_pointedto_by_uievents.xml";
File.WriteAllText(text, this.GetMethodPreserveBlacklistContents());
return text;
}
private string GetMethodPreserveBlacklistContents()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("<linker>");
IEnumerable<IGrouping<string, RuntimeClassRegistry.MethodDescription>> enumerable =
from m in this.m_RuntimeClassRegistry.GetMethodsToPreserve()
group m by m.assembly;
foreach (IGrouping<string, RuntimeClassRegistry.MethodDescription> current in enumerable)
{
stringBuilder.AppendLine(string.Format("\t<assembly fullname=\"{0}\">", current.Key));
IEnumerable<IGrouping<string, RuntimeClassRegistry.MethodDescription>> enumerable2 =
from m in current
group m by m.fullTypeName;
foreach (IGrouping<string, RuntimeClassRegistry.MethodDescription> current2 in enumerable2)
{
stringBuilder.AppendLine(string.Format("\t\t<type fullname=\"{0}\">", current2.Key));
foreach (RuntimeClassRegistry.MethodDescription current3 in current2)
{
stringBuilder.AppendLine(string.Format("\t\t\t<method name=\"{0}\"/>", current3.methodName));
}
stringBuilder.AppendLine("\t\t</type>");
}
stringBuilder.AppendLine("\t</assembly>");
}
stringBuilder.AppendLine("</linker>");
return stringBuilder.ToString();
}
private string GetIl2CppExe()
{
return this.m_PlatformProvider.il2CppFolder + "/il2cpp.exe";
}
}
}