From a1233530680e898236ef30cabcd963ad3d6eb6a6 Mon Sep 17 00:00:00 2001 From: Space Time Date: Thu, 30 May 2024 14:22:56 +0800 Subject: [PATCH] =?UTF-8?q?1.1.0=20->=201.1.1=20=E7=AC=AC9=E6=AC=A1?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Preses/MainPres.cs | 11 ++++++----- Utils/Command.cs | 27 --------------------------- Utils/CommandProc.cs | 18 ++++++++++++++++++ Wins/MainWin.xaml | 2 +- Wins/MainWin.xaml.cs | 40 ++++++++++++++++------------------------ 5 files changed, 41 insertions(+), 57 deletions(-) delete mode 100644 Utils/Command.cs create mode 100644 Utils/CommandProc.cs diff --git a/Preses/MainPres.cs b/Preses/MainPres.cs index b5c6f73..746bbc8 100644 --- a/Preses/MainPres.cs +++ b/Preses/MainPres.cs @@ -1,12 +1,10 @@ using System; using System.IO; -using System.Windows; -using System.Windows.Media; using CommunityToolkit.Mvvm.ComponentModel; -using MaterialDesignColors; using MaterialDesignThemes.Wpf; using Sheas_Cealer.Consts; using Sheas_Cealer.Props; +using Sheas_Cealer.Wins; using File = System.IO.File; namespace Sheas_Cealer.Preses; @@ -17,7 +15,7 @@ internal partial class MainPres : ObservableObject { int browserPathIndex = Array.FindIndex(args, arg => arg == "-b") + 1, upstreamUrlIndex = Array.FindIndex(args, arg => arg == "-u") + 1, - extraArgsIndex = Array.FindIndex(args, arg => arg == "e") + 1; + extraArgsIndex = Array.FindIndex(args, arg => arg == "-e") + 1; BrowserPath = browserPathIndex == 0 ? (!string.IsNullOrWhiteSpace(Settings.Default.BrowserPath) ? Settings.Default.BrowserPath : string.Empty) : @@ -30,6 +28,9 @@ internal partial class MainPres : ObservableObject ExtraArgs = extraArgsIndex == 0 ? (!string.IsNullOrWhiteSpace(Settings.Default.ExtraArgs) ? Settings.Default.ExtraArgs : string.Empty) : args[extraArgsIndex]; + + if (Array.Exists(args, args => args == "-d")) + new MainWin([]).StartCealButton_Click(null!, null!); } [ObservableProperty] @@ -69,7 +70,7 @@ internal partial class MainPres : ObservableObject [ObservableProperty] private string extraArgs; - partial void OnExtraArgsChanged(string value) + private partial void OnExtraArgsChanged(string value) { if (MainConst.ExtraArgsRegex().IsMatch(value)) { diff --git a/Utils/Command.cs b/Utils/Command.cs deleted file mode 100644 index beb94b5..0000000 --- a/Utils/Command.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.Diagnostics; -using SheasCore; - -namespace Sheas_Cealer.Utils; - -internal class Command : Proc -{ - internal bool EnvironmentExitAfterProcessExited = true; - - internal Command(bool environmentExitAfterProcessExited) : base("Cmd.exe") - { - EnvironmentExitAfterProcessExited = environmentExitAfterProcessExited; - } - - public override void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) - { - } - public override void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e) - { - } - public override void Process_Exited(object sender, EventArgs e) - { - if (EnvironmentExitAfterProcessExited) - Environment.Exit(0); - } -} \ No newline at end of file diff --git a/Utils/CommandProc.cs b/Utils/CommandProc.cs new file mode 100644 index 0000000..e959c77 --- /dev/null +++ b/Utils/CommandProc.cs @@ -0,0 +1,18 @@ +using System; +using System.Windows; +using SheasCore; + +namespace Sheas_Cealer.Utils; + +internal class CommandProc : Proc +{ + internal bool ShutDownAppOnProcessExit; + + internal CommandProc(bool shutDownAppOnProcessExit) : base("Cmd.exe") => ShutDownAppOnProcessExit = shutDownAppOnProcessExit; + + public override void Process_Exited(object sender, EventArgs e) + { + if (ShutDownAppOnProcessExit) + Application.Current.Dispatcher.InvokeShutdown(); + } +} \ No newline at end of file diff --git a/Wins/MainWin.xaml b/Wins/MainWin.xaml index 6f2d493..c87196b 100644 --- a/Wins/MainWin.xaml +++ b/Wins/MainWin.xaml @@ -1,9 +1,9 @@  - { - CealingHostWatcher.Changed += CealingHostWatcher_Changed; - CealingHostWatcher_Changed(null!, null!); - }); + CealingHostWatcher.Changed += CealingHostWatcher_Changed; + CealingHostWatcher_Changed(null!, null!); } + protected override void OnSourceInitialized(EventArgs e) => IconRemover.RemoveIcon(this); private void MainWin_Loaded(object sender, RoutedEventArgs e) => SettingsBox.Focus(); - private void MainWin_Closing(object sender, CancelEventArgs e) => Environment.Exit(0); + private void MainWin_Closing(object sender, CancelEventArgs e) => Application.Current.Shutdown(); private void MainWin_DragEnter(object sender, DragEventArgs e) { @@ -97,13 +93,12 @@ public partial class MainWin : Window }; } - private void StartCealButton_Click(object sender, RoutedEventArgs e) + internal void StartCealButton_Click(object sender, RoutedEventArgs e) { if (HoldButtonTimer.IsEnabled) - { HoldButtonTimer.Stop(); + else return; - } if (string.IsNullOrWhiteSpace(CealArgs)) throw new Exception("规则无法识别,请检查伪造规则是否含有语法错误"); @@ -121,7 +116,7 @@ public partial class MainWin : Window browserProcess.WaitForExit(); } - new Command(true).ShellRun(AppDomain.CurrentDomain.SetupInformation.ApplicationBase!, CealArgs + (string.IsNullOrWhiteSpace(MainPres.ExtraArgs) ? "" : " " + MainPres.ExtraArgs)); + new CommandProc(true).ShellRun(AppDomain.CurrentDomain.SetupInformation.ApplicationBase!, (CealArgs + " " + MainPres!.ExtraArgs).Trim()); } private void StartCealButton_PreviewMouseDown(object sender, MouseButtonEventArgs e) { @@ -158,7 +153,7 @@ public partial class MainWin : Window if (string.IsNullOrWhiteSpace(MainPres.ExtraArgs)) MainPres.ExtraArgs = " " + MainPres.ExtraArgs; - new Command(false).ShellRun(AppDomain.CurrentDomain.SetupInformation.ApplicationBase!, CealArgs + (string.IsNullOrWhiteSpace(MainPres.ExtraArgs) ? "" : " " + MainPres.ExtraArgs)); + new CommandProc(false).ShellRun(AppDomain.CurrentDomain.SetupInformation.ApplicationBase!, CealArgs + (string.IsNullOrWhiteSpace(MainPres.ExtraArgs) ? "" : " " + MainPres.ExtraArgs)); } private void EditHostButton_Click(object sender, RoutedEventArgs e) @@ -170,8 +165,9 @@ public partial class MainWin : Window { string hostUrl = MainPres!.UpstreamUrl; string UpdateHostString = await Http.GetAsync(hostUrl, MainClient); - using StreamReader hostLocalStreamReader = new(Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase!, @"Cealing-Host.json")); - string hostLocalString = hostLocalStreamReader.ReadToEnd(); + string hostLocalString; + using (StreamReader hostLocalStreamReader = new(Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase!, @"Cealing-Host.json"))) + hostLocalString = hostLocalStreamReader.ReadToEnd(); if (hostLocalString.Replace("\r", string.Empty) == UpdateHostString) MessageBox.Show("本地伪造规则和上游一模一样"); @@ -190,7 +186,7 @@ public partial class MainWin : Window private void ThemesButton_Click(object sender, RoutedEventArgs e) => MainPres!.IsLightTheme = MainPres.IsLightTheme.HasValue ? (MainPres.IsLightTheme.Value ? null : true) : false; private void AboutButton_Click(object sender, RoutedEventArgs e) => new AboutWin().ShowDialog(); - private void CealingHostWatcher_Changed(object sender, FileSystemEventArgs e) + internal void CealingHostWatcher_Changed(object sender, FileSystemEventArgs e) { try { @@ -206,25 +202,21 @@ public partial class MainWin : Window hostJToken[1] = "c" + ruleIndex; foreach (var hostName in hostJToken[0]!) - { - if (hostName != hostJToken[1]) - hostRules += "MAP " + hostName + " " + hostJToken[1] + ","; - } + hostRules += "MAP " + hostName + " " + hostJToken[1] + ","; hostResolverRules += "MAP " + hostJToken[1] + " " + hostJToken[2] + ","; ++ruleIndex; } - CealArgs = @"/c @start .\""Uncealed-Browser.lnk"" --host-rules=""" + hostRules[0..^1] + @""" --host-resolver-rules=""" + hostResolverRules[0..^1] + @""" --ignore-certificate-errors " + MainPres!.ExtraArgs; + + CealArgs = @$"/c @start .\""Uncealed-Browser.lnk"" --host-rules=""{hostRules[0..^1]}"" --host-resolver-rules=""{hostResolverRules[0..^1]}"" --test-type --ignore-certificate-errors"; } catch { CealArgs = string.Empty; } } private void MainWin_KeyDown(object sender, KeyEventArgs e) { if (e.KeyboardDevice.Modifiers == ModifierKeys.Control && e.Key == Key.W) - Environment.Exit(0); + Application.Current.Shutdown(); } - - protected override void OnSourceInitialized(EventArgs e) => IconRemover.RemoveIcon(this); } \ No newline at end of file