diff --git a/src/MaaWpfGui/App.xaml.cs b/src/MaaWpfGui/App.xaml.cs index bd3026ff56..43f16fda9b 100644 --- a/src/MaaWpfGui/App.xaml.cs +++ b/src/MaaWpfGui/App.xaml.cs @@ -44,10 +44,6 @@ namespace MaaWpfGui } } - // TODO: 现在的启动顺序是:OnStartup -> Bootstrapper.OnStart -> TaskQueueViewModel.OnInitialActivate, - // 在 OnInitialActivate 中会初始化 StageManager,但是 StageManager 中会异步下载更新,OnInitialActivate 不会等待这个异步操作 - // 导致这里切换了配置之后,StageManager 异步结束后调用 UpdateStageList 时会把 UI 现在的关卡配置存到新配置中 - // 先把 StageManager 的联网更新放到这里,之后看看有没有什么更好的办法 protected override void OnStartup(StartupEventArgs e) { if (WineRuntimeInformation.IsRunningUnderWine && MaaDesktopIntegration.Availabile) @@ -56,72 +52,7 @@ namespace MaaWpfGui FontConfigIntegration.Install(); } - ConfigurationHelper.Load(); - base.OnStartup(e); - - string[] args = e.Args; - const string ConfigFlag = "--config"; - - string configArgs = string.Empty; - for (int i = 0; i < args.Length; ++i) - { - switch (args[i]) - { - case ConfigFlag when i + 1 < args.Length: - configArgs = args[i + 1]; - i += 1; - break; - } - } - - if (Config(configArgs)) - { - return; - } - - _ = Instances.TaskQueueViewModel.UpdateDatePromptAndStagesWeb(); - } - - /// - /// 检查配置并切换,如果成功切换则重启 - /// - /// 配置名 - /// 切换并重启 - private static bool Config(string desiredConfig) - { - const string ConfigFile = @".\config\gui.json"; - if (!File.Exists(ConfigFile) || string.IsNullOrEmpty(desiredConfig)) - { - return false; - } - - try - { - if (UpdateConfiguration(desiredConfig)) - { - Bootstrapper.ShutdownAndRestartWithoutArgs(); - return true; - } - } - catch (Exception ex) - { - _logger.Error($"Error updating configuration: {desiredConfig}, ex: {ex.Message}"); - } - - return false; - } - - /// - /// 切换配置 - /// - /// 配置名 - /// 是否成功切换配置 - private static bool UpdateConfiguration(string desiredConfig) - { - // 配置名可能就包在引号中,需要转义符,如 \"a\" - string currentConfig = ConfigurationHelper.GetCurrentConfiguration(); - return currentConfig != desiredConfig && ConfigurationHelper.SwitchConfiguration(desiredConfig); } } } diff --git a/src/MaaWpfGui/Helper/WindowManager.cs b/src/MaaWpfGui/Helper/WindowManager.cs index 759a3b5bb5..34e6ec235c 100644 --- a/src/MaaWpfGui/Helper/WindowManager.cs +++ b/src/MaaWpfGui/Helper/WindowManager.cs @@ -33,10 +33,10 @@ namespace MaaWpfGui.Helper private static readonly ILogger _logger = Log.ForContext(); - private readonly bool _loadWindowPlacement = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.LoadWindowPlacement, bool.TrueString)); - private readonly bool _saveWindowPlacement = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.SaveWindowPlacement, bool.TrueString)); - private readonly bool _minimizeDirectly = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.MinimizeDirectly, bool.FalseString)); - private readonly bool _minimizeToTray = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.MinimizeToTray, bool.FalseString)); + private readonly bool _loadWindowPlacement = Convert.ToBoolean(ConfigurationHelper.GetGlobalValue(ConfigurationKeys.LoadWindowPlacement, bool.TrueString)); + private readonly bool _saveWindowPlacement = Convert.ToBoolean(ConfigurationHelper.GetGlobalValue(ConfigurationKeys.SaveWindowPlacement, bool.TrueString)); + private readonly bool _minimizeDirectly = Convert.ToBoolean(ConfigurationHelper.GetGlobalValue(ConfigurationKeys.MinimizeDirectly, bool.FalseString)); + private readonly bool _minimizeToTray = Convert.ToBoolean(ConfigurationHelper.GetGlobalValue(ConfigurationKeys.MinimizeToTray, bool.FalseString)); /// /// Center other windows in MaaWpfGui.RootView @@ -81,6 +81,13 @@ namespace MaaWpfGui.Helper _logger.Error("Failed to get window placement"); } + WindowPlacement defaultPlacement = default; + if (Equals(windowPlacement, defaultPlacement)) + { + _logger.Information("Window placement is the default value; skipping save."); + return; + } + if (!SetConfiguration(windowPlacement)) { _logger.Error("Failed to save window placement"); @@ -114,10 +121,10 @@ namespace MaaWpfGui.Helper { // 请在配置文件中修改该部分配置,暂不支持从GUI设置 // Please modify this part of configuration in the configuration file. - ConfigurationHelper.SetValue(ConfigurationKeys.LoadWindowPlacement, _loadWindowPlacement.ToString()); - ConfigurationHelper.SetValue(ConfigurationKeys.SaveWindowPlacement, _saveWindowPlacement.ToString()); + ConfigurationHelper.SetGlobalValue(ConfigurationKeys.LoadWindowPlacement, _loadWindowPlacement.ToString()); + ConfigurationHelper.SetGlobalValue(ConfigurationKeys.SaveWindowPlacement, _saveWindowPlacement.ToString()); - return ConfigurationHelper.SetValue(ConfigurationKeys.WindowPlacement, JsonConvert.SerializeObject(wp)); + return ConfigurationHelper.SetGlobalValue(ConfigurationKeys.WindowPlacement, JsonConvert.SerializeObject(wp)); } catch (Exception e) { @@ -130,7 +137,7 @@ namespace MaaWpfGui.Helper private static bool GetConfiguration(out WindowPlacement wp) { wp = default; - var jsonStr = ConfigurationHelper.GetValue(ConfigurationKeys.WindowPlacement, string.Empty); + var jsonStr = ConfigurationHelper.GetGlobalValue(ConfigurationKeys.WindowPlacement, string.Empty); if (string.IsNullOrEmpty(jsonStr)) { return false; diff --git a/src/MaaWpfGui/Main/Bootstrapper.cs b/src/MaaWpfGui/Main/Bootstrapper.cs index a555915fd5..a6f389cc7a 100644 --- a/src/MaaWpfGui/Main/Bootstrapper.cs +++ b/src/MaaWpfGui/Main/Bootstrapper.cs @@ -12,6 +12,7 @@ // using System; +using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Reflection; @@ -93,6 +94,7 @@ namespace MaaWpfGui.Main var maaEnv = Environment.GetEnvironmentVariable("MAA_ENVIRONMENT") == "Debug" ? "Debug" : "Production"; + var args = Environment.GetCommandLineArgs(); var withDebugFile = File.Exists("DEBUG") || File.Exists("DEBUG.txt"); loggerConfiguration = (maaEnv == "Debug" || withDebugFile) ? loggerConfiguration.MinimumLevel.Verbose() @@ -105,6 +107,7 @@ namespace MaaWpfGui.Main _logger.Information($"Version {uiVersion}"); _logger.Information($"Built at {builtDate:O}"); _logger.Information($"Maa ENV: {maaEnv}"); + _logger.Information($"Command Line: {string.Join(' ', args)}"); _logger.Information($"User Dir {Directory.GetCurrentDirectory()}"); if (withDebugFile) { @@ -157,6 +160,16 @@ namespace MaaWpfGui.Main return; } + const string ConfigFlag = "--config"; + const string AnotherFlag = "--another"; // 示例,之后如果有其他参数,可以继续添加 + + var parsedArgs = ParseArgs(args, ConfigFlag, AnotherFlag); + + if (parsedArgs.TryGetValue(ConfigFlag, out string configArgs) && Config(configArgs)) + { + return; + } + _hasMutex = true; ETagCache.Load(); @@ -361,5 +374,63 @@ namespace MaaWpfGui.Main errorView.ShowDialog(); }); } + + private static Dictionary ParseArgs(string[] args, params string[] flags) + { + var result = new Dictionary(); + var flagSet = new HashSet(flags); + + for (int i = 0; i < args.Length; ++i) + { + if (flagSet.Contains(args[i]) && i + 1 < args.Length) + { + result[args[i]] = args[i + 1]; + ++i; + } + } + + return result; + } + + /// + /// 检查配置并切换,如果成功切换则重启 + /// + /// 配置名 + /// 切换并重启 + private static bool Config(string desiredConfig) + { + const string ConfigFile = @".\config\gui.json"; + if (!File.Exists(ConfigFile) || string.IsNullOrEmpty(desiredConfig)) + { + return false; + } + + try + { + if (UpdateConfiguration(desiredConfig)) + { + ShutdownAndRestartWithoutArgs(); + return true; + } + } + catch (Exception ex) + { + _logger.Error($"Error updating configuration: {desiredConfig}, ex: {ex.Message}"); + } + + return false; + } + + /// + /// 切换配置 + /// + /// 配置名 + /// 是否成功切换配置 + private static bool UpdateConfiguration(string desiredConfig) + { + // 配置名可能就包在引号中,需要转义符,如 \"a\" + string currentConfig = ConfigurationHelper.GetCurrentConfiguration(); + return currentConfig != desiredConfig && ConfigurationHelper.SwitchConfiguration(desiredConfig); + } } } diff --git a/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs b/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs index a9bd9fe3e3..a83f85bcc8 100644 --- a/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs @@ -281,6 +281,8 @@ namespace MaaWpfGui.ViewModels.UI LogItemViewModels = []; InitializeItems(); InitTimer(); + + _ = UpdateDatePromptAndStagesWeb(); } /*