Example #1
0
 internal static bool AssureValidity(MethodInfo method, EventHandlerAttribute attr)
 {
     if (!method.IsGenericMethod && !method.IsGenericMethodDefinition && (method.ReturnType == null || method.ReturnType == typeof(void)))
     {
         ParameterInfo[] methodParams = method.GetParameters();
         if (methodParams.Length == 1 && methodParams[0].ParameterType == typeof(NodeEditorInputInfo))
         {
             return(true);
         }
         else
         {
             Debug.LogWarning("Method " + method.Name + " has incorrect signature for EventHandlerAttribute!");
         }
     }
     return(false);
 }
Example #2
0
        public static void SetupInput()
        {
            eventHandlers  = new List <KeyValuePair <EventHandlerAttribute, Delegate> > ();
            hotkeyHandlers = new List <KeyValuePair <HotkeyAttribute, Delegate> > ();
            contextEntries = new List <KeyValuePair <ContextEntryAttribute, PopupMenu.MenuFunctionData> > ();
            contextFillers = new List <KeyValuePair <ContextFillerAttribute, Delegate> > ();

            IEnumerable <Assembly> scriptAssemblies = AppDomain.CurrentDomain.GetAssemblies().Where((Assembly assembly) => assembly.FullName.Contains("Assembly"));

            foreach (Assembly assembly in scriptAssemblies)
            {
                foreach (Type type in assembly.GetTypes())
                {
                    foreach (MethodInfo method in type.GetMethods(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static))
                    {
                        #region Event-Attributes recognition and storing

                        Delegate actionDelegate = null;
                        foreach (object attr in method.GetCustomAttributes(true))
                        {
                            Type attrType = attr.GetType();
                            if (attrType == typeof(EventHandlerAttribute))
                            {
                                if (EventHandlerAttribute.AssureValidity(method, attr as EventHandlerAttribute))
                                {
                                    if (actionDelegate == null)
                                    {
                                        actionDelegate = Delegate.CreateDelegate(typeof(Action <NodeEditorInputInfo>), method);
                                    }
                                    eventHandlers.Add(new KeyValuePair <EventHandlerAttribute, Delegate> (attr as EventHandlerAttribute, actionDelegate));
                                }
                            }
                            else if (attrType == typeof(HotkeyAttribute))
                            {
                                if (HotkeyAttribute.AssureValidity(method, attr as HotkeyAttribute))
                                {
                                    if (actionDelegate == null)
                                    {
                                        actionDelegate = Delegate.CreateDelegate(typeof(Action <NodeEditorInputInfo>), method);
                                    }
                                    hotkeyHandlers.Add(new KeyValuePair <HotkeyAttribute, Delegate> (attr as HotkeyAttribute, actionDelegate));
                                }
                            }
                            else if (attrType == typeof(ContextEntryAttribute))
                            {
                                if (ContextEntryAttribute.AssureValidity(method, attr as ContextEntryAttribute))
                                {
                                    if (actionDelegate == null)
                                    {
                                        actionDelegate = Delegate.CreateDelegate(typeof(Action <NodeEditorInputInfo>), method);
                                    }
                                    PopupMenu.MenuFunctionData menuFunction = (object callbackObj) =>
                                    {
                                        if (!(callbackObj is NodeEditorInputInfo))
                                        {
                                            throw new UnityException("Callback Object passed by context is not of type NodeEditorMenuCallback!");
                                        }
                                        actionDelegate.DynamicInvoke(callbackObj as NodeEditorInputInfo);
                                    };
                                    contextEntries.Add(new KeyValuePair <ContextEntryAttribute, PopupMenu.MenuFunctionData> (attr as ContextEntryAttribute, menuFunction));
                                }
                            }
                            else if (attrType == typeof(ContextFillerAttribute))
                            {
                                if (ContextFillerAttribute.AssureValidity(method, attr as ContextFillerAttribute))
                                {
                                    Delegate methodDel = Delegate.CreateDelegate(typeof(Action <NodeEditorInputInfo, GenericMenu>), method);
                                    contextFillers.Add(new KeyValuePair <ContextFillerAttribute, Delegate> (attr as ContextFillerAttribute, methodDel));
                                }
                            }
                        }

                        #endregion
                    }
                }
            }

            eventHandlers.Sort((handlerA, handlerB) => handlerA.Key.priority.CompareTo(handlerB.Key.priority));
            hotkeyHandlers.Sort((handlerA, handlerB) => handlerA.Key.priority.CompareTo(handlerB.Key.priority));
        }