Entrypoint.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using BepInEx.Preloader.RuntimeFixes;
  7. namespace BepInEx.Preloader
  8. {
  9. internal static class PreloaderRunner
  10. {
  11. public static void PreloaderPreMain(string[] args)
  12. {
  13. string bepinPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetFullPath(EnvVars.DOORSTOP_INVOKE_DLL_PATH)));
  14. Paths.SetExecutablePath(args[0], bepinPath, EnvVars.DOORSTOP_MANAGED_FOLDER_DIR);
  15. AppDomain.CurrentDomain.AssemblyResolve += LocalResolve;
  16. PreloaderMain();
  17. }
  18. private static void PreloaderMain()
  19. {
  20. if (Preloader.ConfigApplyRuntimePatches.Value)
  21. XTermFix.Apply();
  22. Preloader.Run();
  23. }
  24. private static Assembly LocalResolve(object sender, ResolveEventArgs args)
  25. {
  26. var assemblyName = new AssemblyName(args.Name);
  27. var foundAssembly = AppDomain.CurrentDomain.GetAssemblies()
  28. .FirstOrDefault(x => x.GetName().Name == assemblyName.Name);
  29. if (foundAssembly != null)
  30. return foundAssembly;
  31. if (Utility.TryResolveDllAssembly(assemblyName, Paths.BepInExAssemblyDirectory, out foundAssembly)
  32. || Utility.TryResolveDllAssembly(assemblyName, Paths.PatcherPluginPath, out foundAssembly)
  33. || Utility.TryResolveDllAssembly(assemblyName, Paths.PluginPath, out foundAssembly))
  34. return foundAssembly;
  35. return null;
  36. }
  37. }
  38. internal static class Entrypoint
  39. {
  40. private static string preloaderPath;
  41. /// <summary>
  42. /// The main entrypoint of BepInEx, called from Doorstop.
  43. /// </summary>
  44. /// <param name="args">
  45. /// The arguments passed in from Doorstop. First argument is the path of the currently executing
  46. /// process.
  47. /// </param>
  48. public static void Main(string[] args)
  49. {
  50. // We set it to the current directory first as a fallback, but try to use the same location as the .exe file.
  51. string silentExceptionLog = $"preloader_{DateTime.Now:yyyyMMdd_HHmmss_fff}.log";
  52. try
  53. {
  54. EnvVars.LoadVars();
  55. silentExceptionLog = Path.Combine(Path.GetDirectoryName(args[0]), silentExceptionLog);
  56. // 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
  57. preloaderPath = Path.GetDirectoryName(Path.GetFullPath(EnvVars.DOORSTOP_INVOKE_DLL_PATH));
  58. AppDomain.CurrentDomain.AssemblyResolve += ResolveCurrentDirectory;
  59. // In some versions of Unity 4, Mono tries to resolve BepInEx.dll prematurely because of the call to Paths.SetExecutablePath
  60. // 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
  61. typeof(Entrypoint).Assembly.GetType($"BepInEx.Preloader.{nameof(PreloaderRunner)}")
  62. ?.GetMethod(nameof(PreloaderRunner.PreloaderPreMain))
  63. ?.Invoke(null, new object[] { args });
  64. AppDomain.CurrentDomain.AssemblyResolve -= ResolveCurrentDirectory;
  65. }
  66. catch (Exception ex)
  67. {
  68. File.WriteAllText(silentExceptionLog, ex.ToString());
  69. }
  70. }
  71. private static Assembly ResolveCurrentDirectory(object sender, ResolveEventArgs args)
  72. {
  73. var name = new AssemblyName(args.Name);
  74. try
  75. {
  76. return Assembly.LoadFile(Path.Combine(preloaderPath, $"{name.Name}.dll"));
  77. }
  78. catch (Exception)
  79. {
  80. return null;
  81. }
  82. }
  83. }
  84. }