소스 검색

Add testing code for invoke hook

ghorsington 4 년 전
부모
커밋
e6e192756d
1개의 변경된 파일48개의 추가작업 그리고 15개의 파일을 삭제
  1. 48 15
      BepInEx.IL2CPP/IL2CPPChainloader.cs

+ 48 - 15
BepInEx.IL2CPP/IL2CPPChainloader.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Diagnostics;
+using System.IO;
 using System.Linq;
 using System.Reflection;
 using System.Runtime.InteropServices;
@@ -27,35 +28,67 @@ namespace BepInEx.IL2CPP
 			UnityLogSource.LogInfo(log.Trim());
 		}
 
-		[UnmanagedFunctionPointer(CallingConvention.StdCall)]
+		[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
 		private delegate IntPtr RuntimeInvokeDetour(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;
+		
 		public unsafe IL2CPPChainloader()
 		{
+			UnityVersionHandler.Initialize(2019, 3, 15);
+			File.AppendAllText("log.log", "Initialized unhollower\n");
 			ClassInjector.DoHook = (ptr, intPtr) =>
 			{
 				var detour = new NativeDetour(new IntPtr(*((int**)ptr)), intPtr);
 				detour.Apply();
 			};
 
-			var gameAssemblyModule = Process.GetCurrentProcess().Modules.Cast<ProcessModule>().First(x => x.FileName.Contains("GameAssembly"));
-			var functionPtr = DynDll.GetFunction(gameAssemblyModule.BaseAddress, "il2cpp_runtime_invoke");
-
-			RuntimeInvokeDetour originalInvoke = null;
-			RuntimeInvokeDetour invokeHook = (method, obj, parameters, exc) =>
+			foreach (var processModule in Process.GetCurrentProcess().Modules.Cast<ProcessModule>())
 			{
-				UnityLogSource.LogInfo(Marshal.PtrToStringAnsi(UnhollowerBaseLib.IL2CPP.il2cpp_method_get_name(method)));
-				return originalInvoke(method, obj, parameters, exc);
-			};
-
-			var invokeDetour = new NativeDetour(functionPtr, Marshal.GetFunctionPointerForDelegate(invokeHook), new NativeDetourConfig {ManualApply = true});
-
+				File.AppendAllText("wew.log", $"{processModule.ModuleName}\n");
+			}
+			
+			var gameAssemblyModule = Process.GetCurrentProcess().Modules.Cast<ProcessModule>().First(x => x.ModuleName.Contains("GameAssembly"));
+			File.AppendAllText("wew.log", $"Got module: {gameAssemblyModule.ModuleName}; addr: {gameAssemblyModule.BaseAddress}\n");
+			var functionPtr = GetProcAddress(gameAssemblyModule.BaseAddress, "il2cpp_runtime_invoke"); //DynDll.GetFunction(gameAssemblyModule.BaseAddress, "il2cpp_runtime_invoke");
+
+			File.AppendAllText("wew.log", $"Got fptr: {functionPtr}\n");
+			
+			// RuntimeInvokeDetour invokeHook = (method, obj, parameters, exc) =>
+			// {
+			// 	// UnityLogSource.LogInfo(Marshal.PtrToStringAnsi(UnhollowerBaseLib.IL2CPP.il2cpp_method_get_name(method)));
+			// 	return originalInvoke(method, obj, parameters, exc);
+			// };
+			// UnhollowerBaseLib.IL2CPP.il2cpp_method_get_name(method)
+
+			var invokeDetour = new NativeDetour(functionPtr, Marshal.GetFunctionPointerForDelegate(new RuntimeInvokeDetour(OnInvokeMethod)), new NativeDetourConfig {ManualApply = true});
+
+			File.AppendAllText("log.log", "Got detour\n");
 			originalInvoke = invokeDetour.GenerateTrampoline<RuntimeInvokeDetour>();
-
+			File.AppendAllText("log.log", "Got trampoline\n");
+			
 			invokeDetour.Apply();
+			File.AppendAllText("log.log", "Applied!\n");
+		}
 
-
-			//UnityVersionHandler.Initialize(2019, 3, 15);
+		private static IntPtr OnInvokeMethod(IntPtr method, IntPtr obj, IntPtr parameters, IntPtr exc)
+		{
+			lock (originalInvoke)
+			{
+				try
+				{
+					File.AppendAllText("log.log", $"Got call: {Marshal.PtrToStringAnsi(UnhollowerBaseLib.IL2CPP.il2cpp_method_get_name(method))}\n");
+				}
+				catch (Exception e)
+				{
+					File.AppendAllText("err.log", e.ToString() + "\n");
+				}
+			
+				return originalInvoke(method, obj, parameters, exc);
+			}
 		}
 
 		protected override void InitializeLoggers()