DoorstopEntrypoint.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. namespace BepInEx.IL2CPP
  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(EnvVars.DOORSTOP_PROCESS_PATH, bepinPath);
  13. Preloader.IL2CPPUnhollowedPath = Path.Combine(Paths.BepInExRootPath, "unhollowed");
  14. AppDomain.CurrentDomain.AssemblyResolve += LocalResolve;
  15. AppDomain.CurrentDomain.AssemblyResolve -= DoorstopEntrypoint.ResolveCurrentDirectory;
  16. File.WriteAllText("B:\\a.txt", "a");
  17. //AppDomain.CurrentDomain.TypeResolve += (sender, eventArgs) =>
  18. //{
  19. // eventArgs.
  20. //}
  21. Preloader.Run();
  22. }
  23. internal 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. || Utility.TryResolveDllAssembly(assemblyName, Preloader.IL2CPPUnhollowedPath, out foundAssembly))
  34. return foundAssembly;
  35. return null;
  36. }
  37. }
  38. internal static class DoorstopEntrypoint
  39. {
  40. private static string preloaderPath;
  41. /// <summary>
  42. /// The main entrypoint of BepInEx, called from Doorstop.
  43. /// </summary>
  44. /// <param name="args">
  45. /// The arguments passed in from Doorstop. First argument is the path of the currently executing
  46. /// process.
  47. /// </param>
  48. public static void Main(string[] args)
  49. {
  50. // We set it to the current directory first as a fallback, but try to use the same location as the .exe file.
  51. string silentExceptionLog = $"preloader_{DateTime.Now:yyyyMMdd_HHmmss_fff}.log";
  52. try
  53. {
  54. EnvVars.LoadVars();
  55. silentExceptionLog = Path.Combine(Path.GetDirectoryName(EnvVars.DOORSTOP_PROCESS_PATH), silentExceptionLog);
  56. // 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
  57. preloaderPath = Path.GetDirectoryName(Path.GetFullPath(EnvVars.DOORSTOP_INVOKE_DLL_PATH));
  58. AppDomain.CurrentDomain.AssemblyResolve += ResolveCurrentDirectory;
  59. UnityPreloaderRunner.PreloaderMain(args);
  60. }
  61. catch (Exception ex)
  62. {
  63. File.WriteAllText(silentExceptionLog, ex.ToString());
  64. }
  65. }
  66. public static Assembly ResolveCurrentDirectory(object sender, ResolveEventArgs args)
  67. {
  68. var name = new AssemblyName(args.Name);
  69. try
  70. {
  71. return Assembly.LoadFile(Path.Combine(preloaderPath, $"{name.Name}.dll"));
  72. }
  73. catch (Exception)
  74. {
  75. return null;
  76. }
  77. }
  78. }
  79. }