Skip to content

Commit 9bb6bd0

Browse files
author
kkin
committed
* unity2020 debug mode
1 parent aa48850 commit 9bb6bd0

File tree

3 files changed

+71
-22
lines changed

3 files changed

+71
-22
lines changed

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

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public static class PureScriptBuilder
1717
{
1818
public static bool Enable;
1919
static string ScriptEngineDir;
20+
static string il2cppDir;
2021
static bool Inbuild = false;
2122

2223
static PureScriptBuilder()
@@ -72,17 +73,18 @@ public static void BuildDll()
7273
public static void RunBinderBeforeStrip(System.String managedAssemblyFolderPath)
7374
{
7475
Inbuild = false;
76+
il2cppDir = managedAssemblyFolderPath;
7577
var managedPath = Path.Combine(ScriptEngineDir, "Managed");
7678

7779
//copy all assemblys
78-
CopyManagedFile(managedAssemblyFolderPath, managedPath);
80+
CopyManagedFile(il2cppDir, managedPath);
7981

8082
// call binder,bind adapter
8183
CallBinder("Adapter");
8284

8385
// replace adapter by generated assembly
8486
var generatedAdapter = Path.Combine(managedPath, "Adapter.gen.dll");
85-
var adapterGenPath = Path.Combine(managedAssemblyFolderPath, "Adapter.gen.dll");
87+
var adapterGenPath = Path.Combine(il2cppDir, "Adapter.gen.dll");
8688
if (File.Exists(generatedAdapter))
8789
{
8890
File.Copy(generatedAdapter, adapterGenPath, true);
@@ -97,10 +99,11 @@ public static void RunBinderBeforeStrip(System.String managedAssemblyFolderPath)
9799
public static void RunBinderBeforeIl2cpp(string workingDirectory)
98100
{
99101
Inbuild = false;
100-
102+
if (workingDirectory != null)
103+
il2cppDir = workingDirectory;
101104
//copy all striped assemblys
102105
var managedPath = Path.Combine(ScriptEngineDir, "Managed");
103-
CopyManagedFile(workingDirectory, managedPath);
106+
CopyManagedFile(il2cppDir, managedPath);
104107

105108
// call binder,bind icall and adapter
106109
CallBinder("All");
@@ -214,8 +217,33 @@ static void CreateOrCleanDirectory(string dir)
214217
static MethodHooker stripHooker;
215218
static MethodHooker il2cppHooker;
216219

220+
#if UNITY_2020_1_OR_NEWER
221+
public static void RunIl2CppWithArguments(object obj, List<string> arguments, Action<System.Diagnostics.ProcessStartInfo> setupStartInfo)
222+
{
223+
224+
if (il2cppHooker != null)
225+
{
226+
il2cppHooker.Dispose();
227+
il2cppHooker = null;
228+
}
229+
230+
RunBinderBeforeIl2cpp(null);
231+
232+
// call the realy method
233+
if (obj != null)
234+
RunIl2CppWithArgumentsWrap(obj, arguments, setupStartInfo);
235+
}
236+
237+
//redirect to IL2CPPBuilder.RunIl2CppWithArguments
238+
public static void RunIl2CppWithArgumentsWrap(object obj, List<string> arguments, Action<System.Diagnostics.ProcessStartInfo> setupStartInfo)
239+
{
240+
throw new NotImplementedException();
241+
}
242+
243+
#else
217244
public static void RunIl2CppWithArguments(object obj, List<string> arguments, Action<System.Diagnostics.ProcessStartInfo> setupStartInfo, string workingDirectory)
218245
{
246+
219247
if (il2cppHooker != null)
220248
{
221249
il2cppHooker.Dispose();
@@ -234,7 +262,7 @@ public static void RunIl2CppWithArgumentsWrap(object obj, List<string> arguments
234262
{
235263
throw new NotImplementedException();
236264
}
237-
265+
#endif
238266

239267
#if UNITY_2019_1_OR_NEWER
240268
public static void StripAssemblies(string managedAssemblyFolderPath, object unityLinkerPlatformProvider, object il2cppPlatformProvider, object rcr, ManagedStrippingLevel managedStrippingLevel)

DemoProject/Assets/Scripts/TestEntry/MonoEntry.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,17 @@ public class MonoEntry
1313
{
1414
public static void Main()
1515
{
16-
TestCube();
16+
try
17+
{
18+
TestCube();
1719

18-
TestLoadAssembly();
20+
TestLoadAssembly();
21+
}catch(Exception e)
22+
{
23+
Debug.Log(e.ToString());
24+
PureScript.Mono.ScriptEngine.OnException(e.ToString());
25+
}
26+
1927
}
2028

2129

README.md

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@
3030
## 使用
3131
1. Clone本工程,拷贝DemoProject/Assets/Plugins/PureScript目录。
3232
2. 修改 PureScriptBuilder.cs及ScriptEngine/Tools/config.json中的路径配置。
33-
3. config.json中配置运行在interpreter模式的dll(否则以aot运行),以及运行在Il2cpp运行时内的dll(一般用作Adapter)。
33+
3. config.json中配置运行在interpreter模式的dll(否则以aot运行),以及运行在Il2cpp运行时内的dll(一般用作Adapter)。
34+
config.json(默认配置为Demo工程配置):
35+
36+
ScriptEngineDir: 指向PureScript\ScriptEngine目录
37+
AdapterSet: 配置即要在Il2cpp又要在Mono中使用的dll,一般为一些通用插件
38+
InterpSet: 配置需要热更的dll,否则运行在aot中
39+
40+
3441
4. ScriptEngine启动接口请参考 DemoProject\Assets\Scripts\Lancher.cs, (注意修改'reloadDir'变量)。
3542

3643
### iOS平台
@@ -45,8 +52,9 @@
4552
Windows平台仅用来调试,目前未添加自动集成,在构建项目后,需编译 ScriptEngine/ScriptEngine.vcxproj,替换原来Plugins目录下的的ScriptEngine.dll。
4653
ScriptEngine.vcxproj 属性/VC++目录/包含目录中使用了宏:$(UnityEditorPath) 指向Unity/Editor目录。
4754

48-
调试步骤(直接运行时可以忽略):
49-
1. 设置传入ScriptEngine.Setup接口的reloadDir路径为 */ScriptEngine/Managed
55+
调试步骤:
56+
57+
1. 修改Lancher.cs中的reloadDir,替换其中的{path_to_ScriptEngine}指向PureScript\ScriptEngine\Managed目录
5058
2. Unity导出VS工程。
5159
3. 需要删除Unity导出目录下的Managed目录例如($(ExportPath)/DemoProject/Managed),否则Mono会默认从此处加载dll,Il2cpp并不会使用此目录,但是每次构建都会导出。
5260
4. 在导出的解决方案中添加ScriptEngine.vcxproj,并在主项目中添加ScriptEngine项目的依赖(方便调试)。
@@ -55,18 +63,23 @@
5563

5664

5765
## 例子
58-
以下两段代码是等效的,详细参考DemoProject/*/MonoEntry.cs。
59-
60-
ScriptEngine.Setup(reloadDir, "TestEntry.dll");
61-
62-
// equal to:
63-
64-
Assembly assembly = Assembly.Load("TestEntry.dll");
65-
Type type = assembly.GetType("MonoEntry");
66-
MethodInfo mi = type.GetMethod("Main");
67-
var res = mi.Invoke(null, null)
68-
69-
`注意` 需要热更新的程序集如果是Unity自动生成的工程,会自动引用一堆的无用dll,比如UntiyEditor*.dll,和一堆根本不会用到的System*.dll ,因为安装包内并没有带上这些,加载会失败。这时需要手动删掉这些用不到的引用。或者再建一个工程,手动管理引用,后面也可以考虑做自动化工具,strip掉没用的引用。
66+
以下两段代码是等效的,详细参考DemoProject/*/MonoEntry.cs。
67+
```c#
68+
ScriptEngine.Setup(reloadDir, "TestEntry.dll");
69+
70+
// equal to:
71+
72+
Assembly assembly = Assembly.Load("TestEntry.dll");
73+
Type type = assembly.GetType("MonoEntry");
74+
MethodInfo mi = type.GetMethod("Main");
75+
var res = mi.Invoke(null, null)
76+
```
77+
## `注意`
78+
1. 需要热更新的程序集可以使用UnityEditor上的菜单PureScript/Build构建。
79+
2. 目前iOS平台带的mono库为arm64指令集没有开启bitcode,如有其它需求请自己构建一个替换。
80+
3. 如果是windows平台记得根据上面说明生成ScriptEngine.dll 替换导出目录中的dll
81+
4. Unity2020编辑器默release模式导出失败,需要在右下角修改为debug模式即可正常导出。
82+
5. 建议所有网络或文件IO操作统一在il2cpp内执行,配置为Adapter提供给Mono使用。
7083

7184
--------------------------------------------
7285

0 commit comments

Comments
 (0)