NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Framework/Event/EventManager.cs

198 lines
5.5 KiB
C#

using System;
using System.Collections.Generic;
using PhxhSDK;
namespace Framework
{
public partial class EventManager : Singlenton<EventManager>
{
private Dictionary<EventName, HashSet<Delegate>> _eventDict = new Dictionary<EventName, HashSet<Delegate>>();
private Action<EventName> _afterSendCallBack;
public event Action<EventName> AfterSendCallBack
{
add => _afterSendCallBack += value;
remove => _afterSendCallBack -= value;
}
public const int MAX_EVENT_STACK = 3;
public void Register<T>(EventName eventName, Action<T> action)
{
try
{
if (action == null)
{
return;
}
if (!_eventDict.ContainsKey(eventName))
{
_eventDict[eventName] = new HashSet<Delegate>();
}
_eventDict[eventName].Add(action);
}
catch (Exception e)
{
DebugUtil.LogError("EventSystem.Register<T> : {0}, {1}", eventName, e);
}
}
public void Register(EventName eventName, Action action)
{
try
{
if (action == null)
{
return;
}
if (!_eventDict.ContainsKey(eventName))
{
_eventDict[eventName] = new HashSet<Delegate>();
}
_eventDict[eventName].Add(action);
}
catch (Exception e)
{
DebugUtil.LogError("EventSystem.Register : {0}, {1}", eventName, e);
}
}
public void Unregister(EventName eventName, Action action)
{
try
{
if (action == null)
{
return;
}
if (!_eventDict.ContainsKey(eventName))
{
return;
}
_eventDict[eventName].Remove(action);
}
catch (Exception e)
{
DebugUtil.LogError("EventSystem.Unregister : {0}, {1}", eventName, e);
}
}
public void Unregister<T>(EventName eventName, Action<T> action)
{
try
{
if (action == null)
{
return;
}
if (!_eventDict.ContainsKey(eventName))
{
return;
}
_eventDict[eventName].Remove(action);
}
catch (Exception e)
{
DebugUtil.LogError("EventSystem.Unregister<T> : {0}, {1}", eventName, e);
}
}
public void Send<T>(EventName eventName, T arg)
{
try
{
if (EventStackReachMax(++_eventStack))
{
DebugUtil.LogError("EventSystem.Send<T> : {0}, {1}", eventName, arg);
return;
}
//DebugUtil.Log($"EventSystem.Send<T> : {eventName}");
if (!_eventDict.ContainsKey(eventName))
{
_eventStack--;
_afterSendCallBack?.Invoke(eventName);
return;
}
var actions = _eventDict[eventName];
var tempExecuteList = GetTempExecuteList();
tempExecuteList.AddRange(actions);
using var e = tempExecuteList.GetEnumerator();
while (e.MoveNext())
{
if (e.Current is Action<T> actionT)
{
actionT(arg);
}
//空参数函数也要调用
if (e.Current is Action action)
{
action();
}
}
ReCycleTempExecuteList(tempExecuteList);
_afterSendCallBack?.Invoke(eventName);
_eventStack--;
}
catch (Exception exception)
{
DebugUtil.LogError("EventSystem.Send<T> : {0}, {1}", eventName, exception);
}
}
public void Send(EventName eventName)
{
try
{
if (!_eventDict.ContainsKey(eventName))
{
_afterSendCallBack?.Invoke(eventName);
return;
}
var actions = _eventDict[eventName];
var tempExecuteList = GetTempExecuteList();
tempExecuteList.AddRange(actions);
foreach (var action in tempExecuteList)
{
if (action is Action actionT)
{
actionT();
}
}
ReCycleTempExecuteList(tempExecuteList);
_afterSendCallBack?.Invoke(eventName);
}
catch (Exception e)
{
DebugUtil.LogError("EventSystem.Send : {0}, {1}", eventName, e);
}
}
private bool EventStackReachMax(int current)
{
if (current > MAX_EVENT_STACK)
{
DebugUtil.EditorAssert(_eventStack <= MAX_EVENT_STACK, $"EventSystem.Send<T> : {MAX_EVENT_STACK}");
return true;
}
return false;
}
}
}