Entrypoint.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. namespace BepInEx.Preloader
  6. {
  7. internal static class Entrypoint
  8. {
  9. /// <summary>
  10. /// The main entrypoint of BepInEx, called from Doorstop.
  11. /// </summary>
  12. /// <param name="args">
  13. /// The arguments passed in from Doorstop. First argument is the path of the currently executing
  14. /// process.
  15. /// </param>
  16. public static void Main(string[] args)
  17. {
  18. EnvVars.LoadVars();
  19. string bepinPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetFullPath(EnvVars.DOORSTOP_INVOKE_DLL_PATH)));
  20. Paths.SetExecutablePath(args[0], bepinPath, EnvVars.DOORSTOP_MANAGED_FOLDER_DIR);
  21. AppDomain.CurrentDomain.AssemblyResolve += LocalResolve;
  22. Preloader.Run();
  23. }
  24. /// <summary>
  25. /// A handler for <see cref="AppDomain.AssemblyResolve" /> to perform some special handling.
  26. /// <para>
  27. /// It attempts to check currently loaded assemblies (ignoring the version), and then checks the BepInEx/core path,
  28. /// BepInEx/patchers path and the BepInEx folder, all in that order.
  29. /// </para>
  30. /// </summary>
  31. /// <param name="sender"></param>
  32. /// <param name="args"></param>
  33. /// <returns></returns>
  34. internal static Assembly LocalResolve(object sender, ResolveEventArgs args)
  35. {
  36. var assemblyName = new AssemblyName(args.Name);
  37. var foundAssembly = AppDomain.CurrentDomain.GetAssemblies()
  38. .FirstOrDefault(x => x.GetName().Name == assemblyName.Name);
  39. if (foundAssembly != null)
  40. return foundAssembly;
  41. if (Utility.TryResolveDllAssembly(assemblyName, Paths.BepInExAssemblyDirectory, out foundAssembly)
  42. || Utility.TryResolveDllAssembly(assemblyName, Paths.PatcherPluginPath, out foundAssembly)
  43. || Utility.TryResolveDllAssembly(assemblyName, Paths.PluginPath, out foundAssembly))
  44. return foundAssembly;
  45. return null;
  46. }
  47. }
  48. }