Skip to content

Commit

Permalink
Merge #2233 Use shared installer code in GUI and fix reinstall problems
Browse files Browse the repository at this point in the history
  • Loading branch information
politas committed Jan 2, 2018
2 parents 8fc982b + 65bc6a6 commit 17ae4cd
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 217 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ All notable changes to this project will be documented in this file.
- [Multiple] Include invalid instances in KSPManager (#2230 by: HebaruSan; reviewed: politas)
- [Build] Check version of PowerShell in build script (#2235 by: HebaruSan; reviewed: Olympic1)
- [Multiple] Add and change logging to make INFO readable (#2236 by: HebaruSan; reviewed: politas)
- [Multiple] Use shared installer code in GUI and fix reinstall problems (#2233 by: HebaruSan; reviewed: Olympic1, politas)

### Internal

Expand Down
27 changes: 5 additions & 22 deletions Core/ModuleInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ public void InstallList(
{
if (!ksp.Cache.IsCachedZip(module.download))
{
User.RaiseMessage(" * {0} {1}", module.name, module.version);
User.RaiseMessage(" * {0} {1} ({2})", module.name, module.version, module.download.Host);
downloads.Add(module);
}
else
{
User.RaiseMessage(" * {0} {1}(cached)", module.name, module.version);
User.RaiseMessage(" * {0} {1} (cached)", module.name, module.version);
}
}

Expand Down Expand Up @@ -255,23 +255,6 @@ public void InstallList(
User.RaiseProgress("Done!\r\n", 100);
}

public ModuleResolution ResolveModules(IEnumerable<CkanModule> modules, RelationshipResolverOptions options)
{
var resolver = new RelationshipResolver(modules, options, registry_manager.registry, ksp.VersionCriteria());
return new ModuleResolution(resolver.ModList(), m => ksp.Cache.IsCachedZip(m.download));
}

public void EnsureCache(List<CkanModule> modules, IDownloader downloader = null)
{
if (!modules.Any())
{
return;
}

downloader = downloader ?? new NetAsyncModulesDownloader(User);
downloader.DownloadModules(ksp.Cache, modules);
}

public void InstallList(ModuleResolution modules, RelationshipResolverOptions options)
{
// We're about to install all our mods; so begin our transaction.
Expand Down Expand Up @@ -1037,7 +1020,7 @@ public void AddRemove(IEnumerable<CkanModule> add = null, IEnumerable<string> re
/// Will *re-install* with warning even if an upgrade is not available.
/// Throws ModuleNotFoundKraken if module is not installed, or not available.
/// </summary>
public void Upgrade(IEnumerable<string> identifiers, NetAsyncModulesDownloader netAsyncDownloader, bool enforceConsistency = true)
public void Upgrade(IEnumerable<string> identifiers, IDownloader netAsyncDownloader, bool enforceConsistency = true)
{
var options = new RelationshipResolverOptions();

Expand All @@ -1054,7 +1037,7 @@ public void Upgrade(IEnumerable<string> identifiers, NetAsyncModulesDownloader n
/// 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, NetAsyncModulesDownloader netAsyncDownloader, bool enforceConsistency = true)
public void Upgrade(IEnumerable<CkanModule> modules, IDownloader netAsyncDownloader, bool enforceConsistency = true)
{
// Start by making sure we've downloaded everything.
DownloadModules(modules, netAsyncDownloader);
Expand Down Expand Up @@ -1114,7 +1097,7 @@ public void Upgrade(IEnumerable<CkanModule> modules, NetAsyncModulesDownloader n
/// <summary>
/// Makes sure all the specified mods are downloaded.
/// </summary>
private void DownloadModules(IEnumerable<CkanModule> mods, NetAsyncModulesDownloader downloader)
private void DownloadModules(IEnumerable<CkanModule> mods, IDownloader downloader)
{
List<CkanModule> downloads = mods.Where(module => !ksp.Cache.IsCachedZip(module.download)).ToList();

Expand Down
2 changes: 1 addition & 1 deletion Core/Net/AutoUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ internal Tuple<Uri, long> RetrieveUrl(dynamic response, int whichOne)
internal dynamic MakeRequest(Uri url)
{
var web = new WebClient();
web.Headers.Add("user-agent", Net.UserAgentString);
web.Headers.Add("User-Agent", Net.UserAgentString);

try
{
Expand Down
2 changes: 1 addition & 1 deletion Core/Net/NetAsyncDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public NetAsyncDownloaderDownloadPart(Uri url, long expectedSize, string path =
size = expectedSize;
lastProgressUpdateTime = DateTime.Now;

agent.Headers.Add("user-agent", Net.UserAgentString);
agent.Headers.Add("User-Agent", Net.UserAgentString);
}
}

Expand Down
39 changes: 26 additions & 13 deletions GUI/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using CKAN.Versioning;
using CKAN.Exporters;
using CKAN.Properties;
using CKAN.Types;
Expand Down Expand Up @@ -1142,7 +1143,7 @@ private void ModList_MouseDown(object sender, MouseEventArgs e)

private void reinstallToolStripMenuItem_Click(object sender, EventArgs e)
{
var module = ModInfoTabControl.SelectedModule;
GUIMod module = ModInfoTabControl.SelectedModule;
if (module == null || !module.IsCKAN)
return;

Expand All @@ -1151,21 +1152,33 @@ private void reinstallToolStripMenuItem_Click(object sender, EventArgs e)
if (reinstallDialog.ShowYesNoDialog(confirmationText) == DialogResult.No)
return;

ModuleInstaller installer = ModuleInstaller.GetInstance(CurrentInstance, currentUser);
var resolvedMod = installer.ResolveModules(toInstall, new RelationshipResolverOptions());
IRegistryQuerier registry = RegistryManager.Instance(CurrentInstance).registry;
KspVersionCriteria versCrit = CurrentInstance.VersionCriteria();

using (var transaction = CkanTransaction.CreateTransactionScope())
// Build the list of changes, first the mod to remove:
List<ModChange> toReinstall = new List<ModChange>()
{
SetDescription($"Uninstalling {module.Name}");
if (!WasSuccessful(() => installer.UninstallList(module.Identifier)))
return;

SetDescription($"Installing {module.Name}");
if (!WasSuccessful(() => installer.InstallList(resolvedMod, new RelationshipResolverOptions())))
return;

transaction.Complete();
new ModChange(module, GUIModChangeType.Remove, null)
};
// Then everything we need to re-install:
HashSet<string> goners = registry.FindReverseDependencies(
new List<string>() { module.Identifier }
);
foreach (string id in goners)
{
toReinstall.Add(new ModChange(
mainModList.full_list_of_mod_rows[id]?.Tag as GUIMod,
GUIModChangeType.Install,
null
));
}
// Hand off to centralized [un]installer code
installWorker.RunWorkerAsync(
new KeyValuePair<List<ModChange>, RelationshipResolverOptions>(
toReinstall,
RelationshipResolver.DefaultOpts()
)
);
}

private void downloadContentsToolStripMenuItem_Click(object sender, EventArgs e)
Expand Down
Loading

0 comments on commit 17ae4cd

Please sign in to comment.