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.

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