Example #1
0
        internal void Send(CurrentEventInfoImpl message)
        {
            if (!gameObject)
            {
                return;
            }

            using (message.Push())
            {
                if (!message.filterComplete && filters != null)
                {
                    if (filters.Any(info => info.CheckPrereq(message) && info.Filter(message)))
                    {
                        return;
                    }
                }

                // Send the message
                foreach (IPartMessage currMessage in message.Message)
                {
                    string messageName = currMessage.Name;

                    LinkedList <ListenerInfo> listenerList;
                    if (!listeners.TryGetValue(messageName, out listenerList))
                    {
                        continue;
                    }

                    // Shorten parameter list if required
                    object[] newArgs = null;

                    for (var node = listenerList.First; node != null;)
                    {
                        // hold reference for duration of call
                        ListenerInfo info   = node.Value;
                        object       target = info.Target;
                        if (target == null)
                        {
                            // Remove dead links from the list
                            var tmp = node;
                            node = node.Next;
                            listenerList.Remove(tmp);
                            continue;
                        }

                        // Declarative event filtering
                        if (!info.CheckPrereq(message))
                        {
                            node = node.Next;
                            continue;
                        }


                        if (newArgs == null)
                        {
                            newArgs = ShortenArgs(message.Arguments, currMessage.DelegateType);
                        }

                        try
                        {
                            node.Value.method.Invoke(target, newArgs);
                        }
                        catch (TargetException ex)
                        {
                            // Swallow target exceptions, but not anything else.
                            Debug.LogError(string.Format("Invoking {0}.{1} to handle DelegateType {2} resulted in an exception.", target.GetType(), node.Value.method, CurrentEventInfo.Message));
                            Debug.LogException(ex.InnerException);
                        }

                        node = node.Next;
                    }
                }
            }
        }