/// <summary> /// Removes a global event handler. /// You need to have registered the event before being able to remove it. /// </summary> /// <param name="e">The event type from which to deregister</param> /// <param name="del">The event handler to deregister for this event type</param> /// <exception cref="ArgumentNullException">If one of the parameters is null</exception> public static void RemoveHandler(DOLEvent e, DOLEventHandler del) { if (e == null) { throw new ArgumentNullException("e", "No event type given!"); } if (del == null) { throw new ArgumentNullException("del", "No event handler given!"); } GlobalHandlerCollection.RemoveHandler(e, del); }
/// <summary> /// Registers some global events that are specified by attributes /// </summary> /// <param name="asm">The assembly to search through</param> /// <param name="attribute">The custom attribute to search for</param> /// <param name="e">The event to register in case the custom attribute is found</param> /// <exception cref="ArgumentNullException">If one of the parameters is null</exception> public static void RegisterGlobalEvents(Assembly asm, Type attribute, DOLEvent e) { if (asm == null) { throw new ArgumentNullException("asm", "No assembly given to search for global event handlers!"); } if (attribute == null) { throw new ArgumentNullException("attribute", "No attribute given!"); } if (e == null) { throw new ArgumentNullException("e", "No event type given!"); } foreach (Type type in asm.GetTypes()) { if (!type.IsClass) { continue; } foreach (MethodInfo mInfo in type.GetMethods(BindingFlags.Public | BindingFlags.Static)) { object[] myAttribs = mInfo.GetCustomAttributes(attribute, false); if (myAttribs.Length != 0) { try { GlobalHandlerCollection.AddHandler(e, (DOLEventHandler)Delegate.CreateDelegate(typeof(DOLEventHandler), mInfo)); } catch (Exception ex) { if (log.IsErrorEnabled) { log.Error("Error registering global event. Method: " + type.FullName + "." + mInfo.Name, ex); } } } } } }
/// <summary> /// Registers a single global event handler. /// The global event handlers will be called for ALL events, /// so use them wisely, they might incure a big performance /// hit if used unwisely. /// </summary> /// <param name="e">The event type to register for</param> /// <param name="del">The event handler to register for this event type</param> /// <param name="unique">Flag wether event shall be added unique or not</param> /// <exception cref="ArgumentNullException">If one of the parameters is null</exception> private static void AddHandler(DOLEvent e, DOLEventHandler del, bool unique) { if (e == null) { throw new ArgumentNullException("e", "No event type given!"); } if (del == null) { throw new ArgumentNullException("del", "No event handler given!"); } if (unique) { GlobalHandlerCollection.AddHandlerUnique(e, del); } else { GlobalHandlerCollection.AddHandler(e, del); } }
/// <summary> /// Removes all event handlers /// </summary> /// <param name="deep">Specifies if all local registered event handlers /// should also be removed</param> public static void RemoveAllHandlers(bool deep) { if (deep) { if (Lock.TryEnterWriteLock(LOCK_TIMEOUT)) { try { m_gameObjectEventCollections.Clear(); } finally { Lock.ExitWriteLock(); } } else { log.ErrorFormat("Timeout exceeded on attempt to RemoveAllHandlers."); } } GlobalHandlerCollection.RemoveAllHandlers(); }
/// <summary> /// Notifies all global and local event handlers of the occurance /// of a specific event type with some event arguments! /// </summary> /// <param name="e">The event type that occured</param> /// <param name="sender">The sender of this event</param> /// <param name="eArgs">The event arguments</param> /// <exception cref="ArgumentNullException">If no event type given</exception> /// <remarks>Overwrite the EventArgs class to set own arguments</remarks> public static void Notify(DOLEvent e, object sender, EventArgs eArgs) { if (e == null) { throw new ArgumentNullException("e", "No event type given!"); } // notify handlers bounded specifically to the sender if (sender != null) { DOLEventHandlerCollection col; if (Lock.TryEnterReadLock(LOCK_TIMEOUT)) { try { m_gameObjectEventCollections.TryGetValue(sender, out col); } finally { Lock.ExitReadLock(); } if (col != null) { col.Notify(e, sender, eArgs); } } else { log.ErrorFormat("Timeout exceeded on attempt to Notify event: {0} for object: {1}", e.Name, sender.ToString()); } } // notify globally-bound handler GlobalHandlerCollection.Notify(e, sender, eArgs); }