Skip to content

Commit

Permalink
Allow metapackages in changesets
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Sep 23, 2024
1 parent 749e4de commit 2ec3bac
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 54 deletions.
93 changes: 47 additions & 46 deletions Core/ModuleInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ public static string CachedOrDownload(CkanModule module, string? userAgent, NetM
/// </summary>
private void DownloadModules(IEnumerable<CkanModule> mods, IDownloader downloader)
{
List<CkanModule> downloads = mods.Where(module => !Cache.IsCached(module)).ToList();

var downloads = mods.Where(module => !module.IsMetapackage && !Cache.IsCached(module))
.ToList();
if (downloads.Count > 0)
{
downloader.DownloadModules(downloads);
Expand Down Expand Up @@ -166,7 +166,9 @@ public void InstallList(ICollection<CkanModule> modules,
foreach (var module in modsToInstall)
{
User.RaiseMessage(" * {0}", Cache.DescribeAvailability(module));
if (!Cache.IsMaybeCachedZip(module))
// Alert about attempts to install DLC before downloading or installing anything
CheckKindInstallationKraken(module);
if (!module.IsMetapackage && !Cache.IsMaybeCachedZip(module))
{
downloads.Add(module);
}
Expand Down Expand Up @@ -242,8 +244,7 @@ private void Install(CkanModule module,
bool autoInstalled,
Registry registry,
ref HashSet<string>? possibleConfigOnlyDirs,
IProgress<int>? progress,
string? filename = null)
IProgress<int>? progress)
{
CheckKindInstallationKraken(module);
var version = registry.InstalledVersion(module.identifier);
Expand All @@ -256,15 +257,19 @@ private void Install(CkanModule module,
return;
}

// Find ZIP in the cache if we don't already have it.
filename ??= Cache.GetCachedFilename(module);

// If we *still* don't have a file, then kraken bitterly.
if (filename == null)
string? filename = null;
if (!module.IsMetapackage)
{
throw new FileNotFoundKraken(null,
string.Format(Properties.Resources.ModuleInstallerZIPNotInCache,
module));
// Find ZIP in the cache if we don't already have it.
filename ??= Cache.GetCachedFilename(module);

// If we *still* don't have a file, then kraken bitterly.
if (filename == null)
{
throw new FileNotFoundKraken(null,
string.Format(Properties.Resources.ModuleInstallerZIPNotInCache,
module));
}
}

using (var transaction = CkanTransaction.CreateTransactionScope())
Expand All @@ -285,19 +290,14 @@ private void Install(CkanModule module,

// Fire our callback that we've installed a module, if we have one.
onReportModInstalled?.Invoke(module);

}

/// <summary>
/// Check if the given module is a metapackage:
/// Check if the given module is a DLC:
/// if it is, throws a BadCommandKraken.
/// </summary>
private static void CheckKindInstallationKraken(CkanModule module)
{
if (module.IsMetapackage)
{
throw new BadCommandKraken(Properties.Resources.ModuleInstallerMetapackage);
}
if (module.IsDLC)
{
throw new BadCommandKraken(Properties.Resources.ModuleInstallerDLC);
Expand All @@ -313,14 +313,18 @@ private static void CheckKindInstallationKraken(CkanModule module)
/// Propagates a FileExistsKraken if we were going to overwrite a file.
/// </summary>
private List<string> InstallModule(CkanModule module,
string zip_filename,
string? zip_filename,
Registry registry,
ref HashSet<string>? possibleConfigOnlyDirs,
IProgress<int>? moduleProgress)
{
CheckKindInstallationKraken(module);
var createdPaths = new List<string>();

if (module.IsMetapackage || zip_filename == null)
{
// It's OK to include metapackages in changesets,
// but there's no work to do for them
return createdPaths;
}
using (ZipFile zipfile = new ZipFile(zip_filename))
{
var filters = ServiceLocator.Container.Resolve<IConfiguration>().GlobalInstallFilters
Expand Down Expand Up @@ -1057,23 +1061,22 @@ public HashSet<string> AddParentDirectories(HashSet<string> directories)
/// This *will* save the registry.
/// </summary>
/// <param name="add">Modules to add</param>
/// <param name="autoInstalled">true or false for each item in `add`</param>
/// <param name="remove">Modules to remove</param>
/// <param name="newModulesAreAutoInstalled">true if newly installed modules should be marked auto-installed, false otherwise</param>
private void AddRemove(ref HashSet<string>? possibleConfigOnlyDirs,
RegistryManager registry_manager,
IEnumerable<CkanModule> add,
IEnumerable<InstalledModule> remove,
bool enforceConsistency,
bool newModulesAreAutoInstalled)
ICollection<CkanModule> add,
ICollection<bool> autoInstalled,
ICollection<InstalledModule> remove,
bool enforceConsistency)
{
// TODO: We should do a consistency check up-front, rather than relying
// upon our registry catching inconsistencies at the end.

using (var tx = CkanTransaction.CreateTransactionScope())
{
remove = remove.Memoize();
add = add.Memoize();
int totSteps = remove.Count() + add.Count();
int totSteps = remove.Count + add.Count;
int step = 0;
foreach (InstalledModule instMod in remove)
{
Expand All @@ -1085,7 +1088,7 @@ private void AddRemove(ref HashSet<string>? possibleConfigOnlyDirs,
Uninstall(instMod.Module.identifier, ref possibleConfigOnlyDirs, registry_manager.registry);
}

foreach (CkanModule module in add)
foreach ((CkanModule module, bool autoInst) in add.Zip(autoInstalled))
{
var previous = remove?.FirstOrDefault(im => im.Module.identifier == module.identifier);
int percent_complete = (step++ * 70) / totSteps;
Expand All @@ -1095,7 +1098,7 @@ private void AddRemove(ref HashSet<string>? possibleConfigOnlyDirs,
// For upgrading, new modules are dependencies and should be marked auto-installed,
// for replacing, new modules are the replacements and should not be marked auto-installed
Install(module,
previous?.AutoInstalled ?? newModulesAreAutoInstalled,
previous?.AutoInstalled ?? autoInst,
registry_manager.registry,
ref possibleConfigOnlyDirs,
null);
Expand All @@ -1116,16 +1119,16 @@ private void AddRemove(ref HashSet<string>? possibleConfigOnlyDirs,
/// Will *re-install* or *downgrade* (with a warning) as well as upgrade.
/// Throws ModuleNotFoundKraken if a module is not installed.
/// </summary>
public void Upgrade(IEnumerable<CkanModule> modules,
public void Upgrade(ICollection<CkanModule> modules,
IDownloader netAsyncDownloader,
ref HashSet<string>? possibleConfigOnlyDirs,
RegistryManager registry_manager,
bool enforceConsistency = true,
bool resolveRelationships = false,
bool ConfirmPrompt = true)
{
modules = modules.Memoize();
var registry = registry_manager.registry;
var autoInstalled = modules.ToDictionary(m => m, m => true);

if (resolveRelationships)
{
Expand All @@ -1135,9 +1138,9 @@ public void Upgrade(IEnumerable<CkanModule> modules,
.OfType<CkanModule>(),
RelationshipResolverOptions.DependsOnlyOpts(),
registry,
instance.VersionCriteria()
);
modules = resolver.ModList().Memoize();
instance.VersionCriteria());
modules = resolver.ModList().ToArray();
autoInstalled = modules.ToDictionary(m => m, resolver.IsAutoInstalled);
}

User.RaiseMessage(Properties.Resources.ModuleInstallerAboutToUpgrade);
Expand Down Expand Up @@ -1258,9 +1261,9 @@ public void Upgrade(IEnumerable<CkanModule> modules,
AddRemove(ref possibleConfigOnlyDirs,
registry_manager,
modules,
modules.Select(m => autoInstalled[m]).ToArray(),
to_remove,
enforceConsistency,
true);
enforceConsistency);
User.RaiseProgress(Properties.Resources.ModuleInstallerDone, 100);
}

Expand Down Expand Up @@ -1353,14 +1356,12 @@ public void Replace(IEnumerable<ModuleReplacement> replacements,
}
var resolver = new RelationshipResolver(modsToInstall, null, options, registry_manager.registry, instance.VersionCriteria());
var resolvedModsToInstall = resolver.ModList().ToList();
AddRemove(
ref possibleConfigOnlyDirs,
registry_manager,
resolvedModsToInstall,
modsToRemove,
enforceConsistency,
false
);
AddRemove(ref possibleConfigOnlyDirs,
registry_manager,
resolvedModsToInstall,
resolvedModsToInstall.Select(m => false).ToArray(),
modsToRemove,
enforceConsistency);
User.RaiseProgress(Properties.Resources.ModuleInstallerDone, 100);
}

Expand Down
5 changes: 0 additions & 5 deletions Core/Relationships/RelationshipResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,11 +421,6 @@ private string conflictingModDescription(CkanModule? mod, CkanModule? parent)
/// </summary>
private void Add(CkanModule module, SelectionReason reason)
{
if (module.IsMetapackage)
{
AddReason(module, reason);
return;
}
if (module.IsDLC)
{
throw new ModuleIsDLCKraken(module);
Expand Down
2 changes: 1 addition & 1 deletion GUI/Main/MainChangeset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private void Changeset_OnConfirmChanges(List<ModChange> changeset)
{
Wait.StartWaiting(InstallMods, PostInstallMods, true,
new InstallArgument(
// Only pass along user requested mods, so auto-installed can be determined
// Only pass along user requested mods, so auto-installed can be determined
changeset.Where(ch => ch.Reasons.Any(r => r is SelectionReason.UserRequested)
// Include all removes and upgrades
|| ch.ChangeType != GUIModChangeType.Install)
Expand Down
1 change: 0 additions & 1 deletion GUI/Model/ModList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ public Tuple<IEnumerable<ModChange>, Dictionary<CkanModule, string>, List<string
.Union(resolver.ModList()
// Changeset already contains changes for these
.Except(extraInstalls)
.Where(m => !m.IsMetapackage)
.Select(m => new ModChange(m, GUIModChangeType.Install, resolver.ReasonsFor(m)))),
resolver.ConflictList,
resolver.ConflictDescriptions.ToList());
Expand Down
3 changes: 2 additions & 1 deletion Tests/Core/ModuleInstallerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,8 @@ public void Upgrade_WithAutoInst_RemovesAutoRemovable(string[] regularMods,
// Act
installer.Upgrade(upgradeIdentifiers.Select(ident =>
registry.LatestAvailable(ident, inst.KSP.VersionCriteria()))
.OfType<CkanModule>(),
.OfType<CkanModule>()
.ToArray(),
downloader, ref possibleConfigOnlyDirs, regMgr, false);

// Assert
Expand Down

0 comments on commit 2ec3bac

Please sign in to comment.