/// <summary> /// 注册消息 /// </summary> /// <param name="self"></param> /// <param name="_msgHandler"></param> public static void RegisterMsgEvent(this IMsgEvent self, params MsgHandler[] _msgHandler) { if (_msgHandler.Length > 0) { if (!msgEntityMap.ContainsKey(self)) { msgEntityMap.Add(self, _msgHandler); } List <MsgHandler> mhlist = null; for (int i = 0; i < _msgHandler.Length; i++) { if (_msgHandler[i] == null) { continue; } mhlist = GetRegMsgHandlers(_msgHandler[i].msgType); if (mhlist == null) { mhlist = new List <MsgHandler>() { _msgHandler[i] }; msgMap.Add(_msgHandler[i].msgType, mhlist); } else { mhlist.Add(_msgHandler[i]); mhlist.Sort((m1, m2) => m1.eventId - m2.eventId); msgMap[_msgHandler[i].msgType] = mhlist; } } } }
/// <summary> /// 根据消息类型删除 /// </summary> /// <param name="self"></param> /// <param name="msgType">消息类型</param> public static void UnRegisterMsgEvent(this IMsgEvent self, string msgType) { if (msgEntityMap.ContainsKey(self)) { MsgHandler[] msgHandlers = msgEntityMap[self]; int index = 0; for (int i = 0; i < msgHandlers.Length; i++) { if (msgHandlers[i] != null && msgHandlers[i].msgType == msgType) { index = i; break; } } List <MsgHandler> mhlist = null; msgMap.TryGetValue(msgType, out mhlist); if (mhlist != null) { for (int i = mhlist.Count - 1; i >= 0; i--) { if (mhlist[i] == msgHandlers[index]) { mhlist[i].Reset(); mhlist[i] = null; mhlist.RemoveAt(i); msgHandlers[index] = null; break; } } } } }
/// <summary> /// 取消注册消息 /// </summary> /// <param name="self">此对象注册的全部消息</param> public static void UnRegisterMsgEvent(this IMsgEvent self) { if (msgEntityMap.ContainsKey(self)) { MsgHandler handler = null; List <MsgHandler> mhlist = null; for (int i = 0; i < msgEntityMap[self].Length; i++) { handler = msgEntityMap[self][i]; if (handler == null) { continue; } mhlist = GetRegMsgHandlers(handler.msgType); if (mhlist != null && mhlist.Contains(handler)) { handler.Reset(); mhlist.Remove(handler); } handler = null; } msgEntityMap.Remove(self); } }