Exemple #1
0
        public static MethodInfo GetKeySourceMethodInfo(this Type rootType, AssetDictionaryAttribute attribute)
        {
            if (attribute.UseAssetFileNameAsKey)
            {
                return(GetFileNameAsKey(rootType));
            }

            var methodInfo = GetPropertyKeySourceMethodInfo(rootType, attribute, allowGeneral: false);

            if (methodInfo != null)
            {
                return(methodInfo);
            }

            methodInfo = GetCustomKeySourceMethodInfo(rootType, attribute, allowGeneral: false);
            if (methodInfo != null)
            {
                return(methodInfo);
            }

            methodInfo = GetPropertyKeySourceMethodInfo(rootType, attribute, allowGeneral: true);
            if (methodInfo != null)
            {
                return(methodInfo);
            }

            methodInfo = GetCustomKeySourceMethodInfo(rootType, attribute, allowGeneral: true);
            if (methodInfo != null)
            {
                return(methodInfo);
            }

            return(default(MethodInfo));
        }
Exemple #2
0
        private static MethodInfo GetPropertyKeySourceMethodInfo(Type rootType, AssetDictionaryAttribute attribute, bool allowGeneral)
        {
            foreach (var propertyInfo in rootType.GetProperties())
            {
                var propertyAttributes = propertyInfo.Get <DictionaryKeyAttribute>().ToArray();
                if (propertyAttributes.Length == 0)
                {
                    continue;
                }

                foreach (var propertyAttribute in propertyAttributes)
                {
                    if (!propertyInfo.CanRead)
                    {
                        Debug.LogError($"Property {propertyInfo.Name} is not readable and have {nameof(DictionaryKeyAttribute)}. It will be ignored.");
                        continue;
                    }

                    if (allowGeneral && string.IsNullOrEmpty(propertyAttribute.GroupName))
                    {
                        return(propertyInfo.GetGetMethod());
                    }

                    if (propertyAttribute.GroupName == attribute.Group)
                    {
                        return(propertyInfo.GetGetMethod());
                    }
                }
            }

            return(null);
        }
Exemple #3
0
        private static MethodInfo GetCustomKeySourceMethodInfo(Type rootType, AssetDictionaryAttribute attribute, bool allowGeneral)
        {
            var keySourceType = attribute.KeySourceType ?? rootType;

            foreach (var methodInfo in keySourceType.GetMethods(BindingFlags.Static | BindingFlags.Public))
            {
                foreach (var methodAttribute in methodInfo.Get <DictionaryKeySourceAttribute>())
                {
                    if (methodAttribute == null)
                    {
                        continue;
                    }

                    if (!allowGeneral && methodAttribute.Group != attribute.Group)
                    {
                        continue;
                    }

                    var parameters = methodInfo.GetParameters();
                    if (parameters.Length < 1 &&
                        parameters.Length > 2)
                    {
                        continue;
                    }

                    if (parameters.Length == 1 &&
                        parameters[0].ParameterType != rootType)
                    {
                        Debug.LogError($"Method {methodInfo.Name} is not valid for selecting keys! Type {rootType.Name} is not assignable from param type {parameters[0].ParameterType.Name}.");
                        continue;
                    }

                    var isValid = true;
                    foreach (var parameterInfo in parameters)
                    {
                        if (parameterInfo.ParameterType != rootType &&
                            parameterInfo.ParameterType != typeof(TextAsset))
                        {
                            Debug.LogError($"Method {methodInfo.Name} is not valid for selecting keys! Method have invalid param type {parameterInfo.ParameterType}");
                            isValid = false;
                        }
                    }

                    if (!isValid)
                    {
                        continue;
                    }

                    return(methodInfo);
                }
            }

            return(null);
        }