From e21f800775e13f4aa712e7f16a6d43c6ff90bee5 Mon Sep 17 00:00:00 2001 From: ghorsington Date: Sat, 13 Nov 2021 22:25:24 +0200 Subject: [PATCH] Il2Cpp: Add support for .NET 6 base class library --- .../Console/Windows/WindowsConsoleDriver.cs | 31 ++++++++++- BepInEx.IL2CPP/BepInEx.IL2CPP.csproj | 1 - BepInEx.IL2CPP/Hook/FastNativeDetour.cs | 6 +- BepInEx.IL2CPP/IL2CPPChainloader.cs | 25 +-------- BepInEx.IL2CPP/MonoExtensions.cs | 22 -------- BepInEx.IL2CPP/ProxyAssemblyGenerator.cs | 55 ++----------------- 6 files changed, 37 insertions(+), 103 deletions(-) delete mode 100644 BepInEx.IL2CPP/MonoExtensions.cs diff --git a/BepInEx.Core/Console/Windows/WindowsConsoleDriver.cs b/BepInEx.Core/Console/Windows/WindowsConsoleDriver.cs index d9c9d29..cfa7d4c 100644 --- a/BepInEx.Core/Console/Windows/WindowsConsoleDriver.cs +++ b/BepInEx.Core/Console/Windows/WindowsConsoleDriver.cs @@ -30,8 +30,35 @@ namespace BepInEx .PropertyGetter(typeof(Console), nameof(Console.WindowWidth)) ?.CreateDelegate>(); - private int ConsoleWidth => getWindowWidth?.Invoke() ?? 0; - private int ConsoleHeight => getWindowHeight?.Invoke() ?? 0; + private int ConsoleWidth + { + get + { + try + { + return getWindowWidth?.Invoke() ?? 0; + } + catch (IOException) + { + return 0; + } + } + } + + private int ConsoleHeight + { + get + { + try + { + return getWindowHeight?.Invoke() ?? 0; + } + catch (IOException) + { + return 0; + } + } + } public TextWriter StandardOut { get; private set; } public TextWriter ConsoleOut { get; private set; } diff --git a/BepInEx.IL2CPP/BepInEx.IL2CPP.csproj b/BepInEx.IL2CPP/BepInEx.IL2CPP.csproj index 245f91f..5d34fc0 100644 --- a/BepInEx.IL2CPP/BepInEx.IL2CPP.csproj +++ b/BepInEx.IL2CPP/BepInEx.IL2CPP.csproj @@ -24,7 +24,6 @@ - diff --git a/BepInEx.IL2CPP/Hook/FastNativeDetour.cs b/BepInEx.IL2CPP/Hook/FastNativeDetour.cs index d705407..01d395f 100644 --- a/BepInEx.IL2CPP/Hook/FastNativeDetour.cs +++ b/BepInEx.IL2CPP/Hook/FastNativeDetour.cs @@ -143,7 +143,7 @@ namespace BepInEx.IL2CPP.Hook var trampolineAlloc = PageAllocator.Instance.Allocate(OriginalFunctionPtr); - logger.LogDebug($"Original: {OriginalFunctionPtr.ToInt64():X}, Trampoline: {(long) trampolineAlloc:X}, diff: {Math.Abs(OriginalFunctionPtr.ToInt64() - trampolineAlloc):X}; is within +-1GB range: {PageAllocator.IsInRelJmpRange(OriginalFunctionPtr, trampolineAlloc)}"); + logger.LogDebug($"Original: {OriginalFunctionPtr.ToInt64():X}, Trampoline: {(long)trampolineAlloc:X}, diff: {Math.Abs(OriginalFunctionPtr.ToInt64() - trampolineAlloc):X}; is within +-1GB range: {PageAllocator.IsInRelJmpRange(OriginalFunctionPtr, trampolineAlloc)}"); DetourHelper.Native.MakeWritable(trampolineAlloc, PageAllocator.PAGE_SIZE); @@ -165,9 +165,7 @@ namespace BepInEx.IL2CPP.Hook out T original, CallingConvention? callingConvention = null) where T : Delegate { - var toPtr = callingConvention != null - ? MonoExtensions.GetFunctionPointerForDelegate(to, callingConvention.Value) - : Marshal.GetFunctionPointerForDelegate(to); + var toPtr = Marshal.GetFunctionPointerForDelegate(to); var result = new FastNativeDetour(from, toPtr); original = result.GenerateTrampoline(); result.Apply(); diff --git a/BepInEx.IL2CPP/IL2CPPChainloader.cs b/BepInEx.IL2CPP/IL2CPPChainloader.cs index b5d5276..7b71b98 100644 --- a/BepInEx.IL2CPP/IL2CPPChainloader.cs +++ b/BepInEx.IL2CPP/IL2CPPChainloader.cs @@ -12,7 +12,6 @@ using BepInEx.Logging; using BepInEx.Preloader.Core; using BepInEx.Preloader.Core.Logging; using HarmonyLib.Public.Patching; -using Il2Cpp.TlsAdapter; using MonoMod.Utils; using UnhollowerBaseLib; using UnhollowerRuntimeLib; @@ -24,7 +23,6 @@ namespace BepInEx.IL2CPP public class IL2CPPChainloader : BaseChainloader { private static RuntimeInvokeDetourDelegate originalInvoke; - private static InstallUnityTlsInterfaceDelegate originalInstallUnityTlsInterface; private static readonly ConfigEntry ConfigUnityLogging = ConfigFile.CoreConfig.Bind( "Logging", "UnityLogListening", @@ -37,7 +35,6 @@ namespace BepInEx.IL2CPP "Include unity log messages in log file output."); private static FastNativeDetour RuntimeInvokeDetour { get; set; } - private static FastNativeDetour InstallUnityTlsInterfaceDetour { get; set; } public static IL2CPPChainloader Instance { get; set; } @@ -83,25 +80,10 @@ namespace BepInEx.IL2CPP FastNativeDetour.CreateAndApply(runtimeInvokePtr, OnInvokeMethod, out originalInvoke, CallingConvention.Cdecl); - if (gameAssemblyModule.BaseAddress.TryGetFunction("il2cpp_unity_install_unitytls_interface", - out var installTlsPtr)) - InstallUnityTlsInterfaceDetour = - FastNativeDetour.CreateAndApply(installTlsPtr, OnInstallUnityTlsInterface, - out originalInstallUnityTlsInterface, CallingConvention.Cdecl); - - Logger.LogDebug("Initializing TLS adapters"); - Il2CppTlsAdapter.Initialize(); - PreloaderLogger.Log.LogDebug("Runtime invoke patched"); - } - private void OnInstallUnityTlsInterface(IntPtr unityTlsInterfaceStruct) - { - Logger.LogDebug($"Captured UnityTls interface at {unityTlsInterfaceStruct.ToInt64():x8}"); - Il2CppTlsAdapter.Options.UnityTlsInterface = unityTlsInterfaceStruct; - originalInstallUnityTlsInterface(unityTlsInterfaceStruct); - InstallUnityTlsInterfaceDetour.Dispose(); - InstallUnityTlsInterfaceDetour = null; + Logger.LogInfo($"Runtime version: {Environment.Version}"); + Logger.LogInfo($"Runtime information: {RuntimeInformation.FrameworkDescription}"); } private static IntPtr OnInvokeMethod(IntPtr method, IntPtr obj, IntPtr parameters, IntPtr exc) @@ -167,8 +149,5 @@ namespace BepInEx.IL2CPP [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate IntPtr RuntimeInvokeDetourDelegate(IntPtr method, IntPtr obj, IntPtr parameters, IntPtr exc); - - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - private delegate void InstallUnityTlsInterfaceDelegate(IntPtr unityTlsInterfaceStruct); } } diff --git a/BepInEx.IL2CPP/MonoExtensions.cs b/BepInEx.IL2CPP/MonoExtensions.cs deleted file mode 100644 index 599ff98..0000000 --- a/BepInEx.IL2CPP/MonoExtensions.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -namespace BepInEx -{ - /// - /// Contains unofficial extensions to the underlying Mono runtime. - /// - public static class MonoExtensions - { - [MethodImpl(MethodImplOptions.InternalCall)] - private static extern IntPtr GetFunctionPointerForDelegateInternal2(Delegate d, CallingConvention conv); - - public static IntPtr GetFunctionPointerForDelegate(Delegate d, CallingConvention conv) - { - if (d == null) throw new ArgumentNullException(nameof(d)); - - return GetFunctionPointerForDelegateInternal2(d, conv); - } - } -} diff --git a/BepInEx.IL2CPP/ProxyAssemblyGenerator.cs b/BepInEx.IL2CPP/ProxyAssemblyGenerator.cs index eac8770..04c789c 100644 --- a/BepInEx.IL2CPP/ProxyAssemblyGenerator.cs +++ b/BepInEx.IL2CPP/ProxyAssemblyGenerator.cs @@ -115,23 +115,13 @@ namespace BepInEx.IL2CPP ? "Il2CppDumper" : "Cpp2IL"); - var domain = AppDomainHelper.CreateDomain("GeneratorDomain", new AppDomainHelper.AppDomainSetup - { - ApplicationBase = Paths.BepInExAssemblyDirectory - }); - - var runner = - (AppDomainRunner)AppDomainHelper.CreateInstanceAndUnwrap(domain, - typeof(AppDomainRunner).Assembly.FullName, - typeof(AppDomainRunner).FullName); + var runner = new AppDomainRunner(); runner.Setup(Paths.ExecutablePath, Preloader.IL2CPPUnhollowedPath, Paths.BepInExRootPath, Paths.ManagedPath); runner.GenerateAssembliesInternal(new AppDomainListener(), Preloader.UnityVersion.ToString(3), ConfigIl2CppDumperType.Value); - AppDomain.Unload(domain); - File.WriteAllText(HashPath, ComputeHash()); } @@ -141,43 +131,7 @@ namespace BepInEx.IL2CPP Cpp2IL } - private static class AppDomainHelper - { - static AppDomainHelper() - { - var appDomain = typeof(AppDomain); - var evidenceType = typeof(AppDomain).Assembly.GetType("System.Security.Policy.Evidence"); - AppDomainSetupType = typeof(AppDomain).Assembly.GetType("System.AppDomainSetup"); - CreateDomainInternal = - MethodInvoker.GetHandler(AccessTools.Method(appDomain, "CreateDomain", - new[] - { - typeof(string), evidenceType, AppDomainSetupType - })); - CreateInstanceAndUnwrap = - AccessTools.MethodDelegate>(AccessTools.Method(appDomain, - nameof(CreateInstanceAndUnwrap), new[] { typeof(string), typeof(string) })); - } - - public static Func CreateInstanceAndUnwrap { get; } - private static FastInvokeHandler CreateDomainInternal { get; } - private static Type AppDomainSetupType { get; } - - public static AppDomain CreateDomain(string name, AppDomainSetup setup) - { - var realSetup = AccessTools.CreateInstance(AppDomainSetupType); - Traverse.IterateProperties(setup, realSetup, (pSrc, pTgt) => pTgt.SetValue(pSrc.GetValue())); - return CreateDomainInternal(null, name, null, realSetup) as AppDomain; - } - - public class AppDomainSetup - { - public string ApplicationBase { get; set; } - } - } - - [Serializable] - private class AppDomainListener : MarshalByRefObject + private class AppDomainListener { public void DoPreloaderLog(object data, LogLevel level) => Preloader.Log.Log(level, data); @@ -186,8 +140,7 @@ namespace BepInEx.IL2CPP public void DoUnhollowerLog(object data, LogLevel level) => Preloader.UnhollowerLog.Log(level, data); } - [Serializable] - private class AppDomainRunner : MarshalByRefObject + private class AppDomainRunner { public void Setup(string executablePath, string unhollowedPath, string bepinPath, string managedPath) { @@ -291,7 +244,7 @@ namespace BepInEx.IL2CPP var unhollowerOptions = new UnhollowerOptions { GameAssemblyPath = GameAssemblyPath, - MscorlibPath = Path.Combine(Paths.ManagedPath, "mscorlib.dll"), + MscorlibPath = Path.Combine(Paths.BepInExRootPath, "corlib-libs", "mscorlib.dll"), Source = sourceAssemblies, OutputDir = Preloader.IL2CPPUnhollowedPath, UnityBaseLibsDir = Directory.Exists(UnityBaseLibsDirectory) ? UnityBaseLibsDirectory : null,