Ejemplo n.º 1
0
        private bool TryRegisterEvent(string eventName, ulong eventHandlerId)
        {
            if (RegisteredEvents.TryGetValue(eventName, out var eventRegistration))
            {
                Renderer.RegisterEvent(eventHandlerId, eventRegistration.ClearId);
                eventRegistration.SetId(eventHandlerId);

                return(true);
            }
            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Registers all EventHandler methods with the current EventDispatcher.
        /// </summary>
        /// <param name="obj">The class to scan for EventHandlers</param>
        public void RegisterEvents(IEventHandler obj)
        {
            int count = 0;

            var  type    = typeof(Event);
            Type objType = obj.GetType();

            foreach (var method in objType.GetMethods(BindingFlags.Public | BindingFlags.Instance))
            {
                EventHandlerAttribute attribute = method.GetCustomAttribute <EventHandlerAttribute>(false);
                if (attribute == null)
                {
                    continue;
                }

                var parameters = method.GetParameters();
                if (parameters.Length != 1 || !type.IsAssignableFrom(parameters[0].ParameterType))
                {
                    continue;
                }

                var paramType = parameters[0].ParameterType;

                EventDispatcherValues e = null;
                if (!RegisteredEvents.TryGetValue(paramType, out e))
                {
                    if (EventTypes.TryAdd(paramType))
                    {
                        e = new EventDispatcherValues();
                        RegisteredEvents.Add(paramType, e);
                    }
                }

                if (!e.RegisterEventHandler(attribute, obj, method))
                {
                    Log.Warn($"Duplicate found for class \"{obj.GetType()}\" of type \"{paramType}\"");
                }
                else
                {
                    count++;
                }
            }

            Log.Info($"Registered {count} event handlers for \"{obj}\"");
        }
Ejemplo n.º 3
0
 private async Task DispatchPrivate(Event e)
 {
     try
     {
         Type type = e.GetType();
         if (RegisteredEvents.TryGetValue(type, out EventDispatcherValues v))
         {
             await v.DispatchAsync(e);
         }
         else
         {
             Log.Warn($"Unknown event type found! \"{type}\"");
         }
     }
     catch (Exception ex)
     {
         Log.Error("Error while dispatching event!", ex);
     }
 }
Ejemplo n.º 4
0
 private void DispatchPrivate(Event e)
 {
     try
     {
         Type type = e.GetType();
         if (RegisteredEvents.TryGetValue(type, out EventDispatcherValues v))
         {
             v.Dispatch(e);
         }
         else
         {
             Log.Warn($"Unknown event type found! \"{type}\"");
         }
     }
     catch (Exception ex)
     {
         Log.Error(ex, $"Error while dispatching event: {ex.ToString()}");
     }
 }