From 6d82b2e5fcff13d1a99bba7f00f8756f129859fd Mon Sep 17 00:00:00 2001 From: Drew Mochak Date: Tue, 1 Sep 2026 00:56:05 -0400 Subject: [PATCH] Don't kill the chainloader when Unity log callbacks are stripped (#1389) UnityLogSource's static constructor falls back to Application.RegisterLogCallback when Application.logMessageReceived is absent. Unity's managed code stripper can remove both, in which case GetMethod returns null and the unguarded Invoke throws a NullReferenceException out of the static constructor. That exception propagates through chainloader initialisation, so the chainloader never starts and no plugins load. The failure is close to undiagnosable: LogOutput.log ends at "Preloader finished" with no error, and the exception only appears on stdout. Unity log forwarding is optional, so warn and continue instead of taking the whole loader down. Seen on Beacon Pines (Unity 2021.3.45f1, Mono), whose UnityEngine.Application has neither member. --- .../Logging/UnityLogSource.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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 }); }