Entrypoint.cs 1.8 KB

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