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

Add runtime fix for Assembly.GetEntryAssembly() not returning what games expect

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

+ 2 - 0
BepInEx.NetLauncher/BepInEx.NetLauncher.csproj

@@ -64,6 +64,7 @@
     <Compile Include="NetPreloader.cs" />
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="RuntimeFixes\AssemblyFix.cs" />
     <Compile Include="Utility.cs" />
   </ItemGroup>
   <ItemGroup>
@@ -88,5 +89,6 @@
       <Name>Harmony</Name>
     </ProjectReference>
   </ItemGroup>
+  <ItemGroup />
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>

+ 3 - 0
BepInEx.NetLauncher/NetPreloader.cs

@@ -5,6 +5,7 @@ using System.Reflection;
 using BepInEx.Bootstrap;
 using BepInEx.Configuration;
 using BepInEx.Logging;
+using BepInEx.NetLauncher.RuntimeFixes;
 using BepInEx.Preloader.Core;
 using BepInEx.Preloader.Core.RuntimeFixes;
 using MonoMod.RuntimeDetour;
@@ -93,6 +94,8 @@ namespace BepInEx.NetLauncher
 			var assemblyName = AssemblyName.GetAssemblyName(executablePath);
 			var entrypointAssembly = Assembly.Load(assemblyName);
 
+			AssemblyFix.Execute(entrypointAssembly);
+
 			entrypointAssembly.EntryPoint.Invoke(null, new [] { args });
 		}
 

+ 29 - 0
BepInEx.NetLauncher/RuntimeFixes/AssemblyFix.cs

@@ -0,0 +1,29 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+using BepInEx.Harmony;
+using HarmonyLib;
+
+namespace BepInEx.NetLauncher.RuntimeFixes
+{
+	internal class AssemblyFix
+	{
+		private static Assembly EntryAssembly { get; set; }
+
+		public static void Execute(Assembly entryAssembly)
+		{
+			EntryAssembly = entryAssembly;
+			HarmonyWrapper.PatchAll(typeof(AssemblyFix), "bepinex.assemblyfix");
+		}
+
+		[HarmonyPrefix, HarmonyPatch(typeof(Assembly), nameof(Assembly.GetEntryAssembly))]
+		public static bool GetEntryAssemblyPrefix(ref Assembly __result)
+		{
+			__result = EntryAssembly;
+			return false;
+		}
+	}
+}