- 将 ShrinkEventBus、ShrinkDataSaver 及其 EventBus 集成从 gitlink 转为仓库直接维护的完整 UPM 包,补齐运行时、编辑器工具、测试与文档 - 新增 Command 和 Network 的 App 集成组件,支持 ContextLoader 服务发布、可逆注销及 Network Loopback 生命周期管理 - 更新 Starter 与演示组合逻辑,缺失模块时可注册、已有兼容安装器时可覆盖,并补充宿主启动断言 - 升级内部包依赖与 Shared CodeGen 包定义,放宽 Integration.App 包的 Git 忽略规则 - 将独立服务器生成器改为基于已编译程序集的语义扫描,支持 partial、复杂泛型、命名冲突检测及模板 SHA-256 覆写保护 - 新增 Network 语义扫描、模板保护和 App 组件生命周期测试 - 新增真实 UPM 消费工程验证脚本,校验内部版本一致性、程序集加载及 EditMode 测试 - 重构当前架构文档并归档已完成的 Cordis 迁移与旧代码地图
147 lines
4.4 KiB
C#
147 lines
4.4 KiB
C#
#nullable enable
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Reflection;
|
|
using System.Runtime.Serialization;
|
|
|
|
namespace ShrinkEventBus
|
|
{
|
|
public abstract class EventBase : IDisposable
|
|
{
|
|
private sealed class EventMetadata
|
|
{
|
|
public bool IsCancelable;
|
|
public bool HasResult;
|
|
}
|
|
|
|
private static readonly ConcurrentDictionary<Type, EventMetadata> MetadataCache = new();
|
|
|
|
private EventHandlerInfo[]? _dispatchSnapshot;
|
|
private Guid? _eventId;
|
|
private bool _isCanceled;
|
|
private EventResult _result = EventResult.DEFAULT;
|
|
|
|
[IgnoreDataMember]
|
|
public EventHandlerInfo? CurrentHandler { get; internal set; }
|
|
|
|
[IgnoreDataMember]
|
|
public DateTime EventTime { get; private set; } = DateTime.UtcNow;
|
|
|
|
[IgnoreDataMember]
|
|
public Guid EventId => _eventId ??= Guid.NewGuid();
|
|
|
|
[IgnoreDataMember]
|
|
public bool IsCancelable { get; }
|
|
|
|
[IgnoreDataMember]
|
|
public bool HasResult { get; }
|
|
|
|
[IgnoreDataMember]
|
|
public EventPriority? Phase { get; private set; }
|
|
|
|
internal bool IsInPool { get; set; }
|
|
internal Action<EventBase>? ReleaseAction { get; set; }
|
|
|
|
protected EventBase()
|
|
{
|
|
var metadata = GetOrCreateMetadata(GetType());
|
|
IsCancelable = metadata.IsCancelable;
|
|
HasResult = metadata.HasResult;
|
|
Setup();
|
|
}
|
|
|
|
protected virtual void Setup() { }
|
|
|
|
protected virtual void OnReset() { }
|
|
|
|
internal void ResetInternal()
|
|
{
|
|
_isCanceled = false;
|
|
_result = EventResult.DEFAULT;
|
|
CurrentHandler = null;
|
|
Phase = null;
|
|
EventTime = DateTime.UtcNow;
|
|
_eventId = null;
|
|
_dispatchSnapshot = null;
|
|
OnReset();
|
|
}
|
|
|
|
internal void PrepareForDispatch()
|
|
{
|
|
CurrentHandler = null;
|
|
Phase = null;
|
|
EventTime = DateTime.UtcNow;
|
|
_eventId = null;
|
|
_dispatchSnapshot = null;
|
|
}
|
|
|
|
internal void SetListenerSnapshot(EventHandlerInfo[]? handlers)
|
|
{
|
|
_dispatchSnapshot = handlers is { Length: > 0 } ? handlers : null;
|
|
}
|
|
|
|
[IgnoreDataMember]
|
|
public bool IsCanceled
|
|
{
|
|
get => _isCanceled;
|
|
set
|
|
{
|
|
if (!IsCancelable)
|
|
throw new UnsupportedOperationException(
|
|
$"Event {GetType().Name} is not cancelable. Mark it with [Cancelable] to allow cancellation.");
|
|
_isCanceled = value;
|
|
}
|
|
}
|
|
|
|
[IgnoreDataMember]
|
|
public EventResult Result
|
|
{
|
|
get => _result;
|
|
set
|
|
{
|
|
if (!HasResult)
|
|
throw new InvalidOperationException(
|
|
$"Event {GetType().Name} does not support results. Mark it with [HasResult] to allow setting a result.");
|
|
_result = value;
|
|
}
|
|
}
|
|
|
|
internal void SetPhase(EventPriority value)
|
|
{
|
|
if (Phase == value) return;
|
|
if (Phase != null && Phase.Value.CompareTo(value) > 0)
|
|
throw new ArgumentException(
|
|
$"Event phase cannot move backwards from {Phase.Value} to {value}.", nameof(value));
|
|
Phase = value;
|
|
}
|
|
|
|
public void SetCanceled(bool canceled) => IsCanceled = canceled;
|
|
public void SetResult(EventResult result) => Result = result;
|
|
|
|
public EventHandlerInfo[] GetSubscribers()
|
|
{
|
|
var snapshot = _dispatchSnapshot;
|
|
if (snapshot == null || snapshot.Length == 0)
|
|
return Array.Empty<EventHandlerInfo>();
|
|
|
|
var copy = new EventHandlerInfo[snapshot.Length];
|
|
Array.Copy(snapshot, copy, snapshot.Length);
|
|
return copy;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
ReleaseAction?.Invoke(this);
|
|
}
|
|
|
|
private static EventMetadata GetOrCreateMetadata(Type eventType)
|
|
{
|
|
return MetadataCache.GetOrAdd(eventType, static type => new EventMetadata
|
|
{
|
|
IsCancelable = type.GetCustomAttribute<CancelableAttribute>() != null,
|
|
HasResult = type.GetCustomAttribute<HasResultAttribute>() != null
|
|
});
|
|
}
|
|
}
|
|
}
|