Files
BepInEx/BepInEx.IL2CPP/DoorstopEntrypoint.cs

117 lines
4.1 KiB
C#

using System;
using System.IO;
using System.Linq;
using System.Reflection;
using BepInEx.IL2CPP;
using BepInEx.Preloader.Core;
namespace BepInEx.IL2CPP
{
internal static class UnityPreloaderRunner
{
internal static string preloaderPath;
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);
// Cecil 0.11 requires one to manually set up list of trusted assemblies for assembly resolving
AppDomain.CurrentDomain.AddCecilPlatformAssemblies(Paths.ManagedPath);
Preloader.IL2CPPUnhollowedPath = Path.Combine(Paths.BepInExRootPath, "unhollowed");
AppDomain.CurrentDomain.AssemblyResolve += LocalResolve;
AppDomain.CurrentDomain.AssemblyResolve -= ResolveCurrentDirectory;
Preloader.Run();
}
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;
}
public static Assembly ResolveCurrentDirectory(object sender, ResolveEventArgs args)
{
var name = new AssemblyName(args.Name);
try
{
return Assembly.LoadFile(Path.Combine(preloaderPath, $"{name.Name}.dll"));
}
catch (Exception)
{
return null;
}
}
}
}
namespace Doorstop
{
internal static class Entrypoint
{
/// <summary>
/// The main entrypoint of BepInEx, called from Doorstop.
/// </summary>
/// <param name="args">
/// The arguments passed in from Doorstop. First argument is the path of the currently executing
/// process.
/// </param>
public static void Start()
{
// 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";
try
{
EnvVars.LoadVars();
silentExceptionLog =
Path.Combine(Path.GetDirectoryName(EnvVars.DOORSTOP_PROCESS_PATH), silentExceptionLog);
// Get the path of this DLL via Doorstop env var because Assembly.Location mangles non-ASCII characters on some versions of Mono for unknown reasons
UnityPreloaderRunner.preloaderPath =
Path.GetDirectoryName(Path.GetFullPath(EnvVars.DOORSTOP_INVOKE_DLL_PATH));
AppDomain.CurrentDomain.AssemblyResolve += UnityPreloaderRunner.ResolveCurrentDirectory;
UnityPreloaderRunner.PreloaderMain();
}
catch (Exception ex)
{
File.WriteAllText(silentExceptionLog, ex.ToString());
}
}
}
}