Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loop to find max KSP version instead of assuming ordering #2131

Merged
merged 2 commits into from
Oct 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions Core/Registry/AvailableModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ public void Remove(Version version)
}

/// <summary>
/// Return the most recent release of a module with a optional ksp version to target and a RelationshipDescriptor to satisfy.
/// Return the most recent release of a module with a optional ksp version to target and a RelationshipDescriptor to satisfy.
/// </summary>
/// <param name="ksp_version">If not null only consider mods which match this ksp version.</param>
/// <param name="relationship">If not null only consider mods which satisfy the RelationshipDescriptor.</param>
/// <returns></returns>
public CkanModule Latest(KspVersionCriteria ksp_version = null, RelationshipDescriptor relationship=null)
{
{
var available_versions = new List<Version>(module_version.Keys);
CkanModule module;
log.DebugFormat("Our dictionary has {0} keys", module_version.Keys.Count);
log.DebugFormat("Choosing between {0} available versions", available_versions.Count);
log.DebugFormat("Choosing between {0} available versions", available_versions.Count);

// Uh oh, nothing available. Maybe this existed once, but not any longer.
if (available_versions.Count == 0)
Expand Down Expand Up @@ -117,20 +117,38 @@ public CkanModule Latest(KspVersionCriteria ksp_version = null, RelationshipDesc
return version == null ? null : module_version[version];
}
else
{
{
var version = available_versions.FirstOrDefault(v =>
relationship.version_within_bounds(v) &&
module_version[v].IsCompatibleKSP(ksp_version));
return version == null ? null : module_version[version];
return version == null ? null : module_version[version];
}

}

/// <summary>
/// Returns the latest game version that is compatible with this mod.
/// Checks all versions of the mod.
/// </summary>
public KspVersion LatestCompatibleKSP()
{
KspVersion best = null;
foreach (var pair in module_version)
{
KspVersion v = pair.Value.LatestCompatibleKSP();
if (v.IsAny)
// Can't get later than Any, so stop
return v;
else if (best == null || best < v)
best = v;
}
return best;
}

/// <summary>
/// Returns the module with the specified version, or null if that does not exist.
/// </summary>
public CkanModule ByVersion(Version v)
{
{
CkanModule module;
module_version.TryGetValue(v, out module);
return module;
Expand All @@ -155,4 +173,4 @@ public int Compare(Version x, Version y)
return y.CompareTo(x);
}
}
}
}
10 changes: 8 additions & 2 deletions Core/Registry/IRegistryQuerier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public interface IRegistryQuerier
/// </summary>
CkanModule LatestAvailable(string identifier, KspVersionCriteria ksp_version, RelationshipDescriptor relationship_descriptor = null);

/// <summary>
/// Returns the max game version that is compatible with the given mod.
/// </summary>
/// <param name="identifier">Name of mod to check</param>
KspVersion LatestCompatibleKSP(string identifier);

/// <summary>
/// Returns all available version of a module.
/// <exception cref="ModuleNotFoundKraken">Throws if asked for a non-existent module.</exception>
Expand Down Expand Up @@ -146,8 +152,8 @@ public static bool HasUpdate(this IRegistryQuerier querier, string identifier, K
return false;
}
if (newest_version == null) return false;
return !new List<string>(querier.InstalledDlls).Contains(identifier) && querier.IsInstalled(identifier, false)
return !new List<string>(querier.InstalledDlls).Contains(identifier) && querier.IsInstalled(identifier, false)
&& newest_version.version.IsGreaterThan(querier.InstalledVersion(identifier));
}
}
}
}
8 changes: 8 additions & 0 deletions Core/Registry/Registry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,14 @@ public List<CkanModule> AllAvailable(string module)
}
}

/// <summary>
/// Return the latest game version compatible with the given mod.
/// </summary>
/// <param name="identifier">Name of mod to check</param>
public KspVersion LatestCompatibleKSP(string identifier)
{
return available_modules[identifier].LatestCompatibleKSP();
}


/// <summary>
Expand Down
41 changes: 23 additions & 18 deletions Core/Types/CkanModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public int GetHashCode(CkanModule obj)
/// <summary>
/// Describes a CKAN module (ie, what's in the CKAN.schema file).
/// </summary>

// Base class for both modules (installed via the CKAN) and bundled
// modules (which are more lightweight)
[JsonObject(MemberSerialization.OptIn)]
Expand Down Expand Up @@ -447,7 +447,7 @@ internal static bool UniConflicts(CkanModule mod1, CkanModule mod2)
mod1.conflicts.Any(
conflict =>
mod2.ProvidesList.Contains(conflict.name) && conflict.version_within_bounds(mod2.version));
}
}

/// <summary>
/// Returns true if our mod is compatible with the KSP version specified.
Expand All @@ -462,29 +462,35 @@ public bool IsCompatibleKSP(KspVersionCriteria version)

/// <summary>
/// Returns a human readable string indicating the highest compatible
/// version of KSP this module will run with. (Eg: 1.0.2, 1.0.2+,
/// "All version", etc).
///
/// version of KSP this module will run with. (Eg: 1.0.2,
/// "All versions", etc).
///
/// This is for *human consumption only*, as the strings may change in the
/// future as we support additional locales.
/// </summary>
public string HighestCompatibleKSP()
{
KspVersion v = LatestCompatibleKSP();
if (v.IsAny)
return "All versions";
else
return v.ToString();
}

/// <summary>
/// Returns machine readable object indicating the highest compatible
/// version of KSP this module will run with.
/// </summary>
public KspVersion LatestCompatibleKSP()
{
// Find the highest compatible KSP version
if (ksp_version_max != null)
{
return ksp_version_max.ToString();
}
return ksp_version_max;
else if (ksp_version != null)
{
return ksp_version.ToString();
}
else if (ksp_version_min != null )
{
return ksp_version_min + "+";
}

return "All versions";
return ksp_version;
else
// No upper limit.
return KspVersion.Any;
}

/// <summary>
Expand Down Expand Up @@ -601,4 +607,3 @@ public override string ToString()
}
}
}

7 changes: 3 additions & 4 deletions GUI/GUIMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,13 @@ public GUIMod(CkanModule mod, IRegistryQuerier registry, KspVersionCriteria curr
// use that.
if (IsCKAN)
latestAvailableForAnyVersion = (CkanModule) mod;

}

// If there's known information for this mod in any form, calculate the highest compatible
// KSP.
if (latestAvailableForAnyVersion != null)
{
KSPCompatibility = KSPCompatibilityLong = latestAvailableForAnyVersion.HighestCompatibleKSP();
KSPCompatibility = KSPCompatibilityLong = registry.LatestCompatibleKSP(mod.identifier)?.ToString() ?? "";

// If the mod we have installed is *not* the mod we have installed, or we don't know
// what we have installed, indicate that an upgrade would be needed.
Expand Down Expand Up @@ -128,7 +127,7 @@ public GUIMod(CkanModule mod, IRegistryQuerier registry, KspVersionCriteria curr
KSPversion = kspVersion != null ? kspVersion.ToString() : "-";

Abstract = mod.@abstract;

// If we have a homepage provided, use that; otherwise use the spacedock page, curse page or the github repo so that users have somewhere to get more info than just the abstract.

Homepage = "N/A";
Expand Down Expand Up @@ -160,7 +159,7 @@ public GUIMod(CkanModule mod, IRegistryQuerier registry, KspVersionCriteria curr
DownloadSize = "1<KB";
else
DownloadSize = mod.download_size / 1024+"";

Abbrevation = new string(mod.name.Split(' ').
Where(s => s.Length > 0).Select(s => s[0]).ToArray());

Expand Down