Chainloader.cs 3.7 KB

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