Chainloader.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. UnityInjector.ConsoleUtil.ConsoleWindow.Attach();
  34. if (bool.Parse(Config.GetEntry("console-shiftjis", "false")))
  35. UnityInjector.ConsoleUtil.ConsoleEncoding.ConsoleCodePage = 932;
  36. }
  37. try
  38. {
  39. BepInLogger.Log($"BepInEx {Assembly.GetExecutingAssembly().GetName().Version}");
  40. BepInLogger.Log($"Chainloader started");
  41. UnityEngine.Object.DontDestroyOnLoad(ManagerObject);
  42. if (Directory.Exists(Utility.PluginsDirectory))
  43. {
  44. var pluginTypes = TypeLoader.LoadTypes<BaseUnityPlugin>(Utility.PluginsDirectory).ToList();
  45. BepInLogger.Log($"{pluginTypes.Count} plugins found");
  46. Dictionary<Type, IEnumerable<Type>> dependencyDict = new Dictionary<Type, IEnumerable<Type>>();
  47. foreach (Type t in pluginTypes)
  48. {
  49. try
  50. {
  51. IEnumerable<Type> dependencies = TypeLoader.GetDependencies(t, pluginTypes);
  52. dependencyDict[t] = dependencies;
  53. }
  54. catch (MissingDependencyException)
  55. {
  56. var metadata = TypeLoader.GetMetadata(t);
  57. BepInLogger.Log($"Cannot load [{metadata.Name}] due to missing dependencies.");
  58. }
  59. }
  60. pluginTypes = TopologicalSort(dependencyDict.Keys, x => dependencyDict[x]).ToList();
  61. foreach (Type t in pluginTypes)
  62. {
  63. try
  64. {
  65. var metadata = TypeLoader.GetMetadata(t);
  66. var plugin = (BaseUnityPlugin)ManagerObject.AddComponent(t);
  67. Plugins.Add(plugin);
  68. BepInLogger.Log($"Loaded [{metadata.Name} {metadata.Version}]");
  69. }
  70. catch (Exception ex)
  71. {
  72. BepInLogger.Log($"Error loading [{t.Name}] : {ex.Message}");
  73. }
  74. }
  75. }
  76. }
  77. catch (Exception ex)
  78. {
  79. UnityInjector.ConsoleUtil.ConsoleWindow.Attach();
  80. Console.WriteLine("Error occurred starting the game");
  81. Console.WriteLine(ex.ToString());
  82. }
  83. loaded = true;
  84. }
  85. protected static IEnumerable<TNode> TopologicalSort<TNode>(
  86. IEnumerable<TNode> nodes,
  87. Func<TNode, IEnumerable<TNode>> dependencySelector)
  88. {
  89. List<TNode> sorted_list = new List<TNode>();
  90. HashSet<TNode> visited = new HashSet<TNode>();
  91. HashSet<TNode> sorted = new HashSet<TNode>();
  92. foreach (TNode input in nodes)
  93. Visit(input);
  94. return sorted_list;
  95. void Visit(TNode node)
  96. {
  97. if (visited.Contains(node))
  98. {
  99. if (!sorted.Contains(node))
  100. throw new Exception("Cyclic Dependency");
  101. }
  102. else
  103. {
  104. visited.Add(node);
  105. foreach (var dep in dependencySelector(node))
  106. Visit(dep);
  107. sorted.Add(node);
  108. sorted_list.Add(node);
  109. }
  110. }
  111. }
  112. }
  113. }