-
-
Notifications
You must be signed in to change notification settings - Fork 345
/
Copy pathMain.cs
1129 lines (941 loc) · 40.4 KB
/
Main.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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using CKAN.Versioning;
using CKAN.Exporters;
using CKAN.Properties;
using CKAN.Types;
using log4net;
using Timer = System.Windows.Forms.Timer;
namespace CKAN
{
public enum GUIModFilter
{
Compatible = 0,
Installed = 1,
InstalledUpdateAvailable = 2,
NewInRepository = 3,
NotInstalled = 4,
Incompatible = 5,
All = 6,
Cached = 7
}
public enum GUIModChangeType
{
None = 0,
Install = 1,
Remove = 2,
Update = 3
}
public partial class Main : Form
{
private static readonly ILog log = LogManager.GetLogger(typeof(Main));
public delegate void ModChangedCallback(CkanModule module, GUIModChangeType change);
public static event ModChangedCallback modChangedCallback;
public Configuration configuration;
public ControlFactory controlFactory;
public TabController tabController;
public PluginController pluginController;
public volatile KSPManager manager;
public KSP CurrentInstance
{
get { return manager.CurrentInstance; }
}
public KSPManager Manager
{
get { return manager; }
set { manager = value; }
}
public MainModList mainModList { get; }
public NavigationHistory<GUIMod> navHistory;
public string[] commandLineArgs;
public GUIUser currentUser;
private Timer filterTimer;
private DateTime lastSearchTime;
private string lastSearchKey;
private IEnumerable<ModChange> currentChangeSet;
private Dictionary<GUIMod, string> conflicts;
private IEnumerable<ModChange> ChangeSet
{
get { return currentChangeSet; }
set
{
var orig = currentChangeSet;
currentChangeSet = value;
if (!ReferenceEquals(orig, value))
ChangeSetUpdated();
}
}
private Dictionary<GUIMod, string> Conflicts
{
get { return conflicts; }
set
{
var orig = conflicts;
conflicts = value;
if (orig != value)
ConflictsUpdated();
}
}
private void ConflictsUpdated()
{
if (Conflicts == null) {
// Clear status bar if no conflicts
AddStatusMessage("");
}
foreach (DataGridViewRow row in ModList.Rows)
{
GUIMod module = (GUIMod)row.Tag;
string value;
if (Conflicts != null && Conflicts.TryGetValue(module, out value))
{
string conflict_text = value;
foreach (DataGridViewCell cell in row.Cells)
{
cell.ToolTipText = conflict_text;
}
if (row.DefaultCellStyle.BackColor != Color.LightCoral)
{
row.DefaultCellStyle.BackColor = Color.LightCoral;
ModList.InvalidateRow(row.Index);
}
}
else if (row.DefaultCellStyle.BackColor != Color.Empty)
{
foreach (DataGridViewCell cell in row.Cells)
{
cell.ToolTipText = null;
}
row.DefaultCellStyle.BackColor = Color.Empty;
ModList.InvalidateRow(row.Index);
}
}
}
private void ChangeSetUpdated()
{
if (ChangeSet != null && ChangeSet.Any())
{
UpdateChangesDialog(ChangeSet.ToList(), installWorker);
tabController.ShowTab("ChangesetTabPage", 1, false);
ApplyToolButton.Enabled = true;
}
else
{
tabController.HideTab("ChangesetTabPage");
ApplyToolButton.Enabled = false;
}
}
public Main(string[] cmdlineArgs, KSPManager mgr, GUIUser user, bool showConsole)
{
log.Info("Starting the GUI");
commandLineArgs = cmdlineArgs;
// These are used by KSPManager's constructor to show messages about directory creation
user.displayMessage = AddStatusMessage;
user.displayError = ErrorDialog;
manager = mgr ?? new KSPManager(user);
currentUser = user;
controlFactory = new ControlFactory();
Instance = this;
mainModList = new MainModList(source => UpdateFilters(this), TooManyModsProvide, user);
// History is read-only until the UI is started. We switch
// out of it at the end of OnLoad() when we call NavInit().
navHistory = new NavigationHistory<GUIMod> { IsReadOnly = true };
InitializeComponent();
// Replace mono's broken, ugly toolstrip renderer
menuStrip1.Renderer = new FlatToolStripRenderer();
menuStrip2.Renderer = new FlatToolStripRenderer();
fileToolStripMenuItem.DropDown.Renderer = new FlatToolStripRenderer();
settingsToolStripMenuItem.DropDown.Renderer = new FlatToolStripRenderer();
helpToolStripMenuItem.DropDown.Renderer = new FlatToolStripRenderer();
FilterToolButton.DropDown.Renderer = new FlatToolStripRenderer();
// We need to initialize the error dialog first to display errors.
errorDialog = controlFactory.CreateControl<ErrorDialog>();
// We want to check if our current instance is null first,
// as it may have already been set by a command-line option.
if (CurrentInstance == null && manager.GetPreferredInstance() == null)
{
Hide();
var result = new ChooseKSPInstance(!actuallyVisible).ShowDialog();
if (result == DialogResult.Cancel || result == DialogResult.Abort)
{
Application.Exit();
return;
}
}
configuration = Configuration.LoadOrCreateConfiguration
(
Path.Combine(CurrentInstance.CkanDir(), "GUIConfig.xml"),
CKAN.Repository.default_ckan_repo_uri.ToString()
);
// Check if there is any other instances already running.
// This is not entirely necessary, but we can show a nicer error message this way.
try
{
#pragma warning disable 219
var lockedReg = RegistryManager.Instance(CurrentInstance).registry;
#pragma warning restore 219
}
catch (RegistryInUseKraken kraken)
{
errorDialog.ShowErrorDialog(kraken.ToString());
return;
}
FilterToolButton.MouseHover += (sender, args) => FilterToolButton.ShowDropDown();
launchKSPToolStripMenuItem.MouseHover += (sender, args) => launchKSPToolStripMenuItem.ShowDropDown();
ApplyToolButton.MouseHover += (sender, args) => ApplyToolButton.ShowDropDown();
ModList.CurrentCellDirtyStateChanged += ModList_CurrentCellDirtyStateChanged;
ModList.CellValueChanged += ModList_CellValueChanged;
tabController = new TabController(MainTabControl);
tabController.ShowTab("ManageModsTabPage");
RecreateDialogs();
if (!showConsole)
Util.HideConsoleWindow();
// Disable the modinfo controls until a mod has been choosen.
ModInfoTabControl.SelectedModule = null;
// WinForms on Mac OS X has a nasty bug where the UI thread hogs the CPU,
// making our download speeds really slow unless you move the mouse while
// downloading. Yielding periodically addresses that.
// https://bugzilla.novell.com/show_bug.cgi?id=663433
if (Platform.IsMac)
{
var timer = new Timer { Interval = 2 };
timer.Tick += (sender, e) => { Thread.Yield(); };
timer.Start();
}
Application.Run(this);
var registry = RegistryManager.Instance(Manager.CurrentInstance);
registry?.Dispose();
}
private void ModList_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
ModList_CellContentClick(sender, null);
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch (keyData)
{
case Keys.Control | Keys.F:
ActiveControl = FilterByNameTextBox;
return true;
case Keys.Control | Keys.S:
if (ChangeSet != null && ChangeSet.Any())
ApplyToolButton_Click(null, null);
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
public static Main Instance { get; private set; }
/// <summary>
/// Form.Visible says true even when the form hasn't shown yet.
/// This value will tell the truth.
/// </summary>
private static bool actuallyVisible = false;
protected override void OnShown(EventArgs e)
{
actuallyVisible = true;
base.OnShown(e);
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
actuallyVisible = false;
base.OnFormClosed(e);
}
protected override void OnLoad(EventArgs e)
{
Location = configuration.WindowLoc;
Size = configuration.WindowSize;
WindowState = configuration.IsWindowMaximised ? FormWindowState.Maximized : FormWindowState.Normal;
try
{
splitContainer1.SplitterDistance = configuration.PanelPosition;
}
catch
{
// SplitContainer is mis-designed to throw exceptions
// if the min/max limits are exceeded rather than simply obeying them.
}
ModInfoTabControl.ModMetaSplitPosition = configuration.ModInfoPosition;
if (!configuration.CheckForUpdatesOnLaunchNoNag && AutoUpdate.CanUpdate)
{
log.Debug("Asking user if they wish for auto-updates");
if (new AskUserForAutoUpdatesDialog().ShowDialog() == DialogResult.OK)
configuration.CheckForUpdatesOnLaunch = true;
configuration.CheckForUpdatesOnLaunchNoNag = true;
configuration.Save();
}
bool autoUpdating = false;
if (configuration.CheckForUpdatesOnLaunch && AutoUpdate.CanUpdate)
{
try
{
log.Info("Making auto-update call");
AutoUpdate.Instance.FetchLatestReleaseInfo();
var latest_version = AutoUpdate.Instance.latestUpdate.Version;
var current_version = new ModuleVersion(Meta.GetVersion());
if (AutoUpdate.Instance.IsFetched() && latest_version.IsGreaterThan(current_version))
{
log.Debug("Found higher ckan version");
var release_notes = AutoUpdate.Instance.latestUpdate.ReleaseNotes;
var dialog = new NewUpdateDialog(latest_version.ToString(), release_notes);
if (dialog.ShowDialog() == DialogResult.OK)
{
UpdateCKAN();
autoUpdating = true;
}
}
}
catch (Exception exception)
{
currentUser.RaiseError($"Error in auto-update:\n\t{exception.Message}");
log.Error("Error in auto-update", exception);
}
}
m_UpdateRepoWorker = new BackgroundWorker { WorkerReportsProgress = false, WorkerSupportsCancellation = true };
m_UpdateRepoWorker.RunWorkerCompleted += PostUpdateRepo;
m_UpdateRepoWorker.DoWork += UpdateRepo;
installWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
installWorker.RunWorkerCompleted += PostInstallMods;
installWorker.DoWork += InstallMods;
var old_YesNoDialog = currentUser.displayYesNo;
currentUser.displayYesNo = YesNoDialog;
URLHandlers.RegisterURLHandler(configuration, currentUser);
currentUser.displayYesNo = old_YesNoDialog;
ApplyToolButton.Enabled = false;
CurrentInstanceUpdated();
// We would like to refresh if we're configured to refresh on startup,
// or if we have no currently available modules.
bool repoUpdateNeeded = configuration.RefreshOnStartup
|| !RegistryManager.Instance(CurrentInstance).registry.HasAnyAvailable();
// If we're auto-updating the client then we shouldn't interfere with the progress tab
if (!autoUpdating && repoUpdateNeeded)
{
UpdateRepo();
}
Text = $"CKAN {Meta.GetVersion()} - KSP {CurrentInstance.Version()} -- {CurrentInstance.GameDir()}";
if (commandLineArgs.Length >= 2)
{
var identifier = commandLineArgs[1];
if (identifier.StartsWith("//"))
identifier = identifier.Substring(2);
else if (identifier.StartsWith("ckan://"))
identifier = identifier.Substring(7);
if (identifier.EndsWith("/"))
identifier = identifier.Substring(0, identifier.Length - 1);
log.Debug("Attempting to select mod from startup parameters");
FocusMod(identifier, true, true);
ModList.Refresh();
log.Debug("Failed to select mod from startup parameters");
}
var pluginsPath = Path.Combine(CurrentInstance.CkanDir(), "Plugins");
if (!Directory.Exists(pluginsPath))
Directory.CreateDirectory(pluginsPath);
pluginController = new PluginController(pluginsPath);
CurrentInstance.RebuildKSPSubDir();
// Initialize navigation. This should be called as late as
// possible, once the UI is "settled" from its initial load.
NavInit();
log.Info("GUI started");
base.OnLoad(e);
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
// Only close the window, when the user has access to the "Exit" of the menu.
if (!menuStrip1.Enabled)
{
e.Cancel = true;
return;
}
// Copy window location to app settings
configuration.WindowLoc = Location;
// Copy window size to app settings if not maximized
configuration.WindowSize = WindowState == FormWindowState.Normal ? Size : RestoreBounds.Size;
//copy window maximized state to app settings
configuration.IsWindowMaximised = WindowState == FormWindowState.Maximized ? true : false;
// Copy panel position to app settings
configuration.PanelPosition = splitContainer1.SplitterDistance;
// Copy metadata panel split height to app settings
configuration.ModInfoPosition = ModInfoTabControl.ModMetaSplitPosition;
// Save the active filter
configuration.ActiveFilter = (int)mainModList.ModFilter;
// Save settings.
configuration.Save();
base.OnFormClosing(e);
}
private void CurrentInstanceUpdated()
{
Util.Invoke(this, () =>
{
Text = $"CKAN {Meta.GetVersion()} - KSP {CurrentInstance.Version()} -- {CurrentInstance.GameDir()}";
});
configuration = Configuration.LoadOrCreateConfiguration(
Path.Combine(CurrentInstance.CkanDir(), "GUIConfig.xml"),
CKAN.Repository.default_ckan_repo_uri.ToString()
);
if (CurrentInstance.CompatibleVersionsAreFromDifferentKsp)
{
new CompatibleKspVersionsDialog(CurrentInstance, !actuallyVisible)
.ShowDialog();
}
UpdateModsList();
ChangeSet = null;
Conflicts = null;
Filter((GUIModFilter)configuration.ActiveFilter);
}
public void UpdateCKAN()
{
ResetProgress();
ShowWaitDialog(false);
SwitchEnabledState();
ClearLog();
tabController.RenameTab("WaitTabPage", "Updating CKAN");
SetDescription($"Upgrading CKAN to {AutoUpdate.Instance.latestUpdate.Version}");
log.Info("Start ckan update");
BackgroundWorker updateWorker = new BackgroundWorker();
updateWorker.DoWork += (sender, args) => AutoUpdate.Instance.StartUpdateProcess(true, GUI.user);
updateWorker.RunWorkerAsync();
}
private void RefreshToolButton_Click(object sender, EventArgs e)
{
UpdateRepo();
}
private void MarkAllUpdatesToolButton_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in ModList.Rows)
{
var mod = (GUIMod)row.Tag;
if (mod.HasUpdate)
{
MarkModForUpdate(mod.Identifier);
}
}
// only sort by Update column if checkbox in settings checked
if (Main.Instance.configuration.AutoSortByUpdate)
{
// set new sort column
var new_sort_column = ModList.Columns[1];
var current_sort_column = ModList.Columns[configuration.SortByColumnIndex];
// Reset the glyph.
current_sort_column.HeaderCell.SortGlyphDirection = SortOrder.None;
configuration.SortByColumnIndex = new_sort_column.Index;
UpdateFilters(this);
// Selects the top row and scrolls the list to it.
DataGridViewCell cell = ModList.Rows[0].Cells[2];
ModList.CurrentCell = cell;
}
ModList.Refresh();
}
public void UpdateModContentsTree(CkanModule module, bool force = false)
{
ModInfoTabControl.UpdateModContentsTree(module, force);
}
private void ApplyToolButton_Click(object sender, EventArgs e)
{
tabController.ShowTab("ChangesetTabPage", 1);
}
private void ExitToolButton_Click(object sender, EventArgs e)
{
Close();
}
private void FilterByNameTextBox_TextChanged(object sender, EventArgs e)
{
if (Platform.IsMac)
{
// Delay updating to improve typing performance on OS X.
RunFilterUpdateTimer();
}
else
mainModList.ModNameFilter = FilterByNameTextBox.Text;
}
private void FilterByAuthorTextBox_TextChanged(object sender, EventArgs e)
{
if (Platform.IsMac)
{
// Delay updating to improve typing performance on OS X.
RunFilterUpdateTimer();
}
else
mainModList.ModAuthorFilter = FilterByAuthorTextBox.Text;
}
private void FilterByDescriptionTextBox_TextChanged(object sender, EventArgs e)
{
if (Platform.IsMac)
{
// Delay updating to improve typing performance on OS X.
RunFilterUpdateTimer();
}
else
mainModList.ModDescriptionFilter = FilterByDescriptionTextBox.Text;
}
/// <summary>
/// Start or restart a timer to update the filter after an interval since the last keypress.
/// On Mac OS X, this prevents the search field from locking up due to DataGridViews being
/// slow and key strokes being interpreted incorrectly when slowed down:
/// http://mono.1490590.n4.nabble.com/Incorrect-missing-and-duplicate-keypress-events-td4658863.html
/// </summary>
private void RunFilterUpdateTimer()
{
if (filterTimer == null)
{
filterTimer = new Timer();
filterTimer.Tick += OnFilterUpdateTimer;
filterTimer.Interval = 700;
filterTimer.Start();
}
else
{
filterTimer.Stop();
filterTimer.Start();
}
}
/// <summary>
/// Updates the filter after an interval of time has passed since the last keypress.
/// </summary>
private void OnFilterUpdateTimer(object source, EventArgs e)
{
mainModList.ModNameFilter = FilterByNameTextBox.Text;
mainModList.ModAuthorFilter = FilterByAuthorTextBox.Text;
mainModList.ModDescriptionFilter = FilterByDescriptionTextBox.Text;
filterTimer.Stop();
}
private async Task UpdateChangeSetAndConflicts(IRegistryQuerier registry)
{
IEnumerable<ModChange> full_change_set = null;
Dictionary<GUIMod, string> new_conflicts = null;
bool too_many_provides_thrown = false;
var user_change_set = mainModList.ComputeUserChangeSet();
try
{
var module_installer = ModuleInstaller.GetInstance(CurrentInstance, Manager.Cache, GUI.user);
full_change_set = await mainModList.ComputeChangeSetFromModList(registry, user_change_set, module_installer, CurrentInstance.VersionCriteria());
}
catch (InconsistentKraken k)
{
// Need to be recomputed due to ComputeChangeSetFromModList possibly changing it with too many provides handling.
AddStatusMessage(k.ShortDescription);
user_change_set = mainModList.ComputeUserChangeSet();
new_conflicts = MainModList.ComputeConflictsFromModList(registry, user_change_set, CurrentInstance.VersionCriteria());
full_change_set = null;
}
catch (TooManyModsProvideKraken)
{
// Can be thrown by ComputeChangeSetFromModList if the user cancels out of it.
// We can just rerun it as the ModInfoTabControl has been removed.
too_many_provides_thrown = true;
}
catch (DependencyNotSatisfiedKraken k)
{
GUI.user.RaiseError(
"{0} depends on {1}, which is not compatible with the currently installed version of KSP",
k.parent,
k.module
);
// Uncheck the box
MarkModForInstall(k.parent.identifier, true);
}
if (too_many_provides_thrown)
{
await UpdateChangeSetAndConflicts(registry);
new_conflicts = Conflicts;
full_change_set = ChangeSet;
}
last_mod_to_have_install_toggled.Clear();
Conflicts = new_conflicts;
ChangeSet = full_change_set;
}
private void FilterCompatibleButton_Click(object sender, EventArgs e)
{
Filter(GUIModFilter.Compatible);
}
private void FilterInstalledButton_Click(object sender, EventArgs e)
{
Filter(GUIModFilter.Installed);
}
private void FilterInstalledUpdateButton_Click(object sender, EventArgs e)
{
Filter(GUIModFilter.InstalledUpdateAvailable);
}
private void cachedToolStripMenuItem_Click(object sender, EventArgs e)
{
Filter(GUIModFilter.Cached);
}
private void FilterNewButton_Click(object sender, EventArgs e)
{
Filter(GUIModFilter.NewInRepository);
}
private void FilterNotInstalledButton_Click(object sender, EventArgs e)
{
Filter(GUIModFilter.NotInstalled);
}
private void FilterIncompatibleButton_Click(object sender, EventArgs e)
{
Filter(GUIModFilter.Incompatible);
}
private void FilterAllButton_Click(object sender, EventArgs e)
{
Filter(GUIModFilter.All);
}
private void Filter(GUIModFilter filter)
{
mainModList.ModFilter = filter;
if (filter == GUIModFilter.All)
FilterToolButton.Text = "Filter (All)";
else if (filter == GUIModFilter.Incompatible)
FilterToolButton.Text = "Filter (Incompatible)";
else if (filter == GUIModFilter.Installed)
FilterToolButton.Text = "Filter (Installed)";
else if (filter == GUIModFilter.InstalledUpdateAvailable)
FilterToolButton.Text = "Filter (Upgradeable)";
else if (filter == GUIModFilter.Cached)
FilterToolButton.Text = "Filter (Cached)";
else if (filter == GUIModFilter.NewInRepository)
FilterToolButton.Text = "Filter (New)";
else if (filter == GUIModFilter.NotInstalled)
FilterToolButton.Text = "Filter (Not installed)";
else
FilterToolButton.Text = "Filter (Compatible)";
}
private GUIMod GetSelectedModule()
{
if (ModList.SelectedRows.Count == 0)
return null;
DataGridViewRow selected_item = ModList.SelectedRows[0];
var module = (GUIMod)selected_item?.Tag;
return module;
}
private void launchKSPToolStripMenuItem_Click(object sender, EventArgs e)
{
var split = configuration.CommandLineArguments.Split(' ');
if (split.Length == 0)
return;
var binary = split[0];
var args = string.Join(" ", split.Skip(1));
try
{
Directory.SetCurrentDirectory(CurrentInstance.GameDir());
Process.Start(binary, args);
}
catch (Exception exception)
{
GUI.user.RaiseError($"Couldn't start KSP.\r\n{exception.Message}.");
}
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
new AboutDialog().ShowDialog();
}
private void KSPCommandlineToolStripMenuItem_Click(object sender, EventArgs e)
{
var dialog = new KSPCommandLineOptionsDialog();
if (dialog.ShowKSPCommandLineOptionsDialog(configuration.CommandLineArguments) == DialogResult.OK)
{
configuration.CommandLineArguments = dialog.GetResult();
configuration.Save();
}
}
private void CKANSettingsToolStripMenuItem_Click(object sender, EventArgs e)
{
// Flipping enabled here hides the main form itself.
Enabled = false;
settingsDialog.ShowDialog();
Enabled = true;
}
private void pluginsToolStripMenuItem_Click(object sender, EventArgs e)
{
Enabled = false;
pluginsDialog.ShowDialog();
Enabled = true;
}
private void installFromckanToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog open_file_dialog = new OpenFileDialog { Filter = Resources.CKANFileFilter };
if (open_file_dialog.ShowDialog() == DialogResult.OK)
{
var path = open_file_dialog.FileName;
CkanModule module;
try
{
module = CkanModule.FromFile(path);
}
catch (Kraken kraken)
{
currentUser.RaiseError(kraken.InnerException == null
? kraken.Message
: $"{kraken.Message}: {kraken.InnerException.Message}");
return;
}
catch (Exception ex)
{
currentUser.RaiseError(ex.Message);
return;
}
// We'll need to make some registry changes to do this.
RegistryManager registry_manager = RegistryManager.Instance(CurrentInstance);
// Remove this version of the module in the registry, if it exists.
registry_manager.registry.RemoveAvailable(module);
// Sneakily add our version in...
registry_manager.registry.AddAvailable(module);
menuStrip1.Enabled = false;
InstallModuleDriver(registry_manager.registry, module);
}
}
/// <summary>
/// Exports installed mods to a .ckan file.
/// </summary>
private void exportModListToolStripMenuItem_Click(object sender, EventArgs e)
{
var exportOptions = new List<ExportOption>
{
new ExportOption(ExportFileType.CkanFavourite, "CKAN favourites list (*.ckan)", "ckan"),
new ExportOption(ExportFileType.Ckan, "CKAN modpack (enforces exact mod versions) (*.ckan)", "ckan"),
new ExportOption(ExportFileType.PlainText, "Plain text (*.txt)", "txt"),
new ExportOption(ExportFileType.Markdown, "Markdown (*.md)", "md"),
new ExportOption(ExportFileType.BbCode, "BBCode (*.txt)", "txt"),
new ExportOption(ExportFileType.Csv, "Comma-separated values (*.csv)", "csv"),
new ExportOption(ExportFileType.Tsv, "Tab-separated values (*.tsv)", "tsv")
};
var filter = string.Join("|", exportOptions.Select(i => i.ToString()).ToArray());
var dlg = new SaveFileDialog
{
Filter = filter,
Title = Resources.ExportInstalledModsDialogTitle
};
if (dlg.ShowDialog() == DialogResult.OK)
{
var exportOption = exportOptions[dlg.FilterIndex - 1]; // FilterIndex is 1-indexed
if (exportOption.ExportFileType == ExportFileType.Ckan || exportOption.ExportFileType == ExportFileType.CkanFavourite)
{
bool recommends = false;
bool versions = true;
if (exportOption.ExportFileType == ExportFileType.CkanFavourite)
{
recommends = true;
versions = false;
}
// Save, just to be certain that the installed-*.ckan metapackage is generated.
RegistryManager mgr = RegistryManager.Instance(CurrentInstance);
mgr.Save(true);
mgr.ExportInstalled(dlg.FileName, recommends, versions);
}
else
{
var fileMode = File.Exists(dlg.FileName) ? FileMode.Truncate : FileMode.CreateNew;
using (var stream = new FileStream(dlg.FileName, fileMode))
{
var registry = RegistryManager.Instance(CurrentInstance).registry;
new Exporter(exportOption.ExportFileType).Export(registry, stream);
}
}
}
}
private void selectKSPInstallMenuItem_Click(object sender, EventArgs e)
{
Instance.Manager.ClearAutoStart();
var old_instance = Instance.CurrentInstance;
var result = new ChooseKSPInstance(!actuallyVisible).ShowDialog();
if (result == DialogResult.OK && !Equals(old_instance, Instance.CurrentInstance))
Instance.CurrentInstanceUpdated();
}
private void openKspDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
{
Process.Start(Instance.manager.CurrentInstance.GameDir());
}
private void CompatibleKspVersionsToolStripMenuItem_Click(object sender, EventArgs e)
{
CompatibleKspVersionsDialog dialog = new CompatibleKspVersionsDialog(
Instance.manager.CurrentInstance,
!actuallyVisible
);
if (dialog.ShowDialog() != DialogResult.Cancel)
{
// This takes a while, so don't do it if they cancel out
UpdateModsList();
}
}
public void ResetFilterAndSelectModOnList(string key)
{
FilterByNameTextBox.Text = string.Empty;
mainModList.ModNameFilter = string.Empty;
FocusMod(key, true);
}
private void FocusMod(string key, bool exactMatch, bool showAsFirst = false)
{
DataGridViewRow current_row = ModList.CurrentRow;
int currentIndex = current_row?.Index ?? 0;
DataGridViewRow first_match = null;
var does_name_begin_with_key = new Func<DataGridViewRow, bool>(row =>
{
GUIMod mod = row.Tag as GUIMod;
bool row_match;
if (exactMatch)
row_match = mod.Name == key || mod.Identifier == key;
else
row_match = mod.Name.StartsWith(key, StringComparison.OrdinalIgnoreCase) ||
mod.Abbrevation.StartsWith(key, StringComparison.OrdinalIgnoreCase) ||
mod.Identifier.StartsWith(key, StringComparison.OrdinalIgnoreCase);
if (row_match && first_match == null)
{
// Remember the first match to allow cycling back to it if necessary.
first_match = row;
}
if (key.Length == 1 && row_match && row.Index <= currentIndex)
{
// Keep going forward if it's a single key match and not ahead of the current row.
return false;
}
return row_match;
});
ModList.ClearSelection();
var rows = ModList.Rows.Cast<DataGridViewRow>().Where(row => row.Visible);
DataGridViewRow match = rows.FirstOrDefault(does_name_begin_with_key);
if (match == null && first_match != null)
{
// If there were no matches after the first match, cycle over to the beginning.
match = first_match;
}
if (match != null)
{
match.Selected = true;
// Setting this to the 'Name' cell prevents the checkbox from being toggled
// by pressing 'Space' while the row is not indicated as active.
ModList.CurrentCell = match.Cells[2];
if (showAsFirst)
ModList.FirstDisplayedScrollingRowIndex = match.Index;
}
else
{
AddStatusMessage("Not found.");
}
}
private void RecommendedModsToggleCheckbox_CheckedChanged(object sender, EventArgs e)
{
var state = ((CheckBox)sender).Checked;
foreach (ListViewItem item in RecommendedModsListView.Items)
{
if (item.Checked != state)
item.Checked = state;
}
RecommendedModsListView.Refresh();
}
private void reportAnIssueToolStripMenuItem_Click(object sender, EventArgs e)
{
Process.Start("https://github.com/KSP-CKAN/NetKAN/issues/new");
}
private void ModList_MouseDown(object sender, MouseEventArgs e)
{
var rowIndex = ModList.HitTest(e.X, e.Y).RowIndex;