UnityChainloader.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using BepInEx.Configuration;
  2. using BepInEx.Logging;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Reflection;
  6. using BepInEx.Bootstrap;
  7. using BepInEx.Preloader.Core.Logging;
  8. using BepInEx.Unity.Logging;
  9. using MonoMod.Utils;
  10. using UnityEngine;
  11. using Logger = BepInEx.Logging.Logger;
  12. namespace BepInEx.Unity.Bootstrap
  13. {
  14. /// <summary>
  15. /// The manager and loader for all plugins, and the entry point for BepInEx plugin system.
  16. /// </summary>
  17. public class UnityChainloader : BaseChainloader<BaseUnityPlugin>
  18. {
  19. /// <summary>
  20. /// The GameObject that all plugins are attached to as components.
  21. /// </summary>
  22. public static GameObject ManagerObject { get; private set; }
  23. private static void StaticStart(string gameExePath = null)
  24. {
  25. var instance = new UnityChainloader();
  26. instance.Initialize(gameExePath);
  27. instance.Execute();
  28. }
  29. private string _consoleTitle;
  30. protected override string ConsoleTitle => _consoleTitle;
  31. public override void Initialize(string gameExePath = null)
  32. {
  33. UnityTomlTypeConverters.AddUnityEngineConverters();
  34. ThreadingHelper.Initialize();
  35. ManagerObject = new GameObject("BepInEx_Manager");
  36. UnityEngine.Object.DontDestroyOnLoad(ManagerObject);
  37. var productNameProp = typeof(Application).GetProperty("productName", BindingFlags.Public | BindingFlags.Static);
  38. _consoleTitle = $"{CurrentAssemblyName} {CurrentAssemblyVersion} - {productNameProp?.GetValue(null, null) ?? Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().ProcessName)}";
  39. base.Initialize(gameExePath);
  40. }
  41. protected override void InitializeLoggers()
  42. {
  43. Logger.Listeners.Add(new UnityLogListener());
  44. if (ConfigUnityLogging.Value)
  45. Logger.Sources.Add(new UnityLogSource());
  46. base.InitializeLoggers();
  47. if (Utility.CurrentPlatform != Platform.Windows)
  48. {
  49. Logger.LogInfo($"Detected Unity version: v{Application.unityVersion}");
  50. }
  51. if (!ConfigDiskWriteUnityLog.Value)
  52. {
  53. DiskLogListener.BlacklistedSources.Add("Unity Log");
  54. }
  55. ChainloaderLogHelper.RewritePreloaderLogs();
  56. }
  57. public override BaseUnityPlugin LoadPlugin(PluginInfo pluginInfo, Assembly pluginAssembly)
  58. {
  59. return (BaseUnityPlugin)ManagerObject.AddComponent(pluginAssembly.GetType(pluginInfo.TypeName));
  60. }
  61. private static readonly ConfigEntry<bool> ConfigUnityLogging = ConfigFile.CoreConfig.Bind(
  62. "Logging", "UnityLogListening",
  63. true,
  64. "Enables showing unity log messages in the BepInEx logging system.");
  65. private static readonly ConfigEntry<bool> ConfigDiskWriteUnityLog = ConfigFile.CoreConfig.Bind(
  66. "Logging.Disk", "WriteUnityLog",
  67. false,
  68. "Include unity log messages in log file output.");
  69. }
  70. }