DoorstopEntrypoint.cs 2.9 KB

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