From c77eee6ec3ba4f6a8eb8b83f9f219cb6696a5105 Mon Sep 17 00:00:00 2001 From: ghorsington Date: Mon, 19 Oct 2020 13:42:43 +0300 Subject: [PATCH] Specify and preload a list of critical assemblies --- BepInEx.Preloader/Entrypoint.cs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/BepInEx.Preloader/Entrypoint.cs b/BepInEx.Preloader/Entrypoint.cs index 61d8254..c8c227e 100644 --- a/BepInEx.Preloader/Entrypoint.cs +++ b/BepInEx.Preloader/Entrypoint.cs @@ -9,14 +9,47 @@ namespace BepInEx.Preloader { internal static class PreloaderRunner { + // 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); Paths.SetExecutablePath(EnvVars.DOORSTOP_PROCESS_PATH, bepinPath, EnvVars.DOORSTOP_MANAGED_FOLDER_DIR); + AppDomain.CurrentDomain.AssemblyResolve += LocalResolve; // Remove temporary resolver early so it won't override local resolver AppDomain.CurrentDomain.AssemblyResolve -= Entrypoint.ResolveCurrentDirectory; + + LoadCriticalAssemblies(); PreloaderMain(); }