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