Il2Cpp: Add support for .NET 6 base class library

This commit is contained in:
ghorsington committed 2021-11-13 22:25:24 +02:00
1 parent ed3c169e48
commit e21f800775
6 files changed
+37 -103

No files matched your search

@@ -30,8 +30,35 @@ namespace BepInEx
.PropertyGetter(typeof(Console), nameof(Console.WindowWidth))
?.CreateDelegate<Func<int>>();
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; }
-1
View File
@@ -24,7 +24,6 @@
<ProjectReference Include="..\BepInEx.Preloader.Core\BepInEx.Preloader.Core.csproj" PrivateAssets="All"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="BepInEx.Il2Cpp.TlsAdapter" Version="1.0.1"/>
<PackageReference Include="HarmonyX" Version="2.5.7"/>
<PackageReference Include="Iced" Version="1.15.0"/>
<PackageReference Include="Il2CppAssemblyUnhollower.BaseLib" Version="0.4.19.5"/>
+2 -4
View File
@@ -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<T>();
result.Apply();
+2 -23
View File
@@ -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<BasePlugin>
{
private static RuntimeInvokeDetourDelegate originalInvoke;
private static InstallUnityTlsInterfaceDelegate originalInstallUnityTlsInterface;
private static readonly ConfigEntry<bool> 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);
}
}
-22
View File
@@ -1,22 +0,0 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace BepInEx
{
/// <summary>
/// Contains unofficial extensions to the underlying Mono runtime.
/// </summary>
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);
}
}
}
+4 -51
View File
@@ -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<Func<AppDomain, string, string, object>>(AccessTools.Method(appDomain,
nameof(CreateInstanceAndUnwrap), new[] { typeof(string), typeof(string) }));
}
public static Func<AppDomain, string, string, object> 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,