static void AddSendLogEntry(BaseMessage msg, string message) { #if UNITY_EDITOR if (!msg.DontShowInEditorLogInternal) { MessageLogHandler.AddLogEntry(LogEntryType.Send, msg.Name, msg.ToString(), message); } #endif }
/// <summary> /// Add the specified message to the processing queue for sending. /// </summary> /// <param name="msg"></param> /// <returns></returns> public bool QueueMessage(BaseMessage msg) { // if no listeners then just return. if (!_listeners.ContainsKey(msg.Name)) { MessageLogHandler.AddLogEntry(LogEntryType.Send, msg.Name, msg.ToString(), "No listeners are setup. Discarding message!"); return(false); } _messageQueue.Enqueue(msg); return(true); }
/// <summary> /// Remove the listener from the specified message type. /// </summary> /// <param name="messageType"></param> /// <param name="handler"></param> /// <returns></returns> public void RemoveListener(Type messageType, MessageListenerDelegate handler) { var messageName = messageType.Name; Assert.IsTrue(_listeners.ContainsKey(messageName), "You are trying to remove a handler that a message type that isn't registered for " + messageName); List <MessageListenerDelegate> listenerList = _listeners[messageName]; Assert.IsTrue(listenerList.Contains(handler), "You are trying to remove a handler that isn't registered for " + messageName); listenerList.Remove(handler); MessageLogHandler.AddLogEntry(LogEntryType.RemoveListener, messageName); }
/// <summary> /// Add a listener for the specified message type. Be sure to call RemoveListener when you are done. /// </summary> /// <param name="messageType">Type of the message to add a listener for</param> /// <param name="handler"></param> /// <returns></returns> public void AddListener(Type messageType, MessageListenerDelegate handler) { var messageName = messageType.Name; if (!_listeners.ContainsKey(messageName)) { _listeners.Add(messageName, new List <MessageListenerDelegate>()); } List <MessageListenerDelegate> listenerList = _listeners[messageName]; Assert.IsFalse(listenerList.Contains(handler), "You should not add the same listener multiple times for " + messageName); listenerList.Add(handler); MessageLogHandler.AddLogEntry(LogEntryType.AddListener, messageName); }
/// <summary> /// Immediately send the specified message to all listeners. /// </summary> /// <param name="msg"></param> /// <returns></returns> public bool TriggerMessage(BaseMessage msg) { if (!_listeners.ContainsKey(msg.Name)) { MessageLogHandler.AddLogEntry(LogEntryType.Send, msg.Name, msg.ToString(), "No listeners are setup. Discarding message!"); return(false); } List <MessageListenerDelegate> listenerList = _listeners[msg.Name]; for (int i = 0; i < listenerList.Count; ++i) { var sent = listenerList[i](msg); if (msg.SendMode == BaseMessage.SendModeType.SendToFirst && sent) { MessageLogHandler.AddLogEntry(LogEntryType.Send, msg.Name, msg.ToString(), "Sent to first listener."); return(true); } } MessageLogHandler.AddLogEntry(LogEntryType.Send, msg.Name, msg.ToString(), "Sent to " + listenerList.Count + " listeners."); return(true); }