Chainloader.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using BepInEx.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using UnityEngine;
  8. namespace BepInEx
  9. {
  10. /// <summary>
  11. /// The manager and loader for all plugins, and the entry point for BepInEx.
  12. /// </summary>
  13. public class Chainloader
  14. {
  15. /// <summary>
  16. /// The loaded and initialized list of plugins.
  17. /// </summary>
  18. public static List<BaseUnityPlugin> Plugins { get; protected set; } = new List<BaseUnityPlugin>();
  19. /// <summary>
  20. /// The GameObject that all plugins are attached to as components.
  21. /// </summary>
  22. public static GameObject ManagerObject { get; protected set; } = new GameObject("BepInEx_Manager");
  23. static bool loaded = false;
  24. /// <summary>
  25. /// The entry point for BepInEx, called on the very first LoadScene() from UnityEngine.
  26. /// </summary>
  27. public static void Initialize()
  28. {
  29. if (loaded)
  30. return;
  31. if (bool.Parse(Config.GetEntry("console", "false")))
  32. {
  33. try
  34. {
  35. UnityInjector.ConsoleUtil.ConsoleWindow.Attach();
  36. if (bool.Parse(Config.GetEntry("console-shiftjis", "false")))
  37. UnityInjector.ConsoleUtil.ConsoleEncoding.ConsoleCodePage = 932;
  38. }
  39. catch
  40. {
  41. BepInLogger.Log("Failed to allocate console!", true);
  42. }
  43. }
  44. try
  45. {
  46. BepInLogger.Log($"BepInEx {Assembly.GetExecutingAssembly().GetName().Version}");
  47. BepInLogger.Log($"Chainloader started");
  48. UnityEngine.Object.DontDestroyOnLoad(ManagerObject);
  49. if (Directory.Exists(Common.Utility.PluginsDirectory))
  50. {
  51. var pluginTypes = TypeLoader.LoadTypes<BaseUnityPlugin>(Common.Utility.PluginsDirectory).ToList();
  52. BepInLogger.Log($"{pluginTypes.Count} plugins found");
  53. Dictionary<Type, IEnumerable<Type>> dependencyDict = new Dictionary<Type, IEnumerable<Type>>();
  54. foreach (Type t in pluginTypes)
  55. {
  56. try
  57. {
  58. IEnumerable<Type> dependencies = TypeLoader.GetDependencies(t, pluginTypes);
  59. dependencyDict[t] = dependencies;
  60. }
  61. catch (MissingDependencyException)
  62. {
  63. var metadata = TypeLoader.GetMetadata(t);
  64. BepInLogger.Log($"Cannot load [{metadata.Name}] due to missing dependencies.");
  65. }
  66. }
  67. pluginTypes = TopologicalSort(dependencyDict.Keys, x => dependencyDict[x]).ToList();
  68. foreach (Type t in pluginTypes)
  69. {
  70. try
  71. {
  72. var metadata = TypeLoader.GetMetadata(t);
  73. var plugin = (BaseUnityPlugin) ManagerObject.AddComponent(t);
  74. Plugins.Add(plugin);
  75. BepInLogger.Log($"Loaded [{metadata.Name} {metadata.Version}]");
  76. }
  77. catch (Exception ex)
  78. {
  79. BepInLogger.Log($"Error loading [{t.Name}] : {ex.Message}");
  80. }
  81. }
  82. }
  83. }
  84. catch (Exception ex)
  85. {
  86. UnityInjector.ConsoleUtil.ConsoleWindow.Attach();
  87. Console.WriteLine("Error occurred starting the game");
  88. Console.WriteLine(ex.ToString());
  89. }
  90. loaded = true;
  91. }
  92. protected static IEnumerable<TNode> TopologicalSort<TNode>(
  93. IEnumerable<TNode> nodes,
  94. Func<TNode, IEnumerable<TNode>> dependencySelector)
  95. {
  96. List<TNode> sorted_list = new List<TNode>();
  97. HashSet<TNode> visited = new HashSet<TNode>();
  98. HashSet<TNode> sorted = new HashSet<TNode>();
  99. foreach (TNode input in nodes)
  100. Visit(input);
  101. return sorted_list;
  102. void Visit(TNode node)
  103. {
  104. if (visited.Contains(node))
  105. {
  106. if (!sorted.Contains(node))
  107. throw new Exception("Cyclic Dependency");
  108. }
  109. else
  110. {
  111. visited.Add(node);
  112. foreach (var dep in dependencySelector(node))
  113. Visit(dep);
  114. sorted.Add(node);
  115. sorted_list.Add(node);
  116. }
  117. }
  118. }
  119. }
  120. }