static void LogError <TAttribute>(string message, MethodInfo mi, AttributeLoaderBehavior behavior)
 {
     if (behavior == AttributeLoaderBehavior.ThrowOnValidation)
     {
         throw new Exception(message);
     }
     UnityEngine.Debug.LogWarning($"Cannot load method \"{GetMethodFullName(mi)}\" with attribute \"{typeof(TAttribute).FullName}\": ({message})");
 }
Exemple #2
0
 public static IEnumerable <THandlerWrapper> LoadAllMethodsWithAttribute <TAttribute, THandlerWrapper>(Func <MethodInfo, TAttribute, Delegate, THandlerWrapper> generator, MethodSignature supportedSignature, AttributeLoaderBehavior behavior = AttributeLoaderBehavior.ThrowOnValidation)
     where TAttribute : Attribute
     where THandlerWrapper : struct
 {
     return(LoadAllMethodsWithAttribute(generator, new[] { supportedSignature }, behavior));
 }
Exemple #3
0
 public static IEnumerable <THandlerWrapper> LoadAllMethodsWithAttribute <TAttribute, THandlerWrapper>(Func <MethodInfo, TAttribute, Delegate, THandlerWrapper> generator, MethodSignature[] supportedSignatures, AttributeLoaderBehavior behavior = AttributeLoaderBehavior.ThrowOnValidation)
     where TAttribute : Attribute
     where THandlerWrapper : struct
 {
     return(TypeCache.GetMethodsWithAttribute <TAttribute>()
            .Select(mi =>
     {
         try
         {
             return LoadMethodWithAttribute(mi, generator, supportedSignatures).ToArray();
         }
         catch (Exception ex)
         {
             if (behavior == AttributeLoaderBehavior.ThrowOnValidation)
             {
                 throw ex;
             }
             else
             {
                 UnityEngine.Debug.LogWarning($"Cannot load method: \"{mi.Name}\" with attribute: \"{typeof(TAttribute).FullName}\" ({ex.Message})");
             }
             return null;
         }
     })
            .Where(generator => generator != null)
            .SelectMany(generator => generator));
 }
        public static IEnumerable <THandlerWrapper> LoadMethodWithAttribute <TAttribute, THandlerWrapper>(MethodInfo methodInfo, Func <MethodInfo, TAttribute, Delegate, THandlerWrapper> generator, MethodSignature[] supportedSignatures, AttributeLoaderBehavior behavior = AttributeLoaderBehavior.ThrowOnValidation)
            where TAttribute : Attribute
        {
            var loaded = new List <THandlerWrapper>();

            LoadMethodWithAttribute <TAttribute, THandlerWrapper>(methodInfo, (_, mi, att, handler) => generator(mi, att, handler), supportedSignatures, loaded, behavior);
            return(loaded);
        }
        public static IEnumerable <THandlerWrapper> LoadAllMethodsWithAttribute <TAttribute, THandlerWrapper>(Func <IReadOnlyCollection <THandlerWrapper>, MethodInfo, TAttribute, Delegate, THandlerWrapper> generator, MethodSignature[] supportedSignatures, AttributeLoaderBehavior behavior = AttributeLoaderBehavior.ThrowOnValidation)
            where TAttribute : Attribute
        {
            return(TypeCache.GetMethodsWithAttribute <TAttribute>()
                   .Aggregate(new List <THandlerWrapper>(), (accumulated, mi) =>
            {
                try
                {
                    LoadMethodWithAttribute(mi, generator, supportedSignatures, accumulated, behavior);
                }
                catch (Exception ex)
                {
                    if (behavior == AttributeLoaderBehavior.ThrowOnValidation)
                    {
                        throw ex;
                    }
                    else
                    {
                        LogError <TAttribute>(ex.Message, mi, AttributeLoaderBehavior.DoNotThrowOnValidation);
                    }

                    return accumulated;
                }

                return accumulated;
            })
                   .Where(g => g != null));
        }
 public static IEnumerable <THandlerWrapper> LoadAllMethodsWithAttribute <TAttribute, THandlerWrapper>(Func <MethodInfo, TAttribute, Delegate, THandlerWrapper> generator, MethodSignature[] supportedSignatures, AttributeLoaderBehavior behavior = AttributeLoaderBehavior.ThrowOnValidation)
     where TAttribute : Attribute
 {
     return(LoadAllMethodsWithAttribute <TAttribute, THandlerWrapper>((_, mi, att, d) => generator(mi, att, d), supportedSignatures, behavior));
 }
        public static void LoadMethodWithAttribute <TAttribute, THandlerWrapper>(MethodInfo methodInfo, Func <IReadOnlyCollection <THandlerWrapper>, MethodInfo, TAttribute, Delegate, THandlerWrapper> generator, MethodSignature[] supportedSignatures, List <THandlerWrapper> loaded, AttributeLoaderBehavior behavior = AttributeLoaderBehavior.ThrowOnValidation)
            where TAttribute : Attribute
        {
            if (!methodInfo.IsStatic)
            {
                LogError <TAttribute>($"Method {methodInfo.Name} should be static.", methodInfo, behavior);
            }

            foreach (var supportedSignature in supportedSignatures)
            {
                if (!ValidateMethodSignature(methodInfo, supportedSignature))
                {
                    continue;
                }
                var handler = CreateDelegate(methodInfo, supportedSignature.delegateType);
                foreach (var attribute in methodInfo.GetCustomAttributes <TAttribute>())
                {
                    try
                    {
                        if (attribute == null)
                        {
                            LogGeneratorError($"Method {methodInfo.Name} should have an attribute of type {typeof(TAttribute)}.", attribute, methodInfo, behavior);
                        }
                        var handlerWrapper = generator(loaded, methodInfo, attribute, handler);
                        loaded.Add(handlerWrapper);
                    }
                    catch (Exception ex)
                    {
                        if (behavior == AttributeLoaderBehavior.ThrowOnValidation)
                        {
                            throw ex;
                        }
                        LogGeneratorError(ex.Message, attribute, methodInfo, AttributeLoaderBehavior.DoNotThrowOnValidation);
                    }
                }

                return;
            }
            LogError <TAttribute>($"Method {methodInfo.Name} doesn't have the correct signature.", methodInfo, behavior);
        }