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.

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