DoorstopEntrypoint.cs 3.4 KB

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