This commit is contained in:
CortexCore
2025-03-09 13:38:23 +08:00
parent 8261a458e2
commit 18239a5ae4
67 changed files with 8573 additions and 831 deletions

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
@@ -24,7 +25,6 @@ namespace BITKit.StateMachine
public bool Enabled { get; set; } = true;
public T CurrentState { get;private set; }
public T NextOrCurrentState => _nextTargetState.IfNotAllow(CurrentState);
public event Action<T, T> OnStateChanging;
public event Func<T, T, UniTask> OnStateChangeAsync;
public event Action<T, T> OnStateChanged;
@@ -35,7 +35,13 @@ namespace BITKit.StateMachine
public readonly ValidHandle IsBusy=new();
private readonly CancellationTokenSource _cancellationTokenSource=new();
private readonly Dictionary<int, T> _dictionary = new();
private readonly Optional<T> _nextTargetState = new();
private readonly HashSet<int> _isRegistered = new();
private CancellationTokenSource _transitionCts;
private T _entryCompletedState;
public async void Initialize()
{
await IsBusy;
@@ -43,33 +49,27 @@ namespace BITKit.StateMachine
using var _ = IsBusy.GetHandle();
foreach (var (_,value) in StateDictionary)
{
await value.InitializeAsync();
value.Initialize();
if (_isRegistered.Add(value.Identifier))
{
await value.InitializeAsync();
value.Initialize();
}
}
}
public async void UpdateState(float deltaTime)
{
if (CurrentState is null) return;
if (_entryCompletedState is null) return;
using var _ = IsBusy.GetHandle();
CurrentState.OnStateUpdate(deltaTime);
await CurrentState.OnStateUpdateAsync(deltaTime);
_entryCompletedState.OnStateUpdate(deltaTime);
await _entryCompletedState.OnStateUpdateAsync(deltaTime);
}
public async void DisposeState()
public void DisposeState()
{
await IsBusy;
if (_cancellationTokenSource.IsCancellationRequested) return;
if (CurrentState is null) return;
using var _ = IsBusy.GetHandle();
CurrentState.Enabled = false;
await CurrentState.OnStateExitAsync(CurrentState, null);
CurrentState.OnStateExit(CurrentState, null);
CurrentState = null;
TransitionState(null);
}
public T TransitionState<TState>() where TState : T
{
T nextState;
@@ -96,50 +96,60 @@ namespace BITKit.StateMachine
nextState.Identifier = nextState.GetHashCode();
}
}
if (Equals(nextState, CurrentState)) return;
if(_nextTargetState.Allow && Equals(_nextTargetState.Value,nextState))return;
if (_nextTargetState.Allow)
{
_nextTargetState.Value = nextState;
return;
}
_nextTargetState.SetValueThenAllow(nextState);
await IsBusy;
if(CurrentState==nextState)return;
if(_cancellationTokenSource.IsCancellationRequested)return;
using var _ = IsBusy.GetHandle();
OnStateChanging?.Invoke(CurrentState,nextState);
await OnStateChangeAsync.UniTaskFunc(CurrentState,nextState);
if (nextState is not null && _dictionary.TryAdd(nextState.Identifier, nextState))
{
await nextState.InitializeAsync();
if(_cancellationTokenSource.IsCancellationRequested)return;
nextState.Initialize();
}
if (CurrentState is not null)
{
CurrentState.Enabled = false;
await CurrentState.OnStateExitAsync(CurrentState, nextState);
if(_cancellationTokenSource.IsCancellationRequested)return;
CurrentState.OnStateExit(CurrentState,nextState);
}
if (_nextTargetState.Allow && _nextTargetState.Value != nextState)
{
return;
}
var tempState = CurrentState;
CurrentState = _nextTargetState.Value;
_nextTargetState.Clear();
nextState.Enabled = true;
await nextState.OnStateEntryAsync(tempState);
if(_cancellationTokenSource.IsCancellationRequested)return;
nextState.OnStateEntry(tempState);
OnStateChanged?.Invoke(tempState,nextState);
CurrentState = nextState;
_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)
{
if (_entryCompletedState == tempState)
{
_entryCompletedState = null;
}
tempState.Enabled = false;
await tempState.OnStateExitAsync(tempState, nextState);
tempState.OnStateExit(tempState,nextState);
if(_cancellationTokenSource.IsCancellationRequested)return;
}
if(ct.IsCancellationRequested)return;
await OnStateChangeAsync.UniTaskFunc(CurrentState,nextState);
if(ct.IsCancellationRequested)return;
if (nextState is not null)
{
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;
_entryCompletedState = nextState;
}
OnStateChanged?.Invoke(tempState, nextState);
}
public T TransitionState(T nextState)
@@ -148,27 +158,49 @@ namespace BITKit.StateMachine
return nextState;
}
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);
}
public async void UnRegister(T newState)
{
if (newState is null) return;
if (Dictionary.ContainsKey(newState.Identifier) is false) return;
_dictionary.Remove(newState.Identifier);
await IsBusy;
using var _ = IsBusy.GetHandle();
if (Equals(CurrentState, newState))
{
await CurrentState.OnStateExitAsync(CurrentState, null);
CurrentState.OnStateExit(CurrentState, null);
if (CurrentState is IAsyncDisposable asyncDisposable)
{
await asyncDisposable.DisposeAsync();
}
if (CurrentState is IDisposable disposable)
{
disposable.Dispose();
}
CurrentState = null;
}
if (newState is IAsyncDisposable asyncDisposable)
{
await asyncDisposable.DisposeAsync();
}
if (newState is IDisposable disposable)
{
disposable.Dispose();
}
OnStateUnRegistered?.Invoke(newState);
}
public void Dispose()