feat: 重构事件总线为可实例化架构并发布 1.3.0

- 新增 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。
This commit is contained in:
cneicy committed 2026-06-18 17:05:16 +08:00
1 parent 2cea8a7ff5
commit 8acccdcc4b
41 files changed
+3168 -888

No files matched your search

+31 -21
View File
@@ -1,4 +1,6 @@
using System.Collections.Generic;
#nullable enable
using System.Collections.Generic;
using System.IO;
using System.Threading;
using Mono.Cecil;
@@ -6,11 +8,11 @@ using Unity.CompilationPipeline.Common.ILPostProcessing;
namespace ShrinkEventBus.CodeGen
{
internal class PostProcessorAssemblyResolver : IAssemblyResolver
internal sealed class PostProcessorAssemblyResolver : IAssemblyResolver
{
private readonly string[] _references;
private readonly Dictionary<string, AssemblyDefinition> _cache = new();
private AssemblyDefinition _self;
private AssemblyDefinition? _self;
public PostProcessorAssemblyResolver(ICompiledAssembly compiledAssembly)
{
@@ -22,10 +24,12 @@ namespace ShrinkEventBus.CodeGen
_self = assemblyDefinition;
}
public AssemblyDefinition Resolve(AssemblyNameReference name)
=> Resolve(name, new ReaderParameters(ReadingMode.Deferred));
public AssemblyDefinition? Resolve(AssemblyNameReference name)
{
return Resolve(name, new ReaderParameters(ReadingMode.Deferred));
}
public AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
public AssemblyDefinition? Resolve(AssemblyNameReference name, ReaderParameters parameters)
{
lock (_cache)
{
@@ -33,10 +37,12 @@ namespace ShrinkEventBus.CodeGen
return _self;
var path = FindPath(name);
if (path == null) return null;
if (path == null)
return null;
var key = $"{path}{File.GetLastWriteTime(path)}";
if (_cache.TryGetValue(key, out var cached)) return cached;
if (_cache.TryGetValue(key, out var cached))
return cached;
parameters.AssemblyResolver = this;
var ms = ReadFileWithRetry(path);
@@ -51,25 +57,27 @@ namespace ShrinkEventBus.CodeGen
}
}
private string FindPath(AssemblyNameReference name)
private string? FindPath(AssemblyNameReference name)
{
foreach (var r in _references)
foreach (var reference in _references)
{
if (Path.GetFileNameWithoutExtension(r) == name.Name)
return r;
if (Path.GetFileNameWithoutExtension(reference) == name.Name)
return reference;
}
var dirs = new HashSet<string>();
foreach (var r in _references)
foreach (var reference in _references)
{
var dir = Path.GetDirectoryName(r);
if (dir != null) dirs.Add(dir);
var dir = Path.GetDirectoryName(reference);
if (dir != null)
dirs.Add(dir);
}
foreach (var dir in dirs)
{
var candidate = Path.Combine(dir, $"{name.Name}.dll");
if (File.Exists(candidate)) return candidate;
if (File.Exists(candidate))
return candidate;
}
return null;
@@ -81,17 +89,19 @@ namespace ShrinkEventBus.CodeGen
{
try
{
var bytes = File.ReadAllBytes(path);
return new MemoryStream(bytes);
return new MemoryStream(File.ReadAllBytes(path));
}
catch (IOException) when (i < retries - 1)
{
Thread.Sleep(100);
}
}
throw new IOException($"[ShrinkEventBus] 无法读取文件: {path}");
throw new IOException($"[ShrinkEventBus.CodeGen] 无法读取文件: {path}");
}
public void Dispose() { }
public void Dispose()
{
}
}
}
}