DoorstopEntrypoint.cs 3.1 KB

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