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.

684 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. #endif
  19. namespace IPA.Loader
  20. {
  21. /// <summary>
  22. /// A type to manage the loading of plugins.
  23. /// </summary>
  24. public class PluginLoader
  25. {
  26. internal static Task LoadTask() => Task.Factory.StartNew(() =>
  27. {
  28. LoadMetadata();
  29. Resolve();
  30. ComputeLoadOrder();
  31. FilterDisabled();
  32. ResolveDependencies();
  33. });
  34. /// <summary>
  35. /// A class which describes a loaded plugin.
  36. /// </summary>
  37. public class PluginMetadata
  38. {
  39. /// <summary>
  40. /// The assembly the plugin was loaded from.
  41. /// </summary>
  42. /// <value>the loaded Assembly that contains the plugin main type</value>
  43. public Assembly Assembly { get; internal set; }
  44. /// <summary>
  45. /// The TypeDefinition for the main type of the plugin.
  46. /// </summary>
  47. /// <value>the Cecil definition for the plugin main type</value>
  48. public TypeDefinition PluginType { get; internal set; }
  49. /// <summary>
  50. /// The human readable name of the plugin.
  51. /// </summary>
  52. /// <value>the name of the plugin</value>
  53. public string Name { get; internal set; }
  54. /// <summary>
  55. /// The BeatMods ID of the plugin, or null if it doesn't have one.
  56. /// </summary>
  57. /// <value>the updater ID of the plugin</value>
  58. public string Id { get; internal set; }
  59. /// <summary>
  60. /// The version of the plugin.
  61. /// </summary>
  62. /// <value>the version of the plugin</value>
  63. public Version Version { get; internal set; }
  64. /// <summary>
  65. /// The file the plugin was loaded from.
  66. /// </summary>
  67. /// <value>the file the plugin was loaded from</value>
  68. public FileInfo File { get; internal set; }
  69. // ReSharper disable once UnusedAutoPropertyAccessor.Global
  70. /// <summary>
  71. /// The features this plugin requests.
  72. /// </summary>
  73. /// <value>the list of features requested by the plugin</value>
  74. public IReadOnlyList<Feature> Features => InternalFeatures;
  75. internal readonly List<Feature> InternalFeatures = new List<Feature>();
  76. internal bool IsSelf;
  77. /// <summary>
  78. /// Whether or not this metadata object represents a bare manifest.
  79. /// </summary>
  80. /// <value><see langword="true"/> if it is bare, <see langword="false"/> otherwise</value>
  81. public bool IsBare { get; internal set; }
  82. private PluginManifest manifest;
  83. internal HashSet<PluginMetadata> Dependencies { get; } = new HashSet<PluginMetadata>();
  84. internal PluginManifest Manifest
  85. {
  86. get => manifest;
  87. set
  88. {
  89. manifest = value;
  90. Name = value.Name;
  91. Version = value.Version;
  92. Id = value.Id;
  93. }
  94. }
  95. /// <summary>
  96. /// Gets all of the metadata as a readable string.
  97. /// </summary>
  98. /// <returns>the readable printable metadata string</returns>
  99. public override string ToString() => $"{Name}({Id}@{Version})({PluginType?.FullName}) from '{Utils.GetRelativePath(File?.FullName, BeatSaber.InstallPath)}'";
  100. }
  101. /// <summary>
  102. /// A container object for all the data relating to a plugin.
  103. /// </summary>
  104. public class PluginInfo
  105. {
  106. internal IBeatSaberPlugin Plugin { get; set; }
  107. /// <summary>
  108. /// Metadata for the plugin.
  109. /// </summary>
  110. /// <value>the metadata for this plugin</value>
  111. public PluginMetadata Metadata { get; internal set; } = new PluginMetadata();
  112. }
  113. internal static List<PluginMetadata> PluginsMetadata = new List<PluginMetadata>();
  114. internal static List<PluginMetadata> DisabledPlugins = new List<PluginMetadata>();
  115. private static readonly Regex embeddedTextDescriptionPattern = new Regex(@"#!\[(.+)\]", RegexOptions.Compiled | RegexOptions.Singleline);
  116. internal static void LoadMetadata()
  117. {
  118. string[] plugins = Directory.GetFiles(BeatSaber.PluginsPath, "*.dll");
  119. try
  120. {
  121. var selfMeta = new PluginMetadata
  122. {
  123. Assembly = Assembly.GetExecutingAssembly(),
  124. File = new FileInfo(Path.Combine(BeatSaber.InstallPath, "IPA.exe")),
  125. PluginType = null,
  126. IsSelf = true
  127. };
  128. string manifest;
  129. using (var manifestReader =
  130. new StreamReader(
  131. selfMeta.Assembly.GetManifestResourceStream(typeof(PluginLoader), "manifest.json") ??
  132. throw new InvalidOperationException()))
  133. manifest = manifestReader.ReadToEnd();
  134. selfMeta.Manifest = JsonConvert.DeserializeObject<PluginManifest>(manifest);
  135. PluginsMetadata.Add(selfMeta);
  136. }
  137. catch (Exception e)
  138. {
  139. Logger.loader.Critical("Error loading own manifest");
  140. Logger.loader.Critical(e);
  141. }
  142. foreach (var plugin in plugins)
  143. {
  144. try
  145. {
  146. var metadata = new PluginMetadata
  147. {
  148. File = new FileInfo(Path.Combine(BeatSaber.PluginsPath, plugin)),
  149. IsSelf = false
  150. };
  151. var pluginModule = AssemblyDefinition.ReadAssembly(plugin, new ReaderParameters
  152. {
  153. ReadingMode = ReadingMode.Immediate,
  154. ReadWrite = false,
  155. AssemblyResolver = new CecilLibLoader()
  156. }).MainModule;
  157. var iBeatSaberPlugin = pluginModule.ImportReference(typeof(IBeatSaberPlugin));
  158. foreach (var type in pluginModule.Types)
  159. {
  160. foreach (var inter in type.Interfaces)
  161. {
  162. var ifType = inter.InterfaceType;
  163. if (iBeatSaberPlugin.FullName == ifType.FullName)
  164. {
  165. metadata.PluginType = type;
  166. break;
  167. }
  168. }
  169. if (metadata.PluginType != null) break;
  170. }
  171. if (metadata.PluginType == null)
  172. {
  173. Logger.loader.Notice(
  174. #if DIRE_LOADER_WARNINGS
  175. $"Could not find plugin type for {Path.GetFileName(plugin)}"
  176. #else
  177. $"New plugin type not present in {Path.GetFileName(plugin)}; maybe an old plugin?"
  178. #endif
  179. );
  180. continue;
  181. }
  182. foreach (var resource in pluginModule.Resources)
  183. {
  184. if (!(resource is EmbeddedResource embedded) ||
  185. embedded.Name != $"{metadata.PluginType.Namespace}.manifest.json") continue;
  186. string manifest;
  187. using (var manifestReader = new StreamReader(embedded.GetResourceStream()))
  188. manifest = manifestReader.ReadToEnd();
  189. metadata.Manifest = JsonConvert.DeserializeObject<PluginManifest>(manifest);
  190. break;
  191. }
  192. if (metadata.Manifest == null)
  193. {
  194. Logger.loader.Error("Could not find manifest.json in namespace " +
  195. $"{metadata.PluginType.Namespace} for {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)); // 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)); // 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())));
  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())));
  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. }