-
-
Notifications
You must be signed in to change notification settings - Fork 345
/
MainModInfo.cs
502 lines (444 loc) · 18.2 KB
/
MainModInfo.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using CKAN.Versioning;
namespace CKAN
{
public enum RelationshipType
{
Depends = 0,
Recommends = 1,
Suggests = 2,
Supports = 3,
Conflicts = 4
}
public partial class MainModInfo : UserControl
{
private BackgroundWorker m_CacheWorker;
private GUIMod _selectedModule;
public MainModInfo()
{
InitializeComponent();
m_CacheWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
m_CacheWorker.RunWorkerCompleted += PostModCaching;
m_CacheWorker.DoWork += CacheMod;
DependsGraphTree.BeforeExpand += BeforeExpand;
}
public GUIMod SelectedModule
{
set
{
this._selectedModule = value;
if (value == null)
{
ModInfoTabControl.Enabled = false;
}
else
{
var module = value;
ModInfoTabControl.Enabled = module != null;
if (module == null) return;
UpdateModInfo(module);
UpdateModDependencyGraph(module);
UpdateModContentsTree(module);
AllModVersions.SelectedModule = module;
}
}
get
{
return _selectedModule;
}
}
public int ModMetaSplitPosition
{
get
{
return splitContainer2.SplitterDistance;
}
set
{
try
{
this.splitContainer2.SplitterDistance = value;
}
catch
{
// SplitContainer is mis-designed to throw exceptions
// if the min/max limits are exceeded rather than simply obeying them.
}
}
}
private KSPManager manager
{
get
{
return Main.Instance.manager;
}
}
public BackgroundWorker CacheWorker
{
get
{
return m_CacheWorker;
}
set
{
m_CacheWorker = value;
}
}
private void DependsGraphTree_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
Main.Instance.ResetFilterAndSelectModOnList(e.Node.Name);
}
private void ModuleRelationshipType_SelectedIndexChanged(object sender, EventArgs e)
{
GUIMod module = SelectedModule;
if (module == null) return;
UpdateModDependencyGraph(module);
}
private void ContentsPreviewTree_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
OpenFileBrowser(e.Node);
}
private void ContentsDownloadButton_Click(object sender, EventArgs e)
{
var module = SelectedModule;
if (module == null || !module.IsCKAN) return;
Main.Instance.ResetProgress();
Main.Instance.ShowWaitDialog(false);
m_CacheWorker.RunWorkerAsync(module.ToCkanModule());
}
private void LinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Util.OpenLinkFromLinkLabel(sender as LinkLabel);
}
private void UpdateModInfo(GUIMod gui_module)
{
CkanModule module = gui_module.ToModule();
Util.Invoke(MetadataModuleNameLabel, () => MetadataModuleNameLabel.Text = gui_module.Name);
Util.Invoke(MetadataModuleVersionLabel, () => MetadataModuleVersionLabel.Text = gui_module.LatestVersion.ToString());
Util.Invoke(MetadataModuleLicenseLabel, () => MetadataModuleLicenseLabel.Text = string.Join(", ", module.license));
Util.Invoke(MetadataModuleAuthorLabel, () => MetadataModuleAuthorLabel.Text = gui_module.Authors);
Util.Invoke(MetadataModuleAbstractLabel, () => MetadataModuleAbstractLabel.Text = module.@abstract);
Util.Invoke(MetadataIdentifierLabel, () => MetadataIdentifierLabel.Text = module.identifier);
// If we have a homepage provided, use that; otherwise use the spacedock page, curse page or the github repo so that users have somewhere to get more info than just the abstract.
Util.Invoke(MetadataModuleHomePageLinkLabel,
() => MetadataModuleHomePageLinkLabel.Text = gui_module.Homepage.ToString());
if (module.resources != null && module.resources.repository != null)
{
Util.Invoke(MetadataModuleGitHubLinkLabel,
() => MetadataModuleGitHubLinkLabel.Text = module.resources.repository.ToString());
}
else
{
Util.Invoke(MetadataModuleGitHubLinkLabel,
() => MetadataModuleGitHubLinkLabel.Text = "N/A");
}
if (module.release_status != null)
{
Util.Invoke(MetadataModuleReleaseStatusLabel, () => MetadataModuleReleaseStatusLabel.Text = module.release_status.ToString());
}
Util.Invoke(MetadataModuleKSPCompatibilityLabel, () => MetadataModuleKSPCompatibilityLabel.Text = gui_module.KSPCompatibilityLong);
}
private void BeforeExpand(object sender, TreeViewCancelEventArgs args)
{
// Hourglass cursor
Cursor prevCur = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
DependsGraphTree.BeginUpdate();
TreeNode node = args.Node;
IRegistryQuerier registry = RegistryManager.Instance(manager.CurrentInstance).registry;
// Should already have children, since the user is expanding it
foreach (TreeNode child in node.Nodes)
{
// If there are grandchildren, then this child has been loaded before
if (child.Nodes.Count == 0)
{
AddChildren(registry, child);
}
}
DependsGraphTree.EndUpdate();
Cursor.Current = prevCur;
}
private bool ImMyOwnGrandpa(TreeNode node)
{
CkanModule module = node.Tag as CkanModule;
if (module != null)
{
for (TreeNode other = node.Parent; other != null; other = other.Parent)
{
if (module == other.Tag)
{
return true;
}
}
}
return false;
}
private void UpdateModDependencyGraph(CkanModule module)
{
ModInfoTabControl.Tag = module ?? ModInfoTabControl.Tag;
// Can be costly. For now only update when visible.
if (ModInfoTabControl.SelectedIndex != RelationshipTabPage.TabIndex)
{
return;
}
Util.Invoke(DependsGraphTree, _UpdateModDependencyGraph);
}
private void _UpdateModDependencyGraph()
{
CkanModule module = (CkanModule)ModInfoTabControl.Tag;
DependsGraphTree.BeginUpdate();
DependsGraphTree.Nodes.Clear();
IRegistryQuerier registry = RegistryManager.Instance(manager.CurrentInstance).registry;
TreeNode root = new TreeNode($"{module.name} {module.version}", 0, 0)
{
Name = module.identifier,
Tag = module
};
DependsGraphTree.Nodes.Add(root);
AddChildren(registry, root);
root.Expand();
DependsGraphTree.EndUpdate();
}
private static readonly RelationshipType[] kindsOfRelationships = new RelationshipType[]
{
RelationshipType.Depends,
RelationshipType.Recommends,
RelationshipType.Suggests,
RelationshipType.Supports,
RelationshipType.Conflicts
};
private void AddChildren(IRegistryQuerier registry, TreeNode node)
{
// Skip children of nodes from circular dependencies
if (ImMyOwnGrandpa(node))
return;
// Load one layer of grandchildren on demand
CkanModule module = node.Tag as CkanModule;
// Tag is null for non-indexed nodes
if (module != null)
{
foreach (RelationshipType relationship in kindsOfRelationships)
{
IEnumerable<RelationshipDescriptor> relationships = null;
switch (relationship)
{
case RelationshipType.Depends:
relationships = module.depends;
break;
case RelationshipType.Recommends:
relationships = module.recommends;
break;
case RelationshipType.Suggests:
relationships = module.suggests;
break;
case RelationshipType.Supports:
relationships = module.supports;
break;
case RelationshipType.Conflicts:
relationships = module.conflicts;
break;
}
if (relationships != null)
{
foreach (RelationshipDescriptor dependency in relationships)
{
// Look for compatible mods
TreeNode child = findDependencyShallow(
registry, dependency.name, relationship,
manager.CurrentInstance.VersionCriteria())
// Then incompatible mods
?? findDependencyShallow(
registry, dependency.name, relationship, null)
// Then give up and note the name without a module
?? nonindexedNode(dependency.name, relationship);
node.Nodes.Add(child);
}
}
}
}
}
private TreeNode findDependencyShallow(IRegistryQuerier registry, string identifier, RelationshipType relationship, KspVersionCriteria crit)
{
try
{
CkanModule dependencyModule = registry.LatestAvailable(identifier, crit);
if (dependencyModule != null)
{
return indexedNode(registry, dependencyModule, relationship, crit != null);
}
}
catch (ModuleNotFoundKraken)
{
// Maybe it's a DLC?
ModuleVersion installedVersion = registry.InstalledVersion(identifier, false);
if (installedVersion != null)
{
return nonModuleNode(identifier, installedVersion, relationship);
}
// If we don't find a module by this name, look for other modules that provide it.
List<CkanModule> dependencyModules = registry.LatestAvailableWithProvides(identifier, crit);
if (dependencyModules != null && dependencyModules.Count > 0)
{
List<TreeNode> children = new List<TreeNode>();
foreach (CkanModule dep in dependencyModules)
{
children.Add(indexedNode(registry, dep, relationship, crit != null));
}
return providesNode(identifier, relationship, children);
}
}
return null;
}
private TreeNode providesNode(string identifier, RelationshipType relationship, List<TreeNode> children)
{
int icon = (int)relationship + 1;
return new TreeNode(identifier + " (virtual)", icon, icon, children.ToArray())
{
Name = identifier,
ToolTipText = relationship.ToString(),
ForeColor = Color.Gray
};
}
private TreeNode indexedNode(IRegistryQuerier registry, CkanModule module, RelationshipType relationship, bool compatible)
{
int icon = (int)relationship + 1;
string suffix = compatible ? ""
: $" ({registry.CompatibleGameVersions(module.identifier)})";
return new TreeNode($"{module.name} {module.version}{suffix}", icon, icon)
{
Name = module.identifier,
ToolTipText = relationship.ToString(),
Tag = module,
ForeColor = compatible ? Color.Empty : Color.Red
};
}
private TreeNode nonModuleNode(string identifier, ModuleVersion version, RelationshipType relationship)
{
int icon = (int)relationship + 1;
return new TreeNode($"{identifier} {version}", icon, icon)
{
Name = identifier,
ToolTipText = relationship.ToString()
};
}
private TreeNode nonindexedNode(string identifier, RelationshipType relationship)
{
// Completely nonexistent dependency, e.g. "AJE"
int icon = (int)relationship + 1;
return new TreeNode(identifier + " (not indexed)", icon, icon)
{
Name = identifier,
ToolTipText = relationship.ToString(),
ForeColor = Color.Red
};
}
// When switching tabs ensure that the resulting tab is updated.
private void ModInfoIndexChanged(object sender, EventArgs e)
{
if (ModInfoTabControl.SelectedIndex == ContentTabPage.TabIndex)
UpdateModContentsTree(null);
if (ModInfoTabControl.SelectedIndex == RelationshipTabPage.TabIndex)
UpdateModDependencyGraph(null);
}
public void UpdateModContentsTree(CkanModule module, bool force = false)
{
ModInfoTabControl.Tag = module ?? ModInfoTabControl.Tag;
//Can be costly. For now only update when visible.
if (ModInfoTabControl.SelectedIndex != ContentTabPage.TabIndex && !force)
{
return;
}
Util.Invoke(ContentsPreviewTree, () => _UpdateModContentsTree(force));
}
private CkanModule current_mod_contents_module;
private void _UpdateModContentsTree(bool force = false)
{
GUIMod guiMod = SelectedModule;
if (!guiMod.IsCKAN)
{
return;
}
CkanModule module = guiMod.ToCkanModule();
if (Equals(module, current_mod_contents_module) && !force)
{
return;
}
else
{
current_mod_contents_module = module;
}
if (!guiMod.IsCached)
{
NotCachedLabel.Text = "This mod is not in the cache, click 'Download' to preview contents";
ContentsDownloadButton.Enabled = true;
ContentsPreviewTree.Enabled = false;
}
else
{
NotCachedLabel.Text = "Module is cached, preview available";
ContentsDownloadButton.Enabled = false;
ContentsPreviewTree.Enabled = true;
}
ContentsPreviewTree.Nodes.Clear();
ContentsPreviewTree.Nodes.Add(module.name);
IEnumerable<string> contents = ModuleInstaller.GetInstance(manager.CurrentInstance, GUI.user).GetModuleContentsList(module);
if (contents == null)
{
return;
}
foreach (string item in contents)
{
ContentsPreviewTree.Nodes[0].Nodes.Add(item);
}
ContentsPreviewTree.Nodes[0].ExpandAll();
}
private void CacheMod(object sender, DoWorkEventArgs e)
{
Main.Instance.ResetProgress();
Main.Instance.ClearLog();
NetAsyncModulesDownloader downloader = new NetAsyncModulesDownloader(Main.Instance.currentUser);
downloader.DownloadModules(Main.Instance.CurrentInstance.Cache, new List<CkanModule> { (CkanModule)e.Argument });
e.Result = e.Argument;
}
public void PostModCaching(object sender, RunWorkerCompletedEventArgs e)
{
Util.Invoke(this, () => _PostModCaching((CkanModule)e.Result));
}
private void _PostModCaching(CkanModule module)
{
Main.Instance.HideWaitDialog(true);
SelectedModule?.UpdateIsCached(); ;
UpdateModContentsTree(module, true);
Main.Instance.RecreateDialogs();
}
/// <summary>
/// Opens the file browser of the users system
/// with the folder of the clicked node opened
/// TODO: Open a file broweser with the file selected
/// </summary>
/// <param name="node">A node of the ContentsPreviewTree</param>
internal void OpenFileBrowser(TreeNode node)
{
string location = node.Text;
if (File.Exists(location))
{
//We need the Folder of the file
//Otherwise the OS would try to open the file in its default application
location = Path.GetDirectoryName(location);
}
if (!Directory.Exists(location))
{
//User either selected the parent node
//or he clicked on the tree node of a cached, but not installed mod
return;
}
Process.Start(location);
}
}
}