feat(architecture): 添加架构配置选项支持

新增 `IArchitectureOptions` 接口及其实现类 `FunctionalArchitectureOptions`,
用于控制架构行为,如阶段验证严格性和延迟注册许可。
同时为 `Architecture<T>` 类添加虚属性 `Options`,默认返回功能型选项实例。
修改了阶段转换验证、系统/模型注册时机检查逻辑,以支持通过选项动态控制其行为。
调整部分代码格式以提升可读性。
This commit is contained in:
GwWuYou 2025-12-21 14:35:55 +08:00
parent 259c4411c7
commit bb403bd454
5 changed files with 88 additions and 12 deletions

View File

@ -13,8 +13,22 @@ namespace GFramework.Core.architecture;
/// 使用单例模式确保全局唯一实例,并支持命令、查询和事件机制。
/// </summary>
/// <typeparam name="T">派生类类型,用于实现单例</typeparam>
public abstract class Architecture<T> : IArchitecture where T : Architecture<T>, new()
public abstract class Architecture<T> : IArchitecture
where T : Architecture<T>, new()
{
/// <summary>
/// 获取架构选项的虚拟属性
/// </summary>
/// <returns>返回IArchitectureOptions接口实例包含架构配置选项</returns>
/// <remarks>
/// 默认实现返回FunctionalArchitectureOptions实例其中包含两个委托
/// 第一个委托始终返回true第二个委托始终返回false
/// </remarks>
protected virtual IArchitectureOptions Options { get; } = new FunctionalArchitectureOptions(
() => true,
() => false
);
#region Fields and Properties
/// <summary>
@ -57,7 +71,7 @@ public abstract class Architecture<T> : IArchitecture where T : Architecture<T>,
/// 生命周期感知对象列表
/// </summary>
private readonly List<IArchitectureLifecycle> _lifecycleHooks = [];
/// <summary>
/// 当前架构的阶段
/// </summary>
@ -124,17 +138,18 @@ public abstract class Architecture<T> : IArchitecture where T : Architecture<T>,
/// <exception cref="InvalidOperationException">当阶段转换不被允许时抛出异常</exception>
private void EnterPhase(ArchitecturePhase next)
{
// 验证阶段转换是否合法
if (!ArchitectureConstants.PhaseTransitions.TryGetValue(CurrentPhase, out var allowed) ||
!allowed.Contains(next))
if (Options.StrictPhaseValidation &&
(!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>())
{
@ -158,7 +173,7 @@ public abstract class Architecture<T> : IArchitecture where T : Architecture<T>,
/// <param name="hook">生命周期钩子实例</param>
public void RegisterLifecycleHook(IArchitectureLifecycle hook)
{
if (CurrentPhase >= ArchitecturePhase.Ready)
if (CurrentPhase >= ArchitecturePhase.Ready && !Options.AllowLateRegistration)
throw new InvalidOperationException(
"Cannot register lifecycle hook after architecture is Ready");
_lifecycleHooks.Add(hook);
@ -186,7 +201,7 @@ public abstract class Architecture<T> : IArchitecture where T : Architecture<T>,
// 进入销毁阶段并发送销毁开始事件
EnterPhase(ArchitecturePhase.Destroying);
SendEvent(new ArchitectureEvents.ArchitectureDestroyingEvent());
// 销毁所有系统组件并清空系统列表
foreach (var system in _allSystems)
system.Destroy();
@ -225,9 +240,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)
if (CurrentPhase >= ArchitecturePhase.Ready && !Options.AllowLateRegistration)
throw new InvalidOperationException(
$"Cannot register system after Architecture is Ready");
"Cannot register system after Architecture is Ready");
system.SetArchitecture(this);
_mContainer.RegisterPlurality(system);
_allSystems.Add(system);
@ -245,9 +260,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)
if (CurrentPhase >= ArchitecturePhase.Ready && !Options.AllowLateRegistration)
throw new InvalidOperationException(
$"Cannot register system after Architecture is Ready");
"Cannot register system after Architecture is Ready");
model.SetArchitecture(this);
_mContainer.RegisterPlurality(model);

View File

@ -0,0 +1,17 @@

namespace GFramework.Core.architecture;
public class ArchitectureOptionsDelegates
{
/// <summary>
/// 架构可配置选项委托
/// </summary>
/// <returns>是否严格验证阶段转换</returns>
public delegate bool StrictPhaseValidationDelegate();
/// <summary>
/// 架构可配置选项委托
/// </summary>
/// <returns>是否允许在 Ready 阶段后注册系统/模型</returns>
public delegate bool AllowLateRegistrationDelegate();
}

View File

@ -0,0 +1,26 @@
namespace GFramework.Core.architecture;
/// <summary>
/// 函数式架构选项实现,支持匿名实现
/// </summary>
public class FunctionalArchitectureOptions(Func<bool> strictPhaseValidation, Func<bool> allowLateRegistration)
: IArchitectureOptions
{
/// <summary>
/// 初始化 FunctionalArchitectureOptions 类的新实例
/// </summary>
/// <param name="strictPhaseValidation">用于确定是否启用严格阶段验证的函数</param>
/// <param name="allowLateRegistration">用于确定是否允许延迟注册的函数</param>
private readonly Func<bool> _strictPhaseValidation = strictPhaseValidation ?? throw new ArgumentNullException(nameof(strictPhaseValidation));
private readonly Func<bool> _allowLateRegistration = allowLateRegistration ?? throw new ArgumentNullException(nameof(allowLateRegistration));
/// <summary>
/// 获取一个值,该值指示是否启用严格阶段验证
/// </summary>
public bool StrictPhaseValidation => _strictPhaseValidation();
/// <summary>
/// 获取一个值,该值指示是否允许延迟注册
/// </summary>
public bool AllowLateRegistration => _allowLateRegistration();
}

View File

@ -0,0 +1,17 @@
namespace GFramework.Core.architecture;
/// <summary>
/// 架构可配置选项接口
/// </summary>
public interface IArchitectureOptions
{
/// <summary>
/// 是否严格验证阶段转换
/// </summary>
bool StrictPhaseValidation { get; }
/// <summary>
/// 是否允许在 Ready 阶段后注册系统/模型
/// </summary>
bool AllowLateRegistration { get; }
}

View File

@ -11,6 +11,7 @@ namespace GFramework.Godot.architecture;
/// <typeparam name="T">架构的具体类型必须继承自Architecture且能被实例化。</typeparam>
public abstract class AbstractArchitecture<T> : Architecture<T> where T : Architecture<T>, new()
{
/// <summary>
/// 架构锚点节点的唯一标识名称
/// 用于在Godot场景树中创建和查找架构锚点节点