Skip to content

Commit fc0f8ae

Browse files
committed
* support RunBinderBeforeStrip&RunBinderBeforeIl2cpp
1 parent c4b16bb commit fc0f8ae

9 files changed

Lines changed: 13444 additions & 50518 deletions

File tree

BindGenerater/Program.cs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ namespace BindGenerater
1313
{
1414
class Program
1515
{
16-
public enum Platform
17-
{
18-
iOS,
19-
Windows,
20-
}
2116

2217
class BindOptions
2318
{
24-
public Platform Platform;
2519
public string ScriptEngineDir;
2620
public HashSet<string> AdapterSet;
2721
public HashSet<string> InterpSet;
2822
}
23+
public enum BindTarget
24+
{
25+
Adapter,
26+
All,
27+
}
28+
2929

3030
static BindOptions options;
3131

@@ -34,7 +34,7 @@ class BindOptions
3434
"PureScript.dll","Adapter.gen.dll","UnityEngine.UnityAnalyticsModule.dll",
3535
};
3636
public static string ToolsetPath;
37-
37+
public static BindTarget Mode;
3838

3939
static int Main(string[] args)
4040
{
@@ -60,6 +60,10 @@ static void StartBinder(string[] args)
6060
return;
6161
var configFile = args[0];
6262
ToolsetPath = args[1];
63+
if (args.Length >= 3)
64+
Mode = (BindTarget)Enum.Parse(typeof(BindTarget), args[2]);
65+
else
66+
Mode = BindTarget.All;
6367

6468

6569
Console.WriteLine("start binder..");
@@ -73,9 +77,10 @@ static void StartBinder(string[] args)
7377
ReplaceMscorlib("lib", managedDir);
7478

7579
Binder.Init(Path.Combine(adapterDir, "glue"));
80+
CSCGenerater.Init(ToolsetPath, adapterDir, managedDir, options.AdapterSet);
7681
CBinder.Init(Path.Combine(options.ScriptEngineDir, "generated"));
7782
AOTGenerater.Init(options.ScriptEngineDir);
78-
CSCGenerater.Init(ToolsetPath, adapterDir, managedDir, options.AdapterSet);
83+
7984

8085
foreach (var filePath in Directory.GetFiles(managedDir))
8186
{
@@ -87,7 +92,7 @@ static void StartBinder(string[] args)
8792
{
8893
Binder.Bind(filePath);
8994
}
90-
else
95+
else if(Mode == BindTarget.All)
9196
{
9297
if (!options.InterpSet.Contains(file))
9398
{
@@ -108,16 +113,28 @@ static void StartBinder(string[] args)
108113
}
109114
}
110115
}
111-
CBinder.End();
116+
112117
Binder.End();
113118
CSCGenerater.End();
114-
AOTGenerater.End();
119+
120+
if (Mode == BindTarget.All)
121+
{
122+
CBinder.End();
123+
AOTGenerater.End();
124+
}
115125

116126
foreach(var file in options.AdapterSet)
117127
{
118128
var path = Path.Combine(managedDir, file);
119129
if (File.Exists(path))
130+
{
131+
var tempDir = Path.Combine(managedDir, "..", "temp");
132+
if (!Directory.Exists(tempDir))
133+
Directory.CreateDirectory(tempDir);
134+
135+
File.Copy(path, Path.Combine(tempDir, file), true);
120136
File.Delete(path);
137+
}
121138
}
122139
}
123140

DemoProject/Assets/Plugins/PureScript/Editor/PureScriptBuilder.cs

Lines changed: 84 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public static class PureScriptBuilder
1919
static string ScriptEngineDir = "../ScriptEngine";
2020
static bool Inbuild = false;
2121

22-
static MethodHooker hooker;
23-
22+
static MethodHooker stripHooker;
23+
static MethodHooker il2cppHooker;
2424

2525
[UnityEditor.Callbacks.PostProcessScene]
2626
public static void AutoInjectAssemblys()
@@ -31,70 +31,92 @@ public static void AutoInjectAssemblys()
3131
if (Enable && !Inbuild)
3232
{
3333
Inbuild = true;
34-
35-
// wait IL2CPPBuilder,maybe you can use SBP in the future.
34+
ScriptEngineDir = Path.GetFullPath(ScriptEngineDir);
3635
InsertBuildTask();
3736
}
3837
}
3938
[MenuItem("PureScript/TestBuild", false, 1)]
4039
public static void TestBuild()
4140
{
42-
// RunBinder(null, null, null, Application.dataPath.Replace("Assets", Path.Combine("Library", "PlayerDataCache", "Managed")));
41+
//RunBinder(null, null, null, Application.dataPath.Replace("Assets", Path.Combine("Library", "PlayerDataCache", "Managed")));
4342
}
4443

45-
//called by UnityEditor when IL2CPPBuilder.RunIl2CppWithArguments
46-
//public static void RunBinder(object obj, List<string> arguments, Action<System.Diagnostics.ProcessStartInfo> setupStartInfo, string workingDirectory)
47-
public static void RunBinder(string managedAssemblyFolderPath, object platformProvider, object rcr, ManagedStrippingLevel managedStrippingLevel)
44+
/// <summary>
45+
/// bind adapter before strip.
46+
/// called by UnityEditor when AssemblyStripper.StripAssemblies.
47+
/// "replace assembly" after strip will trigger il2cpp.exe`s error.
48+
/// </summary>
49+
public static void RunBinderBeforeStrip(string managedAssemblyFolderPath, object platformProvider, object rcr, ManagedStrippingLevel managedStrippingLevel)
4850
{
49-
var workingDirectory = managedAssemblyFolderPath;
50-
if (hooker != null)
51+
if (stripHooker != null)
5152
{
52-
hooker.Dispose();
53-
hooker = null;
53+
stripHooker.Dispose();
54+
stripHooker = null;
5455
}
5556
Inbuild = false;
5657

57-
ScriptEngineDir = NiceWinPath(ScriptEngineDir);
58+
var managedPath = Path.Combine(ScriptEngineDir, "Managed");
5859

59-
//copy all striped assemblys
60-
var managedPath = Path.Combine(ScriptEngineDir , "Managed");
61-
CreateOrCleanDirectory(managedPath);
62-
63-
foreach (string fi in Directory.GetFiles(workingDirectory))
64-
{
65-
string fname = Path.GetFileName(fi);
66-
string targetfname = Path.Combine(managedPath, fname);
67-
File.Copy(fi, targetfname);
68-
}
60+
//copy all assemblys
61+
CopyManagedFile(managedAssemblyFolderPath, managedPath);
6962

70-
// call binder,bind icall and adapter
71-
var binderPath = Path.Combine(ScriptEngineDir, "Tools", "Binder.exe");
72-
var configPath = Path.GetFullPath(Path.Combine(ScriptEngineDir, "Tools", "config.json"));
73-
var toolsetPath = GetEnginePath(Path.Combine("Tools", "Roslyn"));
74-
CallBinder(binderPath, new List<string>() { configPath, toolsetPath });
63+
// call binder,bind adapter
64+
CallBinder("Adapter");
7565

7666
// replace adapter by generated assembly
7767
var generatedAdapter = Path.Combine(managedPath, "Adapter.gen.dll");
78-
var adapterGenPath = Path.Combine(workingDirectory, "Adapter.gen.dll");
68+
var adapterGenPath = Path.Combine(managedAssemblyFolderPath, "Adapter.gen.dll");
7969
if (File.Exists(generatedAdapter))
8070
{
8171
File.Copy(generatedAdapter, adapterGenPath, true);
8272
File.Delete(generatedAdapter);
8373
}
8474

8575
// call the realy method
86-
/*if (obj != null)
87-
RunIl2CppWithArguments(obj, arguments, setupStartInfo, workingDirectory);*/
88-
8976
StripAssemblies(managedAssemblyFolderPath, platformProvider, rcr, managedStrippingLevel);
9077
}
9178

92-
public static void CallBinder(string binderPath,List<string> args)
79+
/// <summary>
80+
/// resolve all bind task after strip.
81+
/// called by UnityEditor when IL2CPPBuilder.RunIl2CppWithArguments.
82+
/// </summary>
83+
public static void RunBinderBeforeIl2cpp(object obj, List<string> arguments, Action<System.Diagnostics.ProcessStartInfo> setupStartInfo, string workingDirectory)
84+
{
85+
if (il2cppHooker != null)
86+
{
87+
il2cppHooker.Dispose();
88+
il2cppHooker = null;
89+
}
90+
Inbuild = false;
91+
92+
//copy all striped assemblys
93+
var managedPath = Path.Combine(ScriptEngineDir, "Managed");
94+
CopyManagedFile(workingDirectory, managedPath);
95+
96+
// call binder,bind icall and adapter
97+
CallBinder("All");
98+
99+
// call the realy method
100+
if (obj != null)
101+
RunIl2CppWithArguments(obj, arguments, setupStartInfo, workingDirectory);
102+
}
103+
104+
public static void CallBinder(string mode)
93105
{
106+
var binderPath = Path.Combine(ScriptEngineDir, "Tools", "Binder.exe");
107+
var configPath = Path.GetFullPath(Path.Combine(ScriptEngineDir, "Tools", "config.json"));
108+
var toolsetPath = GetEnginePath(Path.Combine("Tools", "Roslyn"));
109+
110+
var args = new List<string>() { configPath, toolsetPath };
111+
if (!string.IsNullOrEmpty(mode))
112+
args.Add(mode);
113+
114+
94115
var monoPath = GetEnginePath(Path.Combine("MonoBleedingEdge", "bin", Application.platform == RuntimePlatform.OSXEditor ? "mono" : "mono.exe"));
95116
if (!File.Exists(monoPath))
96117
{
97118
UnityEngine.Debug.LogError("can not find mono!");
119+
return;
98120
}
99121

100122
if (!File.Exists(binderPath))
@@ -123,7 +145,6 @@ public static void CallBinder(string binderPath,List<string> args)
123145
UnityEngine.Debug.LogWarning(line);
124146
}
125147

126-
127148
binder.WaitForExit();
128149

129150
if (binder.ExitCode != 0)
@@ -134,6 +155,19 @@ public static void CallBinder(string binderPath,List<string> args)
134155
}
135156
}
136157

158+
159+
public static void CopyManagedFile(string workDir,string managedPath)
160+
{
161+
CreateOrCleanDirectory(managedPath);
162+
163+
foreach (string fi in Directory.GetFiles(workDir))
164+
{
165+
string fname = Path.GetFileName(fi);
166+
string targetfname = Path.Combine(managedPath, fname);
167+
File.Copy(fi, targetfname);
168+
}
169+
}
170+
137171
public static string GetEnginePath(string path)
138172
{
139173
string dataPath;
@@ -154,32 +188,35 @@ static string NiceWinPath(string unityPath)
154188
}
155189

156190
//redirect to IL2CPPBuilder.RunIl2CppWithArguments
157-
/*public static void RunIl2CppWithArguments(object obj, List<string> arguments, Action<System.Diagnostics.ProcessStartInfo> setupStartInfo, string workingDirectory)
191+
public static void RunIl2CppWithArguments(object obj, List<string> arguments, Action<System.Diagnostics.ProcessStartInfo> setupStartInfo, string workingDirectory)
158192
{
159193
throw new NotImplementedException();
160-
}*/
194+
}
161195

196+
//redirect to AssemblyStripper.StripAssemblies
162197
public static void StripAssemblies(string managedAssemblyFolderPath, object platformProvider, object rcr, ManagedStrippingLevel managedStrippingLevel)
163198
{
164199
throw new NotImplementedException();
165200
}
166-
167-
201+
168202
private static void InsertBuildTask()
169203
{
170-
if(hooker == null)
204+
if (stripHooker == null)
171205
{
172-
/* var builderType = typeof(Editor).Assembly.GetType("UnityEditorInternal.IL2CPPBuilder");
173-
MethodBase orign = builderType.GetMethod("RunIl2CppWithArguments", BindingFlags.Instance | BindingFlags.NonPublic);
174-
MethodBase custom = typeof(PureScriptBuilder).GetMethod("RunBinder");
175-
MethodBase wrap2Orign = typeof(PureScriptBuilder).GetMethod("RunIl2CppWithArguments");*/
176-
177-
var builderType = typeof(Editor).Assembly.GetType("UnityEditorInternal.AssemblyStripper");
206+
var builderType = typeof(Editor).Assembly.GetType("UnityEditorInternal.AssemblyStripper");
178207
MethodBase orign = builderType.GetMethod("StripAssemblies", BindingFlags.Static | BindingFlags.NonPublic);
179-
MethodBase custom = typeof(PureScriptBuilder).GetMethod("RunBinder");
208+
MethodBase custom = typeof(PureScriptBuilder).GetMethod("RunBinderBeforeStrip");
180209
MethodBase wrap2Orign = typeof(PureScriptBuilder).GetMethod("StripAssemblies");
210+
stripHooker = new MethodHooker(orign, custom, wrap2Orign);
211+
}
181212

182-
hooker = new MethodHooker(orign, custom, wrap2Orign);
213+
if (il2cppHooker == null)
214+
{
215+
var builderType = typeof(Editor).Assembly.GetType("UnityEditorInternal.IL2CPPBuilder");
216+
MethodBase orign = builderType.GetMethod("RunIl2CppWithArguments", BindingFlags.Instance | BindingFlags.NonPublic);
217+
MethodBase custom = typeof(PureScriptBuilder).GetMethod("RunBinderBeforeIl2cpp");
218+
MethodBase wrap2Orign = typeof(PureScriptBuilder).GetMethod("RunIl2CppWithArguments");
219+
il2cppHooker = new MethodHooker(orign, custom, wrap2Orign);
183220
}
184221
}
185222

0 commit comments

Comments
 (0)