diff --git a/Runtimes/Unity/BepInEx.Unity.Mono/Logging/UnityLogSource.cs b/Runtimes/Unity/BepInEx.Unity.Mono/Logging/UnityLogSource.cs index 9d514c4..19289b4 100644 --- a/Runtimes/Unity/BepInEx.Unity.Mono/Logging/UnityLogSource.cs +++ b/Runtimes/Unity/BepInEx.Unity.Mono/Logging/UnityLogSource.cs @@ -2,6 +2,7 @@ using System.Reflection; using BepInEx.Logging; using UnityEngine; +using Logger = BepInEx.Logging.Logger; namespace BepInEx.Unity.Mono.Logging; @@ -61,6 +62,23 @@ public class UnityLogSource : ILogSource { var registerLogCallback = typeof(Application).GetMethod("RegisterLogCallback", BindingFlags.Public | BindingFlags.Static); + + // Unity's managed code stripper can remove both Application.logMessageReceived and the + // legacy Application.RegisterLogCallback. Invoking a null MethodInfo here throws a + // NullReferenceException out of this static constructor, which propagates through + // Chainloader.Initialize and silently prevents the chainloader (and therefore every + // plugin) from starting. Unity log forwarding is optional, so degrade instead of + // taking the whole loader down with us. + if (registerLogCallback == null) + { + Logger.Log(LogLevel.Warning, + "Unity log forwarding is unavailable: neither " + + "Application.logMessageReceived nor Application.RegisterLogCallback exist " + + "in this build (they were most likely stripped). Unity log messages will " + + "not appear in the BepInEx log."); + return; + } + registerLogCallback.Invoke(null, new object[] { callback }); //UnsubscribeAction = () => registerLogCallback.Invoke(null, new object[] { null }); }