DoorstopEntrypoint.cs 3.3 KB

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