forked from CodeHedge/SysBot.Remote
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathForm1.cs
More file actions
697 lines (610 loc) · 19.4 KB
/
Form1.cs
File metadata and controls
697 lines (610 loc) · 19.4 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
using Newtonsoft.Json;
using SysBot.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SysbotMacro
{
public partial class Form1 : Form
{
private bool canSaveData = true;
private List<Bot> bots = new List<Bot>();
private bool live;
private List<Dictionary<string, object>> ipDict = new List<Dictionary<string, object>>();
private List<Dictionary<string, object>> macroDict = new List<Dictionary<string, object>>();
public Form1()
{
InitializeComponent();
LoadData();
}
private void SaveData()
{
if (!canSaveData)
{
return;
}
ipDict.Clear();
foreach (ListViewItem item in ipListView.Items)
{
var itemData = new Dictionary<string, object>
{
{ "IsChecked", item.Checked },
{ "SwitchName", item.SubItems[1].Text },
{ "IPAddress", item.SubItems[2].Text }
};
ipDict.Add(itemData);
}
File.WriteAllText("ipListView.json", JsonConvert.SerializeObject(ipDict));
macroDict.Clear();
foreach (ListViewItem item in macroListView.Items)
{
var itemData = new Dictionary<string, object>
{
{ "Name", item.Text },
{ "Macro", item.SubItems[1].Text }
};
macroDict.Add(itemData);
}
File.WriteAllText("macroListView.json", JsonConvert.SerializeObject(macroDict));
}
private void LoadData()
{
if (File.Exists("macroListView.json"))
{
var macroListViewDataJson = File.ReadAllText("macroListView.json");
var macroListViewData = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(macroListViewDataJson);
macroListView.Items.Clear();
foreach (var itemData in macroListViewData)
{
var newItem = new ListViewItem
{
Text = itemData["Name"].ToString()
};
newItem.SubItems.Add(itemData["Macro"].ToString());
macroListView.Items.Add(newItem);
}
}
if (File.Exists("ipListView.json"))
{
var ipListViewDataJson = File.ReadAllText("ipListView.json");
var ipListViewData = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(ipListViewDataJson);
ipListView.Items.Clear();
foreach (var itemData in ipListViewData)
{
var newItem = new ListViewItem
{
Checked = (bool)itemData["IsChecked"]
};
newItem.SubItems.Add(itemData["SwitchName"].ToString());
newItem.SubItems.Add(itemData["IPAddress"].ToString());
ipListView.Items.Add(newItem);
}
}
}
private void InitializeBots()
{
bots.Clear();
if (ipListView.CheckedItems.Count < 1)
{
UpdateLogger("No IP selected");
return;
}
foreach (ListViewItem item in ipListView.CheckedItems)
{
string ip = item.SubItems[2].Text;
var config = new SwitchConnectionConfig
{
IP = ip,
Port = 6000,
Protocol = SwitchProtocol.WiFi
};
var bot = new Bot(config);
bots.Add(bot);
}
}
private void AppendToLastNumberString(string appendText)
{
string text = textBox1.Text.TrimEnd();
string[] splitText = text.Split(' ');
if (Regex.IsMatch(splitText[splitText.Length - 1], @"\d"))
{
Console.WriteLine("Not a button");
}
else
{
splitText[splitText.Length - 1] += appendText;
}
textBox1.Text = string.Join(" ", splitText);
textBox1.Text += " ";
}
private void Button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text))
{
ListViewItem newItem = new ListViewItem("");
newItem.Text = macroNameTB.Text;
newItem.SubItems.Add(textBox1.Text);
macroListView.Items.Add(newItem);
SaveData();
}
}
#region Joycon Button inputs
private async void LeftbButton_Click(object sender, EventArgs e)
{
if (live)
{
await SendLiveButtonPress("Left");
}
else
{
textBox1.AppendText("Left ");
}
}
private async void RightbButton_Click(object sender, EventArgs e)
{
if (live)
{
await SendLiveButtonPress("Right");
}
else
{
textBox1.AppendText("Right ");
}
}
private async void YbButton_Click(object sender, EventArgs e)
{
if (live)
{
await SendLiveButtonPress("Y");
}
else
{
textBox1.AppendText("Y ");
}
}
private async void XbButton_Click(object sender, EventArgs e)
{
if (live)
{
await SendLiveButtonPress("X");
}
else
{
textBox1.AppendText("X ");
}
}
private async void AaBbutton_Click(object sender, EventArgs e)
{
if (live)
{
await SendLiveButtonPress("A");
}
else
{
textBox1.AppendText("A ");
}
}
private async void BbButton_Click(object sender, EventArgs e)
{
if (live)
{
await SendLiveButtonPress("B");
}
else
{
textBox1.AppendText("B ");
}
}
private async void RtsbButton_Click(object sender, EventArgs e)
{
if (live)
{
await SendLiveButtonPress("RStick");
}
else
{
textBox1.AppendText("RTS ");
}
}
private async void HbButton_Click(object sender, EventArgs e)
{
if (live)
{
await SendLiveButtonPress("Home");
}
else
{
textBox1.AppendText("H ");
}
}
private async void SsbButton_Click(object sender, EventArgs e)
{
if (live)
{
await SendLiveButtonPress("Capture");
}
else
{
textBox1.AppendText("SS ");
}
}
private async void LtsbButton_Click(object sender, EventArgs e)
{
if (live)
{
await SendLiveButtonPress("LStick");
}
else
{
textBox1.AppendText("LTS ");
}
}
private async void DownbButton_Click(object sender, EventArgs e)
{
if (live)
{
await SendLiveButtonPress("Down");
}
else
{
textBox1.AppendText("Down ");
}
}
private async void UpbButton_Click(object sender, EventArgs e)
{
if (live)
{
await SendLiveButtonPress("Up");
}
else
{
textBox1.AppendText("Up ");
}
}
private async void RbButton_Click(object sender, EventArgs e)
{
if (live)
{
await SendLiveButtonPress("Right");
}
else
{
textBox1.AppendText("R ");
}
}
private async void ZrbButton_Click(object sender, EventArgs e)
{
if (live)
{
await SendLiveButtonPress("ZR");
}
else
{
textBox1.AppendText("ZR ");
}
}
private async void ZlbButton_Click_1(object sender, EventArgs e)
{
if (live)
{
await SendLiveButtonPress("ZL");
}
else
{
textBox1.AppendText("ZL ");
}
}
private async void LbButton_Click(object sender, EventArgs e)
{
if (live)
{
await SendLiveButtonPress("L");
}
else
{
textBox1.AppendText("L ");
}
}
private async void ZlbButton_Click(object sender, EventArgs e)
{
if (live)
{
await SendLiveButtonPress("ZL");
}
else
{
textBox1.AppendText("ZL ");
}
}
private void LoadButton_Click(object sender, EventArgs e)
{
SaveData();
try
{
textBox1.Text = macroListView.SelectedItems[0].SubItems[1].Text;
}
catch
{
UpdateLogger("No macro selected");
}
}
private async void PlusbButton_Click(object sender, EventArgs e)
{
if (live)
{
await SendLiveButtonPress("Plus");
}
else
{
textBox1.AppendText("+ ");
}
}
private async void MinusbButton_Click(object sender, EventArgs e)
{
if (live)
{
await SendLiveButtonPress("Minus");
}
else
{
textBox1.AppendText("- ");
}
}
#endregion Button
private void HoldButton_Click(object sender, EventArgs e)
{
/*
string timenumdirty = timerInputField.Text;
string digitsOnly = Regex.Replace(timenumdirty, @"\D", "");
digitsOnly = digitsOnly + " ";
textBox1.AppendText(digitsOnly);
*/
}
private void TimerInputField_TextChanged(object sender, EventArgs e)
{
}
private void DelayButton_Click(object sender, EventArgs e)
{
if (delayInputField.Text != "")
{
string delaynumdirty = delayInputField.Text;
string delaydigitsOnly = Regex.Replace(delaynumdirty, @"\D", "");
delaydigitsOnly = "d" + delaydigitsOnly + " ";
textBox1.AppendText(delaydigitsOnly);
}
}
private void AddIpButton_Click(object sender, EventArgs e)
{
string ipText = ipTextField.Text;
string switchName = switchNameTB.Text;
if (string.IsNullOrEmpty(switchName))
{
UpdateLogger("Add a name for the switch");
return;
}
if (!string.IsNullOrEmpty(ipText))
{
if (IPAddress.TryParse(ipText, out IPAddress address))
{
ListViewItem existingItem = null;
foreach (ListViewItem item in ipListView.Items)
{
if (item.SubItems[1].Text == switchName && item.SubItems[2].Text == ipText)
{
existingItem = item;
break;
}
}
if (existingItem != null)
{
existingItem.SubItems[1].Text = switchName;
existingItem.SubItems[2].Text = ipText;
}
else
{
ListViewItem newItem = new ListViewItem("");
newItem.SubItems.Add(switchName);
newItem.SubItems.Add(ipText);
ipListView.Items.Add(newItem);
ipListView.Invalidate();
}
ipTextField.Clear();
switchNameTB.Clear();
SaveData();
}
else
{
UpdateLogger("Invalid IP address format.");
}
}
}
private void DeleteIpButton_Click(object sender, EventArgs e)
{
if (ipListView.SelectedItems.Count > 0)
{
var selectedItem = ipListView.SelectedItems[0];
ipListView.Items.Remove(selectedItem);
SaveData();
}
}
private void DeleteButton_Click(object sender, EventArgs e)
{
if (macroListView.SelectedItems.Count > 0)
{
macroListView.Items.Remove(macroListView.SelectedItems[0]);
SaveData();
}
else
{
UpdateLogger("No macro selected");
}
}
private CancellationTokenSource cancellationTokenSource;
private async void PlaybButton_Click(object sender, EventArgs e)
{
bots.Clear();
InitializeBots();
if (textBox1.Text == "")
{
UpdateLogger("No macro entered");
return;
}
if (loopCheckbox.Checked == true)
{
pictureBoxStop.Enabled = true;
pictureBoxStop.BackColor = Color.Aqua;
pictureBoxPlay.Enabled = false;
UpdateLogger("Starting Macro Loop");
}
cancellationTokenSource = new CancellationTokenSource();
string commands = textBox1.Text;
Func<bool> loopFunc = () => loopCheckbox.Checked;
foreach (var bot in bots)
try
{
bot.Connect();
await bot.ExecuteCommands(commands, loopFunc, cancellationTokenSource.Token);
bot.Disconnect();
}
catch (Exception ex)
{
UpdateLogger(ex.Message);
}
}
private void StopbButton_Click(object sender, EventArgs e)
{
pictureBoxStop.BackColor = Color.White;
pictureBoxPlay.Enabled = true;
if (cancellationTokenSource != null)
{
cancellationTokenSource.Cancel();
UpdateLogger("Stopping Macro");
}
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBoxStop.Enabled = false;
}
public void UpdateLogger(string text)
{
if (this.InvokeRequired)
{
this.Invoke(new Action<string>(UpdateLogger), text);
}
else
{
logsBox.Text += (text + Environment.NewLine);
}
}
private void LivebButton_Click(object sender, EventArgs e)
{
live = !live;
string msg;
if (live)
{
msg = "Live mode on";
pictureBoxLive.BackColor = Color.FromArgb(253, 53, 58);
}
else
{
msg = "Live mode off";
pictureBoxLive.BackColor = Color.White;
}
UpdateLogger(msg);
}
private async Task SendLiveButtonPress(string button)
{
InitializeBots();
foreach (var bot in bots)
{
try
{
bot.Connect();
switch (button)
{
case "A":
await bot.PressAButton();
break;
case "B":
await bot.PressBButton();
break;
case "X":
await bot.PressXButton();
break;
case "Y":
await bot.PressYButton();
break;
case "L":
await bot.PressLButton();
break;
case "R":
await bot.PressRButton();
break;
case "ZL":
await bot.PressZLButton();
break;
case "ZR":
await bot.PressZRButton();
break;
case "Up":
await bot.PressDPadUp();
break;
case "Down":
await bot.PressDPadDown();
break;
case "Left":
await bot.PressDPadLeft();
break;
case "Right":
await bot.PressDPadRight();
break;
case "Plus":
await bot.PressPlusButton();
break;
case "Minus":
await bot.PressMinusButton();
break;
case "Home":
await bot.PressHomeButton();
break;
case "Capture":
await bot.PressCaptureButton();
break;
case "LStick":
await bot.PressLeftStickButton();
break;
case "RStick":
await bot.PressRightStickButton();
break;
default:
throw new ArgumentException("Invalid button string.");
}
bot.Disconnect();
}
catch (Exception ex)
{
UpdateLogger(ex.Message);
}
}
}
private async void BotStartBButton_Click(object sender, EventArgs e)
{
pictureBoxPlay.Enabled = false;
pictureBoxLive.Enabled = false;
}
private async void BotStopBButton_Click(object sender, EventArgs e)
{
pictureBoxPlay.Enabled = true;
pictureBoxLive.Enabled = true;
}
}
}