Selaa lähdekoodia

Specify and preload a list of critical assemblies

ghorsington 4 vuotta sitten
vanhempi
commit
c77eee6ec3
1 muutettua tiedostoa jossa 33 lisäystä ja 0 poistoa
  1. 33 0
      BepInEx.Preloader/Entrypoint.cs

+ 33 - 0
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();
 		}