Entrypoint.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. namespace BepInEx.Preloader
  7. {
  8. internal static class PreloaderRunner
  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(args[0], bepinPath, EnvVars.DOORSTOP_MANAGED_FOLDER_DIR);
  14. AppDomain.CurrentDomain.AssemblyResolve += LocalResolve;
  15. Preloader.Run();
  16. }
  17. private static Assembly LocalResolve(object sender, ResolveEventArgs args)
  18. {
  19. var assemblyName = new AssemblyName(args.Name);
  20. var foundAssembly = AppDomain.CurrentDomain.GetAssemblies()
  21. .FirstOrDefault(x => x.GetName().Name == assemblyName.Name);
  22. if (foundAssembly != null)
  23. return foundAssembly;
  24. if (Utility.TryResolveDllAssembly(assemblyName, Paths.BepInExAssemblyDirectory, out foundAssembly)
  25. || Utility.TryResolveDllAssembly(assemblyName, Paths.PatcherPluginPath, out foundAssembly)
  26. || Utility.TryResolveDllAssembly(assemblyName, Paths.PluginPath, out foundAssembly))
  27. return foundAssembly;
  28. return null;
  29. }
  30. }
  31. internal static class Entrypoint
  32. {
  33. private static string preloaderPath;
  34. /// <summary>
  35. /// The main entrypoint of BepInEx, called from Doorstop.
  36. /// </summary>
  37. /// <param name="args">
  38. /// The arguments passed in from Doorstop. First argument is the path of the currently executing
  39. /// process.
  40. /// </param>
  41. public static void Main(string[] args)
  42. {
  43. // We set it to the current directory first as a fallback, but try to use the same location as the .exe file.
  44. string silentExceptionLog = $"preloader_{DateTime.Now:yyyyMMdd_HHmmss_fff}.log";
  45. try
  46. {
  47. EnvVars.LoadVars();
  48. silentExceptionLog = Path.Combine(Path.GetDirectoryName(args[0]), silentExceptionLog);
  49. // 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
  50. preloaderPath = Path.GetDirectoryName(Path.GetFullPath(EnvVars.DOORSTOP_INVOKE_DLL_PATH));
  51. AppDomain.CurrentDomain.AssemblyResolve += ResolveCurrentDirectory;
  52. // In some versions of Unity 4, Mono tries to resolve BepInEx.dll prematurely because of the call to Paths.SetExecutablePath
  53. // To prevent that, we have to use reflection and a separate startup class so that we can install required assembly resolvers before the main code
  54. typeof(Entrypoint).Assembly.GetType($"BepInEx.Preloader.{nameof(PreloaderRunner)}")
  55. ?.GetMethod(nameof(PreloaderRunner.PreloaderMain))
  56. ?.Invoke(null, new object[] { args });
  57. AppDomain.CurrentDomain.AssemblyResolve -= ResolveCurrentDirectory;
  58. }
  59. catch (Exception ex)
  60. {
  61. File.WriteAllText(silentExceptionLog, ex.ToString());
  62. }
  63. }
  64. private static Assembly ResolveCurrentDirectory(object sender, ResolveEventArgs args)
  65. {
  66. var name = new AssemblyName(args.Name);
  67. try
  68. {
  69. return Assembly.LoadFile(Path.Combine(preloaderPath, $"{name.Name}.dll"));
  70. }
  71. catch (Exception)
  72. {
  73. return null;
  74. }
  75. }
  76. }
  77. }