-
Notifications
You must be signed in to change notification settings - Fork 3
/
MainWindow.xaml.cs
319 lines (296 loc) · 14.2 KB
/
MainWindow.xaml.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
using Commons.Constants;
using Commons.Models;
using Process.NET;
using Process.NET.Memory;
using Process.NET.Windows;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using WPFTest;
namespace Overlay {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
private const int GwlExstyle = -20;
private const int WsExTransparent = 0x00000020;
private DispatcherTimer _visibilityTimer;
private DispatcherTimer _processTimer;
private DispatcherTimer _listUpdateTimer;
private ProcessSharp _processSharp;
private IWindow _processWindow;
private Configuration _config;
private bool _inLobby = false;
private bool _sessionRunning = false;
private HotKey _hotKeyCopyPlayerStats;
private HotKey _hotKeyCalculateTeamsRank;
private HotKey _hotKeyCalculateTeamsTotalGames;
private HotKey _hotKeyCalculateTeamsWinRatio;
private Action _copyPlayerStats;
private Action _calculateTeamsRank;
private Action _calculateTeamsTotalGames;
private Action _calculateTeamsWinRatio;
public MainWindow() {
InitializeComponent();
StartVisibilityTimer();
StartProcessTimer();
_hotKeyCopyPlayerStats = new HotKey(Key.D1, KeyModifier.Ctrl | KeyModifier.Alt, OnHotKeyCopyPlayerStats);
_hotKeyCalculateTeamsRank = new HotKey(Key.D2, KeyModifier.Ctrl | KeyModifier.Alt, OnHotKeyCalculateTeamsRank);
_hotKeyCalculateTeamsTotalGames = new HotKey(Key.D3, KeyModifier.Ctrl | KeyModifier.Alt, OnHotKeyCalculateTeamsTotalGames);
_hotKeyCalculateTeamsWinRatio = new HotKey(Key.D4, KeyModifier.Ctrl | KeyModifier.Alt, OnHotKeyCalculateTeamsWinRatio);
}
public void RegisterHotKeyHooks(Action copyPlayerStats, Action calculateTeamsRank, Action calculateTeamsTotalGames, Action calculateTeamsWinRatio) {
_copyPlayerStats = copyPlayerStats;
_calculateTeamsRank = calculateTeamsRank;
_calculateTeamsTotalGames = calculateTeamsTotalGames;
_calculateTeamsWinRatio = calculateTeamsWinRatio;
}
public void UpdateConfiguration(Configuration configuration, bool sessionRunning) {
try {
if (configuration != null) {
_config = configuration;
_sessionRunning = sessionRunning;
UpdateVisibility();
}
} catch { }
}
public void UpdateVisibility() {
try {
bool show = true;
if (_config == null || !_config.ShowOverlay) {
show = false;
} else {
if (_config.ShowOverlayWhenInLobby) {
show = _inLobby;
}
if (show == true) {
if (_config.ShowOverlayWhenFocused && (_processWindow == null || !_processWindow.IsActivated)) {
show = false;
}
}
if (show == true && _sessionRunning == false) {
show = false;
}
}
if (Visibility == Visibility.Visible && show == false) {
Application.Current.Dispatcher.Invoke(() => Hide());
} else if (Visibility == Visibility.Hidden && show == true) {
Application.Current.Dispatcher.Invoke(() => Show());
}
UpdatePosition();
} catch (Exception e) {
System.Diagnostics.Debug.WriteLine(e.Message);
try {
if (_config != null && _config.ShowOverlay) {
Show();
}
} catch { }
}
}
public void UpdateLobbyPresence(bool inLobby) {
_inLobby = inLobby;
UpdateVisibility();
}
public void UpdatePlayers(List<Player> players, Dictionary<ulong, int> teams, bool dm) {
try {
if (players == null) {
players = new List<Player>();
}
int index = 1;
foreach (var player in players) {
Application.Current.Dispatcher.Invoke(() => {
try {
var totalGamesLabel = ((Label)GridContainer.FindName("Label" + index + "PlayerTotalGames"));
var dropsLabel = ((Label)GridContainer.FindName("Label" + index + "PlayerDrops"));
var winsLabel = ((Label)GridContainer.FindName("Label" + index + "PlayerWins"));
var locationLabel = ((Label)GridContainer.FindName("Label" + index + "PlayerLocation"));
var gamesLabel = ((Label)GridContainer.FindName("Label" + index + "PlayerGames"));
var badRepLabel = ((Label)GridContainer.FindName("Label" + index + "PlayerBadRep"));
var goodRepLabel = ((Label)GridContainer.FindName("Label" + index + "PlayerGoodRep"));
var teamLabel = ((Label)GridContainer.FindName("Label" + index + "PlayerTeam"));
var rank = "";
((Label)GridContainer.FindName("Label" + index + "PlayerName")).Content = player.Name;
if (player.Rank > 0) {
rank = player.Rank.ToString();
}
((Label)GridContainer.FindName("Label" + index + "PlayerRank")).Content = rank;
if (player.GameStats != null && player.Profile != null) {
int totalGames = !dm ? player.GameStats.TotalGamesRM : player.GameStats.TotalGamesDM;
int winRatio = !dm ? player.GameStats.WinRatioRM : player.GameStats.WinRatioDM;
int dropRatio = !dm ? player.GameStats.DropRatioRM : player.GameStats.DropRatioDM;
var games = "";
var wins = "";
var drops = "";
if (player.Profile.ProfilePrivate) {
if (player.Profile.ProfileDataFetched.HasValue) {
games = "[" + totalGames.ToString() + "]";
wins = "[" + winRatio + "%" + "]";
drops = "[" + dropRatio + "%" + "]";
} else {
games = "PRIVATE ACC";
}
} else {
games = totalGames.ToString();
wins = winRatio + "%";
drops = dropRatio + "%";
}
totalGamesLabel.Content = games;
winsLabel.Content = wins;
dropsLabel.Content = drops;
} else {
totalGamesLabel.Content = "";
winsLabel.Content = "";
dropsLabel.Content = "";
}
if (player.Profile != null && player.Profile.Location != null) {
locationLabel.Content = player.Profile.Location;
} else {
locationLabel.Content = "";
}
if (teams.TryGetValue(player.SteamId, out int value)) {
if (value > 0) {
teamLabel.Content = value;
} else {
teamLabel.Content = "";
}
} else {
teamLabel.Content = "";
}
if (player.ReputationStats != null) {
gamesLabel.Content = player.ReputationStats.Games;
badRepLabel.Content = player.ReputationStats.NegativeReputation;
goodRepLabel.Content = player.ReputationStats.PositiveReputation;
} else {
gamesLabel.Content = "";
badRepLabel.Content = "";
goodRepLabel.Content = "";
}
UpdateFieldColor(gamesLabel, player, PlayerFields.GAMES);
UpdateFieldColor(goodRepLabel, player, PlayerFields.POSITIVE_REPUTATION);
UpdateFieldColor(badRepLabel, player, PlayerFields.NEGATIVE_REPUTATION);
UpdateFieldColor(totalGamesLabel, player, PlayerFields.TOTAL_GAMES);
UpdateFieldColor(winsLabel, player, PlayerFields.WIN_RATIO);
UpdateFieldColor(dropsLabel, player, PlayerFields.DROP_RATIO);
index++;
} catch (Exception e) {
Console.WriteLine("Error while updating overlay slot: " + index);
Console.WriteLine(e.ToString());
}
});
}
} catch (Exception e) {
Console.WriteLine("Error while updating overlay list");
Console.WriteLine(e.ToString());
}
}
public void ShowMessage(string message, bool oneLiner = false) {
Application.Current.Dispatcher.Invoke(() => {
OverlayMessage.Text = message;
Height = oneLiner ? 225 : 245;
});
}
public void HideMessage() {
Application.Current.Dispatcher.Invoke(() => Height = 200);
}
private void UpdatePosition() {
var top = this._config.OverlayInactiveTopPosition;
var left = this._config.OverlayInactiveLeftPosition;
if (_processWindow != null && _processWindow.IsActivated) {
top = this._config.OverlayActiveTopPosition;
left = this._config.OverlayActiveLeftPosition;
}
if (Top != top || Left != left) {
Application.Current.Dispatcher.Invoke(() => {
Top = top;
Left = left;
});
}
}
private void UpdateFieldColor(Label label, Player player, string fieldName) {
switch (player.FieldColors[fieldName]) {
case (int)PlayerFieldColors.NONE:
label.Background = null;
break;
case (int)PlayerFieldColors.SAFE:
label.Background = SafeColor;
break;
case (int)PlayerFieldColors.DANGER:
label.Background = DangerColor;
break;
}
}
private SolidColorBrush DangerColor {
get { return new SolidColorBrush(Color.FromArgb(50, 255, 0, 0)); }
}
private SolidColorBrush SafeColor {
get { return new SolidColorBrush(Color.FromArgb(50, 0, 255, 0)); }
}
private void StartListUpdateTimer() {
_listUpdateTimer = new DispatcherTimer();
_listUpdateTimer.Tick += (sender, args) => {
};
_listUpdateTimer.Interval = new TimeSpan(0, 0, 0, 0, 500);
_listUpdateTimer.Start();
}
private void StartVisibilityTimer() {
_visibilityTimer = new DispatcherTimer();
_visibilityTimer.Tick += (sender, args) => {
UpdateVisibility();
};
_visibilityTimer.Interval = new TimeSpan(0, 0, 0, 0, 100);
_visibilityTimer.Start();
}
private void StartProcessTimer() {
_processTimer = new DispatcherTimer();
_processTimer.Tick += (sender, args) => {
try {
var process = System.Diagnostics.Process.GetProcessesByName("AoK HD").FirstOrDefault();
if (process != null) {
_processSharp = new ProcessSharp(process, MemoryType.Remote);
_processWindow = _processSharp.WindowFactory.MainWindow;
} else {
_processWindow = null;
}
} catch {
_processWindow = null;
}
};
_processTimer.Interval = new TimeSpan(0, 0, 1);
_processTimer.Start();
}
protected override void OnSourceInitialized(EventArgs e) {
base.OnSourceInitialized(e);
MakeWindowTransparent();
}
private void MakeWindowTransparent() {
var hwnd = new WindowInteropHelper(this).Handle;
var extendedStyle = Native.GetWindowLongPtr(hwnd, GwlExstyle);
Native.SetWindowLongPtr(hwnd, GwlExstyle, new IntPtr(extendedStyle.ToInt32() | WsExTransparent));
}
private void OnHotKeyCopyPlayerStats(HotKey hotKey) {
_copyPlayerStats?.Invoke();
}
private void OnHotKeyCalculateTeamsRank(HotKey hotKey) {
_calculateTeamsRank?.Invoke();
}
private void OnHotKeyCalculateTeamsTotalGames(HotKey hotKey) {
_calculateTeamsTotalGames?.Invoke();
}
private void OnHotKeyCalculateTeamsWinRatio(HotKey hotKey) {
_calculateTeamsWinRatio?.Invoke();
}
}
}