Program.cs 3.3 KB

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