-
-
Notifications
You must be signed in to change notification settings - Fork 345
/
Copy pathMainInstall.cs
593 lines (530 loc) · 23.5 KB
/
MainInstall.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using CKAN.Types;
namespace CKAN
{
using ModChanges = List<ModChange>;
public partial class Main
{
private BackgroundWorker installWorker;
// used to signal the install worker that the user canceled the install process
// this may happen on the recommended/suggested mods dialogs
private volatile bool installCanceled;
// this will be the final list of mods we want to install
private HashSet<CkanModule> toInstall = new HashSet<CkanModule>();
/// <summary>
/// Initiate the GUI installer flow for one specific module
/// </summary>
/// <param name="registry">Reference to the registry</param>
/// <param name="module">Module to install</param>
public async void InstallModuleDriver(IRegistryQuerier registry, CkanModule module)
{
RelationshipResolverOptions install_ops = RelationshipResolver.DefaultOpts();
install_ops.with_recommends = false;
try
{
// Resolve the provides relationships in the dependencies
List<ModChange> fullChangeSet = new List<ModChange>(
await mainModList.ComputeChangeSetFromModList(
registry,
new HashSet<ModChange>()
{
new ModChange(
new GUIMod(
module,
registry,
CurrentInstance.VersionCriteria()
),
GUIModChangeType.Install,
null
)
},
ModuleInstaller.GetInstance(CurrentInstance, GUI.user),
CurrentInstance.VersionCriteria()
)
);
if (fullChangeSet != null && fullChangeSet.Count > 0)
{
installWorker.RunWorkerAsync(
new KeyValuePair<List<ModChange>, RelationshipResolverOptions>(
fullChangeSet,
install_ops
)
);
}
}
catch
{
// If we failed, do the clean-up normally done by PostInstallMods.
HideWaitDialog(false);
menuStrip1.Enabled = true;
}
finally
{
changeSet = null;
}
}
private void InstallMods(object sender, DoWorkEventArgs e) // this probably needs to be refactored
{
ShowWaitDialog();
installCanceled = false;
ClearLog();
var opts = (KeyValuePair<ModChanges, RelationshipResolverOptions>) e.Argument;
IRegistryQuerier registry = RegistryManager.Instance(manager.CurrentInstance).registry;
ModuleInstaller installer = ModuleInstaller.GetInstance(CurrentInstance, GUI.user);
// Avoid accumulating multiple event handlers
installer.onReportModInstalled -= OnModInstalled;
installer.onReportModInstalled += OnModInstalled;
// setup progress callback
toInstall = new HashSet<CkanModule>();
var toUninstall = new HashSet<string>();
var toUpgrade = new HashSet<string>();
// First compose sets of what the user wants installed, upgraded, and removed.
foreach (ModChange change in opts.Key)
{
switch (change.ChangeType)
{
case GUIModChangeType.Remove:
toUninstall.Add(change.Mod.Identifier);
break;
case GUIModChangeType.Update:
toUpgrade.Add(change.Mod.Identifier);
break;
case GUIModChangeType.Install:
toInstall.Add(change.Mod.ToModule());
break;
}
}
// Now work on satisifying dependencies.
var recommended = new Dictionary<CkanModule, List<string>>();
var suggested = new Dictionary<CkanModule, List<string>>();
foreach (var change in opts.Key)
{
if (change.ChangeType == GUIModChangeType.Install)
{
AddMod(change.Mod.ToModule().recommends, recommended, change.Mod.Identifier, registry);
AddMod(change.Mod.ToModule().suggests, suggested, change.Mod.Identifier, registry);
}
}
ShowSelection(recommended);
ShowSelection(suggested, true);
tabController.HideTab("ChooseRecommendedModsTabPage");
if (installCanceled)
{
tabController.HideTab("WaitTabPage");
tabController.ShowTab("ManageModsTabPage");
e.Result = new KeyValuePair<bool, ModChanges>(false, opts.Key);
return;
}
// Now let's make all our changes.
tabController.RenameTab("WaitTabPage", "Status log");
tabController.ShowTab("WaitTabPage");
tabController.SetTabLock(true);
IDownloader downloader = new NetAsyncModulesDownloader(GUI.user);
cancelCallback = () =>
{
downloader.CancelDownload();
installCanceled = true;
};
bool resolvedAllProvidedMods = false;
while (!resolvedAllProvidedMods)
{
try
{
e.Result = new KeyValuePair<bool, ModChanges>(false, opts.Key);
if (!installCanceled && toUninstall.Count > 0)
{
installer.UninstallList(toUninstall);
}
if (!installCanceled && toUpgrade.Count > 0)
{
installer.Upgrade(toUpgrade, downloader);
}
if (!installCanceled && toInstall.Count > 0)
{
installer.InstallList(toInstall, opts.Value, downloader);
}
e.Result = new KeyValuePair<bool, ModChanges>(!installCanceled, opts.Key);
if (installCanceled)
{
return;
}
resolvedAllProvidedMods = true;
}
catch (DependencyNotSatisfiedKraken ex)
{
GUI.user.RaiseMessage(
"{0} requires {1} but it is not listed in the index, or not available for your version of KSP.",
ex.parent, ex.module);
return;
}
catch (ModuleNotFoundKraken ex)
{
GUI.user.RaiseMessage(
"Module {0} required but it is not listed in the index, or not available for your version of KSP.",
ex.module);
return;
}
catch (BadMetadataKraken ex)
{
GUI.user.RaiseMessage("Bad metadata detected for module {0}: {1}", ex.module, ex.Message);
return;
}
catch (FileExistsKraken ex)
{
if (ex.owningModule != null)
{
GUI.user.RaiseMessage(
"\r\nOh no! We tried to overwrite a file owned by another mod!\r\n" +
"Please try a `ckan update` and try again.\r\n\r\n" +
"If this problem re-occurs, then it maybe a packaging bug.\r\n" +
"Please report it at:\r\n\r\n" +
"https://github.com/KSP-CKAN/NetKAN/issues/new\r\n\r\n" +
"Please including the following information in your report:\r\n\r\n" +
"File : {0}\r\n" +
"Installing Mod : {1}\r\n" +
"Owning Mod : {2}\r\n" +
"CKAN Version : {3}\r\n",
ex.filename, ex.installingModule, ex.owningModule,
Meta.GetVersion()
);
}
else
{
GUI.user.RaiseMessage(
"\r\n\r\nOh no!\r\n\r\n" +
"It looks like you're trying to install a mod which is already installed,\r\n" +
"or which conflicts with another mod which is already installed.\r\n\r\n" +
"As a safety feature, the CKAN will *never* overwrite or alter a file\r\n" +
"that it did not install itself.\r\n\r\n" +
"If you wish to install {0} via the CKAN,\r\n" +
"then please manually uninstall the mod which owns:\r\n\r\n" +
"{1}\r\n\r\n" + "and try again.\r\n",
ex.installingModule, ex.filename
);
}
GUI.user.RaiseMessage("Your GameData has been returned to its original state.\r\n");
return;
}
catch (InconsistentKraken ex)
{
// The prettiest Kraken formats itself for us.
GUI.user.RaiseMessage(ex.InconsistenciesPretty);
return;
}
catch (CancelledActionKraken)
{
return;
}
catch (MissingCertificateKraken kraken)
{
// Another very pretty kraken.
GUI.user.RaiseMessage(kraken.ToString());
return;
}
catch (DownloadThrottledKraken kraken)
{
string msg = kraken.ToString();
GUI.user.RaiseMessage(msg);
if (YesNoDialog($"{msg}\r\n\r\nOpen settings now?"))
{
// Launch the URL describing this host's throttling practices, if any
if (kraken.infoUrl != null)
{
Process.Start(new ProcessStartInfo()
{
UseShellExecute = true,
FileName = kraken.infoUrl.ToString()
});
}
// Now pretend they clicked the menu option for the settings
Enabled = false;
settingsDialog.ShowDialog();
Enabled = true;
}
return;
}
catch (ModuleDownloadErrorsKraken kraken)
{
GUI.user.RaiseMessage(kraken.ToString());
GUI.user.RaiseError(kraken.ToString());
return;
}
catch (DirectoryNotFoundKraken kraken)
{
GUI.user.RaiseMessage("\r\n{0}", kraken.Message);
return;
}
}
}
private void AddMod(
IEnumerable<RelationshipDescriptor> relations,
Dictionary<CkanModule, List<string>> chooseAble,
string identifier,
IRegistryQuerier registry)
{
if (relations == null)
return;
foreach (RelationshipDescriptor rel in relations)
{
List<CkanModule> providers = registry.LatestAvailableWithProvides(
rel.name,
CurrentInstance.VersionCriteria(),
rel
);
foreach (CkanModule provider in providers)
{
if (!registry.IsInstalled(provider.identifier)
&& !toInstall.Any(m => m.identifier == provider.identifier))
{
// We want to show this mod to the user. Add it.
List<string> dependers;
if (chooseAble.TryGetValue(provider, out dependers))
{
// Add the dependent mod to the list of reasons this dependency is shown.
dependers.Add(identifier);
}
else
{
// Add a new entry if this provider isn't listed yet.
chooseAble.Add(provider, new List<string>() { identifier });
}
}
}
}
}
private void ShowSelection(Dictionary<CkanModule, List<string>> selectable, bool suggest = false)
{
if (installCanceled)
return;
// If we're going to install something anyway, then don't list it in the
// recommended list, since they can't de-select it anyway.
// The ToList dance is because we need to modify the dictionary while iterating over it,
// and C# doesn't like that.
foreach (CkanModule module in selectable.Keys.ToList())
{
if (toInstall.Any(m => m.identifier == module.identifier))
{
selectable.Remove(module);
}
}
Dictionary<CkanModule, string> mods = GetShowableMods(selectable);
// If there are any mods that would be recommended, prompt the user to make
// selections.
if (mods.Any())
{
Util.Invoke(this, () => UpdateRecommendedDialog(mods, suggest));
tabController.ShowTab("ChooseRecommendedModsTabPage", 3);
tabController.SetTabLock(true);
lock (this)
{
Monitor.Wait(this);
}
tabController.SetTabLock(false);
}
}
private void OnModInstalled(CkanModule mod)
{
AddStatusMessage("Module \"{0}\" successfully installed", mod.name);
}
private void PostInstallMods(object sender, RunWorkerCompletedEventArgs e)
{
KeyValuePair<bool, ModChanges> result = (KeyValuePair<bool, ModChanges>) e.Result;
tabController.SetTabLock(false);
if (result.Key)
{
// Rebuilds the list of GUIMods
UpdateModsList(false, result.Value);
if (modChangedCallback != null)
{
foreach (var mod in result.Value)
{
modChangedCallback(mod.Mod, mod.ChangeType);
}
}
// install successful
AddStatusMessage("Success!");
HideWaitDialog(true);
tabController.HideTab("ChangesetTabPage");
ApplyToolButton.Enabled = false;
RetryCurrentActionButton.Visible = false;
UpdateChangesDialog(null, installWorker);
}
else
{
// There was an error
// Stay on the log dialog and re-apply the user's change set to allow retry
AddStatusMessage("Error!");
SetDescription("An error occurred, check the log for information");
UpdateChangesDialog(result.Value, installWorker);
RetryCurrentActionButton.Visible = true;
Util.Invoke(DialogProgressBar, () => DialogProgressBar.Style = ProgressBarStyle.Continuous);
Util.Invoke(DialogProgressBar, () => DialogProgressBar.Value = 0);
}
Util.Invoke(this, () => Enabled = true);
Util.Invoke(menuStrip1, () => menuStrip1.Enabled = true);
}
private TaskCompletionSource<CkanModule> toomany_source;
private void UpdateProvidedModsDialog(TooManyModsProvideKraken tooManyProvides, TaskCompletionSource<CkanModule> task)
{
toomany_source = task;
ChooseProvidedModsLabel.Text =
String.Format(
"Module {0} is provided by more than one available module, please choose one of the following mods:",
tooManyProvides.requested);
ChooseProvidedModsListView.Items.Clear();
ChooseProvidedModsListView.ItemChecked += ChooseProvidedModsListView_ItemChecked;
foreach (CkanModule module in tooManyProvides.modules)
{
ListViewItem item = new ListViewItem()
{
Tag = module,
Checked = false,
Text = CurrentInstance.Cache.IsMaybeCachedZip(module)
? $"{module.name} {module.version} (cached)"
: $"{module.name} {module.version} ({module.download.Host ?? ""}, {CkanModule.FmtSize(module.download_size)})"
};
ListViewItem.ListViewSubItem description = new ListViewItem.ListViewSubItem()
{
Text = module.@abstract
};
item.SubItems.Add(description);
ChooseProvidedModsListView.Items.Add(item);
}
ChooseProvidedModsListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
ChooseProvidedModsContinueButton.Enabled = false;
}
private void ChooseProvidedModsListView_ItemChecked(object sender, ItemCheckedEventArgs e)
{
var any_item_selected = ChooseProvidedModsListView.Items.Cast<ListViewItem>().Any(item => item.Checked);
ChooseProvidedModsContinueButton.Enabled = any_item_selected;
if (!e.Item.Checked)
{
return;
}
foreach (ListViewItem item in ChooseProvidedModsListView.Items.Cast<ListViewItem>()
.Where(item => item != e.Item && item.Checked))
{
item.Checked = false;
}
}
private void ChooseProvidedModsCancelButton_Click(object sender, EventArgs e)
{
toomany_source.SetResult(null);
}
private void ChooseProvidedModsContinueButton_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in ChooseProvidedModsListView.Items)
{
if (item.Checked)
{
toomany_source.SetResult((CkanModule)item.Tag);
}
}
}
/// <summary>
/// Tries to get every mod in the Dictionary, which can be installed
/// It also transforms the Recommender list to a string
/// </summary>
/// <param name="mods"></param>
/// <returns></returns>
private Dictionary<CkanModule, string> GetShowableMods(Dictionary<CkanModule, List<string>> mods)
{
Dictionary<CkanModule, string> modules = new Dictionary<CkanModule, string>();
var opts = new RelationshipResolverOptions
{
with_all_suggests = false,
with_recommends = false,
with_suggests = false,
without_enforce_consistency = false,
without_toomanyprovides_kraken = true
};
foreach (var pair in mods)
{
try
{
RelationshipResolver resolver = new RelationshipResolver(
new List<CkanModule> { pair.Key },
opts,
RegistryManager.Instance(manager.CurrentInstance).registry,
CurrentInstance.VersionCriteria()
);
if (resolver.ModList().Any())
{
// Resolver was able to find a way to install, so show it to the user
modules.Add(pair.Key, String.Join(",", pair.Value.ToArray()));
}
}
catch { }
}
return modules;
}
private void UpdateRecommendedDialog(Dictionary<CkanModule, string> mods, bool suggested = false)
{
if (!suggested)
{
RecommendedDialogLabel.Text =
"The following modules have been recommended by one or more of the chosen modules:";
RecommendedModsListView.Columns[1].Text = "Recommended by:";
RecommendedModsToggleCheckbox.Text = "(De-)select all recommended mods.";
RecommendedModsToggleCheckbox.Checked=true;
tabController.RenameTab("ChooseRecommendedModsTabPage", "Choose recommended mods");
}
else
{
RecommendedDialogLabel.Text =
"The following modules have been suggested by one or more of the chosen modules:";
RecommendedModsListView.Columns[1].Text = "Suggested by:";
RecommendedModsToggleCheckbox.Text = "(De-)select all suggested mods.";
RecommendedModsToggleCheckbox.Checked=false;
tabController.RenameTab("ChooseRecommendedModsTabPage", "Choose suggested mods");
}
RecommendedModsListView.Items.Clear();
foreach (var pair in mods)
{
CkanModule module = pair.Key;
ListViewItem item = new ListViewItem()
{
Tag = module,
Checked = !suggested,
Text = CurrentInstance.Cache.IsMaybeCachedZip(pair.Key)
? $"{pair.Key.name} {pair.Key.version} (cached)"
: $"{pair.Key.name} {pair.Key.version} ({pair.Key.download.Host ?? ""}, {CkanModule.FmtSize(pair.Key.download_size)})"
};
ListViewItem.ListViewSubItem recommendedBy = new ListViewItem.ListViewSubItem() { Text = pair.Value };
item.SubItems.Add(recommendedBy);
ListViewItem.ListViewSubItem description = new ListViewItem.ListViewSubItem {Text = module.@abstract};
item.SubItems.Add(description);
RecommendedModsListView.Items.Add(item);
}
}
private void RecommendedModsContinueButton_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in RecommendedModsListView.Items)
{
if (item.Checked)
{
toInstall.Add((CkanModule) item.Tag);
}
}
lock (this)
{
Monitor.Pulse(this);
}
}
private void RecommendedModsCancelButton_Click(object sender, EventArgs e)
{
installCanceled = true;
lock (this)
{
Monitor.Pulse(this);
}
}
}
}