Entrypoint.cs 1.9 KB

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