mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-18 10:10:45 +08:00
feat: 支持带参数启动切换配置
This commit is contained in:
@@ -11,9 +11,14 @@
|
||||
// but WITHOUT ANY WARRANTY
|
||||
// </copyright>
|
||||
|
||||
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
|
||||
/// </summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
private static readonly ILogger _logger = Log.ForContext<App>();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user