Entrypoint.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using BepInEx.Common;
  5. namespace BepInEx.Bootstrap
  6. {
  7. public 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. Paths.ExecutablePath = args[0];
  19. AppDomain.CurrentDomain.AssemblyResolve += LocalResolve;
  20. Preloader.Run();
  21. }
  22. /// <summary>
  23. /// A handler for <see cref="AppDomain" />.AssemblyResolve to perform some special handling.
  24. /// <para>
  25. /// It attempts to check currently loaded assemblies (ignoring the version), and then checks the BepInEx/core path,
  26. /// BepInEx/patchers path and the BepInEx folder, all in that order.
  27. /// </para>
  28. /// </summary>
  29. /// <param name="sender"></param>
  30. /// <param name="args"></param>
  31. /// <returns></returns>
  32. internal static Assembly LocalResolve(object sender, ResolveEventArgs args)
  33. {
  34. var assemblyName = new AssemblyName(args.Name);
  35. var foundAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => x.GetName().Name == assemblyName.Name);
  36. if (foundAssembly != null)
  37. return foundAssembly;
  38. if (Utility.TryResolveDllAssembly(assemblyName, Paths.BepInExAssemblyDirectory, out foundAssembly)
  39. || Utility.TryResolveDllAssembly(assemblyName, Paths.PatcherPluginPath, out foundAssembly)
  40. || Utility.TryResolveDllAssembly(assemblyName, Paths.PluginPath, out foundAssembly))
  41. return foundAssembly;
  42. return null;
  43. }
  44. }
  45. }