refactor: 在RunningState类中用状态机替换idle处理

This commit is contained in:
uye
2023-07-02 01:44:56 +08:00
parent 6843b50e17
commit 21d6c76ef6
7 changed files with 117 additions and 39 deletions

View File

@@ -28,6 +28,7 @@ using HandyControl.Data;
using MaaWpfGui.Constants;
using MaaWpfGui.Extensions;
using MaaWpfGui.Helper;
using MaaWpfGui.States;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Stylet;
@@ -50,6 +51,8 @@ namespace MaaWpfGui.Main
/// </summary>
public class AsstProxy
{
private readonly RunningState runningState;
private delegate void CallbackDelegate(int msg, IntPtr json_buffer, IntPtr custom_arg);
private delegate void ProcCallbackMsg(AsstMsg msg, JObject details);
@@ -241,6 +244,7 @@ namespace MaaWpfGui.Main
public AsstProxy()
{
_callback = CallbackFunction;
runningState = RunningState.Instance;
}
/// <summary>
@@ -316,7 +320,7 @@ namespace MaaWpfGui.Main
}
Instances.TaskQueueViewModel.SetInited();
Instances.TaskQueueViewModel.Idle = true;
runningState.SetIdle(true);
this.AsstSetInstanceOption(InstanceOptionKey.TouchMode, Instances.SettingsViewModel.TouchMode);
this.AsstSetInstanceOption(InstanceOptionKey.DeploymentWithPause, Instances.SettingsViewModel.DeploymentWithPause ? "1" : "0");
this.AsstSetInstanceOption(InstanceOptionKey.AdbLiteEnabled, Instances.SettingsViewModel.AdbLiteEnabled ? "1" : "0");
@@ -325,7 +329,7 @@ namespace MaaWpfGui.Main
if (Instances.SettingsViewModel.RunDirectly)
{
// 如果是直接运行模式,就先让按钮显示为运行
Instances.TaskQueueViewModel.Idle = false;
runningState.SetIdle(false);
}
await Task.Run(() => Instances.SettingsViewModel.TryToStartEmulator());
@@ -340,7 +344,7 @@ namespace MaaWpfGui.Main
if (Instances.SettingsViewModel.RunDirectly)
{
// 重置按钮状态不影响LinkStart判断
Instances.TaskQueueViewModel.Idle = true;
runningState.SetIdle(true);
Instances.TaskQueueViewModel.LinkStart();
}
});
@@ -473,7 +477,7 @@ namespace MaaWpfGui.Main
case "Disconnect":
connected = false;
Instances.TaskQueueViewModel.AddLog(LocalizationHelper.GetString("ReconnectFailed"), UiLogColor.Error);
if (Instances.TaskQueueViewModel.Idle)
if (runningState.GetIdle())
{
break;
}
@@ -532,7 +536,7 @@ namespace MaaWpfGui.Main
Instances.TaskQueueViewModel.SetStopped();
if (isCoplitTaskChain)
{
Instances.CopilotViewModel.Idle = true;
runningState.SetIdle(true);
}
break;
@@ -544,7 +548,7 @@ namespace MaaWpfGui.Main
toast.Show();
if (isCoplitTaskChain)
{
Instances.CopilotViewModel.Idle = true;
runningState.SetIdle(true);
Instances.CopilotViewModel.AddLog(LocalizationHelper.GetString("CombatError"), UiLogColor.Error);
}
@@ -586,7 +590,7 @@ namespace MaaWpfGui.Main
Instances.TaskQueueViewModel.AddLog(LocalizationHelper.GetString("CompleteTask") + taskChain);
if (isCoplitTaskChain)
{
Instances.CopilotViewModel.Idle = true;
runningState.SetIdle(true);
Instances.CopilotViewModel.AddLog(LocalizationHelper.GetString("CompleteCombat"), UiLogColor.Info);
}
@@ -621,8 +625,7 @@ namespace MaaWpfGui.Main
_latestTaskId.Clear();
Instances.TaskQueueViewModel.ResetFightVariables();
Instances.TaskQueueViewModel.Idle = true;
Instances.CopilotViewModel.Idle = true;
runningState.SetIdle(true);
Instances.RecognizerViewModel.GachaDone = true;
if (isMainTaskQueueAllCompleted)
@@ -1193,7 +1196,7 @@ namespace MaaWpfGui.Main
{
foreach (var address in Instances.SettingsViewModel.DefaultAddress[Instances.SettingsViewModel.ConnectConfig])
{
if (Instances.SettingsViewModel.Idle)
if (runningState.GetIdle())
{
break;
}

View File

@@ -14,16 +14,20 @@
using System;
using System.Windows;
using MaaWpfGui.Helper;
using MaaWpfGui.States;
namespace MaaWpfGui.Services.HotKeys
{
public class MaaHotKeyActionHandler : IMaaHotKeyActionHandler
{
private readonly RunningState runningState;
/// <summary>
/// Initializes a new instance of the <see cref="MaaHotKeyActionHandler"/> class.
/// </summary>
public MaaHotKeyActionHandler()
{
runningState = RunningState.Instance;
}
/// <inheritdoc/>
@@ -53,7 +57,7 @@ namespace MaaWpfGui.Services.HotKeys
return;
}
if (Instances.TaskQueueViewModel.Idle)
if (runningState.GetIdle())
{
Instances.TaskQueueViewModel.LinkStart();

View File

@@ -0,0 +1,52 @@
using System;
namespace MaaWpfGui.States
{
public class RunningState
{
private static RunningState instance;
private RunningState()
{
}
public static RunningState Instance
{
get
{
instance ??= new RunningState();
return instance;
}
}
// values
private bool _idle = true;
public bool Idle
{
get => _idle;
set
{
if (_idle != value)
{
_idle = value;
OnIdleChanged(value);
}
}
}
// getters
public bool GetIdle() => Idle;
// action
public void SetIdle(bool idle) => Idle = idle;
// subscribes
public event EventHandler<bool> IdleChanged;
public virtual void OnIdleChanged(bool newIdleValue)
{
IdleChanged?.Invoke(this, newIdleValue);
}
}
}

View File

@@ -22,6 +22,7 @@ using System.Windows;
using System.Windows.Input;
using MaaWpfGui.Constants;
using MaaWpfGui.Helper;
using MaaWpfGui.States;
using Microsoft.Win32;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@@ -35,6 +36,8 @@ namespace MaaWpfGui.ViewModels.UI
/// </summary>
public class CopilotViewModel : Screen
{
private readonly RunningState runningState;
/// <summary>
/// Gets or sets the view models of log items.
/// </summary>
@@ -48,6 +51,13 @@ namespace MaaWpfGui.ViewModels.UI
DisplayName = LocalizationHelper.GetString("Copilot");
LogItemViewModels = new ObservableCollection<LogItemViewModel>();
AddLog(LocalizationHelper.GetString("CopilotTip"));
runningState = RunningState.Instance;
runningState.IdleChanged += RunningState_IdleChanged;
}
private void RunningState_IdleChanged(object sender, bool e)
{
Idle = e;
}
protected override void OnInitialActivate()
@@ -76,11 +86,7 @@ namespace MaaWpfGui.ViewModels.UI
public bool Idle
{
get => _idle;
set
{
_idle = value;
NotifyOfPropertyChange(() => Idle);
}
set => SetAndNotify(ref _idle, value);
}
private bool _startEnabled = true;
@@ -91,11 +97,7 @@ namespace MaaWpfGui.ViewModels.UI
public bool StartEnabled
{
get => _startEnabled;
set
{
_startEnabled = value;
NotifyOfPropertyChange(() => StartEnabled);
}
set => SetAndNotify(ref _startEnabled, value);
}
/// <summary>
@@ -454,7 +456,7 @@ namespace MaaWpfGui.ViewModels.UI
{
AddLog(Localization.GetString("AutoSquadTip"), LogColor.Message);
}*/
Idle = false;
runningState.SetIdle(false);
if (_isVideoTask)
{
@@ -489,7 +491,7 @@ namespace MaaWpfGui.ViewModels.UI
}
else
{
Idle = true;
runningState.SetIdle(true);
AddLog(LocalizationHelper.GetString("CopilotFileReadError"), UiLogColor.Error);
}
}
@@ -505,7 +507,7 @@ namespace MaaWpfGui.ViewModels.UI
public void Stop()
{
Instances.AsstProxy.AsstStop();
Idle = true;
runningState.SetIdle(true);
}
private bool _isVideoTask = false;

View File

@@ -36,6 +36,7 @@ using MaaWpfGui.Helper;
using MaaWpfGui.Main;
using MaaWpfGui.Models;
using MaaWpfGui.Services.HotKeys;
using MaaWpfGui.States;
using MaaWpfGui.Utilities;
using MaaWpfGui.Utilities.ValueType;
using Microsoft.Win32;
@@ -52,6 +53,8 @@ namespace MaaWpfGui.ViewModels.UI
/// </summary>
public class SettingsViewModel : Screen
{
private readonly RunningState runningState;
private static readonly ILogger _logger = Log.ForContext<SettingsViewModel>();
[DllImport("MaaCore.dll")]
@@ -121,6 +124,8 @@ namespace MaaWpfGui.ViewModels.UI
Application.Current.Shutdown();
Bootstrapper.RestartApplication();
}
runningState = RunningState.Instance;
}
public void Sober()
@@ -381,7 +386,7 @@ namespace MaaWpfGui.ViewModels.UI
{
SetAndNotify(ref _startEmulator, value);
ConfigurationHelper.SetValue(ConfigurationKeys.StartEmulator, value.ToString());
if (ClientType == string.Empty && Idle)
if (ClientType == string.Empty && runningState.GetIdle())
{
ClientType = "Official";
}
@@ -703,10 +708,10 @@ namespace MaaWpfGui.ViewModels.UI
}
// 储存按钮状态,以便后续重置
bool idle = Instances.TaskQueueViewModel.Idle;
bool idle = runningState.GetIdle();
// 让按钮变成停止按钮,可手动停止等待
Instances.TaskQueueViewModel.Idle = false;
runningState.SetIdle(false);
for (var i = 0; i < delay; ++i)
{
if (Instances.TaskQueueViewModel.Stopping)
@@ -730,7 +735,7 @@ namespace MaaWpfGui.ViewModels.UI
_logger.Information("The wait is over");
// 重置按钮状态,不影响后续判断
Instances.TaskQueueViewModel.Idle = idle;
runningState.SetIdle(idle);
}
/// <summary>

View File

@@ -29,6 +29,7 @@ using MaaWpfGui.Helper;
using MaaWpfGui.Main;
using MaaWpfGui.Models;
using MaaWpfGui.Services;
using MaaWpfGui.States;
using MaaWpfGui.Utilities.ValueType;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@@ -47,6 +48,7 @@ namespace MaaWpfGui.ViewModels.UI
{
private readonly IContainer _container;
private StageManager _stageManager;
private readonly RunningState runningState;
private static readonly ILogger _logger = Log.ForContext<TaskQueueViewModel>();
@@ -128,6 +130,13 @@ namespace MaaWpfGui.ViewModels.UI
public TaskQueueViewModel(IContainer container)
{
_container = container;
runningState = RunningState.Instance;
runningState.IdleChanged += RunningState_IdleChanged;
}
private void RunningState_IdleChanged(object sender, bool e)
{
Idle = e;
}
protected override void OnInitialActivate()
@@ -236,7 +245,7 @@ namespace MaaWpfGui.ViewModels.UI
refreshCustomInfrastPlanIndexByPeriod();
if (!Idle && !Instances.SettingsViewModel.ForceScheduledStart)
if (!runningState.GetIdle() && !Instances.SettingsViewModel.ForceScheduledStart)
{
return;
}
@@ -257,7 +266,7 @@ namespace MaaWpfGui.ViewModels.UI
{
if (Instances.SettingsViewModel.ForceScheduledStart)
{
if (!Idle)
if (!runningState.GetIdle())
{
await Stop();
}
@@ -689,12 +698,12 @@ namespace MaaWpfGui.ViewModels.UI
/// </summary>
public async void LinkStart()
{
if (Idle == false)
if (!runningState.GetIdle())
{
return;
}
Idle = false;
runningState.SetIdle(false);
// 虽然更改时已经保存过了,不过保险起见还是在点击开始之后再保存一次任务及基建列表
TaskItemSelectionChanged();
@@ -753,7 +762,7 @@ namespace MaaWpfGui.ViewModels.UI
if (!connected)
{
AddLog(errMsg, UiLogColor.Error);
Idle = true;
runningState.SetIdle(true);
SetStopped();
return;
}
@@ -827,7 +836,7 @@ namespace MaaWpfGui.ViewModels.UI
if (count == 0)
{
AddLog(LocalizationHelper.GetString("UnselectedTask"));
Idle = true;
runningState.SetIdle(true);
SetStopped();
return;
}
@@ -866,13 +875,13 @@ namespace MaaWpfGui.ViewModels.UI
public void SetStopped()
{
if (!Idle || Stopping)
if (!runningState.GetIdle() || Stopping)
{
AddLog(LocalizationHelper.GetString("Stopped"));
}
Stopping = false;
Idle = true;
runningState.SetIdle(true);
}
private bool appendStart()
@@ -1716,7 +1725,7 @@ namespace MaaWpfGui.ViewModels.UI
NotifyOfPropertyChange("Inited");
}
private bool _idle = false;
private bool _idle = true;
/// <summary>
/// Gets or sets a value indicating whether it is idle.
@@ -1727,7 +1736,6 @@ namespace MaaWpfGui.ViewModels.UI
set
{
SetAndNotify(ref _idle, value);
Instances.SettingsViewModel.Idle = value;
if (value)
{
FightTaskRunning = false;

View File

@@ -28,6 +28,7 @@ using System.Windows.Input;
using MaaWpfGui.Constants;
using MaaWpfGui.Helper;
using MaaWpfGui.Main;
using MaaWpfGui.States;
using Markdig;
using Markdig.Wpf;
using Newtonsoft.Json;
@@ -43,11 +44,14 @@ namespace MaaWpfGui.ViewModels.UI
/// </summary>
public class VersionUpdateViewModel : Screen
{
private readonly RunningState runningState;
/// <summary>
/// Initializes a new instance of the <see cref="VersionUpdateViewModel"/> class.
/// </summary>
public VersionUpdateViewModel()
{
runningState = RunningState.Instance;
}
[DllImport("MaaCore.dll")]
@@ -559,7 +563,7 @@ namespace MaaWpfGui.ViewModels.UI
{
if (Instances.SettingsViewModel.AutoInstallUpdatePackage)
{
while (!(Instances.TaskQueueViewModel.Idle && Instances.CopilotViewModel.Idle))
while (!runningState.GetIdle())
{
Thread.Sleep(60000);
}