NetPreloader.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.NetLauncher.RuntimeFixes;
  9. using BepInEx.Preloader.Core;
  10. using BepInEx.Preloader.Core.RuntimeFixes;
  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. HarmonyFixes.Apply();
  42. string consoleTile = $"BepInEx {typeof(Paths).Assembly.GetName().Version} - {Process.GetCurrentProcess().ProcessName}";
  43. Log.LogMessage(consoleTile);
  44. if (ConsoleManager.ConsoleActive)
  45. ConsoleManager.SetConsoleTitle(consoleTile);
  46. //See BuildInfoAttribute for more information about this section.
  47. object[] attributes = typeof(BuildInfoAttribute).Assembly.GetCustomAttributes(typeof(BuildInfoAttribute), false);
  48. if (attributes.Length > 0)
  49. {
  50. var attribute = (BuildInfoAttribute)attributes[0];
  51. Log.LogMessage(attribute.Info);
  52. }
  53. Log.LogInfo($"CLR runtime version: {Environment.Version}");
  54. if (harmonyBridgeException != null)
  55. Log.LogWarning($"Failed to enable fix for Harmony for .NET Standard API. Error message: {harmonyBridgeException.Message}");
  56. Log.LogMessage("Preloader started");
  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. }
  65. Log.LogMessage("Preloader finished");
  66. var chainloader = new NetChainloader();
  67. chainloader.Initialize();
  68. chainloader.Execute();
  69. var assemblyName = AssemblyName.GetAssemblyName(executablePath);
  70. var entrypointAssembly = Assembly.Load(assemblyName);
  71. AssemblyFix.Execute(entrypointAssembly);
  72. entrypointAssembly.EntryPoint.Invoke(null, new [] { args });
  73. }
  74. #region Config
  75. private static readonly ConfigEntry<string> ConfigEntrypointExecutable = ConfigFile.CoreConfig.Bind<string>(
  76. "Preloader.Entrypoint", "Assembly",
  77. null,
  78. "The local filename of the .NET executable to target.");
  79. private static readonly ConfigEntry<bool> ConfigShimHarmony = ConfigFile.CoreConfig.Bind(
  80. "Preloader", "ShimHarmonySupport",
  81. !Utility.CLRSupportsDynamicAssemblies,
  82. "If enabled, basic Harmony functionality is patched to use MonoMod's RuntimeDetour instead.\nTry using this if Harmony does not work in a game.");
  83. private static readonly ConfigEntry<bool> ConfigPreloaderCOutLogging = ConfigFile.CoreConfig.Bind(
  84. "Logging", "PreloaderConsoleOutRedirection",
  85. true,
  86. "Redirects text from Console.Out during preloader patch loading to the BepInEx logging system.");
  87. #endregion
  88. }
  89. }