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

202 lines
5.8 KiB
C#
Raw Normal View History

2023-12-21 15:59:18 +08:00
using System;
2023-08-22 19:08:45 +08:00
using System.Collections.Generic;
2023-10-19 15:00:19 +08:00
using PhxhSDK;
2023-08-22 19:08:45 +08:00
namespace Framework
{
2023-10-19 15:00:19 +08:00
public partial class EventManager : Singlenton<EventManager>
2023-08-22 19:08:45 +08:00
{
2023-11-01 13:33:20 +08:00
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;
}
2023-08-22 19:08:45 +08:00
public const int MAX_EVENT_STACK = 3;
2023-11-01 13:33:20 +08:00
2023-08-22 19:08:45 +08:00
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)
{
2023-10-19 15:00:19 +08:00
DebugUtil.LogError("EventSystem.Register<T> : {0}, {1}", eventName, e);
2023-08-22 19:08:45 +08:00
}
}
2023-11-01 13:33:20 +08:00
2023-08-22 19:08:45 +08:00
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)
{
2023-10-19 15:00:19 +08:00
DebugUtil.LogError("EventSystem.Register : {0}, {1}", eventName, e);
2023-08-22 19:08:45 +08:00
}
}
public void Unregister(EventName eventName, Action action)
{
try
{
if (action == null)
{
return;
}
if (!_eventDict.ContainsKey(eventName))
{
return;
}
_eventDict[eventName].Remove(action);
}
catch (Exception e)
{
2023-10-19 15:00:19 +08:00
DebugUtil.LogError("EventSystem.Unregister : {0}, {1}", eventName, e);
2023-08-22 19:08:45 +08:00
}
}
2023-11-01 13:33:20 +08:00
2023-08-22 19:08:45 +08:00
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)
{
2023-10-19 15:00:19 +08:00
DebugUtil.LogError("EventSystem.Unregister<T> : {0}, {1}", eventName, e);
2023-08-22 19:08:45 +08:00
}
}
public void Send<T>(EventName eventName, T arg)
{
try
{
_eventSendingStack.Push(eventName);
if (EventStackReachMax(_eventSendingStack.Count))
2023-08-22 19:08:45 +08:00
{
DebugUtil.LogError("EventSystem.Send<{0}> : {1}, {2} reach max event count {3}", typeof(T).Name, eventName, arg, MAX_EVENT_STACK);
foreach (var eventNameInStack in _eventSendingStack)
{
DebugUtil.LogError("Event Stack : {0}", eventNameInStack);
}
2023-08-22 19:08:45 +08:00
return;
}
//DebugUtil.Log($"EventSystem.Send<T> : {eventName}");
if (!_eventDict.ContainsKey(eventName))
{
_eventSendingStack.Pop();
2023-11-01 13:33:20 +08:00
_afterSendCallBack?.Invoke(eventName);
2023-08-22 19:08:45 +08:00
return;
}
2023-11-01 13:33:20 +08:00
2023-08-22 19:08:45 +08:00
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);
}
2023-11-03 12:39:51 +08:00
//空参数函数也要调用
if (e.Current is Action action)
{
action();
}
2023-08-22 19:08:45 +08:00
}
2023-11-01 13:33:20 +08:00
2023-08-22 19:08:45 +08:00
ReCycleTempExecuteList(tempExecuteList);
2023-11-01 13:33:20 +08:00
_afterSendCallBack?.Invoke(eventName);
_eventSendingStack.Pop();
2023-08-22 19:08:45 +08:00
}
catch (Exception exception)
{
2023-11-01 13:33:20 +08:00
DebugUtil.LogError("EventSystem.Send<T> : {0}, {1}", eventName, exception);
2023-08-22 19:08:45 +08:00
}
}
public void Send(EventName eventName)
{
try
{
if (!_eventDict.ContainsKey(eventName))
{
2023-11-01 13:33:20 +08:00
_afterSendCallBack?.Invoke(eventName);
2023-08-22 19:08:45 +08:00
return;
}
var actions = _eventDict[eventName];
2023-11-22 15:21:48 +08:00
var tempExecuteList = GetTempExecuteList();
tempExecuteList.AddRange(actions);
foreach (var action in tempExecuteList)
2023-08-22 19:08:45 +08:00
{
if (action is Action actionT)
{
actionT();
}
}
2023-11-22 15:21:48 +08:00
ReCycleTempExecuteList(tempExecuteList);
2023-11-01 13:33:20 +08:00
_afterSendCallBack?.Invoke(eventName);
2023-08-22 19:08:45 +08:00
}
catch (Exception e)
{
2023-10-19 15:00:19 +08:00
DebugUtil.LogError("EventSystem.Send : {0}, {1}", eventName, e);
2023-08-22 19:08:45 +08:00
}
}
2023-11-01 13:33:20 +08:00
2023-08-22 19:08:45 +08:00
private bool EventStackReachMax(int current)
{
if (current > MAX_EVENT_STACK)
{
return true;
}
2023-11-01 13:33:20 +08:00
2023-08-22 19:08:45 +08:00
return false;
}
}
}