@@ -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