You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

856 lines
35 KiB

  1. using IPA.Config;
  2. using IPA.Loader.Features;
  3. using IPA.Logging;
  4. using IPA.Utilities;
  5. using Mono.Cecil;
  6. using Newtonsoft.Json;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Text.RegularExpressions;
  13. using System.Threading.Tasks;
  14. using Version = SemVer.Version;
  15. using SemVer;
  16. #if NET4
  17. using Task = System.Threading.Tasks.Task;
  18. using TaskEx = System.Threading.Tasks.Task;
  19. #endif
  20. #if NET3
  21. using Net3_Proxy;
  22. using Path = Net3_Proxy.Path;
  23. using File = Net3_Proxy.File;
  24. using Directory = Net3_Proxy.Directory;
  25. #endif
  26. namespace IPA.Loader
  27. {
  28. /// <summary>
  29. /// A type to manage the loading of plugins.
  30. /// </summary>
  31. public class PluginLoader
  32. {
  33. internal static Task LoadTask() =>
  34. TaskEx.Run(() =>
  35. {
  36. YeetIfNeeded();
  37. LoadMetadata();
  38. Resolve();
  39. ComputeLoadOrder();
  40. FilterDisabled();
  41. ResolveDependencies();
  42. });
  43. /// <summary>
  44. /// A class which describes a loaded plugin.
  45. /// </summary>
  46. public class PluginMetadata
  47. {
  48. /// <summary>
  49. /// The assembly the plugin was loaded from.
  50. /// </summary>
  51. /// <value>the loaded Assembly that contains the plugin main type</value>
  52. public Assembly Assembly { get; internal set; }
  53. /// <summary>
  54. /// The TypeDefinition for the main type of the plugin.
  55. /// </summary>
  56. /// <value>the Cecil definition for the plugin main type</value>
  57. public TypeDefinition PluginType { get; internal set; }
  58. /// <summary>
  59. /// The human readable name of the plugin.
  60. /// </summary>
  61. /// <value>the name of the plugin</value>
  62. public string Name { get; internal set; }
  63. /// <summary>
  64. /// The BeatMods ID of the plugin, or null if it doesn't have one.
  65. /// </summary>
  66. /// <value>the updater ID of the plugin</value>
  67. public string Id { get; internal set; }
  68. /// <summary>
  69. /// The version of the plugin.
  70. /// </summary>
  71. /// <value>the version of the plugin</value>
  72. public Version Version { get; internal set; }
  73. /// <summary>
  74. /// The file the plugin was loaded from.
  75. /// </summary>
  76. /// <value>the file the plugin was loaded from</value>
  77. public FileInfo File { get; internal set; }
  78. // ReSharper disable once UnusedAutoPropertyAccessor.Global
  79. /// <summary>
  80. /// The features this plugin requests.
  81. /// </summary>
  82. /// <value>the list of features requested by the plugin</value>
  83. public IReadOnlyList<Feature> Features => InternalFeatures;
  84. internal readonly List<Feature> InternalFeatures = new List<Feature>();
  85. internal bool IsSelf;
  86. /// <summary>
  87. /// Whether or not this metadata object represents a bare manifest.
  88. /// </summary>
  89. /// <value><see langword="true"/> if it is bare, <see langword="false"/> otherwise</value>
  90. public bool IsBare { get; internal set; }
  91. private PluginManifest manifest;
  92. internal HashSet<PluginMetadata> Dependencies { get; } = new HashSet<PluginMetadata>();
  93. internal PluginManifest Manifest
  94. {
  95. get => manifest;
  96. set
  97. {
  98. manifest = value;
  99. Name = value.Name;
  100. Version = value.Version;
  101. Id = value.Id;
  102. }
  103. }
  104. public RuntimeOptions RuntimeOptions { get; internal set; }
  105. public bool IsAttributePlugin { get; internal set; } = false;
  106. /// <summary>
  107. /// Gets all of the metadata as a readable string.
  108. /// </summary>
  109. /// <returns>the readable printable metadata string</returns>
  110. public override string ToString() => $"{Name}({Id}@{Version})({PluginType?.FullName}) from '{Utils.GetRelativePath(File?.FullName, BeatSaber.InstallPath)}'";
  111. }
  112. /// <summary>
  113. /// A container object for all the data relating to a plugin.
  114. /// </summary>
  115. public class PluginInfo
  116. {
  117. internal IPlugin Plugin { get; set; }
  118. /// <summary>
  119. /// Metadata for the plugin.
  120. /// </summary>
  121. /// <value>the metadata for this plugin</value>
  122. public PluginMetadata Metadata { get; internal set; } = new PluginMetadata();
  123. }
  124. internal static void YeetIfNeeded()
  125. {
  126. string pluginDir = BeatSaber.PluginsPath;
  127. if (SelfConfig.YeetMods_ && BeatSaber.IsGameVersionBoundary)
  128. {
  129. var oldPluginsName = Path.Combine(BeatSaber.InstallPath, $"Old {BeatSaber.OldVersion} Plugins");
  130. var newPluginsName = Path.Combine(BeatSaber.InstallPath, $"Old {BeatSaber.GameVersion} Plugins");
  131. if (Directory.Exists(oldPluginsName))
  132. Directory.Delete(oldPluginsName, true);
  133. Directory.Move(pluginDir, oldPluginsName);
  134. if (Directory.Exists(newPluginsName))
  135. Directory.Move(newPluginsName, pluginDir);
  136. else
  137. Directory.CreateDirectory(pluginDir);
  138. }
  139. }
  140. internal static List<PluginMetadata> PluginsMetadata = new List<PluginMetadata>();
  141. internal static List<PluginMetadata> DisabledPlugins = new List<PluginMetadata>();
  142. private static readonly Regex embeddedTextDescriptionPattern = new Regex(@"#!\[(.+)\]", RegexOptions.Compiled | RegexOptions.Singleline);
  143. internal static void LoadMetadata()
  144. {
  145. string[] plugins = Directory.GetFiles(BeatSaber.PluginsPath, "*.dll");
  146. try
  147. {
  148. var selfMeta = new PluginMetadata
  149. {
  150. Assembly = Assembly.GetExecutingAssembly(),
  151. File = new FileInfo(Path.Combine(BeatSaber.InstallPath, "IPA.exe")),
  152. PluginType = null,
  153. IsSelf = true
  154. };
  155. string manifest;
  156. using (var manifestReader =
  157. new StreamReader(
  158. selfMeta.Assembly.GetManifestResourceStream(typeof(PluginLoader), "manifest.json") ??
  159. throw new InvalidOperationException()))
  160. manifest = manifestReader.ReadToEnd();
  161. selfMeta.Manifest = JsonConvert.DeserializeObject<PluginManifest>(manifest);
  162. PluginsMetadata.Add(selfMeta);
  163. }
  164. catch (Exception e)
  165. {
  166. Logger.loader.Critical("Error loading own manifest");
  167. Logger.loader.Critical(e);
  168. }
  169. foreach (var plugin in plugins)
  170. {
  171. var metadata = new PluginMetadata
  172. {
  173. File = new FileInfo(Path.Combine(BeatSaber.PluginsPath, plugin)),
  174. IsSelf = false
  175. };
  176. try
  177. {
  178. var pluginModule = AssemblyDefinition.ReadAssembly(plugin, new ReaderParameters
  179. {
  180. ReadingMode = ReadingMode.Immediate,
  181. ReadWrite = false,
  182. AssemblyResolver = new CecilLibLoader()
  183. }).MainModule;
  184. string pluginNs = "";
  185. foreach (var resource in pluginModule.Resources)
  186. {
  187. const string manifestSuffix = ".manifest.json";
  188. if (!(resource is EmbeddedResource embedded) ||
  189. !embedded.Name.EndsWith(manifestSuffix)) continue;
  190. pluginNs = embedded.Name.Substring(0, embedded.Name.Length - manifestSuffix.Length);
  191. string manifest;
  192. using (var manifestReader = new StreamReader(embedded.GetResourceStream()))
  193. manifest = manifestReader.ReadToEnd();
  194. metadata.Manifest = JsonConvert.DeserializeObject<PluginManifest>(manifest);
  195. break;
  196. }
  197. if (metadata.Manifest == null)
  198. {
  199. #if DIRE_LOADER_WARNINGS
  200. Logger.loader.Error($"Could not find manifest.json for {Path.GetFileName(plugin)}");
  201. #else
  202. Logger.loader.Notice($"No manifest.json in {Path.GetFileName(plugin)}");
  203. #endif
  204. continue;
  205. }
  206. void TryGetNamespacedPluginType(string ns, PluginMetadata meta)
  207. {
  208. foreach (var type in pluginModule.Types)
  209. {
  210. if (type.Namespace != ns) continue;
  211. if (type.HasCustomAttributes)
  212. {
  213. var attr = type.CustomAttributes.FirstOrDefault(a => a.Constructor.DeclaringType.FullName == typeof(PluginAttribute).FullName);
  214. if (attr != null)
  215. {
  216. if (!attr.HasConstructorArguments)
  217. {
  218. Logger.loader.Warn($"Attribute plugin found in {type.FullName}, but attribute has no arguments");
  219. return;
  220. }
  221. var args = attr.ConstructorArguments;
  222. if (args.Count != 1)
  223. {
  224. Logger.loader.Warn($"Attribute plugin found in {type.FullName}, but attribute has unexpected number of arguments");
  225. return;
  226. }
  227. var rtOptionsArg = args[0];
  228. if (rtOptionsArg.Type.FullName != typeof(RuntimeOptions).FullName)
  229. {
  230. Logger.loader.Warn($"Attribute plugin found in {type.FullName}, but first argument is of unexpected type {rtOptionsArg.Type.FullName}");
  231. return;
  232. }
  233. var rtOptionsValInt = (int)rtOptionsArg.Value; // `int` is the underlying type of RuntimeOptions
  234. meta.RuntimeOptions = (RuntimeOptions)rtOptionsValInt;
  235. meta.IsAttributePlugin = true;
  236. meta.PluginType = type;
  237. return;
  238. }
  239. }
  240. if (type.HasInterface(typeof(IPlugin).FullName))
  241. {
  242. Logger.loader.Warn("Interface-based plugin found");
  243. meta.RuntimeOptions = RuntimeOptions.SingleDynamicInit;
  244. meta.PluginType = type;
  245. return;
  246. }
  247. }
  248. }
  249. var hint = metadata.Manifest.Misc?.PluginMainHint;
  250. if (hint != null)
  251. {
  252. var type = pluginModule.GetType(hint);
  253. if (type != null)
  254. TryGetNamespacedPluginType(hint, metadata);
  255. }
  256. if (metadata.PluginType == null)
  257. TryGetNamespacedPluginType(pluginNs, metadata);
  258. if (metadata.PluginType == null)
  259. {
  260. Logger.loader.Error($"No plugin found in the manifest {(hint != null ? $"hint path ({hint}) or " : "")}namespace ({pluginNs}) in {Path.GetFileName(plugin)}");
  261. continue;
  262. }
  263. Logger.loader.Debug($"Adding info for {Path.GetFileName(plugin)}");
  264. PluginsMetadata.Add(metadata);
  265. }
  266. catch (Exception e)
  267. {
  268. Logger.loader.Error($"Could not load data for plugin {Path.GetFileName(plugin)}");
  269. Logger.loader.Error(e);
  270. ignoredPlugins.Add(metadata, new IgnoreReason(Reason.Error)
  271. {
  272. ReasonText = "An error ocurred loading the data",
  273. Error = e
  274. });
  275. }
  276. }
  277. IEnumerable<string> bareManifests = Directory.GetFiles(BeatSaber.PluginsPath, "*.json");
  278. bareManifests = bareManifests.Concat(Directory.GetFiles(BeatSaber.PluginsPath, "*.manifest"));
  279. foreach (var manifest in bareManifests)
  280. { // TODO: maybe find a way to allow a bare manifest to specify an associated file
  281. try
  282. {
  283. var metadata = new PluginMetadata
  284. {
  285. File = new FileInfo(Path.Combine(BeatSaber.PluginsPath, manifest)),
  286. IsSelf = false,
  287. IsBare = true,
  288. };
  289. metadata.Manifest = JsonConvert.DeserializeObject<PluginManifest>(File.ReadAllText(manifest));
  290. Logger.loader.Debug($"Adding info for bare manifest {Path.GetFileName(manifest)}");
  291. PluginsMetadata.Add(metadata);
  292. }
  293. catch (Exception e)
  294. {
  295. Logger.loader.Error($"Could not load data for bare manifest {Path.GetFileName(manifest)}");
  296. Logger.loader.Error(e);
  297. }
  298. }
  299. foreach (var meta in PluginsMetadata)
  300. { // process description include
  301. var lines = meta.Manifest.Description.Split('\n');
  302. var m = embeddedTextDescriptionPattern.Match(lines[0]);
  303. if (m.Success)
  304. {
  305. if (meta.IsBare)
  306. {
  307. Logger.loader.Warn($"Bare manifest cannot specify description file");
  308. meta.Manifest.Description = string.Join("\n", lines.Skip(1).StrJP()); // ignore first line
  309. continue;
  310. }
  311. var name = m.Groups[1].Value;
  312. string description;
  313. if (!meta.IsSelf)
  314. {
  315. var resc = meta.PluginType.Module.Resources.Select(r => r as EmbeddedResource)
  316. .NonNull()
  317. .FirstOrDefault(r => r.Name == name);
  318. if (resc == null)
  319. {
  320. Logger.loader.Warn($"Could not find description file for plugin {meta.Name} ({name}); ignoring include");
  321. meta.Manifest.Description = string.Join("\n", lines.Skip(1).StrJP()); // ignore first line
  322. continue;
  323. }
  324. using var reader = new StreamReader(resc.GetResourceStream());
  325. description = reader.ReadToEnd();
  326. }
  327. else
  328. {
  329. using var descriptionReader = new StreamReader(meta.Assembly.GetManifestResourceStream(name));
  330. description = descriptionReader.ReadToEnd();
  331. }
  332. meta.Manifest.Description = description;
  333. }
  334. }
  335. }
  336. internal enum Reason
  337. {
  338. Error, Duplicate, Conflict, Dependency,
  339. Released, Feature, Unsupported
  340. }
  341. internal struct IgnoreReason
  342. {
  343. public Reason Reason { get; }
  344. public string ReasonText { get; set; }
  345. public Exception Error { get; set; }
  346. public PluginMetadata RelatedTo { get; set; }
  347. public IgnoreReason(Reason reason)
  348. {
  349. Reason = reason;
  350. ReasonText = null;
  351. Error = null;
  352. RelatedTo = null;
  353. }
  354. }
  355. // keep track of these for the updater; it should still be able to update mods not loaded
  356. // the thing -> the reason
  357. internal static Dictionary<PluginMetadata, IgnoreReason> ignoredPlugins = new Dictionary<PluginMetadata, IgnoreReason>();
  358. internal static void Resolve()
  359. { // resolves duplicates and conflicts, etc
  360. PluginsMetadata.Sort((a, b) => b.Version.CompareTo(a.Version));
  361. var ids = new HashSet<string>();
  362. var ignore = new Dictionary<PluginMetadata, IgnoreReason>();
  363. var resolved = new List<PluginMetadata>(PluginsMetadata.Count);
  364. foreach (var meta in PluginsMetadata)
  365. {
  366. if (meta.Id != null)
  367. {
  368. if (ids.Contains(meta.Id))
  369. {
  370. Logger.loader.Warn($"Found duplicates of {meta.Id}, using newest");
  371. var ireason = new IgnoreReason(Reason.Duplicate)
  372. {
  373. ReasonText = $"Duplicate entry of same ID ({meta.Id})",
  374. RelatedTo = resolved.First(p => p.Id == meta.Id)
  375. };
  376. ignore.Add(meta, ireason);
  377. ignoredPlugins.Add(meta, ireason);
  378. continue; // because of sorted order, hightest order will always be the first one
  379. }
  380. bool processedLater = false;
  381. foreach (var meta2 in PluginsMetadata)
  382. {
  383. if (ignore.ContainsKey(meta2)) continue;
  384. if (meta == meta2)
  385. {
  386. processedLater = true;
  387. continue;
  388. }
  389. if (!meta2.Manifest.Conflicts.ContainsKey(meta.Id)) continue;
  390. var range = meta2.Manifest.Conflicts[meta.Id];
  391. if (!range.IsSatisfied(meta.Version)) continue;
  392. Logger.loader.Warn($"{meta.Id}@{meta.Version} conflicts with {meta2.Id}");
  393. if (processedLater)
  394. {
  395. Logger.loader.Warn($"Ignoring {meta2.Name}");
  396. ignore.Add(meta2, new IgnoreReason(Reason.Conflict)
  397. {
  398. ReasonText = $"{meta.Id}@{meta.Version} conflicts with {meta2.Id}",
  399. RelatedTo = meta
  400. });
  401. }
  402. else
  403. {
  404. Logger.loader.Warn($"Ignoring {meta.Name}");
  405. ignore.Add(meta, new IgnoreReason(Reason.Conflict)
  406. {
  407. ReasonText = $"{meta2.Id}@{meta2.Version} conflicts with {meta.Id}",
  408. RelatedTo = meta2
  409. });
  410. break;
  411. }
  412. }
  413. }
  414. if (ignore.TryGetValue(meta, out var reason))
  415. {
  416. ignoredPlugins.Add(meta, reason);
  417. continue;
  418. }
  419. if (meta.Id != null)
  420. ids.Add(meta.Id);
  421. resolved.Add(meta);
  422. }
  423. PluginsMetadata = resolved;
  424. }
  425. private static void FilterDisabled()
  426. {
  427. var enabled = new List<PluginMetadata>(PluginsMetadata.Count);
  428. var disabled = DisabledConfig.Instance.DisabledModIds;
  429. foreach (var meta in PluginsMetadata)
  430. {
  431. if (disabled.Contains(meta.Id ?? meta.Name))
  432. DisabledPlugins.Add(meta);
  433. else
  434. enabled.Add(meta);
  435. }
  436. PluginsMetadata = enabled;
  437. }
  438. internal static void ComputeLoadOrder()
  439. {
  440. #if DEBUG
  441. Logger.loader.Debug(string.Join(", ", PluginsMetadata.Select(p => p.ToString()).StrJP()));
  442. #endif
  443. static bool InsertInto(HashSet<PluginMetadata> root, PluginMetadata meta, bool isRoot = false)
  444. { // this is slow, and hella recursive
  445. bool inserted = false;
  446. foreach (var sr in root)
  447. {
  448. inserted = inserted || InsertInto(sr.Dependencies, meta);
  449. if (meta.Id != null)
  450. if (sr.Manifest.Dependencies.ContainsKey(meta.Id) || sr.Manifest.LoadAfter.Contains(meta.Id))
  451. inserted = inserted || sr.Dependencies.Add(meta);
  452. if (sr.Id != null)
  453. if (meta.Manifest.LoadBefore.Contains(sr.Id))
  454. inserted = inserted || sr.Dependencies.Add(meta);
  455. }
  456. if (isRoot)
  457. {
  458. foreach (var sr in root)
  459. {
  460. InsertInto(meta.Dependencies, sr);
  461. if (sr.Id != null)
  462. if (meta.Manifest.Dependencies.ContainsKey(sr.Id) || meta.Manifest.LoadAfter.Contains(sr.Id))
  463. meta.Dependencies.Add(sr);
  464. if (meta.Id != null)
  465. if (sr.Manifest.LoadBefore.Contains(meta.Id))
  466. meta.Dependencies.Add(sr);
  467. }
  468. root.Add(meta);
  469. }
  470. return inserted;
  471. }
  472. var pluginTree = new HashSet<PluginMetadata>();
  473. foreach (var meta in PluginsMetadata)
  474. InsertInto(pluginTree, meta, true);
  475. static void DeTree(List<PluginMetadata> into, HashSet<PluginMetadata> tree)
  476. {
  477. foreach (var st in tree)
  478. if (!into.Contains(st))
  479. {
  480. DeTree(into, st.Dependencies);
  481. into.Add(st);
  482. }
  483. }
  484. PluginsMetadata = new List<PluginMetadata>();
  485. DeTree(PluginsMetadata, pluginTree);
  486. #if DEBUG
  487. Logger.loader.Debug(string.Join(", ", PluginsMetadata.Select(p => p.ToString()).StrJP()));
  488. #endif
  489. }
  490. internal static void ResolveDependencies()
  491. {
  492. var metadata = new List<PluginMetadata>();
  493. var pluginsToLoad = new Dictionary<string, Version>();
  494. var disabledLookup = DisabledPlugins.NonNull(m => m.Id).ToDictionary(m => m.Id, m => m.Version);
  495. foreach (var meta in PluginsMetadata)
  496. {
  497. var missingDeps = new List<(string id, Range version, bool disabled)>();
  498. foreach (var dep in meta.Manifest.Dependencies)
  499. {
  500. #if DEBUG
  501. Logger.loader.Debug($"Looking for dependency {dep.Key} with version range {dep.Value.Intersect(new SemVer.Range("*.*.*"))}");
  502. #endif
  503. if (pluginsToLoad.ContainsKey(dep.Key) && dep.Value.IsSatisfied(pluginsToLoad[dep.Key]))
  504. continue;
  505. if (disabledLookup.ContainsKey(dep.Key) && dep.Value.IsSatisfied(disabledLookup[dep.Key]))
  506. {
  507. Logger.loader.Warn($"Dependency {dep.Key} was found, but disabled. Disabling {meta.Name} too.");
  508. missingDeps.Add((dep.Key, dep.Value, true));
  509. }
  510. else
  511. {
  512. Logger.loader.Warn($"{meta.Name} is missing dependency {dep.Key}@{dep.Value}");
  513. missingDeps.Add((dep.Key, dep.Value, false));
  514. }
  515. }
  516. if (missingDeps.Count == 0)
  517. {
  518. metadata.Add(meta);
  519. if (meta.Id != null)
  520. pluginsToLoad.Add(meta.Id, meta.Version);
  521. }
  522. else if (missingDeps.Any(t => !t.disabled))
  523. { // missing deps
  524. ignoredPlugins.Add(meta, new IgnoreReason(Reason.Dependency)
  525. {
  526. ReasonText = $"Missing dependencies {string.Join(", ", missingDeps.Where(t => !t.disabled).Select(t => $"{t.id}@{t.version}").StrJP())}"
  527. });
  528. }
  529. else
  530. {
  531. DisabledPlugins.Add(meta);
  532. DisabledConfig.Instance.DisabledModIds.Add(meta.Id ?? meta.Name);
  533. }
  534. }
  535. PluginsMetadata = metadata;
  536. }
  537. internal static void InitFeatures()
  538. {
  539. var parsedFeatures = PluginsMetadata.Select(m =>
  540. (metadata: m,
  541. features: m.Manifest.Features.Select(feature =>
  542. (feature, parsed: Ref.Create<Feature.FeatureParse?>(null))
  543. ).ToList()
  544. )
  545. ).ToList();
  546. while (DefineFeature.NewFeature)
  547. {
  548. DefineFeature.NewFeature = false;
  549. foreach (var (metadata, features) in parsedFeatures)
  550. for (var i = 0; i < features.Count; i++)
  551. {
  552. var feature = features[i];
  553. var success = Feature.TryParseFeature(feature.feature, metadata, out var featureObj,
  554. out var exception, out var valid, out var parsed, feature.parsed.Value);
  555. if (!success && !valid && featureObj == null && exception == null) // no feature of type found
  556. feature.parsed.Value = parsed;
  557. else if (success)
  558. {
  559. if (valid && featureObj.StoreOnPlugin)
  560. metadata.InternalFeatures.Add(featureObj);
  561. else if (!valid)
  562. Logger.features.Warn(
  563. $"Feature not valid on {metadata.Name}: {featureObj.InvalidMessage}");
  564. features.RemoveAt(i--);
  565. }
  566. else
  567. {
  568. Logger.features.Error($"Error parsing feature definition on {metadata.Name}");
  569. Logger.features.Error(exception);
  570. features.RemoveAt(i--);
  571. }
  572. }
  573. foreach (var plugin in PluginsMetadata)
  574. foreach (var feature in plugin.Features)
  575. feature.Evaluate();
  576. }
  577. foreach (var plugin in parsedFeatures)
  578. {
  579. if (plugin.features.Count <= 0) continue;
  580. Logger.features.Warn($"On plugin {plugin.metadata.Name}:");
  581. foreach (var feature in plugin.features)
  582. Logger.features.Warn($" Feature not found with name {feature.feature}");
  583. }
  584. }
  585. internal static void ReleaseAll(bool full = false)
  586. {
  587. if (full)
  588. ignoredPlugins = new Dictionary<PluginMetadata, IgnoreReason>();
  589. else
  590. {
  591. foreach (var m in PluginsMetadata)
  592. ignoredPlugins.Add(m, new IgnoreReason(Reason.Released));
  593. foreach (var m in ignoredPlugins.Keys)
  594. { // clean them up so we can still use the metadata for updates
  595. m.InternalFeatures.Clear();
  596. m.PluginType = null;
  597. m.Assembly = null;
  598. }
  599. }
  600. PluginsMetadata = new List<PluginMetadata>();
  601. DisabledPlugins = new List<PluginMetadata>();
  602. Feature.Reset();
  603. GC.Collect();
  604. }
  605. internal static void Load(PluginMetadata meta)
  606. {
  607. if (meta.Assembly == null && meta.PluginType != null)
  608. meta.Assembly = Assembly.LoadFrom(meta.File.FullName);
  609. }
  610. internal static PluginInfo InitPlugin(PluginMetadata meta, IEnumerable<PluginMetadata> alreadyLoaded)
  611. {
  612. if (meta.IsAttributePlugin)
  613. {
  614. ignoredPlugins.Add(meta, new IgnoreReason(Reason.Unsupported) { ReasonText = "Attribute plugins are currently not supported" });
  615. return null;
  616. }
  617. if (meta.PluginType == null)
  618. return new PluginInfo()
  619. {
  620. Metadata = meta,
  621. Plugin = null
  622. };
  623. var info = new PluginInfo();
  624. if (meta.Manifest.GameVersion != BeatSaber.GameVersion)
  625. Logger.loader.Warn($"Mod {meta.Name} developed for game version {meta.Manifest.GameVersion}, so it may not work properly.");
  626. try
  627. {
  628. foreach (var dep in meta.Dependencies)
  629. {
  630. if (alreadyLoaded.Contains(dep)) continue;
  631. // otherwise...
  632. if (ignoredPlugins.TryGetValue(dep, out var reason))
  633. { // was added to the ignore list
  634. ignoredPlugins.Add(meta, new IgnoreReason(Reason.Dependency)
  635. {
  636. ReasonText = $"Dependency was ignored at load time: {reason.ReasonText}",
  637. RelatedTo = dep
  638. });
  639. }
  640. else
  641. { // was not added to ignore list
  642. ignoredPlugins.Add(meta, new IgnoreReason(Reason.Dependency)
  643. {
  644. ReasonText = $"Dependency was not already loaded at load time, but was also not ignored",
  645. RelatedTo = dep
  646. });
  647. }
  648. return null;
  649. }
  650. Load(meta);
  651. Feature denyingFeature = null;
  652. if (!meta.Features.All(f => (denyingFeature = f).BeforeLoad(meta)))
  653. {
  654. Logger.loader.Warn(
  655. $"Feature {denyingFeature?.GetType()} denied plugin {meta.Name} from loading! {denyingFeature?.InvalidMessage}");
  656. ignoredPlugins.Add(meta, new IgnoreReason(Reason.Feature)
  657. {
  658. ReasonText = $"Denied in {nameof(Feature.BeforeLoad)} of feature {denyingFeature?.GetType()}:\n\t{denyingFeature?.InvalidMessage}"
  659. });
  660. return null;
  661. }
  662. var type = meta.Assembly.GetType(meta.PluginType.FullName);
  663. var instance = Activator.CreateInstance(type) as IPlugin;
  664. info.Metadata = meta;
  665. info.Plugin = instance;
  666. var init = type.GetMethod("Init", BindingFlags.Instance | BindingFlags.Public);
  667. if (init != null)
  668. {
  669. denyingFeature = null;
  670. if (!meta.Features.All(f => (denyingFeature = f).BeforeInit(info)))
  671. {
  672. Logger.loader.Warn(
  673. $"Feature {denyingFeature?.GetType()} denied plugin {meta.Name} from initializing! {denyingFeature?.InvalidMessage}");
  674. ignoredPlugins.Add(meta, new IgnoreReason(Reason.Feature)
  675. {
  676. ReasonText = $"Denied in {nameof(Feature.BeforeInit)} of feature {denyingFeature?.GetType()}:\n\t{denyingFeature?.InvalidMessage}"
  677. });
  678. return null;
  679. }
  680. PluginInitInjector.Inject(init, info);
  681. }
  682. foreach (var feature in meta.Features)
  683. try
  684. {
  685. feature.AfterInit(info, info.Plugin);
  686. }
  687. catch (Exception e)
  688. {
  689. Logger.loader.Critical($"Feature errored in {nameof(Feature.AfterInit)}: {e}");
  690. }
  691. /*try // TODO: move this out to after all plugins have been inited
  692. {
  693. instance.OnEnable();
  694. }
  695. catch (Exception e)
  696. {
  697. Logger.loader.Error($"Error occurred trying to enable {meta.Name}");
  698. Logger.loader.Error(e);
  699. return null; // is enable failure a full load failure?
  700. }*/
  701. }
  702. catch (AmbiguousMatchException)
  703. {
  704. Logger.loader.Critical($"Only one Init allowed per plugin (ambiguous match in {meta.Name})");
  705. Logger.loader.Critical("@Developer: you *really* should fix this");
  706. // not adding to ignoredPlugins here because this should only happen in a development context
  707. // if someone fucks this up on release thats on them
  708. return null;
  709. }
  710. catch (Exception e)
  711. {
  712. Logger.loader.Error($"Could not init plugin {meta.Name}: {e}");
  713. ignoredPlugins.Add(meta, new IgnoreReason(Reason.Error)
  714. {
  715. ReasonText = "Error ocurred while initializing",
  716. Error = e
  717. });
  718. return null;
  719. }
  720. return info;
  721. }
  722. internal static List<PluginInfo> LoadPlugins()
  723. {
  724. InitFeatures();
  725. DisabledPlugins.ForEach(Load); // make sure they get loaded into memory so their metadata and stuff can be read more easily
  726. var list = new List<PluginInfo>();
  727. var loaded = new HashSet<PluginMetadata>();
  728. foreach (var meta in PluginsMetadata)
  729. {
  730. var info = InitPlugin(meta, loaded);
  731. if (info != null)
  732. {
  733. list.Add(info);
  734. loaded.Add(meta);
  735. }
  736. }
  737. return list;
  738. }
  739. }
  740. }