mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
feat(architecture): 引入架构阶段管理机制
新增 `ArchitecturePhase` 枚举及生命周期接口,支持在不同阶段执行相应逻辑。 实现基于阶段的状态转换控制与校验,增强架构初始化流程的可控性与扩展性。 添加模块安装接口 `IArchitectureModule` 及相关感知接口,便于插件化集成。 完善系统与模型注册限制,禁止在就绪后注册组件,提升运行时稳定性。 移除旧版补丁注册机制,统一通过生命周期钩子进行扩展。
This commit is contained in:
parent
564a7e3f24
commit
d1cd0cfb05
@ -15,6 +15,41 @@ namespace GFramework.Core.architecture;
|
||||
/// <typeparam name="T">派生类类型,用于实现单例</typeparam>
|
||||
public abstract class Architecture<T> : IArchitecture where T : Architecture<T>, new()
|
||||
{
|
||||
public ArchitecturePhase CurrentPhase { get; private set; }
|
||||
|
||||
private void EnterPhase(ArchitecturePhase next)
|
||||
{
|
||||
if (!ArchitectureConstants.PhaseTransitions.TryGetValue(CurrentPhase, out var allowed) ||
|
||||
!allowed.Contains(next))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Invalid phase transition: {CurrentPhase} -> {next}");
|
||||
}
|
||||
|
||||
CurrentPhase = next;
|
||||
NotifyPhase(next);
|
||||
foreach (var obj in _mContainer.GetAll<IArchitecturePhaseAware>())
|
||||
{
|
||||
obj.OnArchitecturePhase(next);
|
||||
}
|
||||
}
|
||||
|
||||
private readonly List<IArchitectureLifecycle> _lifecycleHooks = [];
|
||||
|
||||
public void RegisterLifecycleHook(IArchitectureLifecycle hook)
|
||||
{
|
||||
if (CurrentPhase >= ArchitecturePhase.Ready)
|
||||
throw new InvalidOperationException(
|
||||
"Cannot register lifecycle hook after architecture is Ready");
|
||||
_lifecycleHooks.Add(hook);
|
||||
}
|
||||
|
||||
private void NotifyPhase(ArchitecturePhase phase)
|
||||
{
|
||||
foreach (var hook in _lifecycleHooks)
|
||||
hook.OnPhase(phase, this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 静态只读字段,用于延迟初始化架构实例
|
||||
/// 使用Lazy确保线程安全的单例模式实现
|
||||
@ -31,23 +66,34 @@ public abstract class Architecture<T> : IArchitecture where T : Architecture<T>,
|
||||
private static readonly Lazy<T> MArchitectureLazy = new(() =>
|
||||
{
|
||||
var arch = new T();
|
||||
// == Architecture Init ==
|
||||
arch.EnterPhase(ArchitecturePhase.Created);
|
||||
arch.EnterPhase(ArchitecturePhase.BeforeInit);
|
||||
// 调用用户实现的初始化
|
||||
arch.Init();
|
||||
arch.EnterPhase(ArchitecturePhase.AfterInit);
|
||||
|
||||
// 执行注册的补丁逻辑
|
||||
OnRegisterPatch(arch);
|
||||
|
||||
// == Model Init ==
|
||||
arch.EnterPhase(ArchitecturePhase.BeforeModelInit);
|
||||
// 初始化所有已注册但尚未初始化的模型
|
||||
foreach (var model in arch._mModels) model.Init();
|
||||
|
||||
arch._mModels.Clear();
|
||||
arch.EnterPhase(ArchitecturePhase.AfterModelInit);
|
||||
|
||||
|
||||
// == System Init ==
|
||||
arch.EnterPhase(ArchitecturePhase.BeforeSystemInit);
|
||||
// 初始化所有已注册但尚未初始化的系统
|
||||
foreach (var system in arch._mSystems) system.Init();
|
||||
|
||||
arch._mSystems.Clear();
|
||||
arch.EnterPhase(ArchitecturePhase.AfterSystemInit);
|
||||
|
||||
|
||||
// == Finalize ==
|
||||
// 冻结IOC容器,不允许 anymore
|
||||
arch._mContainer.Freeze();
|
||||
arch.EnterPhase(ArchitecturePhase.Ready);
|
||||
// 发送架构初始化完成事件
|
||||
arch.SendEvent(new ArchitectureEvents.ArchitectureInitializedEvent());
|
||||
arch._mInited = true;
|
||||
@ -68,7 +114,7 @@ public abstract class Architecture<T> : IArchitecture where T : Architecture<T>,
|
||||
/// 存储尚未初始化的系统集合,在初始化阶段统一调用Init方法
|
||||
/// </summary>
|
||||
private readonly HashSet<ISystem> _mSystems = [];
|
||||
|
||||
|
||||
private readonly HashSet<ISystem> _allSystems = [];
|
||||
|
||||
/// <summary>
|
||||
@ -81,12 +127,6 @@ public abstract class Architecture<T> : IArchitecture where T : Architecture<T>,
|
||||
/// </summary>
|
||||
private bool _mInited;
|
||||
|
||||
/// <summary>
|
||||
/// 注册补丁委托,允许在架构创建后执行额外逻辑
|
||||
/// </summary>
|
||||
public static Action<T> OnRegisterPatch { get; set; } = _ => { };
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取架构实例的静态属性
|
||||
/// </summary>
|
||||
@ -102,6 +142,9 @@ public abstract class Architecture<T> : IArchitecture where T : Architecture<T>,
|
||||
/// <param name="system">要注册的系统实例</param>
|
||||
public void RegisterSystem<TSystem>(TSystem system) where TSystem : ISystem
|
||||
{
|
||||
if (CurrentPhase >= ArchitecturePhase.Ready)
|
||||
throw new InvalidOperationException(
|
||||
$"Cannot register system after Architecture is Ready");
|
||||
system.SetArchitecture(this);
|
||||
_mContainer.RegisterPlurality(system);
|
||||
_allSystems.Add(system);
|
||||
@ -119,6 +162,9 @@ public abstract class Architecture<T> : IArchitecture where T : Architecture<T>,
|
||||
/// <param name="model">要注册的模型实例</param>
|
||||
public void RegisterModel<TModel>(TModel model) where TModel : IModel
|
||||
{
|
||||
if (CurrentPhase >= ArchitecturePhase.Ready)
|
||||
throw new InvalidOperationException(
|
||||
$"Cannot register system after Architecture is Ready");
|
||||
model.SetArchitecture(this);
|
||||
_mContainer.RegisterPlurality(model);
|
||||
|
||||
@ -246,12 +292,17 @@ public abstract class Architecture<T> : IArchitecture where T : Architecture<T>,
|
||||
/// </summary>
|
||||
public void Destroy()
|
||||
{
|
||||
// 销毁所有已注册的系统
|
||||
if (CurrentPhase >= ArchitecturePhase.Destroying)
|
||||
return;
|
||||
|
||||
EnterPhase(ArchitecturePhase.Destroying);
|
||||
|
||||
foreach (var system in _allSystems)
|
||||
{
|
||||
system.Destroy();
|
||||
}
|
||||
|
||||
_allSystems.Clear();
|
||||
|
||||
EnterPhase(ArchitecturePhase.Destroyed);
|
||||
}
|
||||
|
||||
|
||||
@ -293,4 +344,11 @@ public abstract class Architecture<T> : IArchitecture where T : Architecture<T>,
|
||||
query.SetArchitecture(this);
|
||||
return query.Do();
|
||||
}
|
||||
|
||||
public void InstallModule(IArchitectureModule module)
|
||||
{
|
||||
RegisterLifecycleHook(module);
|
||||
_mContainer.RegisterPlurality(module);
|
||||
module.Install(this);
|
||||
}
|
||||
}
|
||||
21
GFramework.Core/architecture/ArchitectureConstants.cs
Normal file
21
GFramework.Core/architecture/ArchitectureConstants.cs
Normal file
@ -0,0 +1,21 @@
|
||||
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace GFramework.Core.architecture;
|
||||
|
||||
public static class ArchitectureConstants
|
||||
{
|
||||
public static readonly ImmutableDictionary<ArchitecturePhase, ArchitecturePhase[]> PhaseTransitions =
|
||||
new Dictionary<ArchitecturePhase, ArchitecturePhase[]>
|
||||
{
|
||||
{ ArchitecturePhase.Created, [ArchitecturePhase.BeforeInit] },
|
||||
{ ArchitecturePhase.BeforeInit, [ArchitecturePhase.AfterInit] },
|
||||
{ ArchitecturePhase.AfterInit, [ArchitecturePhase.BeforeModelInit] },
|
||||
{ ArchitecturePhase.BeforeModelInit, [ArchitecturePhase.AfterModelInit] },
|
||||
{ ArchitecturePhase.AfterModelInit, [ArchitecturePhase.BeforeSystemInit] },
|
||||
{ ArchitecturePhase.BeforeSystemInit, [ArchitecturePhase.AfterSystemInit] },
|
||||
{ ArchitecturePhase.AfterSystemInit, [ArchitecturePhase.Ready] },
|
||||
{ ArchitecturePhase.Ready, [ArchitecturePhase.Destroying] },
|
||||
{ ArchitecturePhase.Destroying, [ArchitecturePhase.Destroyed] }
|
||||
}.ToImmutableDictionary();
|
||||
}
|
||||
61
GFramework.Core/architecture/ArchitecturePhase.cs
Normal file
61
GFramework.Core/architecture/ArchitecturePhase.cs
Normal file
@ -0,0 +1,61 @@
|
||||
namespace GFramework.Core.architecture;
|
||||
|
||||
/// <summary>
|
||||
/// 架构阶段枚举,定义了系统架构初始化和运行过程中的各个关键阶段
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 该枚举用于标记和控制系统架构组件的生命周期,确保在正确的时机执行相应的初始化和处理逻辑。
|
||||
/// 各个阶段按照时间顺序排列,从创建到准备就绪的完整流程。
|
||||
/// </remarks>
|
||||
public enum ArchitecturePhase
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 对象创建阶段,对应 new T() 操作完成后的状态
|
||||
/// </summary>
|
||||
Created,
|
||||
|
||||
/// <summary>
|
||||
/// 初始化之前阶段,在 Init() 方法调用之前的状态
|
||||
/// </summary>
|
||||
BeforeInit,
|
||||
|
||||
/// <summary>
|
||||
/// 初始化之后阶段,在 Init() 方法调用之后的状态
|
||||
/// </summary>
|
||||
AfterInit,
|
||||
|
||||
/// <summary>
|
||||
/// 模型初始化之前阶段
|
||||
/// </summary>
|
||||
BeforeModelInit,
|
||||
|
||||
/// <summary>
|
||||
/// 模型初始化之后阶段
|
||||
/// </summary>
|
||||
AfterModelInit,
|
||||
|
||||
/// <summary>
|
||||
/// 系统初始化之前阶段
|
||||
/// </summary>
|
||||
BeforeSystemInit,
|
||||
|
||||
/// <summary>
|
||||
/// 系统初始化之后阶段
|
||||
/// </summary>
|
||||
AfterSystemInit,
|
||||
|
||||
/// <summary>
|
||||
/// 就绪阶段,完成冻结和事件处理后的最终状态
|
||||
/// </summary>
|
||||
Ready,
|
||||
/// <summary>
|
||||
/// 正在销毁中 暂时不使用
|
||||
/// </summary>
|
||||
Destroying,
|
||||
/// <summary>
|
||||
/// 已销毁 暂时不使用
|
||||
/// </summary>
|
||||
Destroyed
|
||||
|
||||
}
|
||||
15
GFramework.Core/architecture/IArchitectureLifecycle.cs
Normal file
15
GFramework.Core/architecture/IArchitectureLifecycle.cs
Normal file
@ -0,0 +1,15 @@
|
||||
namespace GFramework.Core.architecture;
|
||||
|
||||
/// <summary>
|
||||
/// 架构生命周期接口,定义了架构在不同阶段的回调方法
|
||||
/// </summary>
|
||||
public interface IArchitectureLifecycle
|
||||
{
|
||||
/// <summary>
|
||||
/// 当架构进入指定阶段时触发的回调方法
|
||||
/// </summary>
|
||||
/// <param name="phase">当前的架构阶段</param>
|
||||
/// <param name="arch">相关的架构实例</param>
|
||||
void OnPhase(ArchitecturePhase phase, IArchitecture arch);
|
||||
}
|
||||
|
||||
15
GFramework.Core/architecture/IArchitectureModule.cs
Normal file
15
GFramework.Core/architecture/IArchitectureModule.cs
Normal file
@ -0,0 +1,15 @@
|
||||
|
||||
namespace GFramework.Core.architecture;
|
||||
|
||||
/// <summary>
|
||||
/// 架构模块接口,继承自架构生命周期接口。
|
||||
/// 定义了模块安装到架构中的标准方法。
|
||||
/// </summary>
|
||||
public interface IArchitectureModule : IArchitectureLifecycle, IArchitecturePhaseAware
|
||||
{
|
||||
/// <summary>
|
||||
/// 将当前模块安装到指定的架构中。
|
||||
/// </summary>
|
||||
/// <param name="architecture">要安装模块的目标架构实例。</param>
|
||||
void Install(IArchitecture architecture);
|
||||
}
|
||||
14
GFramework.Core/architecture/IArchitecturePhaseAware.cs
Normal file
14
GFramework.Core/architecture/IArchitecturePhaseAware.cs
Normal file
@ -0,0 +1,14 @@
|
||||
namespace GFramework.Core.architecture;
|
||||
|
||||
/// <summary>
|
||||
/// 定义架构阶段感知接口,用于在架构的不同阶段执行相应的逻辑
|
||||
/// </summary>
|
||||
public interface IArchitecturePhaseAware
|
||||
{
|
||||
/// <summary>
|
||||
/// 当架构进入指定阶段时触发的回调方法
|
||||
/// </summary>
|
||||
/// <param name="phase">架构阶段枚举值,表示当前所处的架构阶段</param>
|
||||
void OnArchitecturePhase(ArchitecturePhase phase);
|
||||
}
|
||||
|
||||
@ -229,7 +229,6 @@ public class IocContainer
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定类型的所有实例(接口 / 抽象类推荐使用)
|
||||
/// </summary>
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
using System;
|
||||
|
||||
namespace GFramework.Generator.Attributes
|
||||
namespace GFramework.Generator.Attributes.generator.enums
|
||||
{
|
||||
/// <summary>
|
||||
/// 标注在 enum 上,Source Generator 会为该 enum 生成扩展方法。
|
||||
|
||||
@ -9,7 +9,7 @@ namespace GFramework.Godot.architecture;
|
||||
/// <typeparam name="T">架构的具体类型,必须继承自Architecture且能被实例化</typeparam>
|
||||
public abstract class AbstractArchitecture<T> : Architecture<T> where T : Architecture<T>, new()
|
||||
{
|
||||
private const string ArchitectureName = "__GFrameworkArchitectureAnchor";
|
||||
private const string ArchitectureName = "__GFramework__Architecture__Anchor";
|
||||
/// <summary>
|
||||
/// 初始化架构,按顺序注册模型、系统和工具
|
||||
/// </summary>
|
||||
|
||||
@ -18,14 +18,16 @@ public partial class ArchitectureAnchorNode : Node
|
||||
{
|
||||
_onExit = onExit;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 当节点从场景树中移除时调用此方法
|
||||
/// 执行绑定的退出回调并清理引用
|
||||
/// </summary>
|
||||
public override void _ExitTree()
|
||||
{
|
||||
// 执行退出回调
|
||||
_onExit?.Invoke();
|
||||
// 清理引用
|
||||
_onExit = null;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user