From 665b6536aa4300da997af374930b9929f89757e0 Mon Sep 17 00:00:00 2001 From: Bepis <36346617+bbepis@users.noreply.github.com> Date: Fri, 6 Nov 2020 13:19:53 +1100 Subject: [PATCH] Specify and preload a list of critical assemblies Rebase of c77eee6 --- BepInEx.Preloader.Unity/DoorstopEntrypoint.cs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/BepInEx.Preloader.Unity/DoorstopEntrypoint.cs b/BepInEx.Preloader.Unity/DoorstopEntrypoint.cs index e9bb524..06a9edf 100644 --- a/BepInEx.Preloader.Unity/DoorstopEntrypoint.cs +++ b/BepInEx.Preloader.Unity/DoorstopEntrypoint.cs @@ -8,6 +8,36 @@ namespace BepInEx.Preloader.Unity { internal static class UnityPreloaderRunner { + // This is a list of important assemblies in BepInEx core folder that should be force-loaded + // Some games can ship these assemblies in Managed folder, in which case assembly resolving bypasses our LocalResolve + // On the other hand, renaming these assemblies is not viable because 3rd party assemblies + // that we don't build (e.g. MonoMod, Harmony, many plugins) depend on them + // As such, we load them early so that the game uses our version instead + // These assemblies should be known to be rarely edited and are known to be shipped as-is with Unity assets + private static readonly string[] CriticalAssemblies = + { + "Mono.Cecil.dll", + "Mono.Cecil.Mdb.dll", + "Mono.Cecil.Pdb.dll", + "Mono.Cecil.Rocks.dll", + }; + + private static void LoadCriticalAssemblies() + { + foreach (string criticalAssembly in CriticalAssemblies) + { + try + { + Assembly.LoadFile(Path.Combine(Paths.BepInExAssemblyDirectory, criticalAssembly)); + } + catch (Exception) + { + // Suppress error for now + // TODO: Should we crash here if load fails? Can't use logging at this point + } + } + } + public static void PreloaderPreMain() { string bepinPath = Utility.ParentDirectory(Path.GetFullPath(EnvVars.DOORSTOP_INVOKE_DLL_PATH), 2); @@ -17,6 +47,7 @@ namespace BepInEx.Preloader.Unity // Remove temporary resolver early so it won't override local resolver AppDomain.CurrentDomain.AssemblyResolve -= DoorstopEntrypoint.ResolveCurrentDirectory; + LoadCriticalAssemblies(); PreloaderMain(); }