BITKit/Src/Core/StateMachine/AsyncStateMachine.cs

218 lines
7.0 KiB
C#
Raw Normal View History

2024-11-08 12:52:09 +08:00
using System;
using System.Collections.Generic;
using System.Threading;
2025-03-09 13:38:23 +08:00
using System.Threading.Tasks;
2024-11-08 12:52:09 +08:00
using Cysharp.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
namespace BITKit.StateMachine
{
/// <summary>
/// 动态异步状态机
/// </summary>
/// <typeparam name="T">异步状态</typeparam>
2024-11-20 11:36:51 +08:00
public class AsyncStateMachine<T>:IStateMachine<T> ,IDisposable where T : class, IStateAsync
2024-11-08 12:52:09 +08:00
{
2024-11-08 17:28:07 +08:00
private readonly IServiceProvider _serviceProvider;
public AsyncStateMachine(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public AsyncStateMachine()
2024-11-08 12:52:09 +08:00
{
2024-11-08 17:28:07 +08:00
_serviceProvider = new ServiceCollection().BuildServiceProvider();
2024-11-08 12:52:09 +08:00
}
2024-11-08 21:06:30 +08:00
public bool Enabled { get; set; } = true;
2024-11-20 11:36:51 +08:00
public T CurrentState { get;private set; }
2024-11-08 17:28:07 +08:00
public event Action<T, T> OnStateChanging;
2025-02-24 23:02:43 +08:00
public event Func<T, T, UniTask> OnStateChangeAsync;
2024-11-08 12:52:09 +08:00
public event Action<T, T> OnStateChanged;
public event Action<T> OnStateRegistered;
2024-11-20 11:36:51 +08:00
public IReadOnlyDictionary<int, T> Dictionary => _dictionary;
2024-11-08 12:52:09 +08:00
public event Action<T> OnStateUnRegistered;
public IDictionary<Type, T> StateDictionary { get; } = new Dictionary<Type, T>();
2024-11-20 11:36:51 +08:00
public readonly ValidHandle IsBusy=new();
2024-11-08 12:52:09 +08:00
private readonly CancellationTokenSource _cancellationTokenSource=new();
2024-11-20 11:36:51 +08:00
private readonly Dictionary<int, T> _dictionary = new();
2025-03-09 13:38:23 +08:00
private readonly HashSet<int> _isRegistered = new();
private CancellationTokenSource _transitionCts;
private T _entryCompletedState;
2024-11-20 11:36:51 +08:00
public async void Initialize()
2024-11-08 12:52:09 +08:00
{
2024-11-20 11:36:51 +08:00
await IsBusy;
if(_cancellationTokenSource.IsCancellationRequested)return;
using var _ = IsBusy.GetHandle();
2024-11-08 17:28:07 +08:00
foreach (var (_,value) in StateDictionary)
2024-11-08 12:52:09 +08:00
{
2025-03-09 13:38:23 +08:00
if (_isRegistered.Add(value.Identifier))
{
await value.InitializeAsync();
value.Initialize();
}
2024-11-08 12:52:09 +08:00
}
}
2024-11-20 11:36:51 +08:00
2024-11-08 17:28:07 +08:00
public async void UpdateState(float deltaTime)
2024-11-08 12:52:09 +08:00
{
2025-03-09 13:38:23 +08:00
if (_entryCompletedState is null) return;
2024-11-20 11:36:51 +08:00
using var _ = IsBusy.GetHandle();
2025-03-09 13:38:23 +08:00
_entryCompletedState.OnStateUpdate(deltaTime);
await _entryCompletedState.OnStateUpdateAsync(deltaTime);
2024-11-08 12:52:09 +08:00
}
2025-03-09 13:38:23 +08:00
public void DisposeState()
2024-11-08 12:52:09 +08:00
{
2025-03-09 13:38:23 +08:00
TransitionState(null);
2024-11-08 12:52:09 +08:00
}
2024-11-08 17:28:07 +08:00
public T TransitionState<TState>() where TState : T
2024-11-08 12:52:09 +08:00
{
2024-11-08 17:28:07 +08:00
T nextState;
2024-11-20 11:36:51 +08:00
foreach (var (type, value) in Dictionary)
2024-11-08 17:28:07 +08:00
{
2024-11-20 11:36:51 +08:00
if ((typeof(TState) == value.GetType()) is false) continue;
2024-11-08 17:28:07 +08:00
nextState = value;
TransitionState(nextState);
return nextState;
}
nextState = _serviceProvider.GetRequiredService<TState>();
2024-11-20 11:36:51 +08:00
2024-11-08 21:06:30 +08:00
return TransitionState(nextState);;
2024-11-08 12:52:09 +08:00
}
2024-11-20 11:36:51 +08:00
public async UniTask TransitionStateAsync(T nextState)
2024-11-08 12:52:09 +08:00
{
2024-11-20 11:36:51 +08:00
if (nextState is not null)
{
if (nextState.Identifier == 0)
{
nextState.Identifier = nextState.GetHashCode();
}
}
2025-03-09 13:38:23 +08:00
2024-11-20 11:36:51 +08:00
if (Equals(nextState, CurrentState)) return;
2025-02-24 23:02:43 +08:00
2025-03-09 13:38:23 +08:00
var tempState = CurrentState;
2025-02-24 23:02:43 +08:00
2025-03-09 13:38:23 +08:00
CurrentState = nextState;
2025-02-24 23:02:43 +08:00
2025-03-09 13:38:23 +08:00
_transitionCts?.Cancel();
_transitionCts = new CancellationTokenSource();
var ct = _transitionCts.Token;
await IsBusy;
using var _ = IsBusy.GetHandle();
if(ct.IsCancellationRequested||_cancellationTokenSource.IsCancellationRequested)return;
OnStateChanging?.Invoke(tempState,nextState);
if (tempState is not null)
2024-11-20 11:36:51 +08:00
{
2025-03-09 13:38:23 +08:00
if (_entryCompletedState == tempState)
{
_entryCompletedState = null;
}
tempState.Enabled = false;
await tempState.OnStateExitAsync(tempState, nextState);
tempState.OnStateExit(tempState,nextState);
2024-11-20 11:36:51 +08:00
if(_cancellationTokenSource.IsCancellationRequested)return;
}
2025-03-09 13:38:23 +08:00
if(ct.IsCancellationRequested)return;
await OnStateChangeAsync.UniTaskFunc(CurrentState,nextState);
if(ct.IsCancellationRequested)return;
if (nextState is not null)
2024-11-08 17:28:07 +08:00
{
2025-03-09 13:38:23 +08:00
if (_isRegistered.Add(nextState.Identifier))
{
await RegisterAsync(nextState);
if(ct.IsCancellationRequested || _cancellationTokenSource.IsCancellationRequested)return;
}
nextState.Enabled = true;
await nextState.OnStateEntryAsync(CurrentState);
nextState.OnStateEntry(CurrentState);
if(ct.IsCancellationRequested || _cancellationTokenSource.IsCancellationRequested)return;
2024-11-08 17:28:07 +08:00
2025-03-09 13:38:23 +08:00
_entryCompletedState = nextState;
2025-02-24 23:02:43 +08:00
}
2025-03-09 13:38:23 +08:00
OnStateChanged?.Invoke(tempState, nextState);
2024-11-20 11:36:51 +08:00
}
public T TransitionState(T nextState)
{
TransitionStateAsync(nextState).Forget();
return nextState;
}
2025-03-09 13:38:23 +08:00
private async UniTask RegisterAsync(T newState)
{
StateDictionary.TryAdd(newState.GetType(),newState);
_dictionary.TryAdd(newState.Identifier, newState);
newState.Initialize();
await newState.InitializeAsync();
}
public async void Register(T newState)
{
await IsBusy;
using var _ = IsBusy.GetHandle();
await RegisterAsync(newState);
}
2024-11-20 11:36:51 +08:00
public async void UnRegister(T newState)
{
if (newState is null) return;
if (Dictionary.ContainsKey(newState.Identifier) is false) return;
_dictionary.Remove(newState.Identifier);
2025-03-09 13:38:23 +08:00
await IsBusy;
using var _ = IsBusy.GetHandle();
2024-11-20 11:36:51 +08:00
if (Equals(CurrentState, newState))
2024-11-08 17:28:07 +08:00
{
2024-11-20 11:36:51 +08:00
await CurrentState.OnStateExitAsync(CurrentState, null);
CurrentState.OnStateExit(CurrentState, null);
2025-03-09 13:38:23 +08:00
2024-11-20 11:36:51 +08:00
CurrentState = null;
2024-11-08 17:28:07 +08:00
}
2024-11-20 11:36:51 +08:00
2025-03-09 13:38:23 +08:00
if (newState is IAsyncDisposable asyncDisposable)
{
await asyncDisposable.DisposeAsync();
}
if (newState is IDisposable disposable)
{
disposable.Dispose();
}
2024-11-20 11:36:51 +08:00
OnStateUnRegistered?.Invoke(newState);
2024-11-08 12:52:09 +08:00
}
public void Dispose()
{
2024-11-20 11:36:51 +08:00
if(_isDisposed)return;
2024-11-08 12:52:09 +08:00
_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();
2024-11-20 11:36:51 +08:00
_isDisposed = true;
2024-11-08 12:52:09 +08:00
}
2024-11-20 11:36:51 +08:00
private bool _isDisposed;
2024-11-08 12:52:09 +08:00
}
}