/******************************************************************** 文件: EventCenter.cs 作者: 梦语 邮箱: 1982614048@qq.com 创建时间: 2024/03/29 17:28:19 最后修改: 梦语 最后修改时间: 2024/04/03 16:06:58 功能: 事件处理中心 使用方法: AddListener: EventCenter.AddListener(NetProtoType.IOCPReceive, IOCPMsgReceive); EventCenter.AddListener(MsgId.Regist, RegistEventDispose); RemoveListener: EventCenter.RemoveListener(NetProtoType.IOCPReceive, IOCPMsgReceive); EventCenter.RemoveListener(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; /// /// 事件处理 /// public class EventCenter { #region 普通事件 private static Dictionary allEventDic = new Dictionary(); private static void OnListenerAdding(string eventType, Delegate callback) { allEventDic.TryAdd(eventType, null); Delegate d = allEventDic[eventType]; if (!string.IsNullOrEmpty(eventType) && d != null && d.GetType() != callback.GetType()) { throw new Exception($"尝试为事件{eventType}添加不同类型的委托,当前事件所对应的委托为{d.GetType()},要添加的委托类型为{callback.GetType()}"); } } private static void OnListenerRemoving(string eventType, Delegate callback) { if (allEventDic.ContainsKey(eventType)) { Delegate d = allEventDic[eventType]; if (d == null) { throw new Exception($"移除监听错误:事件{eventType}没有对应的委托"); } else if (d.GetType() != callback.GetType()) { throw new Exception( $"移除监听错误:尝试为事件{eventType}移除不同类型的委托,当前委托类型为{d.GetType()},要移除的委托类型为{callback.GetType()}"); } return; } throw new Exception($"移除监听错误:没有事件码{eventType}"); } private static void OnListenerRemoved(string eventType) { if (allEventDic[eventType] == null) { allEventDic.Remove(eventType); } } //==================================================监听事件======================================================== /// /// 无参数监听 /// /// 事件 /// 回调 public static void AddListener(string eventType, Action callback) { OnListenerAdding(eventType, callback); allEventDic[eventType] = (Action)allEventDic[eventType] + callback; } /// /// 一个参数监听 /// /// 事件 /// 回调 public static void AddListener(string eventType, Action callback) { OnListenerAdding(eventType, callback); allEventDic[eventType] = (Action)allEventDic[eventType] + callback; } /// /// 两个参数监听 /// /// 事件 /// 回调 public static void AddListener(string eventType, Action callback) { OnListenerAdding(eventType, callback); allEventDic[eventType] = (Action)allEventDic[eventType] + callback; } /// /// 三个参数监听 /// /// 事件 /// 回调 public static void AddListener(string eventType, Action callback) { OnListenerAdding(eventType, callback); allEventDic[eventType] = (Action)allEventDic[eventType] + callback; } /// /// 四个参数监听 /// /// 事件 /// 回调 public static void AddListener(string eventType, Action callback) { OnListenerAdding(eventType, callback); allEventDic[eventType] = (Action)allEventDic[eventType] + callback; } /// /// 五个参数监听 /// /// 事件 /// 回调 public static void AddListener(string eventType, Action callback) { OnListenerAdding(eventType, callback); allEventDic[eventType] = (Action)allEventDic[eventType] + callback; } //==================================================移除监听======================================================== /// /// 无参移除监听 /// /// 事件 /// 回调 public static void RemoveListener(string eventType, Action callback) { OnListenerRemoving(eventType, callback); allEventDic[eventType] = (Action)allEventDic[eventType] - callback; OnListenerRemoved(eventType); } /// /// 一个参数移除监听 /// /// 事件 /// 回调 public static void RemoveListener(string eventType, Action callback) { OnListenerRemoving(eventType, callback); allEventDic[eventType] = (Action)allEventDic[eventType] - callback; OnListenerRemoved(eventType); } /// /// 两个参数移除监听 /// /// 事件 /// 回调 public static void RemoveListener(string eventType, Action callback) { OnListenerRemoving(eventType, callback); allEventDic[eventType] = (Action)allEventDic[eventType] - callback; OnListenerRemoved(eventType); } /// /// 三个参数移除监听 /// /// 事件 /// 回调 public static void RemoveListener(string eventType, Action callback) { OnListenerRemoving(eventType, callback); allEventDic[eventType] = (Action)allEventDic[eventType] - callback; OnListenerRemoved(eventType); } /// /// 四个参数移除监听 /// /// 事件 /// 回调 public static void RemoveListener(string eventType, Action callback) { OnListenerRemoving(eventType, callback); allEventDic[eventType] = (Action)allEventDic[eventType] - callback; OnListenerRemoved(eventType); } /// /// 五个参数移除监听 /// /// 事件 /// 回调 public static void RemoveListener(string eventType, Action callback) { OnListenerRemoving(eventType, callback); allEventDic[eventType] = (Action)allEventDic[eventType] - callback; OnListenerRemoved(eventType); } public static void RemoveListener(string eventType, Delegate callback) { // 如果 dictB 中存在相同的键 if (allEventDic.ContainsKey(eventType)) { // 获取 MulticastDelegate MulticastDelegate multicastDelegate = allEventDic[eventType] as MulticastDelegate; // 移除指定的委托 Delegate[] invocationList = multicastDelegate.GetInvocationList(); Delegate[] newInvocationList = Array.FindAll(invocationList, d => d != callback); // 更新 dictB 中的委托 if (newInvocationList.Length > 0) { allEventDic[eventType] = Delegate.Combine(newInvocationList); } else { // 如果没有剩余的委托,则从 dictB 中删除该键 allEventDic.Remove(eventType); } } } //==================================================广播事件======================================================== /// /// 无参广播事件 /// /// 事件 public static void BroadCast(string eventType) { Delegate d; if (allEventDic.TryGetValue(eventType, out d)) { if (d is Action callback) { callback(); } else { throw new Exception($"广播事件错误:事件{eventType}对应委托具有不同的类型"); } } } /// /// 一个参数广播事件 /// /// 事件 public static void BroadCast(string eventType, K arg) { Delegate d; if (allEventDic.TryGetValue(eventType, out d)) { if (d is Action callback) { callback(arg); } else { throw new Exception($"广播事件错误:事件{eventType}对应委托具有不同的类型"); } } } /// /// 两个参数广播事件 /// /// 事件 public static void BroadCast(string eventType, K arg1, X arg2) { Delegate d; if (allEventDic.TryGetValue(eventType, out d)) { if (d is Action callback) { callback(arg1, arg2); } else { throw new Exception($"广播事件错误:事件{eventType}对应委托具有不同的类型"); } } } /// /// 三个参数广播事件 /// /// 事件 public static void BroadCast(string eventType, K arg1, X arg2, Y arg3) { Delegate d; if (allEventDic.TryGetValue(eventType, out d)) { if (d is Action callback) { callback(arg1, arg2, arg3); } else { throw new Exception($"广播事件错误:事件{eventType}对应委托具有不同的类型"); } } } /// /// 四个参数广播事件 /// /// 事件 public static void BroadCast(string eventType, K arg1, X arg2, Y arg3, Z arg4) { Delegate d; if (allEventDic.TryGetValue(eventType, out d)) { if (d is Action callback) { callback(arg1, arg2, arg3, arg4); } else { throw new Exception($"广播事件错误:事件{eventType}对应委托具有不同的类型"); } } } /// /// 五个参数广播事件 /// /// 事件 public static void BroadCast(string eventType, K arg1, X arg2, Y arg3, Z arg4, W arg5) { Delegate d; if (allEventDic.TryGetValue(eventType, out d)) { if (d is Action callback) { callback(arg1, arg2, arg3, arg4, arg5); } else { throw new Exception($"广播事件错误:事件{eventType}对应委托具有不同的类型"); } } } #endregion #region 枚举事件 //==================================================监听事件======================================================== /// /// 无参数监听 /// /// 事件 /// 回调 public static void AddListener(T eventType, Action callback) where T : Enum { string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; AddListener(eventName, callback); } /// /// 一个参数监听 /// /// 事件 /// 回调 public static void AddListener(T eventType, Action callback) where T : Enum { string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; AddListener(eventName, callback); } /// /// 两个参数监听 /// /// 事件 /// 回调 public static void AddListener(T eventType, Action callback) where T : Enum { string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; AddListener(eventName, callback); } /// /// 三个参数监听 /// /// 事件 /// 回调 public static void AddListener(T eventType, Action callback) where T : Enum { string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; AddListener(eventName, callback); } /// /// 四个参数监听 /// /// 事件 /// 回调 public static void AddListener(T eventType, Action callback) where T : Enum { string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; AddListener(eventName, callback); } /// /// 五个参数监听 /// /// 事件 /// 回调 public static void AddListener(T eventType, Action callback) where T : Enum { string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; AddListener(eventName, callback); } //==================================================移除监听======================================================== /// /// 无参移除监听 /// /// 事件 /// 回调 public static void RemoveListener(T eventType, Action callback) where T : Enum { string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; RemoveListener(eventName, callback); } /// /// 一个参数移除监听 /// /// 事件 /// 回调 public static void RemoveListener(T eventType, Action callback) where T : Enum { string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; RemoveListener(eventName, callback); } /// /// 两个参数移除监听 /// /// 事件 /// 回调 public static void RemoveListener(T eventType, Action callback) where T : Enum { string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; RemoveListener(eventName, callback); } /// /// 三个参数移除监听 /// /// 事件 /// 回调 public static void RemoveListener(T eventType, Action callback) where T : Enum { string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; RemoveListener(eventName, callback); } /// /// 四个参数移除监听 /// /// 事件 /// 回调 public static void RemoveListener(T eventType, Action callback) where T : Enum { string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; RemoveListener(eventName, callback); } /// /// 五个参数移除监听 /// /// 事件 /// 回调 public static void RemoveListener(T eventType, Action callback) where T : Enum { string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; RemoveListener(eventName, callback); } //==================================================广播事件======================================================== /// /// 无参广播事件 /// /// 事件 public static void BroadCast(T eventType) where T : Enum { string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; BroadCast(eventName); } /// /// 一个参数广播事件 /// /// 事件 public static void BroadCast(T eventType, K arg) where T : Enum { string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; BroadCast(eventName, arg); } /// /// 两个参数广播事件 /// /// 事件 public static void BroadCast(T eventType, K arg1, X arg2) where T : Enum { string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; BroadCast(eventName, arg1, arg2); } /// /// 三个参数广播事件 /// /// 事件 public static void BroadCast(T eventType, K arg1, X arg2, Y arg3) where T : Enum { string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; BroadCast(eventName, arg1, arg2, arg3); } /// /// 四个参数广播事件 /// /// 事件 public static void BroadCast(T eventType, K arg1, X arg2, Y arg3, Z arg4) where T : Enum { string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; BroadCast(eventName, arg1, arg2, arg3, arg4); } /// /// 五个参数广播事件 /// /// 事件 public static void BroadCast(T eventType, K arg1, X arg2, Y arg3, Z arg4, W arg5) where T : Enum { string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; BroadCast(eventName, arg1, arg2, arg3, arg4, arg5); } #endregion /// /// 清理 /// public static void Clear() { allEventDic.Clear(); } }