-
Notifications
You must be signed in to change notification settings - Fork 3
/
MainWindow.xaml.cs
257 lines (218 loc) · 8.13 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
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TwitchPlaysBot
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private IRC irc;
private OverlayWindow overlay;
private List<Joypad> joypads;
private Joypad currentJoypad;
private const int ConsoleMaxLines = 50;
public BindingList<KeyValuePair<int, string>> AvailableProcesses { get; set; }
public MainWindow()
{
this.DataContext = this;
PopulateProcessList();
InitJoypads();
InitializeComponent();
InitIRC();
LoadSettings();
}
private void LoadSettings()
{
Username.Text = Properties.Settings.Default.Username;
Channel.Text = Properties.Settings.Default.Channel;
}
private void InitJoypads()
{
joypads = new List<Joypad>();
joypads.Add(
new Joypad(
new Dictionary<string, Key[]>()
{
{ "up", new Key[] { Key.Up } },
{ "down", new Key[] { Key.Down } },
{ "left", new Key[] { Key.Left } },
{ "right", new Key[] { Key.Right } },
{ "a", new Key[] { Key.X } },
{ "b", new Key[] { Key.Z } },
{ "select", new Key[] { Key.Back } },
{ "start", new Key[] { Key.Enter } },
{ "!multi", new Key[] { Key.Down, Key.Down, Key.Left, Key.Left } }
}
) { Name = "Default" }
);
currentJoypad = joypads.First();
}
private void InitIRC()
{
irc = new IRC("irc.twitch.tv", 6667);
//irc = new IRC("199.9.252.26", 6667);
irc.eventConnectionStatus += new ConnectionStatus(ConnectionStatusUpdate);
irc.eventRecievingData += new DataReceived(PrintToConsole);
}
private void InitOverlay()
{
overlay = new OverlayWindow() { Joypad = currentJoypad };
irc.eventRecievingMessage += new MessageReceived(overlay.OnMessagedRecieved);
overlay.Closing += delegate
{
irc.eventRecievingMessage -= new MessageReceived(overlay.OnMessagedRecieved);
overlay = null;
};
}
private void PopulateProcessList()
{
if (AvailableProcesses == null)
{
AvailableProcesses = new BindingList<KeyValuePair<int, string>>();
}
if (AvailableProcesses.Count > 0)
{
AvailableProcesses.Clear();
}
Process[] processList = Process.GetProcesses();
Array.Sort(processList, (x1, x2) => x1.ProcessName.CompareTo(x2.ProcessName));
foreach (Process proc in processList)
{
// only add processes that have a main window handle
if (proc.MainWindowHandle != IntPtr.Zero)
{
AvailableProcesses.Add(new KeyValuePair<int, string>(proc.Id, String.Format("{0} ({1}.exe)", proc.MainWindowTitle, proc.ProcessName)));
}
}
System.Diagnostics.Debug.WriteLine(AvailableProcesses.Count + " processes found");
}
private void PrintToConsole(string data)
{
Dispatcher.BeginInvoke(new Action(() =>
{
// add a new line
if (ConsoleOutput.Text.Length > 0)
{
ConsoleOutput.Text += System.Environment.NewLine;
}
// append to the textbox
ConsoleOutput.Text += data;
// remove overflowing lines
while (ConsoleOutput.LineCount > ConsoleMaxLines)
{
ConsoleOutput.Text = ConsoleOutput.Text.Remove(0, ConsoleOutput.GetLineLength(0));
}
// scroll down to the end
ConsoleOutput.SelectionStart = ConsoleOutput.Text.Length;
ConsoleOutput.ScrollToEnd();
// write to debug console also
System.Diagnostics.Debug.WriteLine(data);
}));
}
private void btnProcessListRefresh_Click(object sender, RoutedEventArgs e)
{
PopulateProcessList();
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
// force shutdown of the overlay if it's running
Application.Current.Shutdown();
}
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
private void btnConnect_Click(object sender, RoutedEventArgs e)
{
if (irc.IsConnected)
{
irc.Disconnect();
}
else
{
// clear the console
ConsoleOutput.Text = "";
// save IRC configuration
Properties.Settings.Default.Username = Username.Text;
Properties.Settings.Default.Password = OAuthToken.SecurePassword;
Properties.Settings.Default.Channel = (Channel.Text.StartsWith("#") ? Channel.Text : "#" + Channel.Text);
Properties.Settings.Default.Save();
// connect to the IRC server asynchronously
Task.Factory.StartNew(() => { irc.Connect(); });
}
}
private void ConnectionStatusUpdate(bool isConnected, bool hasJoinedChannel)
{
Dispatcher.BeginInvoke(new Action(() => {
// change the connect button text
btnConnect.Content = isConnected ? "Disconnect" : "Connect to Twitch";
// enable or disable the console send button
btnConsoleSend.IsEnabled = isConnected && hasJoinedChannel;
}));
}
private void btnConsoleSend_Click(object sender, RoutedEventArgs e)
{
string message = ConsoleInput.Text.Trim();
if (irc.IsConnected && irc.hasJoinedChannel && message.Length > 0)
{
Task.Factory.StartNew(() => {
irc.SendMessage(message);
PrintToConsole("< " + message);
});
ConsoleInput.Text = "";
}
}
private void ConsoleInput_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
btnConsoleSend.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
}
}
private void btnCreateOverlay_Click(object sender, RoutedEventArgs e)
{
if (overlay != null)
{
overlay.Close();
}
InitOverlay();
if (ProcessList.SelectedValue != null)
{
overlay.ProcessId = (int)ProcessList.SelectedValue;
try
{
overlay.Hook();
overlay.Show();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
}
private void ProcessList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
btnCreateOverlay.IsEnabled = true;
}
}
}