Chainloader.cs 3.4 KB

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