Skip to content

Commit

Permalink
Fix exception when the same module is in multiple repos
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Nov 22, 2024
1 parent 9246150 commit d57a514
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
6 changes: 3 additions & 3 deletions Core/Registry/CompatibilitySorter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class CompatibilitySorter
/// <param name="dlc">Collection of installed DLCs</param>
public CompatibilitySorter(GameVersionCriteria crit,
IEnumerable<Dictionary<string, AvailableModule>> available,
IDictionary<string, HashSet<AvailableModule>> providers,
IDictionary<string, AvailableModule[]> providers,
IDictionary<string, InstalledModule> installed,
ICollection<string> dlls,
IDictionary<string, ModuleVersion> dlc)
Expand Down Expand Up @@ -119,8 +119,8 @@ public ICollection<CkanModule> LatestIncompatible
/// Mapping from identifiers to compatible mods providing those identifiers
/// </returns>
private static Dictionary<string, HashSet<AvailableModule>> CompatibleProviders(
GameVersionCriteria crit,
IDictionary<string, HashSet<AvailableModule>> providers)
GameVersionCriteria crit,
IDictionary<string, AvailableModule[]> providers)
=> providers
.AsParallel()
.Select(kvp => new KeyValuePair<string, HashSet<AvailableModule>>(
Expand Down
15 changes: 8 additions & 7 deletions Core/Registry/Registry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ private void EnlistWithTransaction()
// providers[provided] = { provider1, provider2, ... }
// Built by BuildProvidesIndex, makes LatestAvailableWithProvides much faster.
[JsonIgnore]
private Dictionary<string, HashSet<AvailableModule>>? providers;
private Dictionary<string, AvailableModule[]>? providers;

[JsonIgnore]
private IDictionary<string, ModuleVersion>? installedDlc;
Expand Down Expand Up @@ -701,7 +701,7 @@ public string GetAvailableMetadata(string identifier)
/// Generate the providers index so we can find providing modules quicker
/// </summary>
[MemberNotNull(nameof(providers))]
private Dictionary<string, HashSet<AvailableModule>> BuildProvidesIndex()
private Dictionary<string, AvailableModule[]> BuildProvidesIndex()
=> providers = (repoDataMgr?.GetAllAvailableModules(Repositories.Values)
?? Enumerable.Empty<AvailableModule>())
.SelectMany(am => am.AllAvailable()
Expand All @@ -711,7 +711,7 @@ private Dictionary<string, HashSet<AvailableModule>> BuildProvidesIndex()
.GroupBy(tuple => tuple.provided,
tuple => tuple.am)
.ToDictionary(grp => grp.Key,
grp => grp.ToHashSet());
grp => grp.ToArray());

[JsonIgnore]
public Dictionary<string, ModuleTag> Tags
Expand Down Expand Up @@ -770,8 +770,8 @@ private void BuildTagIndex()

public IEnumerable<AvailableModule> AllAvailableByProvides(string identifier)
=> (providers ?? BuildProvidesIndex())
is Dictionary<string, HashSet<AvailableModule>> allProvs
&& allProvs.TryGetValue(identifier, out HashSet<AvailableModule>? provs)
is Dictionary<string, AvailableModule[]> allProvs
&& allProvs.TryGetValue(identifier, out AvailableModule[]? provs)
? provs
: Enumerable.Empty<AvailableModule>();

Expand All @@ -784,13 +784,14 @@ public List<CkanModule> LatestAvailableWithProvides(string ide
ICollection<CkanModule>? installed = null,
ICollection<CkanModule>? toInstall = null)
=> ((providers ?? BuildProvidesIndex())
is Dictionary<string, HashSet<AvailableModule>> allProvs
is Dictionary<string, AvailableModule[]> allProvs
&& Repositories.Values.ToArray() is Repository[] repos
&& allProvs.TryGetValue(identifier, out HashSet<AvailableModule>? provs)
&& allProvs.TryGetValue(identifier, out AvailableModule[]? provs)
// For each AvailableModule, we want the latest one matching our constraints
? provs.Select(am => am.Latest(gameVersion, relationship, installed, toInstall))
.OfType<CkanModule>()
.Where(m => m.ProvidesList.Contains(identifier))
.Distinct()
// Put the most popular one on top
.OrderByDescending(m => repoDataMgr?.GetDownloadCount(repos, m.identifier)
?? 0)
Expand Down
2 changes: 1 addition & 1 deletion Core/Relationships/ResolvedRelationship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public override bool Contains(CkanModule mod)

public override bool Unsatisfied()
=> reason is SelectionReason.Depends
&& resolved.Keys.Count(m => !m.IsDLC) == 0;
&& !resolved.Keys.Any(m => !m.IsDLC);

public override bool Unsatisfied(ICollection<CkanModule> installing)
=> reason is SelectionReason.Depends
Expand Down
2 changes: 1 addition & 1 deletion Tests/Core/Registry/CompatibilitySorter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void Constructor_OverlappingModules_HigherPriorityOverrides(string[] modu
.GetAllAvailableModules(repos)
.GroupBy(am => am.AllAvailable().First().identifier)
.ToDictionary(grp => grp.Key,
grp => grp.ToHashSet());
grp => grp.ToArray());
var installed = new Dictionary<string, InstalledModule>();
var dlls = new Dictionary<string, string>().Keys;
var dlcs = new Dictionary<string, ModuleVersion>();
Expand Down

0 comments on commit d57a514

Please sign in to comment.