Exemplo n.º 1
0
        static void InitializeIfNeeded()
        {
            if (s_VolumeComponentEditorTypes == null)
            {
                s_AdditionalPropertiesVisibilityMethods = TypeCache.GetMethodsWithAttribute <SetAdditionalPropertiesVisibilityAttribute>();

                s_VolumeComponentEditorTypes = TypeCache.GetTypesDerivedFrom <VolumeComponentEditor>()
                                               .Where(
                    t => !t.IsAbstract
                    ).ToList();
            }
        }
    protected virtual string[] GetAdditionalProvidedUsings(string path)
    {
        TypeCache.MethodCollection usingProviders = TypeCache.GetMethodsWithAttribute <AdditionalUsingsProviderAttribute>();

        TryGetAssembly(path, out UnityEditor.Compilation.Assembly compilationAssembly);
        TryGetAssembly(path, out System.Reflection.Assembly assembly);

        object[]      invokeParameters = new object[] { new Info(path, compilationAssembly, assembly) };
        List <string> additionalUsings = new List <string>();

        foreach (MethodInfo providerMethod in usingProviders)
        {
            if (!providerMethod.IsStatic)
            {
                LogBadMethod(providerMethod, "Needs to be static");
                continue;
            }

            if (providerMethod.ReturnType != typeof(string[]))
            {
                LogBadMethod(providerMethod, "Return type needs to be string[]");
                continue;
            }

            var parameters = providerMethod.GetParameters();
            if (parameters.Length != 1 || parameters[0].ParameterType != typeof(Info))
            {
                LogBadMethod(providerMethod, $"Parameter needs to be '{nameof(DefaultSmartScriptResolver)}.{nameof(Info)} info'");
                continue;
            }

            var newUsings = providerMethod.Invoke(null, invokeParameters) as string[];
            if (newUsings != null)
            {
                additionalUsings.AddRange(newUsings);
            }
        }


        void LogBadMethod(MethodInfo providerMethod, string reason)
        {
            Debug.LogWarning($"Method {providerMethod.Name} has the [{nameof(AdditionalUsingsProviderAttribute)}] attribute " +
                             $"but doesn't respect the following criteria: {reason}");
        }

        return(additionalUsings.Distinct().ToArray());
    }
Exemplo n.º 3
0
        public static HashSet <ActionOperation> GetActions()
        {
            HashSet <ActionOperation> allOperations = new HashSet <ActionOperation>();

            TypeCache.MethodCollection methods = TypeCache.GetMethodsWithAttribute <ActionProviderAttribute>();
            Type returnType          = typeof(ActionOperation);
            Type alternateReturnType = typeof(IEnumerable <ActionOperation>);

            foreach (MethodInfo methodInfo in methods)
            {
                if (!methodInfo.IsStatic)
                {
                    LogWarning("is not static.");
                    continue;
                }


                ReturnType type;
                if (methodInfo.ReturnType == returnType)
                {
                    type = ReturnType.Single;
                }
                else if (methodInfo.ReturnType == alternateReturnType)
                {
                    type = ReturnType.Multiple;
                }
                else
                {
                    LogWarning($"does not return a {nameof(ActionOperation)} or {nameof(IEnumerable<ActionOperation>)}");
                    continue;
                }


                switch (type)
                {
                case ReturnType.Invalid:
                    continue;

                case ReturnType.Single:
                {
                    ActionOperation operation;
                    try
                    {
                        operation = (ActionOperation)methodInfo.Invoke(null, null);
                    }
                    catch (Exception e)
                    {
                        Debug.LogException(e);
                        continue;
                    }

                    AppendOperation(operation);
                    break;
                }

                case ReturnType.Multiple:
                {
                    IEnumerable <ActionOperation> operations;
                    try
                    {
                        operations = (IEnumerable <ActionOperation>)methodInfo.Invoke(null, null);
                    }
                    catch (Exception e)
                    {
                        Debug.LogException(e);
                        continue;
                    }

                    foreach (ActionOperation operation in operations)
                    {
                        AppendOperation(operation);
                    }
                    break;
                }

                default:
                    throw new ArgumentOutOfRangeException();
                }

                void AppendOperation(ActionOperation operation)
                {
                    if (operation == null)
                    {
                        //LogWarning($"returned a null {nameof(ActionOperation)}.");
                        return;
                    }

                    allOperations.Add(operation);
                }

                void LogWarning(string message) => Debug.LogWarning($"{methodInfo.DeclaringType}.{methodInfo.Name} with {nameof(ActionProviderAttribute)} {message}");
            }

            return(allOperations);
        }