Program.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using BepInEx.Logging;
  8. using BepInEx.Preloader.Core;
  9. namespace BepInEx.NetLauncher
  10. {
  11. internal static class NetPreloaderRunner
  12. {
  13. internal static void PreloaderMain(string[] args)
  14. {
  15. try
  16. {
  17. Logger.Listeners.Add(new ConsoleLogListener());
  18. ConsoleManager.Initialize(true);
  19. NetPreloader.Start(args);
  20. }
  21. catch (Exception ex)
  22. {
  23. PreloaderLogger.Log.LogFatal("Unhandled exception");
  24. PreloaderLogger.Log.LogFatal(ex);
  25. Program.ReadExit();
  26. }
  27. }
  28. internal static void OuterMain(string[] args, string filename)
  29. {
  30. Paths.SetExecutablePath(filename);
  31. AppDomain.CurrentDomain.AssemblyResolve += LocalResolve;
  32. PreloaderMain(args);
  33. }
  34. private static Assembly LocalResolve(object sender, ResolveEventArgs args)
  35. {
  36. var assemblyName = new AssemblyName(args.Name);
  37. var foundAssembly = AppDomain.CurrentDomain.GetAssemblies()
  38. .FirstOrDefault(x => x.GetName().Name == assemblyName.Name);
  39. if (foundAssembly != null)
  40. return foundAssembly;
  41. if (LocalUtility.TryResolveDllAssembly(assemblyName, Paths.BepInExAssemblyDirectory, out foundAssembly)
  42. || LocalUtility.TryResolveDllAssembly(assemblyName, Paths.PatcherPluginPath, out foundAssembly)
  43. || LocalUtility.TryResolveDllAssembly(assemblyName, Paths.PluginPath, out foundAssembly))
  44. return foundAssembly;
  45. return null;
  46. }
  47. }
  48. class Program
  49. {
  50. internal static void ReadExit()
  51. {
  52. Console.WriteLine("Press enter to exit...");
  53. Console.ReadLine();
  54. Environment.Exit(-1);
  55. }
  56. static void Main(string[] args)
  57. {
  58. try
  59. {
  60. string filename;
  61. #if DEBUG
  62. filename = Path.Combine(Directory.GetCurrentDirectory(), Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName));
  63. #else
  64. filename = Process.GetCurrentProcess().MainModule.FileName;
  65. #endif
  66. ResolveDirectories.Add(Path.Combine(Path.GetDirectoryName(filename), "BepInEx", "Core"));
  67. AppDomain.CurrentDomain.AssemblyResolve += RemoteResolve;
  68. NetPreloaderRunner.OuterMain(args, filename);
  69. }
  70. catch (Exception ex)
  71. {
  72. Console.WriteLine("Unhandled exception");
  73. Console.WriteLine(ex);
  74. ReadExit();
  75. }
  76. }
  77. public static List<string> ResolveDirectories { get; set; } = new List<string>
  78. {
  79. "C:\\Windows\\Microsoft.NET\\assembly\\GAC_32\\Microsoft.Xna.Framework.Game\\v4.0_4.0.0.0__842cf8be1de50553\\"
  80. };
  81. private static Assembly RemoteResolve(object sender, ResolveEventArgs reference)
  82. {
  83. var assemblyName = new AssemblyName(reference.Name);
  84. foreach (var directory in ResolveDirectories)
  85. {
  86. var potentialDirectories = new List<string> { directory };
  87. potentialDirectories.AddRange(Directory.GetDirectories(directory, "*", SearchOption.AllDirectories));
  88. var potentialFiles = potentialDirectories.Select(x => Path.Combine(x, $"{assemblyName.Name}.dll"))
  89. .Concat(potentialDirectories.Select(x => Path.Combine(x, $"{assemblyName.Name}.exe")));
  90. foreach (string path in potentialFiles)
  91. {
  92. if (!File.Exists(path))
  93. continue;
  94. var assembly = Assembly.LoadFrom(path);
  95. if (assembly.GetName().Name == assemblyName.Name)
  96. return assembly;
  97. }
  98. }
  99. return null;
  100. }
  101. }
  102. }