UnityChainloader.cs 2.6 KB

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