-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
399 lines (347 loc) · 10.1 KB
/
Copy pathMainForm.cs
File metadata and controls
399 lines (347 loc) · 10.1 KB
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
using SSC.Chat;
using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using SSC.AI_Integration;
using System.IO;
using SSC.OtherForms;
using Whisper.net;
using OBSWebsocketDotNet.Communication;
using SSC.DataStorage;
using SSC.Forms.VideoRewardsDBEditor;
namespace SSC
{
public enum LineType
{
Generic,
TwitchSocketCommand,
ModCommand,
SoundCommand,
VoiceMod,
WebSocket,
GeminiAI
}
public partial class MainForm : Form
{
public static readonly MarkdownSharp.Markdown Markdown = new MarkdownSharp.Markdown();
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public static MainForm Instance { get; private set; }
public delegate void SetPreviewTextDelegate(string text, LineType type); //used to safely handle the IRC output from bot class
public delegate void SetVolumeSlider(int value); //used to safely change the slider position
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public ChatBot TwitchBot { get; private set; }
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public EventBridgeTwitch TwitchEvents { get; private set; }
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public EventBridgeOBS OBSEvents { get; private set; }
private char PrefixCharacter = '-';
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public SoundDB SoundDB { get; private set; }
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public DataStorage.Videos.OBS_VideoRewardDB VideoDB { get; private set; }
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public OBSWebsocketDotNet.OBSWebsocket OBS { get; private set; }
WebSocketsListener webSockets;
public MainForm()
{
Instance = this;
TwitchEvents = new EventBridgeTwitch();
OBSEvents = new EventBridgeOBS();
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var settings = PrivateSettings.GetInstance();
webSockets = new WebSocketsListener();
//UpdateColors();
connectOnStartupToolStripMenuItem.Checked = settings.Autostart;
int valrr = Convert.ToInt32(100 * settings.Volume);
trackBar_Volume.Value = valrr;
L_Volume.Text = trackBar_Volume.Value.ToString() + "%";
SoundDB = new SoundDB();
VideoDB = new DataStorage.Videos.OBS_VideoRewardDB();
if (settings.Autostart)
{
StartBot();
}
if (settings.RunWebSocketsServer)
webSockets.Start();
UpdateReminderIcon();
}
private void StartBot()
{
TwitchBot = new ChatBot(PrefixCharacter);
TwitchBot.Connect();
ConnectOBS();
}
public void ConnectOBS(bool displayErrors = false)
{
var settings = PrivateSettings.GetInstance();
if (string.IsNullOrEmpty(settings.OBS_Address) || string.IsNullOrEmpty(settings.OBS_Password))
return;
if (OBS == null)
OBS = new OBSWebsocketDotNet.OBSWebsocket();
OBSEvents.RegisterEvents(OBS);
try
{
OBS.ConnectAsync(settings.OBS_Address, settings.OBS_Password);
}
catch (Exception ex)
{
if (displayErrors)
MessageBox.Show("Failed to connect to OBS: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Logger.AddLine("Failed to connect: " + ex.Message);
return;
}
}
#region ThreadSafeFunctions
private void AddPreviewText(string text, LineType type)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.RB_Preview.InvokeRequired)
{
SetPreviewTextDelegate d = new SetPreviewTextDelegate(AddPreviewText);
this.Invoke(d, new object[] { text, type });
}
else
{
var settings = PrivateSettings.GetInstance();
RB_Preview.AppendText(text + "\n");
RB_Preview.Select(RB_Preview.Text.Length - text.Length - 1, text.Length);
switch (type)
{
case LineType.Generic:
RB_Preview.SelectionColor = Color.White;
break;
case LineType.TwitchSocketCommand:
RB_Preview.SelectionColor = Color.DarkGreen;
break;
case LineType.ModCommand:
RB_Preview.SelectionColor = Color.Green;
break;
case LineType.SoundCommand:
RB_Preview.SelectionColor = Color.AliceBlue;
break;
case LineType.WebSocket:
RB_Preview.SelectionColor = Color.GreenYellow;
break;
default:
RB_Preview.SelectionColor = Color.White;
break;
}
}
}
public void ThreadSafeAddPreviewText(string text, LineType type)
{
this.AddPreviewText(text, type);
}
public void SetVolume(int value)
{
if (this.trackBar_Volume.InvokeRequired)
{
SetVolumeSlider d = new SetVolumeSlider(SetVolume);
this.Invoke(d, new object[] { value });
}
else
{
trackBar_Volume.Value = value;
L_Volume.Text = trackBar_Volume.Value.ToString() + "%";
}
}
public int GetVolume() => trackBar_Volume.Value;
private void PerformShutdownTasks()
{
if (TwitchBot != null)
TwitchBot.StopBot();
trayIcon.Visible = false;
var settings = PrivateSettings.GetInstance();
settings.SaveSettings();
this.webSockets.Stop();
System.Environment.Exit(0);
}
#endregion
#region EventHandlers
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
PerformShutdownTasks();
}
private void RB_Preview_TextChanged(object sender, EventArgs e)
{
RB_Preview.SelectionStart = RB_Preview.Text.Length;
RB_Preview.ScrollToCaret();
}
private void TrackBar_Volume_Scroll(object sender, EventArgs e)
{
L_Volume.Text = trackBar_Volume.Value.ToString() + "%";
var settings = PrivateSettings.GetInstance();
settings.Volume = trackBar_Volume.Value / 100f;
if (TwitchBot != null)
TwitchBot.UpdateVolume();
}
private void MainForm_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
trayIcon.Visible = true;
this.Hide();
}
else if (this.WindowState == FormWindowState.Normal)
{
trayIcon.Visible = false;
this.Show();
}
}
#region FileTree_Events
private void RunBotToolStripMenuItem_Click(object sender, EventArgs e)
{
var CastedSender = (ToolStripMenuItem)sender;
if (CastedSender.Checked)
{
StartBot();
}
else
{
StopBot();
}
}
private void ConnectOnStartupToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
{
var CastedSender = (ToolStripMenuItem)sender;
var settings = PrivateSettings.GetInstance();
settings.Autostart = CastedSender.Checked;
settings.SaveSettings();
}
private void ConnectionSettingsToolStripMenuItem_Click(object sender, EventArgs e)
{
SettingsForms.ConnectionSettingsForm form = new SettingsForms.ConnectionSettingsForm(this);
var settings = PrivateSettings.GetInstance();
DialogResult res = form.ShowDialog();
if (res == DialogResult.OK)
{
settings.UserAuth = form.UserAuth;
settings.BotAuth = form.BotAuth;
settings.Debug_mode = form.DebugMode;
settings.WebSocketsServerPort = form.WebsocketPort;
settings.RunWebSocketsServer = form.RunWebsocket;
settings.OBS_Address = form.OBS_Address;
settings.OBS_Password = form.OBS_Password;
settings.SaveSettings();
ReloadBot();
}
}
private void ReloadBot()
{
webSockets.Stop();
if (PrivateSettings.GetInstance().RunWebSocketsServer)
webSockets.Start();
if (TwitchBot != null && TwitchBot.BotRunning)
StopBot();
StartBot();
}
private void StopBot()
{
if (TwitchBot != null && TwitchBot.BotRunning)
{
TwitchBot.StopBot();
}
TwitchBot = null;
}
private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.StopBot();
this.webSockets.Stop();
this.Close();
}
#endregion
#region TrayIcon_Events
private void ShowProgramToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
private void CloseToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void TrayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
#endregion
#endregion
private void DatabaseEditorToolStripMenuItem_Click(object sender, EventArgs e)
{
SoundDatabaseEditor.DB_Editor scf = new SoundDatabaseEditor.DB_Editor(SoundDB.SoundList);
DialogResult res = scf.ShowDialog();
if (res == DialogResult.OK)
{
SoundDB.SoundList = scf.SoundsCopy;
SoundDB.Save();
}
}
private void VoiceModSettings_Click(object sender, EventArgs e)
{
SettingsForms.VoiceModIntegrationForm form = new SettingsForms.VoiceModIntegrationForm();
var result = form.ShowDialog();
if (result == DialogResult.OK)
{
}
}
public void UpdateReminderIcon()
{
if (this.InvokeRequired)
{
this.Invoke(new Action(() => { UpdateReminderIcon(); }));
return;
}
var reminders = Reminders.GetInstance();
foreach (var reminder in reminders.Entities)
{
if (reminder.Notified && !reminder.Acknowledged)
{
notificationToolStripMenuItem.Visible = true;
return;
}
}
notificationToolStripMenuItem.Visible = false;
}
private void remindersToolStripMenuItem_Click(object sender, EventArgs e)
{
if (ReminderForm.Instance != null)
{
ReminderForm.Instance.Focus();
}
else
{
var form = new ReminderForm();
form.Show();
}
}
private void notesToolStripItem_Click(object sender, EventArgs e)
{
NotesForm notesForm;
if (NotesForm.Instance == null)
{
notesForm = new NotesForm();
notesForm.Show();
}
else
{
NotesForm.Instance.Focus();
}
}
internal void ProcessSpeechInput(SegmentData segment)
{
}
private void videoPlayerOBSToolStripMenuItem_Click(object sender, EventArgs e)
{
var form = new VideoDBEditor(VideoDB);
var result = form.ShowDialog();
if (result == DialogResult.OK)
{
VideoDB.StorableData.OBS_Scene = form.SelectedScene;
VideoDB.StorableData.OBS_MultimediaSource = form.SelectedMedia;
VideoDB.StorableData.VideoRewards = form.RewardsCopy;
VideoDB.SaveDB();
VideoDB.RebuildDictionary();
}
}
}
}