UnityChainloader.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using BepInEx.Configuration;
  2. using BepInEx.Logging;
  3. using System;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using BepInEx.Bootstrap;
  10. using BepInEx.Preloader.Core;
  11. using BepInEx.Preloader.Core.Logging;
  12. using BepInEx.Unity.Logging;
  13. using UnityEngine;
  14. using Logger = BepInEx.Logging.Logger;
  15. namespace BepInEx.Unity.Bootstrap
  16. {
  17. /// <summary>
  18. /// The manager and loader for all plugins, and the entry point for BepInEx plugin system.
  19. /// </summary>
  20. public class UnityChainloader : BaseChainloader<BaseUnityPlugin>
  21. {
  22. /// <summary>
  23. /// The GameObject that all plugins are attached to as components.
  24. /// </summary>
  25. public static GameObject ManagerObject { get; private set; }
  26. private static void StaticStart(string gameExePath = null)
  27. {
  28. var instance = new UnityChainloader();
  29. instance.Initialize(gameExePath);
  30. instance.Execute();
  31. }
  32. private string _consoleTitle;
  33. protected override string ConsoleTitle => _consoleTitle;
  34. public override void Initialize(string gameExePath = null)
  35. {
  36. UnityTomlTypeConverters.AddUnityEngineConverters();
  37. ThreadingHelper.Initialize();
  38. ManagerObject = new GameObject("BepInEx_Manager");
  39. UnityEngine.Object.DontDestroyOnLoad(ManagerObject);
  40. var productNameProp = typeof(Application).GetProperty("productName", BindingFlags.Public | BindingFlags.Static);
  41. _consoleTitle = $"{CurrentAssemblyName} {CurrentAssemblyVersion} - {productNameProp?.GetValue(null, null) ?? Process.GetCurrentProcess().ProcessName}";
  42. base.Initialize(gameExePath);
  43. }
  44. protected override void InitializeLoggers()
  45. {
  46. if (ConsoleManager.ConfigConsoleEnabled.Value)
  47. {
  48. ConsoleManager.CreateConsole();
  49. if (!Logger.Listeners.Any(x => x is ConsoleLogListener))
  50. Logger.Listeners.Add(new ConsoleLogListener());
  51. }
  52. // Fix for standard output getting overwritten by UnityLogger
  53. if (ConsoleManager.StandardOut != null)
  54. {
  55. Console.SetOut(ConsoleManager.StandardOut);
  56. var encoding = ConsoleManager.ConfigConsoleShiftJis.Value ? 932 : (uint)Encoding.UTF8.CodePage;
  57. ConsoleManager.SetConsoleEncoding(encoding);
  58. }
  59. Logger.Listeners.Add(new UnityLogListener());
  60. if (ConfigUnityLogging.Value)
  61. Logger.Sources.Add(new UnityLogSource());
  62. base.InitializeLoggers();
  63. if (!ConfigDiskWriteUnityLog.Value)
  64. {
  65. DiskLogListener.BlacklistedSources.Add("Unity Log");
  66. }
  67. // Temporarily disable the console log listener as we replay the preloader logs
  68. var logListener = Logger.Listeners.FirstOrDefault(logger => logger is ConsoleLogListener);
  69. if (logListener != null)
  70. Logger.Listeners.Remove(logListener);
  71. foreach (var preloaderLogEvent in PreloaderConsoleListener.LogEvents)
  72. {
  73. PreloaderLogger.Log.Log(preloaderLogEvent.Level, preloaderLogEvent.Data);
  74. }
  75. if (logListener != null)
  76. Logger.Listeners.Add(logListener);
  77. }
  78. public override BaseUnityPlugin LoadPlugin(PluginInfo pluginInfo, Assembly pluginAssembly)
  79. {
  80. return (BaseUnityPlugin)ManagerObject.AddComponent(pluginAssembly.GetType(pluginInfo.TypeName));
  81. }
  82. private static readonly ConfigEntry<bool> ConfigUnityLogging = ConfigFile.CoreConfig.Bind(
  83. "Logging", "UnityLogListening",
  84. true,
  85. "Enables showing unity log messages in the BepInEx logging system.");
  86. private static readonly ConfigEntry<bool> ConfigDiskWriteUnityLog = ConfigFile.CoreConfig.Bind(
  87. "Logging.Disk", "WriteUnityLog",
  88. false,
  89. "Include unity log messages in log file output.");
  90. }
  91. }