Entrypoint.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. namespace BepInEx.Preloader
  5. {
  6. internal 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.SetExecutablePath(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()
  35. .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. }