Chainloader.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using BepInEx.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using BepInEx.Logging;
  9. using UnityEngine;
  10. using UnityInjector.ConsoleUtil;
  11. using UnityLogWriter = BepInEx.Logging.UnityLogWriter;
  12. namespace BepInEx.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 Chainloader
  18. {
  19. /// <summary>
  20. /// The loaded and initialized list of plugins.
  21. /// </summary>
  22. public static List<BaseUnityPlugin> Plugins { get; protected set; } = new List<BaseUnityPlugin>();
  23. /// <summary>
  24. /// The GameObject that all plugins are attached to as components.
  25. /// </summary>
  26. public static GameObject ManagerObject { get; protected set; } = new GameObject("BepInEx_Manager");
  27. private static bool _loaded = false;
  28. /// <summary>
  29. /// The entry point for the BepInEx plugin system, called on the very first LoadScene() from UnityEngine.
  30. /// </summary>
  31. public static void Initialize()
  32. {
  33. if (_loaded)
  34. return;
  35. if (!Directory.Exists(Utility.PluginsDirectory))
  36. Directory.CreateDirectory(Utility.PluginsDirectory);
  37. Preloader.AllocateConsole();
  38. try
  39. {
  40. UnityLogWriter unityLogWriter = new UnityLogWriter();
  41. if (Preloader.PreloaderLog != null)
  42. unityLogWriter.WriteToLog($"{Preloader.PreloaderLog}\r\n");
  43. Logger.SetLogger(unityLogWriter);
  44. if(bool.Parse(Config.GetEntry("log_unity_messages", "false", "Global")))
  45. UnityLogWriter.ListenUnityLogs();
  46. string consoleTile = $"BepInEx {Assembly.GetExecutingAssembly().GetName().Version} - {Application.productName}";
  47. ConsoleWindow.Title = consoleTile;
  48. Logger.Log(LogLevel.Message, "Chainloader started");
  49. UnityEngine.Object.DontDestroyOnLoad(ManagerObject);
  50. string currentProcess = Process.GetCurrentProcess().ProcessName.ToLower();
  51. var pluginTypes = TypeLoader.LoadTypes<BaseUnityPlugin>(Utility.PluginsDirectory)
  52. .Where(plugin =>
  53. {
  54. //Perform a filter for currently running process
  55. var filters = MetadataHelper.GetAttributes<BepInProcess>(plugin);
  56. if (!filters.Any())
  57. return true;
  58. return filters.Any(x => x.ProcessName.ToLower().Replace(".exe", "") == currentProcess);
  59. })
  60. .ToList();
  61. Logger.Log(LogLevel.Info, $"{pluginTypes.Count} plugins selected");
  62. Dictionary<Type, IEnumerable<Type>> dependencyDict = new Dictionary<Type, IEnumerable<Type>>();
  63. foreach (Type t in pluginTypes)
  64. {
  65. try
  66. {
  67. IEnumerable<Type> dependencies = MetadataHelper.GetDependencies(t, pluginTypes);
  68. dependencyDict[t] = dependencies;
  69. }
  70. catch (MissingDependencyException)
  71. {
  72. var metadata = MetadataHelper.GetMetadata(t);
  73. Logger.Log(LogLevel.Info, $"Cannot load [{metadata.Name}] due to missing dependencies.");
  74. }
  75. }
  76. pluginTypes = Utility.TopologicalSort(dependencyDict.Keys, x => dependencyDict[x]).ToList();
  77. foreach (Type t in pluginTypes)
  78. {
  79. try
  80. {
  81. var metadata = MetadataHelper.GetMetadata(t);
  82. var plugin = (BaseUnityPlugin) ManagerObject.AddComponent(t);
  83. Plugins.Add(plugin);
  84. Logger.Log(LogLevel.Info, $"Loaded [{metadata.Name} {metadata.Version}]");
  85. }
  86. catch (Exception ex)
  87. {
  88. Logger.Log(LogLevel.Info, $"Error loading [{t.Name}] : {ex.Message}");
  89. }
  90. }
  91. }
  92. catch (Exception ex)
  93. {
  94. UnityInjector.ConsoleUtil.ConsoleWindow.Attach();
  95. Console.WriteLine("Error occurred starting the game");
  96. Console.WriteLine(ex.ToString());
  97. }
  98. _loaded = true;
  99. }
  100. }
  101. }