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.

1025 lines
44 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #nullable enable
  2. using IPA.Config;
  3. using IPA.Loader.Features;
  4. using IPA.Logging;
  5. using IPA.Utilities;
  6. using Mono.Cecil;
  7. using Newtonsoft.Json;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Reflection;
  13. using System.Text.RegularExpressions;
  14. using System.Diagnostics.CodeAnalysis;
  15. using System.Diagnostics;
  16. using IPA.AntiMalware;
  17. using Hive.Versioning;
  18. #if NET4
  19. using Task = System.Threading.Tasks.Task;
  20. using TaskEx = System.Threading.Tasks.Task;
  21. #endif
  22. #if NET3
  23. using Net3_Proxy;
  24. using Path = Net3_Proxy.Path;
  25. using File = Net3_Proxy.File;
  26. using Directory = Net3_Proxy.Directory;
  27. #endif
  28. namespace IPA.Loader
  29. {
  30. /// <summary>
  31. /// A type to manage the loading of plugins.
  32. /// </summary>
  33. internal partial class PluginLoader
  34. {
  35. internal static PluginMetadata SelfMeta = null!;
  36. internal static Task LoadTask() =>
  37. TaskEx.Run(() =>
  38. {
  39. YeetIfNeeded();
  40. var sw = Stopwatch.StartNew();
  41. LoadMetadata();
  42. sw.Stop();
  43. Logger.Loader.Info($"Loading metadata took {sw.Elapsed}");
  44. sw.Reset();
  45. sw.Start();
  46. // Features contribute to load order considerations
  47. InitFeatures();
  48. DoOrderResolution();
  49. sw.Stop();
  50. Logger.Loader.Info($"Calculating load order took {sw.Elapsed}");
  51. });
  52. internal static void YeetIfNeeded()
  53. {
  54. string pluginDir = UnityGame.PluginsPath;
  55. if (SelfConfig.YeetMods_ && UnityGame.IsGameVersionBoundary)
  56. {
  57. var oldPluginsName = Path.Combine(UnityGame.InstallPath, $"Old {UnityGame.OldVersion} Plugins");
  58. var newPluginsName = Path.Combine(UnityGame.InstallPath, $"Old {UnityGame.GameVersion} Plugins");
  59. if (Directory.Exists(oldPluginsName))
  60. Directory.Delete(oldPluginsName, true);
  61. Directory.Move(pluginDir, oldPluginsName);
  62. if (Directory.Exists(newPluginsName))
  63. Directory.Move(newPluginsName, pluginDir);
  64. else
  65. _ = Directory.CreateDirectory(pluginDir);
  66. }
  67. }
  68. internal static List<PluginMetadata> PluginsMetadata = new();
  69. internal static List<PluginMetadata> DisabledPlugins = new();
  70. private static readonly Regex embeddedTextDescriptionPattern = new(@"#!\[(.+)\]", RegexOptions.Compiled | RegexOptions.Singleline);
  71. internal static void LoadMetadata()
  72. {
  73. string[] plugins = Directory.GetFiles(UnityGame.PluginsPath, "*.dll");
  74. try
  75. {
  76. var selfMeta = new PluginMetadata
  77. {
  78. Assembly = Assembly.GetExecutingAssembly(),
  79. File = new FileInfo(Path.Combine(UnityGame.InstallPath, "IPA.exe")),
  80. PluginType = null,
  81. IsSelf = true
  82. };
  83. string manifest;
  84. using (var manifestReader =
  85. new StreamReader(
  86. selfMeta.Assembly.GetManifestResourceStream(typeof(PluginLoader), "manifest.json") ??
  87. throw new InvalidOperationException()))
  88. manifest = manifestReader.ReadToEnd();
  89. selfMeta.Manifest = JsonConvert.DeserializeObject<PluginManifest>(manifest);
  90. PluginsMetadata.Add(selfMeta);
  91. SelfMeta = selfMeta;
  92. }
  93. catch (Exception e)
  94. {
  95. Logger.Loader.Critical("Error loading own manifest");
  96. Logger.Loader.Critical(e);
  97. }
  98. using var resolver = new CecilLibLoader();
  99. resolver.AddSearchDirectory(UnityGame.LibraryPath);
  100. resolver.AddSearchDirectory(UnityGame.PluginsPath);
  101. foreach (var plugin in plugins)
  102. {
  103. var metadata = new PluginMetadata
  104. {
  105. File = new FileInfo(Path.Combine(UnityGame.PluginsPath, plugin)),
  106. IsSelf = false
  107. };
  108. try
  109. {
  110. var scanResult = AntiMalwareEngine.Engine.ScanFile(metadata.File);
  111. if (scanResult is ScanResult.Detected)
  112. {
  113. Logger.Loader.Warn($"Scan of {plugin} found malware; not loading");
  114. continue;
  115. }
  116. if (!SelfConfig.AntiMalware_.RunPartialThreatCode_ && scanResult is not ScanResult.KnownSafe and not ScanResult.NotDetected)
  117. {
  118. Logger.Loader.Warn($"Scan of {plugin} found partial threat; not loading. To load this, set AntiMalware.RunPartialThreatCode in the config.");
  119. continue;
  120. }
  121. var pluginModule = AssemblyDefinition.ReadAssembly(metadata.File.FullName, new ReaderParameters
  122. {
  123. ReadingMode = ReadingMode.Immediate,
  124. ReadWrite = false,
  125. AssemblyResolver = resolver
  126. }).MainModule;
  127. string pluginNs = "";
  128. PluginManifest? pluginManifest = null;
  129. foreach (var resource in pluginModule.Resources)
  130. {
  131. const string manifestSuffix = ".manifest.json";
  132. if (resource is not EmbeddedResource embedded ||
  133. !embedded.Name.EndsWith(manifestSuffix, StringComparison.Ordinal)) continue;
  134. pluginNs = embedded.Name.Substring(0, embedded.Name.Length - manifestSuffix.Length);
  135. string manifest;
  136. using (var manifestReader = new StreamReader(embedded.GetResourceStream()))
  137. manifest = manifestReader.ReadToEnd();
  138. pluginManifest = JsonConvert.DeserializeObject<PluginManifest?>(manifest);
  139. break;
  140. }
  141. if (pluginManifest == null)
  142. {
  143. #if DIRE_LOADER_WARNINGS
  144. Logger.loader.Error($"Could not find manifest.json for {Path.GetFileName(plugin)}");
  145. #else
  146. Logger.Loader.Notice($"No manifest.json in {Path.GetFileName(plugin)}");
  147. #endif
  148. continue;
  149. }
  150. if (pluginManifest.Id == null)
  151. {
  152. Logger.Loader.Warn($"Plugin '{pluginManifest.Name}' does not have a listed ID, using name");
  153. pluginManifest.Id = pluginManifest.Name;
  154. }
  155. metadata.Manifest = pluginManifest;
  156. bool TryPopulatePluginType(TypeDefinition type, PluginMetadata meta)
  157. {
  158. if (!type.HasCustomAttributes)
  159. return false;
  160. var attr = type.CustomAttributes.FirstOrDefault(a => a.Constructor.DeclaringType.FullName == typeof(PluginAttribute).FullName);
  161. if (attr is null)
  162. return false;
  163. if (!attr.HasConstructorArguments)
  164. {
  165. Logger.Loader.Warn($"Attribute plugin found in {type.FullName}, but attribute has no arguments");
  166. return false;
  167. }
  168. var args = attr.ConstructorArguments;
  169. if (args.Count != 1)
  170. {
  171. Logger.Loader.Warn($"Attribute plugin found in {type.FullName}, but attribute has unexpected number of arguments");
  172. return false;
  173. }
  174. var rtOptionsArg = args[0];
  175. if (rtOptionsArg.Type.FullName != typeof(RuntimeOptions).FullName)
  176. {
  177. Logger.Loader.Warn($"Attribute plugin found in {type.FullName}, but first argument is of unexpected type {rtOptionsArg.Type.FullName}");
  178. return false;
  179. }
  180. var rtOptionsValInt = (int)rtOptionsArg.Value; // `int` is the underlying type of RuntimeOptions
  181. meta.RuntimeOptions = (RuntimeOptions)rtOptionsValInt;
  182. meta.PluginType = type;
  183. return true;
  184. }
  185. void TryGetNamespacedPluginType(string ns, PluginMetadata meta)
  186. {
  187. foreach (var type in pluginModule.Types)
  188. {
  189. if (type.Namespace != ns) continue;
  190. if (TryPopulatePluginType(type, meta))
  191. return;
  192. }
  193. }
  194. var hint = metadata.Manifest.Misc?.PluginMainHint;
  195. if (hint != null)
  196. {
  197. var type = pluginModule.GetType(hint);
  198. if (type == null || !TryPopulatePluginType(type, metadata))
  199. TryGetNamespacedPluginType(hint, metadata);
  200. }
  201. if (metadata.PluginType == null)
  202. TryGetNamespacedPluginType(pluginNs, metadata);
  203. if (metadata.PluginType == null)
  204. {
  205. Logger.Loader.Error($"No plugin found in the manifest {(hint != null ? $"hint path ({hint}) or " : "")}namespace ({pluginNs}) in {Path.GetFileName(plugin)}");
  206. continue;
  207. }
  208. Logger.Loader.Debug($"Adding info for {Path.GetFileName(plugin)}");
  209. PluginsMetadata.Add(metadata);
  210. }
  211. catch (Exception e)
  212. {
  213. Logger.Loader.Error($"Could not load data for plugin {Path.GetFileName(plugin)}");
  214. Logger.Loader.Error(e);
  215. ignoredPlugins.Add(metadata, new IgnoreReason(Reason.Error)
  216. {
  217. ReasonText = "An error occurred loading the data",
  218. Error = e
  219. });
  220. }
  221. }
  222. IEnumerable<string> bareManifests = Directory.GetFiles(UnityGame.PluginsPath, "*.json");
  223. bareManifests = bareManifests.Concat(Directory.GetFiles(UnityGame.PluginsPath, "*.manifest"));
  224. foreach (var manifest in bareManifests)
  225. {
  226. try
  227. {
  228. var metadata = new PluginMetadata
  229. {
  230. File = new FileInfo(Path.Combine(UnityGame.PluginsPath, manifest)),
  231. IsSelf = false,
  232. IsBare = true,
  233. };
  234. metadata.Manifest = JsonConvert.DeserializeObject<PluginManifest>(File.ReadAllText(manifest));
  235. if (metadata.Manifest.Files.Length < 1)
  236. Logger.Loader.Warn($"Bare manifest {Path.GetFileName(manifest)} does not declare any files. " +
  237. $"Dependency resolution and verification cannot be completed.");
  238. Logger.Loader.Debug($"Adding info for bare manifest {Path.GetFileName(manifest)}");
  239. PluginsMetadata.Add(metadata);
  240. }
  241. catch (Exception e)
  242. {
  243. Logger.Loader.Error($"Could not load data for bare manifest {Path.GetFileName(manifest)}");
  244. Logger.Loader.Error(e);
  245. }
  246. }
  247. foreach (var meta in PluginsMetadata)
  248. { // process description include
  249. var lines = meta.Manifest.Description.Split('\n');
  250. var m = embeddedTextDescriptionPattern.Match(lines[0]);
  251. if (m.Success)
  252. {
  253. if (meta.IsBare)
  254. {
  255. Logger.Loader.Warn($"Bare manifest cannot specify description file");
  256. meta.Manifest.Description = string.Join("\n", lines.Skip(1).StrJP()); // ignore first line
  257. continue;
  258. }
  259. var name = m.Groups[1].Value;
  260. string description;
  261. if (!meta.IsSelf)
  262. {
  263. // plugin type must be non-null for non-self plugins
  264. var resc = meta.PluginType!.Module.Resources.Select(r => r as EmbeddedResource)
  265. .NonNull()
  266. .FirstOrDefault(r => r.Name == name);
  267. if (resc == null)
  268. {
  269. Logger.Loader.Warn($"Could not find description file for plugin {meta.Name} ({name}); ignoring include");
  270. meta.Manifest.Description = string.Join("\n", lines.Skip(1).StrJP()); // ignore first line
  271. continue;
  272. }
  273. using var reader = new StreamReader(resc.GetResourceStream());
  274. description = reader.ReadToEnd();
  275. }
  276. else
  277. {
  278. using var descriptionReader = new StreamReader(meta.Assembly.GetManifestResourceStream(name));
  279. description = descriptionReader.ReadToEnd();
  280. }
  281. meta.Manifest.Description = description;
  282. }
  283. }
  284. }
  285. }
  286. #region Ignore stuff
  287. /// <summary>
  288. /// An enum that represents several categories of ignore reasons that the loader may encounter.
  289. /// </summary>
  290. /// <seealso cref="IgnoreReason"/>
  291. public enum Reason
  292. {
  293. /// <summary>
  294. /// An error was thrown either loading plugin information from disk, or when initializing the plugin.
  295. /// </summary>
  296. /// <remarks>
  297. /// When this is the set <see cref="Reason"/> in an <see cref="IgnoreReason"/> structure, the member
  298. /// <see cref="IgnoreReason.Error"/> will contain the thrown exception.
  299. /// </remarks>
  300. Error,
  301. /// <summary>
  302. /// The plugin this reason is associated with has the same ID as another plugin whose information was
  303. /// already loaded.
  304. /// </summary>
  305. /// <remarks>
  306. /// When this is the set <see cref="Reason"/> in an <see cref="IgnoreReason"/> structure, the member
  307. /// <see cref="IgnoreReason.RelatedTo"/> will contain the metadata of the already loaded plugin.
  308. /// </remarks>
  309. Duplicate,
  310. /// <summary>
  311. /// The plugin this reason is associated with conflicts with another already loaded plugin.
  312. /// </summary>
  313. /// <remarks>
  314. /// When this is the set <see cref="Reason"/> in an <see cref="IgnoreReason"/> structure, the member
  315. /// <see cref="IgnoreReason.RelatedTo"/> will contain the metadata of the plugin it conflicts with.
  316. /// </remarks>
  317. Conflict,
  318. /// <summary>
  319. /// The plugin this reason is associated with is missing a dependency.
  320. /// </summary>
  321. /// <remarks>
  322. /// Since this is only given when a dependency is missing, <see cref="IgnoreReason.RelatedTo"/> will
  323. /// not be set.
  324. /// </remarks>
  325. Dependency,
  326. /// <summary>
  327. /// The plugin this reason is associated with was released for a game update, but is still considered
  328. /// present for the purposes of updating.
  329. /// </summary>
  330. Released,
  331. /// <summary>
  332. /// The plugin this reason is associated with was denied from loading by a <see cref="Features.Feature"/>
  333. /// that it marks.
  334. /// </summary>
  335. Feature,
  336. /// <summary>
  337. /// The plugin this reason is associated with is unsupported.
  338. /// </summary>
  339. /// <remarks>
  340. /// Currently, there is no path in the loader that emits this <see cref="Reason"/>, however there may
  341. /// be in the future.
  342. /// </remarks>
  343. Unsupported,
  344. /// <summary>
  345. /// One of the files that a plugin declared in its manifest is missing.
  346. /// </summary>
  347. MissingFiles
  348. }
  349. /// <summary>
  350. /// A structure describing the reason that a plugin was ignored.
  351. /// </summary>
  352. public struct IgnoreReason : IEquatable<IgnoreReason>
  353. {
  354. /// <summary>
  355. /// Gets the ignore reason, as represented by the <see cref="Loader.Reason"/> enum.
  356. /// </summary>
  357. public Reason Reason { get; }
  358. /// <summary>
  359. /// Gets the textual description of the particular ignore reason. This will typically
  360. /// include details about why the plugin was ignored, if it is present.
  361. /// </summary>
  362. public string? ReasonText { get; internal set; }
  363. /// <summary>
  364. /// Gets the <see cref="Exception"/> that caused this plugin to be ignored, if any.
  365. /// </summary>
  366. public Exception? Error { get; internal set; }
  367. /// <summary>
  368. /// Gets the metadata of the plugin that this ignore was related to, if any.
  369. /// </summary>
  370. public PluginMetadata? RelatedTo { get; internal set; }
  371. /// <summary>
  372. /// Initializes an <see cref="IgnoreReason"/> with the provided data.
  373. /// </summary>
  374. /// <param name="reason">the <see cref="Loader.Reason"/> enum value that describes this reason</param>
  375. /// <param name="reasonText">the textual description of this ignore reason, if any</param>
  376. /// <param name="error">the <see cref="Exception"/> that caused this <see cref="IgnoreReason"/>, if any</param>
  377. /// <param name="relatedTo">the <see cref="PluginMetadata"/> this reason is related to, if any</param>
  378. public IgnoreReason(Reason reason, string? reasonText = null, Exception? error = null, PluginMetadata? relatedTo = null)
  379. {
  380. Reason = reason;
  381. ReasonText = reasonText;
  382. Error = error;
  383. RelatedTo = relatedTo;
  384. }
  385. /// <inheritdoc/>
  386. public override bool Equals(object obj)
  387. => obj is IgnoreReason ir && Equals(ir);
  388. /// <summary>
  389. /// Compares this <see cref="IgnoreReason"/> with <paramref name="other"/> for equality.
  390. /// </summary>
  391. /// <param name="other">the reason to compare to</param>
  392. /// <returns><see langword="true"/> if the two reasons compare equal, <see langword="false"/> otherwise</returns>
  393. public bool Equals(IgnoreReason other)
  394. => Reason == other.Reason && ReasonText == other.ReasonText
  395. && Error == other.Error && RelatedTo == other.RelatedTo;
  396. /// <inheritdoc/>
  397. public override int GetHashCode()
  398. {
  399. int hashCode = 778404373;
  400. hashCode = (hashCode * -1521134295) + Reason.GetHashCode();
  401. hashCode = (hashCode * -1521134295) + ReasonText?.GetHashCode() ?? 0;
  402. hashCode = (hashCode * -1521134295) + Error?.GetHashCode() ?? 0;
  403. hashCode = (hashCode * -1521134295) + RelatedTo?.GetHashCode() ?? 0;
  404. return hashCode;
  405. }
  406. /// <summary>
  407. /// Checks if two <see cref="IgnoreReason"/>s are equal.
  408. /// </summary>
  409. /// <param name="left">the first <see cref="IgnoreReason"/> to compare</param>
  410. /// <param name="right">the second <see cref="IgnoreReason"/> to compare</param>
  411. /// <returns><see langword="true"/> if the two reasons compare equal, <see langword="false"/> otherwise</returns>
  412. public static bool operator ==(IgnoreReason left, IgnoreReason right)
  413. => left.Equals(right);
  414. /// <summary>
  415. /// Checks if two <see cref="IgnoreReason"/>s are not equal.
  416. /// </summary>
  417. /// <param name="left">the first <see cref="IgnoreReason"/> to compare</param>
  418. /// <param name="right">the second <see cref="IgnoreReason"/> to compare</param>
  419. /// <returns><see langword="true"/> if the two reasons are not equal, <see langword="false"/> otherwise</returns>
  420. public static bool operator !=(IgnoreReason left, IgnoreReason right)
  421. => !(left == right);
  422. }
  423. #endregion
  424. internal partial class PluginLoader
  425. {
  426. // keep track of these for the updater; it should still be able to update mods not loaded
  427. // the thing -> the reason
  428. internal static Dictionary<PluginMetadata, IgnoreReason> ignoredPlugins = new();
  429. internal static void DoOrderResolution()
  430. {
  431. #if DEBUG
  432. // print starting order
  433. Logger.Loader.Debug(string.Join(", ", PluginsMetadata.StrJP()));
  434. #endif
  435. PluginsMetadata.Sort((a, b) => b.HVersion.CompareTo(a.HVersion));
  436. #if DEBUG
  437. // print base resolution order
  438. Logger.Loader.Debug(string.Join(", ", PluginsMetadata.StrJP()));
  439. #endif
  440. var metadataCache = new Dictionary<string, (PluginMetadata Meta, bool Enabled)>(PluginsMetadata.Count);
  441. var pluginsToProcess = new List<PluginMetadata>(PluginsMetadata.Count);
  442. var disabledIds = DisabledConfig.Instance.DisabledModIds;
  443. var disabledPlugins = new List<PluginMetadata>();
  444. // build metadata cache
  445. foreach (var meta in PluginsMetadata)
  446. {
  447. if (!metadataCache.TryGetValue(meta.Id, out var existing))
  448. {
  449. if (disabledIds.Contains(meta.Id))
  450. {
  451. metadataCache.Add(meta.Id, (meta, false));
  452. disabledPlugins.Add(meta);
  453. }
  454. else
  455. {
  456. metadataCache.Add(meta.Id, (meta, true));
  457. pluginsToProcess.Add(meta);
  458. }
  459. }
  460. else
  461. {
  462. Logger.Loader.Warn($"Found duplicates of {meta.Id}, using newest");
  463. ignoredPlugins.Add(meta, new(Reason.Duplicate)
  464. {
  465. ReasonText = $"Duplicate entry of same ID ({meta.Id})",
  466. RelatedTo = existing.Meta
  467. });
  468. }
  469. }
  470. // preprocess LoadBefore into LoadAfter
  471. foreach (var (_, (meta, _)) in metadataCache)
  472. { // we iterate the metadata cache because it contains both disabled and enabled plugins
  473. var loadBefore = meta.Manifest.LoadBefore;
  474. foreach (var id in loadBefore)
  475. {
  476. if (metadataCache.TryGetValue(id, out var plugin))
  477. {
  478. // if the id exists in our metadata cache, make sure it knows to load after the plugin in kvp
  479. _ = plugin.Meta.LoadsAfter.Add(meta);
  480. }
  481. }
  482. }
  483. // preprocess conflicts to be mutual
  484. foreach (var (_, (meta, _)) in metadataCache)
  485. {
  486. foreach (var (id, range) in meta.Manifest.Conflicts)
  487. {
  488. if (metadataCache.TryGetValue(id, out var plugin)
  489. && range.Matches(plugin.Meta.HVersion))
  490. {
  491. // make sure that there's a mutual dependency
  492. var targetRange = VersionRange.ForVersion(meta.HVersion);
  493. var targetConflicts = plugin.Meta.Manifest.Conflicts;
  494. if (!targetConflicts.TryGetValue(meta.Id, out var realRange))
  495. {
  496. // there's not already a listed conflict
  497. targetConflicts.Add(meta.Id, targetRange);
  498. }
  499. else if (!realRange.Matches(meta.HVersion))
  500. {
  501. // there is already a listed conflict that isn't mutual
  502. targetRange = realRange | targetRange;
  503. targetConflicts[meta.Id] = targetRange;
  504. }
  505. }
  506. }
  507. }
  508. var loadedPlugins = new Dictionary<string, (PluginMetadata Meta, bool Disabled, bool Ignored)>();
  509. var outputOrder = new List<PluginMetadata>(PluginsMetadata.Count);
  510. var isProcessing = new HashSet<PluginMetadata>();
  511. {
  512. bool TryResolveId(string id, [MaybeNullWhen(false)] out PluginMetadata meta, out bool disabled, out bool ignored, bool partial = false)
  513. {
  514. meta = null;
  515. disabled = false;
  516. ignored = true;
  517. Logger.Loader.Trace($"Trying to resolve plugin '{id}' partial:{partial}");
  518. if (loadedPlugins.TryGetValue(id, out var foundMeta))
  519. {
  520. meta = foundMeta.Meta;
  521. disabled = foundMeta.Disabled;
  522. ignored = foundMeta.Ignored;
  523. Logger.Loader.Trace($"- Found already processed");
  524. return true;
  525. }
  526. if (metadataCache!.TryGetValue(id, out var plugin))
  527. {
  528. Logger.Loader.Trace($"- In metadata cache");
  529. if (partial)
  530. {
  531. Logger.Loader.Trace($" - but requested in a partial lookup");
  532. return false;
  533. }
  534. disabled = !plugin.Enabled;
  535. meta = plugin.Meta;
  536. if (!disabled)
  537. {
  538. try
  539. {
  540. ignored = false;
  541. Resolve(plugin.Meta, ref disabled, out ignored);
  542. }
  543. catch (Exception e)
  544. {
  545. if (e is not DependencyResolutionLoopException)
  546. {
  547. Logger.Loader.Error($"While performing load order resolution for {id}:");
  548. Logger.Loader.Error(e);
  549. }
  550. if (!ignored)
  551. {
  552. ignoredPlugins.Add(plugin.Meta, new(Reason.Error)
  553. {
  554. Error = e
  555. });
  556. }
  557. ignored = true;
  558. }
  559. }
  560. if (!loadedPlugins.ContainsKey(id))
  561. {
  562. // this condition is specifically for when we fail resolution because of a graph loop
  563. Logger.Loader.Trace($"- '{id}' resolved as ignored:{ignored},disabled:{disabled}");
  564. loadedPlugins.Add(id, (plugin.Meta, disabled, ignored));
  565. }
  566. return true;
  567. }
  568. Logger.Loader.Trace($"- Not found");
  569. return false;
  570. }
  571. void Resolve(PluginMetadata plugin, ref bool disabled, out bool ignored)
  572. {
  573. Logger.Loader.Trace($">Resolving '{plugin.Name}'");
  574. // first we need to check for loops in the resolution graph to prevent stack overflows
  575. if (isProcessing.Contains(plugin))
  576. {
  577. Logger.Loader.Error($"Loop detected while processing '{plugin.Name}'; flagging as ignored");
  578. throw new DependencyResolutionLoopException();
  579. }
  580. isProcessing.Add(plugin);
  581. using var _removeProcessing = Utils.ScopeGuard(() => isProcessing.Remove(plugin));
  582. // if this method is being called, this is the first and only time that it has been called for this plugin.
  583. ignored = false;
  584. // perform file existence check before attempting to load dependencies
  585. foreach (var file in plugin.AssociatedFiles)
  586. {
  587. if (!file.Exists)
  588. {
  589. ignoredPlugins.Add(plugin, new IgnoreReason(Reason.MissingFiles)
  590. {
  591. ReasonText = $"File {Utils.GetRelativePath(file.FullName, UnityGame.InstallPath)} does not exist"
  592. });
  593. Logger.Loader.Warn($"File {Utils.GetRelativePath(file.FullName, UnityGame.InstallPath)}" +
  594. $" (declared by '{plugin.Name}') does not exist! Mod installation is incomplete, not loading it.");
  595. ignored = true;
  596. return;
  597. }
  598. }
  599. // first load dependencies
  600. var dependsOnSelf = false;
  601. foreach (var (id, range) in plugin.Manifest.Dependencies)
  602. {
  603. if (id == SelfMeta.Id)
  604. dependsOnSelf = true;
  605. if (!TryResolveId(id, out var depMeta, out var depDisabled, out var depIgnored)
  606. || !range.Matches(depMeta.HVersion))
  607. {
  608. Logger.Loader.Warn($"'{plugin.Id}' is missing dependency '{id}@{range}'; ignoring");
  609. ignoredPlugins.Add(plugin, new(Reason.Dependency)
  610. {
  611. ReasonText = $"Dependency '{id}@{range}' not found",
  612. });
  613. ignored = true;
  614. return;
  615. }
  616. // make a point to propagate ignored
  617. if (depIgnored)
  618. {
  619. Logger.Loader.Warn($"Dependency '{id}' for '{plugin.Id}' previously ignored; ignoring '{plugin.Id}'");
  620. ignoredPlugins.Add(plugin, new(Reason.Dependency)
  621. {
  622. ReasonText = $"Dependency '{id}' ignored",
  623. RelatedTo = depMeta
  624. });
  625. ignored = true;
  626. return;
  627. }
  628. // make a point to propagate disabled
  629. if (depDisabled)
  630. {
  631. Logger.Loader.Warn($"Dependency '{id}' for '{plugin.Id}' disabled; disabling");
  632. disabledPlugins!.Add(plugin);
  633. _ = disabledIds!.Add(plugin.Id);
  634. disabled = true;
  635. }
  636. // we found our dep, lets save the metadata and keep going
  637. _ = plugin.Dependencies.Add(depMeta);
  638. }
  639. // make sure the plugin depends on the loader (assuming it actually needs to)
  640. if (!dependsOnSelf && !plugin.IsSelf && !plugin.IsBare)
  641. {
  642. Logger.Loader.Warn($"Plugin '{plugin.Id}' does not depend on any particular loader version; assuming its incompatible");
  643. ignoredPlugins.Add(plugin, new(Reason.Dependency)
  644. {
  645. ReasonText = "Does not depend on any loader version, so it is assumed to be incompatible",
  646. RelatedTo = SelfMeta
  647. });
  648. ignored = true;
  649. return;
  650. }
  651. // exit early if we've decided we need to be disabled
  652. if (disabled)
  653. return;
  654. // handle LoadsAfter populated by Features processing
  655. foreach (var loadAfter in plugin.LoadsAfter)
  656. {
  657. if (TryResolveId(loadAfter.Id, out _, out _, out _))
  658. {
  659. // do nothing, because the plugin is already in the LoadsAfter set
  660. }
  661. }
  662. // then handle loadafters
  663. foreach (var id in plugin.Manifest.LoadAfter)
  664. {
  665. if (TryResolveId(id, out var meta, out var depDisabled, out var depIgnored))
  666. {
  667. // we only want to make sure to loadafter if its not ignored
  668. // if its disabled, we still wanna track it where possible
  669. _ = plugin.LoadsAfter.Add(meta);
  670. }
  671. }
  672. // after we handle dependencies and loadafters, then check conflicts
  673. foreach (var (id, range) in plugin.Manifest.Conflicts)
  674. {
  675. Logger.Loader.Trace($">- Checking conflict '{id}' {range}");
  676. // this lookup must be partial to prevent loadBefore/conflictsWith from creating a recursion loop
  677. if (TryResolveId(id, out var meta, out var conflDisabled, out var conflIgnored, partial: true)
  678. && range.Matches(meta.HVersion)
  679. && !conflIgnored && !conflDisabled) // the conflict is only *actually* a problem if it is both not ignored and not disabled
  680. {
  681. Logger.Loader.Warn($"Plugin '{plugin.Id}' conflicts with {meta.Id}@{meta.HVersion}; ignoring '{plugin.Id}'");
  682. ignoredPlugins.Add(plugin, new(Reason.Conflict)
  683. {
  684. ReasonText = $"Conflicts with {meta.Id}@{meta.HVersion}",
  685. RelatedTo = meta
  686. });
  687. ignored = true;
  688. return;
  689. }
  690. }
  691. // specifically check if some strange stuff happened (like graph loops) causing this to be ignored
  692. // from some other invocation
  693. if (!ignoredPlugins.ContainsKey(plugin))
  694. {
  695. // we can now load the current plugin
  696. Logger.Loader.Trace($"->'{plugin.Name}' loads here");
  697. outputOrder!.Add(plugin);
  698. }
  699. // loadbefores have already been preprocessed into loadafters
  700. Logger.Loader.Trace($">Processed '{plugin.Name}'");
  701. }
  702. // run TryResolveId over every plugin, which recursively calculates load order
  703. foreach (var plugin in pluginsToProcess)
  704. {
  705. _ = TryResolveId(plugin.Id, out _, out _, out _);
  706. }
  707. // by this point, outputOrder contains the full load order
  708. }
  709. DisabledConfig.Instance.Changed();
  710. DisabledPlugins = disabledPlugins;
  711. PluginsMetadata = outputOrder;
  712. }
  713. internal static void InitFeatures()
  714. {
  715. foreach (var meta in PluginsMetadata)
  716. {
  717. foreach (var feature in meta.Manifest.Features
  718. .SelectMany(f => f.Value.Select(o => (f.Key, o)))
  719. .Select(t => new Feature.Instance(meta, t.Key, t.o)))
  720. {
  721. if (feature.TryGetDefiningPlugin(out var plugin) && plugin == null)
  722. { // this is a DefineFeature, so we want to initialize it early
  723. if (!feature.TryCreate(out var inst))
  724. {
  725. Logger.Features.Error($"Error evaluating {feature.Name}: {inst.InvalidMessage}");
  726. }
  727. else
  728. {
  729. meta.InternalFeatures.Add(inst);
  730. }
  731. }
  732. else
  733. { // this is literally any other feature, so we want to delay its initialization
  734. _ = meta.UnloadedFeatures.Add(feature);
  735. }
  736. }
  737. }
  738. // at this point we have pre-initialized all features, so we can go ahead and use them to add stuff to the dep resolver
  739. foreach (var meta in PluginsMetadata)
  740. {
  741. foreach (var feature in meta.UnloadedFeatures)
  742. {
  743. if (feature.TryGetDefiningPlugin(out var plugin))
  744. {
  745. if (plugin != meta && plugin != null)
  746. { // if the feature is not applied to the defining feature
  747. _ = meta.LoadsAfter.Add(plugin);
  748. }
  749. if (plugin != null)
  750. {
  751. plugin.CreateFeaturesWhenLoaded.Add(feature);
  752. }
  753. }
  754. else
  755. {
  756. Logger.Features.Warn($"No such feature {feature.Name}");
  757. }
  758. }
  759. }
  760. }
  761. internal static void ReleaseAll(bool full = false)
  762. {
  763. if (full)
  764. {
  765. ignoredPlugins = new();
  766. }
  767. else
  768. {
  769. foreach (var m in PluginsMetadata)
  770. ignoredPlugins.Add(m, new IgnoreReason(Reason.Released));
  771. foreach (var m in ignoredPlugins.Keys)
  772. { // clean them up so we can still use the metadata for updates
  773. m.InternalFeatures.Clear();
  774. m.PluginType = null;
  775. m.Assembly = null!;
  776. }
  777. }
  778. PluginsMetadata = new List<PluginMetadata>();
  779. DisabledPlugins = new List<PluginMetadata>();
  780. Feature.Reset();
  781. GC.Collect();
  782. GC.WaitForPendingFinalizers();
  783. }
  784. internal static void Load(PluginMetadata meta)
  785. {
  786. if (meta is { Assembly: null, PluginType: not null })
  787. meta.Assembly = Assembly.LoadFrom(meta.File.FullName);
  788. }
  789. internal static PluginExecutor? InitPlugin(PluginMetadata meta, IEnumerable<PluginMetadata> alreadyLoaded)
  790. {
  791. if (meta.Manifest.GameVersion is { } gv && gv != UnityGame.GameVersion)
  792. Logger.Loader.Warn($"Mod {meta.Name} developed for game version {gv}, so it may not work properly.");
  793. if (meta.IsSelf)
  794. return new PluginExecutor(meta, PluginExecutor.Special.Self);
  795. foreach (var dep in meta.Dependencies)
  796. {
  797. if (alreadyLoaded.Contains(dep)) continue;
  798. // otherwise...
  799. if (ignoredPlugins.TryGetValue(dep, out var reason))
  800. { // was added to the ignore list
  801. ignoredPlugins.Add(meta, new IgnoreReason(Reason.Dependency)
  802. {
  803. ReasonText = $"Dependency was ignored at load time: {reason.ReasonText}",
  804. RelatedTo = dep
  805. });
  806. }
  807. else
  808. { // was not added to ignore list
  809. ignoredPlugins.Add(meta, new IgnoreReason(Reason.Dependency)
  810. {
  811. ReasonText = $"Dependency was not already loaded at load time, but was also not ignored",
  812. RelatedTo = dep
  813. });
  814. }
  815. return null;
  816. }
  817. if (meta.IsBare)
  818. return new PluginExecutor(meta, PluginExecutor.Special.Bare);
  819. Load(meta);
  820. PluginExecutor exec;
  821. try
  822. {
  823. exec = new PluginExecutor(meta);
  824. }
  825. catch (Exception e)
  826. {
  827. Logger.Loader.Error($"Error creating executor for {meta.Name}");
  828. Logger.Loader.Error(e);
  829. return null;
  830. }
  831. foreach (var feature in meta.Features)
  832. {
  833. try
  834. {
  835. feature.BeforeInit(meta);
  836. }
  837. catch (Exception e)
  838. {
  839. Logger.Loader.Critical($"Feature errored in {nameof(Feature.BeforeInit)}:");
  840. Logger.Loader.Critical(e);
  841. }
  842. }
  843. try
  844. {
  845. exec.Create();
  846. }
  847. catch (Exception e)
  848. {
  849. Logger.Loader.Error($"Could not init plugin {meta.Name}");
  850. Logger.Loader.Error(e);
  851. ignoredPlugins.Add(meta, new IgnoreReason(Reason.Error)
  852. {
  853. ReasonText = "Error occurred while initializing",
  854. Error = e
  855. });
  856. return null;
  857. }
  858. // TODO: make this new features system behave better wrt DynamicInit plugins
  859. foreach (var feature in meta.CreateFeaturesWhenLoaded)
  860. {
  861. if (!feature.TryCreate(out var inst))
  862. {
  863. Logger.Features.Warn($"Could not create instance of feature {feature.Name}: {inst.InvalidMessage}");
  864. }
  865. else
  866. {
  867. feature.AppliedTo.InternalFeatures.Add(inst);
  868. _ = feature.AppliedTo.UnloadedFeatures.Remove(feature);
  869. }
  870. }
  871. meta.CreateFeaturesWhenLoaded.Clear(); // if a plugin is loaded twice, for the moment, we don't want to create the feature twice
  872. foreach (var feature in meta.Features)
  873. try
  874. {
  875. feature.AfterInit(meta, exec.Instance);
  876. }
  877. catch (Exception e)
  878. {
  879. Logger.Loader.Critical($"Feature errored in {nameof(Feature.AfterInit)}:");
  880. Logger.Loader.Critical(e);
  881. }
  882. return exec;
  883. }
  884. internal static bool IsFirstLoadComplete { get; private set; }
  885. internal static List<PluginExecutor> LoadPlugins()
  886. {
  887. DisabledPlugins.ForEach(Load); // make sure they get loaded into memory so their metadata and stuff can be read more easily
  888. var list = new List<PluginExecutor>();
  889. var loaded = new HashSet<PluginMetadata>();
  890. foreach (var meta in PluginsMetadata)
  891. {
  892. try
  893. {
  894. var exec = InitPlugin(meta, loaded);
  895. if (exec != null)
  896. {
  897. list.Add(exec);
  898. _ = loaded.Add(meta);
  899. }
  900. }
  901. catch (Exception e)
  902. {
  903. Logger.Default.Critical($"Uncaught exception while loading plugin {meta.Name}:");
  904. Logger.Default.Critical(e);
  905. }
  906. }
  907. // TODO: should this be somewhere else?
  908. IsFirstLoadComplete = true;
  909. return list;
  910. }
  911. }
  912. }