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.

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