Chainloader.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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(Paths.PluginPath))
  36. Directory.CreateDirectory(Paths.PluginPath);
  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("chainloader-log-unity-messages", "false", "BepInEx")))
  45. UnityLogWriter.ListenUnityLogs();
  46. var productNameProp = typeof(Application).GetProperty("productName", BindingFlags.Public | BindingFlags.Static);
  47. if (productNameProp != null)
  48. ConsoleWindow.Title =
  49. $"BepInEx {Assembly.GetExecutingAssembly().GetName().Version} - {productNameProp.GetValue(null, null)}";
  50. Logger.Log(LogLevel.Message, "Chainloader started");
  51. UnityEngine.Object.DontDestroyOnLoad(ManagerObject);
  52. string currentProcess = Process.GetCurrentProcess().ProcessName.ToLower();
  53. var pluginTypes = TypeLoader.LoadTypes<BaseUnityPlugin>(Paths.PluginPath)
  54. .Where(plugin =>
  55. {
  56. //Perform a filter for currently running process
  57. var filters = MetadataHelper.GetAttributes<BepInProcess>(plugin);
  58. if (!filters.Any())
  59. return true;
  60. return filters.Any(x => x.ProcessName.ToLower().Replace(".exe", "") == currentProcess);
  61. })
  62. .ToList();
  63. Logger.Log(LogLevel.Info, $"{pluginTypes.Count} plugins selected");
  64. Dictionary<Type, IEnumerable<Type>> dependencyDict = new Dictionary<Type, IEnumerable<Type>>();
  65. foreach (Type t in pluginTypes)
  66. {
  67. try
  68. {
  69. IEnumerable<Type> dependencies = MetadataHelper.GetDependencies(t, pluginTypes);
  70. dependencyDict[t] = dependencies;
  71. }
  72. catch (MissingDependencyException)
  73. {
  74. var metadata = MetadataHelper.GetMetadata(t);
  75. Logger.Log(LogLevel.Info, $"Cannot load [{metadata.Name}] due to missing dependencies.");
  76. }
  77. }
  78. pluginTypes = Utility.TopologicalSort(dependencyDict.Keys, x => dependencyDict[x]).ToList();
  79. foreach (Type t in pluginTypes)
  80. {
  81. try
  82. {
  83. var metadata = MetadataHelper.GetMetadata(t);
  84. var plugin = (BaseUnityPlugin) ManagerObject.AddComponent(t);
  85. Plugins.Add(plugin);
  86. Logger.Log(LogLevel.Info, $"Loaded [{metadata.Name} {metadata.Version}]");
  87. }
  88. catch (Exception ex)
  89. {
  90. Logger.Log(LogLevel.Info, $"Error loading [{t.Name}] : {ex.Message}");
  91. }
  92. }
  93. }
  94. catch (Exception ex)
  95. {
  96. ConsoleWindow.Attach();
  97. Console.WriteLine("Error occurred starting the game");
  98. Console.WriteLine(ex.ToString());
  99. }
  100. _loaded = true;
  101. }
  102. }
  103. }