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

            // Iterate through each static method

            Assembly assembly = Assembly.GetAssembly(typeof(NodeEditorInputSystem));

            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

                    // Check the method's attributes for input handler definitions
                    Delegate actionDelegate = null;
                    foreach (object attr in method.GetCustomAttributes(true))
                    {
                        Type attrType = attr.GetType();
                        if (attrType == typeof(EventHandlerAttribute))
                        {
                            // Method is an eventHandler
                            if (EventHandlerAttribute.AssureValidity(method, attr as EventHandlerAttribute))
                            {
                                // Method signature is correct, so we register this handler
                                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))
                        {
                            // Method is an hotkeyHandler
                            if (HotkeyAttribute.AssureValidity(method, attr as HotkeyAttribute))
                            {
                                // Method signature is correct, so we register this handler
                                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))
                        {
                            // Method is an contextEntry
                            if (ContextEntryAttribute.AssureValidity(method, attr as ContextEntryAttribute))
                            {
                                // Method signature is correct, so we register this handler
                                if (actionDelegate == null)
                                {
                                    actionDelegate = Delegate.CreateDelegate(typeof(Action <NodeEditorInputInfo>), method);
                                }
                                // Create a proper MenuFunction as a wrapper for the delegate that converts the object to NodeEditorInputInfo
                                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, MenuFunctionData>(attr as ContextEntryAttribute, menuFunction));
                            }
                        }
                        else if (attrType == typeof(ContextFillerAttribute))
                        {
                            // Method is an contextFiller
                            if (ContextFillerAttribute.AssureValidity(method, attr as ContextFillerAttribute))
                            {
                                // Method signature is correct, so we register this handler
                                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));
        }
        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> enumerable = from assembly in AppDomain.CurrentDomain.GetAssemblies()
                                                where assembly.FullName.Contains("Assembly")
                                                select assembly;

            foreach (Assembly item in enumerable)
            {
                Type[] types = item.GetTypes();
                foreach (Type type in types)
                {
                    MethodInfo[] methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
                    foreach (MethodInfo methodInfo in methods)
                    {
                        Delegate actionDelegate   = null;
                        object[] customAttributes = methodInfo.GetCustomAttributes(true);
                        foreach (object obj in customAttributes)
                        {
                            Type type2 = obj.GetType();
                            if (type2 == typeof(EventHandlerAttribute))
                            {
                                if (EventHandlerAttribute.AssureValidity(methodInfo, obj as EventHandlerAttribute))
                                {
                                    if ((object)actionDelegate == null)
                                    {
                                        actionDelegate = Delegate.CreateDelegate(typeof(Action <NodeEditorInputInfo>), methodInfo);
                                    }
                                    eventHandlers.Add(new KeyValuePair <EventHandlerAttribute, Delegate>(obj as EventHandlerAttribute, actionDelegate));
                                }
                            }
                            else if (type2 == typeof(HotkeyAttribute))
                            {
                                if (HotkeyAttribute.AssureValidity(methodInfo, obj as HotkeyAttribute))
                                {
                                    if ((object)actionDelegate == null)
                                    {
                                        actionDelegate = Delegate.CreateDelegate(typeof(Action <NodeEditorInputInfo>), methodInfo);
                                    }
                                    hotkeyHandlers.Add(new KeyValuePair <HotkeyAttribute, Delegate>(obj as HotkeyAttribute, actionDelegate));
                                }
                            }
                            else if (type2 == typeof(ContextEntryAttribute))
                            {
                                if (ContextEntryAttribute.AssureValidity(methodInfo, obj as ContextEntryAttribute))
                                {
                                    if ((object)actionDelegate == null)
                                    {
                                        actionDelegate = Delegate.CreateDelegate(typeof(Action <NodeEditorInputInfo>), methodInfo);
                                    }
                                    PopupMenu.MenuFunctionData value = delegate(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>(obj as ContextEntryAttribute, value));
                                }
                            }
                            else if (type2 == typeof(ContextFillerAttribute) && ContextFillerAttribute.AssureValidity(methodInfo, obj as ContextFillerAttribute))
                            {
                                Delegate value2 = Delegate.CreateDelegate(typeof(Action <NodeEditorInputInfo, GenericMenu>), methodInfo);
                                contextFillers.Add(new KeyValuePair <ContextFillerAttribute, Delegate>(obj as ContextFillerAttribute, value2));
                            }
                        }
                    }
                }
            }
            eventHandlers.Sort((KeyValuePair <EventHandlerAttribute, Delegate> handlerA, KeyValuePair <EventHandlerAttribute, Delegate> handlerB) => handlerA.Key.priority.CompareTo(handlerB.Key.priority));
            hotkeyHandlers.Sort((KeyValuePair <HotkeyAttribute, Delegate> handlerA, KeyValuePair <HotkeyAttribute, Delegate> handlerB) => handlerA.Key.priority.CompareTo(handlerB.Key.priority));
        }
Ejemplo n.º 4
0
 void DrawEntries(ContextEntryAttribute entry)
 {
     EditorGUILayout.EnumPopup(entry.contextType);
     EditorGUILayout.LabelField(entry.contextPath);
 }