IndieGame/client/Assets/Ether/Scripts/Common/EventCenter/EventCenter.cs

721 lines
25 KiB
C#
Raw Normal View History

2024-10-11 10:12:15 +08:00
/********************************************************************
: EventCenter.cs
:
: 1982614048@qq.com
: 2024/03/29 17:28:19
:
: 2024/04/03 16:06:58
:
使:
AddListener:
EventCenter.AddListener<NetProtoType, IOCPServerSession, byte[]>(NetProtoType.IOCPReceive, IOCPMsgReceive);
EventCenter.AddListener<MsgId, HttpListenerResponse, MsgData>(MsgId.Regist, RegistEventDispose);
RemoveListener:
EventCenter.RemoveListener<NetProtoType, IOCPServerSession, byte[]>(NetProtoType.IOCPReceive, IOCPMsgReceive);
EventCenter.RemoveListener<MsgId, HttpListenerResponse, MsgData>(MsgId.Regist, RegistEventDispose);
BroadCast:
EventCenter.BroadCast((MsgId)msgData.msgId, session, msgData);
EventCenter.BroadCast(NetProtoType.HTTPPostReceive, response, content);
*********************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// 事件处理
/// </summary>
public class EventCenter
{
#region
private static Dictionary<Type, Dictionary<Enum, Delegate>> enumEventDic = new Dictionary<Type, Dictionary<Enum, Delegate>>();
private static void OnListenerAdding<T>(T eventType, Delegate callBack) where T : Enum
{
Type type = eventType.GetType();
if (!enumEventDic.ContainsKey(type))
{
enumEventDic.Add(type, new Dictionary<Enum, Delegate>());
}
if (!enumEventDic[type].ContainsKey(eventType))
{
enumEventDic[type].Add(eventType, null);
}
Delegate d = enumEventDic[type][eventType];
if (type != null && d != null && d.GetType() != callBack.GetType())
{
throw new Exception(string.Format("尝试为事件{0}添加不同类型的委托,当前事件所对应的委托为{1},要添加的委托类型为{2}", eventType, d.GetType(), callBack.GetType()));
}
}
private static void OnListenerRemoving<T>(T eventType, Delegate callBack) where T : Enum
{
Type type = eventType.GetType();
if (enumEventDic.ContainsKey(type))
{
if (enumEventDic[type].ContainsKey(eventType))
{
Delegate d = enumEventDic[type][eventType];
if (d == null)
{
throw new Exception(string.Format("移除监听错误:事件{0}没有对应的委托", eventType));
}
else if (d.GetType() != callBack.GetType())
{
throw new Exception(string.Format("移除监听错误:尝试为事件{0}移除不同类型的委托,当前委托类型为{1},要移除的委托类型为{2}", eventType, d.GetType(), callBack.GetType()));
}
return;
}
}
throw new Exception(string.Format("移除监听错误:没有事件码{0}", eventType));
}
private static void OnListenerRemoved<T>(T eventType) where T : Enum
{
Type type = eventType.GetType();
if (enumEventDic[type][eventType] == null)
{
enumEventDic[type].Remove(eventType);
}
if (enumEventDic[type] == null || enumEventDic[type].Count == 0)
{
enumEventDic.Remove(type);
}
}
//==================================================监听事件========================================================
/// <summary>
/// 无参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void AddListener<T>(T eventType, Action callBack) where T : Enum
{
OnListenerAdding(eventType, callBack);
enumEventDic[eventType.GetType()][eventType] = (Action)enumEventDic[eventType.GetType()][eventType] + callBack;
}
/// <summary>
/// 一个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void AddListener<T, K>(T eventType, Action<K> callBack) where T : Enum
{
OnListenerAdding(eventType, callBack);
enumEventDic[eventType.GetType()][eventType] = (Action<K>)enumEventDic[eventType.GetType()][eventType] + callBack;
}
/// <summary>
/// 两个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void AddListener<T, K, X>(T eventType, Action<K, X> callBack) where T : Enum
{
OnListenerAdding(eventType, callBack);
enumEventDic[eventType.GetType()][eventType] = (Action<K, X>)enumEventDic[eventType.GetType()][eventType] + callBack;
}
/// <summary>
/// 三个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void AddListener<T, K, X, Y>(T eventType, Action<K, X, Y> callBack) where T : Enum
{
OnListenerAdding(eventType, callBack);
enumEventDic[eventType.GetType()][eventType] = (Action<K, X, Y>)enumEventDic[eventType.GetType()][eventType] + callBack;
}
/// <summary>
/// 四个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void AddListener<T, K, X, Y, Z>(T eventType, Action<K, X, Y, Z> callBack) where T : Enum
{
OnListenerAdding(eventType, callBack);
enumEventDic[eventType.GetType()][eventType] = (Action<K, X, Y, Z>)enumEventDic[eventType.GetType()][eventType] + callBack;
}
/// <summary>
/// 五个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void AddListener<T, K, X, Y, Z, W>(T eventType, Action<K, X, Y, Z, W> callBack) where T : Enum
{
OnListenerAdding(eventType, callBack);
enumEventDic[eventType.GetType()][eventType] = (Action<K, X, Y, Z, W>)enumEventDic[eventType.GetType()][eventType] + callBack;
}
//==================================================移除监听========================================================
/// <summary>
/// 无参移除监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void RemoveListener<T>(T eventType, Action callBack) where T : Enum
{
OnListenerRemoving(eventType, callBack);
enumEventDic[eventType.GetType()][eventType] = (Action)enumEventDic[eventType.GetType()][eventType] - callBack;
OnListenerRemoved(eventType);
}
/// <summary>
/// 一个参数移除监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void RemoveListener<T, K>(T eventType, Action<K> callBack) where T : Enum
{
OnListenerRemoving(eventType, callBack);
enumEventDic[eventType.GetType()][eventType] = (Action<K>)enumEventDic[eventType.GetType()][eventType] - callBack;
OnListenerRemoved(eventType);
}
/// <summary>
/// 两个参数移除监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void RemoveListener<T, K, X>(T eventType, Action<K, X> callBack) where T : Enum
{
OnListenerRemoving(eventType, callBack);
enumEventDic[eventType.GetType()][eventType] = (Action<K, X>)enumEventDic[eventType.GetType()][eventType] - callBack;
OnListenerRemoved(eventType);
}
/// <summary>
/// 三个参数移除监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void RemoveListener<T, K, X, Y>(T eventType, Action<K, X, Y> callBack) where T : Enum
{
OnListenerRemoving(eventType, callBack);
enumEventDic[eventType.GetType()][eventType] = (Action<K, X, Y>)enumEventDic[eventType.GetType()][eventType] - callBack;
OnListenerRemoved(eventType);
}
/// <summary>
/// 四个参数移除监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void RemoveListener<T, K, X, Y, Z>(T eventType, Action<K, X, Y, Z> callBack) where T : Enum
{
OnListenerRemoving(eventType, callBack);
enumEventDic[eventType.GetType()][eventType] = (Action<K, X, Y, Z>)enumEventDic[eventType.GetType()][eventType] - callBack;
OnListenerRemoved(eventType);
}
/// <summary>
/// 五个参数移除监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void RemoveListener<T, K, X, Y, Z, W>(T eventType, Action<K, X, Y, Z, W> callBack) where T : Enum
{
OnListenerRemoving(eventType, callBack);
enumEventDic[eventType.GetType()][eventType] = (Action<K, X, Y, Z, W>)enumEventDic[eventType.GetType()][eventType] - callBack;
OnListenerRemoved(eventType);
}
//==================================================广播事件========================================================
/// <summary>
/// 无参广播事件
/// </summary>
/// <param name="eventType">事件</param>
public static void BroadCast<T>(T eventType) where T : Enum
{
Delegate d;
Type type = eventType.GetType();
if (enumEventDic.ContainsKey(type))
{
if (enumEventDic[type].TryGetValue(eventType, out d))
{
Action callBack = d as Action;
if (callBack != null)
{
callBack();
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
}
}
}
}
/// <summary>
/// 一个参数广播事件
/// </summary>
/// <param name="eventType">事件</param>
public static void BroadCast<T, K>(T eventType, K arg) where T : Enum
{
Delegate d;
Type type = eventType.GetType();
if (enumEventDic.ContainsKey(type))
{
if (enumEventDic[type].TryGetValue(eventType, out d))
{
Action<K> callBack = d as Action<K>;
if (callBack != null)
{
callBack(arg);
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
}
}
}
}
/// <summary>
/// 两个参数广播事件
/// </summary>
/// <param name="eventType">事件</param>
public static void BroadCast<T, K, X>(T eventType, K arg1, X arg2) where T : Enum
{
Delegate d;
Type type = eventType.GetType();
if (enumEventDic.ContainsKey(type))
{
if (enumEventDic[type].TryGetValue(eventType, out d))
{
Action<K, X> callBack = d as Action<K, X>;
if (callBack != null)
{
callBack(arg1, arg2);
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
}
}
}
}
/// <summary>
/// 三个参数广播事件
/// </summary>
/// <param name="eventType">事件</param>
public static void BroadCast<T, K, X, Y>(T eventType, K arg1, X arg2, Y arg3) where T : Enum
{
Delegate d;
Type type = eventType.GetType();
if (enumEventDic.ContainsKey(type))
{
if (enumEventDic[type].TryGetValue(eventType, out d))
{
Action<K, X, Y> callBack = d as Action<K, X, Y>;
if (callBack != null)
{
callBack(arg1, arg2, arg3);
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
}
}
}
}
/// <summary>
/// 四个参数广播事件
/// </summary>
/// <param name="eventType">事件</param>
public static void BroadCast<T, K, X, Y, Z>(T eventType, K arg1, X arg2, Y arg3, Z arg4) where T : Enum
{
Delegate d;
Type type = eventType.GetType();
if (enumEventDic.ContainsKey(type))
{
if (enumEventDic[type].TryGetValue(eventType, out d))
{
Action<K, X, Y, Z> callBack = d as Action<K, X, Y, Z>;
if (callBack != null)
{
callBack(arg1, arg2, arg3, arg4);
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
}
}
}
}
/// <summary>
/// 五个参数广播事件
/// </summary>
/// <param name="eventType">事件</param>
public static void BroadCast<T, K, X, Y, Z, W>(T eventType, K arg1, X arg2, Y arg3, Z arg4, W arg5) where T : Enum
{
Delegate d;
Type type = eventType.GetType();
if (enumEventDic.ContainsKey(type))
{
if (enumEventDic[type].TryGetValue(eventType, out d))
{
Action<K, X, Y, Z, W> callBack = d as Action<K, X, Y, Z, W>;
if (callBack != null)
{
callBack(arg1, arg2, arg3, arg4, arg5);
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
}
}
}
}
#endregion
#region
private static Dictionary<string, Delegate> normalEventDic = new Dictionary<string, Delegate>();
private static void OnListenerAdding(string eventType, Delegate callBack)
{
if (!normalEventDic.ContainsKey(eventType))
{
normalEventDic.Add(eventType, null);
}
Delegate d = normalEventDic[eventType];
if (!string.IsNullOrEmpty(eventType) && d != null && d.GetType() != callBack.GetType())
{
throw new Exception(string.Format("尝试为事件{0}添加不同类型的委托,当前事件所对应的委托为{1},要添加的委托类型为{2}", eventType, d.GetType(), callBack.GetType()));
}
}
private static void OnListenerRemoving(string eventType, Delegate callBack)
{
if (normalEventDic.ContainsKey(eventType))
{
Delegate d = normalEventDic[eventType];
if (d == null)
{
throw new Exception(string.Format("移除监听错误:事件{0}没有对应的委托", eventType));
}
else if (d.GetType() != callBack.GetType())
{
throw new Exception(string.Format("移除监听错误:尝试为事件{0}移除不同类型的委托,当前委托类型为{1},要移除的委托类型为{2}", eventType, d.GetType(), callBack.GetType()));
}
return;
}
throw new Exception(string.Format("移除监听错误:没有事件码{0}", eventType));
}
private static void OnListenerRemoved(string eventType)
{
if (normalEventDic[eventType] == null)
{
normalEventDic.Remove(eventType);
}
}
//==================================================监听事件========================================================
/// <summary>
/// 无参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void AddListener(string eventType, Action callBack)
{
OnListenerAdding(eventType, callBack);
normalEventDic[eventType] = (Action)normalEventDic[eventType] + callBack;
}
/// <summary>
/// 一个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void AddListener<K>(string eventType, Action<K> callBack)
{
OnListenerAdding(eventType, callBack);
normalEventDic[eventType] = (Action<K>)normalEventDic[eventType] + callBack;
}
/// <summary>
/// 两个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void AddListener<K, X>(string eventType, Action<K, X> callBack)
{
OnListenerAdding(eventType, callBack);
normalEventDic[eventType] = (Action<K, X>)normalEventDic[eventType] + callBack;
}
/// <summary>
/// 三个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void AddListener<K, X, Y>(string eventType, Action<K, X, Y> callBack)
{
OnListenerAdding(eventType, callBack);
normalEventDic[eventType] = (Action<K, X, Y>)normalEventDic[eventType] + callBack;
}
/// <summary>
/// 四个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void AddListener<K, X, Y, Z>(string eventType, Action<K, X, Y, Z> callBack)
{
OnListenerAdding(eventType, callBack);
normalEventDic[eventType] = (Action<K, X, Y, Z>)normalEventDic[eventType] + callBack;
}
/// <summary>
/// 五个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void AddListener<K, X, Y, Z, W>(string eventType, Action<K, X, Y, Z, W> callBack)
{
OnListenerAdding(eventType, callBack);
normalEventDic[eventType] = (Action<K, X, Y, Z, W>)normalEventDic[eventType] + callBack;
}
//==================================================移除监听========================================================
/// <summary>
/// 无参移除监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void RemoveListener(string eventType, Action callBack)
{
OnListenerRemoving(eventType, callBack);
normalEventDic[eventType] = (Action)normalEventDic[eventType] - callBack;
OnListenerRemoved(eventType);
}
/// <summary>
/// 一个参数移除监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void RemoveListener<K>(string eventType, Action<K> callBack)
{
OnListenerRemoving(eventType, callBack);
normalEventDic[eventType] = (Action<K>)normalEventDic[eventType] - callBack;
OnListenerRemoved(eventType);
}
/// <summary>
/// 两个参数移除监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void RemoveListener<K, X>(string eventType, Action<K, X> callBack)
{
OnListenerRemoving(eventType, callBack);
normalEventDic[eventType] = (Action<K, X>)normalEventDic[eventType] - callBack;
OnListenerRemoved(eventType);
}
/// <summary>
/// 三个参数移除监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void RemoveListener<K, X, Y>(string eventType, Action<K, X, Y> callBack)
{
OnListenerRemoving(eventType, callBack);
normalEventDic[eventType] = (Action<K, X, Y>)normalEventDic[eventType] - callBack;
OnListenerRemoved(eventType);
}
/// <summary>
/// 四个参数移除监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void RemoveListener<K, X, Y, Z>(string eventType, Action<K, X, Y, Z> callBack)
{
OnListenerRemoving(eventType, callBack);
normalEventDic[eventType] = (Action<K, X, Y, Z>)normalEventDic[eventType] - callBack;
OnListenerRemoved(eventType);
}
/// <summary>
/// 五个参数移除监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void RemoveListener<K, X, Y, Z, W>(string eventType, Action<K, X, Y, Z, W> callBack)
{
OnListenerRemoving(eventType, callBack);
normalEventDic[eventType] = (Action<K, X, Y, Z, W>)normalEventDic[eventType] - callBack;
OnListenerRemoved(eventType);
}
//==================================================广播事件========================================================
/// <summary>
/// 无参广播事件
/// </summary>
/// <param name="eventType">事件</param>
public static void BroadCast(string eventType)
{
Delegate d;
if (normalEventDic.TryGetValue(eventType, out d))
{
Action callBack = d as Action;
if (callBack != null)
{
callBack();
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
}
}
}
/// <summary>
/// 一个参数广播事件
/// </summary>
/// <param name="eventType">事件</param>
public static void BroadCast<K>(string eventType, K arg)
{
Delegate d;
if (normalEventDic.TryGetValue(eventType, out d))
{
Action<K> callBack = d as Action<K>;
if (callBack != null)
{
callBack(arg);
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
}
}
}
/// <summary>
/// 两个参数广播事件
/// </summary>
/// <param name="eventType">事件</param>
public static void BroadCast<K, X>(string eventType, K arg1, X arg2)
{
Delegate d;
if (normalEventDic.TryGetValue(eventType, out d))
{
Action<K, X> callBack = d as Action<K, X>;
if (callBack != null)
{
callBack(arg1, arg2);
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
}
}
}
/// <summary>
/// 三个参数广播事件
/// </summary>
/// <param name="eventType">事件</param>
public static void BroadCast<K, X, Y>(string eventType, K arg1, X arg2, Y arg3)
{
Delegate d;
if (normalEventDic.TryGetValue(eventType, out d))
{
Action<K, X, Y> callBack = d as Action<K, X, Y>;
if (callBack != null)
{
callBack(arg1, arg2, arg3);
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
}
}
}
/// <summary>
/// 四个参数广播事件
/// </summary>
/// <param name="eventType">事件</param>
public static void BroadCast<K, X, Y, Z>(string eventType, K arg1, X arg2, Y arg3, Z arg4)
{
Delegate d;
if (normalEventDic.TryGetValue(eventType, out d))
{
Action<K, X, Y, Z> callBack = d as Action<K, X, Y, Z>;
if (callBack != null)
{
callBack(arg1, arg2, arg3, arg4);
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
}
}
}
/// <summary>
/// 五个参数广播事件
/// </summary>
/// <param name="eventType">事件</param>
public static void BroadCast<K, X, Y, Z, W>(string eventType, K arg1, X arg2, Y arg3, Z arg4, W arg5)
{
Delegate d;
if (normalEventDic.TryGetValue(eventType, out d))
{
Action<K, X, Y, Z, W> callBack = d as Action<K, X, Y, Z, W>;
if (callBack != null)
{
callBack(arg1, arg2, arg3, arg4, arg5);
}
else
{
throw new Exception(string.Format("广播事件错误:事件{0}对应委托具有不同的类型", eventType));
}
}
}
#endregion
/// <summary>
/// 清理
/// </summary>
public static void Clear()
{
enumEventDic.Clear();
normalEventDic.Clear();
}
}