DoorstopEntrypoint.cs 3.7 KB

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