-
Notifications
You must be signed in to change notification settings - Fork 3
/
StartWindow.xaml.cs
180 lines (157 loc) · 6 KB
/
StartWindow.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
using log4net;
using McHMR_Updater_v2.core.customException;
using McHMR_Updater_v2.core.entity;
using McHMR_Updater_v2.core.utils;
using Microsoft.Win32;
using Newtonsoft.Json;
using System;
using System.Diagnostics;
using System.IO;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using Wpf.Ui.Controls;
namespace McHMR_Updater_v2;
public partial class StartWindow : FluentWindow
{
public static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private int BtnStatus = 0;
private int selectExe = 0;
private string selectedFilePath;
public StartWindow()
{
this.Height = 150;
Wpf.Ui.Appearance.SystemThemeWatcher.Watch(this);
InitializeComponent();
resultMsg.Text = "如果你看到了此窗口,请联系服主";
}
private async void verifyBtn_Click(object sender, RoutedEventArgs e)
{
// 初始化
apiInput.BorderBrush = base.BorderBrush;
launcherInput.BorderBrush = base.BorderBrush;
if (BtnStatus == 1)
{
Process.Start(ConfigurationCheck.getCurrentDir() + "\\McHMR-Updater v2.exe");
await Task.Delay(1000);
Process.GetCurrentProcess().Kill();
}
// 检测地址是否填写
if (apiInput.Text.Equals(null) || apiInput.Text.Equals(""))
{
Log.Info("请填写 API 地址");
apiInput.BorderBrush = Brushes.Red;
resultMsg.Text = "请填写 API 地址";
return;
}
// 检测启动器是否选择
if (launcherInput.Text.Equals(null) || launcherInput.Text.Equals(""))
{
Log.Info("请选择启动器路径");
launcherInput.BorderBrush = Brushes.Red;
resultMsg.Text = "请选择启动器路径";
return;
}
// 检测是否选择文件正常
if (selectExe == 0)
{
Log.Info("文件不在程序目录中");
launcherInput.BorderBrush = Brushes.Red;
resultMsg.Text = "文件必须在程序目录中";
return;
}
// 检测地址是否正常
btnText.Visibility = Visibility.Hidden;
btnLoadding.Visibility = Visibility.Visible;
try
{
var client = new RestSharpClient(apiInput.Text, null, false);
var apiResponse = await client.GetAsync<ApiEntity>("");
if (apiResponse.code == 0)
{
// 保存至配置
FileStream fileStream = new FileStream(ConfigurationCheck.getConfigFile(), FileMode.Truncate);
StreamWriter writer = new StreamWriter(fileStream, Encoding.UTF8);
writer.Write(JsonConvert.SerializeObject(apiResponse.data));
writer.Close();
fileStream.Close();
ConfigureReadAndWriteUtil.SetConfigValue("launcher", launcherInput.Text, typeof(string));
Log.Info("配置完成");
resultMsg.Foreground = Brushes.Green;
resultMsg.Text = "配置完成,点击完成重新启动";
btnText.Visibility = Visibility.Visible;
btnLoadding.Visibility = Visibility.Hidden;
btnText.Text = "完成";
BtnStatus = 1;
}
}
catch (Exception ex)
{
Log.Error("ex.Message", ex);
resultMsg.Text = ex.Message;
btnText.Visibility = Visibility.Visible;
btnLoadding.Visibility = Visibility.Hidden;
}
}
private void TitleBar_CloseClicked(TitleBar sender, RoutedEventArgs args)
{
Process.GetCurrentProcess().Kill();
}
private void FluentWindow_Loaded(object sender, RoutedEventArgs e)
{
// 网络检测
if (!NetworkInterface.GetIsNetworkAvailable())
{
Log.Error("网络未连接");
throw new NetworkNotConnectedException("网络未连接");
}
}
private void selectLauncherBtn_Click(object sender, RoutedEventArgs e)
{
// 显示文件选择对话框
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "可执行文件|*.exe";
openFileDialog.InitialDirectory = ConfigurationCheck.getCurrentDir();
openFileDialog.Title = "选择exe可执行文件";
openFileDialog.Multiselect = false;
if (openFileDialog.ShowDialog() == true) // 确保用户选择了文件
{
selectedFilePath = openFileDialog.FileName;
// 检查文件是否在程序目录中
string appDir = ConfigurationCheck.getCurrentDir();
if (!IsFileInDirectory(selectedFilePath, appDir))
{
// 文件不在程序目录中
Log.Info("文件不在程序目录中");
launcherInput.BorderBrush = Brushes.Red;
resultMsg.Text = "文件必须在程序目录中";
selectExe = 0;
}
else
{
// 文件在程序目录中
selectedFilePath = selectedFilePath.Replace(appDir, "");
launcherInput.BorderBrush = base.BorderBrush;
launcherInput.Text = selectedFilePath;
resultMsg.Text = "";
selectExe = 1;
}
}
else
{
// 用户取消了文件选择
Log.Info("请选择启动器路径");
launcherInput.BorderBrush = Brushes.Red;
resultMsg.Text = "请选择启动器路径";
}
}
private bool IsFileInDirectory(string filePath, string directoryPath)
{
// 获取文件的目录路径
string fileDirectory = System.IO.Path.GetDirectoryName(filePath);
// 检查文件目录是否与应用程序目录相同
return string.Equals(fileDirectory, directoryPath, StringComparison.OrdinalIgnoreCase);
}
}