-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProfileViewModel.cs
559 lines (495 loc) · 19.4 KB
/
ProfileViewModel.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
using GongSolutions.Wpf.DragDrop;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
namespace bagpipe {
class ProfileViewModel : ViewModelBase {
private readonly Profile profile;
public IDropTarget DropHandler { get; }
public ObservableCollection<ProfileEntryViewModel> Entries { get; }
public ProfileViewModel(Profile profile) {
this.profile = profile;
DropHandler = new ProfileDropHandler(profile, this);
Entries = new ViewModelObservableCollection<ProfileEntryViewModel, ProfileEntry>(
profile.Entries,
e => new ProfileEntryViewModel(e, this)
);
Entries.CollectionChanged += (sender, e) => {
UpdateWatchedEntries();
};
profile.ProfileLoaded += (sender, e) => {
GuessDisplayGame(e.Path);
};
}
#region Game
private Game _displayGame;
public Game DisplayGame {
get => _displayGame;
set => SetProperty(ref _displayGame, value);
}
public void GuessDisplayGame(string path) {
FileInfo info = new FileInfo(path);
if (info.Name == "Player.wsg") {
DisplayGame = Game.BL1;
return;
}
DirectoryInfo gameFolder = info.Directory;
while (gameFolder?.Parent != null && gameFolder.Parent.Name != "My Games") {
gameFolder = gameFolder.Parent;
}
switch (gameFolder.Name) {
case "Borderlands": {
DisplayGame = Game.BL1;
return;
}
case "Borderlands Game of the Year": {
DisplayGame = Game.BL1E;
return;
}
case "Borderlands 2": {
DisplayGame = Game.BL2;
return;
}
case "Borderlands The Pre-Sequel": {
DisplayGame = Game.TPS;
return;
}
case "Tiny Tina's Assault On Dragon Keep": {
DisplayGame = Game.AoDK;
return;
}
default: {
break;
}
}
ProfileEntry versionEntry = profile.Entries.FirstOrDefault(e => e.ID == 26);
if (versionEntry != null && versionEntry.Type == SettingsDataType.Int32) {
/*
BL1 1.0 uses 18, 1.5 uses 20
All the other games use the exact same value on the oldest accessible and current patch
It's probably pretty unlikely that we find a different value, but use <= just in case
*/
int version = (int)versionEntry.Value;
if (version <= 20) {
DisplayGame = Game.BL1;
return;
} else if (version <= 39) {
DisplayGame = Game.BL1E;
return;
} else if (version <= 66) {
DisplayGame = Game.BL2;
return;
} else if (version <= 70) {
DisplayGame = Game.AoDK;
return;
} else if (version <= 72) {
DisplayGame = Game.TPS;
return;
}
}
if (!profile.Entries.Any(e => e.ID == 129)) { // PlayerFOV (bl2, tps, aodk) / FOV (bl1e)
DisplayGame = Game.BL1;
} else if (profile.Entries.Any(e => e.ID == 126)) { // ShowCompass
DisplayGame = Game.BL1E;
} else if (profile.Entries.Any(e => e.ID == 168)) { // ResetCameraOnSlam
DisplayGame = Game.TPS;
} else if (profile.Entries.Any(e => e.ID == 170)) { // ShowSubtitleBackground
DisplayGame = Game.TPS;
} else {
DisplayGame = Game.BL2;
}
}
#endregion
#region Watched Entry Updates
private readonly Dictionary<KnownSettingInfo, PropertyChangedEventHandler> watchedEntryCallbacks = new Dictionary<KnownSettingInfo, PropertyChangedEventHandler>();
private void UpdateWatchedEntries() {
void UpdateWatchedEntry(KnownSettingInfo known, ref ProfileEntryViewModel entry, string property) {
ProfileEntryViewModel match = Entries.FirstOrDefault(x => known.Matches(x.ID, x.Type));
if (entry == match) {
return;
}
PropertyChangedEventHandler callback = watchedEntryCallbacks.GetValueOrDefault(known);
if (entry != null && callback != null) {
entry.PropertyChanged -= callback;
}
if (callback == null) {
callback = (sender, e) => {
if (e.PropertyName == nameof(ProfileEntryViewModel.Value)) {
InvokePropertyChanged(property);
}
};
watchedEntryCallbacks[known] = callback;
}
if (match != null) {
match.PropertyChanged += callback;
}
entry = match;
InvokePropertyChanged(property);
}
InvokePropertyChanged(nameof(HasCustomizations));
// We're firing events a bit more than we have to here, but ehh
UpdateWatchedEntry(KnownSettings.GoldenKeysEarned, ref _goldenKeysEarned, nameof(GoldenKeys));
UpdateWatchedEntry(KnownSettings.keyCount, ref _keyCount, nameof(GoldenKeys));
UpdateWatchedEntry(KnownSettings.keysSpent, ref _keysSpent, nameof(GoldenKeys));
InvokePropertyChanged(nameof(MaxGoldenKeys));
UpdateWatchedEntry(KnownSettings.StashSlot0, ref _slashSlot0, nameof(StashSlot0));
UpdateWatchedEntry(KnownSettings.StashSlot1, ref _slashSlot1, nameof(StashSlot1));
UpdateWatchedEntry(KnownSettings.StashSlot2, ref _slashSlot2, nameof(StashSlot2));
UpdateWatchedEntry(KnownSettings.StashSlot3, ref _slashSlot3, nameof(StashSlot3));
UpdateWatchedEntry(KnownSettings.BadassPoints, ref _badassPoints, nameof(BadassRank));
UpdateWatchedEntry(KnownSettings.BadassPointsSpent, ref _badassPointsSpent, nameof(BadassRank));
UpdateWatchedEntry(KnownSettings.BadassTokens, ref _badassTokens, nameof(BadassTokens));
// This is similar, but different enough we can't really use the function
ProfileEntryViewModel rewardsMatch = Entries.FirstOrDefault(x => KnownSettings.BadassRewardsEarned.Matches(x.ID, x.Type));
if (_badassRewardsEntry != rewardsMatch) {
badassRewards = new BARRewards(rewardsMatch);
PropertyChangedEventHandler callback = watchedEntryCallbacks.GetValueOrDefault(KnownSettings.BadassRewardsEarned);
if (_badassRewardsEntry != null && callback != null) {
_badassRewardsEntry.PropertyChanged -= callback;
}
if (callback == null) {
callback = (sender, e) => {
// We don't need all our values to throw changed events when we're only updating one
if (e.PropertyName == nameof(ProfileEntryViewModel.Value) && !badassRewards.InUpdate) {
foreach (string property in BADASS_REWARD_PROPERTIES) {
InvokePropertyChanged(property);
}
}
};
watchedEntryCallbacks[KnownSettings.BadassRewardsEarned] = callback;
}
if (rewardsMatch != null) {
rewardsMatch.PropertyChanged += callback;
}
_badassRewardsEntry = rewardsMatch;
foreach (string property in BADASS_REWARD_PROPERTIES) {
InvokePropertyChanged(property);
}
}
InvokePropertyChanged(nameof(HasBarRewards));
}
private void SetEntryProperty<T>(ProfileEntryViewModel entry, T value, [CallerMemberName] string property = null) {
if (property == null) {
throw new ArgumentNullException();
}
if (entry != null && value != null && !EqualityComparer<T>.Default.Equals((T)entry.Value, value)) {
entry.Value = value;
InvokePropertyChanged(property);
}
}
#endregion
#region Customizations
public bool HasCustomizations => Entries.Any(x => KnownSettings.UnlockedCustomizations.Matches(x.ID, x.Type));
private static readonly byte[] NO_CUSTOMIZATIONS = Enumerable.Repeat((byte)0, 1001).ToArray();
private static readonly byte[] ALL_CUSTOMIZATIONS = Enumerable.Repeat((byte)0xFF, 1001).ToArray();
public void UpdateCustomizations(bool unlock) {
ProfileEntryViewModel entry = Entries.FirstOrDefault(x => KnownSettings.UnlockedCustomizations.Matches(x.ID, x.Type));
if (entry != null) {
entry.Value = unlock ? ALL_CUSTOMIZATIONS : NO_CUSTOMIZATIONS;
}
}
#endregion
#region Golden Keys
private ProfileEntryViewModel _goldenKeysEarned;
private ProfileEntryViewModel _keyCount;
private ProfileEntryViewModel _keysSpent;
private int? _bl1eKeys => _keyCount == null
? null
: (int?)_keyCount.Value - Math.Max(0, (int)(_keysSpent?.Value ?? 0));
private int? _bl2Keys {
get {
if (_goldenKeysEarned == null) {
return null;
}
byte[] keyBytes = (byte[])_goldenKeysEarned.Value;
int sum = 0;
for (int i = 0; i <= (keyBytes.Length - 3); i += 3) {
sum += keyBytes[i + 1] - keyBytes[i + 2];
}
return sum;
}
}
// When we have BL2 style keys, use a conservative max to avoid 9000 byte limit
public int MaxGoldenKeys => _goldenKeysEarned != null ? 333333 : int.MaxValue;
public int? GoldenKeys {
// If we're in BL1E, and we have BL1E style keys, prefer those, otherwise prefer BL2 style keys
get => DisplayGame == Game.BL1E && _keyCount != null
? _bl1eKeys
: _bl2Keys ?? _bl1eKeys;
set {
if (value == null) {
return;
}
// Set both key styles
bool anyChange = false;
if (_keyCount != null && value != _bl1eKeys) {
anyChange = true;
_keyCount.Value = value + Math.Max(0, (int)(_keysSpent?.Value ?? 0));
}
if (_goldenKeysEarned != null && value != _bl2Keys) {
anyChange = true;
int keyNum = (int)value;
byte[] output = new byte[3 * (((keyNum - 1) / 0xFF) + 1)];
for (int i = 0; i <= (output.Length - 3); i += 3) {
if (keyNum > 0xFF) {
output[i + 1] = 0xFF;
keyNum -= 0xFF;
} else {
output[i + 1] = (byte)keyNum;
break;
}
}
_goldenKeysEarned.Value = output;
}
if (anyChange) {
InvokePropertyChanged();
}
}
}
#endregion
#region Stash
private ProfileEntryViewModel _slashSlot0;
public byte[] StashSlot0 {
get => (byte[])_slashSlot0?.Value;
set => SetEntryProperty(_slashSlot0, value);
}
private ProfileEntryViewModel _slashSlot1;
public byte[] StashSlot1 {
get => (byte[])_slashSlot1?.Value;
set => SetEntryProperty(_slashSlot1, value);
}
private ProfileEntryViewModel _slashSlot2;
public byte[] StashSlot2 {
get => (byte[])_slashSlot2?.Value;
set => SetEntryProperty(_slashSlot2, value);
}
private ProfileEntryViewModel _slashSlot3;
public byte[] StashSlot3 {
get => (byte[])_slashSlot3?.Value;
set => SetEntryProperty(_slashSlot3, value);
}
#endregion
private const int BADASS_POINTS_PER_RANK = 5;
private ProfileEntryViewModel _badassPoints;
private ProfileEntryViewModel _badassPointsSpent;
public int? BadassRank {
get => _badassPoints == null ? null : (int?)((int)_badassPoints.Value / BADASS_POINTS_PER_RANK);
set {
if (value == null) {
return;
}
int rank = (int)value * BADASS_POINTS_PER_RANK;
bool anyChange = false;
if (_badassPoints != null && (int)_badassPoints.Value != rank) {
anyChange = true;
_badassPoints.Value = rank;
}
if (_badassPointsSpent != null && (int)_badassPointsSpent.Value != rank) {
anyChange = true;
_badassPointsSpent.Value = rank;
}
if (anyChange) {
InvokePropertyChanged();
}
}
}
private ProfileEntryViewModel _badassTokens;
public int? BadassTokens {
get => (int?)_badassTokens?.Value;
set => SetEntryProperty(_badassTokens, value);
}
#region BAR Rewards
private ProfileEntryViewModel _badassRewardsEntry;
private BARRewards badassRewards;
public bool HasBarRewards => _badassRewardsEntry != null;
private readonly string[] BADASS_REWARD_PROPERTIES = new string[] {
nameof(CritDamage),
nameof(CritDamageInterval),
nameof(ElementalChance),
nameof(ElementalChanceInterval),
nameof(ElementalDamage),
nameof(ElementalDamageInterval),
nameof(FireRate),
nameof(FireRateInterval),
nameof(GrenadeDamage),
nameof(GrenadeDamageInterval),
nameof(GunAccuracy),
nameof(GunAccuracyInterval),
nameof(GunDamage),
nameof(GunDamageInterval),
nameof(MaxHealth),
nameof(MaxHealthInterval),
nameof(MeleeDamage),
nameof(MeleeDamageInterval),
nameof(RecoilReduction),
nameof(RecoilReductionInterval),
nameof(ReloadSpeed),
nameof(ReloadSpeedInterval),
nameof(ShieldCapacity),
nameof(ShieldCapacityInterval),
nameof(ShieldDelay),
nameof(ShieldDelayInterval),
nameof(ShieldRate),
nameof(ShieldRateInterval)
};
public double? CritDamage {
get => badassRewards?[BARRewardStat.CritDamage];
set {
if (value != badassRewards[BARRewardStat.CritDamage]) {
badassRewards[BARRewardStat.CritDamage] = value;
InvokePropertyChanged(nameof(CritDamage));
InvokePropertyChanged(nameof(CritDamageInterval));
}
}
}
public double CritDamageInterval => badassRewards?.GetInterval(BARRewardStat.CritDamage) ?? 1;
public double? ElementalChance {
get => badassRewards?[BARRewardStat.ElementalChance];
set {
if (value != badassRewards[BARRewardStat.ElementalChance]) {
badassRewards[BARRewardStat.ElementalChance] = value;
InvokePropertyChanged(nameof(ElementalChance));
InvokePropertyChanged(nameof(ElementalChanceInterval));
}
}
}
public double ElementalChanceInterval => badassRewards?.GetInterval(BARRewardStat.ElementalChance) ?? 1;
public double? ElementalDamage {
get => badassRewards?[BARRewardStat.ElementalDamage];
set {
if (value != badassRewards[BARRewardStat.ElementalDamage]) {
badassRewards[BARRewardStat.ElementalDamage] = value;
InvokePropertyChanged(nameof(ElementalDamage));
InvokePropertyChanged(nameof(ElementalDamageInterval));
}
}
}
public double ElementalDamageInterval => badassRewards?.GetInterval(BARRewardStat.ElementalDamage) ?? 1;
public double? FireRate {
get => badassRewards?[BARRewardStat.FireRate];
set {
if (value != badassRewards[BARRewardStat.FireRate]) {
badassRewards[BARRewardStat.FireRate] = value;
InvokePropertyChanged(nameof(FireRate));
InvokePropertyChanged(nameof(FireRateInterval));
}
}
}
public double FireRateInterval => badassRewards?.GetInterval(BARRewardStat.FireRate) ?? 1;
public double? GrenadeDamage {
get => badassRewards?[BARRewardStat.GrenadeDamage];
set {
if (value != badassRewards[BARRewardStat.GrenadeDamage]) {
badassRewards[BARRewardStat.GrenadeDamage] = value;
InvokePropertyChanged(nameof(GrenadeDamage));
InvokePropertyChanged(nameof(GrenadeDamageInterval));
}
}
}
public double GrenadeDamageInterval => badassRewards?.GetInterval(BARRewardStat.GrenadeDamage) ?? 1;
public double? GunAccuracy {
get => badassRewards?[BARRewardStat.GunAccuracy];
set {
if (value != badassRewards[BARRewardStat.GunAccuracy]) {
badassRewards[BARRewardStat.GunAccuracy] = value;
InvokePropertyChanged(nameof(GunAccuracy));
InvokePropertyChanged(nameof(GunAccuracyInterval));
}
}
}
public double GunAccuracyInterval => badassRewards?.GetInterval(BARRewardStat.GunAccuracy) ?? 1;
public double? GunDamage {
get => badassRewards?[BARRewardStat.GunDamage];
set {
if (value != badassRewards[BARRewardStat.GunDamage]) {
badassRewards[BARRewardStat.GunDamage] = value;
InvokePropertyChanged(nameof(GunDamage));
InvokePropertyChanged(nameof(GunDamageInterval));
}
}
}
public double GunDamageInterval => badassRewards?.GetInterval(BARRewardStat.GunDamage) ?? 1;
public double? MaxHealth {
get => badassRewards?[BARRewardStat.MaxHealth];
set {
if (value != badassRewards[BARRewardStat.MaxHealth]) {
badassRewards[BARRewardStat.MaxHealth] = value;
InvokePropertyChanged(nameof(MaxHealth));
InvokePropertyChanged(nameof(MaxHealthInterval));
}
}
}
public double MaxHealthInterval => badassRewards?.GetInterval(BARRewardStat.MaxHealth) ?? 1;
public double? MeleeDamage {
get => badassRewards?[BARRewardStat.MeleeDamage];
set {
if (value != badassRewards[BARRewardStat.MeleeDamage]) {
badassRewards[BARRewardStat.MeleeDamage] = value;
InvokePropertyChanged(nameof(MeleeDamage));
InvokePropertyChanged(nameof(MeleeDamageInterval));
}
}
}
public double MeleeDamageInterval => badassRewards?.GetInterval(BARRewardStat.MeleeDamage) ?? 1;
public double? RecoilReduction {
get => badassRewards?[BARRewardStat.RecoilReduction];
set {
if (value != badassRewards[BARRewardStat.RecoilReduction]) {
badassRewards[BARRewardStat.RecoilReduction] = value;
InvokePropertyChanged(nameof(RecoilReduction));
InvokePropertyChanged(nameof(RecoilReductionInterval));
}
}
}
public double RecoilReductionInterval => badassRewards?.GetInterval(BARRewardStat.RecoilReduction) ?? 1;
public double? ReloadSpeed {
get => badassRewards?[BARRewardStat.ReloadSpeed];
set {
if (value != badassRewards[BARRewardStat.ReloadSpeed]) {
badassRewards[BARRewardStat.ReloadSpeed] = value;
InvokePropertyChanged(nameof(ReloadSpeed));
InvokePropertyChanged(nameof(ReloadSpeedInterval));
}
}
}
public double ReloadSpeedInterval => badassRewards?.GetInterval(BARRewardStat.ReloadSpeed) ?? 1;
public double? ShieldCapacity {
get => badassRewards?[BARRewardStat.ShieldCapacity];
set {
if (value != badassRewards[BARRewardStat.ShieldCapacity]) {
badassRewards[BARRewardStat.ShieldCapacity] = value;
InvokePropertyChanged(nameof(ShieldCapacity));
InvokePropertyChanged(nameof(ShieldCapacityInterval));
}
}
}
public double ShieldCapacityInterval => badassRewards?.GetInterval(BARRewardStat.ShieldCapacity) ?? 1;
public double? ShieldDelay {
get => badassRewards?[BARRewardStat.ShieldDelay];
set {
if (value != badassRewards[BARRewardStat.ShieldDelay]) {
badassRewards[BARRewardStat.ShieldDelay] = value;
InvokePropertyChanged(nameof(ShieldDelay));
InvokePropertyChanged(nameof(ShieldDelayInterval));
}
}
}
public double ShieldDelayInterval => badassRewards?.GetInterval(BARRewardStat.ShieldDelay) ?? 1;
public double? ShieldRate {
get => badassRewards?[BARRewardStat.ShieldRate];
set {
if (value != badassRewards[BARRewardStat.ShieldRate]) {
badassRewards[BARRewardStat.ShieldRate] = value;
InvokePropertyChanged(nameof(ShieldRate));
InvokePropertyChanged(nameof(ShieldRateInterval));
}
}
}
public double ShieldRateInterval => badassRewards?.GetInterval(BARRewardStat.ShieldRate) ?? 1;
#endregion
}
}