Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 77878dd

Browse files
committed
Use reflection for adding Unity log callback to avoid unstripping errors
1 parent 594abc4 commit 77878dd

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/Core/Runtime/Il2Cpp/Il2CppProvider.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
5+
using System.Reflection;
6+
using BF = System.Reflection.BindingFlags;
57
using System.Text;
68
using UnhollowerBaseLib;
79
using UnhollowerRuntimeLib;
@@ -21,14 +23,22 @@ public override void Initialize()
2123

2224
public override void SetupEvents()
2325
{
24-
Application.add_logMessageReceived(
25-
new Action<string, string, LogType>(ExplorerCore.Instance.OnUnityLog));
26-
27-
//SceneManager.add_sceneLoaded(
28-
// new Action<Scene, LoadSceneMode>(ExplorerCore.Instance.OnSceneLoaded1));
29-
30-
//SceneManager.add_activeSceneChanged(
31-
// new Action<Scene, Scene>(ExplorerCore.Instance.OnSceneLoaded2));
26+
try
27+
{
28+
//Application.add_logMessageReceived(new Action<string, string, LogType>(ExplorerCore.Instance.OnUnityLog));
29+
30+
var logType = ReflectionUtility.GetTypeByName("UnityEngine.Application+LogCallback");
31+
var castMethod = logType.GetMethod("op_Implicit", new[] { typeof(Action<string, string, LogType>) });
32+
var addMethod = typeof(Application).GetMethod("add_logMessageReceived", BF.Static | BF.Public, null, new[] { logType }, null);
33+
addMethod.Invoke(null, new[]
34+
{
35+
castMethod.Invoke(null, new[] { new Action<string, string, LogType>(ExplorerCore.Instance.OnUnityLog) })
36+
});
37+
}
38+
catch
39+
{
40+
ExplorerCore.LogWarning("Exception setting up Unity log listener, make sure Unity libraries have been unstripped!");
41+
}
3242
}
3343

3444
internal delegate IntPtr d_LayerToName(int layer);

0 commit comments

Comments
 (0)