From 6bd59af09db4330fee32f36022fd817edb8dc1ca Mon Sep 17 00:00:00 2001 From: ghorsington Date: Fri, 18 Sep 2020 21:31:35 +0300 Subject: [PATCH] Add a workaround for MissingMethodException for unityVersion in some rare cases --- BepInEx/Bootstrap/Chainloader.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/BepInEx/Bootstrap/Chainloader.cs b/BepInEx/Bootstrap/Chainloader.cs index 2d78be7..e012a58 100644 --- a/BepInEx/Bootstrap/Chainloader.cs +++ b/BepInEx/Bootstrap/Chainloader.cs @@ -5,7 +5,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; -using System.Text; +using System.Runtime.CompilerServices; using System.Text.RegularExpressions; using Mono.Cecil; using MonoMod.Utils; @@ -25,6 +25,18 @@ namespace BepInEx.Bootstrap public static Dictionary PluginInfos { get; } = new Dictionary(); private static readonly List _plugins = new List(); + + // In some rare cases calling Application.unityVersion seems to cause MissingMethodException + // if a preloader patch applies Harmony patch to Chainloader.Initialize. + // The issue could be related to BepInEx being compiled against Unity 5.6 version of UnityEngine.dll, + // but the issue is apparently present with both official Harmony and HarmonyX + // We specifically prevent inlining to prevent early resolving + // TODO: Figure out better version obtaining mechanism (e.g. from globalmanagers) + private static string UnityVersion + { + [MethodImpl(MethodImplOptions.NoInlining)] + get => Application.unityVersion; + } /// /// List of all loaded via the chainloader. @@ -99,7 +111,7 @@ namespace BepInEx.Bootstrap if (Utility.CurrentOs == Platform.Linux) { - Logger.LogInfo($"Detected Unity version: v{Application.unityVersion}"); + Logger.LogInfo($"Detected Unity version: v{UnityVersion}"); } Logger.LogMessage("Chainloader ready");