- 新增 IShrinkEventBus、ShrinkEventBusBuilder 与 ShrinkEventBusInstance,支持独立 bus、异常策略、启动开关、类型校验和按 phase 派发 - 新增 SubscribeEvent 可释放订阅句柄与订阅快照接口,保留 EventBus 静态门面并委托到默认实例 - 重构监听列表为父子事件链共享锁与快照缓存,支持父事件监听子事件、跨父子数字优先级归并和防御性快照拷贝 - 优化 EventBase 元数据缓存、懒生成 EventId、派发快照挂接与对象池双重归还保护,移除旧 EventCache 热路径 - 恢复本地 ILPostProcessor 兜底并生成静态订阅注册表,避免共享 CodeGen 覆盖外程序集自动注册失效 - 改进事件查看器、Benchmark、README 与 CHANGELOG,并新增 Dispatch/Priority/Registration/Subscription 测试覆盖核心行为 BREAKING CHANGE: 移除 EventBase.GetListenerList(),int 数字优先级重载不再提供默认值,数字优先级 0 现在映射为 NORMAL。
142 lines
5.7 KiB
C#
142 lines
5.7 KiB
C#
using System;
|
|
using System.Reflection;
|
|
using Cysharp.Threading.Tasks;
|
|
#pragma warning disable CS8632 // 只能在 "#nullable" 注释上下文内的代码中使用可为 null 的引用类型的注释。
|
|
|
|
|
|
namespace ShrinkEventBus
|
|
{
|
|
public interface IMethodWrapper
|
|
{
|
|
MethodInfo OriginalMethod { get; }
|
|
}
|
|
|
|
public class EventHandlerInfo
|
|
{
|
|
public long SubscriptionId { get; }
|
|
public Delegate Handler { get; }
|
|
internal Action<EventBase>? SyncInvoker { get; }
|
|
internal Func<EventBase, UniTask>? AsyncInvoker { get; }
|
|
public EventPriority Priority { get; }
|
|
public int NumericPriority { get; }
|
|
public bool ReceiveCanceled { get; }
|
|
public object? Target { get; }
|
|
public MethodInfo Method { get; }
|
|
public string DebugInfo { get; }
|
|
public Type DeclaringType { get; }
|
|
public string MethodName { get; }
|
|
public MethodInfo? OriginalMethod { get; }
|
|
public string OriginalMethodName { get; }
|
|
public Type? OriginalDeclaringType { get; }
|
|
public DateTime RegisteredAtUtc { get; }
|
|
|
|
private EventHandlerInfo(long subscriptionId, Delegate handler, Action<EventBase>? syncInvoker,
|
|
Func<EventBase, UniTask>? asyncInvoker,
|
|
EventPriority priority, int numericPriority, bool receiveCanceled, string debugInfo = "",
|
|
MethodInfo? originalMethod = null)
|
|
{
|
|
SubscriptionId = subscriptionId;
|
|
Handler = handler;
|
|
SyncInvoker = syncInvoker;
|
|
AsyncInvoker = asyncInvoker;
|
|
Priority = priority;
|
|
NumericPriority = numericPriority;
|
|
ReceiveCanceled = receiveCanceled;
|
|
Target = handler.Target;
|
|
Method = handler.Method;
|
|
DebugInfo = debugInfo;
|
|
DeclaringType = Method.DeclaringType ?? typeof(object);
|
|
MethodName = Method.Name;
|
|
|
|
OriginalMethod = ExtractOriginalMethodFromWrapper(handler) ?? originalMethod;
|
|
|
|
if (OriginalMethod != null)
|
|
{
|
|
OriginalMethodName = OriginalMethod.Name;
|
|
OriginalDeclaringType = OriginalMethod.DeclaringType;
|
|
}
|
|
else
|
|
{
|
|
OriginalMethodName = MethodName;
|
|
OriginalDeclaringType = DeclaringType;
|
|
}
|
|
|
|
RegisteredAtUtc = DateTime.UtcNow;
|
|
}
|
|
|
|
private static MethodInfo? ExtractOriginalMethodFromWrapper(Delegate handler)
|
|
{
|
|
if (handler.Target is IMethodWrapper wrapper)
|
|
{
|
|
return wrapper.OriginalMethod;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public string DisplayMethodName => OriginalMethodName;
|
|
public Type DisplayDeclaringType => OriginalDeclaringType ?? DeclaringType;
|
|
|
|
public bool MatchesMethod(MethodInfo method)
|
|
{
|
|
return method != null && (ReferenceEquals(Method, method) || ReferenceEquals(OriginalMethod, method));
|
|
}
|
|
|
|
public bool MatchesDeclaringType(Type declaringType)
|
|
{
|
|
return declaringType != null &&
|
|
(DeclaringType == declaringType || OriginalDeclaringType == declaringType);
|
|
}
|
|
|
|
public static EventHandlerInfo Create(long subscriptionId, Delegate handler, Type eventType,
|
|
EventPriority priority, int numericPriority, bool receiveCanceled, string debugInfo = "",
|
|
MethodInfo? originalMethod = null)
|
|
{
|
|
if (handler == null)
|
|
throw new System.ArgumentNullException(nameof(handler));
|
|
if (eventType == null)
|
|
throw new System.ArgumentNullException(nameof(eventType));
|
|
|
|
Action<EventBase>? syncInvoker = null;
|
|
Func<EventBase, UniTask>? asyncInvoker = null;
|
|
if (handler.Method.ReturnType == typeof(void))
|
|
syncInvoker = CreateSyncInvoker(eventType, handler);
|
|
else if (handler.Method.ReturnType == typeof(UniTask))
|
|
asyncInvoker = CreateAsyncInvoker(eventType, handler);
|
|
else
|
|
throw new System.ArgumentException(
|
|
$"Unsupported event handler return type {handler.Method.ReturnType.FullName} for {handler.Method}.");
|
|
|
|
return new EventHandlerInfo(subscriptionId, handler, syncInvoker, asyncInvoker, priority, numericPriority,
|
|
receiveCanceled, debugInfo, originalMethod);
|
|
}
|
|
|
|
private static Action<EventBase> CreateSyncInvoker(Type eventType, Delegate handler)
|
|
{
|
|
var factory = typeof(EventHandlerInfo).GetMethod(nameof(CreateSyncInvokerGeneric),
|
|
BindingFlags.NonPublic | BindingFlags.Static)!.MakeGenericMethod(eventType);
|
|
return (Action<EventBase>)factory.Invoke(null, new object[] { handler })!;
|
|
}
|
|
|
|
private static Func<EventBase, UniTask> CreateAsyncInvoker(Type eventType, Delegate handler)
|
|
{
|
|
var factory = typeof(EventHandlerInfo).GetMethod(nameof(CreateAsyncInvokerGeneric),
|
|
BindingFlags.NonPublic | BindingFlags.Static)!.MakeGenericMethod(eventType);
|
|
return (Func<EventBase, UniTask>)factory.Invoke(null, new object[] { handler })!;
|
|
}
|
|
|
|
private static Action<EventBase> CreateSyncInvokerGeneric<TEvent>(Delegate handler) where TEvent : EventBase
|
|
{
|
|
var typedHandler = (Action<TEvent>)handler;
|
|
return eventArgs => typedHandler((TEvent)eventArgs);
|
|
}
|
|
|
|
private static Func<EventBase, UniTask> CreateAsyncInvokerGeneric<TEvent>(Delegate handler)
|
|
where TEvent : EventBase
|
|
{
|
|
var typedHandler = (Func<TEvent, UniTask>)handler;
|
|
return eventArgs => typedHandler((TEvent)eventArgs);
|
|
}
|
|
}
|
|
}
|