DoorstopEntrypoint.cs 3.6 KB

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