-
Notifications
You must be signed in to change notification settings - Fork 5
/
MainWindow.xaml.cs
311 lines (284 loc) · 11.8 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
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using Newtonsoft.Json;
using StalkbotGUI.Stalkbot.Discord;
using StalkbotGUI.Stalkbot.Utilities;
using StalkbotGUI.Stalkbot.Utilities.UI;
using MessageBox = System.Windows.MessageBox;
using ProgressBar = StalkbotGUI.Stalkbot.Utilities.UI.ProgressBar;
namespace StalkbotGUI
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
/// <summary>
/// Client responsible for everything related to "stalking"
/// </summary>
private readonly StalkbotClient _client;
private readonly NotifyIcon _notifyIcon;
private static readonly Version Version = Version.Parse("1.4");
/// <summary>
/// Constructor
/// </summary>
public MainWindow()
{
InitializeComponent();
Logger.Init(ref LogText, ref LogView);
_client = new StalkbotClient();
if (Config.Instance.AutoStartDiscord)
new Action(() => OnOffButton_Click(null, null))();
InitButtons();
_notifyIcon = new NotifyIcon
{
BalloonTipText = @"Stalkbot is still running!",
Icon = System.Drawing.Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location)
};
_notifyIcon.DoubleClick += NotifyIconOnDoubleClick;
Task.Delay(1000);
CheckRequirements();
CheckForNewRelease();
#if DEBUG
this.Title += " - DEBUG";
#endif
}
#region ButtonHandlers
/// <summary>
/// Handles clicking the off/on button
/// </summary>
/// <param name="sender">Button object</param>
/// <param name="e">Event args</param>
private async void OnOffButton_Click(object sender, RoutedEventArgs e)
{
await _client.StartStopDiscord();
OnOffButton.Background = new SolidColorBrush(_client.IsRunning ? Colors.DarkGreen : Colors.DarkRed);
OnOffButton.Content = _client.IsRunning ? "On" : "Off";
}
/// <summary>
/// Handles clicking the webcam toggle button
/// </summary>
/// <param name="sender">Button object</param>
/// <param name="e">Event args</param>
private void WebcamToggle_Click(object sender, RoutedEventArgs e)
{
Config.Instance.CamEnabled = !Config.Instance.CamEnabled;
Logger.Log($"Webcam: {Config.Instance.CamEnabled}", LogLevel.Info);
UiHelpers.UpdateButton("webcam", ref WebcamToggle);
Config.Instance.SaveConfig();
}
/// <summary>
/// Handles clicking the screenshot toggle button
/// </summary>
/// <param name="sender">Button object</param>
/// <param name="e">Event args</param>
private void ScreenshotToggle_Click(object sender, RoutedEventArgs e)
{
Config.Instance.SsEnabled = !Config.Instance.SsEnabled;
Logger.Log($"Screenshot: {Config.Instance.SsEnabled}", LogLevel.Info);
UiHelpers.UpdateButton("screenshot", ref ScreenshotToggle);
Config.Instance.SaveConfig();
}
/// <summary>
/// Handles clicking the TTS toggle button
/// </summary>
/// <param name="sender">Button object</param>
/// <param name="e">Event args</param>
private void TtsToggle_Click(object sender, RoutedEventArgs e)
{
Config.Instance.TtsEnabled = !Config.Instance.TtsEnabled;
Logger.Log($"Text to Speech: {Config.Instance.TtsEnabled}", LogLevel.Info);
UiHelpers.UpdateButton("tts", ref TtsToggle);
Config.Instance.SaveConfig();
}
/// <summary>
/// Handles clicking the play toggle button
/// </summary>
/// <param name="sender">Button object</param>
/// <param name="e">Event args</param>
private void PlayToggle_Click(object sender, RoutedEventArgs e)
{
Config.Instance.PlayEnabled = !Config.Instance.PlayEnabled;
Logger.Log($"Playsound: {Config.Instance.PlayEnabled}", LogLevel.Info);
UiHelpers.UpdateButton("play", ref PlayToggle);
Config.Instance.SaveConfig();
}
/// <summary>
/// Handles clicking the processes toggle button
/// </summary>
/// <param name="sender">Button object</param>
/// <param name="e">Event args</param>
private void ProcToggle_Click(object sender, RoutedEventArgs e)
{
Config.Instance.ProcessesEnabled = !Config.Instance.ProcessesEnabled;
Logger.Log($"Processes: {Config.Instance.ProcessesEnabled}", LogLevel.Info);
UiHelpers.UpdateButton("proc", ref ProcToggle);
Config.Instance.SaveConfig();
}
/// <summary>
/// Handles clicking the recording toggle button
/// </summary>
/// <param name="sender">Button object</param>
/// <param name="e">Event args</param>
private void RecordingToggle_Click(object sender, RoutedEventArgs e)
{
Config.Instance.RecordingEnabled = !Config.Instance.RecordingEnabled;
Logger.Log($"Recording: {Config.Instance.RecordingEnabled}", LogLevel.Info);
UiHelpers.UpdateButton("recording", ref RecordingToggle);
Config.Instance.SaveConfig();
}
/// <summary>
/// Handles clicking the undo button
/// </summary>
/// <param name="sender">Button object</param>
/// <param name="e">Event args</param>
private async void UndoButton_Click(object sender, RoutedEventArgs e)
=> await _client.Undo();
/// <summary>
/// Handles clicking the config button
/// </summary>
/// <param name="sender">Button object</param>
/// <param name="e">Event args</param>
private void ConfigButton_Click(object sender, RoutedEventArgs e)
{
var cfg = new ConfigWindow();
cfg.Show();
}
#endregion
#region Utilities
/// <summary>
/// Calls the GitHub API to check for new releases
/// </summary>
private async void CheckForNewRelease()
{
try
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.UserAgent.TryParseAdd("request");
using (var response =
await client.GetAsync("https://api.github.com/repos/M3IY0U/StalkbotGUI/releases"))
{
response.EnsureSuccessStatusCode();
var json = JsonConvert.DeserializeObject<dynamic>(await response.Content.ReadAsStringAsync());
if (json is null)
{
Logger.Log("Couldn't check for newer version", LogLevel.Warning);
return;
}
var latestRelease = Version.Parse((string) json[0].tag_name);
if (latestRelease <= Version) return;
var mb = MessageBox.Show(
$"Your Version: {Version}\nLatest Version: {json[0].tag_name}\nDo you want to download it from GitHub?",
"New Version available!\n", MessageBoxButton.YesNo);
if (mb == MessageBoxResult.Yes)
Process.Start("https://github.com/M3IY0U/StalkbotGUI/releases");
}
}
}
catch
{
Logger.Log("Couldn't check for new releases", LogLevel.Warning);
}
}
/// <summary>
/// Colors the UI toggle buttons
/// </summary>
private void InitButtons()
{
UiHelpers.UpdateButton("webcam", ref WebcamToggle);
UiHelpers.UpdateButton("screenshot", ref ScreenshotToggle);
UiHelpers.UpdateButton("play", ref PlayToggle);
UiHelpers.UpdateButton("tts", ref TtsToggle);
UiHelpers.UpdateButton("proc", ref ProcToggle);
UiHelpers.UpdateButton("recording", ref RecordingToggle);
}
/// <summary>
/// Checks for a config and ffmpeg
/// </summary>
private async void CheckRequirements()
{
// Check for config.json
if (!File.Exists("config.json"))
{
Logger.Log("config.json was not found, creating a default one", LogLevel.Warning);
Config.Instance.SaveConfig();
}
if (!File.Exists("ffmpeg.exe"))
{
Logger.Log("ffmpeg was not found in directory, downloading it...", LogLevel.Warning);
if (!await DownloadFile("https://timostestdoma.in/res/ffmpeg.exe", "ffmpeg.exe"))
Logger.Log("Error downloading ffmpeg, please try starting again or download it manually",
LogLevel.Error);
else
Logger.Log("Successfully downloaded ffmpeg!", LogLevel.Info);
}
if (File.Exists("youtube-dl.exe")) return;
Logger.Log("youtube-dl was not found in directory, downloading it...", LogLevel.Warning);
if (!await DownloadFile("https://timostestdoma.in/res/youtube-dl.exe", "youtube-dl.exe"))
Logger.Log("Error downloading youtube-dl, please try starting again or download it manually",
LogLevel.Error);
else
Logger.Log("Successfully downloaded youtube-dl!", LogLevel.Info);
}
/// <summary>
/// Downloads ffmpeg with progress bar
/// </summary>
/// <returns>Whether the download was successful</returns>
private async Task<bool> DownloadFile(string url, string filename)
{
var error = false;
await Dispatcher.Invoke(async () =>
{
var pbw = new ProgressBar(filename);
pbw.Show();
using (var client = new WebClient())
{
client.DownloadProgressChanged += (sender, args) => pbw.UpdateProgress(args.ProgressPercentage);
client.DownloadDataCompleted += (sender, args) => error = args.Cancelled || args.Error != null;
await client.DownloadFileTaskAsync(new Uri(url), filename);
}
});
return !error;
}
/// <summary>
/// Restores from tray
/// </summary>
/// <param name="sender">NotifyIcon object</param>
/// <param name="e">Event args</param>
private void NotifyIconOnDoubleClick(object sender, EventArgs e)
{
Show();
WindowState = WindowState.Normal;
_notifyIcon.Visible = false;
}
/// <summary>
/// Minimizes to tray
/// </summary>
/// <param name="sender">Window object</param>
/// <param name="e">Event args</param>
private void Window_StateChanged(object sender, EventArgs e)
{
if (WindowState != WindowState.Minimized || !Config.Instance.MinimizeToTray) return;
Hide();
_notifyIcon.Visible = true;
_notifyIcon.ShowBalloonTip(1000);
}
/// <summary>
/// Disposes of the client on close
/// </summary>
/// <param name="sender">Window object</param>
/// <param name="e">Event args</param>
private async void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
=> await _client.Dispose();
#endregion
}
}