diff --git a/App.xaml.cs b/App.xaml.cs index 459c28f..8c7cdbf 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -20,9 +20,9 @@ public partial class App : Application #region Primary Color PaletteHelper paletteHelper = new(); Theme newTheme = paletteHelper.GetTheme(); - System.Drawing.Color newColor = Settings.Default.PrimaryColor; + System.Drawing.Color newPrimaryColor = Settings.Default.PrimaryColor; - newTheme.SetPrimaryColor(Color.FromRgb(newColor.R, newColor.G, newColor.B)); + newTheme.SetPrimaryColor(Color.FromRgb(newPrimaryColor.R, newPrimaryColor.G, newPrimaryColor.B)); paletteHelper.SetTheme(newTheme); #endregion Primary Color @@ -30,16 +30,17 @@ public partial class App : Application if (Environment.OSVersion.Version.Build < 22000) { Style newWindowStyle = new(typeof(Window), Current.Resources["CommonWindow"] as Style); + newWindowStyle.Setters.Add(new Setter(Window.BackgroundProperty, new DynamicResourceExtension("MaterialDesignBackground"))); Current.Resources["CommonWindow"] = newWindowStyle; } #endregion Background Color #region Foreground Color - Color? foregroundColor = ForegroundGenerator.GetForeground(newColor.R, newColor.G, newColor.B); - Style newButtonStyle = new(typeof(Button), Current.Resources[typeof(Button)] as Style); - newButtonStyle.Setters.Add(new Setter(Button.ForegroundProperty, foregroundColor.HasValue ? new SolidColorBrush(foregroundColor.Value) : new DynamicResourceExtension("MaterialDesignBackground"))); + Color? newForegroundColor = ForegroundGenerator.GetForeground(newPrimaryColor.R, newPrimaryColor.G, newPrimaryColor.B); + + newButtonStyle.Setters.Add(new Setter(Button.ForegroundProperty, newForegroundColor.HasValue ? new SolidColorBrush(newForegroundColor.Value) : new DynamicResourceExtension("MaterialDesignBackground"))); Current.Resources[typeof(Button)] = newButtonStyle; #endregion Foreground Color diff --git a/Preses/MainPres.cs b/Preses/MainPres.cs index 99048c6..89f08c0 100644 --- a/Preses/MainPres.cs +++ b/Preses/MainPres.cs @@ -14,8 +14,6 @@ internal partial class MainPres : GlobalPres internal MainPres(string[] args) { int browserPathIndex = Array.FindIndex(args, arg => arg.Equals("-b", StringComparison.OrdinalIgnoreCase)) + 1; - int upstreamUrlIndex = Array.FindIndex(args, arg => arg.Equals("-u", StringComparison.OrdinalIgnoreCase)) + 1; - int extraArgsIndex = Array.FindIndex(args, arg => arg.Equals("-e", StringComparison.OrdinalIgnoreCase)) + 1; BrowserPath = browserPathIndex != 0 && browserPathIndex != args.Length ? args[browserPathIndex] : !string.IsNullOrWhiteSpace(Settings.Default.BrowserPath) ? Settings.Default.BrowserPath : @@ -24,10 +22,14 @@ internal partial class MainPres : GlobalPres Registry.LocalMachine.OpenSubKey(MainConst.BraveBrowserRegistryPath)?.GetValue(string.Empty, null) ?? string.Empty).ToString()!; + int upstreamUrlIndex = Array.FindIndex(args, arg => arg.Equals("-u", StringComparison.OrdinalIgnoreCase)) + 1; + UpstreamUrl = upstreamUrlIndex == 0 || upstreamUrlIndex == args.Length ? !string.IsNullOrWhiteSpace(Settings.Default.UpstreamUrl) ? Settings.Default.UpstreamUrl : MainConst.DefaultUpstreamUrl : args[upstreamUrlIndex]; + int extraArgsIndex = Array.FindIndex(args, arg => arg.Equals("-e", StringComparison.OrdinalIgnoreCase)) + 1; + ExtraArgs = extraArgsIndex == 0 || extraArgsIndex == args.Length ? !string.IsNullOrWhiteSpace(Settings.Default.ExtraArgs) ? Settings.Default.ExtraArgs : string.Empty : args[extraArgsIndex]; diff --git a/Preses/SettingsPres.cs b/Preses/SettingsPres.cs index 29a675a..52185f9 100644 --- a/Preses/SettingsPres.cs +++ b/Preses/SettingsPres.cs @@ -49,13 +49,14 @@ internal partial class SettingsPres : GlobalPres private static bool? isLightWeight = null; partial void OnIsLightWeightChanged(bool? value) { + Style newWindowStyle = new(typeof(Window), Application.Current.Resources["CommonWindow"] as Style); FontWeight newWeight = value.HasValue ? value.Value ? FontWeights.Light : FontWeights.Bold : FontWeights.Regular; - Style newWindowStyle = new(typeof(Window), Application.Current.Resources["CommonWindow"] as Style); newWindowStyle.Setters.Add(new Setter(Window.FontWeightProperty, newWeight)); Application.Current.Resources["CommonWindow"] = newWindowStyle; Style newButtonStyle = new(typeof(Button), Application.Current.Resources[typeof(Button)] as Style); + newButtonStyle.Setters.Add(new Setter(Button.FontWeightProperty, newWeight)); Application.Current.Resources[typeof(Button)] = newButtonStyle; diff --git a/Proces/BrowserProc.cs b/Proces/BrowserProc.cs index 4170e69..4beaac6 100644 --- a/Proces/BrowserProc.cs +++ b/Proces/BrowserProc.cs @@ -6,7 +6,7 @@ namespace Sheas_Cealer.Proces; internal class BrowserProc : Proc { - private static bool ShutDownAppOnProcessExit; + private readonly bool ShutDownAppOnProcessExit; internal BrowserProc(string browserPath, bool shutDownAppOnProcessExit) : base(browserPath) => ShutDownAppOnProcessExit = shutDownAppOnProcessExit; diff --git a/Wins/AboutWin.xaml b/Wins/AboutWin.xaml index 4591992..3a73ab6 100644 --- a/Wins/AboutWin.xaml +++ b/Wins/AboutWin.xaml @@ -5,7 +5,9 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:consts="clr-namespace:Sheas_Cealer.Consts" xmlns:convs="clr-namespace:Sheas_Cealer.Convs" + xmlns:preses="clr-namespace:Sheas_Cealer.Preses" mc:Ignorable="d" + d:DataContext="{d:DesignInstance preses:AboutPres}" Style="{DynamicResource CommonWindow}" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" SizeToContent="Height" Width="500" KeyDown="AboutWin_KeyDown"> diff --git a/Wins/AboutWin.xaml.cs b/Wins/AboutWin.xaml.cs index e375f19..6e4784f 100644 --- a/Wins/AboutWin.xaml.cs +++ b/Wins/AboutWin.xaml.cs @@ -11,34 +11,33 @@ namespace Sheas_Cealer.Wins; public partial class AboutWin : Window { - private static AboutPres? AboutPres; + private readonly AboutPres AboutPres; internal AboutWin() { InitializeComponent(); - AboutPres = new(); + DataContext = AboutPres = new(); } protected override void OnSourceInitialized(EventArgs e) { IconRemover.RemoveIcon(this); - BorderThemeSetter.SetBorderTheme(this, AboutPres!.IsLightTheme); + BorderThemeSetter.SetBorderTheme(this, AboutPres.IsLightTheme); } private void AboutButton_Click(object sender, RoutedEventArgs e) { - Button? senderButton = sender as Button; + Button senderButton = (Button)sender; if (senderButton == VersionButton) MessageBox.Show($"{AboutConst._ReleasePagePasswordLabel} 3wnj"); - ProcessStartInfo processStartInfo = new(senderButton == EmailButton ? "mailto:" : string.Empty + senderButton!.ToolTip) { UseShellExecute = true }; + ProcessStartInfo processStartInfo = new(senderButton == EmailButton ? "mailto:" : string.Empty + senderButton.ToolTip) { UseShellExecute = true }; try { Process.Start(processStartInfo); } catch (UnauthorizedAccessException) { processStartInfo.Verb = "RunAs"; - Process.Start(processStartInfo); } } diff --git a/Wins/MainWin.xaml.cs b/Wins/MainWin.xaml.cs index 320acf1..9e19e73 100644 --- a/Wins/MainWin.xaml.cs +++ b/Wins/MainWin.xaml.cs @@ -33,27 +33,27 @@ namespace Sheas_Cealer.Wins; public partial class MainWin : Window { - private static MainPres? MainPres; - private static readonly HttpClient MainClient = new(new HttpClientHandler() { ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator }); - private static DispatcherTimer? HoldButtonTimer; - private static readonly DispatcherTimer ProxyTimer = new() { Interval = TimeSpan.FromSeconds(0.1) }; - private static readonly FileSystemWatcher CealHostWatcher = new(Path.GetDirectoryName(MainConst.CealHostPath)!, Path.GetFileName(MainConst.CealHostPath)) { EnableRaisingEvents = true, NotifyFilter = NotifyFilters.LastWrite }; - private static readonly FileSystemWatcher NginxConfWatcher = new(Path.GetDirectoryName(MainConst.NginxConfPath)!, Path.GetFileName(MainConst.NginxConfPath)) { EnableRaisingEvents = true, NotifyFilter = NotifyFilters.LastWrite }; - private static readonly FileSystemWatcher MihomoConfWatcher = new(Path.GetDirectoryName(MainConst.MihomoConfPath)!, Path.GetFileName(MainConst.MihomoConfPath)) { EnableRaisingEvents = true, NotifyFilter = NotifyFilters.LastWrite }; + private readonly MainPres MainPres; + private readonly HttpClient MainClient = new(new HttpClientHandler() { ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator }); + private DispatcherTimer? HoldButtonTimer; + private readonly DispatcherTimer ProxyTimer = new() { Interval = TimeSpan.FromSeconds(0.1) }; + private readonly FileSystemWatcher CealHostWatcher = new(Path.GetDirectoryName(MainConst.CealHostPath)!, Path.GetFileName(MainConst.CealHostPath)) { EnableRaisingEvents = true, NotifyFilter = NotifyFilters.LastWrite }; + private readonly FileSystemWatcher NginxConfWatcher = new(Path.GetDirectoryName(MainConst.NginxConfPath)!, Path.GetFileName(MainConst.NginxConfPath)) { EnableRaisingEvents = true, NotifyFilter = NotifyFilters.LastWrite }; + private readonly FileSystemWatcher MihomoConfWatcher = new(Path.GetDirectoryName(MainConst.MihomoConfPath)!, Path.GetFileName(MainConst.MihomoConfPath)) { EnableRaisingEvents = true, NotifyFilter = NotifyFilters.LastWrite }; - private static readonly SortedDictionary cealHostDomainPairs, string? cealHostSni, string cealHostIp)>> CealHostRulesDict = []; - private static string CealArgs = string.Empty; - private static NginxConfig? NginxConfs; - private static string? ExtraNginxConfs; - private static string? MihomoConfs; - private static string? ExtraMihomoConfs; + private readonly SortedDictionary cealHostDomainPairs, string? cealHostSni, string cealHostIp)>> CealHostRulesDict = []; + private string CealArgs = string.Empty; + private NginxConfig? NginxConfs; + private string? ExtraNginxConfs; + private string? MihomoConfs; + private string? ExtraMihomoConfs; - private static int NginxHttpPort = 80; - private static int NginxHttpsPort = 443; - private static int MihomoMixedPort = 7880; + private int NginxHttpPort = 80; + private int NginxHttpsPort = 443; + private int MihomoMixedPort = 7880; - private static int GameClickTime = 0; - private static int GameFlashInterval = 1000; + private int GameClickTime = 0; + private int GameFlashInterval = 1000; internal MainWin(string[] args) { @@ -64,12 +64,10 @@ public partial class MainWin : Window protected override void OnSourceInitialized(EventArgs e) { IconRemover.RemoveIcon(this); - BorderThemeSetter.SetBorderTheme(this, MainPres!.IsLightTheme); + BorderThemeSetter.SetBorderTheme(this, MainPres.IsLightTheme); } private async void MainWin_Loaded(object sender, RoutedEventArgs e) { - SettingsBox.Focus(); - await Task.Run(() => { ProxyTimer.Tick += ProxyTimer_Tick; @@ -95,12 +93,12 @@ public partial class MainWin : Window private void MainWin_Drop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) - MainPres!.BrowserPath = ((string[])e.Data.GetData(DataFormats.FileDrop))[0]; + MainPres.BrowserPath = ((string[])e.Data.GetData(DataFormats.FileDrop))[0]; } private void SettingsBox_TextChanged(object sender, TextChangedEventArgs e) { - switch (MainPres!.SettingsMode) + switch (MainPres.SettingsMode) { case MainConst.SettingsMode.BrowserPathMode: MainPres.BrowserPath = SettingsBox.Text; @@ -115,7 +113,7 @@ public partial class MainWin : Window } private void SettingsModeButton_Click(object sender, RoutedEventArgs e) { - MainPres!.SettingsMode = MainPres.SettingsMode switch + MainPres.SettingsMode = MainPres.SettingsMode switch { MainConst.SettingsMode.BrowserPathMode => MainConst.SettingsMode.UpstreamUrlMode, MainConst.SettingsMode.UpstreamUrlMode => MainConst.SettingsMode.ExtraArgsMode, @@ -127,7 +125,7 @@ public partial class MainWin : Window { OpenFileDialog browserPathDialog = new() { Filter = $"{MainConst._BrowserPathDialogFilterFileType} (*.exe)|*.exe" }; - switch (MainPres!.SettingsMode) + switch (MainPres.SettingsMode) { case MainConst.SettingsMode.BrowserPathMode when browserPathDialog.ShowDialog().GetValueOrDefault(): SettingsBox.Focus(); @@ -161,7 +159,7 @@ public partial class MainWin : Window (MessageBox.Show(MainConst._KillBrowserProcessPrompt, string.Empty, MessageBoxButton.YesNo) != MessageBoxResult.Yes)) return; - foreach (Process browserProcess in Process.GetProcessesByName(Path.GetFileNameWithoutExtension(MainPres!.BrowserPath))) + foreach (Process browserProcess in Process.GetProcessesByName(Path.GetFileNameWithoutExtension(MainPres.BrowserPath))) { browserProcess.Kill(); await browserProcess.WaitForExitAsync(); @@ -187,7 +185,7 @@ public partial class MainWin : Window { HoldButtonTimer?.Stop(); - if (!MainPres!.IsNginxRunning) + if (!MainPres.IsNginxRunning) { if ((CealHostRulesDict.ContainsValue(null!) && MessageBox.Show(MainConst._CealHostErrorPrompt, string.Empty, MessageBoxButton.YesNo) != MessageBoxResult.Yes) || (NginxHttpsPort != 443 && MessageBox.Show(MainConst._NginxHttpsPortOccupiedPrompt, string.Empty, MessageBoxButton.YesNo) != MessageBoxResult.Yes) || @@ -311,7 +309,7 @@ public partial class MainWin : Window { HoldButtonTimer?.Stop(); - if (!MainPres!.IsMihomoRunning) + if (!MainPres.IsMihomoRunning) { if (string.IsNullOrWhiteSpace(MihomoConfs)) throw new Exception(MainConst._MihomoConfErrorMsg); @@ -401,7 +399,7 @@ public partial class MainWin : Window if (!File.Exists(MainConst.UpstreamHostPath)) File.Create(MainConst.UpstreamHostPath).Dispose(); - string upstreamUpstreamHostUrl = (MainPres!.UpstreamUrl.StartsWith("http://") || MainPres.UpstreamUrl.StartsWith("https://") ? string.Empty : "https://") + MainPres.UpstreamUrl; + string upstreamUpstreamHostUrl = (MainPres.UpstreamUrl.StartsWith("http://") || MainPres.UpstreamUrl.StartsWith("https://") ? string.Empty : "https://") + MainPres.UpstreamUrl; string upstreamUpstreamHostString = await Http.GetAsync(upstreamUpstreamHostUrl, MainClient); string localUpstreamHostString = File.ReadAllText(MainConst.UpstreamHostPath); @@ -446,7 +444,7 @@ public partial class MainWin : Window return; } - if (!MainPres!.IsFlashing) + if (!MainPres.IsFlashing) { MessageBox.Show(MainConst._GameStartMsg); MainPres.IsFlashing = true; @@ -461,20 +459,20 @@ public partial class MainWin : Window PaletteHelper paletteHelper = new(); Theme newTheme = paletteHelper.GetTheme(); - Color newColor = Color.FromRgb((byte)random.Next(256), (byte)random.Next(256), (byte)random.Next(256)); + Color newPrimaryColor = Color.FromRgb((byte)random.Next(256), (byte)random.Next(256), (byte)random.Next(256)); bool isLightTheme = random.Next(2) == 0; - newTheme.SetPrimaryColor(newColor); + newTheme.SetPrimaryColor(newPrimaryColor); newTheme.SetBaseTheme(isLightTheme ? BaseTheme.Light : BaseTheme.Dark); paletteHelper.SetTheme(newTheme); foreach (Window currentWindow in Application.Current.Windows) BorderThemeSetter.SetBorderTheme(currentWindow, isLightTheme); - Color? foregroundColor = ForegroundGenerator.GetForeground(newColor.R, newColor.G, newColor.B); + Color? newForegroundColor = ForegroundGenerator.GetForeground(newPrimaryColor.R, newPrimaryColor.G, newPrimaryColor.B); Style newButtonStyle = new(typeof(Button), Application.Current.Resources[typeof(Button)] as Style); - newButtonStyle.Setters.Add(new Setter(Button.ForegroundProperty, foregroundColor.HasValue ? new SolidColorBrush(foregroundColor.Value) : new DynamicResourceExtension("MaterialDesignBackground"))); + newButtonStyle.Setters.Add(new Setter(Button.ForegroundProperty, newForegroundColor.HasValue ? new SolidColorBrush(newForegroundColor.Value) : new DynamicResourceExtension("MaterialDesignBackground"))); Application.Current.Resources[typeof(Button)] = newButtonStyle; if (GameFlashInterval > 100) @@ -510,7 +508,7 @@ public partial class MainWin : Window private void ProxyTimer_Tick(object? sender, EventArgs e) { - MainPres!.IsNginxExist = File.Exists(Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase!, Path.GetFileName(MainConst.NginxPath))); + MainPres.IsNginxExist = File.Exists(Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase!, Path.GetFileName(MainConst.NginxPath))); MainPres.IsNginxRunning = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(MainConst.NginxPath)).Length != 0; MainPres.IsMihomoExist = File.Exists(Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase!, Path.GetFileName(MainConst.MihomoPath))); MainPres.IsMihomoRunning = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(MainConst.MihomoPath)).Length != 0; @@ -585,7 +583,7 @@ public partial class MainWin : Window } private void NginxConfWatcher_Changed(object sender, FileSystemEventArgs e) { - if (MainConst.IsAdmin && MainPres!.IsNginxExist) + if (MainConst.IsAdmin && MainPres.IsNginxExist) { if (!File.Exists(MainConst.NginxConfPath)) File.Create(MainConst.NginxConfPath).Dispose(); @@ -661,7 +659,7 @@ public partial class MainWin : Window } private void MihomoConfWatcher_Changed(object sender, FileSystemEventArgs e) { - if (MainConst.IsAdmin && MainPres!.IsMihomoExist) + if (MainConst.IsAdmin && MainPres.IsMihomoExist) { try { diff --git a/Wins/SettingsWin.xaml.cs b/Wins/SettingsWin.xaml.cs index ddaf23f..418136a 100644 --- a/Wins/SettingsWin.xaml.cs +++ b/Wins/SettingsWin.xaml.cs @@ -13,7 +13,7 @@ namespace Sheas_Cealer.Wins; public partial class SettingsWin : Window { - private static SettingsPres? SettingsPres; + private readonly SettingsPres SettingsPres; internal SettingsWin() { @@ -24,37 +24,36 @@ public partial class SettingsWin : Window protected override void OnSourceInitialized(EventArgs e) { IconRemover.RemoveIcon(this); - BorderThemeSetter.SetBorderTheme(this, SettingsPres!.IsLightTheme); + BorderThemeSetter.SetBorderTheme(this, SettingsPres.IsLightTheme); } - private void ThemesButton_Click(object sender, RoutedEventArgs e) => SettingsPres!.IsLightTheme = SettingsPres.IsLightTheme.HasValue ? SettingsPres.IsLightTheme.Value ? null : true : false; + private void ThemesButton_Click(object sender, RoutedEventArgs e) => SettingsPres.IsLightTheme = SettingsPres.IsLightTheme.HasValue ? SettingsPres.IsLightTheme.Value ? null : true : false; private void LangsButton_Click(object sender, RoutedEventArgs e) { - SettingsPres!.IsEnglishLang = SettingsPres.IsEnglishLang.HasValue ? SettingsPres.IsEnglishLang.Value ? null : true : false; + SettingsPres.IsEnglishLang = SettingsPres.IsEnglishLang.HasValue ? SettingsPres.IsEnglishLang.Value ? null : true : false; MessageBox.Show(SettingsConst._ChangeLangSuccessMsg); } private void ColorsButton_Click(object sender, RoutedEventArgs e) { Random random = new(); - PaletteHelper paletteHelper = new(); Theme newTheme = paletteHelper.GetTheme(); - Color newColor = Color.FromRgb((byte)random.Next(256), (byte)random.Next(256), (byte)random.Next(256)); + Color newPrimaryColor = Color.FromRgb((byte)random.Next(256), (byte)random.Next(256), (byte)random.Next(256)); - newTheme.SetPrimaryColor(newColor); + newTheme.SetPrimaryColor(newPrimaryColor); paletteHelper.SetTheme(newTheme); - Color? foregroundColor = ForegroundGenerator.GetForeground(newColor.R, newColor.G, newColor.B); - Style newButtonStyle = new(typeof(Button), Application.Current.Resources[typeof(Button)] as Style); - newButtonStyle.Setters.Add(new Setter(Button.ForegroundProperty, foregroundColor.HasValue ? new SolidColorBrush(foregroundColor.Value) : new DynamicResourceExtension("MaterialDesignBackground"))); + Color? newForegroundColor = ForegroundGenerator.GetForeground(newPrimaryColor.R, newPrimaryColor.G, newPrimaryColor.B); + + newButtonStyle.Setters.Add(new Setter(Button.ForegroundProperty, newForegroundColor.HasValue ? new SolidColorBrush(newForegroundColor.Value) : new DynamicResourceExtension("MaterialDesignBackground"))); Application.Current.Resources[typeof(Button)] = newButtonStyle; - Settings.Default.PrimaryColor = System.Drawing.Color.FromArgb(newColor.A, newColor.R, newColor.G, newColor.B); + Settings.Default.PrimaryColor = System.Drawing.Color.FromArgb(newPrimaryColor.A, newPrimaryColor.R, newPrimaryColor.G, newPrimaryColor.B); Settings.Default.Save(); } - private void WeightsButton_Click(object sender, RoutedEventArgs e) => SettingsPres!.IsLightWeight = SettingsPres.IsLightWeight.HasValue ? SettingsPres.IsLightWeight.Value ? null : true : false; + private void WeightsButton_Click(object sender, RoutedEventArgs e) => SettingsPres.IsLightWeight = SettingsPres.IsLightWeight.HasValue ? SettingsPres.IsLightWeight.Value ? null : true : false; private void SettingsWin_KeyDown(object sender, KeyEventArgs e) {