DoorstopEntrypoint.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using BepInEx.Preloader.Core;
  6. namespace BepInEx.IL2CPP
  7. {
  8. internal static class UnityPreloaderRunner
  9. {
  10. public static void PreloaderMain(string[] args)
  11. {
  12. string bepinPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetFullPath(EnvVars.DOORSTOP_INVOKE_DLL_PATH)));
  13. Paths.SetExecutablePath(EnvVars.DOORSTOP_PROCESS_PATH, bepinPath);
  14. Preloader.IL2CPPUnhollowedPath = Path.Combine(Paths.BepInExRootPath, "unhollowed");
  15. AppDomain.CurrentDomain.AssemblyResolve += LocalResolve;
  16. AppDomain.CurrentDomain.AssemblyResolve -= DoorstopEntrypoint.ResolveCurrentDirectory;
  17. PlatformUtils.SetPlatform();
  18. Preloader.Run();
  19. }
  20. internal static Assembly LocalResolve(object sender, ResolveEventArgs args)
  21. {
  22. var assemblyName = new AssemblyName(args.Name);
  23. var foundAssembly = AppDomain.CurrentDomain.GetAssemblies()
  24. .FirstOrDefault(x => x.GetName().Name == assemblyName.Name);
  25. if (foundAssembly != null)
  26. return foundAssembly;
  27. if (Utility.TryResolveDllAssembly(assemblyName, Paths.BepInExAssemblyDirectory, out foundAssembly)
  28. || Utility.TryResolveDllAssembly(assemblyName, Paths.PatcherPluginPath, out foundAssembly)
  29. || Utility.TryResolveDllAssembly(assemblyName, Paths.PluginPath, out foundAssembly)
  30. || Utility.TryResolveDllAssembly(assemblyName, Preloader.IL2CPPUnhollowedPath, out foundAssembly))
  31. return foundAssembly;
  32. return null;
  33. }
  34. }
  35. internal static class DoorstopEntrypoint
  36. {
  37. private static string preloaderPath;
  38. /// <summary>
  39. /// The main entrypoint of BepInEx, called from Doorstop.
  40. /// </summary>
  41. /// <param name="args">
  42. /// The arguments passed in from Doorstop. First argument is the path of the currently executing
  43. /// process.
  44. /// </param>
  45. public static void Main(string[] args)
  46. {
  47. // We set it to the current directory first as a fallback, but try to use the same location as the .exe file.
  48. string silentExceptionLog = $"preloader_{DateTime.Now:yyyyMMdd_HHmmss_fff}.log";
  49. try
  50. {
  51. EnvVars.LoadVars();
  52. silentExceptionLog = Path.Combine(Path.GetDirectoryName(EnvVars.DOORSTOP_PROCESS_PATH), silentExceptionLog);
  53. // 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
  54. preloaderPath = Path.GetDirectoryName(Path.GetFullPath(EnvVars.DOORSTOP_INVOKE_DLL_PATH));
  55. AppDomain.CurrentDomain.AssemblyResolve += ResolveCurrentDirectory;
  56. UnityPreloaderRunner.PreloaderMain(args);
  57. }
  58. catch (Exception ex)
  59. {
  60. File.WriteAllText(silentExceptionLog, ex.ToString());
  61. }
  62. }
  63. public static Assembly ResolveCurrentDirectory(object sender, ResolveEventArgs args)
  64. {
  65. var name = new AssemblyName(args.Name);
  66. try
  67. {
  68. return Assembly.LoadFile(Path.Combine(preloaderPath, $"{name.Name}.dll"));
  69. }
  70. catch (Exception)
  71. {
  72. return null;
  73. }
  74. }
  75. }
  76. }