66 lines
1.5 KiB
C#
66 lines
1.5 KiB
C#
|
|
using System;
|
|||
|
|
using AlicizaX.Runtime;
|
|||
|
|
using Cysharp.Threading.Tasks;
|
|||
|
|
|
|||
|
|
namespace AlicizaX.EventKit.Runtime
|
|||
|
|
{
|
|||
|
|
public class EventProcessorAttribute : Attribute
|
|||
|
|
{
|
|||
|
|
public EventProcessorAttribute()
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public interface IEventProcessor
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// <see cref="IEvent.EventType"/>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
Type EventType();
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <see cref="IEvent.Invoke"/>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
UniTask Invoke(object self);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public abstract class EventProcessorHandler<T> : IEventProcessor where T : unmanaged, IEvent
|
|||
|
|
{
|
|||
|
|
private readonly Type _selfType = typeof(T);
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <see cref="IEvent.EventType"/>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public Type EventType()
|
|||
|
|
{
|
|||
|
|
return _selfType;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 事件调用的方法,要在这个方法里编写事件发生的逻辑
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="self"></param>
|
|||
|
|
protected abstract UniTask Handler(T self);
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <see cref="IEvent.Invoke"/>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public async UniTask Invoke(object self)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
await Handler((T)self);
|
|||
|
|
}
|
|||
|
|
catch (Exception e)
|
|||
|
|
{
|
|||
|
|
Log.Error($"{_selfType.Name} Error {e}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|