-
-
Notifications
You must be signed in to change notification settings - Fork 345
/
MainInstall.cs
437 lines (399 loc) · 17.8 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
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;
/// <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, Manager.Cache, 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
{
installCanceled = false;
ClearLog();
var opts = (KeyValuePair<ModChanges, RelationshipResolverOptions>) e.Argument;
IRegistryQuerier registry = RegistryManager.Instance(manager.CurrentInstance).registry;
ModuleInstaller installer = ModuleInstaller.GetInstance(CurrentInstance, Manager.Cache, GUI.user);
// Avoid accumulating multiple event handlers
installer.onReportModInstalled -= OnModInstalled;
installer.onReportModInstalled += OnModInstalled;
// setup progress callback
// this will be the final list of mods we want to install
HashSet<CkanModule> 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, toInstall);
AddMod(change.Mod.ToModule().suggests, suggested, change.Mod.Identifier, registry, toInstall);
}
}
ShowSelection(recommended, toInstall);
ShowSelection(suggested, toInstall, true);
tabController.HideTab("ChooseRecommendedModsTabPage");
if (installCanceled)
{
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");
ShowWaitDialog();
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 (TooManyModsProvideKraken k)
{
// Prompt user to choose which mod to use
CkanModule chosen = TooManyModsProvideCore(k).Result;
// Close the selection prompt
Util.Invoke(this, () =>
{
tabController.ShowTab("WaitTabPage");
tabController.HideTab("ChooseProvidedModsTabPage");
});
if (chosen != null)
{
// User picked a mod, queue it up for installation
toInstall.Add(chosen);
// DON'T return so we can loop around and try the above InstallList call again
}
else
{
// User cancelled, get out
tabController.ShowTab("ManageModsTabPage");
e.Result = new KeyValuePair<bool, ModChanges>(false, opts.Key);
return;
}
}
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;
}
catch (DllNotFoundException)
{
if (GUI.user.RaiseYesNoDialog("libcurl installation not found. Open wiki page for help?"))
{
Process.Start(new ProcessStartInfo()
{
UseShellExecute = true,
FileName = "https://github.com/KSP-CKAN/CKAN/wiki/libcurl"
});
}
throw;
}
}
}
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 = Manager.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);
}
}
}
}
}