Entrypoint.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. namespace BepInEx.Preloader
  7. {
  8. internal static class PreloaderRunner
  9. {
  10. public static void PreloaderMain()
  11. {
  12. string bepinPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetFullPath(EnvVars.DOORSTOP_INVOKE_DLL_PATH)));
  13. Paths.SetExecutablePath(EnvVars.DOORSTOP_PROCESS_PATH, bepinPath, EnvVars.DOORSTOP_MANAGED_FOLDER_DIR);
  14. AppDomain.CurrentDomain.AssemblyResolve += LocalResolve;
  15. Preloader.Run();
  16. }
  17. private static Assembly LocalResolve(object sender, ResolveEventArgs args)
  18. {
  19. var assemblyName = new AssemblyName(args.Name);
  20. // Use parse assembly name on managed side because native GetName() can fail on some locales
  21. // if the game path has "exotic" characters
  22. var foundAssembly = AppDomain.CurrentDomain.GetAssemblies()
  23. .FirstOrDefault(x => new AssemblyName(x.FullName).Name == assemblyName.Name);
  24. if (foundAssembly != null)
  25. return foundAssembly;
  26. if (Utility.TryResolveDllAssembly(assemblyName, Paths.BepInExAssemblyDirectory, out foundAssembly)
  27. || Utility.TryResolveDllAssembly(assemblyName, Paths.PatcherPluginPath, out foundAssembly)
  28. || Utility.TryResolveDllAssembly(assemblyName, Paths.PluginPath, out foundAssembly))
  29. return foundAssembly;
  30. return null;
  31. }
  32. }
  33. internal static class Entrypoint
  34. {
  35. private static string preloaderPath;
  36. /// <summary>
  37. /// The main entrypoint of BepInEx, called from Doorstop.
  38. /// </summary>
  39. /// <param name="args">
  40. /// The arguments passed in from Doorstop. First argument is the path of the currently executing
  41. /// process.
  42. /// </param>
  43. public static void Main(string[] args)
  44. {
  45. // We set it to the current directory first as a fallback, but try to use the same location as the .exe file.
  46. string silentExceptionLog = $"preloader_{DateTime.Now:yyyyMMdd_HHmmss_fff}.log";
  47. try
  48. {
  49. EnvVars.LoadVars();
  50. silentExceptionLog = Path.Combine(EnvVars.DOORSTOP_PROCESS_PATH, silentExceptionLog);
  51. // Get the path of this DLL via Doorstop env var because Assembly.Location mangles non-ASCII characters on some versions of Mono for unknown reasons
  52. preloaderPath = Path.GetDirectoryName(Path.GetFullPath(EnvVars.DOORSTOP_INVOKE_DLL_PATH));
  53. AppDomain.CurrentDomain.AssemblyResolve += ResolveCurrentDirectory;
  54. // In some versions of Unity 4, Mono tries to resolve BepInEx.dll prematurely because of the call to Paths.SetExecutablePath
  55. // To prevent that, we have to use reflection and a separate startup class so that we can install required assembly resolvers before the main code
  56. typeof(Entrypoint).Assembly.GetType($"BepInEx.Preloader.{nameof(PreloaderRunner)}")
  57. ?.GetMethod(nameof(PreloaderRunner.PreloaderMain))
  58. ?.Invoke(null, null);
  59. AppDomain.CurrentDomain.AssemblyResolve -= ResolveCurrentDirectory;
  60. }
  61. catch (Exception ex)
  62. {
  63. File.WriteAllText(silentExceptionLog, ex.ToString());
  64. }
  65. }
  66. private static Assembly ResolveCurrentDirectory(object sender, ResolveEventArgs args)
  67. {
  68. var name = new AssemblyName(args.Name);
  69. try
  70. {
  71. return Assembly.LoadFile(Path.Combine(preloaderPath, $"{name.Name}.dll"));
  72. }
  73. catch (Exception)
  74. {
  75. return null;
  76. }
  77. }
  78. }
  79. }