-
-
Notifications
You must be signed in to change notification settings - Fork 348
/
GUIMod.cs
343 lines (294 loc) · 13 KB
/
GUIMod.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
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq;
using CKAN.Versioning;
using CKAN.GameVersionProviders;
namespace CKAN
{
public sealed class GUIMod
{
private CkanModule Mod { get; set; }
public string Name
{
get { return Mod.name.Trim(); }
}
public bool IsInstalled { get; private set; }
public bool HasUpdate { get; private set; }
public bool IsIncompatible { get; private set; }
public bool IsAutodetected { get; private set; }
public string Authors { get; private set; }
public string InstalledVersion { get; private set; }
public string LatestVersion { get; private set; }
public string DownloadSize { get; private set; }
public bool IsCached { get; private set; }
// These indicate the maximum KSP version that the maximum available
// version of this mod can handle. The "Long" version also indicates
// to the user if a mod upgrade would be required. (#1270)
public string KSPCompatibility { get; private set; }
public string KSPCompatibilityLong { get; private set; }
public string KSPversion { get; private set; }
public string Abstract { get; private set; }
public string Homepage { get; private set; }
public string Identifier { get; private set; }
public bool IsInstallChecked { get; set; }
public bool IsUpgradeChecked { get; private set; }
public bool IsNew { get; set; }
public bool IsCKAN { get; private set; }
public string Abbrevation { get; private set; }
private bool VersionMaxWasGenerated = false;
private Dictionary<string, KspVersion> VersionsMax;
/// <summary>
/// Return whether this mod is installable.
/// Used for determining whether to show a checkbox in the leftmost column.
/// </summary>
/// <returns>
/// False if the mod is auto detected,
/// otherwise true if it's compatible or already installed,
/// otherwise false.
/// </returns>
public bool IsInstallable()
{
// Auto detected mods are never installable
if (IsAutodetected)
return false;
// Compatible mods are installable, but so are mods that are already installed
return !IsIncompatible || IsInstalled;
}
public string Version
{
get { return IsInstalled ? InstalledVersion : LatestVersion; }
}
/// <returns>
/// 0 - 9 // 9 - 99 // 99 - 999
/// 1 - 9 // 10 - 99 // 100 - 999
/// 8 - 9 // 98 - 99 //
/// </returns>
private int UptoNines(int num)
{
//if (num == 0)
// return 0;
//else
return (int)Math.Pow(10, Math.Floor(Math.Log10(num + 1)) + 1) - 1;
}
public GUIMod(CkanModule mod, IRegistryQuerier registry, KspVersionCriteria current_ksp_version, bool incompatible = false)
{
IsCKAN = mod is CkanModule;
//Currently anything which could alter these causes a full reload of the modlist
// If this is ever changed these could be moved into the properties
Mod = mod;
IsInstalled = registry.IsInstalled(mod.identifier, false);
IsInstallChecked = IsInstalled;
HasUpdate = registry.HasUpdate(mod.identifier, current_ksp_version);
IsIncompatible = incompatible || !mod.IsCompatibleKSP(current_ksp_version);
IsAutodetected = registry.IsAutodetected(mod.identifier);
Authors = mod.author == null ? "N/A" : String.Join(",", mod.author);
var installed_version = registry.InstalledVersion(mod.identifier);
ModuleVersion latest_version = null;
var ksp_version = mod.ksp_version;
try
{
var latest_available = registry.LatestAvailable(mod.identifier, current_ksp_version);
if (latest_available != null)
latest_version = latest_available.version;
}
catch (ModuleNotFoundKraken)
{
latest_version = installed_version;
}
InstalledVersion = installed_version != null ? installed_version.ToString() : "-";
// Let's try to find the compatibility for this mod. If it's not in the registry at
// all (because it's a DarkKAN mod) then this might fail.
CkanModule latest_available_for_any_ksp = null;
try
{
latest_available_for_any_ksp = registry.LatestAvailable(mod.identifier, null);
}
catch
{
// If we can't find the mod in the CKAN, but we've a CkanModule installed, then
// use that.
if (IsCKAN)
latest_available_for_any_ksp = (CkanModule) mod;
}
// If there's known information for this mod in any form, calculate the highest compatible
// KSP.
if (latest_available_for_any_ksp != null)
{
if (!VersionMaxWasGenerated)
{
VersionMaxWasGenerated = true;
List<KspVersion> versions = new KspBuildMap(new Win32Registry()).KnownVersions; // should be sorted
VersionsMax = new Dictionary<string, KspVersion>();
VersionsMax[""] = versions.Last();
foreach (var v in versions)
{
VersionsMax[v.Major.ToString()] = v; // add or replace
VersionsMax[v.Major + "." + v.Minor] = v;
}
}
const int Undefined = -1;
KspVersion ksp_ver = registry.LatestCompatibleKSP(mod.identifier);
string ver = ksp_ver?.ToString();
int major = ksp_ver.Major, minor = ksp_ver.Minor, patch = ksp_ver.Patch;
KspVersion value;
if ( major == Undefined
//|| (major >= UptoNines(VersionsMax[""].Major)) // 9.99.99
|| (major > VersionsMax[""].Major) // 2.0.0
|| (major == VersionsMax[""].Major && VersionsMax.TryGetValue(major.ToString(), out value) && minor >= UptoNines(value.Minor)) // 1.99.99 ?
)
KSPCompatibility = "any";
else if (minor != Undefined
&& VersionsMax.TryGetValue(major + "." + minor, out value)
&& (patch == Undefined || patch >= UptoNines(value.Patch))
)
KSPCompatibility = major + "." + minor + "." + UptoNines(value.Patch);
else
KSPCompatibility = ver;
// KSPCompatibility += " | " + major + "." + minor + "." + patch; // for testing
// If the mod we have installed is *not* the mod we have installed, or we don't know
// what we have installed, indicate that an upgrade would be needed.
if (installed_version == null || !latest_available_for_any_ksp.version.IsEqualTo(installed_version))
{
KSPCompatibilityLong = string.Format("{0} (using mod version {1})",
KSPCompatibility, latest_available_for_any_ksp.version);
// ver, latest_available_for_any_ksp.version); // true values in the right tab
}
else
{
KSPCompatibilityLong = KSPCompatibility;
// KSPCompatibilityLong = ver; // true values in the right tab
}
}
else
{
// No idea what this mod is, sorry!
KSPCompatibility = KSPCompatibilityLong = "unknown";
}
if (latest_version != null)
{
LatestVersion = latest_version.ToString();
}
else if (latest_available_for_any_ksp != null)
{
LatestVersion = latest_available_for_any_ksp.version.ToString();
}
else
{
LatestVersion = "-";
}
KSPversion = ksp_version != null ? ksp_version.ToString() : "-";
Abstract = mod.@abstract;
// 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.
Homepage = "N/A";
if (mod.resources != null)
{
if (mod.resources.homepage != null)
{
Homepage = mod.resources.homepage.ToString();
}
else if (mod.resources.spacedock != null)
{
Homepage = mod.resources.spacedock.ToString();
}
else if (mod.resources.curse != null)
{
Homepage = mod.resources.curse.ToString();
}
else if (mod.resources.repository != null)
{
Homepage = mod.resources.repository.ToString();
}
}
Identifier = mod.identifier;
DownloadSize = (mod.download_size == 0)
? "N/A"
: CkanModule.FmtSize(mod.download_size);
Abbrevation = new string(mod.name.Split(' ').
Where(s => s.Length > 0).Select(s => s[0]).ToArray());
UpdateIsCached();
}
public void UpdateIsCached()
{
if (Main.Instance?.CurrentInstance?.Cache == null || Mod?.download == null)
{
return;
}
IsCached = Main.Instance.CurrentInstance.Cache.IsMaybeCachedZip(Mod);
}
public CkanModule ToCkanModule()
{
if (!IsCKAN) throw new InvalidCastException("Method can not be called unless IsCKAN");
var mod = Mod as CkanModule;
return mod;
}
public CkanModule ToModule()
{
return Mod;
}
public KeyValuePair<GUIMod, GUIModChangeType>? GetRequestedChange()
{
if (IsInstalled ^ IsInstallChecked)
{
var change_type = IsInstalled ? GUIModChangeType.Remove : GUIModChangeType.Install;
return new KeyValuePair<GUIMod, GUIModChangeType>(this, change_type);
}
if (IsInstalled && (IsInstallChecked && HasUpdate && IsUpgradeChecked))
{
return new KeyValuePair<GUIMod, GUIModChangeType>(this, GUIModChangeType.Update);
}
return null;
}
public static implicit operator CkanModule(GUIMod mod)
{
return mod.ToModule();
}
public void SetUpgradeChecked(DataGridViewRow row, bool? set_value_to = null)
{
//Contract.Requires<ArgumentException>(row.Cells[1] is DataGridViewCheckBoxCell);
var update_cell = row.Cells[1] as DataGridViewCheckBoxCell;
var old_value = (bool) update_cell.Value;
bool value = set_value_to ?? old_value;
IsUpgradeChecked = value;
if (old_value != value) update_cell.Value = value;
}
public void SetInstallChecked(DataGridViewRow row, bool? set_value_to = null)
{
//Contract.Requires<ArgumentException>(row.Cells[0] is DataGridViewCheckBoxCell);
var install_cell = row.Cells[0] as DataGridViewCheckBoxCell;
if (install_cell != null)
{
bool changeTo = set_value_to ?? (bool)install_cell.Value;
if (IsInstallChecked != changeTo)
{
IsInstallChecked = changeTo;
}
// Setting this property causes ModList_CellValueChanged to be called,
// which calls SetInstallChecked again. Treat it conservatively.
if ((bool)install_cell.Value != IsInstallChecked)
{
install_cell.Value = IsInstallChecked;
// These calls are needed to force the UI to update,
// otherwise the checkbox will look checked when it's unchecked or vice versa
row.DataGridView.RefreshEdit();
row.DataGridView.Refresh();
}
}
}
private bool Equals(GUIMod other)
{
return Equals(Name, other.Name);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((GUIMod) obj);
}
public override int GetHashCode()
{
return (Name != null ? Name.GetHashCode() : 0);
}
}
}