Skip to content

Commit

Permalink
Merge #1957, #1958 Select Compatible KSP versions
Browse files Browse the repository at this point in the history
Merge #1957 Select compatible KSP versions by @grzegrzk
Merge #1958 Add IntersectWith method to KspVersionRange by @dbent
  • Loading branch information
politas committed Dec 16, 2016
2 parents 4540197 + bb29084 commit 4c1f0cc
Show file tree
Hide file tree
Showing 49 changed files with 1,466 additions and 353 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ All notable changes to this project will be documented in this file.
- [NetKAN] Add regexp second test for filespecs (#1919 by: politas; reviewed: ayan4m1)
- [Core] Changed name of registry lock file to registry.lock (#1944 by: politas; reviewed: ayan4m1)
- [GUI] Modlist hides epochs by default (#1942 by: politas; reviewed: ayan4m1)
- [Core/GUI] Let users select compatible KSP versions (#1957 by: grzegrzk; reviewed: dbent, politas)
- [Core] Add IntersectWith method to KspVersionRange (#1958 by: dbent; reviewed: grzegrzk, politas)

## v1.20.1

Expand Down
2 changes: 1 addition & 1 deletion Cmdline/Action/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public int RunCommand(CKAN.KSP ksp, object raw_options)
try
{
// Check if upgrades are available, and show appropriately.
CkanModule latest = registry.LatestAvailable(mod.Key, ksp.Version());
CkanModule latest = registry.LatestAvailable(mod.Key, ksp.VersionCriteria());

log.InfoFormat("Latest {0} is {1}", mod.Key, latest);

Expand Down
2 changes: 1 addition & 1 deletion Cmdline/Action/Search.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public List<CkanModule> PerformSearch(CKAN.KSP ksp, string term)
{
var registry = RegistryManager.Instance(ksp).registry;
return registry
.Available(ksp.Version())
.Available(ksp.VersionCriteria())
.Where((module) =>
{
// Extract the description. This is an optional field and may be null.
Expand Down
2 changes: 1 addition & 1 deletion Cmdline/Action/Show.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public int RunCommand(CKAN.KSP ksp, object raw_options)
// Module was not installed, look for an exact match in the available modules,
// either by "name" (the user-friendly display name) or by identifier
CkanModule moduleToShow = registry
.Available(ksp.Version())
.Available(ksp.VersionCriteria())
.SingleOrDefault(
mod => mod.name == options.Modname
|| mod.identifier == options.Modname
Expand Down
4 changes: 2 additions & 2 deletions Cmdline/Action/Update.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public int RunCommand(CKAN.KSP ksp, object raw_options)
{
// Get a list of available modules prior to the update.
var registry = RegistryManager.Instance(ksp).registry;
available_prior = registry.Available(ksp.Version());
available_prior = registry.Available(ksp.VersionCriteria());
}

// If no repository is selected, select all.
Expand Down Expand Up @@ -56,7 +56,7 @@ public int RunCommand(CKAN.KSP ksp, object raw_options)
if (options.list_changes)
{
var registry = RegistryManager.Instance(ksp).registry;
PrintChanges(available_prior, registry.Available(ksp.Version()));
PrintChanges(available_prior, registry.Available(ksp.VersionCriteria()));
}

return Exit.OK;
Expand Down
2 changes: 1 addition & 1 deletion Cmdline/Action/Upgrade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public int RunCommand(CKAN.KSP ksp, object raw_options)
try
{
// Check if upgrades are available
var latest = registry.LatestAvailable(mod.Key, ksp.Version());
var latest = registry.LatestAvailable(mod.Key, ksp.VersionCriteria());

// This may be an unindexed mod. If so,
// skip rather than crash. See KSP-CKAN/CKAN#841.
Expand Down
2 changes: 1 addition & 1 deletion Cmdline/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ private static int Version(IUser user)

private static int Available(CKAN.KSP current_instance, IUser user)
{
List<CkanModule> available = RegistryManager.Instance(current_instance).registry.Available(current_instance.Version());
List<CkanModule> available = RegistryManager.Instance(current_instance).registry.Available(current_instance.VersionCriteria());

user.RaiseMessage("Mods available for KSP {0}", current_instance.Version());
user.RaiseMessage("");
Expand Down
3 changes: 3 additions & 0 deletions Core/CKAN-core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="CompatibleKspVersionsDto.cs" />
<Compile Include="Exporters\BbCodeExporter.cs" />
<Compile Include="Exporters\DelimeterSeperatedValueExporter.cs" />
<Compile Include="Exporters\Exporter.cs" />
Expand Down Expand Up @@ -110,6 +111,8 @@
<Compile Include="Types\GameComparator\GrasGameComparator.cs" />
<Compile Include="Types\GameComparator\StrictGameComparator.cs" />
<Compile Include="Types\GameComparator\YoyoGameComparator.cs" />
<Compile Include="Versioning\KspVersionCriteria.cs" />
<Compile Include="Types\GameComparator\BaseGameComparator.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
Expand Down
22 changes: 22 additions & 0 deletions Core/CompatibleKspVersionsDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CKAN
{
//
//This is DTO object to be serialized/deserialized as JSON
//
class CompatibleKspVersionsDto
{
public CompatibleKspVersionsDto()
{
this.CompatibleKspVersions = new List<String>();
}

public String VersionOfKspWhenWritten { get; set; }

public List<String> CompatibleKspVersions { get; set; }
}
}
3 changes: 3 additions & 0 deletions Core/GameVersionProviders/IKspBuildMap.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using CKAN.Versioning;
using System.Collections.Generic;

namespace CKAN.GameVersionProviders
{
public interface IKspBuildMap
{
KspVersion this[string buildId] { get; }

List<KspVersion> KnownVersions { get; }

void Refresh();
}
}
14 changes: 14 additions & 0 deletions Core/GameVersionProviders/KspBuildMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ public sealed class KspBuildMap : IKspBuildMap
}
}

public List<KspVersion> KnownVersions
{
get
{
EnsureBuildMap();
List<KspVersion> knownVersions = new List<KspVersion>();
foreach (var version in _jBuilds.Builds)
{
knownVersions.Add(KspVersion.Parse(version.Value));
}
return knownVersions;
}
}

public KspBuildMap(IWin32Registry registry)
{
_registry = registry;
Expand Down
54 changes: 54 additions & 0 deletions Core/KSP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using CKAN.GameVersionProviders;
using CKAN.Versioning;
using log4net;
using Newtonsoft.Json;

[assembly: InternalsVisibleTo("CKAN.Tests")]

Expand All @@ -29,6 +30,9 @@ public class KSP : IDisposable

private readonly string gameDir;
private KspVersion version;
private List<KspVersion> _compatibleVersions = new List<KspVersion>();
public KspVersion VersionOfKspWhenCompatibleVersionsWereStored { get; private set; }
public bool CompatibleVersionsAreFromDifferentKsp { get { return _compatibleVersions.Count > 0 && VersionOfKspWhenCompatibleVersionsWereStored != Version(); } }

public NetFileCache Cache { get; private set; }

Expand Down Expand Up @@ -92,9 +96,53 @@ private void Init()
foreach (DirectoryInfo subDirectory in directory.GetDirectories()) subDirectory.Delete(true);
}

LoadCompatibleVersions();

log.DebugFormat("Initialised {0}", CkanDir());
}

public void SetCompatibleVersions(List<KspVersion> compatibleVersions)
{
this._compatibleVersions = compatibleVersions.Distinct().ToList();
SaveCompatibleVersions();
}

private void SaveCompatibleVersions()
{
CompatibleKspVersionsDto compatibleKspVersionsDto = new CompatibleKspVersionsDto();

compatibleKspVersionsDto.VersionOfKspWhenWritten = Version().ToString();
compatibleKspVersionsDto.CompatibleKspVersions = _compatibleVersions.Select(v => v.ToString()).ToList();

String json = JsonConvert.SerializeObject(compatibleKspVersionsDto);
File.WriteAllText(CompatibleKspVersionsFile(), json);

this.VersionOfKspWhenCompatibleVersionsWereStored = Version();
}

private void LoadCompatibleVersions()
{
String path = CompatibleKspVersionsFile();
if (File.Exists(path))
{
string json = File.ReadAllText(path);
CompatibleKspVersionsDto compatibleKspVersionsDto = JsonConvert.DeserializeObject<CompatibleKspVersionsDto>(json);

_compatibleVersions = compatibleKspVersionsDto.CompatibleKspVersions.Select(v => KspVersion.Parse(v)).ToList();
this.VersionOfKspWhenCompatibleVersionsWereStored = KspVersion.Parse(compatibleKspVersionsDto.VersionOfKspWhenWritten);
}
}

private string CompatibleKspVersionsFile()
{
return Path.Combine(CkanDir(), "compatible_ksp_versions.json");
}

public List<KspVersion> GetCompatibleVersions()
{
return new List<KspVersion>(this._compatibleVersions);
}

#endregion

#region Destructors and Disposal
Expand Down Expand Up @@ -334,6 +382,12 @@ public KspVersion Version()
return version = DetectVersion(GameDir());
}


public KspVersionCriteria VersionCriteria ()
{
return new KspVersionCriteria(version, _compatibleVersions);
}

#endregion

#region CKAN/GameData Directory Maintenance
Expand Down
6 changes: 3 additions & 3 deletions Core/ModuleInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public static string CachedOrDownload(string identifier, Version version, Uri ur
IDownloader downloader = null
)
{
var resolver = new RelationshipResolver(modules, options, registry_manager.registry, ksp.Version());
var resolver = new RelationshipResolver(modules, options, registry_manager.registry, ksp.VersionCriteria());
var modsToInstall = resolver.ModList().ToList();
InstallList(modsToInstall, options, downloader);
}
Expand All @@ -173,7 +173,7 @@ public static string CachedOrDownload(string identifier, Version version, Uri ur
IDownloader downloader = null
)
{
var resolver = new RelationshipResolver(modules, options, registry_manager.registry, ksp.Version());
var resolver = new RelationshipResolver(modules, options, registry_manager.registry, ksp.VersionCriteria());
var modsToInstall = resolver.ModList().ToList();
List<CkanModule> downloads = new List<CkanModule> ();

Expand Down Expand Up @@ -993,7 +993,7 @@ public void Upgrade(IEnumerable<string> identifiers, NetAsyncModulesDownloader n
options.with_recommends = false;
options.with_suggests = false;

var resolver = new RelationshipResolver(identifiers.ToList(), options, registry_manager.registry, ksp.Version());
var resolver = new RelationshipResolver(identifiers.ToList(), options, registry_manager.registry, ksp.VersionCriteria());
Upgrade(resolver.ModList(), netAsyncDownloader, enforceConsistency);
}

Expand Down
4 changes: 2 additions & 2 deletions Core/Net/Repo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static int UpdateAllRepositories(RegistryManager registry_manager, KSP ks
ShowUserInconsistencies(registry_manager.registry, user);

// Return how many we got!
return registry_manager.registry.Available(ksp.Version()).Count;
return registry_manager.registry.Available(ksp.VersionCriteria()).Count;
}

public static int Update(RegistryManager registry_manager, KSP ksp, IUser user, Boolean clear = true, string repo = null)
Expand Down Expand Up @@ -414,7 +414,7 @@ private static int Update(RegistryManager registry_manager, KSP ksp, IUser user,
ShowUserInconsistencies(registry_manager.registry, user);

// Return how many we got!
return registry_manager.registry.Available(ksp.Version()).Count;
return registry_manager.registry.Available(ksp.VersionCriteria()).Count;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Core/Registry/AvailableModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void Remove(Version version)
/// <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(KspVersion ksp_version = null, RelationshipDescriptor relationship=null)
public CkanModule Latest(KspVersionCriteria ksp_version = null, RelationshipDescriptor relationship=null)
{
var available_versions = new List<Version>(module_version.Keys);
CkanModule module;
Expand Down
10 changes: 5 additions & 5 deletions Core/Registry/IRegistryQuerier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface IRegistryQuerier
/// the specified version of KSP.
/// </summary>
// TODO: This name is misleading. It's more a LatestAvailable's'
List<CkanModule> Available(KspVersion ksp_version);
List<CkanModule> Available(KspVersionCriteria ksp_version);

/// <summary>
/// Returns the latest available version of a module that
Expand All @@ -25,7 +25,7 @@ public interface IRegistryQuerier
/// If no ksp_version is provided, the latest module for *any* KSP is returned.
/// <exception cref="ModuleNotFoundKraken">Throws if asked for a non-existent module.</exception>
/// </summary>
CkanModule LatestAvailable(string identifier, KspVersion ksp_version, RelationshipDescriptor relationship_descriptor = null);
CkanModule LatestAvailable(string identifier, KspVersionCriteria ksp_version, RelationshipDescriptor relationship_descriptor = null);

/// <summary>
/// Returns the latest available version of a module that satisifes the specified version and
Expand All @@ -34,7 +34,7 @@ public interface IRegistryQuerier
/// Returns an empty list if nothing is available for our system, which includes if no such module exists.
/// If no KSP version is provided, the latest module for *any* KSP version is given.
/// </summary>
List<CkanModule> LatestAvailableWithProvides(string identifier, KspVersion ksp_version, RelationshipDescriptor relationship_descriptor = null);
List<CkanModule> LatestAvailableWithProvides(string identifier, KspVersionCriteria ksp_version, RelationshipDescriptor relationship_descriptor = null);

/// <summary>
/// Checks the sanity of the registry, to ensure that all dependencies are met,
Expand Down Expand Up @@ -65,7 +65,7 @@ public interface IRegistryQuerier
/// Returns a simple array of all incompatible modules for
/// the specified version of KSP.
/// </summary>
List<CkanModule> Incompatible(KspVersion ksp_version);
List<CkanModule> Incompatible(KspVersionCriteria ksp_version);

/// <summary>
/// Returns a dictionary of all modules installed, along with their
Expand Down Expand Up @@ -128,7 +128,7 @@ public static bool IsAutodetected(this IRegistryQuerier querier, string identifi
/// Is the mod installed and does it have a newer version compatible with version
/// We can't update AD mods
/// </summary>
public static bool HasUpdate(this IRegistryQuerier querier, string identifier, KspVersion version)
public static bool HasUpdate(this IRegistryQuerier querier, string identifier, KspVersionCriteria version)
{
CkanModule newest_version;
try
Expand Down
10 changes: 5 additions & 5 deletions Core/Registry/Registry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ public void RemoveAvailable(CkanModule module)
/// <summary>
/// <see cref="IRegistryQuerier.Available"/>
/// </summary>
public List<CkanModule> Available(KspVersion ksp_version)
public List<CkanModule> Available(KspVersionCriteria ksp_version)
{
var candidates = new List<string>(available_modules.Keys);
var compatible = new List<CkanModule>();
Expand Down Expand Up @@ -461,7 +461,7 @@ public List<CkanModule> Available(KspVersion ksp_version)
/// <summary>
/// <see cref="IRegistryQuerier.Incompatible"/>
/// </summary>
public List<CkanModule> Incompatible(KspVersion ksp_version)
public List<CkanModule> Incompatible(KspVersionCriteria ksp_version)
{
var candidates = new List<string>(available_modules.Keys);
var incompatible = new List<CkanModule>();
Expand Down Expand Up @@ -492,7 +492,7 @@ public List<CkanModule> Incompatible(KspVersion ksp_version)
// be calling LatestAvailableWithProvides()
public CkanModule LatestAvailable(
string module,
KspVersion ksp_version,
KspVersionCriteria ksp_version,
RelationshipDescriptor relationship_descriptor =null)
{
log.DebugFormat("Finding latest available for {0}", module);
Expand All @@ -514,7 +514,7 @@ public List<CkanModule> Incompatible(KspVersion ksp_version)
/// <summary>
/// <see cref = "IRegistryQuerier.LatestAvailableWithProvides" />
/// </summary>
public List<CkanModule> LatestAvailableWithProvides(string module, KspVersion ksp_version, RelationshipDescriptor relationship_descriptor = null)
public List<CkanModule> LatestAvailableWithProvides(string module, KspVersionCriteria ksp_version, RelationshipDescriptor relationship_descriptor = null)
{
// This public interface calculates a cache of modules which
// are compatible with the current version of KSP, and then
Expand All @@ -530,7 +530,7 @@ public List<CkanModule> LatestAvailableWithProvides(string module, KspVersion ks
/// the `available_for_current_version` list has been correctly
/// calculated. Not for direct public consumption. ;)
/// </summary>
private List<CkanModule> LatestAvailableWithProvides(string module, KspVersion ksp_version,
private List<CkanModule> LatestAvailableWithProvides(string module, KspVersionCriteria ksp_version,
IEnumerable<CkanModule> available_for_current_version, RelationshipDescriptor relationship_descriptor=null)
{
log.DebugFormat("Finding latest available with provides for {0}", module);
Expand Down

0 comments on commit 4c1f0cc

Please sign in to comment.