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.

679 lines
26 KiB

  1. using IPA.Loader.Features;
  2. using IPA.Logging;
  3. using IPA.Utilities;
  4. using Mono.Cecil;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Text.RegularExpressions;
  12. using System.Threading.Tasks;
  13. using Version = SemVer.Version;
  14. namespace IPA.Loader
  15. {
  16. /// <summary>
  17. /// A type to manage the loading of plugins.
  18. /// </summary>
  19. public class PluginLoader
  20. {
  21. internal static Task LoadTask() => Task.Run(() =>
  22. {
  23. LoadMetadata();
  24. Resolve();
  25. ComputeLoadOrder();
  26. FilterDisabled();
  27. ResolveDependencies();
  28. });
  29. /// <summary>
  30. /// A class which describes a loaded plugin.
  31. /// </summary>
  32. public class PluginMetadata
  33. {
  34. /// <summary>
  35. /// The assembly the plugin was loaded from.
  36. /// </summary>
  37. /// <value>the loaded Assembly that contains the plugin main type</value>
  38. public Assembly Assembly { get; internal set; }
  39. /// <summary>
  40. /// The TypeDefinition for the main type of the plugin.
  41. /// </summary>
  42. /// <value>the Cecil definition for the plugin main type</value>
  43. public TypeDefinition PluginType { get; internal set; }
  44. /// <summary>
  45. /// The human readable name of the plugin.
  46. /// </summary>
  47. /// <value>the name of the plugin</value>
  48. public string Name { get; internal set; }
  49. /// <summary>
  50. /// The BeatMods ID of the plugin, or null if it doesn't have one.
  51. /// </summary>
  52. /// <value>the updater ID of the plugin</value>
  53. public string Id { get; internal set; }
  54. /// <summary>
  55. /// The version of the plugin.
  56. /// </summary>
  57. /// <value>the version of the plugin</value>
  58. public Version Version { get; internal set; }
  59. /// <summary>
  60. /// The file the plugin was loaded from.
  61. /// </summary>
  62. /// <value>the file the plugin was loaded from</value>
  63. public FileInfo File { get; internal set; }
  64. // ReSharper disable once UnusedAutoPropertyAccessor.Global
  65. /// <summary>
  66. /// The features this plugin requests.
  67. /// </summary>
  68. /// <value>the list of features requested by the plugin</value>
  69. public IReadOnlyList<Feature> Features => InternalFeatures;
  70. internal readonly List<Feature> InternalFeatures = new List<Feature>();
  71. internal bool IsSelf;
  72. /// <summary>
  73. /// Whether or not this metadata object represents a bare manifest.
  74. /// </summary>
  75. /// <value><see langword="true"/> if it is bare, <see langword="false"/> otherwise</value>
  76. public bool IsBare { get; internal set; }
  77. private PluginManifest manifest;
  78. internal HashSet<PluginMetadata> Dependencies { get; } = new HashSet<PluginMetadata>();
  79. internal PluginManifest Manifest
  80. {
  81. get => manifest;
  82. set
  83. {
  84. manifest = value;
  85. Name = value.Name;
  86. Version = value.Version;
  87. Id = value.Id;
  88. }
  89. }
  90. /// <summary>
  91. /// Gets all of the metadata as a readable string.
  92. /// </summary>
  93. /// <returns>the readable printable metadata string</returns>
  94. public override string ToString() => $"{Name}({Id}@{Version})({PluginType?.FullName}) from '{Utils.GetRelativePath(File?.FullName, BeatSaber.InstallPath)}'";
  95. }
  96. /// <summary>
  97. /// A container object for all the data relating to a plugin.
  98. /// </summary>
  99. public class PluginInfo
  100. {
  101. internal IBeatSaberPlugin Plugin { get; set; }
  102. /// <summary>
  103. /// Metadata for the plugin.
  104. /// </summary>
  105. /// <value>the metadata for this plugin</value>
  106. public PluginMetadata Metadata { get; internal set; } = new PluginMetadata();
  107. }
  108. internal static List<PluginMetadata> PluginsMetadata = new List<PluginMetadata>();
  109. internal static List<PluginMetadata> DisabledPlugins = new List<PluginMetadata>();
  110. private static readonly Regex embeddedTextDescriptionPattern = new Regex(@"#!\[(.+)\]", RegexOptions.Compiled | RegexOptions.Singleline);
  111. internal static void LoadMetadata()
  112. {
  113. string[] plugins = Directory.GetFiles(BeatSaber.PluginsPath, "*.dll");
  114. try
  115. {
  116. var selfMeta = new PluginMetadata
  117. {
  118. Assembly = Assembly.GetExecutingAssembly(),
  119. File = new FileInfo(Path.Combine(BeatSaber.InstallPath, "IPA.exe")),
  120. PluginType = null,
  121. IsSelf = true
  122. };
  123. string manifest;
  124. using (var manifestReader =
  125. new StreamReader(
  126. selfMeta.Assembly.GetManifestResourceStream(typeof(PluginLoader), "manifest.json") ??
  127. throw new InvalidOperationException()))
  128. manifest = manifestReader.ReadToEnd();
  129. selfMeta.Manifest = JsonConvert.DeserializeObject<PluginManifest>(manifest);
  130. PluginsMetadata.Add(selfMeta);
  131. }
  132. catch (Exception e)
  133. {
  134. Logger.loader.Critical("Error loading own manifest");
  135. Logger.loader.Critical(e);
  136. }
  137. foreach (var plugin in plugins)
  138. {
  139. try
  140. {
  141. var metadata = new PluginMetadata
  142. {
  143. File = new FileInfo(Path.Combine(BeatSaber.PluginsPath, plugin)),
  144. IsSelf = false
  145. };
  146. var pluginModule = AssemblyDefinition.ReadAssembly(plugin, new ReaderParameters
  147. {
  148. ReadingMode = ReadingMode.Immediate,
  149. ReadWrite = false,
  150. AssemblyResolver = new CecilLibLoader()
  151. }).MainModule;
  152. var iBeatSaberPlugin = pluginModule.ImportReference(typeof(IBeatSaberPlugin));
  153. foreach (var type in pluginModule.Types)
  154. {
  155. foreach (var inter in type.Interfaces)
  156. {
  157. var ifType = inter.InterfaceType;
  158. if (iBeatSaberPlugin.FullName == ifType.FullName)
  159. {
  160. metadata.PluginType = type;
  161. break;
  162. }
  163. }
  164. if (metadata.PluginType != null) break;
  165. }
  166. if (metadata.PluginType == null)
  167. {
  168. Logger.loader.Notice(
  169. #if DIRE_LOADER_WARNINGS
  170. $"Could not find plugin type for {Path.GetFileName(plugin)}"
  171. #else
  172. $"New plugin type not present in {Path.GetFileName(plugin)}; maybe an old plugin?"
  173. #endif
  174. );
  175. continue;
  176. }
  177. foreach (var resource in pluginModule.Resources)
  178. {
  179. if (!(resource is EmbeddedResource embedded) ||
  180. embedded.Name != $"{metadata.PluginType.Namespace}.manifest.json") continue;
  181. string manifest;
  182. using (var manifestReader = new StreamReader(embedded.GetResourceStream()))
  183. manifest = manifestReader.ReadToEnd();
  184. metadata.Manifest = JsonConvert.DeserializeObject<PluginManifest>(manifest);
  185. break;
  186. }
  187. if (metadata.Manifest == null)
  188. {
  189. Logger.loader.Error("Could not find manifest.json in namespace " +
  190. $"{metadata.PluginType.Namespace} for {Path.GetFileName(plugin)}");
  191. continue;
  192. }
  193. Logger.loader.Debug($"Adding info for {Path.GetFileName(plugin)}");
  194. PluginsMetadata.Add(metadata);
  195. }
  196. catch (Exception e)
  197. {
  198. Logger.loader.Error($"Could not load data for plugin {Path.GetFileName(plugin)}");
  199. Logger.loader.Error(e);
  200. }
  201. }
  202. IEnumerable<string> bareManifests = Directory.GetFiles(BeatSaber.PluginsPath, "*.json");
  203. bareManifests = bareManifests.Concat(Directory.GetFiles(BeatSaber.PluginsPath, "*.manifest"));
  204. foreach (var manifest in bareManifests)
  205. {
  206. try
  207. {
  208. var metadata = new PluginMetadata
  209. {
  210. File = new FileInfo(Path.Combine(BeatSaber.PluginsPath, manifest)),
  211. IsSelf = false,
  212. IsBare = true,
  213. };
  214. metadata.Manifest = JsonConvert.DeserializeObject<PluginManifest>(File.ReadAllText(manifest));
  215. Logger.loader.Debug($"Adding info for bare manifest {Path.GetFileName(manifest)}");
  216. PluginsMetadata.Add(metadata);
  217. }
  218. catch (Exception e)
  219. {
  220. Logger.loader.Error($"Could not load data for bare manifest {Path.GetFileName(manifest)}");
  221. Logger.loader.Error(e);
  222. }
  223. }
  224. foreach (var meta in PluginsMetadata)
  225. { // process description include
  226. var lines = meta.Manifest.Description.Split('\n');
  227. var m = embeddedTextDescriptionPattern.Match(lines[0]);
  228. if (m.Success)
  229. {
  230. if (meta.IsBare)
  231. {
  232. Logger.loader.Warn($"Bare manifest cannot specify description file");
  233. meta.Manifest.Description = string.Join("\n", lines.Skip(1)); // ignore first line
  234. continue;
  235. }
  236. var name = m.Groups[1].Value;
  237. string description;
  238. if (!meta.IsSelf)
  239. {
  240. var resc = meta.PluginType.Module.Resources.Select(r => r as EmbeddedResource)
  241. .Where(r => r != null)
  242. .FirstOrDefault(r => r.Name == name);
  243. if (resc == null)
  244. {
  245. Logger.loader.Warn($"Could not find description file for plugin {meta.Name} ({name}); ignoring include");
  246. meta.Manifest.Description = string.Join("\n", lines.Skip(1)); // ignore first line
  247. continue;
  248. }
  249. using (var reader = new StreamReader(resc.GetResourceStream()))
  250. description = reader.ReadToEnd();
  251. }
  252. else
  253. {
  254. using (var descriptionReader =
  255. new StreamReader(
  256. meta.Assembly.GetManifestResourceStream(name) ??
  257. throw new InvalidOperationException()))
  258. description = descriptionReader.ReadToEnd();
  259. }
  260. meta.Manifest.Description = description;
  261. }
  262. }
  263. }
  264. // keep track of these for the updater; it should still be able to update mods not loaded
  265. // TODO: add ignore reason
  266. internal static HashSet<PluginMetadata> ignoredPlugins = new HashSet<PluginMetadata>();
  267. internal static void Resolve()
  268. { // resolves duplicates and conflicts, etc
  269. PluginsMetadata.Sort((a, b) => b.Version.CompareTo(a.Version));
  270. var ids = new HashSet<string>();
  271. var ignore = new HashSet<PluginMetadata>();
  272. var resolved = new List<PluginMetadata>(PluginsMetadata.Count);
  273. foreach (var meta in PluginsMetadata)
  274. {
  275. if (meta.Id != null)
  276. {
  277. if (ids.Contains(meta.Id))
  278. {
  279. Logger.loader.Warn($"Found duplicates of {meta.Id}, using newest");
  280. ignore.Add(meta);
  281. ignoredPlugins.Add(meta);
  282. continue; // because of sorted order, hightest order will always be the first one
  283. }
  284. bool processedLater = false;
  285. foreach (var meta2 in PluginsMetadata)
  286. {
  287. if (ignore.Contains(meta2)) continue;
  288. if (meta == meta2)
  289. {
  290. processedLater = true;
  291. continue;
  292. }
  293. if (!meta2.Manifest.Conflicts.ContainsKey(meta.Id)) continue;
  294. var range = meta2.Manifest.Conflicts[meta.Id];
  295. if (!range.IsSatisfied(meta.Version)) continue;
  296. Logger.loader.Warn($"{meta.Id}@{meta.Version} conflicts with {meta2.Name}");
  297. if (processedLater)
  298. {
  299. Logger.loader.Warn($"Ignoring {meta2.Name}");
  300. ignore.Add(meta2);
  301. }
  302. else
  303. {
  304. Logger.loader.Warn($"Ignoring {meta.Name}");
  305. ignore.Add(meta);
  306. break;
  307. }
  308. }
  309. }
  310. if (ignore.Contains(meta))
  311. {
  312. ignoredPlugins.Add(meta);
  313. continue;
  314. }
  315. if (meta.Id != null)
  316. ids.Add(meta.Id);
  317. resolved.Add(meta);
  318. }
  319. PluginsMetadata = resolved;
  320. }
  321. private static void FilterDisabled()
  322. {
  323. var enabled = new List<PluginMetadata>(PluginsMetadata.Count);
  324. var disabled = DisabledConfig.Ref.Value.DisabledModIds;
  325. foreach (var meta in PluginsMetadata)
  326. {
  327. if (disabled.Contains(meta.Id ?? meta.Name))
  328. DisabledPlugins.Add(meta);
  329. else
  330. enabled.Add(meta);
  331. }
  332. PluginsMetadata = enabled;
  333. }
  334. internal static void ComputeLoadOrder()
  335. {
  336. #if DEBUG
  337. Logger.loader.Debug(string.Join(", ", PluginsMetadata.Select(p => p.ToString())));
  338. #endif
  339. bool InsertInto(HashSet<PluginMetadata> root, PluginMetadata meta, bool isRoot = false)
  340. { // this is slow, and hella recursive
  341. bool inserted = false;
  342. foreach (var sr in root)
  343. {
  344. inserted = inserted || InsertInto(sr.Dependencies, meta);
  345. if (meta.Id != null)
  346. if (sr.Manifest.Dependencies.ContainsKey(meta.Id) || sr.Manifest.LoadAfter.Contains(meta.Id))
  347. inserted = inserted || sr.Dependencies.Add(meta);
  348. if (sr.Id != null)
  349. if (meta.Manifest.LoadBefore.Contains(sr.Id))
  350. inserted = inserted || sr.Dependencies.Add(meta);
  351. }
  352. if (isRoot)
  353. {
  354. foreach (var sr in root)
  355. {
  356. InsertInto(meta.Dependencies, sr);
  357. if (sr.Id != null)
  358. if (meta.Manifest.Dependencies.ContainsKey(sr.Id) || meta.Manifest.LoadAfter.Contains(sr.Id))
  359. meta.Dependencies.Add(sr);
  360. if (meta.Id != null)
  361. if (sr.Manifest.LoadBefore.Contains(meta.Id))
  362. meta.Dependencies.Add(sr);
  363. }
  364. root.Add(meta);
  365. }
  366. return inserted;
  367. }
  368. var pluginTree = new HashSet<PluginMetadata>();
  369. foreach (var meta in PluginsMetadata)
  370. InsertInto(pluginTree, meta, true);
  371. void DeTree(List<PluginMetadata> into, HashSet<PluginMetadata> tree)
  372. {
  373. foreach (var st in tree)
  374. if (!into.Contains(st))
  375. {
  376. DeTree(into, st.Dependencies);
  377. into.Add(st);
  378. }
  379. }
  380. PluginsMetadata = new List<PluginMetadata>();
  381. DeTree(PluginsMetadata, pluginTree);
  382. #if DEBUG
  383. Logger.loader.Debug(string.Join(", ", PluginsMetadata.Select(p => p.ToString())));
  384. #endif
  385. }
  386. internal static void ResolveDependencies()
  387. {
  388. var metadata = new List<PluginMetadata>();
  389. var pluginsToLoad = new Dictionary<string, Version>();
  390. var disabledLookup = DisabledPlugins.Where(m => m.Id != null).ToDictionary(m => m.Id, m => m.Version);
  391. foreach (var meta in PluginsMetadata)
  392. {
  393. bool load = true;
  394. bool disable = false;
  395. foreach (var dep in meta.Manifest.Dependencies)
  396. {
  397. #if DEBUG
  398. Logger.loader.Debug($"Looking for dependency {dep.Key} with version range {dep.Value.Intersect(new SemVer.Range("*.*.*"))}");
  399. #endif
  400. if (pluginsToLoad.ContainsKey(dep.Key) && dep.Value.IsSatisfied(pluginsToLoad[dep.Key]))
  401. continue;
  402. load = false;
  403. if (disabledLookup.ContainsKey(dep.Key) && dep.Value.IsSatisfied(disabledLookup[dep.Key]))
  404. {
  405. disable = true;
  406. Logger.loader.Warn($"Dependency {dep.Key} was found, but disabled. Disabling {meta.Name} too.");
  407. }
  408. else
  409. Logger.loader.Warn($"{meta.Name} is missing dependency {dep.Key}@{dep.Value}");
  410. break;
  411. }
  412. if (load)
  413. {
  414. metadata.Add(meta);
  415. if (meta.Id != null)
  416. pluginsToLoad.Add(meta.Id, meta.Version);
  417. }
  418. else if (disable)
  419. {
  420. DisabledPlugins.Add(meta);
  421. DisabledConfig.Ref.Value.DisabledModIds.Add(meta.Id ?? meta.Name);
  422. }
  423. else
  424. ignoredPlugins.Add(meta);
  425. }
  426. PluginsMetadata = metadata;
  427. }
  428. internal static void InitFeatures()
  429. {
  430. var parsedFeatures = PluginsMetadata.Select(m =>
  431. Tuple.Create(m,
  432. m.Manifest.Features.Select(f =>
  433. Tuple.Create(f, Ref.Create<Feature.FeatureParse?>(null))
  434. ).ToList()
  435. )
  436. ).ToList();
  437. while (DefineFeature.NewFeature)
  438. {
  439. DefineFeature.NewFeature = false;
  440. foreach (var plugin in parsedFeatures)
  441. for (var i = 0; i < plugin.Item2.Count; i++)
  442. {
  443. var feature = plugin.Item2[i];
  444. var success = Feature.TryParseFeature(feature.Item1, plugin.Item1, out var featureObj,
  445. out var exception, out var valid, out var parsed, feature.Item2.Value);
  446. if (!success && !valid && featureObj == null && exception == null) // no feature of type found
  447. feature.Item2.Value = parsed;
  448. else if (success)
  449. {
  450. if (valid && featureObj.StoreOnPlugin)
  451. plugin.Item1.InternalFeatures.Add(featureObj);
  452. else if (!valid)
  453. Logger.features.Warn(
  454. $"Feature not valid on {plugin.Item1.Name}: {featureObj.InvalidMessage}");
  455. plugin.Item2.RemoveAt(i--);
  456. }
  457. else
  458. {
  459. Logger.features.Error($"Error parsing feature definition on {plugin.Item1.Name}");
  460. Logger.features.Error(exception);
  461. plugin.Item2.RemoveAt(i--);
  462. }
  463. }
  464. foreach (var plugin in PluginsMetadata)
  465. foreach (var feature in plugin.Features)
  466. feature.Evaluate();
  467. }
  468. foreach (var plugin in parsedFeatures)
  469. {
  470. if (plugin.Item2.Count <= 0) continue;
  471. Logger.features.Warn($"On plugin {plugin.Item1.Name}:");
  472. foreach (var feature in plugin.Item2)
  473. Logger.features.Warn($" Feature not found with name {feature.Item1}");
  474. }
  475. }
  476. internal static void ReleaseAll(bool full = false)
  477. {
  478. if (full)
  479. ignoredPlugins = new HashSet<PluginMetadata>();
  480. else
  481. {
  482. foreach (var m in PluginsMetadata)
  483. ignoredPlugins.Add(m);
  484. foreach (var m in ignoredPlugins)
  485. { // clean them up so we can still use the metadata for updates
  486. m.InternalFeatures.Clear();
  487. m.PluginType = null;
  488. m.Assembly = null;
  489. }
  490. }
  491. PluginsMetadata = new List<PluginMetadata>();
  492. DisabledPlugins = new List<PluginMetadata>();
  493. Feature.Reset();
  494. GC.Collect();
  495. }
  496. internal static void Load(PluginMetadata meta)
  497. {
  498. if (meta.Assembly == null && meta.PluginType != null)
  499. meta.Assembly = Assembly.LoadFrom(meta.File.FullName);
  500. }
  501. internal static PluginInfo InitPlugin(PluginMetadata meta)
  502. {
  503. if (meta.PluginType == null)
  504. return new PluginInfo()
  505. {
  506. Metadata = meta,
  507. Plugin = null
  508. };
  509. var info = new PluginInfo();
  510. if (meta.Manifest.GameVersion != BeatSaber.GameVersion)
  511. Logger.loader.Warn($"Mod {meta.Name} developed for game version {meta.Manifest.GameVersion}, so it may not work properly.");
  512. try
  513. {
  514. Load(meta);
  515. Feature denyingFeature = null;
  516. if (!meta.Features.All(f => (denyingFeature = f).BeforeLoad(meta)))
  517. {
  518. Logger.loader.Warn(
  519. $"Feature {denyingFeature?.GetType()} denied plugin {meta.Name} from loading! {denyingFeature?.InvalidMessage}");
  520. ignoredPlugins.Add(meta);
  521. return null;
  522. }
  523. var type = meta.Assembly.GetType(meta.PluginType.FullName);
  524. var instance = (IBeatSaberPlugin)Activator.CreateInstance(type);
  525. info.Metadata = meta;
  526. info.Plugin = instance;
  527. var init = type.GetMethod("Init", BindingFlags.Instance | BindingFlags.Public);
  528. if (init != null)
  529. {
  530. denyingFeature = null;
  531. if (!meta.Features.All(f => (denyingFeature = f).BeforeInit(info)))
  532. {
  533. Logger.loader.Warn(
  534. $"Feature {denyingFeature?.GetType()} denied plugin {meta.Name} from initializing! {denyingFeature?.InvalidMessage}");
  535. ignoredPlugins.Add(meta);
  536. return null;
  537. }
  538. PluginInitInjector.Inject(init, info);
  539. }
  540. foreach (var feature in meta.Features)
  541. try
  542. {
  543. feature.AfterInit(info, info.Plugin);
  544. }
  545. catch (Exception e)
  546. {
  547. Logger.loader.Critical($"Feature errored in {nameof(Feature.AfterInit)}: {e}");
  548. }
  549. if (instance is IDisablablePlugin disable)
  550. try // TODO: move this out to after all plugins have been inited
  551. {
  552. disable.OnEnable();
  553. }
  554. catch (Exception e)
  555. {
  556. Logger.loader.Error($"Error occurred trying to enable {meta.Name}");
  557. Logger.loader.Error(e);
  558. }
  559. }
  560. catch (AmbiguousMatchException)
  561. {
  562. Logger.loader.Error($"Only one Init allowed per plugin (ambiguous match in {meta.Name})");
  563. // not adding to ignoredPlugins here because this should only happen in a development context
  564. // if someone fucks this up on release thats on them
  565. return null;
  566. }
  567. catch (Exception e)
  568. {
  569. Logger.loader.Error($"Could not init plugin {meta.Name}: {e}");
  570. ignoredPlugins.Add(meta);
  571. return null;
  572. }
  573. return info;
  574. }
  575. internal static List<PluginInfo> LoadPlugins()
  576. {
  577. InitFeatures();
  578. DisabledPlugins.ForEach(Load); // make sure they get loaded into memory so their metadata and stuff can be read more easily
  579. return PluginsMetadata.Select(InitPlugin).Where(p => p != null).ToList();
  580. }
  581. }
  582. }