Просмотр исходного кода

Actually use the patched entrypoint assembly instead of only pretending to

Bepis 4 лет назад
Родитель
Сommit
185e89515c

+ 17 - 2
BepInEx.NetLauncher/NetPreloader.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Diagnostics;
 using System.IO;
+using System.Linq;
 using System.Reflection;
 using BepInEx.Bootstrap;
 using BepInEx.Configuration;
@@ -71,6 +72,7 @@ namespace BepInEx.NetLauncher
 
 			Log.LogMessage("Preloader started");
 
+			Assembly entrypointAssembly;
 
 			using (var assemblyPatcher = new AssemblyPatcher())
 			{
@@ -83,6 +85,21 @@ namespace BepInEx.NetLauncher
 				Log.LogInfo($"{assemblyPatcher.AssembliesToPatch.Count} assemblies discovered");
 
 				assemblyPatcher.PatchAndLoad();
+
+
+				var assemblyName = AssemblyName.GetAssemblyName(executablePath);
+
+				entrypointAssembly = assemblyPatcher.LoadedAssemblies.Values.FirstOrDefault(x => x.FullName == assemblyName.FullName);
+
+				if (entrypointAssembly != null)
+				{
+					Log.LogDebug("Found patched entrypoint assembly! Using it");
+				}
+				else
+				{
+					Log.LogDebug("Using entrypoint assembly from disk");
+					entrypointAssembly = Assembly.LoadFrom(executablePath);
+				}
 			}
 
 			Log.LogMessage("Preloader finished");
@@ -91,8 +108,6 @@ namespace BepInEx.NetLauncher
 			chainloader.Initialize();
 			chainloader.Execute();
 
-			var assemblyName = AssemblyName.GetAssemblyName(executablePath);
-			var entrypointAssembly = Assembly.Load(assemblyName);
 
 			AssemblyFix.Execute(entrypointAssembly);
 

+ 12 - 2
BepInEx.Preloader.Core/Patching/AssemblyPatcher.cs

@@ -39,6 +39,12 @@ namespace BepInEx.Preloader.Core
 		public Dictionary<string, AssemblyDefinition> AssembliesToPatch { get; } = new Dictionary<string, AssemblyDefinition>();
 
 		/// <summary>
+		/// <para>Contains a dictionary of assemblies that have been loaded as part of executing this assembly patcher..</para>
+		/// <para>The key is the same key as used in <see cref="LoadedAssemblies"/>, while the value is the actual assembly itself.</para>
+		/// </summary>
+		public Dictionary<string, Assembly> LoadedAssemblies { get; } = new Dictionary<string, Assembly>();
+
+		/// <summary>
 		/// The directory location as to where patched assemblies will be saved to and loaded from disk, for debugging purposes. Defaults to BepInEx/DumpedAssemblies
 		/// </summary>
 		public string DumpedAssembliesPath { get; set; } = Path.Combine(Paths.BepInExRootPath, "DumpedAssemblies");
@@ -335,17 +341,21 @@ namespace BepInEx.Preloader.Core
 				// but because some games *rely* on that because of messed up internal dependencies.
 				if (patchedAssemblies.Contains(filename))
 				{
+					Assembly loadedAssembly;
+
 					if (ConfigLoadDumpedAssemblies.Value)
-						Assembly.LoadFile(Path.Combine(DumpedAssembliesPath, filename));
+						loadedAssembly = Assembly.LoadFile(Path.Combine(DumpedAssembliesPath, filename));
 					else
 					{
 						using (var assemblyStream = new MemoryStream())
 						{
 							assembly.Write(assemblyStream);
-							Assembly.Load(assemblyStream.ToArray());
+							loadedAssembly =Assembly.Load(assemblyStream.ToArray());
 						}
 					}
 
+					LoadedAssemblies.Add(filename, loadedAssembly);
+
 					Logger.LogDebug($"Loaded '{assembly.FullName}' into memory");
 				}