WIP for running on .NET 6

This commit is contained in:
ghorsington committed 2021-12-04 22:52:08 +02:00
1 parent e21f800775
commit f50e7e2555
7 files changed
+29 -21

No files matched your search

+1
View File
@@ -123,6 +123,7 @@ namespace BepInEx
// Apparently some versions of Mono throw a "Encoding name 'xxx' not supported"
// if you use Encoding.GetEncoding
// That's why we use of codepages directly and handle then in console drivers separately
var codepage = ConfigConsoleShiftJis.Value ? SHIFT_JIS_CP : (uint)Encoding.UTF8.CodePage;
Driver.CreateConsole(codepage);
@@ -23,26 +23,23 @@ namespace UnityInjector.ConsoleUtil
[DllImport("kernel32.dll")]
private static extern uint GetACP();
[DllImport("kernel32.dll", SetLastError = true)]
[DllImport("kernel32.dll")]
private static extern int MultiByteToWideChar(uint codePage,
uint dwFlags,
[In] [MarshalAs(UnmanagedType.LPArray)]
byte[] lpMultiByteStr,
[MarshalAs(UnmanagedType.LPArray)] byte[] lpMultiByteStr,
int cbMultiByte,
[Out] [MarshalAs(UnmanagedType.LPWStr)]
char[] lpWideCharStr,
[MarshalAs(UnmanagedType.LPWStr)] char[] lpWideCharStr,
int cchWideChar);
[DllImport("kernel32.dll")]
private static extern IntPtr SetConsoleOutputCP(uint codepage);
[DllImport("kernel32.dll", SetLastError = true)]
[DllImport("kernel32.dll")]
private static extern int WideCharToMultiByte(uint codePage,
uint dwFlags,
[In] [MarshalAs(UnmanagedType.LPWStr)] char[] lpWideCharStr,
[MarshalAs(UnmanagedType.LPWStr)] char[] lpWideCharStr,
int cchWideChar,
[Out] [MarshalAs(UnmanagedType.LPArray)]
byte[] lpMultiByteStr,
[MarshalAs(UnmanagedType.LPArray)] byte[] lpMultiByteStr,
int cbMultiByte,
IntPtr lpDefaultChar,
IntPtr lpUsedDefaultChar);
@@ -89,7 +89,7 @@ namespace BepInEx
// Make sure of ConsoleEncoding helper class because on some Monos
// Encoding.GetEncoding throws NotImplementedException on most codepages
// NOTE: We don't set Console.OutputEncoding because it resets any existing Console.Out writers
ConsoleEncoding.ConsoleCodePage = codepage;
// ConsoleEncoding.ConsoleCodePage = codepage;
// If stdout exists, write to it, otherwise make it the same as console out
// Not sure if this is needed? Does the original Console.Out still work?
@@ -109,7 +109,7 @@ namespace BepInEx
var consoleOutStream = OpenFileStream(ConsoleWindow.ConsoleOutHandle);
// Can't use Console.OutputEncoding because it can be null (i.e. not preference by user)
ConsoleOut = new StreamWriter(consoleOutStream, ConsoleEncoding.OutputEncoding)
ConsoleOut = new StreamWriter(consoleOutStream, Utility.UTF8NoBom)
{
AutoFlush = true
};
+3 -1
View File
@@ -209,8 +209,10 @@ namespace BepInEx
{
assembly = loader(path);
}
catch (Exception)
catch (Exception e)
{
File.AppendAllText("resolve.log", $"Could not resolve: {e}\n");
continue;
}
+13 -4
View File
@@ -8,14 +8,15 @@ namespace BepInEx.IL2CPP
{
internal static class UnityPreloaderRunner
{
public static void PreloaderMain(string[] args)
public static void PreloaderMain()
{
var bepinPath =
Path.GetDirectoryName(Path.GetDirectoryName(Path.GetFullPath(EnvVars.DOORSTOP_INVOKE_DLL_PATH)));
PlatformUtils.SetPlatform();
Paths.SetExecutablePath(EnvVars.DOORSTOP_PROCESS_PATH, bepinPath, EnvVars.DOORSTOP_MANAGED_FOLDER_DIR, EnvVars.DOORSTOP_DLL_SEARCH_DIRS);
Paths.SetExecutablePath(EnvVars.DOORSTOP_PROCESS_PATH, bepinPath, EnvVars.DOORSTOP_MANAGED_FOLDER_DIR,
EnvVars.DOORSTOP_DLL_SEARCH_DIRS);
// Cecil 0.11 requires one to manually set up list of trusted assemblies for assembly resolving
AppDomain.CurrentDomain.AddCecilPlatformAssemblies(Paths.ManagedPath);
@@ -32,18 +33,26 @@ namespace BepInEx.IL2CPP
internal static Assembly LocalResolve(object sender, ResolveEventArgs args)
{
var assemblyName = new AssemblyName(args.Name);
File.AppendAllText("resolve.log", $"{args.Name}\n");
var foundAssembly = AppDomain.CurrentDomain.GetAssemblies()
.FirstOrDefault(x => x.GetName().Name == assemblyName.Name);
if (foundAssembly != null)
{
File.AppendAllText("resolve.log", $"Resolved from memory: {foundAssembly}\n");
return foundAssembly;
}
if (Utility.TryResolveDllAssembly(assemblyName, Paths.BepInExAssemblyDirectory, out foundAssembly)
|| Utility.TryResolveDllAssembly(assemblyName, Paths.PatcherPluginPath, out foundAssembly)
|| Utility.TryResolveDllAssembly(assemblyName, Paths.PluginPath, out foundAssembly)
|| Utility.TryResolveDllAssembly(assemblyName, Preloader.IL2CPPUnhollowedPath, out foundAssembly))
{
File.AppendAllText("resolve.log", $"Resolved: {foundAssembly}\n");
return foundAssembly;
}
return null;
}
@@ -60,7 +69,7 @@ namespace BepInEx.IL2CPP
/// The arguments passed in from Doorstop. First argument is the path of the currently executing
/// process.
/// </param>
public static void Main(string[] args)
public static void Main()
{
// We set it to the current directory first as a fallback, but try to use the same location as the .exe file.
var silentExceptionLog = $"preloader_{DateTime.Now:yyyyMMdd_HHmmss_fff}.log";
@@ -77,7 +86,7 @@ namespace BepInEx.IL2CPP
AppDomain.CurrentDomain.AssemblyResolve += ResolveCurrentDirectory;
UnityPreloaderRunner.PreloaderMain(args);
UnityPreloaderRunner.PreloaderMain();
}
catch (Exception ex)
{
+3 -5
View File
@@ -15,8 +15,6 @@ using HarmonyLib.Public.Patching;
using MonoMod.Utils;
using UnhollowerBaseLib;
using UnhollowerRuntimeLib;
using UnityEngine;
using Logger = BepInEx.Logging.Logger;
namespace BepInEx.IL2CPP
{
@@ -97,10 +95,10 @@ namespace BepInEx.IL2CPP
{
if (ConfigUnityLogging.Value)
{
Logger.Sources.Add(new IL2CPPUnityLogSource());
// Logger.Sources.Add(new IL2CPPUnityLogSource());
Application.CallLogCallback("Test call after applying unity logging hook", "", LogType.Assert,
true);
// Application.CallLogCallback("Test call after applying unity logging hook", "", LogType.Assert,
// true);
}
unhook = true;
+1
View File
@@ -147,6 +147,7 @@ namespace BepInEx.IL2CPP
Paths.SetExecutablePath(executablePath, bepinPath, managedPath);
Preloader.IL2CPPUnhollowedPath = unhollowedPath;
AppDomain.CurrentDomain.AddCecilPlatformAssemblies(Paths.ManagedPath);
AppDomain.CurrentDomain.AddCecilPlatformAssemblies(Path.GetDirectoryName(Paths.ManagedPath));
AppDomain.CurrentDomain.AddCecilPlatformAssemblies(UnityBaseLibsDirectory);
}