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.

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