diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-05-14 18:17:34 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-05-14 18:17:34 -0400 |
commit | 07aadf36126bfadd5df624ccf810828adf679788 (patch) | |
tree | 90474aadfd143e5b507a5f149e1a7b2c2208276a /src/StardewModdingAPI/Framework/ModLoading | |
parent | f3ff871eb76ca49e298d5418203366fdb46b1dc3 (diff) | |
download | SMAPI-07aadf36126bfadd5df624ccf810828adf679788.tar.gz SMAPI-07aadf36126bfadd5df624ccf810828adf679788.tar.bz2 SMAPI-07aadf36126bfadd5df624ccf810828adf679788.zip |
replace mod indexes with references in dependency-sorting logic (#285)
Diffstat (limited to 'src/StardewModdingAPI/Framework/ModLoading')
-rw-r--r-- | src/StardewModdingAPI/Framework/ModLoading/ModResolver.cs | 24 |
1 files changed, 11 insertions, 13 deletions
diff --git a/src/StardewModdingAPI/Framework/ModLoading/ModResolver.cs b/src/StardewModdingAPI/Framework/ModLoading/ModResolver.cs index e3f4fc12..8efe57d9 100644 --- a/src/StardewModdingAPI/Framework/ModLoading/ModResolver.cs +++ b/src/StardewModdingAPI/Framework/ModLoading/ModResolver.cs @@ -49,7 +49,7 @@ namespace StardewModdingAPI.Framework.ModLoading // get compatibility record ModCompatibility compatibility = null; - if(manifest != null) + if (manifest != null) { string key = !string.IsNullOrWhiteSpace(manifest.UniqueID) ? manifest.UniqueID : manifest.EntryDll; compatibility = ( @@ -132,16 +132,16 @@ namespace StardewModdingAPI.Framework.ModLoading { var unsortedMods = mods.ToList(); var sortedMods = new Stack<IModMetadata>(); - var visitedMods = new bool[unsortedMods.Count]; + var visitedMods = new HashSet<IModMetadata>(); var currentChain = new List<IModMetadata>(); bool success = true; - for (int index = 0; index < unsortedMods.Count; index++) + foreach (IModMetadata mod in unsortedMods) { - if (unsortedMods[index].Status == ModMetadataStatus.Failed) + if (mod.Status == ModMetadataStatus.Failed) continue; - success = this.ProcessDependencies(index, visitedMods, sortedMods, currentChain, unsortedMods); + success = this.ProcessDependencies(mod, visitedMods, sortedMods, currentChain, unsortedMods); if (!success) break; } @@ -157,21 +157,20 @@ namespace StardewModdingAPI.Framework.ModLoading ** Private methods *********/ /// <summary>Sort a mod's dependencies by the order they should be loaded, and remove any mods that can't be loaded due to missing or conflicting dependencies.</summary> - /// <param name="modIndex">The index of the mod being processed in the <paramref name="unsortedMods"/>.</param> - /// <param name="visitedMods">The mods which have been processed.</param> + /// <param name="mod">The mod whose dependencies to process.</param> + /// <param name="visited">The mods which have been visited.</param> /// <param name="sortedMods">The list in which to save mods sorted by dependency order.</param> /// <param name="currentChain">The current change of mod dependencies.</param> /// <param name="unsortedMods">The mods remaining to sort.</param> /// <returns>Returns whether the mod can be loaded.</returns> - private bool ProcessDependencies(int modIndex, bool[] visitedMods, Stack<IModMetadata> sortedMods, List<IModMetadata> currentChain, List<IModMetadata> unsortedMods) + private bool ProcessDependencies(IModMetadata mod, HashSet<IModMetadata> visited, Stack<IModMetadata> sortedMods, List<IModMetadata> currentChain, List<IModMetadata> unsortedMods) { // visit mod - if (visitedMods[modIndex]) + if (visited.Contains(mod)) return true; // already sorted - visitedMods[modIndex] = true; + visited.Add(mod); // mod already failed - IModMetadata mod = unsortedMods[modIndex]; if (mod.Status == ModMetadataStatus.Failed) return false; @@ -215,8 +214,7 @@ namespace StardewModdingAPI.Framework.ModLoading // recursively sort dependencies foreach (IModMetadata requiredMod in modsToLoadFirst) { - int index = unsortedMods.IndexOf(requiredMod); - success = this.ProcessDependencies(index, visitedMods, sortedMods, currentChain, unsortedMods); + success = this.ProcessDependencies(requiredMod, visited, sortedMods, currentChain, unsortedMods); if (!success) break; } |