Skip to content

Commit

Permalink
Exclude DLLs being upgraded to full modules
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Nov 6, 2024
1 parent 2895a45 commit 8c2059e
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 16 deletions.
12 changes: 9 additions & 3 deletions Core/Relationships/RelationshipResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ public RelationshipResolver(IEnumerable<CkanModule> modulesToInstall,
}

var toInst = modulesToInstall.ToArray();
resolved = new ResolvedRelationshipsTree(toInst, registry,
// DLLs that we are upgrading to full modules should be excluded
dlls = registry.InstalledDlls
.Except(modulesToInstall.Select(m => m.identifier))
.ToHashSet();
resolved = new ResolvedRelationshipsTree(toInst, registry, dlls,
installed_modules, versionCrit,
options.OptionalHandling());
if (!options.proceed_with_inconsistencies)
Expand Down Expand Up @@ -127,7 +131,7 @@ private void AddModulesToInstall(CkanModule[] modules)
{
// Check that our solution is actually sane
SanityChecker.EnforceConsistency(modlist.Values.Concat(installed_modules),
registry.InstalledDlls,
dlls,
registry.InstalledDlc);
}
catch (BadRelationshipsKraken k) when (options.without_enforce_consistency)
Expand Down Expand Up @@ -276,7 +280,7 @@ private void ResolveStanza(List<RelationshipDescriptor>? stanza,

// If it's already installed, skip.
if (descriptor.MatchesAny(installed_modules,
registry.InstalledDlls,
dlls,
registry.InstalledDlc,
out CkanModule? installedCandidate))
{
Expand Down Expand Up @@ -649,6 +653,8 @@ private void AddReason(CkanModule module, SelectionReason reason)
private readonly Dictionary<CkanModule, List<SelectionReason>> reasons =
new Dictionary<CkanModule, List<SelectionReason>>();

private readonly HashSet<string> dlls;

/// <summary>
/// Depends relationships with suppress_recommendations=true,
/// to be applied to all recommendations and suggestions
Expand Down
3 changes: 2 additions & 1 deletion Core/Relationships/ResolvedRelationship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,15 @@ public ResolvedByNew(CkanModule source,
ICollection<CkanModule> definitelyInstalling,
ICollection<CkanModule> allInstalling,
IRegistryQuerier registry,
ICollection<string> dlls,
ICollection<CkanModule> installed,
GameVersionCriteria crit,
OptionalRelationships optRels,
RelationshipCache relationshipCache)
: this(source, relationship, reason,
providers.ToDictionary(prov => prov,
prov => ResolvedRelationshipsTree.ResolveModule(
prov, definitelyInstalling, allInstalling, registry, installed, crit,
prov, definitelyInstalling, allInstalling, registry, dlls, installed, crit,
relationship.suppress_recommendations
? optRels & ~OptionalRelationships.Recommendations
& ~OptionalRelationships.Suggestions
Expand Down
21 changes: 13 additions & 8 deletions Core/Relationships/ResolvedRelationshipsTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,33 @@ public class ResolvedRelationshipsTree
{
public ResolvedRelationshipsTree(ICollection<CkanModule> modules,
IRegistryQuerier registry,
ICollection<string> dlls,
ICollection<CkanModule> installed,
GameVersionCriteria crit,
OptionalRelationships optRels)
{
resolved = ResolveManyCached(modules, registry, installed, crit, optRels, relationshipCache).ToArray();
resolved = ResolveManyCached(modules, registry, dlls, installed, crit, optRels, relationshipCache).ToArray();
}

public static IEnumerable<ResolvedRelationship> ResolveModule(CkanModule module,
ICollection<CkanModule> definitelyInstalling,
ICollection<CkanModule> allInstalling,
IRegistryQuerier registry,
ICollection<string> dlls,
ICollection<CkanModule> installed,
GameVersionCriteria crit,
OptionalRelationships optRels,
RelationshipCache relationshipCache)
=> ResolveRelationships(module, module.depends, new SelectionReason.Depends(module),
definitelyInstalling, allInstalling, registry, installed, crit, optRels, relationshipCache)
definitelyInstalling, allInstalling, registry, dlls, installed, crit, optRels, relationshipCache)
.Concat((optRels & OptionalRelationships.Recommendations) == 0
? Enumerable.Empty<ResolvedRelationship>()
: ResolveRelationships(module, module.recommends, new SelectionReason.Recommended(module, 0),
definitelyInstalling, allInstalling, registry, installed, crit, optRels, relationshipCache))
definitelyInstalling, allInstalling, registry, dlls, installed, crit, optRels, relationshipCache))
.Concat((optRels & OptionalRelationships.Suggestions) == 0
? Enumerable.Empty<ResolvedRelationship>()
: ResolveRelationships(module, module.suggests, new SelectionReason.Suggested(module),
definitelyInstalling, allInstalling, registry, installed, crit, optRels, relationshipCache));
definitelyInstalling, allInstalling, registry, dlls, installed, crit, optRels, relationshipCache));

public IEnumerable<ResolvedRelationship[]> Unsatisfied()
=> resolved.SelectMany(UnsatisfiedFrom);
Expand Down Expand Up @@ -102,11 +104,12 @@ public override string ToString()

private static IEnumerable<ResolvedRelationship> ResolveManyCached(ICollection<CkanModule> modules,
IRegistryQuerier registry,
ICollection<string> dlls,
ICollection<CkanModule> installed,
GameVersionCriteria crit,
OptionalRelationships optRels,
RelationshipCache relationshipCache)
=> modules.SelectMany(m => ResolveModule(m, modules, modules, registry, installed, crit, optRels,
=> modules.SelectMany(m => ResolveModule(m, modules, modules, registry, dlls, installed, crit, optRels,
relationshipCache));

private static IEnumerable<ResolvedRelationship> ResolveRelationships(CkanModule module,
Expand All @@ -115,12 +118,13 @@ private static IEnumerable<ResolvedRelationship> ResolveRelationships(CkanModule
ICollection<CkanModule> definitelyInstalling,
ICollection<CkanModule> allInstalling,
IRegistryQuerier registry,
ICollection<string> dlls,
ICollection<CkanModule> installed,
GameVersionCriteria crit,
OptionalRelationships optRels,
RelationshipCache relationshipCache)
=> relationships?.Select(dep => Resolve(module, dep, reason,
definitelyInstalling, allInstalling, registry, installed,
definitelyInstalling, allInstalling, registry, dlls, installed,
crit, optRels, relationshipCache))
?? Enumerable.Empty<ResolvedRelationship>();

Expand All @@ -130,14 +134,15 @@ private static ResolvedRelationship Resolve(CkanModule source,
ICollection<CkanModule> definitelyInstalling,
ICollection<CkanModule> allInstalling,
IRegistryQuerier registry,
ICollection<string> dlls,
ICollection<CkanModule> installed,
GameVersionCriteria crit,
OptionalRelationships optRels,
RelationshipCache relationshipCache)
=> relationshipCache.TryGetValue(relationship,
out ResolvedRelationship? cachedRel)
? cachedRel.WithSource(source, reason)
: relationship.MatchesAny(installed, registry.InstalledDlls, registry.InstalledDlc,
: relationship.MatchesAny(installed, dlls, registry.InstalledDlc,
out CkanModule? installedMatch)
? relationshipCache.GetOrAdd(
relationship,
Expand All @@ -156,7 +161,7 @@ private static ResolvedRelationship Resolve(CkanModule source,
installed, definitelyInstalling),
definitelyInstalling,
allInstalling.Append(source).ToArray(),
registry, installed, crit, optRels,
registry, dlls, installed, crit, optRels,
relationshipCache));

private readonly ResolvedRelationship[] resolved;
Expand Down
8 changes: 8 additions & 0 deletions Netkan/Extensions/JObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ public static void SafeMerge(this JObject jobject, string propertyName, JToken?
}
}

public static void SafeMerge(this JObject jobject, JObject other)
{
foreach (var property in other.Properties())
{
jobject.SafeAdd(property.Name, property.Value);
}
}

public static JToken? ToJValueOrJArray<T>(this IEnumerable<T?> source) where T: notnull
{
var items = source.OfType<T>()
Expand Down
56 changes: 52 additions & 4 deletions Tests/Core/Relationships/RelationshipResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using CKAN.Games.KerbalSpaceProgram;
using CKAN.Versioning;
using RelationshipDescriptor = CKAN.RelationshipDescriptor;
using CKAN.NetKAN.Extensions;

namespace Tests.Core.Relationships
{
Expand Down Expand Up @@ -1065,6 +1066,55 @@ public void AutodetectedCanSatisfyRelationships()
}
}

[Test,
TestCase(new string[] {
@"{
""identifier"": ""Deferred"",
""conflicts"": [
{
""name"": ""RasterPropMonitor"",
""max_version"": ""1:v0.31.13.4""
}
]
}",
@"{
""identifier"": ""RasterPropMonitor"",
""version"": ""1:v1.0.2""
}",
},
new string[] { "Deferred", "RasterPropMonitor" })]
public void Constructor_UpgradeVersionSpecificConflictedAutoDetected_DoesNotThrow(
string[] availableModules,
string[] installIdents)
{
var user = new NullUser();
var crit = new GameVersionCriteria(new GameVersion(1, 12, 5));
using (var repo = new TemporaryRepository(availableModules.Select(MergeWithDefaults)
.ToArray()))
using (var repoData = new TemporaryRepositoryData(user, repo.repo))
using (var inst = new DisposableKSP())
{
var registry = new CKAN.Registry(repoData.Manager, repo.repo);
registry.SetDlls(new Dictionary<string, string>()
{
{
"RasterPropMonitor",
inst.KSP.ToRelativeGameDir(Path.Combine(inst.KSP.game.PrimaryModDirectory(inst.KSP),
"RasterPropMonitor.dll"))
}
});
Assert.DoesNotThrow(() =>
{
var rr = new RelationshipResolver(
installIdents.Select(ident => registry.LatestAvailable(ident, crit))
.OfType<CkanModule>(),
null,
RelationshipResolverOptions.DependsOnlyOpts(),
registry, inst.KSP.game, crit);
});
}
}

// Models the EVE - EVE-Config - AVP - AVP-Textures relationship
[Test]
public void UninstallingConflictingModule_InstallingRecursiveDependencies_ResolvesSuccessfully()
Expand Down Expand Up @@ -1510,9 +1560,7 @@ public void Constructor_WithPlayModes_DoesNotThrow(string[] availableModules,
.OfType<CkanModule>(),
null,
RelationshipResolverOptions.DependsOnlyOpts(),
registry,
game,
crit);
registry, game, crit);
var idents = rr.ModList().Select(m => m.identifier).ToArray();
foreach (var goodSubstring in goodSubstrings)
{
Expand All @@ -1534,7 +1582,7 @@ public void Constructor_WithPlayModes_DoesNotThrow(string[] availableModules,
public static string MergeWithDefaults(string json)
{
var incoming = JObject.Parse(json);
incoming.Merge(moduleDefaults);
incoming.SafeMerge(moduleDefaults);
return incoming.ToString();
}

Expand Down

0 comments on commit 8c2059e

Please sign in to comment.