internal static bool IsValid(MethodInfo method, ContextFillerAttribute attribute) { if (!method.IsGenericMethod && !method.IsGenericMethodDefinition && method.ReturnType == typeof(void)) { // Check if the method has the correct signature var methodParams = method.GetParameters(); if (methodParams.Length == 2 && methodParams[0].ParameterType == typeof(FolderManagerInputInfo) && methodParams[1].ParameterType == typeof(GenericMenu)) { return(true); } Debug.LogWarning("Method " + method.Name + " has incorrect signature for ContextAttribute!"); } return(false); }
public static void SetupInput() { _eventHandlers = new List <KeyValuePair <EventHandlerAttribute, Delegate> >(); _contextEntries = new List <KeyValuePair <ContextEntryAttribute, PopupMenu.MenuFunctionData> >(); _contextFillers = new List <KeyValuePair <ContextFillerAttribute, Delegate> >(); var scriptAssemblies = AppDomain.CurrentDomain.GetAssemblies() .Where(assembly => assembly.FullName.Contains("Assembly")); foreach (var assembly in scriptAssemblies) { foreach (var type in assembly.GetTypes()) { foreach (var method in type.GetMethods(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)) { #region Event Attributes recognition and storing Delegate actionDelegate = null; foreach (var attr in method.GetCustomAttributes(true)) { var attrType = attr.GetType(); if (attrType == typeof(EventHandlerAttribute)) { if (EventHandlerAttribute.IsValid(method, attr as EventHandlerAttribute)) { if (actionDelegate == null) { actionDelegate = Delegate.CreateDelegate(typeof(Action <FolderManagerInputInfo>), method); } _eventHandlers.Add( new KeyValuePair <EventHandlerAttribute, Delegate>(attr as EventHandlerAttribute, actionDelegate)); } } else if (attrType == typeof(ContextEntryAttribute)) { if (ContextEntryAttribute.IsValid(method, attr as ContextEntryAttribute)) { if (actionDelegate == null) { actionDelegate = Delegate.CreateDelegate(typeof(Action <FolderManagerInputInfo>), method); } PopupMenu.MenuFunctionData menuFunction = callbackObj => { if (!(callbackObj is FolderManagerInputInfo)) { throw new UnityException( "Callback Object passed by context is not of type NodeEditorMenuCallback!"); } actionDelegate.DynamicInvoke((FolderManagerInputInfo)callbackObj); }; _contextEntries.Add( new KeyValuePair <ContextEntryAttribute, PopupMenu.MenuFunctionData>( attr as ContextEntryAttribute, menuFunction)); } } else if (attrType == typeof(ContextFillerAttribute)) { if (ContextFillerAttribute.IsValid(method, attr as ContextFillerAttribute)) { var methodDel = Delegate.CreateDelegate(typeof(Action <FolderManagerInputInfo, GenericMenu>), method); _contextFillers.Add( new KeyValuePair <ContextFillerAttribute, Delegate>(attr as ContextFillerAttribute, methodDel)); } } } #endregion } } } _eventHandlers.Sort((handlerA, handlerB) => handlerA.Key.Priority.CompareTo(handlerB.Key.Priority)); }