refactor(architecture): 移除架构上下文中的日志属性依赖

- 从 ArchitectureContext 构造函数中移除 LoggerProperties 参数
- 更新 Architecture 类中上下文初始化逻辑,不再传递日志属性
- 优化事件系统中的空值安全处理
- 修正 EasyEvent 泛型类中的类型参数命名
- 移除不再需要的 IsExternalInit 内部实现
- 更新包引用配置添加 PrivateAssets 属性
This commit is contained in:
GwWuYou 2026-01-01 22:13:06 +08:00
parent 0599f289f0
commit 2f5af5c067
14 changed files with 47 additions and 100 deletions

View File

@ -233,7 +233,7 @@ public abstract class Architecture(
// 设置日志工厂提供程序,用于创建日志记录器
LoggerFactoryResolver.Provider = Configuration.LoggerProperties.LoggerFactoryProvider;
_logger = LoggerFactoryResolver.Provider.CreateLogger(GetType().Name);
_context ??= new ArchitectureContext(Container, TypeEventSystem, Configuration.LoggerProperties);
_context ??= new ArchitectureContext(Container, TypeEventSystem);
GameContext.Bind(GetType(), _context);
// 创建架构运行时实例
Runtime = new ArchitectureRuntime(_context);
@ -289,7 +289,7 @@ public abstract class Architecture(
LoggerFactoryResolver.Provider = Configuration.LoggerProperties.LoggerFactoryProvider;
// 创建日志记录器
_logger = LoggerFactoryResolver.Provider.CreateLogger(GetType().Name);
_context ??= new ArchitectureContext(Container, TypeEventSystem, Configuration.LoggerProperties);
_context ??= new ArchitectureContext(Container, TypeEventSystem);
GameContext.Bind(GetType(), _context);
// 创建架构运行时实例
Runtime = new ArchitectureRuntime(_context);

View File

@ -3,7 +3,6 @@ using GFramework.Core.Abstractions.command;
using GFramework.Core.Abstractions.events;
using GFramework.Core.Abstractions.ioc;
using GFramework.Core.Abstractions.model;
using GFramework.Core.Abstractions.properties;
using GFramework.Core.Abstractions.query;
using GFramework.Core.Abstractions.system;
using GFramework.Core.Abstractions.utility;
@ -15,8 +14,7 @@ namespace GFramework.Core.architecture;
/// </summary>
public class ArchitectureContext(
IIocContainer container,
ITypeEventSystem typeEventSystem,
LoggerProperties loggerProperties)
ITypeEventSystem typeEventSystem)
: IArchitectureContext
{
private readonly IIocContainer _container = container ?? throw new ArgumentNullException(nameof(container));

View File

@ -8,7 +8,7 @@ namespace GFramework.Core.events;
/// <param name="onUnRegister">注销时要执行的回调函数</param>
public class DefaultUnRegister(Action onUnRegister) : IUnRegister
{
private Action _mOnUnRegister = onUnRegister;
private Action? _mOnUnRegister = onUnRegister;
/// <summary>
/// 执行注销操作,调用注册的回调函数并清理引用
@ -16,7 +16,7 @@ public class DefaultUnRegister(Action onUnRegister) : IUnRegister
public void UnRegister()
{
// 调用注销回调函数并清理引用
_mOnUnRegister.Invoke();
_mOnUnRegister?.Invoke();
_mOnUnRegister = null;
}
}

View File

@ -7,7 +7,7 @@ namespace GFramework.Core.events;
/// </summary>
public class EasyEvent
{
private Action _mOnEvent = () => { };
private Action? _mOnEvent = () => { };
/// <summary>
/// 注册事件回调函数

View File

@ -13,7 +13,7 @@ public class EasyEvent<T> : IEasyEvent
/// 存储已注册的事件处理委托。
/// 默认为空操作no-op委托避免 null 检查。
/// </summary>
private Action<T> _mOnEvent = e => { };
private Action<T>? _mOnEvent = _ => { };
/// <summary>
/// 显式实现 IEasyEvent 接口中的 Register 方法。
@ -66,14 +66,14 @@ public class EasyEvent<T> : IEasyEvent
/// 提供事件注册、注销和触发功能。
/// </summary>
/// <typeparam name="T">第一个参数类型。</typeparam>
/// <typeparam name="TK">第二个参数类型。</typeparam>
public class EasyEvent<T, TK> : IEasyEvent
/// <typeparam name="Tk">第二个参数类型。</typeparam>
public class EasyEvent<T, Tk> : IEasyEvent
{
/// <summary>
/// 存储已注册的双参数事件处理委托。
/// 默认为空操作no-op委托。
/// </summary>
private Action<T, TK> _mOnEvent = (_, _) => { };
private Action<T, Tk>? _mOnEvent = (_, _) => { };
/// <summary>
/// 显式实现 IEasyEvent 接口中的 Register 方法。
@ -85,7 +85,7 @@ public class EasyEvent<T, TK> : IEasyEvent
{
return Register(Action);
void Action(T _, TK __)
void Action(T _, Tk __)
{
onEvent();
}
@ -96,7 +96,7 @@ public class EasyEvent<T, TK> : IEasyEvent
/// </summary>
/// <param name="onEvent">要注册的事件处理方法。</param>
/// <returns>IUnRegister 对象,用于稍后注销该事件监听器。</returns>
public IUnRegister Register(Action<T, TK> onEvent)
public IUnRegister Register(Action<T, Tk> onEvent)
{
_mOnEvent += onEvent;
return new DefaultUnRegister(() => { UnRegister(onEvent); });
@ -106,7 +106,7 @@ public class EasyEvent<T, TK> : IEasyEvent
/// 取消指定的双参数事件监听器。
/// </summary>
/// <param name="onEvent">需要被注销的事件处理方法。</param>
public void UnRegister(Action<T, TK> onEvent)
public void UnRegister(Action<T, Tk> onEvent)
{
_mOnEvent -= onEvent;
}
@ -116,71 +116,8 @@ public class EasyEvent<T, TK> : IEasyEvent
/// </summary>
/// <param name="t">第一个参数。</param>
/// <param name="k">第二个参数。</param>
public void Trigger(T t, TK k)
public void Trigger(T t, Tk k)
{
_mOnEvent?.Invoke(t, k);
}
}
/// <summary>
/// 支持三个泛型参数 T、TK 和 TS 的事件类。
/// 提供事件注册、注销和触发功能。
/// </summary>
/// <typeparam name="T">第一个参数类型。</typeparam>
/// <typeparam name="TK">第二个参数类型。</typeparam>
/// <typeparam name="TS">第三个参数类型。</typeparam>
public class EasyEvent<T, TK, TS> : IEasyEvent
{
/// <summary>
/// 存储已注册的三参数事件处理委托。
/// 默认为空操作no-op委托。
/// </summary>
private Action<T, TK, TS> _mOnEvent = (_, _, _) => { };
/// <summary>
/// 显式实现 IEasyEvent 接口中的 Register 方法。
/// 允许使用无参 Action 来订阅当前带参事件。
/// </summary>
/// <param name="onEvent">无参事件处理方法。</param>
/// <returns>IUnRegister 对象,用于稍后注销该事件监听器。</returns>
IUnRegister IEasyEvent.Register(Action onEvent)
{
return Register(Action);
void Action(T _, TK __, TS ___)
{
onEvent();
}
}
/// <summary>
/// 注册一个接受三个参数的事件监听器,并返回可用于取消注册的对象。
/// </summary>
/// <param name="onEvent">要注册的事件处理方法。</param>
/// <returns>IUnRegister 对象,用于稍后注销该事件监听器。</returns>
public IUnRegister Register(Action<T, TK, TS> onEvent)
{
_mOnEvent += onEvent;
return new DefaultUnRegister(() => { UnRegister(onEvent); });
}
/// <summary>
/// 取消指定的三参数事件监听器。
/// </summary>
/// <param name="onEvent">需要被注销的事件处理方法。</param>
public void UnRegister(Action<T, TK, TS> onEvent)
{
_mOnEvent -= onEvent;
}
/// <summary>
/// 触发所有已注册的事件处理程序,并传递参数 t、k 和 s。
/// </summary>
/// <param name="t">第一个参数。</param>
/// <param name="k">第二个参数。</param>
/// <param name="s">第三个参数。</param>
public void Trigger(T t, TK k, TS s)
{
_mOnEvent?.Invoke(t, k, s);
}
}

View File

@ -53,7 +53,7 @@ public class EasyEvents
/// <returns>指定类型的事件实例,如果不存在则返回默认值</returns>
public T GetEvent<T>() where T : IEasyEvent
{
return _mTypeEvents.TryGetValue(typeof(T), out var e) ? (T)e : default;
return _mTypeEvents.TryGetValue(typeof(T), out var e) ? (T)e : default!;
}
/// <summary>

View File

@ -8,12 +8,12 @@ namespace GFramework.Core.events;
/// </summary>
public class OrEvent : IUnRegisterList
{
private Action _mOnEvent = () => { };
private Action? _mOnEvent = () => { };
/// <summary>
/// 获取取消注册列表
/// </summary>
public IList<IUnRegister> UnregisterList { get; }
public IList<IUnRegister> UnregisterList { get; } = new List<IUnRegister>();
/// <summary>
/// 将指定的事件与当前OrEvent进行或逻辑组合

View File

@ -12,7 +12,7 @@ public class UnRegisterList : IUnRegisterList
/// <summary>
/// 获取取消注册列表的只读属性
/// </summary>
public IList<IUnRegister> UnregisterList { get; }
public IList<IUnRegister> UnregisterList { get; } = null!;
/// <summary>
/// 向取消注册列表中添加一个新的可取消注册对象

View File

@ -12,7 +12,7 @@ public abstract class AbstractModel : IModel
/// <summary>
/// 模型所属的架构实例
/// </summary>
protected IArchitectureContext _context { get; private set; }
protected IArchitectureContext _context { get; private set; } = null!;
/// <summary>
/// 初始化模型调用抽象方法OnInit执行具体初始化逻辑

View File

@ -14,20 +14,25 @@ public class BindablePropertyUnRegister<T>(BindableProperty<T> bindableProperty,
/// <summary>
/// 获取或设置可绑定属性实例
/// </summary>
public BindableProperty<T> BindableProperty { get; set; } = bindableProperty;
public BindableProperty<T>? BindableProperty { get; set; } = bindableProperty;
/// <summary>
/// 获取或设置值变化时的回调函数
/// </summary>
public Action<T> OnValueChanged { get; set; } = onValueChanged;
public Action<T>? OnValueChanged { get; set; } = onValueChanged;
/// <summary>
/// 执行注销操作,取消注册值变化监听并清理引用
/// </summary>
public void UnRegister()
{
// 调用可绑定属性的注销方法,传入值变化回调函数
BindableProperty.UnRegister(OnValueChanged);
// 检查两个引用都不为null时才执行注销操作
if (BindableProperty != null && OnValueChanged != null)
{
// 调用可绑定属性的注销方法,传入值变化回调函数
BindableProperty.UnRegister(OnValueChanged);
}
// 清理属性引用
BindableProperty = null;
// 清理回调函数引用

View File

@ -1,10 +1,4 @@
namespace System.Runtime.CompilerServices
{
// 这个类用于支持C# 9.0中的init访问器和记录类型功能
internal static class IsExternalInit;
}
namespace GFramework.Game.Abstractions.assets
namespace GFramework.Game.Abstractions.assets
{
/// <summary>
/// 资源目录类,用于定义和管理游戏中的场景和资源标识符

View File

@ -0,0 +1,12 @@
// IsExternalInit.cs
// This type is required to support init-only setters and record types
// when targeting netstandard2.0 or older frameworks.
#pragma warning disable S2094 // Remove this empty class
namespace System.Runtime.CompilerServices
{
internal static class IsExternalInit
{
}
}
#pragma warning restore S2094

View File

@ -8,8 +8,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Godot.SourceGenerators" Version="4.5.1"/>
<PackageReference Include="GodotSharpEditor" Version="4.5.1"/>
<PackageReference Include="Godot.SourceGenerators" Version="4.5.1" PrivateAssets="all"/>
<PackageReference Include="GodotSharpEditor" Version="4.5.1" PrivateAssets="all"/>
</ItemGroup>
<ItemGroup>

View File

@ -3,6 +3,7 @@
### New Rules
Rule ID | Category | Severity | Notes
-------------|----------------------------------|----------|------------------------
GF_Rule_001 | GFramework.SourceGenerators.rule | Error | ContextAwareDiagnostic
Rule ID | Category | Severity | Notes
----------------|----------------------------------|----------|------------------------
GF_Logging_001 | GFramework.Godot.logging | Warning | LoggerDiagnostics
GF_Rule_001 | GFramework.SourceGenerators.rule | Error | ContextAwareDiagnostic