public void RegisterHandler(MessageProcessUnitBase module, MessageHandlerDelegate handler, Type msgType) { if (!typeof(IMessage).IsAssignableFrom(msgType)) { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("消息结构体[{0}]没有实现IMessage接口", msgType.Name)); } return; } #endif #if UNITY_EDITOR if (!msgType.IsValueType) { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("消息结构体[{0}]必须定义为值类型(struct)", msgType.Name)); } return; } object[] fieldsDefined = msgType.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (fieldsDefined.Length <= 0) { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("消息结构体[{0}]中没有定义任何成员,如果不需要向外通知任何参数的话,使用Notification", msgType.Name)); } return; } #endif if (module != null && handler != null) { MessageHandler messageHandler = new MessageHandler(msgType, handler); if (!messageHandlerTableLocked) { if (messageHandlerTable == null) { messageHandlerTable = new Dictionary <Type, List <MessageHandler> >(); } CollectionUtil.AddIntoTable(msgType, messageHandler, messageHandlerTable); #if UNITY_EDITOR if (LogUtil.ShowDebug != null) { LogUtil.ShowDebug(string.Format("[本地消息中心]1.注册消息处理器:来自[{0}] 消息类型[{1}] 处理回调[{2}]", module.GetType().Name, msgType.Name, StringUtility.ToString(handler))); } #endif } else { RegisterModifier modifier = pool.Aquire(); modifier.isUnregister = false; modifier.messageType = msgType; modifier.messageHandler = messageHandler; modifier.module = module; if (messageHandlerRegisterChangeCache == null) { messageHandlerRegisterChangeCache = new List <RegisterModifier>(); } messageHandlerRegisterChangeCache.Add(modifier); } if (moduleMessageRegisterTable == null) { moduleMessageRegisterTable = new Dictionary <MessageProcessUnitBase, Dictionary <Type, MessageHandler> >(); } CollectionUtil.AddIntoTable(module, msgType, messageHandler, moduleMessageRegisterTable); } #if UNITY_EDITOR else { if (LogUtil.ShowError != null) { LogUtil.ShowError(string.Format("注册消息处理器时传递的参数有[null]值:消息类型[{0}] 模块[{1}] 处理回调[{2}]", msgType.Name, StringUtility.ToString(module), StringUtility.ToString(handler))); } } #endif }
// public virtual void DeliverMessage<T>(T msg, MessageProcessUnitBase terminal) where T : Message // { // if (msg != null && terminal != null) // { // msg.TimeStamp = DateTimeUtil.NowMillisecond; // if(moduleMessageRegisterTable != null) // InternalDeliverMessage(msg, terminal); // } //#if UNITY_EDITOR // else // { // if (LogUtil.ShowError != null) // LogUtil.ShowError(string.Format("发送消息时传递的参数有[null]值:消息[{0}] 接收端[{1}]", // StringUtil.ToString(msg), StringUtil.ToString(terminal))); // } //#endif // } // public virtual void DeliverMessage<T>(T msg, IEnumerable<MessageProcessUnitBase> terminals) where T : Message // { // if (msg != null && terminals != null) // { // msg.TimeStamp = DateTimeUtil.NowMillisecond; // if (moduleMessageRegisterTable != null) // foreach (MessageProcessUnitBase terminal in terminals) // InternalDeliverMessage(msg, terminal); // } //#if UNITY_EDITOR // else // { // if (LogUtil.ShowError != null) // LogUtil.ShowError(string.Format("发送消息时传递的参数有[null]值:消息[{0}] 接收端[{1}]", // StringUtil.ToString(msg), StringUtil.ToString(terminals))); // } //#endif // } // public virtual void DeliverMessage<T1, T2>(T1 msg, IEnumerable<T2> terminals) // where T1 : Message // where T2 : MessageProcessUnitBase // { // if (msg != null && terminals != null) // { // msg.TimeStamp = DateTimeUtil.NowMillisecond; // if (moduleMessageRegisterTable != null) // foreach (MessageProcessUnitBase terminal in terminals) // InternalDeliverMessage(msg, terminal); // } //#if UNITY_EDITOR // else // { // if (LogUtil.ShowError != null) // LogUtil.ShowError(string.Format("发送消息时传递的参数有[null]值:消息[{0}] 接收端[{1}]", // StringUtil.ToString(msg), StringUtil.ToString(terminals))); // } //#endif // } // public virtual void DeliverMessageExclude<T>(T msg, MessageProcessUnitBase excludeTerminal) where T : Message // { // if (msg != null && excludeTerminal != null) // { // msg.TimeStamp = DateTimeUtil.NowMillisecond; // Type msgType = typeof(T); // if(messageHandlerTable != null) // { // List<MessageHandler> handlers; // if (messageHandlerTable.TryGetValue(msgType, out handlers)) // { // if(moduleMessageRegisterTable != null) // { // MessageHandler excludeHandler = CollectionUtil.GetFromTable(excludeTerminal, msgType, moduleMessageRegisterTable); // foreach (MessageHandler handler in handlers) // { // if (handler != excludeHandler) // Invoke(handler, msg); // } // } // } // } // } //#if UNITY_EDITOR // else // { // if (LogUtil.ShowError != null) // LogUtil.ShowError(string.Format("发送消息时传递的参数有[null]值:消息[{0}] 接收端[{1}]", // StringUtil.ToString(msg), StringUtil.ToString(excludeTerminal))); // } //#endif // } // public virtual void DeliverMessage<T>(T msg, MessageProcessUnitBase terminal, 投递选项 option) where T : Message // { // switch (option) // { // case 投递选项.指定目标为投递对象: // DeliverMessage(msg, terminal); // break; // case 投递选项.指定目标为排除对象: // DeliverMessageExclude(msg, terminal); // break; // } // } //private void InternalDeliverMessage<T>(T msg, MessageProcessUnitBase terminal) where T : Message //{ // Dictionary<Type, MessageHandler> handlers; // if (moduleMessageRegisterTable.TryGetValue(terminal, out handlers)) // { // MessageHandler handler; // if (handlers.TryGetValue(msg.GetType(), out handler)) // Invoke(handler, msg); // } //} #if USE_GENERIC private void Invoke <T>(MessageHandler handler, T msg) where T : IMessage