Entrypoint.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using BepInEx.Preloader.RuntimeFixes;
  6. namespace BepInEx.Preloader
  7. {
  8. internal static class PreloaderRunner
  9. {
  10. public static void PreloaderPreMain()
  11. {
  12. PlatformUtils.SetPlatform();
  13. string bepinPath = Utility.ParentDirectory(Path.GetFullPath(EnvVars.DOORSTOP_INVOKE_DLL_PATH), 2);
  14. Paths.SetExecutablePath(EnvVars.DOORSTOP_PROCESS_PATH, 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. {
  22. XTermFix.Apply();
  23. ConsoleSetOutFix.Apply();
  24. }
  25. Preloader.Run();
  26. }
  27. private static Assembly LocalResolve(object sender, ResolveEventArgs args)
  28. {
  29. if (!Utility.TryParseAssemblyName(args.Name, out var assemblyName))
  30. return null;
  31. // Use parse assembly name on managed side because native GetName() can fail on some locales
  32. // if the game path has "exotic" characters
  33. var foundAssembly = AppDomain.CurrentDomain.GetAssemblies()
  34. .FirstOrDefault(x => Utility.TryParseAssemblyName(x.FullName, out var name) && name.Name == assemblyName.Name);
  35. if (foundAssembly != null)
  36. return foundAssembly;
  37. if (Utility.TryResolveDllAssembly(assemblyName, Paths.BepInExAssemblyDirectory, out foundAssembly)
  38. || Utility.TryResolveDllAssembly(assemblyName, Paths.PatcherPluginPath, out foundAssembly)
  39. || Utility.TryResolveDllAssembly(assemblyName, Paths.PluginPath, out foundAssembly))
  40. return foundAssembly;
  41. return null;
  42. }
  43. }
  44. internal static class Entrypoint
  45. {
  46. private static string preloaderPath;
  47. /// <summary>
  48. /// The main entrypoint of BepInEx, called from Doorstop.
  49. /// </summary>
  50. public static void Main()
  51. {
  52. // We set it to the current directory first as a fallback, but try to use the same location as the .exe file.
  53. string silentExceptionLog = $"preloader_{DateTime.Now:yyyyMMdd_HHmmss_fff}.log";
  54. try
  55. {
  56. EnvVars.LoadVars();
  57. string gamePath = Path.GetDirectoryName(EnvVars.DOORSTOP_PROCESS_PATH) ?? ".";
  58. silentExceptionLog = Path.Combine(gamePath, silentExceptionLog);
  59. // 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
  60. preloaderPath = Path.GetDirectoryName(Path.GetFullPath(EnvVars.DOORSTOP_INVOKE_DLL_PATH));
  61. AppDomain.CurrentDomain.AssemblyResolve += ResolveCurrentDirectory;
  62. // In some versions of Unity 4, Mono tries to resolve BepInEx.dll prematurely because of the call to Paths.SetExecutablePath
  63. // 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
  64. typeof(Entrypoint).Assembly.GetType($"BepInEx.Preloader.{nameof(PreloaderRunner)}")
  65. ?.GetMethod(nameof(PreloaderRunner.PreloaderPreMain))
  66. ?.Invoke(null, null);
  67. AppDomain.CurrentDomain.AssemblyResolve -= ResolveCurrentDirectory;
  68. }
  69. catch (Exception ex)
  70. {
  71. File.WriteAllText(silentExceptionLog, ex.ToString());
  72. }
  73. }
  74. private static Assembly ResolveCurrentDirectory(object sender, ResolveEventArgs args)
  75. {
  76. // Can't use Utils here because it's not yet resolved
  77. var name = new AssemblyName(args.Name);
  78. try
  79. {
  80. return Assembly.LoadFile(Path.Combine(preloaderPath, $"{name.Name}.dll"));
  81. }
  82. catch (Exception)
  83. {
  84. return null;
  85. }
  86. }
  87. }
  88. }