Entrypoint.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. namespace BepInEx.Preloader
  6. {
  7. internal static class PreloaderRunner
  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(args[0], bepinPath, EnvVars.DOORSTOP_MANAGED_FOLDER_DIR);
  13. AppDomain.CurrentDomain.AssemblyResolve += LocalResolve;
  14. Preloader.Run();
  15. }
  16. private static Assembly LocalResolve(object sender, ResolveEventArgs args)
  17. {
  18. var assemblyName = new AssemblyName(args.Name);
  19. var foundAssembly = AppDomain.CurrentDomain.GetAssemblies()
  20. .FirstOrDefault(x => x.GetName().Name == assemblyName.Name);
  21. if (foundAssembly != null)
  22. return foundAssembly;
  23. if (Utility.TryResolveDllAssembly(assemblyName, Paths.BepInExAssemblyDirectory, out foundAssembly)
  24. || Utility.TryResolveDllAssembly(assemblyName, Paths.PatcherPluginPath, out foundAssembly)
  25. || Utility.TryResolveDllAssembly(assemblyName, Paths.PluginPath, out foundAssembly))
  26. return foundAssembly;
  27. return null;
  28. }
  29. }
  30. internal static class Entrypoint
  31. {
  32. private static string preloaderPath;
  33. /// <summary>
  34. /// The main entrypoint of BepInEx, called from Doorstop.
  35. /// </summary>
  36. /// <param name="args">
  37. /// The arguments passed in from Doorstop. First argument is the path of the currently executing
  38. /// process.
  39. /// </param>
  40. public static void Main(string[] args)
  41. {
  42. EnvVars.LoadVars();
  43. // 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
  44. preloaderPath = Path.GetDirectoryName(Path.GetFullPath(EnvVars.DOORSTOP_INVOKE_DLL_PATH));
  45. AppDomain.CurrentDomain.AssemblyResolve += ResolveCurrentDirectory;
  46. // In some versions of Unity 4, Mono tries to resolve BepInEx.dll prematurely because of the call to Paths.SetExecutablePath
  47. // 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
  48. typeof(Entrypoint).Assembly.GetType($"BepInEx.Preloader.{nameof(PreloaderRunner)}")
  49. ?.GetMethod(nameof(PreloaderRunner.PreloaderMain))
  50. ?.Invoke(null, new object[] { args });
  51. AppDomain.CurrentDomain.AssemblyResolve -= ResolveCurrentDirectory;
  52. }
  53. private static Assembly ResolveCurrentDirectory(object sender, ResolveEventArgs args)
  54. {
  55. var name = new AssemblyName(args.Name);
  56. try
  57. {
  58. return Assembly.LoadFile(Path.Combine(preloaderPath, $"{name.Name}.dll"));
  59. }
  60. catch (Exception)
  61. {
  62. return null;
  63. }
  64. }
  65. }
  66. }