You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
313 lines
10 KiB
313 lines
10 KiB
using log4net; |
|
using SparkClient.Model.Helper; |
|
using SparkClient.ViewModel.Configuration.SettingsPages; |
|
using System.Data; |
|
using System.Diagnostics; |
|
using System.Text; |
|
using System.Windows; |
|
using System.Windows.Input; |
|
using Microsoft.Win32; |
|
using MessageBox = SparkClient.Views.Dialog.MessageBox; |
|
namespace SparkClient; |
|
|
|
public partial class LoginWindow : Window |
|
{ |
|
private static readonly ILog Logger = LogManager.GetLogger(typeof(LoginWindow)); |
|
public LoginWindow() |
|
{ |
|
InitializeComponent(); |
|
WindowStartupLocation = WindowStartupLocation.CenterScreen; |
|
AccountTextBox.Text = Settings.SelectValueById("SAVE_ACCOUNT"); |
|
PasswordBox.Password = Settings.SelectValueById("SAVE_PASSWORD"); |
|
//checkCUDA(); |
|
checkNvidia(); |
|
// 检测MSVCRuntime是否安装 |
|
checkMSVCRuntime(); |
|
} |
|
|
|
private void checkNvidia() |
|
{ |
|
string nvidiaVersion = ""; |
|
try |
|
{ |
|
using (Process process = new Process()) |
|
{ |
|
process.StartInfo.FileName = "nvidia-smi"; |
|
process.StartInfo.Arguments = "--query-gpu=driver_version --format=csv,noheader,nounits"; |
|
process.StartInfo.UseShellExecute = false; |
|
process.StartInfo.CreateNoWindow = true; |
|
process.StartInfo.RedirectStandardOutput = true; |
|
process.StartInfo.RedirectStandardError = true; |
|
|
|
// 使用StringBuilder来捕获输出和错误信息 |
|
StringBuilder outputBuilder = new StringBuilder(); |
|
StringBuilder errorBuilder = new StringBuilder(); |
|
|
|
process.OutputDataReceived += (sender, e) => |
|
{ |
|
if (e.Data != null) |
|
outputBuilder.AppendLine(e.Data); |
|
}; |
|
process.ErrorDataReceived += (sender, e) => |
|
{ |
|
if (e.Data != null) |
|
errorBuilder.AppendLine(e.Data); |
|
}; |
|
|
|
process.Start(); |
|
|
|
// 开始异步读取输出和错误流 |
|
process.BeginOutputReadLine(); |
|
process.BeginErrorReadLine(); |
|
|
|
process.WaitForExit(); |
|
|
|
// 获取完整的输出和错误信息 |
|
string outputMsg = outputBuilder.ToString(); |
|
string errorMsg = errorBuilder.ToString(); |
|
nvidiaVersion = (string.IsNullOrWhiteSpace(errorMsg) ? outputMsg : errorMsg).Trim(); |
|
if (process.ExitCode == 0 || !string.IsNullOrWhiteSpace(nvidiaVersion)) |
|
{ |
|
if (CompareVersions(nvidiaVersion, "528.33") <= 0) |
|
{ |
|
MessageBox messageBox = new MessageBox(); |
|
messageBox.Show(MultilingualHelper.getString("NotNvidiaVersionLow") |
|
.Replace("%version", nvidiaVersion)); |
|
} |
|
} |
|
else |
|
{ |
|
MessageBox messageBox = new MessageBox(); |
|
messageBox.Show(MultilingualHelper.getString("NotNvidia")); |
|
} |
|
} |
|
} |
|
catch (Exception ex) |
|
{ |
|
Logger.Error(ex.Message); |
|
MessageBox messageBox = new MessageBox(); |
|
messageBox.Show(MultilingualHelper.getString("NotNvidia")); |
|
} |
|
} |
|
|
|
public static int CompareVersions(string versionA, string versionB) |
|
{ |
|
int[] partsA = ParseVersion(versionA); |
|
int[] partsB = ParseVersion(versionB); |
|
|
|
int maxLength = Math.Max(partsA.Length, partsB.Length); |
|
for (int i = 0; i < maxLength; i++) |
|
{ |
|
int a = (i < partsA.Length) ? partsA[i] : 0; |
|
int b = (i < partsB.Length) ? partsB[i] : 0; |
|
|
|
if (a != b) |
|
{ |
|
return a.CompareTo(b); |
|
} |
|
} |
|
|
|
return 0; // 版本相同 |
|
} |
|
private static int[] ParseVersion(string version) |
|
{ |
|
string[] stringParts = version.Split('.'); |
|
int[] intParts = new int[stringParts.Length]; |
|
|
|
for (int i = 0; i < stringParts.Length; i++) |
|
{ |
|
if (!int.TryParse(stringParts[i], out int part)) |
|
{ |
|
return [0, 0, 0]; |
|
} |
|
intParts[i] = part; |
|
} |
|
|
|
return intParts; |
|
} |
|
private void checkCUDA() |
|
{ |
|
try |
|
{ |
|
ProcessStartInfo startInfo = new ProcessStartInfo |
|
{ |
|
FileName = "nvcc", |
|
Arguments = "--version", |
|
RedirectStandardOutput = true, |
|
UseShellExecute = false, |
|
CreateNoWindow = true |
|
}; |
|
|
|
using (Process process = new Process { StartInfo = startInfo }) |
|
{ |
|
process.Start(); |
|
string output = process.StandardOutput.ReadToEnd(); |
|
process.WaitForExit(); |
|
|
|
if (process.ExitCode == 0) |
|
{ |
|
Logger.Error("CUDA 已安装\n" + output); |
|
} |
|
else |
|
{ |
|
MessageBox messageBox = new MessageBox(); |
|
messageBox.Show(MultilingualHelper.getString("CUDANoInstall")); |
|
Logger.Error("CUDA 未安装"); |
|
} |
|
} |
|
} |
|
catch (Exception ex) |
|
{ |
|
MessageBox messageBox = new MessageBox(); |
|
messageBox.Show(MultilingualHelper.getString("CUDANoInstall")); |
|
Logger.Error("CUDA 安装检测错误:"+ ex); |
|
} |
|
} |
|
private void checkMSVCRuntime() |
|
{ |
|
try |
|
{ |
|
ProcessStartInfo startInfo = new ProcessStartInfo |
|
{ |
|
FileName = "where", |
|
Arguments = "msvcp140.dll", |
|
RedirectStandardOutput = true, |
|
UseShellExecute = false, |
|
CreateNoWindow = true |
|
}; |
|
|
|
using (Process process = new Process { StartInfo = startInfo }) |
|
{ |
|
process.Start(); |
|
string output = process.StandardOutput.ReadToEnd(); |
|
process.WaitForExit(); |
|
|
|
if (string.IsNullOrEmpty(output)) |
|
{ |
|
MessageBox messageBox = new MessageBox(); |
|
messageBox.Show(MultilingualHelper.getString("MSVCRuntimeNoInstall")); |
|
Logger.Error("MSVC Runtime 未安装"); |
|
Environment.Exit(0); // 退出程序 |
|
} |
|
else |
|
{ |
|
Logger.Info("MSVC Runtime 已安装"); |
|
} |
|
} |
|
} |
|
catch (Exception ex) |
|
{ |
|
MessageBox messageBox = new MessageBox(); |
|
messageBox.Show(MultilingualHelper.getString("MSVCRuntimeNoInstall")); |
|
Logger.Error("MSVC Runtime 安装检测错误:" + ex); |
|
Environment.Exit(0); // 退出程序 |
|
} |
|
} |
|
private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) |
|
{ |
|
if (e.ButtonState == MouseButtonState.Pressed) |
|
{ |
|
this.DragMove(); |
|
} |
|
} |
|
|
|
private void UIElement_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) |
|
{ |
|
AccountTextBox.Text = string.Empty; |
|
} |
|
|
|
private void CloseButton_OnClick(object sender, RoutedEventArgs e) |
|
{ |
|
this.Close(); |
|
Environment.Exit(0); |
|
} |
|
|
|
private void ConfirmButton_Click(object sender, RoutedEventArgs e) |
|
{ |
|
#if DEBUG |
|
savePassword(); |
|
new MainWindow().Show(); |
|
this.Close(); |
|
#else |
|
string account = AccountTextBox.Text; |
|
string password = PasswordBox.Password; |
|
string passwordEnc = Common.GenerateMd5Hash(password); |
|
string passworddb = getPassword(account); |
|
if (string.IsNullOrEmpty(password) || !passworddb.Equals(passwordEnc)) |
|
{ |
|
MessageBox messageBox = new MessageBox(); |
|
messageBox.Show(MultilingualHelper.getString("NoPassword")); |
|
return; |
|
} |
|
savePassword(); |
|
new MainWindow().Show(); |
|
this.Close(); |
|
#endif |
|
} |
|
|
|
private void savePassword() |
|
{ |
|
if (IsRemberPassword.IsChecked ?? false) |
|
{ |
|
if (!"admin".Equals(AccountTextBox.Text)){ |
|
Settings saveAccount = new Settings() |
|
{ |
|
Key = "SAVE_ACCOUNT", |
|
ItemName = "保存用户名", |
|
Value = AccountTextBox.Text, |
|
}; |
|
saveAccount.insert(); |
|
Settings savePassword = new Settings() |
|
{ |
|
Key = "SAVE_PASSWORD", |
|
ItemName = "保存密码", |
|
Value = PasswordBox.Password, |
|
}; |
|
savePassword.insert(); |
|
} |
|
} |
|
else |
|
{ |
|
Settings saveAccount = new Settings() |
|
{ |
|
Key = "SAVE_ACCOUNT", |
|
ItemName = "保存用户名", |
|
Value = "", |
|
}; |
|
saveAccount.insert(); |
|
Settings savePassword = new Settings() |
|
{ |
|
Key = "SAVE_PASSWORD", |
|
ItemName = "保存密码", |
|
Value = "", |
|
}; |
|
savePassword.insert(); |
|
} |
|
Settings PERMISSIONS = new Settings() |
|
{ |
|
Key = "PERMISSIONS", |
|
ItemName = "权限", |
|
Value = AccountTextBox.Text, |
|
}; |
|
PERMISSIONS.insert(); |
|
} |
|
|
|
private string getPassword(string userName) |
|
{ |
|
string password = string.Empty; |
|
string sql = $"SELECT USER_PASSWORD FROM USER WHERE USER_NAME='{userName}';"; |
|
DataTable dt = DataBaseHelper.ExecuteQuery(sql); |
|
if (dt != null && dt.Rows.Count > 0) |
|
{ |
|
password = dt.Rows[0]["USER_PASSWORD"].ToString(); |
|
} |
|
return password; |
|
} |
|
private void PasswordBox_KeyDown(object sender, KeyEventArgs e) |
|
{ |
|
if (e.Key == Key.Enter) |
|
{ |
|
ConfirmButton_Click(sender, new RoutedEventArgs()); |
|
} |
|
} |
|
} |