DoorstopEntrypoint.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. PlatformUtils.SetPlatform();
  14. Paths.SetExecutablePath(EnvVars.DOORSTOP_PROCESS_PATH, bepinPath);
  15. Preloader.IL2CPPUnhollowedPath = Path.Combine(Paths.BepInExRootPath, "unhollowed");
  16. AppDomain.CurrentDomain.AssemblyResolve += LocalResolve;
  17. AppDomain.CurrentDomain.AssemblyResolve -= DoorstopEntrypoint.ResolveCurrentDirectory;
  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. }