Entrypoint.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. Paths.SetExecutablePath(args[0]);
  19. AppDomain.CurrentDomain.AssemblyResolve += LocalResolve;
  20. foreach (var dll in Directory.GetFiles(Paths.BepInExAssemblyDirectory))
  21. Assembly.Load(dll);
  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. }