NetPreloader.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using BepInEx.Bootstrap;
  7. using BepInEx.Configuration;
  8. using BepInEx.Logging;
  9. using BepInEx.NetLauncher.RuntimeFixes;
  10. using BepInEx.Preloader.Core;
  11. using MonoMod.RuntimeDetour;
  12. namespace BepInEx.NetLauncher
  13. {
  14. public static class NetPreloader
  15. {
  16. private static readonly ManualLogSource Log = PreloaderLogger.Log;
  17. public static void Start(string[] args)
  18. {
  19. if (ConfigEntrypointExecutable.Value == null)
  20. {
  21. Log.LogFatal($"Entry executable was not set. Please set this in your config before launching the application");
  22. Program.ReadExit();
  23. return;
  24. }
  25. string executablePath = Path.GetFullPath(ConfigEntrypointExecutable.Value);
  26. if (!File.Exists(executablePath))
  27. {
  28. Log.LogFatal($"Unable to locate executable: {ConfigEntrypointExecutable.Value}");
  29. Program.ReadExit();
  30. return;
  31. }
  32. Paths.SetExecutablePath(executablePath);
  33. Program.ResolveDirectories.Add(Paths.GameRootPath);
  34. TypeLoader.SearchDirectories.Add(Paths.GameRootPath);
  35. bool bridgeInitialized = Utility.TryDo(() =>
  36. {
  37. if (ConfigShimHarmony.Value)
  38. HarmonyDetourBridge.Init();
  39. }, out var harmonyBridgeException);
  40. Logger.Sources.Add(TraceLogSource.CreateSource());
  41. string consoleTile = $"BepInEx {typeof(Paths).Assembly.GetName().Version} - {Process.GetCurrentProcess().ProcessName}";
  42. Log.LogMessage(consoleTile);
  43. if (ConsoleManager.ConsoleActive)
  44. ConsoleManager.SetConsoleTitle(consoleTile);
  45. //See BuildInfoAttribute for more information about this section.
  46. object[] attributes = typeof(BuildInfoAttribute).Assembly.GetCustomAttributes(typeof(BuildInfoAttribute), false);
  47. if (attributes.Length > 0)
  48. {
  49. var attribute = (BuildInfoAttribute)attributes[0];
  50. Log.LogMessage(attribute.Info);
  51. }
  52. Log.LogInfo($"CLR runtime version: {Environment.Version}");
  53. if (harmonyBridgeException != null)
  54. Log.LogWarning($"Failed to enable fix for Harmony for .NET Standard API. Error message: {harmonyBridgeException.Message}");
  55. Log.LogMessage("Preloader started");
  56. Assembly entrypointAssembly;
  57. using (var assemblyPatcher = new AssemblyPatcher())
  58. {
  59. assemblyPatcher.AddPatchersFromDirectory(Paths.PatcherPluginPath);
  60. Log.LogInfo($"{assemblyPatcher.PatcherPlugins.Count} patcher plugin(s) loaded");
  61. assemblyPatcher.LoadAssemblyDirectory(Paths.GameRootPath, "dll", "exe");
  62. Log.LogInfo($"{assemblyPatcher.AssembliesToPatch.Count} assemblies discovered");
  63. assemblyPatcher.PatchAndLoad();
  64. var assemblyName = AssemblyName.GetAssemblyName(executablePath);
  65. entrypointAssembly = assemblyPatcher.LoadedAssemblies.Values.FirstOrDefault(x => x.FullName == assemblyName.FullName);
  66. if (entrypointAssembly != null)
  67. {
  68. Log.LogDebug("Found patched entrypoint assembly! Using it");
  69. }
  70. else
  71. {
  72. Log.LogDebug("Using entrypoint assembly from disk");
  73. entrypointAssembly = Assembly.LoadFrom(executablePath);
  74. }
  75. }
  76. Log.LogMessage("Preloader finished");
  77. var chainloader = new NetChainloader();
  78. chainloader.Initialize();
  79. chainloader.Execute();
  80. AssemblyFix.Execute(entrypointAssembly);
  81. entrypointAssembly.EntryPoint.Invoke(null, new [] { args });
  82. }
  83. #region Config
  84. private static readonly ConfigEntry<string> ConfigEntrypointExecutable = ConfigFile.CoreConfig.Bind<string>(
  85. "Preloader.Entrypoint", "Assembly",
  86. null,
  87. "The local filename of the .NET executable to target.");
  88. private static readonly ConfigEntry<bool> ConfigShimHarmony = ConfigFile.CoreConfig.Bind(
  89. "Preloader", "ShimHarmonySupport",
  90. !Utility.CLRSupportsDynamicAssemblies,
  91. "If enabled, basic Harmony functionality is patched to use MonoMod's RuntimeDetour instead.\nTry using this if Harmony does not work in a game.");
  92. private static readonly ConfigEntry<bool> ConfigPreloaderCOutLogging = ConfigFile.CoreConfig.Bind(
  93. "Logging", "PreloaderConsoleOutRedirection",
  94. true,
  95. "Redirects text from Console.Out during preloader patch loading to the BepInEx logging system.");
  96. #endregion
  97. }
  98. }