Program.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. Logger.Listeners.Add(new ConsoleLogListener());
  16. ConsoleManager.ForceSetActive(true);
  17. NetPreloader.Start(args);
  18. }
  19. internal static void OuterMain(string[] args, string filename)
  20. {
  21. try
  22. {
  23. Paths.SetExecutablePath(filename);
  24. AppDomain.CurrentDomain.AssemblyResolve += LocalResolve;
  25. PreloaderMain(args);
  26. }
  27. catch (Exception ex)
  28. {
  29. PreloaderLogger.Log.LogFatal("Unhandled exception");
  30. PreloaderLogger.Log.LogFatal(ex);
  31. Program.ReadExit();
  32. }
  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. Console.WriteLine("test");
  61. string filename;
  62. #if DEBUG
  63. filename = Path.Combine(Directory.GetCurrentDirectory(), Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName));
  64. #else
  65. filename = Process.GetCurrentProcess().MainModule.FileName;
  66. #endif
  67. ResolveDirectories.Add(Path.Combine(Path.GetDirectoryName(filename), "BepInEx", "Core"));
  68. AppDomain.CurrentDomain.AssemblyResolve += RemoteResolve;
  69. NetPreloaderRunner.OuterMain(args, filename);
  70. }
  71. catch (Exception ex)
  72. {
  73. Console.WriteLine("Unhandled exception");
  74. Console.WriteLine(ex);
  75. ReadExit();
  76. }
  77. }
  78. public static List<string> ResolveDirectories { get; set; } = new List<string>
  79. {
  80. "C:\\Windows\\Microsoft.NET\\assembly\\GAC_32\\Microsoft.Xna.Framework.Game\\v4.0_4.0.0.0__842cf8be1de50553\\"
  81. };
  82. private static Assembly RemoteResolve(object sender, ResolveEventArgs reference)
  83. {
  84. var assemblyName = new AssemblyName(reference.Name);
  85. foreach (var directory in ResolveDirectories)
  86. {
  87. var potentialDirectories = new List<string> { directory };
  88. potentialDirectories.AddRange(Directory.GetDirectories(directory, "*", SearchOption.AllDirectories));
  89. var potentialFiles = potentialDirectories.Select(x => Path.Combine(x, $"{assemblyName.Name}.dll"))
  90. .Concat(potentialDirectories.Select(x => Path.Combine(x, $"{assemblyName.Name}.exe")));
  91. foreach (string path in potentialFiles)
  92. {
  93. if (!File.Exists(path))
  94. continue;
  95. var assembly = Assembly.LoadFrom(path);
  96. if (assembly.GetName().Name == assemblyName.Name)
  97. return assembly;
  98. }
  99. }
  100. return null;
  101. }
  102. }
  103. }