-
-
Notifications
You must be signed in to change notification settings - Fork 347
/
ManageGameInstancesDialog.cs
403 lines (353 loc) · 14.7 KB
/
ManageGameInstancesDialog.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
#if NET5_0_OR_GREATER
using System.Runtime.Versioning;
#endif
using CKAN.Versioning;
namespace CKAN.GUI
{
#if NET5_0_OR_GREATER
[SupportedOSPlatform("windows")]
#endif
public partial class ManageGameInstancesDialog : Form
{
/// <summary>
/// Initialize the game instance selection window
/// </summary>
/// <param name="mgr">Game instance manager object to provide our game instances</param>
/// <param name="centerScreen">true to center the window on the screen, false to center it on the parent</param>
/// <param name="user">IUser object reference for raising dialogs</param>
public ManageGameInstancesDialog(GameInstanceManager mgr,
bool centerScreen,
IUser user)
{
manager = mgr;
this.user = user;
InitializeComponent();
DialogResult = DialogResult.Cancel;
instanceDialog.Filter = GameFolderFilter(manager);
instanceDialog.FileOk += InstanceFileOK;
if (centerScreen)
{
StartPosition = FormStartPosition.CenterScreen;
}
if (!manager.Instances.Any())
{
manager.FindAndRegisterDefaultInstances();
}
// Set the renderer for the AddNewMenu
if (Platform.IsMono)
{
AddNewMenu.Renderer = new FlatToolStripRenderer();
InstanceListContextMenuStrip.Renderer = new FlatToolStripRenderer();
}
UpdateInstancesList();
UpdateButtonState();
}
public void UpdateInstancesList()
{
GameInstancesListView.Items.Clear();
UpdateButtonState();
var allSameGame = manager.Instances.Select(i => i.Value.game).Distinct().Count() <= 1;
var hasPlayTime = manager.Instances.Any(instance => (instance.Value.playTime?.Time ?? TimeSpan.Zero) > TimeSpan.Zero);
AddOrRemoveColumn(GameInstancesListView, Game, !allSameGame, GameInstallVersion.Index);
AddOrRemoveColumn(GameInstancesListView, GamePlayTime, hasPlayTime, GameInstallPath.Index);
GameInstancesListView.Items.AddRange(
manager.Instances.OrderByDescending(instance => instance.Value.game.FirstReleaseDate)
.ThenByDescending(instance => instance.Value.Version())
.ThenBy(instance => instance.Key)
.Select(instance => new ListViewItem(
rowItems(instance.Value, !allSameGame, hasPlayTime))
{
Tag = instance.Key
})
.ToArray());
GameInstancesListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
GameInstancesListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
}
/// <summary>
/// Generate filter string for OpenFileDialog
/// </summary>
/// <param name="mgr">Game instance manager that can tell us about the build ID files</param>
/// <returns>
/// "Build metadata files (buildID.txt;buildID64.txt)|buildID.txt;buildID64.txt"
/// </returns>
public static string GameFolderFilter(GameInstanceManager mgr)
=> Properties.Resources.GameProgramFileDescription
+ "|" + string.Join(";", mgr.AllInstanceAnchorFiles);
public bool HasSelections => GameInstancesListView.SelectedItems.Count > 0;
private void AddOrRemoveColumn(ListView listView, ColumnHeader column, bool condition, int index)
{
if (condition && !listView.Columns.Contains(column))
{
listView.Columns.Insert(index, column);
}
else if (!condition && listView.Columns.Contains(column))
{
listView.Columns.Remove(column);
}
}
private string[] rowItems(GameInstance instance, bool includeGame, bool includePlayTime)
{
var list = new List<string>
{
!instance.Valid
? string.Format(Properties.Resources.ManageGameInstancesNameColumnInvalid, instance.Name)
: !(manager.CurrentInstance?.Equals(instance) ?? false) && instance.IsMaybeLocked
? string.Format(Properties.Resources.ManageGameInstancesNameColumnLocked, instance.Name)
: instance.Name
};
if (includeGame)
{
list.Add(instance.game.ShortName);
}
list.Add(FormatVersion(instance.Version()));
if (includePlayTime)
{
list.Add(instance.playTime?.ToString() ?? "");
}
list.Add(Platform.FormatPath(instance.GameDir()));
return list.ToArray();
}
/// <summary>
/// Open the user guide when the user presses F1
/// </summary>
protected override void OnHelpRequested(HelpEventArgs evt)
{
evt.Handled = Util.TryOpenWebPage(HelpURLs.ManageInstances);
}
/// <summary>
/// Open the user guide when the user clicks the help button
/// </summary>
protected override void OnHelpButtonClicked(CancelEventArgs evt)
{
evt.Cancel = Util.TryOpenWebPage(HelpURLs.ManageInstances);
}
private static string FormatVersion(GameVersion? v)
=> v == null
? Properties.Resources.CompatibleGameVersionsDialogNone
// The BUILD component is not useful visually
: new GameVersion(v.Major, v.Minor, v.Patch).ToString() ?? "";
private void AddToCKANMenuItem_Click(object? sender, EventArgs? e)
{
try
{
if (instanceDialog.ShowDialog(this) == DialogResult.OK
&& File.Exists(instanceDialog.FileName)
&& Path.GetDirectoryName(instanceDialog.FileName) is string path)
{
var instanceName = Path.GetFileName(path);
if (string.IsNullOrWhiteSpace(instanceName))
{
instanceName = path;
}
instanceName = manager.GetNextValidInstanceName(instanceName);
manager.AddInstance(path, instanceName, user);
UpdateInstancesList();
}
}
catch (NotKSPDirKraken k)
{
user.RaiseError(Properties.Resources.ManageGameInstancesNotValid,
k.path);
}
catch (Exception exc)
{
user.RaiseError(exc.Message);
}
}
private void ImportFromSteamMenuItem_Click(object? sender, EventArgs? e)
{
var currentDirs = manager.Instances.Values
.Select(inst => inst.GameDir())
.ToHashSet(Platform.PathComparer);
var toAdd = manager.FindDefaultInstances()
.Where(inst => !currentDirs.Contains(inst.GameDir()));
foreach (var inst in toAdd)
{
manager.AddInstance(inst);
}
UpdateInstancesList();
}
private void CloneGameInstanceMenuItem_Click(object? sender, EventArgs? e)
{
if (GameInstancesListView.SelectedItems[0].Tag is string instName)
{
var old_instance = manager.CurrentInstance;
var result = new CloneGameInstanceDialog(manager, user, instName).ShowDialog(this);
if (result == DialogResult.OK && !Equals(old_instance, manager.CurrentInstance))
{
DialogResult = DialogResult.OK;
Close();
}
UpdateInstancesList();
}
}
private void SelectButton_Click(object? sender, EventArgs? e)
{
UseSelectedInstance();
}
private void UseSelectedInstance()
{
if (GameInstancesListView.SelectedItems.Count == 0)
{
return;
}
var selected = GameInstancesListView.SelectedItems[0];
if (selected?.Tag is string instName)
{
try
{
manager.SetCurrentInstance(instName);
DialogResult = DialogResult.OK;
Close();
}
catch (NotKSPDirKraken k)
{
user.RaiseError(Properties.Resources.ManageGameInstancesNotValid, k.path);
}
}
}
private void SetAsDefaultCheckbox_Click(object? sender, EventArgs? e)
{
if (SetAsDefaultCheckbox.Checked)
{
manager.ClearAutoStart();
SetAsDefaultCheckbox.Checked = false;
return;
}
var selected = GameInstancesListView.SelectedItems[0];
if (selected?.Tag is string instName)
{
try
{
manager.SetAutoStart(instName);
SetAsDefaultCheckbox.Checked = true;
}
catch (NotKSPDirKraken k)
{
user.RaiseError(Properties.Resources.ManageGameInstancesNotValid, k.path);
}
}
}
private void GameInstancesListView_SelectedIndexChanged(object? sender, EventArgs? e)
{
if (GameInstancesListView.SelectedItems[0].Tag is string instName)
{
UpdateButtonState();
if (GameInstancesListView.SelectedItems.Count == 0)
{
return;
}
SetAsDefaultCheckbox.Checked = manager.AutoStartInstance?.Equals(instName) ?? false;
}
}
private void GameInstancesListView_DoubleClick(object sender, EventArgs r)
{
if (HasSelections)
{
UseSelectedInstance();
}
}
private void GameInstancesListView_Click(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
InstanceListContextMenuStrip.Show(this, new Point(e.X, e.Y));
}
}
private void GameInstancesListView_KeyDown(object? sender, KeyEventArgs? e)
{
switch (e?.KeyCode)
{
case Keys.Apps:
InstanceListContextMenuStrip.Show(Cursor.Position);
e.Handled = true;
break;
}
}
private void OpenDirectoryMenuItem_Click(object? sender, EventArgs? e)
{
if (GameInstancesListView.SelectedItems[0].Tag is string instName)
{
string path = manager.Instances[instName].GameDir();
if (!Directory.Exists(path))
{
user.RaiseError(Properties.Resources.ManageGameInstancesDirectoryDeleted, path);
return;
}
Utilities.ProcessStartURL(path);
}
}
private void RenameButton_Click(object? sender, EventArgs? e)
{
if (GameInstancesListView.SelectedItems[0].Tag is string instName)
{
// show the dialog, and only continue if the user selected "OK"
var renameInstanceDialog = new RenameInstanceDialog();
if (renameInstanceDialog.ShowRenameInstanceDialog(instName) != DialogResult.OK)
{
return;
}
// proceed with instance rename
manager.RenameInstance(instName, renameInstanceDialog.GetResult());
UpdateInstancesList();
}
}
private void Forget_Click(object? sender, EventArgs? e)
{
foreach (var instance in GameInstancesListView.SelectedItems
.OfType<ListViewItem>()
.Select(item => item.Tag as string)
.OfType<string>())
{
manager.RemoveInstance(instance);
UpdateInstancesList();
}
}
private void UpdateButtonState()
{
if (GameInstancesListView.SelectedItems[0].Tag is string instName)
{
RenameButton.Enabled = SelectButton.Enabled = SetAsDefaultCheckbox.Enabled = CloneGameInstanceMenuItem.Enabled = HasSelections;
ForgetButton.Enabled = HasSelections && instName != manager.CurrentInstance?.Name;
ImportFromSteamMenuItem.Enabled = manager.SteamLibrary.Games.Length > 0;
}
}
private readonly GameInstanceManager manager;
private readonly IUser user;
private readonly OpenFileDialog instanceDialog = new OpenFileDialog()
{
AddExtension = false,
CheckFileExists = false,
CheckPathExists = false,
DereferenceLinks = false,
InitialDirectory = Environment.CurrentDirectory,
Multiselect = false,
};
private void InstanceFileOK(object? sender, CancelEventArgs? e)
{
if (e != null && sender is OpenFileDialog dlg)
{
// OpenFileDialog always shows shortcuts (!!!!!),
// so we have to re-enforce the filter ourselves
var chosen = Path.GetFileName(dlg.FileName);
var allowed = manager.AllInstanceAnchorFiles;
if (!allowed.Contains(chosen))
{
e.Cancel = true;
user.RaiseError(Properties.Resources.ManageGameInstancesInvalidFileSelected,
chosen,
string.Join(Environment.NewLine,
allowed.OrderBy(f => f)
.Select(f => $" - {f}")));
}
}
}
}
}