NetPreloader.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Reflection;
  5. using BepInEx.Bootstrap;
  6. using BepInEx.Configuration;
  7. using BepInEx.Logging;
  8. using BepInEx.Preloader.Core;
  9. using BepInEx.Preloader.Core.RuntimeFixes;
  10. using MonoMod.RuntimeDetour;
  11. namespace BepInEx.NetLauncher
  12. {
  13. public static class NetPreloader
  14. {
  15. private static readonly ManualLogSource Log = PreloaderLogger.Log;
  16. public static void Start(string[] args)
  17. {
  18. if (ConfigEntrypointExecutable.Value == null)
  19. {
  20. Log.LogFatal($"Entry executable was not set. Please set this in your config before launching the application");
  21. Program.ReadExit();
  22. return;
  23. }
  24. string executablePath = Path.GetFullPath(ConfigEntrypointExecutable.Value);
  25. if (!File.Exists(executablePath))
  26. {
  27. Log.LogFatal($"Unable to locate executable: {ConfigEntrypointExecutable.Value}");
  28. Program.ReadExit();
  29. return;
  30. }
  31. Paths.SetExecutablePath(executablePath);
  32. Program.ResolveDirectories.Add(Paths.GameRootPath);
  33. TypeLoader.SearchDirectories.Add(Paths.GameRootPath);
  34. bool bridgeInitialized = Utility.TryDo(() =>
  35. {
  36. if (ConfigShimHarmony.Value)
  37. HarmonyDetourBridge.Init();
  38. }, out var harmonyBridgeException);
  39. Logger.Sources.Add(TraceLogSource.CreateSource());
  40. HarmonyFixes.Apply();
  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. using (var assemblyPatcher = new AssemblyPatcher())
  57. {
  58. assemblyPatcher.AddPatchersFromDirectory(Paths.PatcherPluginPath);
  59. Log.LogInfo($"{assemblyPatcher.PatcherPlugins.Count} patcher plugin(s) loaded");
  60. assemblyPatcher.LoadAssemblyDirectory(Paths.GameRootPath, "dll", "exe");
  61. Log.LogInfo($"{assemblyPatcher.AssembliesToPatch.Count} assemblies discovered");
  62. assemblyPatcher.PatchAndLoad();
  63. }
  64. Log.LogMessage("Preloader finished");
  65. var chainloader = new NetChainloader();
  66. chainloader.Initialize();
  67. chainloader.Execute();
  68. var assemblyName = AssemblyName.GetAssemblyName(executablePath);
  69. var entrypointAssembly = Assembly.Load(assemblyName);
  70. entrypointAssembly.EntryPoint.Invoke(null, new [] { args });
  71. }
  72. #region Config
  73. private static readonly ConfigEntry<string> ConfigEntrypointExecutable = ConfigFile.CoreConfig.Bind<string>(
  74. "Preloader.Entrypoint", "Assembly",
  75. null,
  76. "The local filename of the .NET executable to target.");
  77. private static readonly ConfigEntry<bool> ConfigShimHarmony = ConfigFile.CoreConfig.Bind(
  78. "Preloader", "ShimHarmonySupport",
  79. !Utility.CLRSupportsDynamicAssemblies,
  80. "If enabled, basic Harmony functionality is patched to use MonoMod's RuntimeDetour instead.\nTry using this if Harmony does not work in a game.");
  81. private static readonly ConfigEntry<bool> ConfigPreloaderCOutLogging = ConfigFile.CoreConfig.Bind(
  82. "Logging", "PreloaderConsoleOutRedirection",
  83. true,
  84. "Redirects text from Console.Out during preloader patch loading to the BepInEx logging system.");
  85. #endregion
  86. }
  87. }