public void PrioritizedEventListenerDictionaryTests_ContainsKey_Null() { EventListenerRegistrationComparer comparer = new EventListenerRegistrationComparer(); var dictionary = new PrioritizedEventListenerDictionary <Type, EventListenerRegistration>(comparer); Assert.IsFalse(dictionary.ContainsKey(null)); }
public void PrioritizedEventListenerDictionaryTests_ContainsKey_Valid() { EventListenerRegistrationComparer comparer = new EventListenerRegistrationComparer(); var dictionary = new PrioritizedEventListenerDictionary <Type, EventListenerRegistration>(comparer); var mockListener = new EventListenerRegistration(new MockEventListener(true, false), new MockEventHook(GetType(), SurvivalKit.Events.Priority.LOW, null)); dictionary.Add(GetType(), mockListener); Assert.IsTrue(dictionary.ContainsKey(GetType())); }
/// <summary> /// Method to dispatch an event. /// </summary> /// <typeparam name="TEventType">The type of the event that will be dispatched.</typeparam> /// <param name="eventInstance">The event instance that should be pushed to all modules.</param> /// <param name="fireSubEvents">Should we fire sub events</param> public void DispatchEvent <TEventType>(TEventType eventInstance, bool fireSubEvents) where TEventType : IDispatchableEvent { if (GameDisabled) { // perhaps cancel the event? LogUtility.Out("[SK] Dispatching failed: " + eventInstance.GetType().Name); return; } // use the type they are giving us, not the actual type. var eventType = eventInstance.GetType(); if (eventInstance.IsCancelled()) { // stop processing if the event is cancelled? LogUtility.Out("[SK] Dispatching cancelled: " + eventInstance.GetType().Name); return; } if (_hookRegistry.ContainsKey(eventType)) { var eventHookRegistrations = _hookRegistry[eventType]; foreach (var eventHookRegistration in eventHookRegistrations) { var method = eventHookRegistration.EventHook.MethodToInvoke; var instance = eventHookRegistration.Instance; var tooManyArgumentsRequired = eventHookRegistration.GetRequiredMethodArguments() > 1; if (tooManyArgumentsRequired) { var message = string.Format("[SK] SurvivalKit warning: Skipped listener {0} for event type {1} is skipped. Argument mismatch!", instance.GetType(), eventType); LogUtility.Warning(message); continue; } try { LogUtility.Out("[SK] Invoking listener: " + instance.GetType().Name); method.Invoke(instance, new object[1] { eventInstance }); if (!eventInstance.IsCancelled() && fireSubEvents) { // The current event was cancelled by the handler. No need to trigger child events? var subEvents = eventInstance.getSubevents(); foreach (var dispatchableEvent in subEvents) { // might end up in an infinite loop. dispatchableEvent.Dispatch(); } } } catch (Exception exception) { // TODO Perhaps disable this plugin? var wrappedException = new EventDispatchingException(eventType.Name, eventType.AssemblyQualifiedName, "Exception while invoking plugin with listener: " + instance.GetType().FullName, exception); LogUtility.Exception(wrappedException); } } } }