From 017870421ee3c4ede773cb813a13d6ae26644f05 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Fri, 26 Dec 2025 13:50:14 +0800 Subject: [PATCH] =?UTF-8?q?style(csharp):=20=E7=BB=9F=E4=B8=80=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=A0=BC=E5=BC=8F=E5=8C=96=E8=A7=84=E8=8C=83=E5=B9=B6?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=B9=E6=B3=95=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 调整注释格式统一使用4个空格缩进 - 重新排列字段声明顺序提升代码可读性 - 将简单属性访问器改为表达式主体语法 - 优化AudioManagerSystem中音量设置逻辑 - 移除AbstractAssetCatalogSystem中多余空行 - 重构日志类中方法实现为表达式主体形式 - 统一空行分隔符保持代码结构一致性 - 优化方法内部逻辑表达式简化代码 - [no tag] --- GFramework.Core/architecture/Architecture.cs | 53 +- .../architecture/ArchitectureConfiguration.cs | 30 +- .../architecture/ArchitectureConstants.cs | 3 +- .../architecture/ArchitectureContext.cs | 2 +- .../architecture/ArchitectureOptions.cs | 14 +- .../architecture/ArchitecturePhase.cs | 36 +- .../architecture/ArchitectureRuntime.cs | 32 +- .../architecture/ArchitectureServices.cs | 5 +- GFramework.Core/architecture/IArchitecture.cs | 48 +- .../IArchitectureConfiguration.cs | 10 +- .../architecture/IArchitectureContext.cs | 24 +- .../architecture/IArchitectureExtensible.cs | 11 +- .../architecture/IArchitectureLifecycle.cs | 7 +- .../architecture/IArchitectureModule.cs | 11 +- .../architecture/IArchitecturePhaseAware.cs | 7 +- .../architecture/IArchitectureRuntime.cs | 18 +- .../architecture/IArchitectureServices.cs | 10 +- .../architecture/IAsyncInitializable.cs | 6 +- GFramework.Core/command/ICommand.cs | 4 +- .../constants/GFrameworkConstants.cs | 6 +- GFramework.Core/events/ArchitectureEvents.cs | 20 +- GFramework.Core/events/ITypeEventSystem.cs | 12 +- GFramework.Core/events/TypeEventSystem.cs | 25 +- GFramework.Core/ioc/IIocContainer.cs | 32 +- GFramework.Core/ioc/IocContainer.cs | 50 +- GFramework.Core/logging/AbstractLogger.cs | 352 +++++--- GFramework.Core/logging/ConsoleLogger.cs | 47 +- .../logging/ConsoleLoggerFactory.cs | 6 +- GFramework.Core/logging/ILogger.cs | 134 +-- GFramework.Core/logging/ILoggerFactory.cs | 4 +- GFramework.Core/logging/LogLevel.cs | 21 +- GFramework.Core/logging/NoopLogger.cs | 7 +- GFramework.Core/logging/NoopLoggerFactory.cs | 6 +- GFramework.Core/logging/README.md | 21 +- GFramework.Core/model/AbstractModel.cs | 16 +- GFramework.Core/model/IModel.cs | 6 +- GFramework.Core/query/AbstractQuery.cs | 4 +- GFramework.Core/query/IQuery.cs | 2 +- GFramework.Core/rule/ContextAwareBase.cs | 14 +- GFramework.Core/rule/IContextAware.cs | 6 +- GFramework.Core/rule/ILogAware.cs | 6 +- GFramework.Core/system/ISystem.cs | 2 +- .../utility/AbstractContextUtility.cs | 6 +- GFramework.Core/utility/IContextUtility.cs | 11 +- GFramework.Game/GFramework.Game.csproj | 2 +- .../assets/AbstractAssetCatalogSystem.cs | 81 +- GFramework.Game/assets/AssetCatalog.cs | 24 +- GFramework.Game/assets/IAssetCatalogSystem.cs | 27 +- .../assets/IResourceFactorySystem.cs | 11 +- GFramework.Game/assets/ResourceFactory.cs | 60 +- .../logging/GodotLogAttribute.cs | 6 +- .../logging/Diagnostic.cs | 44 - .../logging/GodotLoggerDiagnostic.cs | 44 + .../logging/GodotLoggerGenerator.cs | 10 +- GFramework.Godot/GFramework.Godot.csproj | 2 +- .../architecture/AbstractArchitecture.cs | 68 +- .../architecture/AbstractGodotModule.cs | 24 +- .../architecture/ArchitectureAnchor.cs | 18 +- GFramework.Godot/architecture/IGodotModule.cs | 8 +- .../assets/AbstractResourceFactorySystem.cs | 64 +- .../assets/IResourceLoadSystem.cs | 30 +- GFramework.Godot/assets/ResourceLoadSystem.cs | 141 +-- GFramework.Godot/extensions/NodeExtensions.cs | 6 +- GFramework.Godot/logging/GodotLogger.cs | 9 +- .../logging/GodotLoggerFactory.cs | 8 +- .../system/AbstractAudioManagerSystem.cs | 842 +++++++++--------- .../system/IAudioManagerSystem.cs | 104 +-- ...amework.SourceGenerators.Attributes.csproj | 2 +- .../enums/EnumExtensionsAttribute.cs | 27 +- .../logging/LogAttribute.cs | 4 +- .../logging/Diagnostic.cs | 37 - .../logging/LoggerDiagnostic.cs | 37 + .../logging/LoggerGenerator.cs | 12 +- GFramework.csproj | 24 +- 74 files changed, 1519 insertions(+), 1404 deletions(-) delete mode 100644 GFramework.Godot.SourceGenerators/logging/Diagnostic.cs create mode 100644 GFramework.Godot.SourceGenerators/logging/GodotLoggerDiagnostic.cs delete mode 100644 GFramework.SourceGenerators/logging/Diagnostic.cs create mode 100644 GFramework.SourceGenerators/logging/LoggerDiagnostic.cs diff --git a/GFramework.Core/architecture/Architecture.cs b/GFramework.Core/architecture/Architecture.cs index 8d24f6d..5a55424 100644 --- a/GFramework.Core/architecture/Architecture.cs +++ b/GFramework.Core/architecture/Architecture.cs @@ -8,9 +8,9 @@ using GFramework.Core.utility; namespace GFramework.Core.architecture; /// -/// 架构基类,提供系统、模型、工具等组件的注册与管理功能。 -/// 专注于生命周期管理、初始化流程控制和架构阶段转换。 -/// 不直接提供业务操作方法,业务操作通过 ArchitectureRuntime 提供。 +/// 架构基类,提供系统、模型、工具等组件的注册与管理功能。 +/// 专注于生命周期管理、初始化流程控制和架构阶段转换。 +/// 不直接提供业务操作方法,业务操作通过 ArchitectureRuntime 提供。 /// public abstract class Architecture( IArchitectureConfiguration? configuration = null, @@ -20,49 +20,49 @@ public abstract class Architecture( : IArchitecture, IArchitectureLifecycle { /// - /// 获取架构配置对象 + /// 获取架构配置对象 /// /// - /// 返回一个IArchitectureConfiguration接口的实例,默认为DefaultArchitectureConfiguration类型 + /// 返回一个IArchitectureConfiguration接口的实例,默认为DefaultArchitectureConfiguration类型 /// private IArchitectureConfiguration Configuration { get; } = configuration ?? new ArchitectureConfiguration(); /// - /// 获取架构服务对象 + /// 获取架构服务对象 /// /// - /// 返回一个IArchitectureServices接口的实例,默认为DefaultArchitectureServices类型 + /// 返回一个IArchitectureServices接口的实例,默认为DefaultArchitectureServices类型 /// private IArchitectureServices Services { get; } = services ?? new ArchitectureServices(); /// - /// 获取依赖注入容器 + /// 获取依赖注入容器 /// /// - /// 通过Services属性获取的IArchitectureServices中的Container属性 + /// 通过Services属性获取的IArchitectureServices中的Container属性 /// private IIocContainer Container => Services.Container; /// - /// 获取类型事件系统 + /// 获取类型事件系统 /// /// - /// 通过Services属性获取的IArchitectureServices中的TypeEventSystem属性 + /// 通过Services属性获取的IArchitectureServices中的TypeEventSystem属性 /// private ITypeEventSystem TypeEventSystem => Services.TypeEventSystem; /// - /// 获取架构运行时实例 + /// 获取架构运行时实例 /// /// - /// 统一的操作入口,负责命令、查询、事件的执行 + /// 统一的操作入口,负责命令、查询、事件的执行 /// public IArchitectureRuntime Runtime { get; private set; } = null!; #region Module Management /// - /// 安装架构模块 + /// 安装架构模块 /// /// 要安装的模块 public void InstallModule(IArchitectureModule module) @@ -80,7 +80,7 @@ public abstract class Architecture( #region IArchitectureLifecycle Implementation /// - /// 处理架构阶段变更通知 + /// 处理架构阶段变更通知 /// /// 当前架构阶段 /// 架构实例 @@ -123,7 +123,7 @@ public abstract class Architecture( private ArchitecturePhase CurrentPhase { get; set; } /// - /// 日志记录器实例,用于记录应用程序的运行日志 + /// 日志记录器实例,用于记录应用程序的运行日志 /// private ILogger _logger = null!; @@ -136,7 +136,7 @@ public abstract class Architecture( #region Lifecycle Management /// - /// 进入指定的架构阶段,并执行相应的生命周期管理操作 + /// 进入指定的架构阶段,并执行相应的生命周期管理操作 /// /// 要进入的下一个架构阶段 /// 当阶段转换不被允许时抛出异常 @@ -156,10 +156,7 @@ public abstract class Architecture( var previousPhase = CurrentPhase; CurrentPhase = next; - if (previousPhase != next) - { - logger.Info($"Architecture phase changed: {previousPhase} -> {next}"); - } + if (previousPhase != next) logger.Info($"Architecture phase changed: {previousPhase} -> {next}"); NotifyPhase(next); @@ -172,7 +169,7 @@ public abstract class Architecture( } /// - /// 通知所有生命周期钩子当前阶段变更 + /// 通知所有生命周期钩子当前阶段变更 /// /// 当前架构阶段 private void NotifyPhase(ArchitecturePhase phase) @@ -182,7 +179,7 @@ public abstract class Architecture( } /// - /// 注册生命周期钩子 + /// 注册生命周期钩子 /// /// 生命周期钩子实例 public void RegisterLifecycleHook(IArchitectureLifecycle hook) @@ -200,11 +197,11 @@ public abstract class Architecture( protected abstract void Init(); /// - /// 销毁架构并清理所有系统资源 + /// 销毁架构并清理所有系统资源 /// /// - /// 此函数负责有序地销毁架构中的所有系统组件,并发送相应的生命周期事件。 - /// 函数会确保只执行一次销毁操作,避免重复销毁。 + /// 此函数负责有序地销毁架构中的所有系统组件,并发送相应的生命周期事件。 + /// 函数会确保只执行一次销毁操作,避免重复销毁。 /// public virtual void Destroy() { @@ -368,7 +365,9 @@ public abstract class Architecture( Container.RegisterPlurality(system); _allSystems.Add(system); if (!_mInited) + { _mSystems.Add(system); + } else { _logger.Debug($"Immediately initializing system: {typeof(TSystem).Name}"); @@ -400,7 +399,9 @@ public abstract class Architecture( Container.RegisterPlurality(model); if (!_mInited) + { _mModels.Add(model); + } else { _logger.Debug($"Immediately initializing model: {typeof(TModel).Name}"); diff --git a/GFramework.Core/architecture/ArchitectureConfiguration.cs b/GFramework.Core/architecture/ArchitectureConfiguration.cs index f61335a..89f7f89 100644 --- a/GFramework.Core/architecture/ArchitectureConfiguration.cs +++ b/GFramework.Core/architecture/ArchitectureConfiguration.cs @@ -3,26 +3,26 @@ namespace GFramework.Core.architecture; /// -/// 默认架构配置类,实现IArchitectureConfiguration接口 -/// 提供日志工厂、日志级别和架构选项的默认配置 +/// 默认架构配置类,实现IArchitectureConfiguration接口 +/// 提供日志工厂、日志级别和架构选项的默认配置 /// -public class ArchitectureConfiguration: IArchitectureConfiguration +public class ArchitectureConfiguration : IArchitectureConfiguration { /// - /// 获取或设置日志工厂实例 - /// 默认使用控制台日志工厂 - /// - public ILoggerFactory LoggerFactory { get; set; } = new ConsoleLoggerFactory(); - - /// - /// 获取或设置日志级别 - /// 默认设置为Info级别 + /// 获取或设置日志级别 + /// 默认设置为Info级别 /// public LogLevel LogLevel { get; set; } = LogLevel.Info; - + /// - /// 获取或设置架构选项 - /// 默认创建新的ArchitectureOptions实例 + /// 获取或设置日志工厂实例 + /// 默认使用控制台日志工厂 + /// + public ILoggerFactory LoggerFactory { get; set; } = new ConsoleLoggerFactory(); + + /// + /// 获取或设置架构选项 + /// 默认创建新的ArchitectureOptions实例 /// public ArchitectureOptions Options { get; set; } = new(); -} +} \ No newline at end of file diff --git a/GFramework.Core/architecture/ArchitectureConstants.cs b/GFramework.Core/architecture/ArchitectureConstants.cs index 784b15d..19e71bc 100644 --- a/GFramework.Core/architecture/ArchitectureConstants.cs +++ b/GFramework.Core/architecture/ArchitectureConstants.cs @@ -1,5 +1,4 @@ - -using System.Collections.Immutable; +using System.Collections.Immutable; namespace GFramework.Core.architecture; diff --git a/GFramework.Core/architecture/ArchitectureContext.cs b/GFramework.Core/architecture/ArchitectureContext.cs index e907ca6..cb5265b 100644 --- a/GFramework.Core/architecture/ArchitectureContext.cs +++ b/GFramework.Core/architecture/ArchitectureContext.cs @@ -10,7 +10,7 @@ using GFramework.Core.utility; namespace GFramework.Core.architecture; /// -/// 架构上下文类,提供对系统、模型、工具等组件的访问以及命令、查询、事件的执行管理 +/// 架构上下文类,提供对系统、模型、工具等组件的访问以及命令、查询、事件的执行管理 /// public class ArchitectureContext( IIocContainer container, diff --git a/GFramework.Core/architecture/ArchitectureOptions.cs b/GFramework.Core/architecture/ArchitectureOptions.cs index 9baaa1c..8d5c916 100644 --- a/GFramework.Core/architecture/ArchitectureOptions.cs +++ b/GFramework.Core/architecture/ArchitectureOptions.cs @@ -1,7 +1,7 @@ namespace GFramework.Core.architecture; /// -/// 架构选项配置类,用于定义架构行为的相关配置选项 +/// 架构选项配置类,用于定义架构行为的相关配置选项 /// public sealed class ArchitectureOptions( bool strictPhaseValidation = true, @@ -9,12 +9,12 @@ public sealed class ArchitectureOptions( ) { /// - /// 严格阶段验证开关,当设置为true时启用严格的阶段验证机制 - /// - public bool StrictPhaseValidation = strictPhaseValidation; - - /// - /// 允许延迟注册开关,当设置为true时允许在初始化完成后进行组件注册 + /// 允许延迟注册开关,当设置为true时允许在初始化完成后进行组件注册 /// public bool AllowLateRegistration = allowLateRegistration; + + /// + /// 严格阶段验证开关,当设置为true时启用严格的阶段验证机制 + /// + public bool StrictPhaseValidation = strictPhaseValidation; } \ No newline at end of file diff --git a/GFramework.Core/architecture/ArchitecturePhase.cs b/GFramework.Core/architecture/ArchitecturePhase.cs index 052ea91..ef93dfa 100644 --- a/GFramework.Core/architecture/ArchitecturePhase.cs +++ b/GFramework.Core/architecture/ArchitecturePhase.cs @@ -1,49 +1,51 @@ namespace GFramework.Core.architecture; /// -/// 架构阶段枚举,定义了系统架构初始化和运行过程中的各个关键阶段 +/// 架构阶段枚举,定义了系统架构初始化和运行过程中的各个关键阶段 /// /// -/// 该枚举用于标记和控制系统架构组件的生命周期,确保在正确的时机执行相应的初始化和处理逻辑。 -/// 各个阶段按照时间顺序排列,从创建到准备就绪的完整流程。 +/// 该枚举用于标记和控制系统架构组件的生命周期,确保在正确的时机执行相应的初始化和处理逻辑。 +/// 各个阶段按照时间顺序排列,从创建到准备就绪的完整流程。 /// public enum ArchitecturePhase { /// - /// 无效阶段,表示未定义的阶段 + /// 无效阶段,表示未定义的阶段 /// None = 0, + /// - /// 模型初始化之前阶段 + /// 模型初始化之前阶段 /// BeforeModelInit, - + /// - /// 模型初始化之后阶段 + /// 模型初始化之后阶段 /// AfterModelInit, - + /// - /// 系统初始化之前阶段 + /// 系统初始化之前阶段 /// BeforeSystemInit, - + /// - /// 系统初始化之后阶段 + /// 系统初始化之后阶段 /// AfterSystemInit, - + /// - /// 就绪阶段,完成冻结和事件处理后的最终状态 + /// 就绪阶段,完成冻结和事件处理后的最终状态 /// Ready, + /// - /// 正在销毁中 暂时不使用 + /// 正在销毁中 暂时不使用 /// Destroying, + /// - /// 已销毁 暂时不使用 + /// 已销毁 暂时不使用 /// Destroyed - -} +} \ No newline at end of file diff --git a/GFramework.Core/architecture/ArchitectureRuntime.cs b/GFramework.Core/architecture/ArchitectureRuntime.cs index 6ee87bc..b5ac076 100644 --- a/GFramework.Core/architecture/ArchitectureRuntime.cs +++ b/GFramework.Core/architecture/ArchitectureRuntime.cs @@ -5,12 +5,27 @@ using GFramework.Core.query; namespace GFramework.Core.architecture; /// -/// 架构运行时默认实现,委托 ArchitectureContext 执行具体操作 +/// 架构运行时默认实现,委托 ArchitectureContext 执行具体操作 /// public class ArchitectureRuntime(IArchitectureContext context) : IArchitectureRuntime { private readonly IArchitectureContext _context = context ?? throw new ArgumentNullException(nameof(context)); + #region Query Execution + + /// + /// 发起一次查询请求并获得其结果 + /// + /// 查询结果的数据类型 + /// 要发起的查询对象 + /// 查询得到的结果数据 + public TResult SendQuery(IQuery query) + { + return _context.SendQuery(query); + } + + #endregion + #region Command Execution /// @@ -36,21 +51,6 @@ public class ArchitectureRuntime(IArchitectureContext context) : IArchitectureRu #endregion - #region Query Execution - - /// - /// 发起一次查询请求并获得其结果 - /// - /// 查询结果的数据类型 - /// 要发起的查询对象 - /// 查询得到的结果数据 - public TResult SendQuery(IQuery query) - { - return _context.SendQuery(query); - } - - #endregion - #region Event Management /// diff --git a/GFramework.Core/architecture/ArchitectureServices.cs b/GFramework.Core/architecture/ArchitectureServices.cs index 09a8412..804b657 100644 --- a/GFramework.Core/architecture/ArchitectureServices.cs +++ b/GFramework.Core/architecture/ArchitectureServices.cs @@ -1,10 +1,9 @@ - -using GFramework.Core.events; +using GFramework.Core.events; using GFramework.Core.ioc; namespace GFramework.Core.architecture; -public class ArchitectureServices: IArchitectureServices +public class ArchitectureServices : IArchitectureServices { public IIocContainer Container { get; } = new IocContainer(); public ITypeEventSystem TypeEventSystem { get; } = new TypeEventSystem(); diff --git a/GFramework.Core/architecture/IArchitecture.cs b/GFramework.Core/architecture/IArchitecture.cs index 33818db..a501457 100644 --- a/GFramework.Core/architecture/IArchitecture.cs +++ b/GFramework.Core/architecture/IArchitecture.cs @@ -1,8 +1,4 @@ -using GFramework.Core.architecture; -using GFramework.Core.command; -using GFramework.Core.events; -using GFramework.Core.model; -using GFramework.Core.query; +using GFramework.Core.model; using GFramework.Core.system; using GFramework.Core.utility; @@ -12,64 +8,64 @@ namespace GFramework.Core.architecture; /// 架构接口,专注于生命周期管理,包括系统、模型、工具的注册和获取 /// 业务操作通过 ArchitectureRuntime 提供 /// -public interface IArchitecture: IAsyncInitializable +public interface IArchitecture : IAsyncInitializable { /// - /// 初始化方法,用于执行对象的初始化操作 + /// 获取架构上下文 + /// + IArchitectureContext Context { get; } + + /// + /// 获取架构运行时实例 + /// + IArchitectureRuntime Runtime { get; } + + /// + /// 初始化方法,用于执行对象的初始化操作 /// /// - /// 该方法通常用于设置初始状态、初始化成员变量或执行必要的预处理操作 + /// 该方法通常用于设置初始状态、初始化成员变量或执行必要的预处理操作 /// void Initialize(); - + /// - /// 销毁方法,用于执行对象的清理和销毁操作 + /// 销毁方法,用于执行对象的清理和销毁操作 /// /// - /// 该方法通常用于释放资源、清理内存或执行必要的清理操作 + /// 该方法通常用于释放资源、清理内存或执行必要的清理操作 /// void Destroy(); /// - /// 注册系统实例到架构中 + /// 注册系统实例到架构中 /// /// 系统类型,必须实现ISystem接口 /// 要注册的系统实例 void RegisterSystem(T system) where T : ISystem; /// - /// 注册模型实例到架构中 + /// 注册模型实例到架构中 /// /// 模型类型,必须实现IModel接口 /// 要注册的模型实例 void RegisterModel(T model) where T : IModel; /// - /// 注册工具实例到架构中 + /// 注册工具实例到架构中 /// /// 工具类型,必须实现IUtility接口 /// 要注册的工具实例 void RegisterUtility(T utility) where T : IUtility; /// - /// 安装架构模块 + /// 安装架构模块 /// /// 要安装的模块 void InstallModule(IArchitectureModule module); /// - /// 注册生命周期钩子 + /// 注册生命周期钩子 /// /// 生命周期钩子实例 void RegisterLifecycleHook(IArchitectureLifecycle hook); - - /// - /// 获取架构上下文 - /// - IArchitectureContext Context { get; } - - /// - /// 获取架构运行时实例 - /// - IArchitectureRuntime Runtime { get; } } \ No newline at end of file diff --git a/GFramework.Core/architecture/IArchitectureConfiguration.cs b/GFramework.Core/architecture/IArchitectureConfiguration.cs index 9fbe6a5..cf4bd3d 100644 --- a/GFramework.Core/architecture/IArchitectureConfiguration.cs +++ b/GFramework.Core/architecture/IArchitectureConfiguration.cs @@ -3,17 +3,17 @@ namespace GFramework.Core.architecture; /// -/// 定义架构配置的接口,提供日志工厂、日志级别和架构选项的配置功能 +/// 定义架构配置的接口,提供日志工厂、日志级别和架构选项的配置功能 /// public interface IArchitectureConfiguration { /// - /// 获取或设置日志工厂,用于创建日志记录器实例 + /// 获取或设置日志工厂,用于创建日志记录器实例 /// ILoggerFactory LoggerFactory { get; set; } - + /// - /// 获取或设置架构选项,包含架构相关的配置参数 + /// 获取或设置架构选项,包含架构相关的配置参数 /// ArchitectureOptions Options { get; set; } -} +} \ No newline at end of file diff --git a/GFramework.Core/architecture/IArchitectureContext.cs b/GFramework.Core/architecture/IArchitectureContext.cs index 2a2fa88..ae589d6 100644 --- a/GFramework.Core/architecture/IArchitectureContext.cs +++ b/GFramework.Core/architecture/IArchitectureContext.cs @@ -9,44 +9,44 @@ using GFramework.Core.utility; namespace GFramework.Core.architecture; /// -/// 架构上下文接口,提供对系统、模型、工具类的访问以及命令、查询、事件的发送和注册功能 +/// 架构上下文接口,提供对系统、模型、工具类的访问以及命令、查询、事件的发送和注册功能 /// public interface IArchitectureContext { /// - /// 获取日志工厂 + /// 获取日志工厂 /// ILoggerFactory LoggerFactory { get; } /// - /// 获取指定类型的系统实例 + /// 获取指定类型的系统实例 /// /// 系统类型,必须继承自ISystem接口 /// 系统实例,如果不存在则返回null TSystem? GetSystem() where TSystem : class, ISystem; /// - /// 获取指定类型的模型实例 + /// 获取指定类型的模型实例 /// /// 模型类型,必须继承自IModel接口 /// 模型实例,如果不存在则返回null TModel? GetModel() where TModel : class, IModel; /// - /// 获取指定类型的工具类实例 + /// 获取指定类型的工具类实例 /// /// 工具类类型,必须继承自IUtility接口 /// 工具类实例,如果不存在则返回null TUtility? GetUtility() where TUtility : class, IUtility; /// - /// 发送一个命令 + /// 发送一个命令 /// /// 要发送的命令 void SendCommand(ICommand command); /// - /// 发送一个带返回值的命令 + /// 发送一个带返回值的命令 /// /// 命令执行结果类型 /// 要发送的命令 @@ -54,7 +54,7 @@ public interface IArchitectureContext TResult SendCommand(ICommand command); /// - /// 发送一个查询请求 + /// 发送一个查询请求 /// /// 查询结果类型 /// 要发送的查询 @@ -62,20 +62,20 @@ public interface IArchitectureContext TResult SendQuery(IQuery query); /// - /// 发送一个事件 + /// 发送一个事件 /// /// 事件类型,必须具有无参构造函数 void SendEvent() where TEvent : new(); /// - /// 发送一个带参数的事件 + /// 发送一个带参数的事件 /// /// 事件类型 /// 事件参数 void SendEvent(TEvent e) where TEvent : class; /// - /// 注册事件处理器 + /// 注册事件处理器 /// /// 事件类型 /// 事件处理委托 @@ -83,7 +83,7 @@ public interface IArchitectureContext IUnRegister RegisterEvent(Action handler); /// - /// 取消注册事件监听器 + /// 取消注册事件监听器 /// /// 事件类型 /// 要取消注册的事件回调方法 diff --git a/GFramework.Core/architecture/IArchitectureExtensible.cs b/GFramework.Core/architecture/IArchitectureExtensible.cs index 87545af..1fd93a7 100644 --- a/GFramework.Core/architecture/IArchitectureExtensible.cs +++ b/GFramework.Core/architecture/IArchitectureExtensible.cs @@ -1,20 +1,19 @@ namespace GFramework.Core.architecture; /// -/// 可扩展架构接口,继承自IArchitecture接口,提供模块安装和生命周期钩子注册功能 +/// 可扩展架构接口,继承自IArchitecture接口,提供模块安装和生命周期钩子注册功能 /// public interface IArchitectureExtensible : IArchitecture { /// - /// 安装架构模块 + /// 安装架构模块 /// /// 要安装的架构模块实例 void InstallModule(IArchitectureModule module); - + /// - /// 注册架构生命周期钩子 + /// 注册架构生命周期钩子 /// /// 要注册的架构生命周期钩子实例 void RegisterLifecycleHook(IArchitectureLifecycle hook); -} - +} \ No newline at end of file diff --git a/GFramework.Core/architecture/IArchitectureLifecycle.cs b/GFramework.Core/architecture/IArchitectureLifecycle.cs index 370d608..97cc324 100644 --- a/GFramework.Core/architecture/IArchitectureLifecycle.cs +++ b/GFramework.Core/architecture/IArchitectureLifecycle.cs @@ -1,15 +1,14 @@ namespace GFramework.Core.architecture; /// -/// 架构生命周期接口,定义了架构在不同阶段的回调方法 +/// 架构生命周期接口,定义了架构在不同阶段的回调方法 /// public interface IArchitectureLifecycle { /// - /// 当架构进入指定阶段时触发的回调方法 + /// 当架构进入指定阶段时触发的回调方法 /// /// 当前的架构阶段 /// 相关的架构实例 void OnPhase(ArchitecturePhase phase, IArchitecture architecture); -} - +} \ No newline at end of file diff --git a/GFramework.Core/architecture/IArchitectureModule.cs b/GFramework.Core/architecture/IArchitectureModule.cs index 8c2bc4c..273cc14 100644 --- a/GFramework.Core/architecture/IArchitectureModule.cs +++ b/GFramework.Core/architecture/IArchitectureModule.cs @@ -1,15 +1,14 @@ - -namespace GFramework.Core.architecture; +namespace GFramework.Core.architecture; /// -/// 架构模块接口,继承自架构生命周期接口。 -/// 定义了模块安装到架构中的标准方法。 +/// 架构模块接口,继承自架构生命周期接口。 +/// 定义了模块安装到架构中的标准方法。 /// public interface IArchitectureModule : IArchitectureLifecycle, IArchitecturePhaseAware { /// - /// 将当前模块安装到指定的架构中。 + /// 将当前模块安装到指定的架构中。 /// /// 要安装模块的目标架构实例。 void Install(IArchitecture architecture); -} +} \ No newline at end of file diff --git a/GFramework.Core/architecture/IArchitecturePhaseAware.cs b/GFramework.Core/architecture/IArchitecturePhaseAware.cs index 42467e6..557fd79 100644 --- a/GFramework.Core/architecture/IArchitecturePhaseAware.cs +++ b/GFramework.Core/architecture/IArchitecturePhaseAware.cs @@ -1,14 +1,13 @@ namespace GFramework.Core.architecture; /// -/// 定义架构阶段感知接口,用于在架构的不同阶段执行相应的逻辑 +/// 定义架构阶段感知接口,用于在架构的不同阶段执行相应的逻辑 /// public interface IArchitecturePhaseAware { /// - /// 当架构进入指定阶段时触发的回调方法 + /// 当架构进入指定阶段时触发的回调方法 /// /// 架构阶段枚举值,表示当前所处的架构阶段 void OnArchitecturePhase(ArchitecturePhase phase); -} - +} \ No newline at end of file diff --git a/GFramework.Core/architecture/IArchitectureRuntime.cs b/GFramework.Core/architecture/IArchitectureRuntime.cs index 37ccb67..134da12 100644 --- a/GFramework.Core/architecture/IArchitectureRuntime.cs +++ b/GFramework.Core/architecture/IArchitectureRuntime.cs @@ -5,20 +5,20 @@ using GFramework.Core.query; namespace GFramework.Core.architecture; /// -/// 架构运行时接口,提供统一的命令、查询、事件操作入口 -/// 负责委托 ArchitectureContext 的能力执行具体操作 +/// 架构运行时接口,提供统一的命令、查询、事件操作入口 +/// 负责委托 ArchitectureContext 的能力执行具体操作 /// public interface IArchitectureRuntime { /// - /// 发送并执行指定的命令 + /// 发送并执行指定的命令 /// /// 命令类型,必须实现ICommand接口 /// 要执行的命令实例 void SendCommand(T command) where T : ICommand; /// - /// 发送并执行带有返回值的命令 + /// 发送并执行带有返回值的命令 /// /// 命令执行结果的类型 /// 要执行的命令实例 @@ -26,7 +26,7 @@ public interface IArchitectureRuntime TResult SendCommand(ICommand command); /// - /// 发送并执行查询操作 + /// 发送并执行查询操作 /// /// 查询结果的类型 /// 要执行的查询实例 @@ -34,20 +34,20 @@ public interface IArchitectureRuntime TResult SendQuery(IQuery query); /// - /// 发送无参事件 + /// 发送无参事件 /// /// 事件类型,必须具有无参构造函数 void SendEvent() where TEvent : new(); /// - /// 发送指定的事件实例 + /// 发送指定的事件实例 /// /// 事件类型 /// 要发送的事件实例 void SendEvent(TEvent e) where TEvent : class; /// - /// 注册事件监听器 + /// 注册事件监听器 /// /// 事件类型 /// 事件触发时的回调方法 @@ -55,7 +55,7 @@ public interface IArchitectureRuntime IUnRegister RegisterEvent(Action onEvent); /// - /// 取消注册事件监听器 + /// 取消注册事件监听器 /// /// 事件类型 /// 要取消注册的事件回调方法 diff --git a/GFramework.Core/architecture/IArchitectureServices.cs b/GFramework.Core/architecture/IArchitectureServices.cs index 13f2fd5..fe72e56 100644 --- a/GFramework.Core/architecture/IArchitectureServices.cs +++ b/GFramework.Core/architecture/IArchitectureServices.cs @@ -4,19 +4,19 @@ using GFramework.Core.ioc; namespace GFramework.Core.architecture; /// -/// 架构服务接口,定义了框架核心架构所需的服务组件 +/// 架构服务接口,定义了框架核心架构所需的服务组件 /// public interface IArchitectureServices { /// - /// 获取依赖注入容器 + /// 获取依赖注入容器 /// /// IIocContainer类型的依赖注入容器实例 IIocContainer Container { get; } - + /// - /// 获取类型事件系统 + /// 获取类型事件系统 /// /// ITypeEventSystem类型的事件系统实例 ITypeEventSystem TypeEventSystem { get; } -} +} \ No newline at end of file diff --git a/GFramework.Core/architecture/IAsyncInitializable.cs b/GFramework.Core/architecture/IAsyncInitializable.cs index 5f71077..03db994 100644 --- a/GFramework.Core/architecture/IAsyncInitializable.cs +++ b/GFramework.Core/architecture/IAsyncInitializable.cs @@ -1,13 +1,13 @@ namespace GFramework.Core.architecture; /// -/// 定义异步初始化接口,用于需要异步初始化的组件或服务 +/// 定义异步初始化接口,用于需要异步初始化的组件或服务 /// public interface IAsyncInitializable { /// - /// 异步初始化方法,用于执行组件或服务的异步初始化逻辑 + /// 异步初始化方法,用于执行组件或服务的异步初始化逻辑 /// /// 表示异步初始化操作的Task Task InitializeAsync(); -} +} \ No newline at end of file diff --git a/GFramework.Core/command/ICommand.cs b/GFramework.Core/command/ICommand.cs index 3e37b03..b4c2214 100644 --- a/GFramework.Core/command/ICommand.cs +++ b/GFramework.Core/command/ICommand.cs @@ -6,7 +6,7 @@ namespace GFramework.Core.command; /// 命令接口,定义了无返回值命令的基本契约 /// 该接口继承了多个框架能力接口,使命令可以访问架构、系统、模型、工具,并能够发送事件、命令和查询 /// -public interface ICommand: IContextAware +public interface ICommand : IContextAware { /// /// 执行命令的核心方法 @@ -20,7 +20,7 @@ public interface ICommand: IContextAware /// 该接口继承了多个框架能力接口,使命令可以访问架构、系统、模型、工具,并能够发送事件、命令和查询 /// /// 命令执行后返回的结果类型 -public interface ICommand: IContextAware +public interface ICommand : IContextAware { /// /// 执行命令的核心方法 diff --git a/GFramework.Core/constants/GFrameworkConstants.cs b/GFramework.Core/constants/GFrameworkConstants.cs index ef0e309..b00301b 100644 --- a/GFramework.Core/constants/GFrameworkConstants.cs +++ b/GFramework.Core/constants/GFrameworkConstants.cs @@ -1,12 +1,12 @@ namespace GFramework.Core.constants; /// -/// GFramework框架常量定义类 +/// GFramework框架常量定义类 /// public static class GFrameworkConstants { /// - /// 框架名称常量 + /// 框架名称常量 /// public const string FrameworkName = "GFramework"; -} +} \ No newline at end of file diff --git a/GFramework.Core/events/ArchitectureEvents.cs b/GFramework.Core/events/ArchitectureEvents.cs index d40b9fb..8a77c03 100644 --- a/GFramework.Core/events/ArchitectureEvents.cs +++ b/GFramework.Core/events/ArchitectureEvents.cs @@ -1,25 +1,25 @@ namespace GFramework.Core.events; /// -/// 架构事件定义类,包含应用程序架构生命周期相关的事件结构体 +/// 架构事件定义类,包含应用程序架构生命周期相关的事件结构体 /// public static class ArchitectureEvents { /// - /// 架构生命周期准备就绪事件 - /// 当架构完成初始化并准备就绪时触发此事件 + /// 架构生命周期准备就绪事件 + /// 当架构完成初始化并准备就绪时触发此事件 /// public readonly struct ArchitectureLifecycleReadyEvent; - + /// - /// 架构销毁中事件 - /// 当架构开始销毁过程时触发此事件,表示系统正在关闭 + /// 架构销毁中事件 + /// 当架构开始销毁过程时触发此事件,表示系统正在关闭 /// public readonly struct ArchitectureDestroyingEvent; - + /// - /// 架构已销毁事件 - /// 当架构完全销毁完成后触发此事件,表示系统已关闭 + /// 架构已销毁事件 + /// 当架构完全销毁完成后触发此事件,表示系统已关闭 /// public readonly struct ArchitectureDestroyedEvent; -} +} \ No newline at end of file diff --git a/GFramework.Core/events/ITypeEventSystem.cs b/GFramework.Core/events/ITypeEventSystem.cs index 491cc40..e645daf 100644 --- a/GFramework.Core/events/ITypeEventSystem.cs +++ b/GFramework.Core/events/ITypeEventSystem.cs @@ -1,25 +1,25 @@ namespace GFramework.Core.events; /// -/// 类型事件系统接口,定义基于类型的事件发送、注册和注销功能 +/// 类型事件系统接口,定义基于类型的事件发送、注册和注销功能 /// public interface ITypeEventSystem { /// - /// 发送事件,自动创建事件实例 + /// 发送事件,自动创建事件实例 /// /// 事件类型,必须具有无参构造函数 void Send() where T : new(); /// - /// 发送指定的事件实例 + /// 发送指定的事件实例 /// /// 事件类型 /// 事件实例 void Send(T e); /// - /// 注册事件监听器 + /// 注册事件监听器 /// /// 事件类型 /// 事件处理回调函数 @@ -27,9 +27,9 @@ public interface ITypeEventSystem IUnRegister Register(Action onEvent); /// - /// 注销事件监听器 + /// 注销事件监听器 /// /// 事件类型 /// 要注销的事件处理回调函数 void UnRegister(Action onEvent); -} +} \ No newline at end of file diff --git a/GFramework.Core/events/TypeEventSystem.cs b/GFramework.Core/events/TypeEventSystem.cs index adadd71..fd54abb 100644 --- a/GFramework.Core/events/TypeEventSystem.cs +++ b/GFramework.Core/events/TypeEventSystem.cs @@ -1,20 +1,23 @@ namespace GFramework.Core.events; /// -/// 类型事件系统,提供基于类型的事件发送、注册和注销功能 +/// 类型事件系统,提供基于类型的事件发送、注册和注销功能 /// public class TypeEventSystem : ITypeEventSystem { private readonly EasyEvents _mEvents = new(); /// - /// 发送事件,自动创建事件实例 + /// 发送事件,自动创建事件实例 /// /// 事件类型,必须具有无参构造函数 - public void Send() where T : new() => _mEvents.GetEvent>().Trigger(new T()); + public void Send() where T : new() + { + _mEvents.GetEvent>().Trigger(new T()); + } /// - /// 发送指定的事件实例 + /// 发送指定的事件实例 /// /// 事件类型 /// 事件实例 @@ -24,17 +27,23 @@ public class TypeEventSystem : ITypeEventSystem } /// - /// 注册事件监听器 + /// 注册事件监听器 /// /// 事件类型 /// 事件处理回调函数 /// 反注册接口,用于注销事件监听 - public IUnRegister Register(Action onEvent) => _mEvents.GetOrAddEvent>().Register(onEvent); + public IUnRegister Register(Action onEvent) + { + return _mEvents.GetOrAddEvent>().Register(onEvent); + } /// - /// 注销事件监听器 + /// 注销事件监听器 /// /// 事件类型 /// 要注销的事件处理回调函数 - public void UnRegister(Action onEvent) => _mEvents.GetEvent>().UnRegister(onEvent); + public void UnRegister(Action onEvent) + { + _mEvents.GetEvent>().UnRegister(onEvent); + } } \ No newline at end of file diff --git a/GFramework.Core/ioc/IIocContainer.cs b/GFramework.Core/ioc/IIocContainer.cs index ddc9207..dda1924 100644 --- a/GFramework.Core/ioc/IIocContainer.cs +++ b/GFramework.Core/ioc/IIocContainer.cs @@ -4,15 +4,15 @@ using GFramework.Core.system; namespace GFramework.Core.ioc; /// -/// 依赖注入容器接口,定义了服务注册、解析和管理的基本操作 +/// 依赖注入容器接口,定义了服务注册、解析和管理的基本操作 /// public interface IIocContainer : IContextAware { #region Register Methods /// - /// 注册单例 - /// 一个类型只允许一个实例 + /// 注册单例 + /// 一个类型只允许一个实例 /// /// 要注册为单例的类型 /// 要注册的单例实例 @@ -21,27 +21,27 @@ public interface IIocContainer : IContextAware /// - /// 注册一个实例及其所有可赋值的接口类型到容器中 + /// 注册一个实例及其所有可赋值的接口类型到容器中 /// /// 实例的类型 /// 要注册的实例对象,不能为null void RegisterPlurality(T instance); /// - /// 注册系统实例,将其绑定到其所有实现的接口上 + /// 注册系统实例,将其绑定到其所有实现的接口上 /// /// 系统实例对象 void RegisterSystem(ISystem system); /// - /// 注册指定类型的实例到容器中 + /// 注册指定类型的实例到容器中 /// /// 要注册的实例类型 /// 要注册的实例对象,不能为null void Register(T instance); /// - /// 注册指定类型的实例到容器中 + /// 注册指定类型的实例到容器中 /// /// 要注册的实例类型 /// 要注册的实例对象 @@ -52,15 +52,15 @@ public interface IIocContainer : IContextAware #region Get Methods /// - /// 获取单个实例(通常用于具体类型) - /// 如果存在多个,只返回第一个 + /// 获取单个实例(通常用于具体类型) + /// 如果存在多个,只返回第一个 /// /// 期望获取的实例类型 /// 找到的第一个实例;如果未找到则返回 null T? Get() where T : class; /// - /// 获取指定类型的必需实例 + /// 获取指定类型的必需实例 /// /// 期望获取的实例类型 /// 找到的唯一实例 @@ -68,14 +68,14 @@ public interface IIocContainer : IContextAware T GetRequired() where T : class; /// - /// 获取指定类型的所有实例(接口 / 抽象类推荐使用) + /// 获取指定类型的所有实例(接口 / 抽象类推荐使用) /// /// 期望获取的实例类型 /// 所有符合条件的实例列表;如果没有则返回空数组 IReadOnlyList GetAll() where T : class; /// - /// 获取并排序(系统调度专用) + /// 获取并排序(系统调度专用) /// /// 期望获取的实例类型 /// 比较器委托,定义排序规则 @@ -87,26 +87,26 @@ public interface IIocContainer : IContextAware #region Utility Methods /// - /// 检查容器中是否包含指定类型的实例 + /// 检查容器中是否包含指定类型的实例 /// /// 要检查的类型 /// 如果容器中包含指定类型的实例则返回true,否则返回false bool Contains(); /// - /// 判断容器中是否包含某个具体的实例对象 + /// 判断容器中是否包含某个具体的实例对象 /// /// 待查询的实例对象 /// 若容器中包含该实例则返回true,否则返回false bool ContainsInstance(object instance); /// - /// 清空容器中的所有实例 + /// 清空容器中的所有实例 /// void Clear(); /// - /// 冻结容器,防止后续修改 + /// 冻结容器,防止后续修改 /// void Freeze(); diff --git a/GFramework.Core/ioc/IocContainer.cs b/GFramework.Core/ioc/IocContainer.cs index 4091954..cbb289a 100644 --- a/GFramework.Core/ioc/IocContainer.cs +++ b/GFramework.Core/ioc/IocContainer.cs @@ -12,9 +12,9 @@ public class IocContainer : ContextAwareBase, IIocContainer #region Lock /// - /// 读写锁对象,用于控制多线程环境下对共享资源的访问 - /// 使用ReaderWriterLockSlim提供高效的读写锁定机制 - /// 配置为不支持递归锁,避免死锁风险 + /// 读写锁对象,用于控制多线程环境下对共享资源的访问 + /// 使用ReaderWriterLockSlim提供高效的读写锁定机制 + /// 配置为不支持递归锁,避免死锁风险 /// private readonly ReaderWriterLockSlim _lock = new(LockRecursionPolicy.NoRecursion); @@ -23,8 +23,8 @@ public class IocContainer : ContextAwareBase, IIocContainer #region Flag /// - /// 冻结标志位,用于标识对象是否已被冻结 - /// true表示对象已冻结,不可修改;false表示对象可正常修改 + /// 冻结标志位,用于标识对象是否已被冻结 + /// true表示对象已冻结,不可修改;false表示对象可正常修改 /// private volatile bool _frozen; @@ -33,14 +33,14 @@ public class IocContainer : ContextAwareBase, IIocContainer #region Core /// - /// 存储所有已注册对象实例的集合,用于跟踪和管理容器中的所有对象 - /// 使用HashSet确保对象唯一性,避免重复注册同一实例 + /// 存储所有已注册对象实例的集合,用于跟踪和管理容器中的所有对象 + /// 使用HashSet确保对象唯一性,避免重复注册同一实例 /// private readonly HashSet _objects = []; /// - /// 类型索引字典,用于快速查找指定类型的所有实例 - /// 键为类型对象,值为该类型对应的所有实例集合 + /// 类型索引字典,用于快速查找指定类型的所有实例 + /// 键为类型对象,值为该类型对应的所有实例集合 /// private readonly Dictionary> _typeIndex = new(); @@ -57,8 +57,8 @@ public class IocContainer : ContextAwareBase, IIocContainer } /// - /// 注册单例 - /// 一个类型只允许一个实例 + /// 注册单例 + /// 一个类型只允许一个实例 /// /// 要注册为单例的类型 /// 要注册的单例实例 @@ -94,7 +94,7 @@ public class IocContainer : ContextAwareBase, IIocContainer /// - /// 注册一个实例及其所有可赋值的接口类型到容器中 + /// 注册一个实例及其所有可赋值的接口类型到容器中 /// /// 实例的类型 /// 要注册的实例对象,不能为null @@ -126,7 +126,7 @@ public class IocContainer : ContextAwareBase, IIocContainer } /// - /// 在内部字典中注册指定类型和实例的映射关系 + /// 在内部字典中注册指定类型和实例的映射关系 /// /// 要注册的类型 /// 要注册的实例 @@ -152,7 +152,7 @@ public class IocContainer : ContextAwareBase, IIocContainer /// - /// 注册系统实例,将其绑定到其所有实现的接口上 + /// 注册系统实例,将其绑定到其所有实现的接口上 /// /// 系统实例对象 public void RegisterSystem(ISystem system) @@ -162,7 +162,7 @@ public class IocContainer : ContextAwareBase, IIocContainer /// - /// 注册指定类型的实例到容器中 + /// 注册指定类型的实例到容器中 /// /// 要注册的实例类型 /// 要注册的实例对象,不能为null @@ -182,7 +182,7 @@ public class IocContainer : ContextAwareBase, IIocContainer } /// - /// 注册指定类型的实例到容器中 + /// 注册指定类型的实例到容器中 /// /// 要注册的实例类型 /// 要注册的实例对象 @@ -206,8 +206,8 @@ public class IocContainer : ContextAwareBase, IIocContainer #region Get /// - /// 获取单个实例(通常用于具体类型) - /// 如果存在多个,只返回第一个 + /// 获取单个实例(通常用于具体类型) + /// 如果存在多个,只返回第一个 /// /// 期望获取的实例类型 /// 找到的第一个实例;如果未找到则返回 null @@ -234,7 +234,7 @@ public class IocContainer : ContextAwareBase, IIocContainer /// - /// 获取指定类型的必需实例 + /// 获取指定类型的必需实例 /// /// 期望获取的实例类型 /// 找到的唯一实例 @@ -262,7 +262,7 @@ public class IocContainer : ContextAwareBase, IIocContainer } /// - /// 获取指定类型的所有实例(接口 / 抽象类推荐使用) + /// 获取指定类型的所有实例(接口 / 抽象类推荐使用) /// /// 期望获取的实例类型 /// 所有符合条件的实例列表;如果没有则返回空数组 @@ -283,7 +283,7 @@ public class IocContainer : ContextAwareBase, IIocContainer /// - /// 获取并排序(系统调度专用) + /// 获取并排序(系统调度专用) /// /// 期望获取的实例类型 /// 比较器委托,定义排序规则 @@ -301,7 +301,7 @@ public class IocContainer : ContextAwareBase, IIocContainer #region Utility /// - /// 检查容器中是否包含指定类型的实例 + /// 检查容器中是否包含指定类型的实例 /// /// 要检查的类型 /// 如果容器中包含指定类型的实例则返回true,否则返回false @@ -319,7 +319,7 @@ public class IocContainer : ContextAwareBase, IIocContainer } /// - /// 清空容器中的所有实例 + /// 清空容器中的所有实例 /// public void Clear() { @@ -336,7 +336,7 @@ public class IocContainer : ContextAwareBase, IIocContainer } /// - /// 判断容器中是否包含某个具体的实例对象 + /// 判断容器中是否包含某个具体的实例对象 /// /// 待查询的实例对象 /// 若容器中包含该实例则返回true,否则返回false @@ -355,7 +355,7 @@ public class IocContainer : ContextAwareBase, IIocContainer /// - /// 冻结容器,防止后续修改 + /// 冻结容器,防止后续修改 /// public void Freeze() { diff --git a/GFramework.Core/logging/AbstractLogger.cs b/GFramework.Core/logging/AbstractLogger.cs index 336610d..622b749 100644 --- a/GFramework.Core/logging/AbstractLogger.cs +++ b/GFramework.Core/logging/AbstractLogger.cs @@ -1,8 +1,8 @@ namespace GFramework.Core.logging; /// -/// 日志抽象基类,封装日志级别判断、格式化与异常处理逻辑。 -/// 平台日志器只需实现 Write 方法即可。 +/// 日志抽象基类,封装日志级别判断、格式化与异常处理逻辑。 +/// 平台日志器只需实现 Write 方法即可。 /// public abstract class AbstractLogger( string? name = null, @@ -13,298 +13,420 @@ public abstract class AbstractLogger( #region Metadata /// - /// 获取日志器的名称 + /// 获取日志器的名称 /// /// 日志器名称 - public string Name() => _name; + public string Name() + { + return _name; + } #endregion + /// + /// 平台输出入口,由具体实现负责真正的日志写入。 + /// + /// 日志级别 + /// 日志消息 + /// 异常对象(可为null) + protected abstract void Write(LogLevel level, string message, Exception? exception); + #region Level Checks /// - /// 判断指定日志级别是否启用 + /// 判断指定日志级别是否启用 /// /// 要检查的日志级别 /// 如果指定级别大于等于最小级别则返回true,否则返回false - protected bool IsEnabled(LogLevel level) => level >= minLevel; + protected bool IsEnabled(LogLevel level) + { + return level >= minLevel; + } /// - /// 检查Trace级别日志是否启用 + /// 检查Trace级别日志是否启用 /// /// 如果Trace级别启用返回true,否则返回false - public bool IsTraceEnabled() => IsEnabled(LogLevel.Trace); - + public bool IsTraceEnabled() + { + return IsEnabled(LogLevel.Trace); + } + /// - /// 检查Debug级别日志是否启用 + /// 检查Debug级别日志是否启用 /// /// 如果Debug级别启用返回true,否则返回false - public bool IsDebugEnabled() => IsEnabled(LogLevel.Debug); - + public bool IsDebugEnabled() + { + return IsEnabled(LogLevel.Debug); + } + /// - /// 检查Info级别日志是否启用 + /// 检查Info级别日志是否启用 /// /// 如果Info级别启用返回true,否则返回false - public bool IsInfoEnabled() => IsEnabled(LogLevel.Info); - + public bool IsInfoEnabled() + { + return IsEnabled(LogLevel.Info); + } + /// - /// 检查Warning级别日志是否启用 + /// 检查Warning级别日志是否启用 /// /// 如果Warning级别启用返回true,否则返回false - public bool IsWarnEnabled() => IsEnabled(LogLevel.Warning); - + public bool IsWarnEnabled() + { + return IsEnabled(LogLevel.Warning); + } + /// - /// 检查Error级别日志是否启用 + /// 检查Error级别日志是否启用 /// /// 如果Error级别启用返回true,否则返回false - public bool IsErrorEnabled() => IsEnabled(LogLevel.Error); - + public bool IsErrorEnabled() + { + return IsEnabled(LogLevel.Error); + } + /// - /// 检查Fatal级别日志是否启用 + /// 检查Fatal级别日志是否启用 /// /// 如果Fatal级别启用返回true,否则返回false - public bool IsFatalEnabled() => IsEnabled(LogLevel.Fatal); + public bool IsFatalEnabled() + { + return IsEnabled(LogLevel.Fatal); + } #endregion #region Trace /// - /// 记录Trace级别日志 + /// 记录Trace级别日志 /// /// 日志消息 - public void Trace(string msg) => Log(LogLevel.Trace, msg); - + public void Trace(string msg) + { + Log(LogLevel.Trace, msg); + } + /// - /// 记录Trace级别日志(带格式化参数) + /// 记录Trace级别日志(带格式化参数) /// /// 格式化字符串 /// 格式化参数 - public void Trace(string format, object arg) => Log(LogLevel.Trace, format, arg); - + public void Trace(string format, object arg) + { + Log(LogLevel.Trace, format, arg); + } + /// - /// 记录Trace级别日志(带两个格式化参数) + /// 记录Trace级别日志(带两个格式化参数) /// /// 格式化字符串 /// 第一个格式化参数 /// 第二个格式化参数 - public void Trace(string format, object arg1, object arg2) => Log(LogLevel.Trace, format, arg1, arg2); - + public void Trace(string format, object arg1, object arg2) + { + Log(LogLevel.Trace, format, arg1, arg2); + } + /// - /// 记录Trace级别日志(带多个格式化参数) + /// 记录Trace级别日志(带多个格式化参数) /// /// 格式化字符串 /// 格式化参数数组 - public void Trace(string format, params object[] arguments) => Log(LogLevel.Trace, format, arguments); - + public void Trace(string format, params object[] arguments) + { + Log(LogLevel.Trace, format, arguments); + } + /// - /// 记录Trace级别日志(带异常信息) + /// 记录Trace级别日志(带异常信息) /// /// 日志消息 /// 异常对象 - public void Trace(string msg, Exception t) => Log(LogLevel.Trace, msg, t); + public void Trace(string msg, Exception t) + { + Log(LogLevel.Trace, msg, t); + } #endregion #region Debug /// - /// 记录Debug级别日志 + /// 记录Debug级别日志 /// /// 日志消息 - public void Debug(string msg) => Log(LogLevel.Debug, msg); - + public void Debug(string msg) + { + Log(LogLevel.Debug, msg); + } + /// - /// 记录Debug级别日志(带格式化参数) + /// 记录Debug级别日志(带格式化参数) /// /// 格式化字符串 /// 格式化参数 - public void Debug(string format, object arg) => Log(LogLevel.Debug, format, arg); - + public void Debug(string format, object arg) + { + Log(LogLevel.Debug, format, arg); + } + /// - /// 记录Debug级别日志(带两个格式化参数) + /// 记录Debug级别日志(带两个格式化参数) /// /// 格式化字符串 /// 第一个格式化参数 /// 第二个格式化参数 - public void Debug(string format, object arg1, object arg2) => Log(LogLevel.Debug, format, arg1, arg2); - + public void Debug(string format, object arg1, object arg2) + { + Log(LogLevel.Debug, format, arg1, arg2); + } + /// - /// 记录Debug级别日志(带多个格式化参数) + /// 记录Debug级别日志(带多个格式化参数) /// /// 格式化字符串 /// 格式化参数数组 - public void Debug(string format, params object[] arguments) => Log(LogLevel.Debug, format, arguments); - + public void Debug(string format, params object[] arguments) + { + Log(LogLevel.Debug, format, arguments); + } + /// - /// 记录Debug级别日志(带异常信息) + /// 记录Debug级别日志(带异常信息) /// /// 日志消息 /// 异常对象 - public void Debug(string msg, Exception t) => Log(LogLevel.Debug, msg, t); + public void Debug(string msg, Exception t) + { + Log(LogLevel.Debug, msg, t); + } #endregion #region Info /// - /// 记录Info级别日志 + /// 记录Info级别日志 /// /// 日志消息 - public void Info(string msg) => Log(LogLevel.Info, msg); - + public void Info(string msg) + { + Log(LogLevel.Info, msg); + } + /// - /// 记录Info级别日志(带格式化参数) + /// 记录Info级别日志(带格式化参数) /// /// 格式化字符串 /// 格式化参数 - public void Info(string format, object arg) => Log(LogLevel.Info, format, arg); - + public void Info(string format, object arg) + { + Log(LogLevel.Info, format, arg); + } + /// - /// 记录Info级别日志(带两个格式化参数) + /// 记录Info级别日志(带两个格式化参数) /// /// 格式化字符串 /// 第一个格式化参数 /// 第二个格式化参数 - public void Info(string format, object arg1, object arg2) => Log(LogLevel.Info, format, arg1, arg2); - + public void Info(string format, object arg1, object arg2) + { + Log(LogLevel.Info, format, arg1, arg2); + } + /// - /// 记录Info级别日志(带多个格式化参数) + /// 记录Info级别日志(带多个格式化参数) /// /// 格式化字符串 /// 格式化参数数组 - public void Info(string format, params object[] arguments) => Log(LogLevel.Info, format, arguments); - + public void Info(string format, params object[] arguments) + { + Log(LogLevel.Info, format, arguments); + } + /// - /// 记录Info级别日志(带异常信息) + /// 记录Info级别日志(带异常信息) /// /// 日志消息 /// 异常对象 - public void Info(string msg, Exception t) => Log(LogLevel.Info, msg, t); + public void Info(string msg, Exception t) + { + Log(LogLevel.Info, msg, t); + } #endregion #region Warn /// - /// 记录Warning级别日志 + /// 记录Warning级别日志 /// /// 日志消息 - public void Warn(string msg) => Log(LogLevel.Warning, msg); - + public void Warn(string msg) + { + Log(LogLevel.Warning, msg); + } + /// - /// 记录Warning级别日志(带格式化参数) + /// 记录Warning级别日志(带格式化参数) /// /// 格式化字符串 /// 格式化参数 - public void Warn(string format, object arg) => Log(LogLevel.Warning, format, arg); - + public void Warn(string format, object arg) + { + Log(LogLevel.Warning, format, arg); + } + /// - /// 记录Warning级别日志(带两个格式化参数) + /// 记录Warning级别日志(带两个格式化参数) /// /// 格式化字符串 /// 第一个格式化参数 /// 第二个格式化参数 - public void Warn(string format, object arg1, object arg2) => Log(LogLevel.Warning, format, arg1, arg2); - + public void Warn(string format, object arg1, object arg2) + { + Log(LogLevel.Warning, format, arg1, arg2); + } + /// - /// 记录Warning级别日志(带多个格式化参数) + /// 记录Warning级别日志(带多个格式化参数) /// /// 格式化字符串 /// 格式化参数数组 - public void Warn(string format, params object[] arguments) => Log(LogLevel.Warning, format, arguments); - + public void Warn(string format, params object[] arguments) + { + Log(LogLevel.Warning, format, arguments); + } + /// - /// 记录Warning级别日志(带异常信息) + /// 记录Warning级别日志(带异常信息) /// /// 日志消息 /// 异常对象 - public void Warn(string msg, Exception t) => Log(LogLevel.Warning, msg, t); + public void Warn(string msg, Exception t) + { + Log(LogLevel.Warning, msg, t); + } #endregion #region Error /// - /// 记录Error级别日志 + /// 记录Error级别日志 /// /// 日志消息 - public void Error(string msg) => Log(LogLevel.Error, msg); - + public void Error(string msg) + { + Log(LogLevel.Error, msg); + } + /// - /// 记录Error级别日志(带格式化参数) + /// 记录Error级别日志(带格式化参数) /// /// 格式化字符串 /// 格式化参数 - public void Error(string format, object arg) => Log(LogLevel.Error, format, arg); - + public void Error(string format, object arg) + { + Log(LogLevel.Error, format, arg); + } + /// - /// 记录Error级别日志(带两个格式化参数) + /// 记录Error级别日志(带两个格式化参数) /// /// 格式化字符串 /// 第一个格式化参数 /// 第二个格式化参数 - public void Error(string format, object arg1, object arg2) => Log(LogLevel.Error, format, arg1, arg2); - + public void Error(string format, object arg1, object arg2) + { + Log(LogLevel.Error, format, arg1, arg2); + } + /// - /// 记录Error级别日志(带多个格式化参数) + /// 记录Error级别日志(带多个格式化参数) /// /// 格式化字符串 /// 格式化参数数组 - public void Error(string format, params object[] arguments) => Log(LogLevel.Error, format, arguments); - + public void Error(string format, params object[] arguments) + { + Log(LogLevel.Error, format, arguments); + } + /// - /// 记录Error级别日志(带异常信息) + /// 记录Error级别日志(带异常信息) /// /// 日志消息 /// 异常对象 - public void Error(string msg, Exception t) => Log(LogLevel.Error, msg, t); + public void Error(string msg, Exception t) + { + Log(LogLevel.Error, msg, t); + } #endregion #region Fatal /// - /// 记录Fatal级别日志 + /// 记录Fatal级别日志 /// /// 日志消息 - public void Fatal(string msg) => Log(LogLevel.Fatal, msg); - + public void Fatal(string msg) + { + Log(LogLevel.Fatal, msg); + } + /// - /// 记录Fatal级别日志(带格式化参数) + /// 记录Fatal级别日志(带格式化参数) /// /// 格式化字符串 /// 格式化参数 - public void Fatal(string format, object arg) => Log(LogLevel.Fatal, format, arg); - + public void Fatal(string format, object arg) + { + Log(LogLevel.Fatal, format, arg); + } + /// - /// 记录Fatal级别日志(带两个格式化参数) + /// 记录Fatal级别日志(带两个格式化参数) /// /// 格式化字符串 /// 第一个格式化参数 /// 第二个格式化参数 - public void Fatal(string format, object arg1, object arg2) => Log(LogLevel.Fatal, format, arg1, arg2); - + public void Fatal(string format, object arg1, object arg2) + { + Log(LogLevel.Fatal, format, arg1, arg2); + } + /// - /// 记录Fatal级别日志(带多个格式化参数) + /// 记录Fatal级别日志(带多个格式化参数) /// /// 格式化字符串 /// 格式化参数数组 - public void Fatal(string format, params object[] arguments) => Log(LogLevel.Fatal, format, arguments); - + public void Fatal(string format, params object[] arguments) + { + Log(LogLevel.Fatal, format, arguments); + } + /// - /// 记录Fatal级别日志(带异常信息) + /// 记录Fatal级别日志(带异常信息) /// /// 日志消息 /// 异常对象 - public void Fatal(string msg, Exception t) => Log(LogLevel.Fatal, msg, t); + public void Fatal(string msg, Exception t) + { + Log(LogLevel.Fatal, msg, t); + } #endregion #region Core Pipeline /// - /// 核心日志记录方法(无参数) + /// 核心日志记录方法(无参数) /// /// 日志级别 /// 日志消息 @@ -315,7 +437,7 @@ public abstract class AbstractLogger( } /// - /// 核心日志记录方法(带参数格式化) + /// 核心日志记录方法(带参数格式化) /// /// 日志级别 /// 格式化字符串 @@ -327,7 +449,7 @@ public abstract class AbstractLogger( } /// - /// 核心日志记录方法(带异常) + /// 核心日志记录方法(带异常) /// /// 日志级别 /// 日志消息 @@ -339,12 +461,4 @@ public abstract class AbstractLogger( } #endregion - - /// - /// 平台输出入口,由具体实现负责真正的日志写入。 - /// - /// 日志级别 - /// 日志消息 - /// 异常对象(可为null) - protected abstract void Write(LogLevel level, string message, Exception? exception); -} +} \ No newline at end of file diff --git a/GFramework.Core/logging/ConsoleLogger.cs b/GFramework.Core/logging/ConsoleLogger.cs index d1e58bf..b457f23 100644 --- a/GFramework.Core/logging/ConsoleLogger.cs +++ b/GFramework.Core/logging/ConsoleLogger.cs @@ -1,7 +1,7 @@ namespace GFramework.Core.logging; /// -/// 控制台日志记录器 +/// 控制台日志记录器 /// public sealed class ConsoleLogger( string? name = null, @@ -9,11 +9,11 @@ public sealed class ConsoleLogger( TextWriter? writer = null, bool useColors = true) : AbstractLogger(name ?? ILogger.RootLoggerName, minLevel) { - private readonly TextWriter _writer = writer ?? Console.Out; private readonly bool _useColors = useColors && writer == Console.Out; - + private readonly TextWriter _writer = writer ?? Console.Out; + /// - /// 写入日志消息到控制台 + /// 写入日志消息到控制台 /// /// 日志级别 /// 日志消息 @@ -25,24 +25,18 @@ public sealed class ConsoleLogger( var log = $"[{timestamp}] {levelStr} [{Name()}] {message}"; // 添加异常信息到日志 - if (exception != null) - { - log += Environment.NewLine + exception; - } + if (exception != null) log += Environment.NewLine + exception; if (_useColors) - { WriteColored(level, log); - } else - { _writer.WriteLine(log); - } } - + #region Internal Core + /// - /// 以指定颜色写入日志消息 + /// 以指定颜色写入日志消息 /// /// 日志级别 /// 日志消息 @@ -61,20 +55,23 @@ public sealed class ConsoleLogger( } /// - /// 根据日志级别获取对应的颜色 + /// 根据日志级别获取对应的颜色 /// /// 日志级别 /// 控制台颜色 - private static ConsoleColor GetColor(LogLevel level) => level switch + private static ConsoleColor GetColor(LogLevel level) { - LogLevel.Trace => ConsoleColor.DarkGray, - LogLevel.Debug => ConsoleColor.Cyan, - LogLevel.Info => ConsoleColor.White, - LogLevel.Warning => ConsoleColor.Yellow, - LogLevel.Error => ConsoleColor.Red, - LogLevel.Fatal => ConsoleColor.Magenta, - _ => ConsoleColor.White - }; + return level switch + { + LogLevel.Trace => ConsoleColor.DarkGray, + LogLevel.Debug => ConsoleColor.Cyan, + LogLevel.Info => ConsoleColor.White, + LogLevel.Warning => ConsoleColor.Yellow, + LogLevel.Error => ConsoleColor.Red, + LogLevel.Fatal => ConsoleColor.Magenta, + _ => ConsoleColor.White + }; + } #endregion -} +} \ No newline at end of file diff --git a/GFramework.Core/logging/ConsoleLoggerFactory.cs b/GFramework.Core/logging/ConsoleLoggerFactory.cs index ec0b8a6..770461c 100644 --- a/GFramework.Core/logging/ConsoleLoggerFactory.cs +++ b/GFramework.Core/logging/ConsoleLoggerFactory.cs @@ -1,12 +1,12 @@ namespace GFramework.Core.logging; /// -/// 控制台日志提供程序,用于创建控制台日志记录器实例 +/// 控制台日志提供程序,用于创建控制台日志记录器实例 /// public class ConsoleLoggerFactory : ILoggerFactory { /// - /// 获取指定名称的控制台日志记录器实例 + /// 获取指定名称的控制台日志记录器实例 /// /// 日志记录器的名称 /// 控制台日志记录器实例 @@ -14,4 +14,4 @@ public class ConsoleLoggerFactory : ILoggerFactory { return new ConsoleLogger(name); } -} +} \ No newline at end of file diff --git a/GFramework.Core/logging/ILogger.cs b/GFramework.Core/logging/ILogger.cs index 55b573e..d0d91fe 100644 --- a/GFramework.Core/logging/ILogger.cs +++ b/GFramework.Core/logging/ILogger.cs @@ -1,61 +1,61 @@ namespace GFramework.Core.logging; /// -/// 定义日志记录接口,提供日志记录和级别检查功能 +/// 定义日志记录接口,提供日志记录和级别检查功能 /// public interface ILogger { /// - /// 根日志记录器的名称常量 + /// 根日志记录器的名称常量 /// public const string RootLoggerName = "ROOT"; - + /// - /// 获取日志记录器的名称 + /// 获取日志记录器的名称 /// /// 日志记录器的名称 string Name(); - + #region Level Enabled Check /// - /// 检查是否启用了Trace级别日志 + /// 检查是否启用了Trace级别日志 /// /// 如果启用了Trace级别日志则返回true,否则返回false bool IsTraceEnabled(); - + /// - /// 检查是否启用了Debug级别日志 + /// 检查是否启用了Debug级别日志 /// /// 如果启用了Debug级别日志则返回true,否则返回false bool IsDebugEnabled(); - + /// - /// 检查是否启用了Info级别日志 + /// 检查是否启用了Info级别日志 /// /// 如果启用了Info级别日志则返回true,否则返回false bool IsInfoEnabled(); - + /// - /// 检查是否启用了Warn级别日志 + /// 检查是否启用了Warn级别日志 /// /// 如果启用了Warn级别日志则返回true,否则返回false bool IsWarnEnabled(); - + /// - /// 检查是否启用了Error级别日志 + /// 检查是否启用了Error级别日志 /// /// 如果启用了Error级别日志则返回true,否则返回false bool IsErrorEnabled(); - + /// - /// 检查是否启用了Fatal级别日志 + /// 检查是否启用了Fatal级别日志 /// /// 如果启用了Fatal级别日志则返回true,否则返回false bool IsFatalEnabled(); /// - /// 检查指定的日志级别是否已启用 + /// 检查指定的日志级别是否已启用 /// /// 要检查的日志级别 /// 如果指定的日志级别已启用则返回true,否则返回false @@ -74,26 +74,26 @@ public interface ILogger } #endregion - + #region Trace Logging Methods /// - /// 记录 TRACE 级别的消息 + /// 记录 TRACE 级别的消息 /// /// 要记录的消息字符串 void Trace(string msg); /// - /// 根据指定格式和参数记录 TRACE 级别的消息 - /// 当日志记录器对 TRACE 级别禁用时,此方法可避免不必要的对象创建 + /// 根据指定格式和参数记录 TRACE 级别的消息 + /// 当日志记录器对 TRACE 级别禁用时,此方法可避免不必要的对象创建 /// /// 格式字符串 /// 参数 void Trace(string format, object arg); /// - /// 根据指定格式和参数记录 TRACE 级别的消息 - /// 当日志记录器对 TRACE 级别禁用时,此方法可避免不必要的对象创建 + /// 根据指定格式和参数记录 TRACE 级别的消息 + /// 当日志记录器对 TRACE 级别禁用时,此方法可避免不必要的对象创建 /// /// 格式字符串 /// 第一个参数 @@ -101,15 +101,15 @@ public interface ILogger void Trace(string format, object arg1, object arg2); /// - /// 根据指定格式和参数数组记录 TRACE 级别的消息 - /// 当日志记录器对 TRACE 级别禁用时,此方法可避免不必要的字符串连接 + /// 根据指定格式和参数数组记录 TRACE 级别的消息 + /// 当日志记录器对 TRACE 级别禁用时,此方法可避免不必要的字符串连接 /// /// 格式字符串 /// 参数数组 void Trace(string format, params object[] arguments); /// - /// 使用伴随消息在 TRACE 级别记录异常 + /// 使用伴随消息在 TRACE 级别记录异常 /// /// 伴随异常的消息 /// 要记录的异常 @@ -120,22 +120,22 @@ public interface ILogger #region Debug Logging Methods /// - /// 记录 DEBUG 级别的消息 + /// 记录 DEBUG 级别的消息 /// /// 要记录的消息字符串 void Debug(string msg); /// - /// 根据指定格式和参数记录 DEBUG 级别的消息 - /// 当日志记录器对 DEBUG 级别禁用时,此方法可避免不必要的对象创建 + /// 根据指定格式和参数记录 DEBUG 级别的消息 + /// 当日志记录器对 DEBUG 级别禁用时,此方法可避免不必要的对象创建 /// /// 格式字符串 /// 参数 void Debug(string format, object arg); /// - /// 根据指定格式和参数记录 DEBUG 级别的消息 - /// 当日志记录器对 DEBUG 级别禁用时,此方法可避免不必要的对象创建 + /// 根据指定格式和参数记录 DEBUG 级别的消息 + /// 当日志记录器对 DEBUG 级别禁用时,此方法可避免不必要的对象创建 /// /// 格式字符串 /// 第一个参数 @@ -143,15 +143,15 @@ public interface ILogger void Debug(string format, object arg1, object arg2); /// - /// 根据指定格式和参数数组记录 DEBUG 级别的消息 - /// 当日志记录器对 DEBUG 级别禁用时,此方法可避免不必要的字符串连接 + /// 根据指定格式和参数数组记录 DEBUG 级别的消息 + /// 当日志记录器对 DEBUG 级别禁用时,此方法可避免不必要的字符串连接 /// /// 格式字符串 /// 参数数组 void Debug(string format, params object[] arguments); /// - /// 使用伴随消息在 DEBUG 级别记录异常 + /// 使用伴随消息在 DEBUG 级别记录异常 /// /// 伴随异常的消息 /// 要记录的异常 @@ -162,22 +162,22 @@ public interface ILogger #region Info Logging Methods /// - /// 记录 INFO 级别的消息 + /// 记录 INFO 级别的消息 /// /// 要记录的消息字符串 void Info(string msg); /// - /// 根据指定格式和参数记录 INFO 级别的消息 - /// 当日志记录器对 INFO 级别禁用时,此方法可避免不必要的对象创建 + /// 根据指定格式和参数记录 INFO 级别的消息 + /// 当日志记录器对 INFO 级别禁用时,此方法可避免不必要的对象创建 /// /// 格式字符串 /// 参数 void Info(string format, object arg); /// - /// 根据指定格式和参数记录 INFO 级别的消息 - /// 当日志记录器对 INFO 级别禁用时,此方法可避免不必要的对象创建 + /// 根据指定格式和参数记录 INFO 级别的消息 + /// 当日志记录器对 INFO 级别禁用时,此方法可避免不必要的对象创建 /// /// 格式字符串 /// 第一个参数 @@ -185,15 +185,15 @@ public interface ILogger void Info(string format, object arg1, object arg2); /// - /// 根据指定格式和参数数组记录 INFO 级别的消息 - /// 当日志记录器对 INFO 级别禁用时,此方法可避免不必要的字符串连接 + /// 根据指定格式和参数数组记录 INFO 级别的消息 + /// 当日志记录器对 INFO 级别禁用时,此方法可避免不必要的字符串连接 /// /// 格式字符串 /// 参数数组 void Info(string format, params object[] arguments); /// - /// 使用伴随消息在 INFO 级别记录异常 + /// 使用伴随消息在 INFO 级别记录异常 /// /// 伴随异常的消息 /// 要记录的异常 @@ -204,22 +204,22 @@ public interface ILogger #region Warn Logging Methods /// - /// 记录 WARN 级别的消息 + /// 记录 WARN 级别的消息 /// /// 要记录的消息字符串 void Warn(string msg); /// - /// 根据指定格式和参数记录 WARN 级别的消息 - /// 当日志记录器对 WARN 级别禁用时,此方法可避免不必要的对象创建 + /// 根据指定格式和参数记录 WARN 级别的消息 + /// 当日志记录器对 WARN 级别禁用时,此方法可避免不必要的对象创建 /// /// 格式字符串 /// 参数 void Warn(string format, object arg); /// - /// 根据指定格式和参数记录 WARN 级别的消息 - /// 当日志记录器对 WARN 级别禁用时,此方法可避免不必要的对象创建 + /// 根据指定格式和参数记录 WARN 级别的消息 + /// 当日志记录器对 WARN 级别禁用时,此方法可避免不必要的对象创建 /// /// 格式字符串 /// 第一个参数 @@ -227,15 +227,15 @@ public interface ILogger void Warn(string format, object arg1, object arg2); /// - /// 根据指定格式和参数数组记录 WARN 级别的消息 - /// 当日志记录器对 WARN 级别禁用时,此方法可避免不必要的字符串连接 + /// 根据指定格式和参数数组记录 WARN 级别的消息 + /// 当日志记录器对 WARN 级别禁用时,此方法可避免不必要的字符串连接 /// /// 格式字符串 /// 参数数组 void Warn(string format, params object[] arguments); /// - /// 使用伴随消息在 WARN 级别记录异常 + /// 使用伴随消息在 WARN 级别记录异常 /// /// 伴随异常的消息 /// 要记录的异常 @@ -246,22 +246,22 @@ public interface ILogger #region Error Logging Methods /// - /// 记录 ERROR 级别的消息 + /// 记录 ERROR 级别的消息 /// /// 要记录的消息字符串 void Error(string msg); /// - /// 根据指定格式和参数记录 ERROR 级别的消息 - /// 当日志记录器对 ERROR 级别禁用时,此方法可避免不必要的对象创建 + /// 根据指定格式和参数记录 ERROR 级别的消息 + /// 当日志记录器对 ERROR 级别禁用时,此方法可避免不必要的对象创建 /// /// 格式字符串 /// 参数 void Error(string format, object arg); /// - /// 根据指定格式和参数记录 ERROR 级别的消息 - /// 当日志记录器对 ERROR 级别禁用时,此方法可避免不必要的对象创建 + /// 根据指定格式和参数记录 ERROR 级别的消息 + /// 当日志记录器对 ERROR 级别禁用时,此方法可避免不必要的对象创建 /// /// 格式字符串 /// 第一个参数 @@ -269,15 +269,15 @@ public interface ILogger void Error(string format, object arg1, object arg2); /// - /// 根据指定格式和参数数组记录 ERROR 级别的消息 - /// 当日志记录器对 ERROR 级别禁用时,此方法可避免不必要的字符串连接 + /// 根据指定格式和参数数组记录 ERROR 级别的消息 + /// 当日志记录器对 ERROR 级别禁用时,此方法可避免不必要的字符串连接 /// /// 格式字符串 /// 参数数组 void Error(string format, params object[] arguments); /// - /// 使用伴随消息在 ERROR 级别记录异常 + /// 使用伴随消息在 ERROR 级别记录异常 /// /// 伴随异常的消息 /// 要记录的异常 @@ -288,22 +288,22 @@ public interface ILogger #region Fatal Logging Methods /// - /// 记录 FATAL 级别的消息 + /// 记录 FATAL 级别的消息 /// /// 要记录的消息字符串 void Fatal(string msg); /// - /// 根据指定格式和参数记录 FATAL 级别的消息 - /// 当日志记录器对 FATAL 级别禁用时,此方法可避免不必要的对象创建 + /// 根据指定格式和参数记录 FATAL 级别的消息 + /// 当日志记录器对 FATAL 级别禁用时,此方法可避免不必要的对象创建 /// /// 格式字符串 /// 参数 void Fatal(string format, object arg); /// - /// 根据指定格式和参数记录 FATAL 级别的消息 - /// 当日志记录器对 FATAL 级别禁用时,此方法可避免不必要的对象创建 + /// 根据指定格式和参数记录 FATAL 级别的消息 + /// 当日志记录器对 FATAL 级别禁用时,此方法可避免不必要的对象创建 /// /// 格式字符串 /// 第一个参数 @@ -311,19 +311,19 @@ public interface ILogger void Fatal(string format, object arg1, object arg2); /// - /// 根据指定格式和参数数组记录 FATAL 级别的消息 - /// 当日志记录器对 FATAL 级别禁用时,此方法可避免不必要的字符串连接 + /// 根据指定格式和参数数组记录 FATAL 级别的消息 + /// 当日志记录器对 FATAL 级别禁用时,此方法可避免不必要的字符串连接 /// /// 格式字符串 /// 参数数组 void Fatal(string format, params object[] arguments); /// - /// 使用伴随消息在 FATAL 级别记录异常 + /// 使用伴随消息在 FATAL 级别记录异常 /// /// 伴随异常的消息 /// 要记录的异常 void Fatal(string msg, Exception t); #endregion -} +} \ No newline at end of file diff --git a/GFramework.Core/logging/ILoggerFactory.cs b/GFramework.Core/logging/ILoggerFactory.cs index a347016..b4f2179 100644 --- a/GFramework.Core/logging/ILoggerFactory.cs +++ b/GFramework.Core/logging/ILoggerFactory.cs @@ -1,12 +1,12 @@ namespace GFramework.Core.logging; /// -/// 定义日志工厂接口,用于创建日志记录器实例 +/// 定义日志工厂接口,用于创建日志记录器实例 /// public interface ILoggerFactory { /// - /// 根据指定的名称获取日志记录器实例 + /// 根据指定的名称获取日志记录器实例 /// /// 日志记录器的名称 /// 指定名称的日志记录器实例 diff --git a/GFramework.Core/logging/LogLevel.cs b/GFramework.Core/logging/LogLevel.cs index a9469a6..1bced4e 100644 --- a/GFramework.Core/logging/LogLevel.cs +++ b/GFramework.Core/logging/LogLevel.cs @@ -1,32 +1,37 @@ namespace GFramework.Core.logging; /// -/// 定义日志级别的枚举,用于标识不同严重程度的日志消息 +/// 定义日志级别的枚举,用于标识不同严重程度的日志消息 /// public enum LogLevel { /// - /// 跟踪级别,用于详细的程序执行流程信息 + /// 跟踪级别,用于详细的程序执行流程信息 /// Trace, + /// - /// 调试级别,用于调试过程中的详细信息 + /// 调试级别,用于调试过程中的详细信息 /// Debug, + /// - /// 信息级别,用于一般性的程序运行信息 + /// 信息级别,用于一般性的程序运行信息 /// Info, + /// - /// 警告级别,用于表示可能的问题或异常情况 + /// 警告级别,用于表示可能的问题或异常情况 /// Warning, + /// - /// 错误级别,用于表示错误但程序仍可继续运行的情况 + /// 错误级别,用于表示错误但程序仍可继续运行的情况 /// Error, + /// - /// 致命级别,用于表示严重的错误导致程序无法继续运行 + /// 致命级别,用于表示严重的错误导致程序无法继续运行 /// Fatal -} +} \ No newline at end of file diff --git a/GFramework.Core/logging/NoopLogger.cs b/GFramework.Core/logging/NoopLogger.cs index bc6e2f0..2767fe6 100644 --- a/GFramework.Core/logging/NoopLogger.cs +++ b/GFramework.Core/logging/NoopLogger.cs @@ -1,7 +1,7 @@ namespace GFramework.Core.logging; /// -/// 空操作日志记录器实现,不执行任何实际的日志记录操作 +/// 空操作日志记录器实现,不执行任何实际的日志记录操作 /// /// 日志记录器名称,默认为null /// 最小日志级别,默认为Info @@ -10,13 +10,12 @@ internal sealed class NoopLogger( LogLevel minLevel = LogLevel.Info) : AbstractLogger(name ?? ILogger.RootLoggerName, minLevel) { /// - /// 重写写入方法,空操作实现,不执行任何实际的日志记录操作 + /// 重写写入方法,空操作实现,不执行任何实际的日志记录操作 /// /// 日志级别 /// 日志消息 /// 异常信息 protected override void Write(LogLevel level, string message, Exception? exception) { - } -} +} \ No newline at end of file diff --git a/GFramework.Core/logging/NoopLoggerFactory.cs b/GFramework.Core/logging/NoopLoggerFactory.cs index 4119889..3b505b6 100644 --- a/GFramework.Core/logging/NoopLoggerFactory.cs +++ b/GFramework.Core/logging/NoopLoggerFactory.cs @@ -1,12 +1,12 @@ namespace GFramework.Core.logging; /// -/// 无操作日志工厂实现,用于提供空的日志记录功能 +/// 无操作日志工厂实现,用于提供空的日志记录功能 /// -public class NoopLoggerFactory: ILoggerFactory +public class NoopLoggerFactory : ILoggerFactory { /// - /// 获取指定名称的无操作日志记录器 + /// 获取指定名称的无操作日志记录器 /// /// 日志记录器的名称 /// 返回一个NoopLogger实例,该实例不执行任何实际的日志记录操作 diff --git a/GFramework.Core/logging/README.md b/GFramework.Core/logging/README.md index 981cc55..4cd1fac 100644 --- a/GFramework.Core/logging/README.md +++ b/GFramework.Core/logging/README.md @@ -2,7 +2,8 @@ ## 概述 -GFramework.Core 提供了一个灵活、可配置的日志系统,支持多级别、多类别和多种输出方式的日志记录。默认日志级别为 `Info`,确保框架的关键操作都能被记录下来。 +GFramework.Core 提供了一个灵活、可配置的日志系统,支持多级别、多类别和多种输出方式的日志记录。默认日志级别为 `Info` +,确保框架的关键操作都能被记录下来。 ## 主要特性 @@ -241,27 +242,32 @@ paymentLogger.Info("支付成功", new { OrderId = "ORD_789", TransactionId = "t GFramework.Core 在以下关键位置自动添加了日志记录: ### 架构模块 (Architecture) + - 架构初始化流程 - 组件注册和初始化 - 生命周期阶段变更 - 模块安装和卸载 ### IOC容器 (IOC) + - 对象注册和获取 - 容器冻结操作 - 重复注册检测 ### 事件系统 (Event) + - 事件发送和接收 - 事件处理器注册和注销 ### 系统模块 (System) + - 系统初始化和销毁 - 组件生命周期管理 ## 输出格式 ### 控制台输出示例 + ``` [2025-12-23 12:34:56.789] INFO Architecture Architecture initialized [2025-12-23 12:34:56.790] INFO Architecture Initializing 3 systems @@ -271,6 +277,7 @@ GFramework.Core 在以下关键位置自动添加了日志记录: ``` ### 日志输出级别颜色 + - **Trace**: 灰色 - **Debug**: 青色 - **Info**: 白色 @@ -281,11 +288,11 @@ GFramework.Core 在以下关键位置自动添加了日志记录: ## 最佳实践 1. **使用合适的日志级别**: - - 使用 `Info` 记录重要业务流程 - - 使用 `Debug` 记录调试信息 - - 使用 `Warning` 记录异常情况 - - 使用 `Error` 记录错误但不影响程序运行 - - 使用 `Fatal` 记录严重错误 + - 使用 `Info` 记录重要业务流程 + - 使用 `Debug` 记录调试信息 + - 使用 `Warning` 记录异常情况 + - 使用 `Error` 记录错误但不影响程序运行 + - 使用 `Fatal` 记录严重错误 2. **提供上下文信息**: ```csharp @@ -331,6 +338,7 @@ GFramework.Core 在以下关键位置自动添加了日志记录: ## 配置建议 ### 开发环境 + ```csharp Log.Configure( minLevel: LogLevel.Debug, @@ -341,6 +349,7 @@ Log.Configure( ``` ### 生产环境 + ```csharp var config = new LogConfig { diff --git a/GFramework.Core/model/AbstractModel.cs b/GFramework.Core/model/AbstractModel.cs index b34b932..9e9d9f0 100644 --- a/GFramework.Core/model/AbstractModel.cs +++ b/GFramework.Core/model/AbstractModel.cs @@ -12,6 +12,14 @@ public abstract class AbstractModel : IModel /// protected IArchitecture Architecture; + /// + /// 初始化模型,调用抽象方法OnInit执行具体初始化逻辑 + /// + void IModel.Init() + { + OnInit(); + } + /// /// 获取模型所属的架构实例 /// @@ -30,14 +38,6 @@ public abstract class AbstractModel : IModel Architecture = architecture; } - /// - /// 初始化模型,调用抽象方法OnInit执行具体初始化逻辑 - /// - void IModel.Init() - { - OnInit(); - } - /// /// 抽象初始化方法,由子类实现具体的初始化逻辑 /// diff --git a/GFramework.Core/model/IModel.cs b/GFramework.Core/model/IModel.cs index 045d23c..a83fd2a 100644 --- a/GFramework.Core/model/IModel.cs +++ b/GFramework.Core/model/IModel.cs @@ -1,8 +1,4 @@ -using GFramework.Core.events; -using GFramework.Core.rule; -using GFramework.Core.utility; - -namespace GFramework.Core.model; +namespace GFramework.Core.model; /// /// 模型接口,定义了模型的基本行为和功能 diff --git a/GFramework.Core/query/AbstractQuery.cs b/GFramework.Core/query/AbstractQuery.cs index 65bc706..efb6381 100644 --- a/GFramework.Core/query/AbstractQuery.cs +++ b/GFramework.Core/query/AbstractQuery.cs @@ -6,7 +6,7 @@ namespace GFramework.Core.query; /// 抽象查询类,提供查询操作的基础实现 /// /// 查询结果的类型 -public abstract class AbstractQuery :ContextAwareBase, IQuery +public abstract class AbstractQuery : ContextAwareBase, IQuery { /// /// 执行查询操作 @@ -16,7 +16,7 @@ public abstract class AbstractQuery :ContextAwareBase, IQuery { return OnDo(); } - + /// /// 抽象方法,由子类实现具体的查询逻辑 /// diff --git a/GFramework.Core/query/IQuery.cs b/GFramework.Core/query/IQuery.cs index 5d8417a..6439077 100644 --- a/GFramework.Core/query/IQuery.cs +++ b/GFramework.Core/query/IQuery.cs @@ -6,7 +6,7 @@ namespace GFramework.Core.query; /// 查询接口,定义了执行查询操作的契约 /// /// 查询结果的类型 -public interface IQuery:IContextAware +public interface IQuery : IContextAware { /// /// 执行查询操作并返回结果 diff --git a/GFramework.Core/rule/ContextAwareBase.cs b/GFramework.Core/rule/ContextAwareBase.cs index cd76290..696ddbf 100644 --- a/GFramework.Core/rule/ContextAwareBase.cs +++ b/GFramework.Core/rule/ContextAwareBase.cs @@ -3,17 +3,17 @@ namespace GFramework.Core.rule; /// -/// 上下文感知基类,实现了IContextAware接口,为需要感知架构上下文的类提供基础实现 +/// 上下文感知基类,实现了IContextAware接口,为需要感知架构上下文的类提供基础实现 /// public abstract class ContextAwareBase : IContextAware { /// - /// 获取当前实例的架构上下文 + /// 获取当前实例的架构上下文 /// protected IArchitectureContext Context { get; private set; } = null!; /// - /// 设置架构上下文的实现方法,由框架调用 + /// 设置架构上下文的实现方法,由框架调用 /// /// 要设置的架构上下文实例 void IContextAware.SetContext(IArchitectureContext context) @@ -23,7 +23,9 @@ public abstract class ContextAwareBase : IContextAware } /// - /// 当上下文准备就绪时调用的虚方法,子类可以重写此方法来执行上下文相关的初始化逻辑 + /// 当上下文准备就绪时调用的虚方法,子类可以重写此方法来执行上下文相关的初始化逻辑 /// - protected virtual void OnContextReady() { } -} + protected virtual void OnContextReady() + { + } +} \ No newline at end of file diff --git a/GFramework.Core/rule/IContextAware.cs b/GFramework.Core/rule/IContextAware.cs index c6319e6..d11140a 100644 --- a/GFramework.Core/rule/IContextAware.cs +++ b/GFramework.Core/rule/IContextAware.cs @@ -3,13 +3,13 @@ namespace GFramework.Core.rule; /// -/// 上下文感知接口,用于为实现该接口的类提供设置架构上下文的能力 +/// 上下文感知接口,用于为实现该接口的类提供设置架构上下文的能力 /// public interface IContextAware { /// - /// 设置架构上下文 + /// 设置架构上下文 /// /// 架构上下文对象,用于提供架构级别的服务和功能访问 void SetContext(IArchitectureContext context); -} +} \ No newline at end of file diff --git a/GFramework.Core/rule/ILogAware.cs b/GFramework.Core/rule/ILogAware.cs index dec2952..d52b6e6 100644 --- a/GFramework.Core/rule/ILogAware.cs +++ b/GFramework.Core/rule/ILogAware.cs @@ -3,13 +3,13 @@ namespace GFramework.Core.rule; /// -/// 定义一个支持日志记录的接口,允许实现类设置和使用日志记录器 +/// 定义一个支持日志记录的接口,允许实现类设置和使用日志记录器 /// public interface ILogAware { /// - /// 设置日志记录器 + /// 设置日志记录器 /// /// 要设置的ILogger实例 void SetLogger(ILogger logger); -} +} \ No newline at end of file diff --git a/GFramework.Core/system/ISystem.cs b/GFramework.Core/system/ISystem.cs index 739fd5e..c9261b0 100644 --- a/GFramework.Core/system/ISystem.cs +++ b/GFramework.Core/system/ISystem.cs @@ -13,7 +13,7 @@ public interface ISystem : IContextAware /// 在系统被创建后调用,用于执行系统的初始化逻辑 /// void Init(); - + /// /// 销毁系统 /// 在系统被销毁前调用,用于执行系统的资源清理逻辑 diff --git a/GFramework.Core/utility/AbstractContextUtility.cs b/GFramework.Core/utility/AbstractContextUtility.cs index 5033fa5..86fa4a3 100644 --- a/GFramework.Core/utility/AbstractContextUtility.cs +++ b/GFramework.Core/utility/AbstractContextUtility.cs @@ -4,15 +4,15 @@ using GFramework.Core.rule; namespace GFramework.Core.utility; /// -/// 抽象上下文工具类,提供上下文相关的通用功能实现 -/// 继承自ContextAwareBase并实现IContextUtility接口 +/// 抽象上下文工具类,提供上下文相关的通用功能实现 +/// 继承自ContextAwareBase并实现IContextUtility接口 /// public abstract class AbstractContextUtility : ContextAwareBase, IContextUtility { protected ILogger Logger = null !; /// - /// 初始化上下文工具类 + /// 初始化上下文工具类 /// void IContextUtility.Init() { diff --git a/GFramework.Core/utility/IContextUtility.cs b/GFramework.Core/utility/IContextUtility.cs index 7f597c9..34fdedb 100644 --- a/GFramework.Core/utility/IContextUtility.cs +++ b/GFramework.Core/utility/IContextUtility.cs @@ -3,14 +3,13 @@ namespace GFramework.Core.utility; /// -/// 上下文工具接口,继承自IUtility和IContextAware接口 -/// 提供具有上下文感知能力的工具功能 +/// 上下文工具接口,继承自IUtility和IContextAware接口 +/// 提供具有上下文感知能力的工具功能 /// -public interface IContextUtility:IUtility,IContextAware +public interface IContextUtility : IUtility, IContextAware { /// - /// 初始化上下文工具 + /// 初始化上下文工具 /// void Init(); -} - +} \ No newline at end of file diff --git a/GFramework.Game/GFramework.Game.csproj b/GFramework.Game/GFramework.Game.csproj index 271f741..629ae18 100644 --- a/GFramework.Game/GFramework.Game.csproj +++ b/GFramework.Game/GFramework.Game.csproj @@ -7,6 +7,6 @@ enable - + diff --git a/GFramework.Game/assets/AbstractAssetCatalogSystem.cs b/GFramework.Game/assets/AbstractAssetCatalogSystem.cs index f88a8ed..bac333d 100644 --- a/GFramework.Game/assets/AbstractAssetCatalogSystem.cs +++ b/GFramework.Game/assets/AbstractAssetCatalogSystem.cs @@ -3,20 +3,20 @@ using GFramework.Core.system; namespace GFramework.Game.assets; /// -/// 资源目录系统抽象基类,用于管理和注册游戏中的场景和资源。 -/// 提供了统一的接口来注册和查询不同类型的资产(如游戏单元、模板、普通资源)。 -/// 子类需要实现 方法以完成具体资产的注册逻辑。 +/// 资源目录系统抽象基类,用于管理和注册游戏中的场景和资源。 +/// 提供了统一的接口来注册和查询不同类型的资产(如游戏单元、模板、普通资源)。 +/// 子类需要实现 方法以完成具体资产的注册逻辑。 /// public abstract class AbstractAssetCatalogSystem : AbstractSystem, IAssetCatalogSystem { - private readonly Dictionary _sceneUnits = new(); - private readonly Dictionary _scenePages = new(); private readonly Dictionary _assets = new(); + private readonly Dictionary _scenePages = new(); + private readonly Dictionary _sceneUnits = new(); /// - /// 系统初始化时调用,用于触发资产注册流程。 - /// 此方法会调用抽象方法 ,由子类提供实际注册逻辑。 + /// 系统初始化时调用,用于触发资产注册流程。 + /// 此方法会调用抽象方法 ,由子类提供实际注册逻辑。 /// protected override void OnInit() { @@ -24,16 +24,15 @@ public abstract class AbstractAssetCatalogSystem : AbstractSystem, IAssetCatalog } /// - /// 抽象方法,必须在子类中重写。用于定义具体的资产注册逻辑。 - /// 在此方法中应通过调用各种 Register 方法将资产信息添加到系统中。 + /// 抽象方法,必须在子类中重写。用于定义具体的资产注册逻辑。 + /// 在此方法中应通过调用各种 Register 方法将资产信息添加到系统中。 /// protected abstract void RegisterAssets(); #region Register(内部 or Module 使用) - /// - /// 注册场景单元到资产目录中 + /// 注册场景单元到资产目录中 /// /// 场景单元的唯一标识键 /// 场景单元资源的路径 @@ -46,7 +45,7 @@ public abstract class AbstractAssetCatalogSystem : AbstractSystem, IAssetCatalog } /// - /// 通过资产目录映射注册场景单元 + /// 通过资产目录映射注册场景单元 /// /// 包含场景单元信息的资产目录映射对象 /// 当映射ID不是SceneUnitId类型或键已存在时抛出异常 @@ -62,7 +61,7 @@ public abstract class AbstractAssetCatalogSystem : AbstractSystem, IAssetCatalog } /// - /// 注册场景页面模板 + /// 注册场景页面模板 /// /// 场景页面的唯一标识键 /// 场景页面资源路径 @@ -72,9 +71,9 @@ public abstract class AbstractAssetCatalogSystem : AbstractSystem, IAssetCatalog if (!_scenePages.TryAdd(key, new AssetCatalog.ScenePageId(path))) throw new InvalidOperationException($"Template key duplicated: {key}"); } - + /// - /// 通过资产目录映射注册场景页面 + /// 通过资产目录映射注册场景页面 /// /// 包含场景页面信息的资产目录映射对象 /// 当映射ID不是ScenePageId类型或键已存在时抛出异常 @@ -91,7 +90,7 @@ public abstract class AbstractAssetCatalogSystem : AbstractSystem, IAssetCatalog /// - /// 注册一个通用资源(Asset),使用指定的键和路径。 + /// 注册一个通用资源(Asset),使用指定的键和路径。 /// /// 唯一标识该资源的字符串键。 /// 该资源对应的资源路径。 @@ -103,11 +102,11 @@ public abstract class AbstractAssetCatalogSystem : AbstractSystem, IAssetCatalog } /// - /// 根据映射对象注册一个通用资源(Asset)。 + /// 根据映射对象注册一个通用资源(Asset)。 /// /// 包含键与ID映射关系的对象。 /// - /// 当映射ID不是 类型或键重复时抛出异常。 + /// 当映射ID不是 类型或键重复时抛出异常。 /// public void RegisterAsset(AssetCatalog.AssetCatalogMapping mapping) { @@ -117,51 +116,71 @@ public abstract class AbstractAssetCatalogSystem : AbstractSystem, IAssetCatalog if (!_assets.TryAdd(mapping.Key, assetId)) throw new InvalidOperationException($"Asset key duplicated: {mapping.Key}"); } + #endregion #region Query(对外) /// - /// 根据指定的键获取场景单元标识符 + /// 根据指定的键获取场景单元标识符 /// /// 用于查找场景单元的键值 /// 返回与指定键对应的场景单元标识符 - public AssetCatalog.SceneUnitId GetSceneUnit(string key) => _sceneUnits[key]; + public AssetCatalog.SceneUnitId GetSceneUnit(string key) + { + return _sceneUnits[key]; + } /// - /// 根据指定的键获取场景页面标识符 + /// 根据指定的键获取场景页面标识符 /// /// 用于查找场景页面的键值 /// 返回与指定键对应的场景页面标识符 - public AssetCatalog.ScenePageId GetScenePage(string key) => _scenePages[key]; + public AssetCatalog.ScenePageId GetScenePage(string key) + { + return _scenePages[key]; + } /// - /// 获取指定键对应的通用资源ID。 + /// 获取指定键对应的通用资源ID。 /// /// 要查找的通用资源键。 /// 对应的通用资源ID。 /// 如果未找到指定键则抛出异常。 - public AssetCatalog.AssetId GetAsset(string key) => _assets[key]; + public AssetCatalog.AssetId GetAsset(string key) + { + return _assets[key]; + } /// - /// 检查是否存在指定键的场景单元 + /// 检查是否存在指定键的场景单元 /// /// 用于查找场景单元的键值 /// 存在返回true,否则返回false - public bool HasSceneUnit(string key) => _sceneUnits.ContainsKey(key); + public bool HasSceneUnit(string key) + { + return _sceneUnits.ContainsKey(key); + } /// - /// 检查是否存在指定键的场景页面 + /// 检查是否存在指定键的场景页面 /// /// 用于查找场景页面的键值 /// 存在返回true,否则返回false - public bool HasScenePage(string key) => _scenePages.ContainsKey(key); + public bool HasScenePage(string key) + { + return _scenePages.ContainsKey(key); + } /// - /// 判断是否存在指定键的通用资源。 + /// 判断是否存在指定键的通用资源。 /// /// 要检查的通用资源键。 /// 若存在返回 true,否则返回 false。 - public bool HasAsset(string key) => _assets.ContainsKey(key); + public bool HasAsset(string key) + { + return _assets.ContainsKey(key); + } + #endregion -} +} \ No newline at end of file diff --git a/GFramework.Game/assets/AssetCatalog.cs b/GFramework.Game/assets/AssetCatalog.cs index 221f857..f1abd49 100644 --- a/GFramework.Game/assets/AssetCatalog.cs +++ b/GFramework.Game/assets/AssetCatalog.cs @@ -1,47 +1,45 @@ namespace GFramework.Game.assets; /// -/// 资源目录类,用于定义和管理游戏中的场景和资源标识符 +/// 资源目录类,用于定义和管理游戏中的场景和资源标识符 /// public static class AssetCatalog { /// - /// 资源标识符接口,定义了资源路径的访问接口 + /// 资源标识符接口,定义了资源路径的访问接口 /// public interface IAssetId { /// - /// 获取资源的路径 + /// 获取资源的路径 /// string Path { get; } } /// - /// 资源目录映射结构体,用于存储资源目录的键值对映射关系 + /// 资源目录映射结构体,用于存储资源目录的键值对映射关系 /// /// 资源目录的键 /// 资源标识符 public readonly record struct AssetCatalogMapping(string Key, IAssetId Id); - + /// - /// 场景页面资源标识符结构体,用于标识场景页面资源 + /// 场景页面资源标识符结构体,用于标识场景页面资源 /// /// 场景页面资源路径 public readonly record struct ScenePageId(string Path) : IAssetId; - + /// - /// 场景单元资源标识符结构体,用于标识场景单元资源 + /// 场景单元资源标识符结构体,用于标识场景单元资源 /// /// 场景单元资源路径 public readonly record struct SceneUnitId(string Path) : IAssetId; - + /// - /// 通用资源标识符结构体,实现IAssetId接口 + /// 通用资源标识符结构体,实现IAssetId接口 /// /// 资源路径 public readonly record struct AssetId(string Path) : IAssetId; - -} - +} \ No newline at end of file diff --git a/GFramework.Game/assets/IAssetCatalogSystem.cs b/GFramework.Game/assets/IAssetCatalogSystem.cs index 2f2345b..660eb15 100644 --- a/GFramework.Game/assets/IAssetCatalogSystem.cs +++ b/GFramework.Game/assets/IAssetCatalogSystem.cs @@ -3,19 +3,19 @@ namespace GFramework.Game.assets; /// -/// 资源目录系统接口,用于管理场景和资源的获取与查询 +/// 资源目录系统接口,用于管理场景和资源的获取与查询 /// public interface IAssetCatalogSystem : ISystem { /// - /// 根据指定的键获取场景单元标识符 + /// 根据指定的键获取场景单元标识符 /// /// 用于查找场景单元的键值 /// 返回与指定键对应的场景单元标识符 AssetCatalog.SceneUnitId GetSceneUnit(string key); /// - /// 根据指定的键获取场景页面标识符 + /// 根据指定的键获取场景页面标识符 /// /// 用于查找场景页面的键值 /// 返回与指定键对应的场景页面标识符 @@ -23,14 +23,14 @@ public interface IAssetCatalogSystem : ISystem /// - /// 根据指定的键获取资源ID + /// 根据指定的键获取资源ID /// /// 用于查找资源的键值 /// 返回对应的资源ID,如果未找到则返回默认值 AssetCatalog.AssetId GetAsset(string key); /// - /// 注册场景单元到资产目录中 + /// 注册场景单元到资产目录中 /// /// 场景单元的唯一标识键 /// 场景单元资源的路径 @@ -38,14 +38,14 @@ public interface IAssetCatalogSystem : ISystem public void RegisterSceneUnit(string key, string path); /// - /// 通过资产目录映射注册场景单元 + /// 通过资产目录映射注册场景单元 /// /// 包含场景单元信息的资产目录映射对象 /// 当映射ID不是SceneUnitId类型或键已存在时抛出异常 public void RegisterSceneUnit(AssetCatalog.AssetCatalogMapping mapping); /// - /// 注册场景页面模板 + /// 注册场景页面模板 /// /// 场景页面的唯一标识键 /// 场景页面资源路径 @@ -53,28 +53,28 @@ public interface IAssetCatalogSystem : ISystem void RegisterScenePage(string key, string path); /// - /// 通过资产目录映射注册场景页面 + /// 通过资产目录映射注册场景页面 /// /// 包含场景页面信息的资产目录映射对象 /// 当映射ID不是ScenePageId类型或键已存在时抛出异常 void RegisterScenePage(AssetCatalog.AssetCatalogMapping mapping); /// - /// 注册普通资产资源到资产目录中 + /// 注册普通资产资源到资产目录中 /// /// 资产的唯一标识键值 /// 资产资源的路径 void RegisterAsset(string key, string path); /// - /// 根据映射配置注册普通资产资源到资产目录中 + /// 根据映射配置注册普通资产资源到资产目录中 /// /// 包含键值和路径映射关系的配置对象 void RegisterAsset(AssetCatalog.AssetCatalogMapping mapping); /// - /// 检查是否存在指定键的场景单元 + /// 检查是否存在指定键的场景单元 /// /// 用于查找场景单元的键值 /// 存在返回true,否则返回false @@ -82,17 +82,16 @@ public interface IAssetCatalogSystem : ISystem /// - /// 检查是否存在指定键的场景页面 + /// 检查是否存在指定键的场景页面 /// /// 用于查找场景页面的键值 /// 存在返回true,否则返回false bool HasScenePage(string key); /// - /// 检查是否存在指定键的资源 + /// 检查是否存在指定键的资源 /// /// 用于查找资源的键值 /// 存在返回true,否则返回false bool HasAsset(string key); - } \ No newline at end of file diff --git a/GFramework.Game/assets/IResourceFactorySystem.cs b/GFramework.Game/assets/IResourceFactorySystem.cs index a6a0753..2ecd778 100644 --- a/GFramework.Game/assets/IResourceFactorySystem.cs +++ b/GFramework.Game/assets/IResourceFactorySystem.cs @@ -3,24 +3,23 @@ namespace GFramework.Game.assets; /// -/// 资源工厂系统接口,用于获取指定类型的资源创建函数 +/// 资源工厂系统接口,用于获取指定类型的资源创建函数 /// public interface IResourceFactorySystem : ISystem { /// - /// 根据指定键名获取指定类型T的资源创建函数 + /// 根据指定键名获取指定类型T的资源创建函数 /// /// 要获取创建函数的资源类型 /// 用于标识资源的键名 /// 返回一个创建T类型实例的函数委托 Func GetFactory(string key); - + /// - /// 根据资产目录映射获取指定类型T的资源创建函数 + /// 根据资产目录映射获取指定类型T的资源创建函数 /// /// 要获取创建函数的资源类型 /// 资产目录映射信息 /// 返回一个创建T类型实例的函数委托 Func GetFactory(AssetCatalog.AssetCatalogMapping mapping); -} - +} \ No newline at end of file diff --git a/GFramework.Game/assets/ResourceFactory.cs b/GFramework.Game/assets/ResourceFactory.cs index b46e258..d85babd 100644 --- a/GFramework.Game/assets/ResourceFactory.cs +++ b/GFramework.Game/assets/ResourceFactory.cs @@ -1,82 +1,85 @@ namespace GFramework.Game.assets; /// -/// 资源工厂类,用于注册和解析各种资源的创建工厂 +/// 资源工厂类,用于注册和解析各种资源的创建工厂 /// public static class ResourceFactory { /// - /// 可预加载条目接口,定义了是否需要预加载以及执行工厂的方法 + /// 可预加载条目接口,定义了是否需要预加载以及执行工厂的方法 /// private interface IPreloadableEntry { /// - /// 获取一个值,表示该资源是否需要预加载 + /// 获取一个值,表示该资源是否需要预加载 /// bool Preload { get; } /// - /// 执行与该条目关联的工厂方法 - /// - void ExecuteFactory(); - - /// - /// 获取资源类型 + /// 获取资源类型 /// Type ResourceType { get; } - + /// - /// 获取资源键值 + /// 获取资源键值 /// string Key { get; } + + /// + /// 执行与该条目关联的工厂方法 + /// + void ExecuteFactory(); } /// - /// 表示一个具体的资源工厂条目,实现 IPreloadableEntry 接口 + /// 表示一个具体的资源工厂条目,实现 IPreloadableEntry 接口 /// /// 资源类型 private sealed class Entry(string key, Func factory, bool preload) : IPreloadableEntry { /// - /// 获取用于创建资源的工厂函数 + /// 获取用于创建资源的工厂函数 /// public Func Factory { get; } = factory; /// - /// 获取一个值,表示该资源是否需要预加载 + /// 获取一个值,表示该资源是否需要预加载 /// public bool Preload { get; } = preload; /// - /// 执行工厂函数以创建资源实例 + /// 执行工厂函数以创建资源实例 /// - public void ExecuteFactory() => Factory(); + public void ExecuteFactory() + { + Factory(); + } /// - /// 获取资源的类型 + /// 获取资源的类型 /// public Type ResourceType => typeof(T); /// - /// 获取资源的键值 + /// 获取资源的键值 /// public string Key { get; } = key; } - + /// - /// 工厂注册表,管理所有已注册的资源工厂 + /// 工厂注册表,管理所有已注册的资源工厂 /// public sealed class Registry { /// - /// 存储所有已注册的工厂函数,键为资源类型,值为对应的工厂条目对象 + /// 存储所有已注册的工厂函数,键为资源类型,值为对应的工厂条目对象 /// private readonly Dictionary<(Type type, string key), IPreloadableEntry> _factories = new(); /// - /// 注册指定类型的资源工厂 + /// 注册指定类型的资源工厂 /// /// 要注册的资源类型 /// 键 @@ -93,7 +96,7 @@ public static class ResourceFactory } /// - /// 解析并获取指定类型的工厂函数 + /// 解析并获取指定类型的工厂函数 /// /// 要获取工厂函数的资源类型 /// 资源键 @@ -105,26 +108,21 @@ public static class ResourceFactory if (_factories.TryGetValue(dictKey, out var entry) && entry is Entry typed) - { return typed.Factory; - } throw new InvalidOperationException( $"Factory not registered: {typeof(T).Name} with key '{key}'"); } - + /// - /// 预加载所有标记为需要预加载的资源 + /// 预加载所有标记为需要预加载的资源 /// public void PreloadAll() { // 遍历所有已注册的工厂 foreach (var entry in _factories.Values.Where(entry => entry.Preload)) - { // 执行其工厂方法进行预加载 entry.ExecuteFactory(); - } } } - -} +} \ No newline at end of file diff --git a/GFramework.Godot.SourceGenerators.Attributes/logging/GodotLogAttribute.cs b/GFramework.Godot.SourceGenerators.Attributes/logging/GodotLogAttribute.cs index 704cf0c..17afa44 100644 --- a/GFramework.Godot.SourceGenerators.Attributes/logging/GodotLogAttribute.cs +++ b/GFramework.Godot.SourceGenerators.Attributes/logging/GodotLogAttribute.cs @@ -4,20 +4,20 @@ using System; namespace GFramework.Godot.SourceGenerators.Attributes.logging; /// -/// Godot日志特性,用于在类上标记以自动生成日志字段 +/// Godot日志特性,用于在类上标记以自动生成日志字段 /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] public sealed class GodotLogAttribute : Attribute { /// - /// 初始化 GodotLogAttribute 类的新实例 + /// 初始化 GodotLogAttribute 类的新实例 /// public GodotLogAttribute() { } /// - /// 初始化 GodotLogAttribute 类的新实例 + /// 初始化 GodotLogAttribute 类的新实例 /// /// 日志分类名 public GodotLogAttribute(string? name) diff --git a/GFramework.Godot.SourceGenerators/logging/Diagnostic.cs b/GFramework.Godot.SourceGenerators/logging/Diagnostic.cs deleted file mode 100644 index bf235c0..0000000 --- a/GFramework.Godot.SourceGenerators/logging/Diagnostic.cs +++ /dev/null @@ -1,44 +0,0 @@ -using Microsoft.CodeAnalysis; - -namespace GFramework.Godot.SourceGenerators.logging; - -/// -/// 提供诊断描述符的静态类,用于GFramework日志生成器的编译时检查 -/// -internal static class GodotLoggerDiagnostics -{ - /// - /// 诊断描述符:标识使用[GodotLog]特性的类必须声明为partial - /// - /// - /// ID: GFLOG001 - /// 严重性: Error - /// 分类: GFramework.Godot.Logging - /// - public static readonly DiagnosticDescriptor MustBePartial = - new( - id: "GFLOG001", - title: "Class must be partial", - messageFormat: "Class '{0}' must be declared as partial to use [GodotLog]", - category: "GFramework.Godot.Logging", - DiagnosticSeverity.Error, - isEnabledByDefault: true - ); - - - /// - /// 诊断描述符:标识GodotLogAttribute无法在指定类上生成Logger - /// - /// - /// ID: GFW_LOG001 - /// 严重性: Warning - /// 分类: GFramework.Godot.Logging - /// - public static readonly DiagnosticDescriptor LogAttributeInvalid = new( - id: "GFW_LOG001", - title: "GodotLogAttribute cannot generate Logger", - messageFormat: "GodotLogAttribute on class '{0}' is ineffective: {1}", - category: "GFramework.Godot.Logging", - DiagnosticSeverity.Warning, - isEnabledByDefault: true); -} \ No newline at end of file diff --git a/GFramework.Godot.SourceGenerators/logging/GodotLoggerDiagnostic.cs b/GFramework.Godot.SourceGenerators/logging/GodotLoggerDiagnostic.cs new file mode 100644 index 0000000..66a046d --- /dev/null +++ b/GFramework.Godot.SourceGenerators/logging/GodotLoggerDiagnostic.cs @@ -0,0 +1,44 @@ +using Microsoft.CodeAnalysis; + +namespace GFramework.Godot.SourceGenerators.logging; + +/// +/// 提供诊断描述符的静态类,用于GFramework日志生成器的编译时检查 +/// +internal static class GodotLoggerDiagnostics +{ + /// + /// 诊断描述符:标识使用[GodotLog]特性的类必须声明为partial + /// + /// + /// ID: GFLOG001 + /// 严重性: Error + /// 分类: GFramework.Godot.Logging + /// + public static readonly DiagnosticDescriptor MustBePartial = + new( + "GFLOG001", + "Class must be partial", + "Class '{0}' must be declared as partial to use [GodotLog]", + "GFramework.Godot.Logging", + DiagnosticSeverity.Error, + true + ); + + + /// + /// 诊断描述符:标识GodotLogAttribute无法在指定类上生成Logger + /// + /// + /// ID: GFW_LOG001 + /// 严重性: Warning + /// 分类: GFramework.Godot.Logging + /// + public static readonly DiagnosticDescriptor LogAttributeInvalid = new( + "GFW_LOG001", + "GodotLogAttribute cannot generate Logger", + "GodotLogAttribute on class '{0}' is ineffective: {1}", + "GFramework.Godot.Logging", + DiagnosticSeverity.Warning, + true); +} \ No newline at end of file diff --git a/GFramework.Godot.SourceGenerators/logging/GodotLoggerGenerator.cs b/GFramework.Godot.SourceGenerators/logging/GodotLoggerGenerator.cs index b4b5cab..89b04e4 100644 --- a/GFramework.Godot.SourceGenerators/logging/GodotLoggerGenerator.cs +++ b/GFramework.Godot.SourceGenerators/logging/GodotLoggerGenerator.cs @@ -9,7 +9,7 @@ using Microsoft.CodeAnalysis.Text; namespace GFramework.Godot.SourceGenerators.logging; /// -/// 日志生成器,用于为标记了LogAttribute的类自动生成日志字段 +/// 日志生成器,用于为标记了LogAttribute的类自动生成日志字段 /// [Generator] public sealed class GodotLoggerGenerator : IIncrementalGenerator @@ -21,7 +21,7 @@ public sealed class GodotLoggerGenerator : IIncrementalGenerator private const string AttributeShortNameWithoutSuffix = "GodotLog"; /// - /// 初始化生成器,设置语法过滤和代码生成逻辑 + /// 初始化生成器,设置语法过滤和代码生成逻辑 /// /// 增量生成器初始化上下文 public void Initialize(IncrementalGeneratorInitializationContext context) @@ -86,7 +86,7 @@ public sealed class GodotLoggerGenerator : IIncrementalGenerator } /// - /// 获取类符号上的LogAttribute特性 + /// 获取类符号上的LogAttribute特性 /// /// 类符号 /// LogAttribute特性数据,如果不存在则返回null @@ -104,7 +104,7 @@ public sealed class GodotLoggerGenerator : IIncrementalGenerator } /// - /// 生成日志字段代码 + /// 生成日志字段代码 /// /// 类符号 /// LogAttribute特性数据 @@ -173,7 +173,7 @@ public sealed class GodotLoggerGenerator : IIncrementalGenerator } /// - /// 从特性数据中获取命名参数的值 + /// 从特性数据中获取命名参数的值 /// /// 特性数据 /// 参数名称 diff --git a/GFramework.Godot/GFramework.Godot.csproj b/GFramework.Godot/GFramework.Godot.csproj index fc85a79..6c052b3 100644 --- a/GFramework.Godot/GFramework.Godot.csproj +++ b/GFramework.Godot/GFramework.Godot.csproj @@ -14,7 +14,7 @@ - + diff --git a/GFramework.Godot/architecture/AbstractArchitecture.cs b/GFramework.Godot/architecture/AbstractArchitecture.cs index c2d2135..49943ab 100644 --- a/GFramework.Godot/architecture/AbstractArchitecture.cs +++ b/GFramework.Godot/architecture/AbstractArchitecture.cs @@ -6,46 +6,45 @@ using Godot; namespace GFramework.Godot.architecture; /// -/// 抽象架构类,为特定类型的架构提供基础实现框架。 -/// 此类负责管理架构的初始化、生命周期绑定以及扩展模块的安装与销毁。 +/// 抽象架构类,为特定类型的架构提供基础实现框架。 +/// 此类负责管理架构的初始化、生命周期绑定以及扩展模块的安装与销毁。 /// -public abstract class AbstractArchitecture: Architecture +public abstract class AbstractArchitecture : Architecture { - /// - /// 架构锚点节点的唯一标识名称 - /// 用于在Godot场景树中创建和查找架构锚点节点 + /// 架构锚点节点的唯一标识名称 + /// 用于在Godot场景树中创建和查找架构锚点节点 /// private const string ArchitectureAnchorName = $"__{GFrameworkConstants.FrameworkName}__ArchitectureAnchor__"; - + /// - /// 存储所有已安装的Godot架构扩展组件列表 - /// 用于在架构销毁时正确清理所有扩展资源 + /// 存储所有已安装的Godot架构扩展组件列表 + /// 用于在架构销毁时正确清理所有扩展资源 /// private readonly List _extensions = []; - + /// - /// 架构锚点节点引用 - /// 用于将架构绑定到Godot生命周期并作为扩展节点的父节点 + /// 架构锚点节点引用 + /// 用于将架构绑定到Godot生命周期并作为扩展节点的父节点 /// private ArchitectureAnchor? _anchor; /// - /// 获取架构根节点。如果尚未初始化或已被销毁,则抛出异常。 + /// 标记架构是否已被销毁的状态标志 + /// 用于防止架构被重复销毁,确保资源清理只执行一次 + /// + private bool _destroyed; + + /// + /// 获取架构根节点。如果尚未初始化或已被销毁,则抛出异常。 /// /// 当架构未准备就绪时抛出。 protected Node ArchitectureRoot => _anchor ?? throw new InvalidOperationException("Architecture root not ready"); - /// - /// 标记架构是否已被销毁的状态标志 - /// 用于防止架构被重复销毁,确保资源清理只执行一次 - /// - private bool _destroyed; - /// - /// 初始化架构,按顺序注册模型、系统和工具。 - /// 包括将架构绑定到Godot生命周期并调用模块安装逻辑。 + /// 初始化架构,按顺序注册模型、系统和工具。 + /// 包括将架构绑定到Godot生命周期并调用模块安装逻辑。 /// protected override void Init() { @@ -54,14 +53,14 @@ public abstract class AbstractArchitecture: Architecture } /// - /// 安装模块抽象方法,由子类实现具体的模块注册逻辑。 - /// 子类应在此方法中完成所有模型、系统及工具的注册工作。 + /// 安装模块抽象方法,由子类实现具体的模块注册逻辑。 + /// 子类应在此方法中完成所有模型、系统及工具的注册工作。 /// protected abstract void InstallModules(); /// - /// 将架构绑定到Godot生命周期中,确保在场景树销毁时能够正确清理资源。 - /// 通过创建一个锚节点来监听场景树的销毁事件。 + /// 将架构绑定到Godot生命周期中,确保在场景树销毁时能够正确清理资源。 + /// 通过创建一个锚节点来监听场景树的销毁事件。 /// private void AttachToGodotLifecycle() { @@ -82,9 +81,9 @@ public abstract class AbstractArchitecture: Architecture tree.Root.CallDeferred(Node.MethodName.AddChild, _anchor); } - + /// - /// 安装Godot模块扩展 + /// 安装Godot模块扩展 /// /// 模块类型,必须实现IGodotModule接口 /// 要安装的模块实例 @@ -92,30 +91,29 @@ public abstract class AbstractArchitecture: Architecture protected async Task InstallGodotModule(TModule module) where TModule : IGodotModule { module.Install(this); - + // 检查锚点是否已初始化,未初始化则抛出异常 if (_anchor == null) throw new InvalidOperationException("Anchor not initialized"); - + // 等待锚点准备就绪 await _anchor.WaitUntilReady(); - + // 延迟调用将扩展节点添加为锚点的子节点 _anchor.CallDeferred(Node.MethodName.AddChild, module.Node); // 调用扩展的附加回调方法 module.OnAttach(this); - + // 将扩展添加到扩展集合中 _extensions.Add(module); } - /// - /// 销毁架构及其相关资源。 - /// 调用所有已安装扩展的OnDetach方法,并清空扩展列表。 - /// 若已被销毁则直接返回。 + /// 销毁架构及其相关资源。 + /// 调用所有已安装扩展的OnDetach方法,并清空扩展列表。 + /// 若已被销毁则直接返回。 /// public override void Destroy() { diff --git a/GFramework.Godot/architecture/AbstractGodotModule.cs b/GFramework.Godot/architecture/AbstractGodotModule.cs index 2d2c09f..e0bf752 100644 --- a/GFramework.Godot/architecture/AbstractGodotModule.cs +++ b/GFramework.Godot/architecture/AbstractGodotModule.cs @@ -4,54 +4,50 @@ using Godot; namespace GFramework.Godot.architecture; /// -/// 抽象的Godot模块基类,用于定义Godot框架中的模块行为 +/// 抽象的Godot模块基类,用于定义Godot框架中的模块行为 /// -public abstract class AbstractGodotModule: IGodotModule +public abstract class AbstractGodotModule : IGodotModule { /// - /// 当架构阶段发生变化时调用此方法 + /// 当架构阶段发生变化时调用此方法 /// /// 当前的架构阶段 /// 架构实例 public virtual void OnPhase(ArchitecturePhase phase, IArchitecture arch) { - } /// - /// 当架构阶段发生变化时调用此方法 + /// 当架构阶段发生变化时调用此方法 /// /// 当前的架构阶段 public virtual void OnArchitecturePhase(ArchitecturePhase phase) { - } /// - /// 安装模块到指定架构中 + /// 安装模块到指定架构中 /// /// 要安装到的架构实例 public abstract void Install(IArchitecture architecture); /// - /// 获取模块关联的Godot节点 + /// 获取模块关联的Godot节点 /// public abstract Node Node { get; } - + /// - /// 当模块被附加到架构时调用此方法 + /// 当模块被附加到架构时调用此方法 /// /// 被附加到的架构实例 public virtual void OnAttach(Architecture architecture) { - } /// - /// 当模块从架构中分离时调用此方法 + /// 当模块从架构中分离时调用此方法 /// public virtual void OnDetach() { - } -} +} \ No newline at end of file diff --git a/GFramework.Godot/architecture/ArchitectureAnchor.cs b/GFramework.Godot/architecture/ArchitectureAnchor.cs index 4f41564..2884184 100644 --- a/GFramework.Godot/architecture/ArchitectureAnchor.cs +++ b/GFramework.Godot/architecture/ArchitectureAnchor.cs @@ -3,29 +3,28 @@ namespace GFramework.Godot.architecture; /// -/// 架构锚点节点类,用于在Godot场景树中作为架构组件的根节点 -/// 该类提供了退出时的回调绑定功能,可以在节点从场景树中移除时执行清理操作 +/// 架构锚点节点类,用于在Godot场景树中作为架构组件的根节点 +/// 该类提供了退出时的回调绑定功能,可以在节点从场景树中移除时执行清理操作 /// public partial class ArchitectureAnchor : Node { private Action? _onExit; + /// - /// 绑定节点退出时的回调动作 + /// 绑定节点退出时的回调动作 /// /// 当节点从场景树退出时要执行的动作 public void Bind(Action onExit) { if (_onExit != null) - { GD.PushWarning( $"{nameof(ArchitectureAnchor)} already bound. Rebinding will override previous callback."); - } _onExit = onExit; } - + /// - /// 当节点从场景树中移除时调用此方法 - /// 执行绑定的退出回调并清理引用 + /// 当节点从场景树中移除时调用此方法 + /// 执行绑定的退出回调并清理引用 /// public override void _ExitTree() { @@ -33,5 +32,4 @@ public partial class ArchitectureAnchor : Node _onExit = null; callback?.Invoke(); } -} - +} \ No newline at end of file diff --git a/GFramework.Godot/architecture/IGodotModule.cs b/GFramework.Godot/architecture/IGodotModule.cs index 154f521..1b18fa4 100644 --- a/GFramework.Godot/architecture/IGodotModule.cs +++ b/GFramework.Godot/architecture/IGodotModule.cs @@ -4,23 +4,23 @@ using Godot; namespace GFramework.Godot.architecture; /// -/// Godot模块接口,定义了Godot引擎中模块的基本行为和属性 +/// Godot模块接口,定义了Godot引擎中模块的基本行为和属性 /// public interface IGodotModule : IArchitectureModule { /// - /// 获取模块关联的Godot节点 + /// 获取模块关联的Godot节点 /// Node Node { get; } /// - /// 当模块被附加到架构时调用 + /// 当模块被附加到架构时调用 /// /// 要附加到的架构实例 void OnAttach(Architecture architecture); /// - /// 当模块从架构分离时调用 + /// 当模块从架构分离时调用 /// void OnDetach(); } \ No newline at end of file diff --git a/GFramework.Godot/assets/AbstractResourceFactorySystem.cs b/GFramework.Godot/assets/AbstractResourceFactorySystem.cs index 5745f45..982b645 100644 --- a/GFramework.Godot/assets/AbstractResourceFactorySystem.cs +++ b/GFramework.Godot/assets/AbstractResourceFactorySystem.cs @@ -7,29 +7,17 @@ using Godot; namespace GFramework.Godot.assets; /// -/// 资源工厂系统抽象基类,用于统一管理各类资源的创建与预加载逻辑。 -/// 提供注册场景和资源的方法,并通过依赖的资源加载系统和资产目录系统完成实际资源的获取与构造。 +/// 资源工厂系统抽象基类,用于统一管理各类资源的创建与预加载逻辑。 +/// 提供注册场景和资源的方法,并通过依赖的资源加载系统和资产目录系统完成实际资源的获取与构造。 /// public abstract class AbstractResourceFactorySystem : AbstractSystem, IResourceFactorySystem, IArchitectureLifecycle { + private IAssetCatalogSystem? _assetCatalogSystem; private ResourceFactory.Registry? _registry; private IResourceLoadSystem? _resourceLoadSystem; - private IAssetCatalogSystem? _assetCatalogSystem; /// - /// 系统初始化方法,在系统启动时执行一次。 - /// 初始化资源注册表,并获取依赖的资源加载系统和资产目录系统。 - /// 最后执行所有已注册资源的预加载操作。 - /// - protected override void OnInit() - { - _registry = new ResourceFactory.Registry(); - _resourceLoadSystem = Context.GetSystem(); - _assetCatalogSystem = Context.GetSystem(); - } - - /// - /// 架构阶段回调,在架构就绪时注册和预加载资源 + /// 架构阶段回调,在架构就绪时注册和预加载资源 /// /// 当前架构阶段 /// 架构实例 @@ -44,34 +32,51 @@ public abstract class AbstractResourceFactorySystem : AbstractSystem, IResourceF } } - /// - /// 注册系统所需的各种资源类型。由子类实现具体注册逻辑。 - /// - protected abstract void RegisterResources(); - /// - /// 根据指定的键获取资源工厂函数。 + /// 根据指定的键获取资源工厂函数。 /// /// 资源类型 /// 资源键 /// 返回创建指定类型资源的工厂函数 - public Func GetFactory(string key) => _registry!.ResolveFactory(key); + public Func GetFactory(string key) + { + return _registry!.ResolveFactory(key); + } /// - /// 根据资产目录映射信息获取资源工厂函数。 + /// 根据资产目录映射信息获取资源工厂函数。 /// /// 资源类型 /// 资产目录映射信息 /// 返回创建指定类型资源的工厂函数 - public Func GetFactory(AssetCatalog.AssetCatalogMapping mapping) => _registry!.ResolveFactory(mapping.Key); + public Func GetFactory(AssetCatalog.AssetCatalogMapping mapping) + { + return _registry!.ResolveFactory(mapping.Key); + } + + /// + /// 系统初始化方法,在系统启动时执行一次。 + /// 初始化资源注册表,并获取依赖的资源加载系统和资产目录系统。 + /// 最后执行所有已注册资源的预加载操作。 + /// + protected override void OnInit() + { + _registry = new ResourceFactory.Registry(); + _resourceLoadSystem = Context.GetSystem(); + _assetCatalogSystem = Context.GetSystem(); + } + + /// + /// 注册系统所需的各种资源类型。由子类实现具体注册逻辑。 + /// + protected abstract void RegisterResources(); #region Register Helpers(声明式) - /// - /// 注册场景单元到资源管理系统中 + /// 注册场景单元到资源管理系统中 /// /// 场景单元类型,必须继承自Node /// 场景单元键值,用于标识特定的场景单元资源 @@ -92,7 +97,7 @@ public abstract class AbstractResourceFactorySystem : AbstractSystem, IResourceF /// - /// 注册场景页面到资源管理系统中 + /// 注册场景页面到资源管理系统中 /// /// 场景页面类型,必须继承自Node /// 场景页面键值,用于标识特定的场景页面资源 @@ -112,7 +117,7 @@ public abstract class AbstractResourceFactorySystem : AbstractSystem, IResourceF } /// - /// 注册通用资产资源到资源管理系统中 + /// 注册通用资产资源到资源管理系统中 /// /// 资产类型,必须继承自Resource /// 资产键值,用于标识特定的资产资源 @@ -131,6 +136,5 @@ public abstract class AbstractResourceFactorySystem : AbstractSystem, IResourceF ); } - #endregion } \ No newline at end of file diff --git a/GFramework.Godot/assets/IResourceLoadSystem.cs b/GFramework.Godot/assets/IResourceLoadSystem.cs index 81b1a96..81b3481 100644 --- a/GFramework.Godot/assets/IResourceLoadSystem.cs +++ b/GFramework.Godot/assets/IResourceLoadSystem.cs @@ -5,35 +5,35 @@ using Godot; namespace GFramework.Godot.system; /// -/// 资源加载系统接口,提供资源和场景的加载、实例化、预加载等功能 +/// 资源加载系统接口,提供资源和场景的加载、实例化、预加载等功能 /// public interface IResourceLoadSystem : ISystem { /// - /// 加载指定路径的资源 + /// 加载指定路径的资源 /// /// 资源类型,必须继承自Resource /// 资源路径 /// 加载的资源实例 public T? LoadResource(string path) where T : Resource; - + /// - /// 获取场景加载器,用于延迟加载场景 + /// 获取场景加载器,用于延迟加载场景 /// /// 场景路径 /// 场景的延迟加载包装器 public Lazy GetSceneLoader(string path); /// - /// 创建指定路径场景的实例 + /// 创建指定路径场景的实例 /// /// 节点类型,必须继承自Node /// 场景路径 /// 场景实例化的节点对象 public T? CreateInstance(string path) where T : Node; - + /// - /// 获取或注册游戏单位工厂函数 + /// 获取或注册游戏单位工厂函数 /// /// 节点类型,必须继承自Node /// 场景资源标识符 @@ -43,7 +43,7 @@ public interface IResourceLoadSystem : ISystem ) where T : Node; /// - /// 获取或注册模板资源工厂函数 + /// 获取或注册模板资源工厂函数 /// /// 节点类型,必须继承自Node /// 模板资源标识符 @@ -53,7 +53,7 @@ public interface IResourceLoadSystem : ISystem ) where T : Node; /// - /// 获取或注册通用资产工厂函数 + /// 获取或注册通用资产工厂函数 /// /// 资源类型,必须继承自Resource /// 资产资源标识符 @@ -65,19 +65,19 @@ public interface IResourceLoadSystem : ISystem ) where T : Resource; /// - /// 预加载指定路径的多个资源 + /// 预加载指定路径的多个资源 /// /// 需要预加载的资源路径集合 public void Preload(IEnumerable paths); - + /// - /// 卸载指定路径的资源 + /// 卸载指定路径的资源 /// /// 需要卸载的资源路径 public void Unload(string path); - + /// - /// 清除所有已加载的资源 + /// 清除所有已加载的资源 /// public void ClearAll(); -} +} \ No newline at end of file diff --git a/GFramework.Godot/assets/ResourceLoadSystem.cs b/GFramework.Godot/assets/ResourceLoadSystem.cs index 204ef44..1bf0917 100644 --- a/GFramework.Godot/assets/ResourceLoadSystem.cs +++ b/GFramework.Godot/assets/ResourceLoadSystem.cs @@ -5,90 +5,33 @@ using Godot; namespace GFramework.Godot.system; /// -/// 资源加载系统,用于统一管理和缓存Godot资源(如场景、纹理等)的加载与实例化。 -/// 提供基础加载、场景实例化、资源工厂注册以及缓存管理功能。 +/// 资源加载系统,用于统一管理和缓存Godot资源(如场景、纹理等)的加载与实例化。 +/// 提供基础加载、场景实例化、资源工厂注册以及缓存管理功能。 /// public class ResourceLoadSystem : AbstractSystem, IResourceLoadSystem { /// - /// 已加载的资源缓存字典,键为资源路径,值为已加载的Resource对象。 + /// 已加载的资源缓存字典,键为资源路径,值为已加载的Resource对象。 /// private readonly Dictionary _loadedResources = new(); /// - /// 场景懒加载器缓存,键为场景路径,值为延迟加载的PackedScene对象。 - /// - private readonly Dictionary> _sceneLoaders = new(); - - /// - /// 场景实例化工厂委托缓存,键为场景路径,值为创建该场景实例的Func委托。 - /// - private readonly Dictionary _sceneFactories = new(); - - /// - /// 资源获取/复制工厂委托缓存,键为资源路径,值为获取或复制资源的Func委托。 + /// 资源获取/复制工厂委托缓存,键为资源路径,值为获取或复制资源的Func委托。 /// private readonly Dictionary _resourceFactories = new(); /// - /// 初始化方法,在系统初始化时打印日志信息。 + /// 场景实例化工厂委托缓存,键为场景路径,值为创建该场景实例的Func委托。 /// - protected override void OnInit() - { - } - - #region 基础加载 + private readonly Dictionary _sceneFactories = new(); /// - /// 加载指定类型的资源并进行缓存。如果资源已经加载过则直接从缓存中返回。 + /// 场景懒加载器缓存,键为场景路径,值为延迟加载的PackedScene对象。 /// - /// 要加载的资源类型,必须继承自Resource。 - /// 资源在项目中的相对路径。 - /// 成功加载的资源对象;若路径无效或加载失败则返回null。 - public T? LoadResource(string path) where T : Resource - { - if (string.IsNullOrEmpty(path)) - return null; - - if (_loadedResources.TryGetValue(path, out var cached)) - return cached as T; - - var res = GD.Load(path); - if (res == null) - { - GD.PrintErr($"[ResourceLoadSystem] Load failed: {path}"); - return null; - } - - _loadedResources[path] = res; - return res; - } + private readonly Dictionary> _sceneLoaders = new(); /// - /// 获取一个场景的懒加载器,用于按需加载PackedScene资源。 - /// 若对应路径尚未注册加载器,则会自动创建一个新的Lazy实例。 - /// - /// 场景文件的相对路径。 - /// 表示该场景懒加载逻辑的Lazy<PackedScene>对象。 - public Lazy GetSceneLoader(string path) - { - if (_sceneLoaders.TryGetValue(path, out var loader)) - return loader; - - loader = new Lazy(() => - { - var scene = LoadResource(path); - return scene ?? throw new InvalidOperationException($"Failed to load scene: {path}"); - }); - - _sceneLoaders[path] = loader; - return loader; - } - - #endregion - - /// - /// 根据给定路径加载场景,并创建其节点实例。 + /// 根据给定路径加载场景,并创建其节点实例。 /// /// 期望返回的节点类型,必须是Node的子类。 /// 场景文件的相对路径。 @@ -159,10 +102,68 @@ public class ResourceLoadSystem : AbstractSystem, IResourceLoadSystem _resourceFactories[path] = factory; return factory; } + + /// + /// 初始化方法,在系统初始化时打印日志信息。 + /// + protected override void OnInit() + { + } + + #region 基础加载 + + /// + /// 加载指定类型的资源并进行缓存。如果资源已经加载过则直接从缓存中返回。 + /// + /// 要加载的资源类型,必须继承自Resource。 + /// 资源在项目中的相对路径。 + /// 成功加载的资源对象;若路径无效或加载失败则返回null。 + public T? LoadResource(string path) where T : Resource + { + if (string.IsNullOrEmpty(path)) + return null; + + if (_loadedResources.TryGetValue(path, out var cached)) + return cached as T; + + var res = GD.Load(path); + if (res == null) + { + GD.PrintErr($"[ResourceLoadSystem] Load failed: {path}"); + return null; + } + + _loadedResources[path] = res; + return res; + } + + /// + /// 获取一个场景的懒加载器,用于按需加载PackedScene资源。 + /// 若对应路径尚未注册加载器,则会自动创建一个新的Lazy实例。 + /// + /// 场景文件的相对路径。 + /// 表示该场景懒加载逻辑的Lazy<PackedScene>对象。 + public Lazy GetSceneLoader(string path) + { + if (_sceneLoaders.TryGetValue(path, out var loader)) + return loader; + + loader = new Lazy(() => + { + var scene = LoadResource(path); + return scene ?? throw new InvalidOperationException($"Failed to load scene: {path}"); + }); + + _sceneLoaders[path] = loader; + return loader; + } + + #endregion + #region 缓存管理 /// - /// 预加载一组资源和场景到内存中以提升后续访问速度。 + /// 预加载一组资源和场景到内存中以提升后续访问速度。 /// /// 待预加载的资源路径集合。 public void Preload(IEnumerable paths) @@ -175,7 +176,7 @@ public class ResourceLoadSystem : AbstractSystem, IResourceLoadSystem } /// - /// 清除指定路径的所有相关缓存数据,包括资源、场景加载器及各类工厂。 + /// 清除指定路径的所有相关缓存数据,包括资源、场景加载器及各类工厂。 /// /// 要卸载的资源路径。 public void Unload(string path) @@ -187,7 +188,7 @@ public class ResourceLoadSystem : AbstractSystem, IResourceLoadSystem } /// - /// 清空所有当前系统的资源缓存、加载器和工厂列表。 + /// 清空所有当前系统的资源缓存、加载器和工厂列表。 /// public void ClearAll() { @@ -198,4 +199,4 @@ public class ResourceLoadSystem : AbstractSystem, IResourceLoadSystem } #endregion -} +} \ No newline at end of file diff --git a/GFramework.Godot/extensions/NodeExtensions.cs b/GFramework.Godot/extensions/NodeExtensions.cs index 13737d9..b45a82f 100644 --- a/GFramework.Godot/extensions/NodeExtensions.cs +++ b/GFramework.Godot/extensions/NodeExtensions.cs @@ -243,7 +243,7 @@ public static class NodeExtensions /// - /// 将指定节点转换为目标类型T + /// 将指定节点转换为目标类型T /// /// 目标节点类型,必须继承自Node /// 要转换的节点对象,可以为null @@ -252,10 +252,8 @@ public static class NodeExtensions public static T OfType(this Node? node) where T : Node { // 检查节点是否有效且类型匹配 - if (node.IsValidNode()&& node is T t) + if (node.IsValidNode() && node is T t) return t; throw new InvalidCastException($"Cannot cast {node} to {typeof(T)}"); } - - } \ No newline at end of file diff --git a/GFramework.Godot/logging/GodotLogger.cs b/GFramework.Godot/logging/GodotLogger.cs index cf20e97..3241c88 100644 --- a/GFramework.Godot/logging/GodotLogger.cs +++ b/GFramework.Godot/logging/GodotLogger.cs @@ -4,21 +4,18 @@ using Godot; namespace GFramework.Godot.logging; /// -/// Godot平台的日志记录器实现 +/// Godot平台的日志记录器实现 /// public sealed class GodotLogger( string? name = null, - LogLevel minLevel = LogLevel.Info) : AbstractLogger(name??ILogger.RootLoggerName, minLevel) + LogLevel minLevel = LogLevel.Info) : AbstractLogger(name ?? ILogger.RootLoggerName, minLevel) { protected override void Write(LogLevel level, string message, Exception? exception) { var prefix = $"[{level.ToString().ToUpper()}][{Name()}]"; // 将异常信息追加到日志消息中 - if (exception != null) - { - message += "\n" + exception; - } + if (exception != null) message += "\n" + exception; // 根据日志级别选择不同的输出方法 switch (level) diff --git a/GFramework.Godot/logging/GodotLoggerFactory.cs b/GFramework.Godot/logging/GodotLoggerFactory.cs index 2694db4..20e7565 100644 --- a/GFramework.Godot/logging/GodotLoggerFactory.cs +++ b/GFramework.Godot/logging/GodotLoggerFactory.cs @@ -3,12 +3,12 @@ namespace GFramework.Godot.logging; /// -/// Godot日志工厂类,用于创建Godot平台专用的日志记录器实例 +/// Godot日志工厂类,用于创建Godot平台专用的日志记录器实例 /// -public class GodotLoggerFactory: ILoggerFactory +public class GodotLoggerFactory : ILoggerFactory { /// - /// 获取指定名称的日志记录器实例 + /// 获取指定名称的日志记录器实例 /// /// 日志记录器的名称 /// 返回GodotLogger类型的日志记录器实例 @@ -16,4 +16,4 @@ public class GodotLoggerFactory: ILoggerFactory { return new GodotLogger(name); } -} +} \ No newline at end of file diff --git a/GFramework.Godot/system/AbstractAudioManagerSystem.cs b/GFramework.Godot/system/AbstractAudioManagerSystem.cs index 7504f49..514a870 100644 --- a/GFramework.Godot/system/AbstractAudioManagerSystem.cs +++ b/GFramework.Godot/system/AbstractAudioManagerSystem.cs @@ -5,102 +5,427 @@ using Godot; namespace GFramework.Godot.system; /// -/// 音频管理器抽象基类,提供音频播放的基础实现 +/// 音频管理器抽象基类,提供音频播放的基础实现 /// public abstract class AbstractAudioManagerSystem : AbstractSystem, IAudioManagerSystem { /// - /// 音频资源加载系统依赖 - /// - protected IResourceLoadSystem? ResourceLoadSystem; - - /// - /// 资源目录系统依赖 - /// - protected IAssetCatalogSystem? AssetCatalogSystem; - - /// - /// 背景音乐播放器 - /// - protected AudioStreamPlayer? MusicPlayer; - - /// - /// 音效播放器列表 - /// - protected readonly List SoundPlayers = []; - - /// - /// 可用音效播放器队列 - /// - protected readonly Queue AvailableSoundPlayers = new(); - - /// - /// 3D音效播放器列表 - /// - protected readonly List Sound3DPlayers = []; - - /// - /// 可用3D音效播放器队列 - /// - protected readonly Queue AvailableSound3DPlayers = new(); - - /// - /// 资源工厂系统依赖 - /// - protected IResourceFactorySystem? ResourceFactorySystem; - - /// - /// 背景音乐音量 - /// - protected float MusicVolume = 1.0f; - - /// - /// 音效音量 - /// - protected float SoundVolume = 1.0f; - - /// - /// 主音量 - /// - protected float MasterVolume = 1.0f; - - /// - /// 特效音量 - /// - protected float SfxVolume = 1.0f; - - /// - /// 语音音量 - /// - protected float VoiceVolume = 1.0f; - - /// - /// 环境音量 - /// - protected float AmbientVolume = 1.0f; - - /// - /// 音乐淡入淡出动画 - /// - protected Tween? MusicFadeTween; - - /// - /// 最大同时播放的音效数量 + /// 最大同时播放的音效数量 /// protected const int MaxSoundPlayers = 10; - + /// - /// 最大同时播放的3D音效数量 + /// 最大同时播放的3D音效数量 /// protected const int MaxSound3DPlayers = 5; /// - /// 所有者节点的抽象属性 + /// 可用3D音效播放器队列 + /// + protected readonly Queue AvailableSound3DPlayers = new(); + + /// + /// 可用音效播放器队列 + /// + protected readonly Queue AvailableSoundPlayers = new(); + + /// + /// 3D音效播放器列表 + /// + protected readonly List Sound3DPlayers = []; + + /// + /// 音效播放器列表 + /// + protected readonly List SoundPlayers = []; + + /// + /// 环境音量 + /// + protected float AmbientVolume = 1.0f; + + /// + /// 资源目录系统依赖 + /// + protected IAssetCatalogSystem? AssetCatalogSystem; + + /// + /// 主音量 + /// + protected float MasterVolume = 1.0f; + + /// + /// 音乐淡入淡出动画 + /// + protected Tween? MusicFadeTween; + + /// + /// 背景音乐播放器 + /// + protected AudioStreamPlayer? MusicPlayer; + + /// + /// 背景音乐音量 + /// + protected float MusicVolume = 1.0f; + + /// + /// 资源工厂系统依赖 + /// + protected IResourceFactorySystem? ResourceFactorySystem; + + /// + /// 音频资源加载系统依赖 + /// + protected IResourceLoadSystem? ResourceLoadSystem; + + /// + /// 特效音量 + /// + protected float SfxVolume = 1.0f; + + /// + /// 音效音量 + /// + protected float SoundVolume = 1.0f; + + /// + /// 语音音量 + /// + protected float VoiceVolume = 1.0f; + + /// + /// 所有者节点的抽象属性 /// protected abstract Node Owner { get; } /// - /// 系统初始化方法 + /// 播放背景音乐 + /// + /// 音频文件路径 + /// 音量大小,范围0-1 + /// 是否循环播放 + public virtual void PlayMusic(string audioPath, float volume = 1.0f, bool loop = true) + { + var audioStream = ResourceLoadSystem?.LoadResource(audioPath); + if (audioStream == null || MusicPlayer == null) return; + + // 停止当前正在进行的淡入淡出效果 + MusicFadeTween?.Kill(); + + MusicPlayer.Stream = audioStream; + MusicPlayer.VolumeDb = LinearToDb(volume * MusicVolume * MasterVolume); + MusicPlayer.Play(); + } + + /// + /// 播放音效 + /// + /// 音频文件路径 + /// 音量大小,范围0-1 + /// 音调调整 + public virtual void PlaySound(string audioPath, float volume = 1.0f, float pitch = 1.0f) + { + if (AvailableSoundPlayers.Count == 0) return; + + var audioStream = ResourceLoadSystem?.LoadResource(audioPath); + if (audioStream == null) return; + + var player = AvailableSoundPlayers.Dequeue(); + player.Stream = audioStream; + player.VolumeDb = LinearToDb(volume * SoundVolume * MasterVolume); + player.Play(); + } + + /// + /// 播放特效音效 + /// + /// 音频文件路径 + /// 音量大小,范围0-1 + /// 音调调整 + public virtual void PlaySfx(string audioPath, float volume = 1.0f, float pitch = 1.0f) + { + if (AvailableSoundPlayers.Count == 0) return; + + var audioStream = ResourceLoadSystem?.LoadResource(audioPath); + if (audioStream == null) return; + + var player = AvailableSoundPlayers.Dequeue(); + player.Stream = audioStream; + player.VolumeDb = LinearToDb(volume * SfxVolume * MasterVolume); + player.Play(); + } + + /// + /// 播放语音 + /// + /// 音频文件路径 + /// 音量大小,范围0-1 + /// 音调调整 + public virtual void PlayVoice(string audioPath, float volume = 1.0f, float pitch = 1.0f) + { + if (AvailableSoundPlayers.Count == 0) return; + + var audioStream = ResourceLoadSystem?.LoadResource(audioPath); + if (audioStream == null) return; + + var player = AvailableSoundPlayers.Dequeue(); + player.Stream = audioStream; + player.VolumeDb = LinearToDb(volume * VoiceVolume * MasterVolume); + player.Play(); + } + + /// + /// 播放环境音效 + /// + /// 音频文件路径 + /// 音量大小,范围0-1 + /// 音调调整 + public virtual void PlayAmbient(string audioPath, float volume = 1.0f, float pitch = 1.0f) + { + if (AvailableSoundPlayers.Count == 0) return; + + var audioStream = ResourceLoadSystem?.LoadResource(audioPath); + if (audioStream == null) return; + + var player = AvailableSoundPlayers.Dequeue(); + player.Stream = audioStream; + player.VolumeDb = LinearToDb(volume * AmbientVolume * MasterVolume); + player.Play(); + } + + /// + /// 播放3D音效 + /// + /// 音频文件路径 + /// 3D空间中的位置 + /// 音量大小,范围0-1 + public virtual void PlaySound3D(string audioPath, Vector3 position, float volume = 1.0f) + { + if (AvailableSound3DPlayers.Count == 0) return; + + var audioStream = ResourceLoadSystem?.LoadResource(audioPath); + if (audioStream == null) return; + + var player = AvailableSound3DPlayers.Dequeue(); + player.Stream = audioStream; + player.VolumeDb = LinearToDb(volume * SoundVolume * MasterVolume); + player.Position = position; + player.Play(); + } + + /// + /// 停止背景音乐 + /// + public virtual void StopMusic() + { + MusicFadeTween?.Kill(); + MusicPlayer?.Stop(); + } + + /// + /// 暂停背景音乐 + /// + public virtual void PauseMusic() + { + MusicFadeTween?.Kill(); + // todo 需要记录音乐播放位置,以便恢复播放时从正确位置开始 + } + + /// + /// 恢复背景音乐播放 + /// + public virtual void ResumeMusic() + { + MusicPlayer?.Play(); + } + + /// + /// 设置背景音乐音量 + /// + /// 音量大小,范围0-1 + public virtual void SetMusicVolume(float volume) + { + MusicVolume = volume; + if (MusicPlayer != null) MusicPlayer.VolumeDb = LinearToDb(MusicVolume * MasterVolume); + } + + /// + /// 获取背景音乐音量 + /// + /// 音量大小,范围0-1 + public virtual float GetMusicVolume() + { + return MusicVolume; + } + + /// + /// 设置音效音量 + /// + /// 音量大小,范围0-1 + public virtual void SetSoundVolume(float volume) + { + SoundVolume = volume; + } + + /// + /// 获取音效音量 + /// + /// 音量大小,范围0-1 + public virtual float GetSoundVolume() + { + return SoundVolume; + } + + /// + /// 设置主音量 + /// + /// 音量大小,范围0-1 + public virtual void SetMasterVolume(float volume) + { + MasterVolume = volume; + + // 更新音乐音量 + if (MusicPlayer != null) MusicPlayer.VolumeDb = LinearToDb(MusicVolume * MasterVolume); + } + + /// + /// 获取主音量 + /// + /// 音量大小,范围0-1 + public virtual float GetMasterVolume() + { + return MasterVolume; + } + + /// + /// 设置SFX音量 + /// + /// 音量大小,范围0-1 + public virtual void SetSfxVolume(float volume) + { + SfxVolume = volume; + } + + /// + /// 获取SFX音量 + /// + /// 音量大小,范围0-1 + public virtual float GetSfxVolume() + { + return SfxVolume; + } + + /// + /// 设置语音音量 + /// + /// 音量大小,范围0-1 + public virtual void SetVoiceVolume(float volume) + { + VoiceVolume = volume; + } + + /// + /// 获取语音音量 + /// + /// 音量大小,范围0-1 + public virtual float GetVoiceVolume() + { + return VoiceVolume; + } + + /// + /// 设置环境音量 + /// + /// 音量大小,范围0-1 + public virtual void SetAmbientVolume(float volume) + { + AmbientVolume = volume; + } + + /// + /// 获取环境音量 + /// + /// 音量大小,范围0-1 + public virtual float GetAmbientVolume() + { + return AmbientVolume; + } + + /// + /// 检查背景音乐是否正在播放 + /// + /// 正在播放返回true,否则返回false + public virtual bool IsMusicPlaying() + { + return MusicPlayer?.Playing ?? false; + } + + /// + /// 淡入背景音乐 + /// + /// 音频文件路径 + /// 淡入持续时间(秒) + /// 目标音量 + public virtual void FadeInMusic(string audioPath, float duration, float volume = 1.0f) + { + var audioStream = ResourceLoadSystem?.LoadResource(audioPath); + if (audioStream == null || MusicPlayer == null) return; + + // 停止当前正在进行的淡入淡出效果 + MusicFadeTween?.Kill(); + + MusicPlayer.Stream = audioStream; + MusicPlayer.VolumeDb = LinearToDb(0.0f); // 初始音量为0 + MusicPlayer.Play(); + + // 创建淡入动画 + MusicFadeTween = Owner.CreateTween(); + MusicFadeTween.TweenProperty(MusicPlayer, "volume_db", LinearToDb(volume * MusicVolume * MasterVolume), + duration); + } + + /// + /// 淡出背景音乐 + /// + /// 淡出持续时间(秒) + public virtual void FadeOutMusic(float duration) + { + if (MusicPlayer == null) return; + + // 停止当前正在进行的淡入淡出效果 + MusicFadeTween?.Kill(); + + // 创建淡出动画 + MusicFadeTween = Owner.CreateTween(); + MusicFadeTween.TweenProperty(MusicPlayer, "volume_db", LinearToDb(0.0f), duration); + MusicFadeTween.TweenCallback(Callable.From(() => MusicPlayer.Stop())); + } + + /// + /// 设置低通滤波器强度 + /// + /// 滤波器强度,范围0-1 + public virtual void SetLowPassFilter(float amount) + { + // TODO: 实现低通滤波器效果 + // 可以通过AudioEffectLowPassFilter实现 + } + + /// + /// 设置音频混响效果 + /// + /// 房间大小 + /// 阻尼 + /// 湿声级别 + public virtual void SetReverb(float roomSize, float damping, float wetLevel) + { + // TODO: 实现音频混响效果 + // 可以通过AudioEffectReverb实现 + } + + /// + /// 系统初始化方法 /// protected override void OnInit() { @@ -135,7 +460,7 @@ public abstract class AbstractAudioManagerSystem : AbstractSystem, IAudioManager } /// - /// 当音效播放完成时的回调 + /// 当音效播放完成时的回调 /// /// 完成播放的音频播放器 private void OnSoundFinished(AudioStreamPlayer player) @@ -145,7 +470,7 @@ public abstract class AbstractAudioManagerSystem : AbstractSystem, IAudioManager } /// - /// 当3D音效播放完成时的回调 + /// 当3D音效播放完成时的回调 /// /// 完成播放的3D音频播放器 private void OnSound3DFinished(AudioStreamPlayer3D player) @@ -155,26 +480,7 @@ public abstract class AbstractAudioManagerSystem : AbstractSystem, IAudioManager } /// - /// 播放背景音乐 - /// - /// 音频文件路径 - /// 音量大小,范围0-1 - /// 是否循环播放 - public virtual void PlayMusic(string audioPath, float volume = 1.0f, bool loop = true) - { - var audioStream = ResourceLoadSystem?.LoadResource(audioPath); - if (audioStream == null || MusicPlayer == null) return; - - // 停止当前正在进行的淡入淡出效果 - MusicFadeTween?.Kill(); - - MusicPlayer.Stream = audioStream; - MusicPlayer.VolumeDb = LinearToDb(volume * MusicVolume * MasterVolume); - MusicPlayer.Play(); - } - - /// - /// 通过资源ID播放背景音乐 + /// 通过资源ID播放背景音乐 /// /// 音乐资源ID /// 音量大小,范围0-1 @@ -185,83 +491,7 @@ public abstract class AbstractAudioManagerSystem : AbstractSystem, IAudioManager } /// - /// 播放音效 - /// - /// 音频文件路径 - /// 音量大小,范围0-1 - /// 音调调整 - public virtual void PlaySound(string audioPath, float volume = 1.0f, float pitch = 1.0f) - { - if (AvailableSoundPlayers.Count == 0) return; - - var audioStream = ResourceLoadSystem?.LoadResource(audioPath); - if (audioStream == null) return; - - var player = AvailableSoundPlayers.Dequeue(); - player.Stream = audioStream; - player.VolumeDb = LinearToDb(volume * SoundVolume * MasterVolume); - player.Play(); - } - - /// - /// 播放特效音效 - /// - /// 音频文件路径 - /// 音量大小,范围0-1 - /// 音调调整 - public virtual void PlaySfx(string audioPath, float volume = 1.0f, float pitch = 1.0f) - { - if (AvailableSoundPlayers.Count == 0) return; - - var audioStream = ResourceLoadSystem?.LoadResource(audioPath); - if (audioStream == null) return; - - var player = AvailableSoundPlayers.Dequeue(); - player.Stream = audioStream; - player.VolumeDb = LinearToDb(volume * SfxVolume * MasterVolume); - player.Play(); - } - - /// - /// 播放语音 - /// - /// 音频文件路径 - /// 音量大小,范围0-1 - /// 音调调整 - public virtual void PlayVoice(string audioPath, float volume = 1.0f, float pitch = 1.0f) - { - if (AvailableSoundPlayers.Count == 0) return; - - var audioStream = ResourceLoadSystem?.LoadResource(audioPath); - if (audioStream == null) return; - - var player = AvailableSoundPlayers.Dequeue(); - player.Stream = audioStream; - player.VolumeDb = LinearToDb(volume * VoiceVolume * MasterVolume); - player.Play(); - } - - /// - /// 播放环境音效 - /// - /// 音频文件路径 - /// 音量大小,范围0-1 - /// 音调调整 - public virtual void PlayAmbient(string audioPath, float volume = 1.0f, float pitch = 1.0f) - { - if (AvailableSoundPlayers.Count == 0) return; - - var audioStream = ResourceLoadSystem?.LoadResource(audioPath); - if (audioStream == null) return; - - var player = AvailableSoundPlayers.Dequeue(); - player.Stream = audioStream; - player.VolumeDb = LinearToDb(volume * AmbientVolume * MasterVolume); - player.Play(); - } - - /// - /// 通过资源ID播放音效 + /// 通过资源ID播放音效 /// /// 音效资源ID /// 音量大小,范围0-1 @@ -272,243 +502,7 @@ public abstract class AbstractAudioManagerSystem : AbstractSystem, IAudioManager } /// - /// 播放3D音效 - /// - /// 音频文件路径 - /// 3D空间中的位置 - /// 音量大小,范围0-1 - public virtual void PlaySound3D(string audioPath, Vector3 position, float volume = 1.0f) - { - if (AvailableSound3DPlayers.Count == 0) return; - - var audioStream = ResourceLoadSystem?.LoadResource(audioPath); - if (audioStream == null) return; - - var player = AvailableSound3DPlayers.Dequeue(); - player.Stream = audioStream; - player.VolumeDb = LinearToDb(volume * SoundVolume * MasterVolume); - player.Position = position; - player.Play(); - } - - /// - /// 停止背景音乐 - /// - public virtual void StopMusic() - { - MusicFadeTween?.Kill(); - MusicPlayer?.Stop(); - } - - /// - /// 暂停背景音乐 - /// - public virtual void PauseMusic() - { - MusicFadeTween?.Kill(); - // todo 需要记录音乐播放位置,以便恢复播放时从正确位置开始 - } - - /// - /// 恢复背景音乐播放 - /// - public virtual void ResumeMusic() - { - MusicPlayer?.Play(); - } - - /// - /// 设置背景音乐音量 - /// - /// 音量大小,范围0-1 - public virtual void SetMusicVolume(float volume) - { - MusicVolume = volume; - if (MusicPlayer != null) - { - MusicPlayer.VolumeDb = LinearToDb(MusicVolume * MasterVolume); - } - } - - /// - /// 获取背景音乐音量 - /// - /// 音量大小,范围0-1 - public virtual float GetMusicVolume() - { - return MusicVolume; - } - - /// - /// 设置音效音量 - /// - /// 音量大小,范围0-1 - public virtual void SetSoundVolume(float volume) - { - SoundVolume = volume; - } - - /// - /// 获取音效音量 - /// - /// 音量大小,范围0-1 - public virtual float GetSoundVolume() - { - return SoundVolume; - } - - /// - /// 设置主音量 - /// - /// 音量大小,范围0-1 - public virtual void SetMasterVolume(float volume) - { - MasterVolume = volume; - - // 更新音乐音量 - if (MusicPlayer != null) - { - MusicPlayer.VolumeDb = LinearToDb(MusicVolume * MasterVolume); - } - } - - /// - /// 获取主音量 - /// - /// 音量大小,范围0-1 - public virtual float GetMasterVolume() - { - return MasterVolume; - } - - /// - /// 设置SFX音量 - /// - /// 音量大小,范围0-1 - public virtual void SetSfxVolume(float volume) - { - SfxVolume = volume; - } - - /// - /// 获取SFX音量 - /// - /// 音量大小,范围0-1 - public virtual float GetSfxVolume() - { - return SfxVolume; - } - - /// - /// 设置语音音量 - /// - /// 音量大小,范围0-1 - public virtual void SetVoiceVolume(float volume) - { - VoiceVolume = volume; - } - - /// - /// 获取语音音量 - /// - /// 音量大小,范围0-1 - public virtual float GetVoiceVolume() - { - return VoiceVolume; - } - - /// - /// 设置环境音量 - /// - /// 音量大小,范围0-1 - public virtual void SetAmbientVolume(float volume) - { - AmbientVolume = volume; - } - - /// - /// 获取环境音量 - /// - /// 音量大小,范围0-1 - public virtual float GetAmbientVolume() - { - return AmbientVolume; - } - - /// - /// 检查背景音乐是否正在播放 - /// - /// 正在播放返回true,否则返回false - public virtual bool IsMusicPlaying() - { - return MusicPlayer?.Playing ?? false; - } - - /// - /// 淡入背景音乐 - /// - /// 音频文件路径 - /// 淡入持续时间(秒) - /// 目标音量 - public virtual void FadeInMusic(string audioPath, float duration, float volume = 1.0f) - { - var audioStream = ResourceLoadSystem?.LoadResource(audioPath); - if (audioStream == null || MusicPlayer == null) return; - - // 停止当前正在进行的淡入淡出效果 - MusicFadeTween?.Kill(); - - MusicPlayer.Stream = audioStream; - MusicPlayer.VolumeDb = LinearToDb(0.0f); // 初始音量为0 - MusicPlayer.Play(); - - // 创建淡入动画 - MusicFadeTween = Owner.CreateTween(); - MusicFadeTween.TweenProperty(MusicPlayer, "volume_db", LinearToDb(volume * MusicVolume * MasterVolume), - duration); - } - - /// - /// 淡出背景音乐 - /// - /// 淡出持续时间(秒) - public virtual void FadeOutMusic(float duration) - { - if (MusicPlayer == null) return; - - // 停止当前正在进行的淡入淡出效果 - MusicFadeTween?.Kill(); - - // 创建淡出动画 - MusicFadeTween = Owner.CreateTween(); - MusicFadeTween.TweenProperty(MusicPlayer, "volume_db", LinearToDb(0.0f), duration); - MusicFadeTween.TweenCallback(Callable.From(() => MusicPlayer.Stop())); - } - - /// - /// 设置低通滤波器强度 - /// - /// 滤波器强度,范围0-1 - public virtual void SetLowPassFilter(float amount) - { - // TODO: 实现低通滤波器效果 - // 可以通过AudioEffectLowPassFilter实现 - } - - /// - /// 设置音频混响效果 - /// - /// 房间大小 - /// 阻尼 - /// 湿声级别 - public virtual void SetReverb(float roomSize, float damping, float wetLevel) - { - // TODO: 实现音频混响效果 - // 可以通过AudioEffectReverb实现 - } - - /// - /// 将线性音量值转换为分贝值 + /// 将线性音量值转换为分贝值 /// /// 线性音量值(0-1) /// 分贝值 @@ -518,7 +512,7 @@ public abstract class AbstractAudioManagerSystem : AbstractSystem, IAudioManager } /// - /// 将分贝值转换为线性音量值 + /// 将分贝值转换为线性音量值 /// /// 分贝值 /// 线性音量值(0-1) @@ -528,7 +522,7 @@ public abstract class AbstractAudioManagerSystem : AbstractSystem, IAudioManager } /// - /// 系统销毁时清理资源 + /// 系统销毁时清理资源 /// protected override void OnDestroy() { @@ -539,16 +533,10 @@ public abstract class AbstractAudioManagerSystem : AbstractSystem, IAudioManager MusicPlayer?.QueueFree(); // 清理音效播放器池 - foreach (var player in SoundPlayers) - { - player.QueueFree(); - } + foreach (var player in SoundPlayers) player.QueueFree(); // 清理3D音效播放器池 - foreach (var player in Sound3DPlayers) - { - player.QueueFree(); - } + foreach (var player in Sound3DPlayers) player.QueueFree(); SoundPlayers.Clear(); AvailableSoundPlayers.Clear(); diff --git a/GFramework.Godot/system/IAudioManagerSystem.cs b/GFramework.Godot/system/IAudioManagerSystem.cs index e035bc2..e9c9c35 100644 --- a/GFramework.Godot/system/IAudioManagerSystem.cs +++ b/GFramework.Godot/system/IAudioManagerSystem.cs @@ -4,173 +4,173 @@ using Godot; namespace GFramework.Godot.system; /// -/// 音频管理器系统接口,用于统一管理背景音乐和音效的播放 +/// 音频管理器系统接口,用于统一管理背景音乐和音效的播放 /// public interface IAudioManagerSystem : ISystem { /// - /// 播放背景音乐 + /// 播放背景音乐 /// /// 音频文件路径 /// 音量大小,范围0-1 /// 是否循环播放 void PlayMusic(string audioPath, float volume = 1.0f, bool loop = true); - + /// - /// 播放音效 + /// 播放音效 /// /// 音频文件路径 /// 音量大小,范围0-1 /// 音调调整 void PlaySound(string audioPath, float volume = 1.0f, float pitch = 1.0f); - + /// - /// 播放特效音效 + /// 播放特效音效 /// /// 音频文件路径 /// 音量大小,范围0-1 /// 音调调整 void PlaySfx(string audioPath, float volume = 1.0f, float pitch = 1.0f); - + /// - /// 播放语音 + /// 播放语音 /// /// 音频文件路径 /// 音量大小,范围0-1 /// 音调调整 void PlayVoice(string audioPath, float volume = 1.0f, float pitch = 1.0f); - + /// - /// 播放环境音效 + /// 播放环境音效 /// /// 音频文件路径 /// 音量大小,范围0-1 /// 音调调整 void PlayAmbient(string audioPath, float volume = 1.0f, float pitch = 1.0f); - + /// - /// 停止背景音乐 + /// 停止背景音乐 /// void StopMusic(); - + /// - /// 暂停背景音乐 + /// 暂停背景音乐 /// void PauseMusic(); - + /// - /// 恢复背景音乐播放 + /// 恢复背景音乐播放 /// void ResumeMusic(); - + /// - /// 设置背景音乐音量 + /// 设置背景音乐音量 /// /// 音量大小,范围0-1 void SetMusicVolume(float volume); - + /// - /// 获取背景音乐音量 + /// 获取背景音乐音量 /// /// 音量大小,范围0-1 float GetMusicVolume(); - + /// - /// 设置音效音量 + /// 设置音效音量 /// /// 音量大小,范围0-1 void SetSoundVolume(float volume); - + /// - /// 获取音效音量 + /// 获取音效音量 /// /// 音量大小,范围0-1 float GetSoundVolume(); - + /// - /// 设置特效音量 + /// 设置特效音量 /// /// 音量大小,范围0-1 void SetSfxVolume(float volume); - + /// - /// 获取特效音量 + /// 获取特效音量 /// /// 音量大小,范围0-1 float GetSfxVolume(); - + /// - /// 设置语音音量 + /// 设置语音音量 /// /// 音量大小,范围0-1 void SetVoiceVolume(float volume); - + /// - /// 获取语音音量 + /// 获取语音音量 /// /// 音量大小,范围0-1 float GetVoiceVolume(); - + /// - /// 设置环境音量 + /// 设置环境音量 /// /// 音量大小,范围0-1 void SetAmbientVolume(float volume); - + /// - /// 获取环境音量 + /// 获取环境音量 /// /// 音量大小,范围0-1 float GetAmbientVolume(); - + /// - /// 设置主音量 + /// 设置主音量 /// /// 音量大小,范围0-1 void SetMasterVolume(float volume); - + /// - /// 获取主音量 + /// 获取主音量 /// /// 音量大小,范围0-1 float GetMasterVolume(); - + /// - /// 检查背景音乐是否正在播放 + /// 检查背景音乐是否正在播放 /// /// 正在播放返回true,否则返回false bool IsMusicPlaying(); - + /// - /// 淡入背景音乐 + /// 淡入背景音乐 /// /// 音频文件路径 /// 淡入持续时间(秒) /// 目标音量 void FadeInMusic(string audioPath, float duration, float volume = 1.0f); - + /// - /// 淡出背景音乐 + /// 淡出背景音乐 /// /// 淡出持续时间(秒) void FadeOutMusic(float duration); - + /// - /// 播放3D音效 + /// 播放3D音效 /// /// 音频文件路径 /// 3D空间中的位置 /// 音量大小,范围0-1 void PlaySound3D(string audioPath, Vector3 position, float volume = 1.0f); - + /// - /// 设置低通滤波器强度 + /// 设置低通滤波器强度 /// /// 滤波器强度,范围0-1 void SetLowPassFilter(float amount); - + /// - /// 设置音频混响效果 + /// 设置音频混响效果 /// /// 房间大小 /// 阻尼 diff --git a/GFramework.SourceGenerators.Attributes/GFramework.SourceGenerators.Attributes.csproj b/GFramework.SourceGenerators.Attributes/GFramework.SourceGenerators.Attributes.csproj index 698a56e..5ec19a5 100644 --- a/GFramework.SourceGenerators.Attributes/GFramework.SourceGenerators.Attributes.csproj +++ b/GFramework.SourceGenerators.Attributes/GFramework.SourceGenerators.Attributes.csproj @@ -6,6 +6,6 @@ 10 - + diff --git a/GFramework.SourceGenerators.Attributes/enums/EnumExtensionsAttribute.cs b/GFramework.SourceGenerators.Attributes/enums/EnumExtensionsAttribute.cs index 8799cfa..1e388ca 100644 --- a/GFramework.SourceGenerators.Attributes/enums/EnumExtensionsAttribute.cs +++ b/GFramework.SourceGenerators.Attributes/enums/EnumExtensionsAttribute.cs @@ -1,21 +1,20 @@ using System; -namespace GFramework.SourceGenerators.Attributes.enums +namespace GFramework.SourceGenerators.Attributes.enums; + +/// +/// 标注在 enum 上,Source Generator 会为该 enum 生成扩展方法。 +/// +[AttributeUsage(AttributeTargets.Enum)] +public sealed class GenerateEnumExtensionsAttribute : Attribute { /// - /// 标注在 enum 上,Source Generator 会为该 enum 生成扩展方法。 + /// 是否为每个枚举项生成单独的 IsXXX 方法(默认 true)。 /// - [AttributeUsage(AttributeTargets.Enum)] - public sealed class GenerateEnumExtensionsAttribute : Attribute - { - /// - /// 是否为每个枚举项生成单独的 IsXXX 方法(默认 true)。 - /// - public bool GenerateIsMethods { get; set; } = true; + public bool GenerateIsMethods { get; set; } = true; - /// - /// 是否生成一个 IsIn(params T[]) 方法以简化多值判断(默认 true)。 - /// - public bool GenerateIsInMethod { get; set; } = true; - } + /// + /// 是否生成一个 IsIn(params T[]) 方法以简化多值判断(默认 true)。 + /// + public bool GenerateIsInMethod { get; set; } = true; } \ No newline at end of file diff --git a/GFramework.SourceGenerators.Attributes/logging/LogAttribute.cs b/GFramework.SourceGenerators.Attributes/logging/LogAttribute.cs index aff9912..a38f0d3 100644 --- a/GFramework.SourceGenerators.Attributes/logging/LogAttribute.cs +++ b/GFramework.SourceGenerators.Attributes/logging/LogAttribute.cs @@ -4,7 +4,7 @@ using System; namespace GFramework.SourceGenerators.Attributes.logging; /// -/// 标注在类上,Source Generator 会为该类自动生成一个日志记录器字段。 +/// 标注在类上,Source Generator 会为该类自动生成一个日志记录器字段。 /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] public sealed class LogAttribute : Attribute @@ -14,7 +14,7 @@ public sealed class LogAttribute : Attribute } /// - /// 初始化 GodotLogAttribute 类的新实例 + /// 初始化 GodotLogAttribute 类的新实例 /// /// 日志分类名,默认使用类名 public LogAttribute(string? name) diff --git a/GFramework.SourceGenerators/logging/Diagnostic.cs b/GFramework.SourceGenerators/logging/Diagnostic.cs deleted file mode 100644 index 10aea63..0000000 --- a/GFramework.SourceGenerators/logging/Diagnostic.cs +++ /dev/null @@ -1,37 +0,0 @@ -using Microsoft.CodeAnalysis; - -namespace GFramework.SourceGenerators.logging; - -/// -/// 提供诊断描述符的静态类,用于GFramework日志生成器的编译时检查 -/// -internal static class LoggerDiagnostics -{ - /// - /// 定义诊断描述符:要求使用[Log]特性的类必须声明为partial - /// - /// - /// 当类使用[Log]特性但未声明为partial时,编译器将报告此错误 - /// - public static readonly DiagnosticDescriptor MustBePartial = - new( - id: "GFLOG001", - title: "Class must be partial", - messageFormat: "Class '{0}' must be declared partial to use [Log]", - category: "GFramework.Logging", - DiagnosticSeverity.Error, - isEnabledByDefault: true - ); - - /// - /// 定义诊断描述符:LogAttribute无法生成Logger的错误情况 - /// - public static readonly DiagnosticDescriptor LogAttributeInvalid = - new( - id: "GFW_LOG001", - title: "LogAttribute cannot generate Logger", - messageFormat: "LogAttribute on class '{0}' is ineffective: {1}", - category: "GFramework.Godot.Logging", - DiagnosticSeverity.Warning, - isEnabledByDefault: true); -} \ No newline at end of file diff --git a/GFramework.SourceGenerators/logging/LoggerDiagnostic.cs b/GFramework.SourceGenerators/logging/LoggerDiagnostic.cs new file mode 100644 index 0000000..8448556 --- /dev/null +++ b/GFramework.SourceGenerators/logging/LoggerDiagnostic.cs @@ -0,0 +1,37 @@ +using Microsoft.CodeAnalysis; + +namespace GFramework.SourceGenerators.logging; + +/// +/// 提供诊断描述符的静态类,用于GFramework日志生成器的编译时检查 +/// +internal static class LoggerDiagnostics +{ + /// + /// 定义诊断描述符:要求使用[Log]特性的类必须声明为partial + /// + /// + /// 当类使用[Log]特性但未声明为partial时,编译器将报告此错误 + /// + public static readonly DiagnosticDescriptor MustBePartial = + new( + "GFLOG001", + "Class must be partial", + "Class '{0}' must be declared partial to use [Log]", + "GFramework.Logging", + DiagnosticSeverity.Error, + true + ); + + /// + /// 定义诊断描述符:LogAttribute无法生成Logger的错误情况 + /// + public static readonly DiagnosticDescriptor LogAttributeInvalid = + new( + "GFW_LOG001", + "LogAttribute cannot generate Logger", + "LogAttribute on class '{0}' is ineffective: {1}", + "GFramework.Godot.Logging", + DiagnosticSeverity.Warning, + true); +} \ No newline at end of file diff --git a/GFramework.SourceGenerators/logging/LoggerGenerator.cs b/GFramework.SourceGenerators/logging/LoggerGenerator.cs index 20a0bcd..d4899d9 100644 --- a/GFramework.SourceGenerators/logging/LoggerGenerator.cs +++ b/GFramework.SourceGenerators/logging/LoggerGenerator.cs @@ -12,7 +12,7 @@ using Microsoft.CodeAnalysis.Text; namespace GFramework.SourceGenerators.logging; /// -/// 日志生成器,用于为标记了LogAttribute的类自动生成日志字段 +/// 日志生成器,用于为标记了LogAttribute的类自动生成日志字段 /// [Generator] public sealed class LoggerGenerator : IIncrementalGenerator @@ -22,7 +22,7 @@ public sealed class LoggerGenerator : IIncrementalGenerator private const string AttributeShortNameWithoutSuffix = "Log"; /// - /// 初始化生成器,设置语法过滤和代码生成逻辑 + /// 初始化生成器,设置语法过滤和代码生成逻辑 /// /// 增量生成器初始化上下文 public void Initialize(IncrementalGeneratorInitializationContext context) @@ -87,7 +87,7 @@ public sealed class LoggerGenerator : IIncrementalGenerator } /// - /// 获取类符号上的LogAttribute特性 + /// 获取类符号上的LogAttribute特性 /// /// 类符号 /// LogAttribute特性数据,如果不存在则返回null @@ -105,7 +105,7 @@ public sealed class LoggerGenerator : IIncrementalGenerator } /// - /// 生成日志字段代码 + /// 生成日志字段代码 /// /// 类符号 /// LogAttribute特性数据 @@ -160,7 +160,7 @@ public sealed class LoggerGenerator : IIncrementalGenerator sb.AppendLine($" public partial class {className}"); sb.AppendLine(" {"); - sb.AppendLine($" /// Auto-generated logger"); + sb.AppendLine(" /// Auto-generated logger"); sb.AppendLine( $" {access} {staticKeyword}readonly ILogger {fieldName} = " + $"new ConsoleLoggerFactory().GetLogger(\"{name}\");"); @@ -173,7 +173,7 @@ public sealed class LoggerGenerator : IIncrementalGenerator } /// - /// 从特性数据中获取命名参数的值 + /// 从特性数据中获取命名参数的值 /// /// 特性数据 /// 参数名称 diff --git a/GFramework.csproj b/GFramework.csproj index a9aa03e..5a873a8 100644 --- a/GFramework.csproj +++ b/GFramework.csproj @@ -20,10 +20,10 @@ - - - - + + + + GFramework.SorceGenerators\bin\Debug\netstandard2.0\GFramework.Generator.Attributes.dll @@ -47,13 +47,13 @@ - - + + - - - + + + GFramework.SorceGenerators\enums\EnumExtensionsGenerator.cs @@ -70,9 +70,9 @@ - - - + + +