-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
109 lines (104 loc) · 3.78 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
using Panuon.UI.Silver;
using StoneWeather.Class.Discords;
using StoneWeather.Class.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
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 StoneWeather
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : WindowX
{
#region 声明字段
private DiscordBot Bot = new DiscordBot();
#endregion
#region 核心方法
public MainWindow()
{
InitializeComponent();
try
{
ConfigLoad con = new ConfigLoad("Stone Weather\\Config.ini");
string Base64Data = con.GetStringValue("BotToken");
byte[] FromBase64Data = Convert.FromBase64String(Base64Data);
string DecryptedData = null;
using (Class.Crypto.AES AES = new Class.Crypto.AES())
{
string ID = Class.Tools.Device.ID.GetIdentificationCode();
DecryptedData = Encoding.UTF8.GetString(AES.Decrypt(FromBase64Data, ID));
}
this.BotTokenPasswordBox.Password = DecryptedData;
}
catch { }
this.Closed += delegate
{
StopBot("Close", new RoutedEventArgs());
};
}
#endregion
#region UI 元素方法
internal async void StartBot(object sender, RoutedEventArgs e)
{
this.StartBotButton.IsEnabled = false;
ButtonHelper.SetIsPending(this.StartBotButton, true);
Task.Run(async () =>
{
await Bot.Connect(this.BotTokenPasswordBox.Password);
Bot.Client.Ready += delegate
{
this.Dispatcher.BeginInvoke(() =>
{
ButtonHelper.SetIsPending(this.StartBotButton, false);
this.StopBotButton.IsEnabled = true;
});
return Task.CompletedTask;
};
});
}
internal async void StopBot(object sender, RoutedEventArgs e)
{
this.StopBotButton.IsEnabled = false;
ButtonHelper.SetIsPending(this.StopBotButton, true);
Task.Run(async () =>
{
await Bot.Disconnect();
this.Dispatcher.BeginInvoke(() =>
{
ButtonHelper.SetIsPending(this.StopBotButton, false);
this.StartBotButton.IsEnabled = true;
});
});
}
internal void SaveBotToken(object sender, RoutedEventArgs e)
{
this.SaveBotTokenButton.IsEnabled = false;
ButtonHelper.SetIsPending(this.SaveBotTokenButton, true);
ConfigSet cs = new ConfigSet("Stone Weather\\Config.ini", true);
byte[] ByteData = Encoding.UTF8.GetBytes(this.BotTokenPasswordBox.Password);
byte[] EncryptedData = null;
using (Class.Crypto.AES AES = new Class.Crypto.AES())
{
string ID = Class.Tools.Device.ID.GetIdentificationCode();
EncryptedData = AES.Encrypt(ByteData, ID);
}
cs.SetConfigValue("BotToken", Convert.ToBase64String(EncryptedData));
cs.WriteConfigToFile(ConfigFile.newFile);
ButtonHelper.SetIsPending(this.SaveBotTokenButton, false);
this.SaveBotTokenButton.IsEnabled = true;
}
#endregion
}
}