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); + } } }