using GFramework.Core.Abstractions.Architectures; using GFramework.Core.Abstractions.Enums; using GFramework.Core.Abstractions.Environment; using GFramework.Core.Abstractions.Logging; using GFramework.Core.Abstractions.Model; using GFramework.Core.Abstractions.Systems; using GFramework.Core.Abstractions.Utility; using GFramework.Core.Environment; using GFramework.Core.Logging; using Microsoft.Extensions.DependencyInjection; namespace GFramework.Core.Architectures; /// /// 架构基类,提供系统、模型、工具等组件的注册与管理功能。 /// 专注于生命周期管理、初始化流程控制和架构阶段转换。 /// /// 重构说明:此类已重构为协调器模式,将职责委托给专门的管理器: /// - ArchitectureLifecycle: 生命周期管理 /// - ArchitectureComponentRegistry: 组件注册管理 /// - ArchitectureModules: 模块管理 /// public abstract class Architecture : IArchitecture { #region Constructor /// /// 构造函数,初始化架构和管理器 /// /// 架构配置 /// 环境配置 /// 服务管理器 /// 架构上下文 protected Architecture( IArchitectureConfiguration? configuration = null, IEnvironment? environment = null, IArchitectureServices? services = null, IArchitectureContext? context = null) { Configuration = configuration ?? new ArchitectureConfiguration(); Environment = environment ?? new DefaultEnvironment(); Services = services ?? new ArchitectureServices(); _context = context; // 初始化 Logger LoggerFactoryResolver.Provider = Configuration.LoggerProperties.LoggerFactoryProvider; _logger = LoggerFactoryResolver.Provider.CreateLogger(GetType().Name); // 初始化管理器 _lifecycle = new ArchitectureLifecycle(this, Configuration, Services, _logger); _componentRegistry = new ArchitectureComponentRegistry(this, Configuration, Services, _lifecycle, _logger); _modules = new ArchitectureModules(this, Services, _logger); } #endregion #region Lifecycle Hook Management /// /// 注册生命周期钩子 /// /// 生命周期钩子实例 /// 注册的钩子实例 public IArchitectureLifecycleHook RegisterLifecycleHook(IArchitectureLifecycleHook hook) { return _lifecycle.RegisterLifecycleHook(hook); } #endregion #region Properties /// /// 获取架构配置对象 /// private IArchitectureConfiguration Configuration { get; } /// /// 获取环境配置对象 /// private IEnvironment Environment { get; } /// /// 获取服务管理器 /// private IArchitectureServices Services { get; } /// /// 当前架构的阶段 /// public ArchitecturePhase CurrentPhase => _lifecycle.CurrentPhase; /// /// 架构上下文 /// public IArchitectureContext Context => _context!; /// /// 获取一个布尔值,指示当前架构是否处于就绪状态 /// public bool IsReady => _lifecycle.IsReady; /// /// 获取用于配置服务集合的委托 /// 默认实现返回null,子类可以重写此属性以提供自定义配置逻辑 /// public virtual Action? Configurator => null; /// /// 阶段变更事件(用于测试和扩展) /// public event Action? PhaseChanged { add => _lifecycle.PhaseChanged += value; remove => _lifecycle.PhaseChanged -= value; } #endregion #region Fields /// /// 日志记录器实例 /// private readonly ILogger _logger; /// /// 架构上下文实例 /// private IArchitectureContext? _context; /// /// 生命周期管理器 /// private readonly ArchitectureLifecycle _lifecycle; /// /// 组件注册管理器 /// private readonly ArchitectureComponentRegistry _componentRegistry; /// /// 模块管理器 /// private readonly ArchitectureModules _modules; #endregion #region Module Management /// /// 注册中介行为管道 /// 用于配置Mediator框架的行为拦截和处理逻辑 /// /// 行为类型,必须是引用类型 public void RegisterMediatorBehavior() where TBehavior : class { _modules.RegisterMediatorBehavior(); } /// /// 安装架构模块 /// /// 要安装的模块 /// 安装的模块实例 public IArchitectureModule InstallModule(IArchitectureModule module) { return _modules.InstallModule(module); } #endregion #region Component Registration /// /// 注册一个系统到架构中 /// /// 要注册的系统类型 /// 要注册的系统实例 /// 注册成功的系统实例 public TSystem RegisterSystem(TSystem system) where TSystem : ISystem { return _componentRegistry.RegisterSystem(system); } /// /// 注册系统类型,由 DI 容器自动创建实例 /// /// 系统类型 /// 可选的实例创建后回调 public void RegisterSystem(Action? onCreated = null) where T : class, ISystem { _componentRegistry.RegisterSystem(onCreated); } /// /// 注册一个模型到架构中 /// /// 要注册的模型类型 /// 要注册的模型实例 /// 注册成功的模型实例 public TModel RegisterModel(TModel model) where TModel : IModel { return _componentRegistry.RegisterModel(model); } /// /// 注册模型类型,由 DI 容器自动创建实例 /// /// 模型类型 /// 可选的实例创建后回调 public void RegisterModel(Action? onCreated = null) where T : class, IModel { _componentRegistry.RegisterModel(onCreated); } /// /// 注册一个工具到架构中 /// /// 要注册的工具类型 /// 要注册的工具实例 /// 注册成功的工具实例 public TUtility RegisterUtility(TUtility utility) where TUtility : IUtility { return _componentRegistry.RegisterUtility(utility); } /// /// 注册工具类型,由 DI 容器自动创建实例 /// /// 工具类型 /// 可选的实例创建后回调 public void RegisterUtility(Action? onCreated = null) where T : class, IUtility { _componentRegistry.RegisterUtility(onCreated); } #endregion #region Initialization /// /// 抽象初始化方法,由子类重写以进行自定义初始化操作 /// protected abstract void OnInitialize(); /// /// 同步初始化方法,阻塞当前线程直到初始化完成 /// public void Initialize() { try { InitializeInternalAsync(false).GetAwaiter().GetResult(); } catch (Exception e) { _logger.Error("Architecture initialization failed:", e); _lifecycle.MarkAsFailed(e); throw; } } /// /// 异步初始化方法,返回Task以便调用者可以等待初始化完成 /// public async Task InitializeAsync() { try { await InitializeInternalAsync(true); } catch (Exception e) { _logger.Error("Architecture initialization failed:", e); _lifecycle.MarkAsFailed(e); throw; } } /// /// 异步初始化架构内部组件 /// /// 是否启用异步模式 private async Task InitializeInternalAsync(bool asyncMode) { // === 基础环境初始化 === Environment.Initialize(); // 注册内置服务模块 Services.ModuleManager.RegisterBuiltInModules(Services.Container); // 将 Environment 注册到容器 if (!Services.Container.Contains()) Services.Container.RegisterPlurality(Environment); // 初始化架构上下文 _context ??= new ArchitectureContext(Services.Container); GameContext.Bind(GetType(), _context); // 为服务设置上下文 Services.SetContext(_context); if (Configurator is null) { _logger.Debug("Mediator-based cqrs will not take effect without the service setter configured!"); } // 执行服务钩子 Services.Container.ExecuteServicesHook(Configurator); // 初始化服务模块 await Services.ModuleManager.InitializeAllAsync(asyncMode); // === 用户 OnInitialize === _logger.Debug("Calling user OnInitialize()"); OnInitialize(); _logger.Debug("User OnInitialize() completed"); // === 组件初始化阶段 === await _lifecycle.InitializeAllComponentsAsync(asyncMode); // === 初始化完成阶段 === Services.Container.Freeze(); _logger.Info("IOC container frozen"); _lifecycle.MarkAsReady(); _logger.Info($"Architecture {GetType().Name} is ready - all components initialized"); } /// /// 等待架构初始化完成(Ready 阶段) /// public Task WaitUntilReadyAsync() { return _lifecycle.WaitUntilReadyAsync(); } #endregion #region Destruction /// /// 异步销毁架构及所有组件 /// public virtual async ValueTask DestroyAsync() { await _lifecycle.DestroyAsync(); } /// /// 销毁架构并清理所有组件资源(同步方法,保留用于向后兼容) /// [Obsolete("建议使用 DestroyAsync() 以支持异步清理")] public virtual void Destroy() { _lifecycle.Destroy(); } #endregion }