Browse Source

Implement IL2CPP support in HarmonyX

Bepis 3 years ago
parent
commit
b2662fb8a5

+ 3 - 19
BepInEx.Core/BepInEx.Core.csproj

@@ -39,18 +39,6 @@
     <DebugSymbols>true</DebugSymbols>
   </PropertyGroup>
   <ItemGroup>
-    <Reference Include="Mono.Cecil, Version=0.10.4.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
-      <HintPath>..\packages\Mono.Cecil.0.10.4\lib\net35\Mono.Cecil.dll</HintPath>
-    </Reference>
-    <Reference Include="Mono.Cecil.Mdb, Version=0.10.4.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
-      <HintPath>..\packages\Mono.Cecil.0.10.4\lib\net35\Mono.Cecil.Mdb.dll</HintPath>
-    </Reference>
-    <Reference Include="Mono.Cecil.Pdb, Version=0.10.4.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
-      <HintPath>..\packages\Mono.Cecil.0.10.4\lib\net35\Mono.Cecil.Pdb.dll</HintPath>
-    </Reference>
-    <Reference Include="Mono.Cecil.Rocks, Version=0.10.4.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
-      <HintPath>..\packages\Mono.Cecil.0.10.4\lib\net35\Mono.Cecil.Rocks.dll</HintPath>
-    </Reference>
     <Reference Include="System" />
   </ItemGroup>
   <ItemGroup>
@@ -91,13 +79,9 @@
     <Compile Include="Utility.cs" />
   </ItemGroup>
   <ItemGroup>
-    <ProjectReference Include="..\submodules\BepInEx.Harmony\BepInEx.Harmony\BepInEx.Harmony.csproj">
-      <Project>{54161cfe-ff42-4dde-b161-3a49545db5cd}</Project>
-      <Name>BepInEx.Harmony</Name>
-    </ProjectReference>
-  </ItemGroup>
-  <ItemGroup>
-    <None Include="packages.config" />
+    <PackageReference Include="Mono.Cecil">
+      <Version>0.10.4</Version>
+    </PackageReference>
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>

+ 0 - 4
BepInEx.Core/packages.config

@@ -1,4 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<packages>
-  <package id="Mono.Cecil" version="0.10.4" targetFramework="net35" />
-</packages>

+ 14 - 28
BepInEx.IL2CPP/BepInEx.IL2CPP.csproj

@@ -25,40 +25,20 @@
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
-    <DebugType>pdbonly</DebugType>
+    <DebugType>portable</DebugType>
     <Optimize>true</Optimize>
     <OutputPath>..\bin\il2cpp\</OutputPath>
     <DefineConstants>TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+    <DebugSymbols>true</DebugSymbols>
   </PropertyGroup>
   <ItemGroup>
-    <Reference Include="Iced, Version=1.6.0.0, Culture=neutral, PublicKeyToken=5baba79f4264913b, processorArchitecture=MSIL">
-      <HintPath>..\packages\Iced.1.6.0\lib\net45\Iced.dll</HintPath>
-    </Reference>
     <Reference Include="Il2Cppmscorlib">
       <HintPath>..\lib\Il2Cppmscorlib.dll</HintPath>
       <Private>False</Private>
     </Reference>
-    <Reference Include="Mono.Cecil, Version=0.11.2.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
-      <HintPath>..\packages\Mono.Cecil.0.11.2\lib\net40\Mono.Cecil.dll</HintPath>
-    </Reference>
-    <Reference Include="Mono.Cecil.Mdb, Version=0.11.2.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
-      <HintPath>..\packages\Mono.Cecil.0.11.2\lib\net40\Mono.Cecil.Mdb.dll</HintPath>
-    </Reference>
-    <Reference Include="Mono.Cecil.Pdb, Version=0.11.2.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
-      <HintPath>..\packages\Mono.Cecil.0.11.2\lib\net40\Mono.Cecil.Pdb.dll</HintPath>
-    </Reference>
-    <Reference Include="Mono.Cecil.Rocks, Version=0.11.2.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
-      <HintPath>..\packages\Mono.Cecil.0.11.2\lib\net40\Mono.Cecil.Rocks.dll</HintPath>
-    </Reference>
-    <Reference Include="MonoMod.RuntimeDetour, Version=20.5.21.5, Culture=neutral, processorArchitecture=MSIL">
-      <HintPath>..\packages\MonoMod.RuntimeDetour.20.5.21.5\lib\net40\MonoMod.RuntimeDetour.dll</HintPath>
-    </Reference>
-    <Reference Include="MonoMod.Utils, Version=20.5.21.5, Culture=neutral, processorArchitecture=MSIL">
-      <HintPath>..\packages\MonoMod.Utils.20.5.21.5\lib\net40\MonoMod.Utils.dll</HintPath>
-    </Reference>
     <Reference Include="System" />
     <Reference Include="System.Core" />
     <Reference Include="Microsoft.CSharp" />
@@ -80,6 +60,7 @@
     <Compile Include="DoorstopEntrypoint.cs" />
     <Compile Include="EnvVars.cs" />
     <Compile Include="Hook\FastNativeDetour.cs" />
+    <Compile Include="Hook\IL2CPPDetourMethodPatcher.cs" />
     <Compile Include="IL2CPPChainloader.cs" />
     <Compile Include="MonoExtensions.cs" />
     <Compile Include="Preloader.cs" />
@@ -96,15 +77,20 @@
       <Project>{15f8bc38-a761-4f93-8903-1b531ac5d9f9}</Project>
       <Name>BepInEx.Preloader.Core</Name>
     </ProjectReference>
-    <ProjectReference Include="..\submodules\BepInEx.Harmony\BepInEx.Harmony\BepInEx.Harmony.csproj">
-      <Project>{54161cfe-ff42-4dde-b161-3a49545db5cd}</Project>
-      <Name>BepInEx.Harmony</Name>
-    </ProjectReference>
   </ItemGroup>
   <ItemGroup>
     <None Include="app.config" />
-    <None Include="packages.config" />
   </ItemGroup>
-  <ItemGroup />
+  <ItemGroup>
+    <PackageReference Include="HarmonyX">
+      <Version>2.1.0-beta</Version>
+    </PackageReference>
+    <PackageReference Include="Iced">
+      <Version>1.6.0</Version>
+    </PackageReference>
+    <PackageReference Include="MonoMod.RuntimeDetour">
+      <Version>20.5.21.5</Version>
+    </PackageReference>
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>

+ 22 - 0
BepInEx.IL2CPP/Hook/DetourGenerator.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Runtime.InteropServices;
 using BepInEx.Logging;
 using Iced.Intel;
+using MonoMod.RuntimeDetour;
 
 namespace BepInEx.IL2CPP
 {
@@ -58,6 +59,27 @@ namespace BepInEx.IL2CPP
 				Marshal.WriteByte(functionPtr + i, 0x90);
 		}
 
+		public static IntPtr CreateTrampolineFromFunction(IntPtr originalFuncPointer, out int trampolineLength, out int jmpLength)
+		{
+			byte[] instructionBuffer = new byte[32];
+			Marshal.Copy(originalFuncPointer, instructionBuffer, 0, 32);
+
+			var trampolinePtr = DetourHelper.Native.MemAlloc(80);
+
+			DetourHelper.Native.MakeWritable(trampolinePtr, 80);
+
+			var arch = IntPtr.Size == 8 ? Architecture.X64 : Architecture.X86;
+
+			int minimumTrampolineLength = GetDetourLength(arch);
+
+			CreateTrampolineFromFunction(instructionBuffer, originalFuncPointer, trampolinePtr, minimumTrampolineLength, arch, out trampolineLength, out jmpLength);
+
+			DetourHelper.Native.MakeExecutable(originalFuncPointer, 32);
+			DetourHelper.Native.MakeExecutable(trampolinePtr, (uint)trampolineLength);
+
+			return trampolinePtr;
+		}
+
 		/// <summary>
 		/// Reads assembly from <see cref="functionPtr"/> (at least <see cref="minimumTrampolineLength"/> bytes), and writes it to <see cref="trampolinePtr"/> plus a jmp to continue execution.
 		/// </summary>

+ 337 - 0
BepInEx.IL2CPP/Hook/IL2CPPDetourMethodPatcher.cs

@@ -0,0 +1,337 @@
+using System;
+using System.Linq;
+using System.Reflection;
+using System.Reflection.Emit;
+using System.Runtime.InteropServices;
+using BepInEx.Logging;
+using HarmonyLib;
+using HarmonyLib.Public.Patching;
+using MonoMod.Cil;
+using MonoMod.RuntimeDetour;
+using MonoMod.Utils;
+using UnhollowerBaseLib;
+using UnhollowerBaseLib.Runtime;
+
+namespace BepInEx.IL2CPP.Hook
+{
+	public unsafe class IL2CPPDetourMethodPatcher : MethodPatcher
+	{
+		private static readonly MethodInfo IL2CPPToManagedStringMethodInfo
+			= AccessTools.Method(typeof(UnhollowerBaseLib.IL2CPP), nameof(UnhollowerBaseLib.IL2CPP.Il2CppStringToManaged));
+
+		private static readonly MethodInfo ManagedToIL2CPPStringMethodInfo
+			= AccessTools.Method(typeof(UnhollowerBaseLib.IL2CPP), nameof(UnhollowerBaseLib.IL2CPP.ManagedStringToIl2Cpp));
+
+		private static readonly MethodInfo ObjectBaseToPtrMethodInfo
+			= AccessTools.Method(typeof(UnhollowerBaseLib.IL2CPP), nameof(UnhollowerBaseLib.IL2CPP.Il2CppObjectBaseToPtr));
+
+
+		private static readonly ManualLogSource DetourLogger = Logger.CreateLogSource("Detour");
+
+		private FastNativeDetour nativeDetour;
+
+		private Il2CppMethodInfo* originalNativeMethodInfo;
+		private Il2CppMethodInfo* modifiedNativeMethodInfo;
+
+		/// <summary>
+		/// Constructs a new instance of <see cref="NativeDetour"/> method patcher.
+		/// </summary>
+		/// <param name="original"></param>
+		public IL2CPPDetourMethodPatcher(MethodBase original) : base(original)
+		{
+			Init();
+		}
+
+		private void Init()
+		{
+			// Get the native MethodInfo struct for the target method
+
+			originalNativeMethodInfo = (Il2CppMethodInfo*)
+				(IntPtr)UnhollowerUtils.GetIl2CppMethodInfoPointerFieldForGeneratedMethod(Original).GetValue(null);
+
+			// Create a trampoline from the original target method
+
+			var trampolinePtr = DetourGenerator.CreateTrampolineFromFunction(originalNativeMethodInfo->methodPointer, out _, out _);
+
+			// Create a modified native MethodInfo struct to point towards the trampoline
+
+			modifiedNativeMethodInfo = (Il2CppMethodInfo*)Marshal.AllocHGlobal(Marshal.SizeOf<Il2CppMethodInfo>());
+			Marshal.StructureToPtr(*originalNativeMethodInfo, (IntPtr)modifiedNativeMethodInfo, false);
+
+			modifiedNativeMethodInfo->methodPointer = trampolinePtr;
+		}
+
+		/// <inheritdoc />
+		public override DynamicMethodDefinition PrepareOriginal()
+		{
+			return null;
+		}
+
+		/// <inheritdoc />
+		public override MethodBase DetourTo(MethodBase replacement)
+		{
+			// Unpatch an existing detour if it exists
+
+			nativeDetour?.Dispose();
+
+			// Generate a new DMD of the modified unhollowed method, and apply harmony patches to it
+
+			var copiedDmd = CopyOriginal();
+
+			HarmonyManipulator.Manipulate(copiedDmd.OriginalMethod, copiedDmd.OriginalMethod.GetPatchInfo(), new ILContext(copiedDmd.Definition));
+
+
+			// Generate the MethodInfo instances
+
+			var managedHookedMethod = copiedDmd.Generate();
+			var unmanagedTrampolineMethod = GenerateNativeToManagedTrampoline(managedHookedMethod).Generate();
+
+
+			// Apply a detour from the unmanaged implementation to the patched harmony method
+
+			var unmanagedDelegateType = DelegateTypeFactory.instance.CreateDelegateType(unmanagedTrampolineMethod.ReturnType,
+				unmanagedTrampolineMethod.GetParameters().Select(x => x.ParameterType).ToArray());
+
+
+			var detourPtr = MonoExtensions.GetFunctionPointerForDelegate(unmanagedTrampolineMethod.CreateDelegate(unmanagedDelegateType),
+				CallingConvention.Cdecl);
+
+			nativeDetour = new FastNativeDetour(originalNativeMethodInfo->methodPointer, detourPtr);
+
+			nativeDetour.Apply();
+
+			// TODO: Add an ILHook for the original unhollowed method to go directly to managedHookedMethod
+			// Right now it goes through three times as much interop conversion as it needs to, when being called from managed side
+
+			return managedHookedMethod;
+		}
+
+		/// <inheritdoc />
+		public override DynamicMethodDefinition CopyOriginal()
+		{
+			var dmd = new DynamicMethodDefinition(Original);
+			dmd.Definition.Name = "UnhollowedWrapper_" + dmd.Definition.Name;
+
+			var cursor = new ILCursor(new ILContext(dmd.Definition));
+
+
+			// Remove il2cpp_object_get_virtual_method
+
+			bool foundVirtMethodCall = false;
+
+			if (cursor.TryGotoNext(x => x.MatchLdarg(0),
+				x => x.MatchCall(typeof(UnhollowerBaseLib.IL2CPP), nameof(UnhollowerBaseLib.IL2CPP.Il2CppObjectBaseToPtr)),
+				x => x.MatchLdsfld(out _),
+				x => x.MatchCall(typeof(UnhollowerBaseLib.IL2CPP), nameof(UnhollowerBaseLib.IL2CPP.il2cpp_object_get_virtual_method))))
+			{
+				cursor.RemoveRange(4);
+
+				foundVirtMethodCall = true;
+			}
+
+			// Replace original IL2CPPMethodInfo pointer with a modified one that points to the trampoline
+
+			if (!foundVirtMethodCall)
+			{
+				cursor.Goto(0)
+					.GotoNext(x => x.MatchLdsfld(UnhollowerUtils.GetIl2CppMethodInfoPointerFieldForGeneratedMethod(Original)))
+					.Remove();
+			}
+
+			cursor
+				.Emit(Mono.Cecil.Cil.OpCodes.Ldc_I8, ((IntPtr)modifiedNativeMethodInfo).ToInt64())
+				.Emit(Mono.Cecil.Cil.OpCodes.Conv_I);
+
+			return dmd;
+		}
+
+		/// <summary>
+		/// A handler for <see cref="PatchManager.ResolvePatcher"/> that checks if a method doesn't have a body
+		/// (e.g. it's icall or marked with <see cref="DynDllImportAttribute"/>) and thus can be patched with
+		/// <see cref="NativeDetour"/>.
+		/// </summary>
+		/// <param name="sender">Not used</param>
+		/// <param name="args">Patch resolver arguments</param>
+		///
+		public static void TryResolve(object sender, PatchManager.PatcherResolverEventArgs args)
+		{
+			if (args.Original.DeclaringType?.IsSubclassOf(typeof(Il2CppObjectBase)) == true)
+				args.MethodPatcher = new IL2CPPDetourMethodPatcher(args.Original);
+		}
+
+		private DynamicMethodDefinition GenerateNativeToManagedTrampoline(MethodInfo targetManagedMethodInfo)
+		{
+			// managedParams are the unhollower types used on the managed side
+			// unmanagedParams are IntPtr references that are used by IL2CPP compiled assembly
+
+			var managedParams = targetManagedMethodInfo.GetParameters().Select(x => x.ParameterType).ToArray();
+			var unmanagedParams = managedParams.Select(ConvertManagedTypeToIL2CPPType).ToArray();
+
+			var managedReturnType = AccessTools.GetReturnedType(targetManagedMethodInfo);
+			var unmanagedReturnType = ConvertManagedTypeToIL2CPPType(managedReturnType);
+
+			var dmd = new DynamicMethodDefinition("(il2cpp -> managed) " + targetManagedMethodInfo.Name,
+				unmanagedReturnType,
+				unmanagedParams
+			);
+
+			var il = dmd.GetILGenerator();
+
+			il.BeginExceptionBlock();
+
+
+			// Declare a list of variables to dereference back to the original pointers.
+			// This is required due to the needed unhollower type conversions, so we can't directly pass some addresses as byref types
+
+			LocalBuilder[] indirectVariables = new LocalBuilder[managedParams.Length];
+
+			for (int i = 0; i < managedParams.Length; ++i)
+			{
+				il.Emit(OpCodes.Ldarg_S, i);
+				EmitConvertArgumentToManaged(il, managedParams[i], out indirectVariables[i]);
+			}
+
+			// Run the managed method
+
+			il.Emit(OpCodes.Call, targetManagedMethodInfo);
+
+
+			// Store the managed return type temporarily (if there was one)
+
+			LocalBuilder managedReturnVariable = null;
+
+			if (managedReturnType != typeof(void))
+			{
+				managedReturnVariable = il.DeclareLocal(managedReturnType);
+				il.Emit(OpCodes.Stloc, managedReturnVariable);
+			}
+
+
+			// Convert any managed byref values into their relevant IL2CPP types, and then store the values into their relevant dereferenced pointers
+
+			for (int i = 0; i < managedParams.Length; ++i)
+			{
+				if (indirectVariables[i] == null)
+					continue;
+
+				il.Emit(OpCodes.Ldarg_S, i);
+				il.Emit(OpCodes.Ldloc, indirectVariables[i]);
+
+				EmitConvertManagedTypeToIL2CPP(il, managedParams[i].GetElementType());
+
+				il.Emit(OpCodes.Stind_I);
+			}
+
+			// Handle any lingering exceptions
+
+			il.BeginCatchBlock(typeof(Exception));
+
+			il.Emit(OpCodes.Call, AccessTools.Method(typeof(IL2CPPDetourMethodPatcher), nameof(ReportException)));
+
+			il.EndExceptionBlock();
+
+			// Convert the return value back to an IL2CPP friendly type (if there was a return value), and then return
+
+			if (managedReturnVariable != null)
+			{
+				il.Emit(OpCodes.Ldloc, managedReturnVariable);
+				EmitConvertManagedTypeToIL2CPP(il, managedReturnType);
+			}
+
+			il.Emit(OpCodes.Ret);
+
+			return dmd;
+		}
+
+		private static void ReportException(Exception ex)
+		{
+			DetourLogger.LogError(ex.ToString());
+		}
+
+		private static Type ConvertManagedTypeToIL2CPPType(Type managedType)
+		{
+			if (managedType.IsByRef)
+			{
+				Type directType = managedType.GetElementType();
+				if (directType == typeof(string) || directType.IsSubclassOf(typeof(Il2CppObjectBase)))
+				{
+					return typeof(IntPtr*);
+				}
+			}
+			else if (managedType == typeof(string) || managedType.IsSubclassOf(typeof(Il2CppObjectBase)))
+			{
+				return typeof(IntPtr);
+			}
+
+			return managedType;
+		}
+
+		private static void EmitConvertManagedTypeToIL2CPP(ILGenerator il, Type returnType)
+		{
+			if (returnType == typeof(string))
+			{
+				il.Emit(OpCodes.Call, ManagedToIL2CPPStringMethodInfo);
+			}
+			else if (!returnType.IsValueType && returnType.IsSubclassOf(typeof(Il2CppObjectBase)))
+			{
+				il.Emit(OpCodes.Call, ObjectBaseToPtrMethodInfo);
+			}
+		}
+
+		private static void EmitConvertArgumentToManaged(ILGenerator il, Type managedParamType, out LocalBuilder variable)
+		{
+			variable = null;
+
+			if (managedParamType.IsValueType) // don't need to convert blittable types
+				return;
+
+			void EmitCreateIl2CppObject()
+			{
+				Label endLabel = il.DefineLabel();
+				Label notNullLabel = il.DefineLabel();
+
+				il.Emit(OpCodes.Dup);
+				il.Emit(OpCodes.Brtrue_S, notNullLabel);
+
+				il.Emit(OpCodes.Pop);
+				il.Emit(OpCodes.Ldnull);
+				il.Emit(OpCodes.Br_S, endLabel);
+
+				il.MarkLabel(notNullLabel);
+				il.Emit(OpCodes.Newobj, AccessTools.DeclaredConstructor(managedParamType, new[] { typeof(IntPtr) }));
+
+				il.MarkLabel(endLabel);
+			}
+
+			void HandleTypeConversion(Type originalType)
+			{
+				if (originalType == typeof(string))
+				{
+					il.Emit(OpCodes.Call, IL2CPPToManagedStringMethodInfo);
+				}
+				else if (originalType.IsSubclassOf(typeof(Il2CppObjectBase)))
+				{
+					EmitCreateIl2CppObject();
+				}
+			}
+
+			if (managedParamType.IsByRef)
+			{
+				Type directType = managedParamType.GetElementType();
+
+				variable = il.DeclareLocal(directType);
+
+				il.Emit(OpCodes.Ldind_I);
+
+				HandleTypeConversion(directType);
+
+				il.Emit(OpCodes.Stloc, variable);
+				il.Emit(OpCodes.Ldloca, variable);
+			}
+			else
+			{
+				HandleTypeConversion(managedParamType);
+			}
+		}
+	}
+}

+ 32 - 35
BepInEx.IL2CPP/IL2CPPChainloader.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Collections.Generic;
 using System.Diagnostics;
 using System.Linq;
 using System.Reflection;
@@ -9,10 +8,12 @@ using BepInEx.IL2CPP.Hook;
 using BepInEx.Logging;
 using BepInEx.Preloader.Core;
 using BepInEx.Preloader.Core.Logging;
+using HarmonyLib.Public.Patching;
 using UnhollowerBaseLib.Runtime;
 using UnhollowerRuntimeLib;
 using UnityEngine;
 using Logger = BepInEx.Logging.Logger;
+using BaseUnityEngine = UnityEngine;
 
 namespace BepInEx.IL2CPP
 {
@@ -25,30 +26,32 @@ namespace BepInEx.IL2CPP
 			UnityLogSource.LogInfo(logLine.Trim());
 		}
 
-
-//		public const CallingConvention ArchConvention =
-//#if X64
-//			CallingConvention.Stdcall;
-//#else
-//			CallingConvention.Cdecl;
-//#endif
-
-		[UnmanagedFunctionPointer(CallingConvention.StdCall)]
-		private delegate IntPtr RuntimeInvokeDetour(IntPtr method, IntPtr obj, IntPtr parameters, IntPtr exc);
+		[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+		private delegate IntPtr RuntimeInvokeDetourDelegate(IntPtr method, IntPtr obj, IntPtr parameters, IntPtr exc);
 
 		[DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
 		private static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
 
-		private static RuntimeInvokeDetour originalInvoke;
+		private static RuntimeInvokeDetourDelegate originalInvoke;
 
 		private static readonly ManualLogSource unhollowerLogSource = Logger.CreateLogSource("Unhollower");
 
+		private static FastNativeDetour RuntimeInvokeDetour { get; set; }
+
+		private static IL2CPPChainloader Instance { get; set; }
+
 
 		public override unsafe void Initialize(string gameExePath = null)
 		{
+			PatchManager.ResolvePatcher += IL2CPPDetourMethodPatcher.TryResolve;
+
 			base.Initialize(gameExePath);
+			Instance = this;
+
+			var version = //Version.Parse(Application.unityVersion);
+				Version.Parse(Process.GetCurrentProcess().MainModule.FileVersionInfo.FileVersion);
 
-			UnityVersionHandler.Initialize(2019, 2, 17);
+			UnityVersionHandler.Initialize(version.Major, version.Minor, version.Revision);
 
 			// One or the other here for Unhollower to work correctly
 
@@ -72,54 +75,49 @@ namespace BepInEx.IL2CPP
 
 			PreloaderLogger.Log.LogDebug($"Runtime invoke pointer: 0x{functionPtr.ToInt64():X}");
 
-			var invokeDetour = new FastNativeDetour(functionPtr, Marshal.GetFunctionPointerForDelegate(new RuntimeInvokeDetour(OnInvokeMethod)));
+			RuntimeInvokeDetour = new FastNativeDetour(functionPtr,
+				MonoExtensions.GetFunctionPointerForDelegate(new RuntimeInvokeDetourDelegate(OnInvokeMethod), CallingConvention.Cdecl));
 
-			invokeDetour.Apply(unhollowerLogSource);
+			RuntimeInvokeDetour.Apply();
 
-			originalInvoke = invokeDetour.GenerateTrampoline<RuntimeInvokeDetour>();
+			originalInvoke = RuntimeInvokeDetour.GenerateTrampoline<RuntimeInvokeDetourDelegate>();
+
+			PreloaderLogger.Log.LogDebug("Runtime invoke patched");
 		}
 
 
-		private static bool HasSet = false;
 
-		private static HashSet<string> recordedNames = new HashSet<string>();
 		private static IntPtr OnInvokeMethod(IntPtr method, IntPtr obj, IntPtr parameters, IntPtr exc)
 		{
 			string methodName = Marshal.PtrToStringAnsi(UnhollowerBaseLib.IL2CPP.il2cpp_method_get_name(method));
-			IntPtr methodClass = UnhollowerBaseLib.IL2CPP.il2cpp_method_get_class(method);
-			string methodClassName = Marshal.PtrToStringAnsi(UnhollowerBaseLib.IL2CPP.il2cpp_class_get_name(methodClass));
-			string methodClassNamespace = Marshal.PtrToStringAnsi(UnhollowerBaseLib.IL2CPP.il2cpp_class_get_namespace(methodClass));
 
-			string methodFullName = $"{methodClassNamespace}.{methodClassName}::{methodName}";
+			bool unhook = false;
 
-			if (!HasSet && methodName == "Internal_ActiveSceneChanged")
+			if (methodName == "Internal_ActiveSceneChanged")
 			{
 				try
 				{
-					Application.s_LogCallbackHandler = new Action<string, string, LogType>(UnityLogCallback);
+					//Application.s_LogCallbackHandler = new Action<string, string, LogType>(UnityLogCallback);
+
+					//Application.CallLogCallback("test from OnInvokeMethod", "", LogType.Log, true);
 
-					UnityLogSource.LogMessage($"callback set - {methodName}");
+					unhook = true;
 
-					Application.CallLogCallback("test from OnInvokeMethod", "", LogType.Log, true);
+					Instance.Execute();
 				}
 				catch (Exception ex)
 				{
 					UnityLogSource.LogError(ex);
 				}
-
-				HasSet = true;
 			}
 
 			var result = originalInvoke(method, obj, parameters, exc);
 
-			//UnityLogSource.LogDebug(methodName + " => " + result.ToString("X"));
-
-			if (!recordedNames.Contains(methodFullName))
+			if (unhook)
 			{
-				UnityLogSource.LogDebug(methodFullName + " => " + result.ToString("X"));
+				RuntimeInvokeDetour.Dispose();
 
-				lock (recordedNames)
-					recordedNames.Add(methodFullName);
+				PreloaderLogger.Log.LogDebug("Runtime invoke unpatched");
 			}
 
 			return result;
@@ -135,7 +133,6 @@ namespace BepInEx.IL2CPP
 			Logger.Sources.Add(UnityLogSource);
 
 
-
 			UnhollowerBaseLib.LogSupport.InfoHandler += unhollowerLogSource.LogInfo;
 			UnhollowerBaseLib.LogSupport.WarningHandler += unhollowerLogSource.LogWarning;
 			UnhollowerBaseLib.LogSupport.TraceHandler += unhollowerLogSource.LogDebug;

+ 1 - 0
BepInEx.IL2CPP/MonoExtensions.cs

@@ -18,6 +18,7 @@ namespace BepInEx
 			{
 				throw new ArgumentNullException("d");
 			}
+			
 			return GetFunctionPointerForDelegateInternal2(d, conv);
 		}
 	}

+ 0 - 4
BepInEx.IL2CPP/Preloader.cs

@@ -30,8 +30,6 @@ namespace BepInEx.IL2CPP
 
 
 				Log.LogInfo($"Running under Unity v{FileVersionInfo.GetVersionInfo(Paths.ExecutablePath).FileVersion}");
-				//Log.LogInfo($"CLR runtime version: {Environment.Version}");
-				//Log.LogInfo($"Supports SRE: {Utility.CLRSupportsDynamicAssemblies}");
 
 				Log.LogDebug($"Game executable path: {Paths.ExecutablePath}");
 				Log.LogDebug($"Unhollowed assembly directory: {IL2CPPUnhollowedPath}");
@@ -43,8 +41,6 @@ namespace BepInEx.IL2CPP
 				Chainloader = new IL2CPPChainloader();
 
 				Chainloader.Initialize();
-
-				Chainloader.Execute();
 			}
 			catch (Exception ex)
 			{

+ 0 - 7
BepInEx.IL2CPP/packages.config

@@ -1,7 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<packages>
-  <package id="Iced" version="1.6.0" targetFramework="net472" />
-  <package id="Mono.Cecil" version="0.11.2" targetFramework="net472" />
-  <package id="MonoMod.RuntimeDetour" version="20.5.21.5" targetFramework="net472" />
-  <package id="MonoMod.Utils" version="20.5.21.5" targetFramework="net472" />
-</packages>

+ 5 - 27
BepInEx.NetLauncher/BepInEx.NetLauncher.csproj

@@ -37,27 +37,6 @@
     </DocumentationFile>
   </PropertyGroup>
   <ItemGroup>
-    <Reference Include="0Harmony, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
-      <HintPath>..\packages\HarmonyX.2.0.6\lib\net40\0Harmony.dll</HintPath>
-    </Reference>
-    <Reference Include="Mono.Cecil, Version=0.11.2.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
-      <HintPath>..\packages\Mono.Cecil.0.11.2\lib\net40\Mono.Cecil.dll</HintPath>
-    </Reference>
-    <Reference Include="Mono.Cecil.Mdb, Version=0.11.2.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
-      <HintPath>..\packages\Mono.Cecil.0.11.2\lib\net40\Mono.Cecil.Mdb.dll</HintPath>
-    </Reference>
-    <Reference Include="Mono.Cecil.Pdb, Version=0.11.2.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
-      <HintPath>..\packages\Mono.Cecil.0.11.2\lib\net40\Mono.Cecil.Pdb.dll</HintPath>
-    </Reference>
-    <Reference Include="Mono.Cecil.Rocks, Version=0.11.2.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
-      <HintPath>..\packages\Mono.Cecil.0.11.2\lib\net40\Mono.Cecil.Rocks.dll</HintPath>
-    </Reference>
-    <Reference Include="MonoMod.RuntimeDetour, Version=20.5.21.5, Culture=neutral, processorArchitecture=MSIL">
-      <HintPath>..\packages\MonoMod.RuntimeDetour.20.5.21.5\lib\net40\MonoMod.RuntimeDetour.dll</HintPath>
-    </Reference>
-    <Reference Include="MonoMod.Utils, Version=20.5.21.5, Culture=neutral, processorArchitecture=MSIL">
-      <HintPath>..\packages\MonoMod.Utils.20.5.21.5\lib\net40\MonoMod.Utils.dll</HintPath>
-    </Reference>
     <Reference Include="System" />
     <Reference Include="System.Core" />
   </ItemGroup>
@@ -72,7 +51,6 @@
   </ItemGroup>
   <ItemGroup>
     <None Include="App.config" />
-    <None Include="packages.config" />
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\BepInEx.Core\BepInEx.Core.csproj">
@@ -83,11 +61,11 @@
       <Project>{15f8bc38-a761-4f93-8903-1b531ac5d9f9}</Project>
       <Name>BepInEx.Preloader.Core</Name>
     </ProjectReference>
-    <ProjectReference Include="..\submodules\BepInEx.Harmony\BepInEx.Harmony\BepInEx.Harmony.csproj">
-      <Project>{54161cfe-ff42-4dde-b161-3a49545db5cd}</Project>
-      <Name>BepInEx.Harmony</Name>
-    </ProjectReference>
   </ItemGroup>
-  <ItemGroup />
+  <ItemGroup>
+    <PackageReference Include="HarmonyX">
+      <Version>2.1.0-beta</Version>
+    </PackageReference>
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>

+ 1 - 2
BepInEx.NetLauncher/RuntimeFixes/AssemblyFix.cs

@@ -1,5 +1,4 @@
 using System.Reflection;
-using BepInEx.Harmony;
 using HarmonyLib;
 
 namespace BepInEx.NetLauncher.RuntimeFixes
@@ -11,7 +10,7 @@ namespace BepInEx.NetLauncher.RuntimeFixes
 		public static void Execute(Assembly entryAssembly)
 		{
 			EntryAssembly = entryAssembly;
-			HarmonyWrapper.PatchAll(typeof(AssemblyFix), "bepinex.assemblyfix");
+			Harmony.CreateAndPatchAll(typeof(AssemblyFix), "bepinex.assemblyfix");
 		}
 
 		[HarmonyPrefix, HarmonyPatch(typeof(Assembly), nameof(Assembly.GetEntryAssembly))]

+ 0 - 7
BepInEx.NetLauncher/packages.config

@@ -1,7 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<packages>
-  <package id="HarmonyX" version="2.0.6" targetFramework="net452" />
-  <package id="Mono.Cecil" version="0.11.2" targetFramework="net452" />
-  <package id="MonoMod.RuntimeDetour" version="20.5.21.5" targetFramework="net452" />
-  <package id="MonoMod.Utils" version="20.5.21.5" targetFramework="net452" />
-</packages>

+ 1 - 5
BepInEx.Preloader.Core/BepInEx.Preloader.Core.csproj

@@ -49,14 +49,10 @@
       <Project>{4ffba620-f5ed-47f9-b90c-dad1316fd9b9}</Project>
       <Name>BepInEx.Core</Name>
     </ProjectReference>
-    <ProjectReference Include="..\submodules\BepInEx.Harmony\BepInEx.Harmony\BepInEx.Harmony.csproj">
-      <Project>{54161cfe-ff42-4dde-b161-3a49545db5cd}</Project>
-      <Name>BepInEx.Harmony</Name>
-    </ProjectReference>
   </ItemGroup>
   <ItemGroup>
     <PackageReference Include="HarmonyX">
-      <Version>2.0.6</Version>
+      <Version>2.1.0-beta</Version>
     </PackageReference>
     <PackageReference Include="Mono.Cecil">
       <Version>0.10.4</Version>

+ 5 - 28
BepInEx.Preloader.Unity/BepInEx.Preloader.Unity.csproj

@@ -32,27 +32,6 @@
     <DocumentationFile>..\bin\BepInEx.Preloader.Unity.xml</DocumentationFile>
   </PropertyGroup>
   <ItemGroup>
-    <Reference Include="0Harmony, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
-      <HintPath>..\packages\HarmonyX.2.0.6\lib\net35\0Harmony.dll</HintPath>
-    </Reference>
-    <Reference Include="Mono.Cecil, Version=0.10.4.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
-      <HintPath>..\packages\Mono.Cecil.0.10.4\lib\net35\Mono.Cecil.dll</HintPath>
-    </Reference>
-    <Reference Include="Mono.Cecil.Mdb, Version=0.10.4.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
-      <HintPath>..\packages\Mono.Cecil.0.10.4\lib\net35\Mono.Cecil.Mdb.dll</HintPath>
-    </Reference>
-    <Reference Include="Mono.Cecil.Pdb, Version=0.10.4.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
-      <HintPath>..\packages\Mono.Cecil.0.10.4\lib\net35\Mono.Cecil.Pdb.dll</HintPath>
-    </Reference>
-    <Reference Include="Mono.Cecil.Rocks, Version=0.10.4.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
-      <HintPath>..\packages\Mono.Cecil.0.10.4\lib\net35\Mono.Cecil.Rocks.dll</HintPath>
-    </Reference>
-    <Reference Include="MonoMod.RuntimeDetour, Version=20.5.21.5, Culture=neutral, processorArchitecture=MSIL">
-      <HintPath>..\packages\MonoMod.RuntimeDetour.20.5.21.5\lib\net35\MonoMod.RuntimeDetour.dll</HintPath>
-    </Reference>
-    <Reference Include="MonoMod.Utils, Version=20.5.21.5, Culture=neutral, processorArchitecture=MSIL">
-      <HintPath>..\packages\MonoMod.Utils.20.5.21.5\lib\net35\MonoMod.Utils.dll</HintPath>
-    </Reference>
     <Reference Include="System" />
     <Reference Include="System.Core" />
   </ItemGroup>
@@ -65,9 +44,6 @@
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
   <ItemGroup>
-    <None Include="packages.config" />
-  </ItemGroup>
-  <ItemGroup>
     <ProjectReference Include="..\BepInEx.Core\BepInEx.Core.csproj">
       <Project>{4ffba620-f5ed-47f9-b90c-dad1316fd9b9}</Project>
       <Name>BepInEx.Core</Name>
@@ -76,10 +52,11 @@
       <Project>{15f8bc38-a761-4f93-8903-1b531ac5d9f9}</Project>
       <Name>BepInEx.Preloader.Core</Name>
     </ProjectReference>
-    <ProjectReference Include="..\submodules\BepInEx.Harmony\BepInEx.Harmony\BepInEx.Harmony.csproj">
-      <Project>{54161cfe-ff42-4dde-b161-3a49545db5cd}</Project>
-      <Name>BepInEx.Harmony</Name>
-    </ProjectReference>
+  </ItemGroup>
+  <ItemGroup>
+    <PackageReference Include="HarmonyX">
+      <Version>2.1.0-beta</Version>
+    </PackageReference>
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>

+ 2 - 3
BepInEx.Preloader.Unity/RuntimeFixes/UnityPatches.cs

@@ -1,7 +1,6 @@
 using System;
 using System.Collections.Generic;
 using System.Reflection;
-using BepInEx.Harmony;
 using HarmonyLib;
 
 namespace BepInEx.Preloader.RuntimeFixes
@@ -14,8 +13,8 @@ namespace BepInEx.Preloader.RuntimeFixes
 
 		public static void Apply()
 		{
-			HarmonyInstance = HarmonyWrapper.PatchAll(typeof(UnityPatches));
-
+			HarmonyInstance = Harmony.CreateAndPatchAll(typeof(UnityPatches));
+			
 			try
 			{
 				TraceFix.ApplyFix();

+ 0 - 7
BepInEx.Preloader.Unity/packages.config

@@ -1,7 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<packages>
-  <package id="HarmonyX" version="2.0.6" targetFramework="net35" />
-  <package id="Mono.Cecil" version="0.10.4" targetFramework="net35" />
-  <package id="MonoMod.RuntimeDetour" version="20.5.21.5" targetFramework="net35" />
-  <package id="MonoMod.Utils" version="20.5.21.5" targetFramework="net35" />
-</packages>

+ 3 - 13
BepInEx.Unity/BepInEx.Unity.csproj

@@ -32,18 +32,6 @@
     <DocumentationFile>..\bin\BepInEx.Unity.xml</DocumentationFile>
   </PropertyGroup>
   <ItemGroup>
-    <Reference Include="0Harmony, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
-      <HintPath>..\packages\HarmonyX.2.0.6\lib\net35\0Harmony.dll</HintPath>
-    </Reference>
-    <Reference Include="Mono.Cecil, Version=0.10.4.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
-      <HintPath>..\packages\Mono.Cecil.0.10.4\lib\net35\Mono.Cecil.dll</HintPath>
-    </Reference>
-    <Reference Include="MonoMod.RuntimeDetour, Version=20.5.21.5, Culture=neutral, processorArchitecture=MSIL">
-      <HintPath>..\packages\MonoMod.RuntimeDetour.20.5.21.5\lib\net35\MonoMod.RuntimeDetour.dll</HintPath>
-    </Reference>
-    <Reference Include="MonoMod.Utils, Version=20.5.21.5, Culture=neutral, processorArchitecture=MSIL">
-      <HintPath>..\packages\MonoMod.Utils.20.5.21.5\lib\net35\MonoMod.Utils.dll</HintPath>
-    </Reference>
     <Reference Include="System" />
     <Reference Include="System.Core" />
     <Reference Include="UnityEngine">
@@ -77,7 +65,9 @@
     </ProjectReference>
   </ItemGroup>
   <ItemGroup>
-    <None Include="packages.config" />
+    <PackageReference Include="HarmonyX">
+      <Version>2.1.0-beta</Version>
+    </PackageReference>
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>

+ 0 - 7
BepInEx.Unity/packages.config

@@ -1,7 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<packages>
-  <package id="HarmonyX" version="2.0.6" targetFramework="net35" />
-  <package id="Mono.Cecil" version="0.10.4" targetFramework="net35" />
-  <package id="MonoMod.RuntimeDetour" version="20.5.21.5" targetFramework="net35" />
-  <package id="MonoMod.Utils" version="20.5.21.5" targetFramework="net35" />
-</packages>

+ 0 - 13
BepInEx.sln

@@ -11,10 +11,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BepInEx.Patcher", "BepInEx.
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BepInEx.Bootstrap", "BepInEx.Bootstrap\BepInEx.Bootstrap.csproj", "{6E6BC1E5-5BE8-4566-B3AE-52C4CB218AEB}"
 EndProject
-Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Submodules", "Submodules", "{BAC58F7E-AAD8-4D0C-9490-9765ACBBA6FB}"
-EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BepInEx.Harmony", "submodules\BepInEx.Harmony\BepInEx.Harmony\BepInEx.Harmony.csproj", "{54161CFE-FF42-4DDE-B161-3A49545DB5CD}"
-EndProject
 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Patcher", "Patcher", "{A9071994-3533-4C1B-89DC-D817B676AB41}"
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BepInExTests", "BepInExTests\BepInExTests.csproj", "{E7CD429A-D057-48E3-8C51-E5C934E8E07B}"
@@ -60,14 +56,6 @@ Global
 		{6E6BC1E5-5BE8-4566-B3AE-52C4CB218AEB}.Release_Unity|Any CPU.ActiveCfg = Release|Any CPU
 		{6E6BC1E5-5BE8-4566-B3AE-52C4CB218AEB}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{6E6BC1E5-5BE8-4566-B3AE-52C4CB218AEB}.Release|Any CPU.Build.0 = Release|Any CPU
-		{54161CFE-FF42-4DDE-B161-3A49545DB5CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
-		{54161CFE-FF42-4DDE-B161-3A49545DB5CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
-		{54161CFE-FF42-4DDE-B161-3A49545DB5CD}.Release_NetFramework|Any CPU.ActiveCfg = Release|Any CPU
-		{54161CFE-FF42-4DDE-B161-3A49545DB5CD}.Release_NetFramework|Any CPU.Build.0 = Release|Any CPU
-		{54161CFE-FF42-4DDE-B161-3A49545DB5CD}.Release_Unity|Any CPU.ActiveCfg = Release|Any CPU
-		{54161CFE-FF42-4DDE-B161-3A49545DB5CD}.Release_Unity|Any CPU.Build.0 = Release|Any CPU
-		{54161CFE-FF42-4DDE-B161-3A49545DB5CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{54161CFE-FF42-4DDE-B161-3A49545DB5CD}.Release|Any CPU.Build.0 = Release|Any CPU
 		{E7CD429A-D057-48E3-8C51-E5C934E8E07B}.Debug|Any CPU.ActiveCfg = Debug|x86
 		{E7CD429A-D057-48E3-8C51-E5C934E8E07B}.Debug|Any CPU.Build.0 = Debug|x86
 		{E7CD429A-D057-48E3-8C51-E5C934E8E07B}.Release_NetFramework|Any CPU.ActiveCfg = Release|x86
@@ -125,7 +113,6 @@ Global
 	GlobalSection(NestedProjects) = preSolution
 		{DC89F18B-235B-4C01-AB31-AF40DCE5C4C7} = {A9071994-3533-4C1B-89DC-D817B676AB41}
 		{6E6BC1E5-5BE8-4566-B3AE-52C4CB218AEB} = {A9071994-3533-4C1B-89DC-D817B676AB41}
-		{54161CFE-FF42-4DDE-B161-3A49545DB5CD} = {BAC58F7E-AAD8-4D0C-9490-9765ACBBA6FB}
 		{EAE9FAE6-8011-45A3-8B6E-0C7F14210533} = {6E2DD21E-0854-4F4A-B925-D90E0016D03D}
 		{D404C973-441D-48ED-B266-C21320BA0D87} = {6E2DD21E-0854-4F4A-B925-D90E0016D03D}
 		{490B052B-7F23-4052-AD04-613856618901} = {6EC98884-A3DC-4828-85EF-5F10F7519429}

+ 0 - 1
submodules/BepInEx.Harmony

@@ -1 +0,0 @@
-Subproject commit dae82954004248028d38a43e72b42037efca0a80