-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigureForm.cs
314 lines (282 loc) · 11 KB
/
ConfigureForm.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
using Gma.System.MouseKeyHook;
using OBSWebsocketDotNet;
using OBSWebsocketDotNet.Types;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static OHK.Configuration;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
namespace OHK
{
public partial class ConfigureForm : Form
{
private static readonly ConfigureForm instance = new ConfigureForm();
private OBSWebsocket OBSwsTest;
private Timer TestConnectionTimer;
private IKeyboardMouseEvents CFkme;
public ConfigureForm()
{
InitializeComponent();
}
public static ConfigureForm Instance
{
get
{
return instance;
}
}
private void ConfigureForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
Hide();
}
}
public void UpdateFields()
{
if (MapHideGroup.InvokeRequired)
{
MapHideGroup.Invoke(new MethodInvoker(delegate { UpdateFields(); }));
return;
}
var MainFormInstance = MainForm.Instance;
if (MainFormInstance.OBSws.IsConnected)
{
MapHideGroup.Text = "Map Hide";
foreach (Control control in MapHideGroup.Controls)
{
control.Enabled = true;
}
}
else
{
MapHideGroup.Text = "Map Hide (Connection Required)";
foreach (Control control in MapHideGroup.Controls)
{
control.Enabled = false;
}
}
ip.Text = Configuration.OHK.Ip;
port.Text = Configuration.OHK.Port;
password.Text = Configuration.OHK.Password;
hotkey.Text = Configuration.OHK.hotKeys[0].Key.ToString();
scene.Items.Clear();
scene.Items.Add(Configuration.OHK.hotKeys[0].Scene);
scene.Text = Configuration.OHK.hotKeys[0].Scene;
source.Items.Clear();
source.Items.Add(Configuration.OHK.hotKeys[0].Source);
source.Text = Configuration.OHK.hotKeys[0].Source;
delay.Text = Configuration.OHK.hotKeys[0].Delay.ToString();
}
public void UnHide()
{
UpdateFields();
Show();
}
private void digit_KeyPress(object sender, KeyPressEventArgs e)
{
// 8 is backspace
if (Char.IsDigit(e.KeyChar)) return;
if (e.KeyChar == (char)8) return;
e.Handled = true;
}
private void SaveConfig(object sender, EventArgs e)
{
Configuration.OHK.Ip = ip.Text;
Configuration.OHK.Port = port.Text;
Configuration.OHK.Password = password.Text;
if (Enum.TryParse<Keys>(hotkey.Text, out Keys keyEnum))
{
Configuration.OHK.hotKeys[0].Key = keyEnum;
}
Configuration.OHK.hotKeys[0].Scene = scene.Text;
Configuration.OHK.hotKeys[0].Source = source.Text;
Configuration.OHK.hotKeys[0].Delay = int.Parse(delay.Text);
Configuration.SaveConfig();
Hide();
}
private void Cancel_Click(object sender, EventArgs e)
{
Hide();
}
private void TestConnection_Click(object sender, EventArgs e)
{
if (!ushort.TryParse(port.Text, out ushort shortPort))
{
ConfigureForm.Instance.Invoke((MethodInvoker)delegate {
MessageBox.Show(this, "Port must be below 65536", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);;
});
return;
}
if (OBSwsTest != null)
{
OBSwsTest.Disconnect();
}
TestConnection.Text = "Testing";
TestConnection.Enabled = false;
OBSwsTest = new OBSWebsocket();
OBSwsTest.Connected += TestOnConnect;
OBSwsTest.Disconnected += TestOnDisconnect;
OBSwsTest.ConnectAsync($"ws://{ip.Text}:{port.Text}", password.Text);
TestConnectionTimer = new Timer();
TestConnectionTimer.Tick += TestConnectionTimerEventProcessor;
TestConnectionTimer.Interval = 5000;
TestConnectionTimer.Start();
}
private void TestOnConnect(object sender, EventArgs e)
{
if (TestConnectionTimer != null && TestConnectionTimer.Enabled)
{
TestConnectionTimer.Stop();
TestConnectionTimer.Dispose();
}
var versionInfo = OBSwsTest.GetVersion();
Log($"PluginV: {versionInfo.PluginVersion} OBSv: {versionInfo.OBSStudioVersion}");
if (int.Parse(versionInfo.PluginVersion.Split('.')[0]) < 5)
{
Log("OBS Test Old websocket server! Check port!");
OBSwsTest.Disconnect();
}
Log("OBS Test Connected to OBS");
ConfigureForm.Instance.Invoke((MethodInvoker)delegate {
MessageBox.Show(this, "Test OK!", "Connection test");
});
OBSwsTest.Disconnect();
}
private void TestOnDisconnect(object sender, OBSWebsocketDotNet.Communication.ObsDisconnectionInfo e)
{
OBSwsTest.Disconnected -= TestOnDisconnect;
if (TestConnectionTimer != null && TestConnectionTimer.Enabled)
{
TestConnectionTimer.Stop();
TestConnectionTimer.Dispose();
}
Log("Disconnected from OBS Test");
TestConnection.Invoke(new MethodInvoker(delegate {
TestConnection.Text = "Test";
TestConnection.Enabled = true;
}));
if (e.ObsCloseCode == OBSWebsocketDotNet.Communication.ObsCloseCodes.AuthenticationFailed)
{
ConfigureForm.Instance.Invoke((MethodInvoker)delegate {
MessageBox.Show(this, "Wrong password.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
});
return;
}
else if (e.WebsocketDisconnectionInfo != null)
{
if (e.WebsocketDisconnectionInfo.Exception != null)
{
ConfigureForm.Instance.Invoke((MethodInvoker)delegate {
MessageBox.Show(this, $"Connection failed: CloseCode: {e.ObsCloseCode} Desc: {e.WebsocketDisconnectionInfo?.CloseStatusDescription} Exception:{e.WebsocketDisconnectionInfo?.Exception?.Message}\nType: {e.WebsocketDisconnectionInfo.Type}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
});
}
else
{
if (e.WebsocketDisconnectionInfo.Type == Websocket.Client.DisconnectionType.ByUser)
return;
if (e.WebsocketDisconnectionInfo.Type == Websocket.Client.DisconnectionType.Exit)
return;
if (e.WebsocketDisconnectionInfo.Type == Websocket.Client.DisconnectionType.Error)
{
ConfigureForm.Instance.Invoke((MethodInvoker)delegate
{
MessageBox.Show(this, $"{e.WebsocketDisconnectionInfo?.CloseStatusDescription}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
});
return;
}
ConfigureForm.Instance.Invoke((MethodInvoker)delegate
{
MessageBox.Show(ConfigureForm.Instance, $"Connection failed: CloseCode: {e.ObsCloseCode}\n Desc: {e.WebsocketDisconnectionInfo?.CloseStatusDescription}\nType: {e.WebsocketDisconnectionInfo.Type}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
});
}
}
else
{
ConfigureForm.Instance.Invoke((MethodInvoker)delegate
{
MessageBox.Show(this, $"Connection failed: CloseCode: {e.ObsCloseCode}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
});
return;
}
}
private void TestConnectionTimerEventProcessor(object sender, EventArgs e)
{
if (sender is Timer timer)
{
timer.Dispose();
ConfigureForm.Instance.Invoke((MethodInvoker)delegate
{
MessageBox.Show(this, "Test timed out", "Connection test");
});
Log("Connection timed out waiting for handshake OBS Test");
OBSwsTest.Disconnect();
}
}
private void SetButtom_Click(object sender, EventArgs e)
{
CFkme = Hook.GlobalEvents();
CFkme.KeyDown += CFkme_KeyDown;
SetButtom.Text = "Press";
}
private void CFkme_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
Log("Set Hotkey: "+ e.ToString());
hotkey.Text = e.KeyCode.ToString();
CFkme.KeyDown -= CFkme_KeyDown;
SetButtom.Text = "Set";
}
private void Log(string text)
{
if (IsDisposed)
{
return;
}
DebugLogForm.Instance.Log(text);
}
private void scene_DropDown(object sender, EventArgs e)
{
var MainFormInstance = MainForm.Instance;
var scenes = MainFormInstance.OBSws.ListScenes();
scene.Items.Clear();
foreach (var sceneItem in scenes)
{
scene.Items.Add(sceneItem.Name);
}
source.Text = "";
}
private void source_DropDown(object sender, EventArgs e)
{
var MainFormInstance = MainForm.Instance;
List<SceneItemDetails> sources;
try
{
sources = MainFormInstance.OBSws.GetSceneItemList(scene.Text);
}
catch (Exception ex)
{
sources = new List<SceneItemDetails>();
}
source.Items.Clear();
foreach (var sourceeItem in sources)
{
source.Items.Add(sourceeItem.SourceName);
}
}
private void delay_Validating(object sender, CancelEventArgs e)
{
if (string.IsNullOrEmpty(delay.Text))
{
delay.Text = "0";
}
}
}
}