forked from Soda-Quantum-Lab/SodaCL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainPage.xaml.cs
409 lines (375 loc) · 16.3 KB
/
MainPage.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
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
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SodaCL.Core.Auth;
using SodaCL.Core.Auth.Model;
using SodaCL.Core.Download;
using SodaCL.Core.Game;
using SodaCL.Core.Java;
using static SodaCL.Toolkits.Dialog;
using static SodaCL.Toolkits.GetResources;
using static SodaCL.Toolkits.Logger;
namespace SodaCL.Pages
{
/// <summary>
/// MainPage.xaml 的交互逻辑
/// </summary>
public partial class MainPage : Page
{
public static MainPage mainPage;
private string yiYanText;
#region 初始化
public MainPage()
{
InitializeComponent();
mainPage = this;
}
private async void Page_Initialized(object sender, EventArgs e)
{
SayHello();
TextAni();
await GetYiyanAsync();
var da = new DoubleAnimation(1, TimeSpan.FromSeconds(0.4));
YiYanTxb.BeginAnimation(OpacityProperty, da);
}
private void TextAni()
{
Storyboard storyboard = new Storyboard();
DoubleAnimation doubleAnimation = new DoubleAnimation(0.0, 1.0, TimeSpan.FromSeconds(0.5));
Storyboard.SetTarget(doubleAnimation, SayHelloUsernameTxb);
Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Opacity"));
storyboard.Children.Add(doubleAnimation);
DoubleAnimation doubleAnimation2 = new DoubleAnimation(0.0, 1.0, TimeSpan.FromSeconds(0.5));
Storyboard.SetTarget(doubleAnimation2, SayHelloTimeTxb);
Storyboard.SetTargetProperty(doubleAnimation2, new PropertyPath("Opacity"));
storyboard.Children.Add(doubleAnimation2);
DoubleAnimation doubleAnimation3 = new DoubleAnimation(0.0, 1.0, TimeSpan.FromSeconds(0.5))
{
BeginTime = TimeSpan.FromSeconds(0.2)
};
Storyboard.SetTarget(doubleAnimation3, YiYanTxb);
Storyboard.SetTargetProperty(doubleAnimation3, new PropertyPath("Opacity"));
storyboard.Children.Add(doubleAnimation3);
ThicknessAnimation thicknessAnimation = new ThicknessAnimation(new Thickness(0.0, 0.0, 0.0, 0.0), TimeSpan.FromSeconds(0.4))
{
BeginTime = TimeSpan.FromSeconds(0.2),
EasingFunction = new CubicEase
{
EasingMode = EasingMode.EaseInOut
}
};
Storyboard.SetTarget(thicknessAnimation, LaunchBar);
Storyboard.SetTargetProperty(thicknessAnimation, new PropertyPath("Margin"));
storyboard.Children.Add(thicknessAnimation);
storyboard.Begin();
}
private void SayHello()
{
try
{
SayHelloUsernameTxb.Text = Environment.UserName;
int hour = DateTime.Now.Hour;
switch (hour)
{
case int n when (n >= 3 && n < 5):
SayHelloTimeTxb.Text = "凌晨好!";
break;
case int n when (n >= 5 && n < 11):
SayHelloTimeTxb.Text = "上午好!";
break;
case int n when (n >= 11 && n < 13):
SayHelloTimeTxb.Text = "中午好!";
break;
case int n when (n >= 13 && n < 17):
SayHelloTimeTxb.Text = "下午好!";
break;
case int n when (n >= 17 && n < 19):
SayHelloTimeTxb.Text = "傍晚好!";
break;
case int n when ((n <= 23 && n >= 19) || n > 23):
SayHelloTimeTxb.Text = "晚上好!";
break;
case int n when (n >= 0 && n < 3):
SayHelloTimeTxb.Text = "午夜好!";
break;
}
}
catch (Exception ex)
{
Log(true, ModuleList.IO, LogInfo.Error, ex: ex);
}
}
/// <summary>
/// 向Api接口获取一言并做出处理
/// </summary>
private async Task GetYiyanAsync()
{
string text;
try
{
if (yiYanText == null)
{
Log(false, ModuleList.Network, LogInfo.Info, "正在获取一言");
do
{
using (var client = new HttpClient())
{
var yiYanApiAdd = "https://v1.hitokoto.cn/?c=c&c=a&encode=json&charset=utf-8&maxlength=10";
client.Timeout = TimeSpan.FromSeconds(5);
string jsonResponse = await client.GetStringAsync(yiYanApiAdd);
JObject jObj = JsonConvert.DeserializeObject<JObject>(jsonResponse);
string yiYan = (string)jObj["hitokoto"];
string space;
string endSpace;
if (yiYan.EndsWith("。") || yiYan.EndsWith("?") || yiYan.EndsWith("!"))
{
space = " ";
endSpace = "";
}
else
{
space = " ";
endSpace = " ";
}
YiYanTxb.Margin = new Thickness(10, 0, 0, 0);
text = $"「{space + yiYan + endSpace}」 —— {(string)jObj["from"]}";
}
}
while (text.Length > 35);
YiYanTxb.Text = text;
yiYanText = YiYanTxb.Text;
Log(false, ModuleList.Network, LogInfo.Info, "一言获取成功");
}
else
{
YiYanTxb.Text = yiYanText;
}
}
catch (Exception ex)
{
YiYanTxb.Text = "一言获取失败";
Log(false, ModuleList.Network, LogInfo.Warning, "一言获取失败", ex);
;
}
}
#endregion 初始化
#region 事件
private void DownloadTestButtonClick(object sender, RoutedEventArgs e)
{
MultiDownload multiDownload = new(8, "http://jk-insider.bakaxl.com:8888/job/BakaXL%20Insider%20Parrot/lastSuccessfulBuild/artifact/BakaXL_Public/bin/Jenkins%20Release/BakaXL_Secure/BakaXL.exe", ".\\SodaCL\\BakaXL.exe");
multiDownload.Start();
MessageBox.Show("下载开始,请等待大约 30s 后点击启动按钮\n若启动器崩溃请重新打开启动器并执行下载");
Log(true, ModuleList.Network, LogInfo.Info, "下载线程已启动");
for (int i = 0; i < 1; i--)
{
if (multiDownload.IsComplete)
{
MessageBox.Show("下载完成");
Log(true, ModuleList.Network, LogInfo.Info, "下载已完成");
break;
}
}
}
private void LogFolderOpenerButtonClick(object sender, RoutedEventArgs e)
{
Process.Start("explorer.exe", ".\\SodaCL\\logs");
}
private void DownloadButtonClick(object sender, RoutedEventArgs e)
{
try
{
System.Diagnostics.Process.Start(".\\SodaCL\\BakaXL.exe");
Log(true, ModuleList.Main, LogInfo.Info, "BakaXL 已启动");
}
catch (Exception ex)
{
Log(true, ModuleList.Main, LogInfo.Warning, "BakaXL 未能正常启动,可能是下载的文件不完整", ex);
}
}
private void EnvironmentCheckButtonClick(object sender, RoutedEventArgs e)
{
MC_VersionList.GetVersionList();
JavaFinding.AutoJavaFinding(true);
}
//登录Task取消Token
public CancellationTokenSource loginTsCancelSrc;
private async void StartBtn_Click(object sender, RoutedEventArgs e)
{
MSAuth msOAuth = new();
msOAuth.OpenWindows += MSOAuth_OpenWindows;
loginTsCancelSrc = new CancellationTokenSource();
MicrosoftAccount msAccount;
try
{
msAccount = await Task.Run<MicrosoftAccount>(async () =>
{
return await msOAuth.StartAuthAsync(GetText("OAuth2Token"));
}, loginTsCancelSrc.Token);
}
catch (MicrosoftAuthException ex)
{
string errorMsg;
switch (ex.ErrorType)
{
case MsAuthErrorType.AuthDeclined:
errorMsg = GetText("Login_Microsoft_Error_AuthDeclined");
Log(true, ModuleList.Login, LogInfo.Warning, "最终用户拒绝了授权请求", ex);
break;
case MsAuthErrorType.ExpiredToken:
errorMsg = GetText("Login_Microsoft_Error_ExpiredToken");
Log(true, ModuleList.Login, LogInfo.Warning, "登录超时", ex);
break;
case MsAuthErrorType.NoXboxAccount:
errorMsg = GetText("Login_Microsoft_Error_NoXboxAccount");
Log(true, ModuleList.Login, LogInfo.Warning, "用户未创建Xbox账户", ex);
break;
case MsAuthErrorType.XboxDisable:
errorMsg = GetText("Login_Microsoft_Error_XboxDisable");
Log(true, ModuleList.Login, LogInfo.Warning, " Xbox Live 不可用/禁止的国家/地区", ex);
break;
case MsAuthErrorType.NeedAdultAuth:
errorMsg = GetText("Login_Microsoft_Error_NeedAdultAuth");
Log(true, ModuleList.Login, LogInfo.Warning, "需要在 Xbox 页面上进行成人验证", ex);
break;
case MsAuthErrorType.NeedJoiningInFamily:
errorMsg = GetText("Login_Microsoft_Error_NeedJoiningInFamily");
Log(true, ModuleList.Login, LogInfo.Warning, "需要在 Xbox 页面上进行成人验证", ex);
break;
case MsAuthErrorType.NoGame:
errorMsg = GetText("Login_Microsoft_Error_NoGame");
Log(true, ModuleList.Login, LogInfo.Warning, "该帐户是儿童账户", ex);
break;
};
OpenDialog();
}
catch (OperationCanceledException)
{
Log(false, ModuleList.Login, LogInfo.Warning, "登录操作已取消");
}
catch (Exception ex)
{
Dispatcher.Invoke(new Action(() => { CloseDialog(); }));
Log(true, ModuleList.Network, LogInfo.Error, ex: ex);
}
finally
{
loginTsCancelSrc.Dispose();
}
}
private async void MSOAuth_OpenWindows(object sender, (WindowsTypes, string) e)
{
if (e.Item1.Equals(WindowsTypes.OpenInBrowser))
{
Dispatcher.Invoke(new Action(() => { ChangeDialog(); }));
await OpenOpenInBrowserWindow(e.Item2);
}
switch (e)
{
case (WindowsTypes.StartLogin, null):
{
await Dispatcher.InvokeAsync(() =>
{
OpenDialog();
MainWindow.mainWindow.DialogStackPan.Children.Add(new TextBlock() { Text = "正在初始化微软登录服务", FontSize = 18, TextAlignment = TextAlignment.Center });
MainWindow.mainWindow.DialogStackPan.Children.Add(new ProgressBar() { IsIndeterminate = true, Height = 10, Width = 300, Margin = new Thickness(0, 30, 0, 0) });
});
break;
}
}
}
#endregion 事件
/// <summary>
/// 打开登录说明界面
/// </summary>
/// <param name="deviceCode">显示的登录代码</param>
private async Task OpenOpenInBrowserWindow(string deviceCode)
{
await Dispatcher.InvokeAsync(() =>
{
var StackPan = new StackPanel { Margin = new Thickness(10, 20, 10, 0), Orientation = Orientation.Horizontal };
var iconBor = new Border
{
Height = 32,
Width = 32,
Margin = new Thickness(5, 0, 0, 0),
Background = GetBrush("Color1"),
CornerRadius = new CornerRadius(16),
Child = new System.Windows.Controls.Image
{
Width = 20,
Height = 20,
Source = GetSvg("Svg_Information"),
}
};
var exitButton = new Button
{
Margin = new Thickness(120, 0, 0, 0),
Height = 32,
Width = 32,
Style = GetStyle("Btn_NoBackground"),
Content = new System.Windows.Controls.Image
{
Width = 20,
Height = 20,
Source = GetSvg("Svg_Close")
}
};
var okButton = new Button
{
Margin = new Thickness(270, 0, 0, 0),
Content = GetText("Butten_OK"),
Style = GetStyle("Btn_Main")
};
exitButton.Click += (s, e) =>
{
loginTsCancelSrc.Cancel();
Dispatcher.Invoke(new Action(() => { CloseDialog(); }));
Log(false, ModuleList.Login, LogInfo.Warning, "用户取消了登录操作");
};
okButton.Click += (s, be) =>
{
Clipboard.SetText(deviceCode);
Log(false, ModuleList.IO, LogInfo.Info, "成功将DeviceCode复制到剪切板");
Process.Start("explorer", "https://www.microsoft.com/link");
Log(false, ModuleList.IO, LogInfo.Info, "成功打开浏览器");
};
StackPan.Children.Add(iconBor);
StackPan.Children.Add(new TextBlock
{
Height = 28,
Margin = new Thickness(10, 0, 0, 0),
Padding = new Thickness(0, 3, 0, 0),
Style = GetStyle("Text_Bold"),
Text = GetText("Login_Microsoft_MessageBox_OpenInBrowser_Title")
});
StackPan.Children.Add(exitButton);
MainWindow.mainWindow.DialogStackPan.Children.Add(StackPan);
MainWindow.mainWindow.DialogStackPan.Children.Add(new TextBlock
{
Margin = new Thickness(57, 10, 20, 0),
Text = GetText("Login_Microsoft_MessageBox_OpenInBrowser_Text_Tip")
});
MainWindow.mainWindow.DialogStackPan.Children.Add(new TextBlock
{
Margin = new Thickness(56, 10, 20, 0),
Style = GetStyle("Text_Bold"),
Text = GetText("Login_Microsoft_MessageBox_OpenInBrowser_Text_YourLoginCode")
});
MainWindow.mainWindow.DialogStackPan.Children.Add(new TextBlock
{
Margin = new Thickness(55, 5, 20, 0),
Text = deviceCode,
FontSize = 24,
});
MainWindow.mainWindow.DialogStackPan.Children.Add(okButton);
});
}
}
}