Пример #1
0
        public bool QueueEvent(Cv_Event newEvent, bool threadSafe = false)
        {
            if (!threadSafe)
            {
                Cv_Debug.Assert((m_iActiveQueue >= 0 && m_iActiveQueue < NUM_QUEUES), "EventManager must have an active event queue.");

                if (newEvent == null)
                {
                    Cv_Debug.Error("Invalid event in QueueEvent.");
                    return(false);
                }

                if (newEvent.WriteToLog)
                {
                    Cv_Debug.Log("Events", "Attempting to queue event " + newEvent.VGetName() + " for entity " + newEvent.EntityID);
                }

                lock (m_EventListeners)
                {
                    if (m_EventListeners.ContainsKey(newEvent.Type))
                    {
                        m_EventQueues[m_iActiveQueue].AddLast(newEvent);
                        if (newEvent.WriteToLog)
                        {
                            Cv_Debug.Log("Events", "Successfully queued event " + newEvent.VGetName());
                        }
                        return(true);
                    }
                }

                lock (m_ScriptEventListeners)
                {
                    if (m_ScriptEventListeners.ContainsKey(newEvent.Type))
                    {
                        m_EventQueues[m_iActiveQueue].AddLast(newEvent);
                        if (newEvent.WriteToLog)
                        {
                            Cv_Debug.Log("Events", "Successfully queued event " + newEvent.VGetName());
                        }
                        return(true);
                    }
                }

                Cv_Debug.Log("Events", "Skipping event " + newEvent.VGetName() + " since there are no listeners for it.");
                return(false);
            }
            else
            {
                m_RealTimeEventQueue.Enqueue(newEvent);
                return(true);
            }
        }
Пример #2
0
        public bool TriggerEvent(Cv_Event newEvent)
        {
            if (newEvent.WriteToLog)
            {
                Cv_Debug.Log("Events", "Attempting to trigger event " + newEvent.VGetName() + " for entity " + newEvent.EntityID);
            }

            var processed = false;

            List <NewEventDelegate> listenersCopy = null;

            lock (m_EventListeners)
            {
                List <NewEventDelegate> listeners;
                if (m_EventListeners.TryGetValue(newEvent.Type, out listeners))
                {
                    listenersCopy = new List <NewEventDelegate>(listeners);
                }
            }

            if (listenersCopy != null)
            {
                foreach (var l in listenersCopy)
                {
                    if (newEvent.WriteToLog)
                    {
                        Cv_Debug.Log("Events", "Sending event " + newEvent.VGetName() + " to listener.");
                    }

                    l(newEvent);
                    processed = true;
                }
            }

            List <Cv_ScriptListener> scriptListenersCopy = null;

            lock (m_ScriptEventListeners)
            {
                List <Cv_ScriptListener> scriptListeners;
                if (m_ScriptEventListeners.TryGetValue(newEvent.Type, out scriptListeners))
                {
                    scriptListenersCopy = new List <Cv_ScriptListener>(scriptListeners);
                }
            }

            if (scriptListenersCopy != null)
            {
                foreach (var l in scriptListenersCopy)
                {
                    if (newEvent.WriteToLog)
                    {
                        Cv_Debug.Log("Events", "Sending event " + newEvent.VGetName() + " to listener.");
                    }

                    Cv_ScriptManager.Instance.VExecuteString("Cv_EventManager", l.Delegate, false, newEvent, l.Entity);
                    processed = true;
                }
            }

            return(processed);
        }