コード例 #1
0
        internal static MethodInfoSorter GetMethodsWithAttribute <T>(BindingFlags bindingFlags = kAllStatic) where T : Attribute
        {
            MethodInfoSorter result;

            if (!s_DecoratedMethodsByAttrTypeCache.TryGetValue(typeof(T), out result))
            {
                var tmp = new List <MethodWithAttribute>();
                foreach (var method in EditorAssemblies.GetAllMethodsWithAttribute <T>(bindingFlags))
                {
                    if (method.IsGenericMethod)
                    {
                        Debug.LogErrorFormat(
                            "{0} is a generic method. {1} cannot be applied to it.", MethodToString(method), typeof(T)
                            );
                    }
                    else
                    {
                        foreach (var attr in method.GetCustomAttributes(typeof(T), false))
                        {
                            if (MethodMatchesAnyRequiredSignatureOfAttribute(method, typeof(T)))
                            {
                                var methodWithAttribute = new MethodWithAttribute {
                                    info = method, attribute = (T)attr
                                };
                                tmp.Add(methodWithAttribute);
                            }
                        }
                    }
                }

                result = new MethodInfoSorter(tmp);
                s_DecoratedMethodsByAttrTypeCache[typeof(T)] = result;
            }
            return(result);
        }
コード例 #2
0
 internal static IEnumerable <TReturnValue> CallMethodsWithAttribute <TReturnValue, TAttr>(params object[] arguments)
     where TAttr : Attribute
 {
     foreach (var method in EditorAssemblies.GetAllMethodsWithAttribute <TAttr>(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
     {
         yield return((TReturnValue)method.Invoke(null, arguments));
     }
 }
コード例 #3
0
 private static int[] ProcessInitializeOnLoadAttributes()
 {
     EditorAssemblies.m_TotalNumRuntimeInitializeMethods = 0;
     EditorAssemblies.m_RuntimeInitializeClassInfoList   = new List <RuntimeInitializeClassInfo>();
     foreach (Type current in EditorAssemblies.GetAllTypesWithAttribute <InitializeOnLoadAttribute>())
     {
         EditorAssemblies.ProcessEditorInitializeOnLoad(current);
     }
     foreach (MethodInfo current2 in EditorAssemblies.GetAllMethodsWithAttribute <RuntimeInitializeOnLoadMethodAttribute>(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
     {
         EditorAssemblies.ProcessRuntimeInitializeOnLoad(current2);
     }
     foreach (MethodInfo current3 in EditorAssemblies.GetAllMethodsWithAttribute <InitializeOnLoadMethodAttribute>(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
     {
         EditorAssemblies.ProcessInitializeOnLoadMethod(current3);
     }
     return(null);
 }
コード例 #4
0
        static MonoGizmoMethod[] ExtractGizmos(Assembly assembly)
        {
            var commands = new List <MonoGizmoMethod>();

            foreach (var mi in EditorAssemblies.GetAllMethodsWithAttribute <DrawGizmo>(BindingFlags.Static).Where(m => m.DeclaringType.Assembly == assembly))
            {
                var attrs = mi.GetCustomAttributes(typeof(DrawGizmo), false).Cast <DrawGizmo>();
                foreach (var gizmoAttr in attrs)
                {
                    var parameters = mi.GetParameters();
                    if (parameters.Length != 2)
                    {
                        Debug.LogWarningFormat(
                            "Method {0}.{1} is marked with the DrawGizmo attribute but does not take parameters (ComponentType, GizmoType) so will be ignored.",
                            mi.DeclaringType?.FullName, mi.Name
                            );
                        continue;
                    }
                    if (mi.DeclaringType != null && mi.DeclaringType.IsGenericTypeDefinition)
                    {
                        Debug.LogWarningFormat(
                            "Method {0}.{1} is marked with the DrawGizmo attribute but is defined on a generic type definition, so will be ignored.",
                            mi.DeclaringType.FullName, mi.Name
                            );
                        continue;
                    }

                    var item = new MonoGizmoMethod();

                    if (gizmoAttr.drawnType == null)
                    {
                        item.drawnType = parameters[0].ParameterType;
                    }
                    else if (parameters[0].ParameterType.IsAssignableFrom(gizmoAttr.drawnType))
                    {
                        item.drawnType = gizmoAttr.drawnType;
                    }
                    else
                    {
                        Debug.LogWarningFormat(
                            "Method {0}.{1} is marked with the DrawGizmo attribute but the component type it applies to could not be determined.",
                            mi.DeclaringType?.FullName, mi.Name
                            );
                        continue;
                    }

                    if (parameters[1].ParameterType != typeof(GizmoType) &&
                        parameters[1].ParameterType != typeof(int))
                    {
                        Debug.LogWarningFormat(
                            "Method {0}.{1} is marked with the DrawGizmo attribute but does not take a second parameter of type GizmoType so will be ignored.",
                            mi.DeclaringType?.FullName, mi.Name
                            );
                        continue;
                    }

                    item.drawGizmo = mi;
                    item.options   = (int)gizmoAttr.drawOptions;

                    commands.Add(item);
                }
            }
            return(commands.ToArray());
        }
コード例 #5
0
        private static AttributeHelper.MonoGizmoMethod[] ExtractGizmos(Assembly assembly)
        {
            List <AttributeHelper.MonoGizmoMethod> list = new List <AttributeHelper.MonoGizmoMethod>();

            foreach (MethodInfo current in from m in EditorAssemblies.GetAllMethodsWithAttribute <DrawGizmo>(BindingFlags.Static)
                     where m.DeclaringType.Assembly == assembly
                     select m)
            {
                IEnumerable <DrawGizmo> enumerable = current.GetCustomAttributes(typeof(DrawGizmo), false).Cast <DrawGizmo>();
                foreach (DrawGizmo current2 in enumerable)
                {
                    ParameterInfo[] parameters = current.GetParameters();
                    if (parameters.Length != 2)
                    {
                        UnityEngine.Debug.LogWarningFormat("Method {0}.{1} is marked with the DrawGizmo attribute but does not take parameters (ComponentType, GizmoType) so will be ignored.", new object[]
                        {
                            current.DeclaringType.FullName,
                            current.Name
                        });
                    }
                    else if (current.DeclaringType != null && current.DeclaringType.IsGenericTypeDefinition)
                    {
                        UnityEngine.Debug.LogWarningFormat("Method {0}.{1} is marked with the DrawGizmo attribute but is defined on a generic type definition, so will be ignored.", new object[]
                        {
                            current.DeclaringType.FullName,
                            current.Name
                        });
                    }
                    else
                    {
                        AttributeHelper.MonoGizmoMethod item = default(AttributeHelper.MonoGizmoMethod);
                        if (current2.drawnType == null)
                        {
                            item.drawnType = parameters[0].ParameterType;
                        }
                        else
                        {
                            if (!parameters[0].ParameterType.IsAssignableFrom(current2.drawnType))
                            {
                                UnityEngine.Debug.LogWarningFormat("Method {0}.{1} is marked with the DrawGizmo attribute but the component type it applies to could not be determined.", new object[]
                                {
                                    current.DeclaringType.FullName,
                                    current.Name
                                });
                                continue;
                            }
                            item.drawnType = current2.drawnType;
                        }
                        if (parameters[1].ParameterType != typeof(GizmoType) && parameters[1].ParameterType != typeof(int))
                        {
                            UnityEngine.Debug.LogWarningFormat("Method {0}.{1} is marked with the DrawGizmo attribute but does not take a second parameter of type GizmoType so will be ignored.", new object[]
                            {
                                current.DeclaringType.FullName,
                                current.Name
                            });
                        }
                        else
                        {
                            item.drawGizmo = current;
                            item.options   = (int)current2.drawOptions;
                            list.Add(item);
                        }
                    }
                }
            }
            return(list.ToArray());
        }