From 94d6eb00ffdf76780ed293b398bad365d8dcd03e Mon Sep 17 00:00:00 2001 From: uye <99072975+ABA2396@users.noreply.github.com> Date: Sat, 26 Aug 2023 13:22:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E5=B8=A6=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E5=90=AF=E5=8A=A8=E5=88=87=E6=8D=A2=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MaaWpfGui/App.xaml.cs | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/MaaWpfGui/App.xaml.cs b/src/MaaWpfGui/App.xaml.cs index 60ae6a18a5..1955e9441d 100644 --- a/src/MaaWpfGui/App.xaml.cs +++ b/src/MaaWpfGui/App.xaml.cs @@ -11,9 +11,14 @@ // but WITHOUT ANY WARRANTY // +using System; using System.Diagnostics; +using System.IO; using System.Windows; using System.Windows.Documents; +using MaaWpfGui.Helper; +using MaaWpfGui.Main; +using Serilog; namespace MaaWpfGui { @@ -22,6 +27,8 @@ namespace MaaWpfGui /// public partial class App : Application { + private static readonly ILogger _logger = Log.ForContext(); + public void Hyperlink_Click(object sender, RoutedEventArgs e) { Hyperlink link = sender as Hyperlink; @@ -30,5 +37,53 @@ namespace MaaWpfGui Process.Start(new ProcessStartInfo(link.NavigateUri.AbsoluteUri)); } } + + protected override void OnStartup(StartupEventArgs e) + { + base.OnStartup(e); + + string[] args = e.Args; + const string ConfigFile = @".\config\gui.json"; + const string ConfigFlag = "--config"; + + string desiredConfig = string.Empty; + + for (int i = 0; i < args.Length; ++i) + { + if (args[i] != ConfigFlag || i + 1 >= args.Length) + { + continue; + } + + desiredConfig = args[i + 1]; + i += 1; + } + + if (!File.Exists(ConfigFile) || string.IsNullOrEmpty(desiredConfig)) + { + return; + } + + try + { + if (!UpdateConfiguration(desiredConfig)) + { + return; + } + + Bootstrapper.ShutdownAndRestartWithOutArgs(); + } + catch (Exception ex) + { + _logger.Error($"Error updating configuration: {desiredConfig}, ex: {ex.Message}"); + } + } + + private static bool UpdateConfiguration(string desiredConfig) + { + // 配置名可能就包在引号中,需要转义符,如 \"a\" + string currentConfig = ConfigurationHelper.GetCurrentConfiguration(); + return currentConfig != desiredConfig && ConfigurationHelper.SwitchConfiguration(desiredConfig); + } } }