using GFramework.Core.Abstractions.architecture; using GFramework.Core.Abstractions.enums; using GFramework.Core.Abstractions.lifecycle; using GFramework.Core.Abstractions.rule; using GFramework.Core.Abstractions.state; using GFramework.Core.extensions; namespace GFramework.Core.state; /// /// 上下文感知状态机,继承自StateMachine并实现ISystem接口 /// 该状态机能够感知架构上下文,并在状态切换时发送状态变更事件 /// public class StateMachineSystem : StateMachine, IStateMachineSystem { /// /// 架构上下文对象,用于提供系统运行所需的上下文信息 /// private IArchitectureContext _context = null!; /// /// 设置架构上下文的方法 /// /// 要设置的架构上下文对象 public void SetContext(IArchitectureContext context) { _context = context; } /// /// 获取当前架构上下文的方法 /// /// 当前的架构上下文对象 public IArchitectureContext GetContext() { return _context; } /// /// 处理架构生命周期阶段的方法 /// /// 当前所处的架构生命周期阶段 public virtual void OnArchitecturePhase(ArchitecturePhase phase) { } /// /// 初始化方法,在系统启动时调用 /// 遍历所有状态实例,为实现了IContextAware接口的状态设置上下文 /// public virtual void Initialize() { foreach (var state in States.Values.OfType()) state.SetContext(_context); } /// /// 销毁方法,在系统关闭时调用(同步方法,保留用于向后兼容) /// [Obsolete("建议使用 DestroyAsync() 以支持异步清理")] public virtual void Destroy() { DestroyAsync().AsTask().GetAwaiter().GetResult(); } /// /// 异步销毁方法,在系统关闭时调用 /// public virtual async ValueTask DestroyAsync() { // 退出当前状态 if (Current != null) { if (Current is IAsyncState asyncState) { await asyncState.OnExitAsync(null); // ✅ 正确等待异步清理 } else { Current.OnExit(null); } Current = null; } // 清理所有状态 foreach (var state in States.Values) { if (state is IAsyncDestroyable asyncDestroyable) { await asyncDestroyable.DestroyAsync(); } else if (state is IDestroyable destroyable) { destroyable.Destroy(); } } States.Clear(); } /// /// 异步内部状态切换方法,重写基类方法以添加状态变更事件通知功能 /// /// 要切换到的下一个状态 protected override async Task ChangeInternalAsync(IState next) { var old = Current; await base.ChangeInternalAsync(next); // 发送状态变更事件,通知监听者状态已发生改变 this.SendEvent(new StateChangedEvent { OldState = old, NewState = Current }); } }