-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOverlayWindow.xaml.cs
322 lines (283 loc) · 12 KB
/
OverlayWindow.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
320
321
322
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace TwitchPlaysBot
{
/// <summary>
/// Interaction logic for OverlayWindow.xaml
/// </summary>
public partial class OverlayWindow : Window
{
private Process process;
private IntPtr processHandle;
private Queue<Keys[]> keyPressQueue;
private Thread keyPressQueueThread;
private string gameTitle;
public string GameTitle
{
get { return this.gameTitle; }
set
{
this.gameTitle = "Twitch Plays \n" + value;
GameTitleBlock.Text = this.gameTitle;
}
}
public int ProcessId { get; set; }
public bool RemoveWindowsStyle { get; set; }
public bool CloseProcessOnExit { get; set; }
public Joypad Joypad { get; set; }
// constants to remove styling
private const int SWP_NOZORDER = 0x0004;
private const int SWP_NOACTIVATE = 0x0010;
private const int GWL_STYLE = -16;
private const int WS_CAPTION = 0x00C00000;
private const int WS_THICKFRAME = 0x00040000;
public OverlayWindow()
{
this.DataContext = this;
InitializeComponent();
keyPressQueue = new Queue<Keys[]>();
Joypad = new Joypad();
GameTitle = "";
}
public void Hook()
{
try
{
process = Process.GetProcessById(ProcessId);
if (process != null)
{
// wait for the process to become available before getting the window handle
process.WaitForInputIdle();
processHandle = process.MainWindowHandle;
// close the overlay if the process exits
process.Exited += delegate { Dispatcher.BeginInvoke(new Action(() => { this.Close(); })); };
System.Diagnostics.Debug.WriteLine("Hooked process ID: " + process.Id);
System.Diagnostics.Debug.WriteLine("Hooked process Handle: " + processHandle);
// focus process
FocusProcessWindow();
if (RemoveWindowsStyle)
{
// remove caption and borders
int style = GetWindowLong(processHandle, GWL_STYLE);
style = style & ~WS_CAPTION & ~WS_THICKFRAME;
SetWindowLong(processHandle, GWL_STYLE, style);
}
// position the overlay window to the right of the application
Rect processDimensions = new Rect();
GetWindowRect(processHandle, ref processDimensions);
if (processDimensions.Top > 0 && processDimensions.Right > 0)
{
this.Top = processDimensions.Top;
this.Left = processDimensions.Right;
}
// start working through the command queue
keyPressQueueThread = new Thread(() =>
{
while (!process.HasExited)
{
SendKeyPress();
}
}) { IsBackground = true };
keyPressQueueThread.Start();
}
}
catch (Exception e)
{
throw new Exception("Error when hooking process: " + e.Message, e.InnerException);
}
}
public void OnMessagedRecieved(string username, string message)
{
message = message.ToLower();
Keys[] keys;
if (Joypad.CommandKeyPairs.TryGetValue(message, out keys))
{
keyPressQueue.Enqueue(keys);
PrintToActionLog(String.Format("{0}: {1}", username, message));
}
}
private void SendKeyPress()
{
if (keyPressQueue.Count > 0)
{
if (!ProcessHasFocus())
{
FocusProcessWindow();
}
Keys[] keys = keyPressQueue.Dequeue();
Joypad.PressManyKeys(keys);
System.Diagnostics.Debug.WriteLine("Using joypad layout: " + Joypad.Name);
System.Diagnostics.Debug.WriteLine("Key press: " + String.Join(" ", keys.Select(k=>k.ToString()).ToArray()));
}
}
private void FocusProcessWindow()
{
// show windows that have been hidden
ShowWindow(processHandle, WindowShowStyle.Show);
// show windows that have been minimized
ShowWindow(processHandle, WindowShowStyle.Restore);
// finally focus the window
SetForegroundWindow(processHandle);
}
private bool ProcessHasFocus()
{
if (process == null)
{
// process has not been created
return false;
}
// handle for the window that currently has focus
var activatedHandle = GetForegroundWindow();
if (activatedHandle == IntPtr.Zero)
{
// no window is currently activated
return false;
}
// get the window's process ID
int activeProcessId;
GetWindowThreadProcessId(activatedHandle, out activeProcessId);
// compare it to the embedded process' ID
return activeProcessId == process.Id;
}
private void PrintToActionLog(string line)
{
Dispatcher.BeginInvoke(new Action(() =>
{
// add a new line
if (ActionLog.Text.Length > 0)
{
ActionLog.Text += System.Environment.NewLine;
}
// append to the textbox
ActionLog.Text += line;
// remove overflowing lines
while (ActionLog.LineCount > 15)
{
ActionLog.Text = ActionLog.Text.Remove(0, ActionLog.GetLineLength(0));
}
// scroll down to the end
ActionLog.SelectionStart = ActionLog.Text.Length;
ActionLog.ScrollToEnd();
// write to debug console also
System.Diagnostics.Debug.WriteLine(line);
}));
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
if (keyPressQueueThread != null)
{
// stop processing the queue
keyPressQueueThread.Abort();
}
if (CloseProcessOnExit && process != null)
{
// close the process
process.CloseMainWindow();
process.Close();
}
base.OnClosing(e);
}
[DllImport("user32")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32")]
private static extern int SetForegroundWindow(IntPtr hwnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ShowWindow(IntPtr hWnd, WindowShowStyle nCmdShow);
/// <summary>Enumeration of the different ways of showing a window using
/// ShowWindow</summary>
private enum WindowShowStyle : uint
{
/// <summary>Hides the window and activates another window.</summary>
/// <remarks>See SW_HIDE</remarks>
Hide = 0,
/// <summary>Activates and displays a window. If the window is minimized
/// or maximized, the system restores it to its original size and
/// position. An application should specify this flag when displaying
/// the window for the first time.</summary>
/// <remarks>See SW_SHOWNORMAL</remarks>
ShowNormal = 1,
/// <summary>Activates the window and displays it as a minimized window.</summary>
/// <remarks>See SW_SHOWMINIMIZED</remarks>
ShowMinimized = 2,
/// <summary>Activates the window and displays it as a maximized window.</summary>
/// <remarks>See SW_SHOWMAXIMIZED</remarks>
ShowMaximized = 3,
/// <summary>Maximizes the specified window.</summary>
/// <remarks>See SW_MAXIMIZE</remarks>
Maximize = 3,
/// <summary>Displays a window in its most recent size and position.
/// This value is similar to "ShowNormal", except the window is not
/// actived.</summary>
/// <remarks>See SW_SHOWNOACTIVATE</remarks>
ShowNormalNoActivate = 4,
/// <summary>Activates the window and displays it in its current size
/// and position.</summary>
/// <remarks>See SW_SHOW</remarks>
Show = 5,
/// <summary>Minimizes the specified window and activates the next
/// top-level window in the Z order.</summary>
/// <remarks>See SW_MINIMIZE</remarks>
Minimize = 6,
/// <summary>Displays the window as a minimized window. This value is
/// similar to "ShowMinimized", except the window is not activated.</summary>
/// <remarks>See SW_SHOWMINNOACTIVE</remarks>
ShowMinNoActivate = 7,
/// <summary>Displays the window in its current size and position. This
/// value is similar to "Show", except the window is not activated.</summary>
/// <remarks>See SW_SHOWNA</remarks>
ShowNoActivate = 8,
/// <summary>Activates and displays the window. If the window is
/// minimized or maximized, the system restores it to its original size
/// and position. An application should specify this flag when restoring
/// a minimized window.</summary>
/// <remarks>See SW_RESTORE</remarks>
Restore = 9,
/// <summary>Sets the show state based on the SW_ value specified in the
/// STARTUPINFO structure passed to the CreateProcess function by the
/// program that started the application.</summary>
/// <remarks>See SW_SHOWDEFAULT</remarks>
ShowDefault = 10,
/// <summary>Windows 2000/XP: Minimizes a window, even if the thread
/// that owns the window is hung. This flag should only be used when
/// minimizing windows from a different thread.</summary>
/// <remarks>See SW_FORCEMINIMIZE</remarks>
ForceMinimized = 11
}
[DllImport("user32")]
private static extern bool GetWindowRect(IntPtr hWnd, ref Rect rect);
private struct Rect
{
// the x-coordinate of the upper-left corner of the rectangle
public int Left { get; set; }
// the y-coordinate of the upper-left corner of the rectangle
public int Top { get; set; }
// the x-coordinate of the lower-right corner of the rectangle
public int Right { get; set; }
// the y-coordinate of the lower-right corner of the rectangle
public int Bottom { get; set; }
}
[DllImport("user32")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32")]
private static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);
}
}