Esempio n. 1
0
 private bool IsVisualElementQuery(InjectionData injectionData)
 {
     return(injectionData.InjectionKeys.Length == 1 &&
            injectionData.InjectionKeys[0] is string injectionKeyString &&
            (injectionKeyString.StartsWith("#") || injectionKeyString.StartsWith(".")) &&
            ((injectionData.MemberInfo is FieldInfo fieldInfo && typeof(VisualElement).IsAssignableFrom(fieldInfo.FieldType)) ||
             (injectionData.MemberInfo is PropertyInfo propertyInfo && typeof(VisualElement).IsAssignableFrom(propertyInfo.PropertyType))));
 }
Esempio n. 2
0
        private static InjectionData CreateInjectionDataForMethod(Type type, MethodInfo methodInfo, InjectAttribute injectAttribute)
        {
            ParameterInfo[] parameterInfos = methodInfo.GetParameters();
            object[]        injectionKeys  = new object[parameterInfos.Length];
            foreach (ParameterInfo parameterInfo in parameterInfos)
            {
                object injectionKey   = GetInjectionKey(parameterInfo);
                int    parameterIndex = parameterInfo.Position;
                injectionKeys[parameterIndex] = injectionKey;
            }
            InjectionData injectionData = new InjectionData(type, methodInfo, injectionKeys, injectAttribute.searchMethod, injectAttribute.optional);

            return(injectionData);
        }
Esempio n. 3
0
        private static InjectionData CreateInjectionDataForFieldOrProperty(Type type, MemberInfo memberInfo, InjectAttribute injectAttribute)
        {
            object injectionKey = injectAttribute.key;

            if (injectionKey == null)
            {
                Type typeOfMember = GetTypeOfFieldOrProperty(type, memberInfo);
                injectionKey = typeOfMember;
            }
            object[]      injectionKeys = new object[] { injectionKey };
            InjectionData injectionData = new InjectionData(type, memberInfo, injectionKeys, injectAttribute.searchMethod, injectAttribute.optional);

            return(injectionData);
        }
Esempio n. 4
0
 public void Inject(object target, InjectionData injectionData)
 {
     if (injectionData.searchMethod == SearchMethods.SearchInBindings)
     {
         InjectMemberFromBindings(target, injectionData.MemberInfo, injectionData.InjectionKeys, injectionData.isOptional);
     }
     else if (target is MonoBehaviour)
     {
         InjectMemberFromUnitySearchMethod(target as MonoBehaviour, injectionData.MemberInfo, injectionData.searchMethod, injectionData.isOptional);
     }
     else
     {
         throw new InjectionException($"Cannot perform injection via {injectionData.searchMethod} into an object of type {target.GetType()}."
                                      + " Only MonoBehaviour instances are supported.");
     }
 }
Esempio n. 5
0
        internal static List <InjectionData> CreateInjectionDatas(Type type)
        {
            List <InjectionData> result = new List <InjectionData>();

            MemberInfo[] memberInfos = type.GetMembers(BindingFlags.Public
                                                       | BindingFlags.NonPublic
                                                       | BindingFlags.Instance);
            foreach (MemberInfo memberInfo in memberInfos)
            {
                InjectionData newInjectionData = CreateInjectionData(type, memberInfo);
                if (newInjectionData != null)
                {
                    result.Add(newInjectionData);
                }
            }
            return(result);
        }
Esempio n. 6
0
        private static InjectionData CreateInjectionData(Type type, MemberInfo memberInfo)
        {
            InjectAttribute injectAttribute = memberInfo.GetCustomAttribute <InjectAttribute>();
            InjectionData   result          = null;

            if (injectAttribute != null)
            {
                if (memberInfo is FieldInfo || memberInfo is PropertyInfo)
                {
                    result = ReflectionUtils.CreateInjectionDataForFieldOrProperty(type, memberInfo, injectAttribute);
                }
                else if (memberInfo is MethodInfo)
                {
                    result = ReflectionUtils.CreateInjectionDataForMethod(type, memberInfo as MethodInfo, injectAttribute);
                }
            }
            return(result);
        }
Esempio n. 7
0
 public void Inject(object target, InjectionData injectionData)
 {
     // Check if the injection key is a USS Selector to query a VisualElement
     if (IsVisualElementQuery(injectionData))
     {
         InjectMemberFromUiDocument(target, injectionData.MemberInfo, injectionData.InjectionKeys[0] as string, injectionData.isOptional);
     }
     else if (injectionData.searchMethod == SearchMethods.SearchInBindings)
     {
         InjectMemberFromBindings(target, injectionData.MemberInfo, injectionData.InjectionKeys, injectionData.isOptional);
     }
     else if (target is MonoBehaviour)
     {
         InjectMemberFromUnitySearchMethod(target as MonoBehaviour, injectionData.MemberInfo, injectionData.searchMethod, injectionData.isOptional);
     }
     else
     {
         throw new InjectionException($"Cannot perform injection via {injectionData.searchMethod} into an object of type {target.GetType()}."
                                      + " Only MonoBehaviour instances are supported.");
     }
 }
        private static int CheckInjectableFromUnitySearchMethod(MonoBehaviour script, Type type, InjectionData injectionData)
        {
            if (injectionData.InjectionKeys.Length > 1)
            {
                // If there are multiple keys, then it must be for a method or constructor with multiple parameters
                LogErrorCannotBeInjected($"The search method {injectionData.searchMethod} can only be used on a field or property.",
                                         script, type, injectionData.MemberInfo);
                return(1);
            }

            object injectionKey = injectionData.InjectionKeys[0];

            if (!(injectionKey is Type))
            {
                LogErrorCannotBeInjected($"The search method {injectionData.searchMethod} can not be used with a custom key.",
                                         script, type, injectionData.MemberInfo);
                return(1);
            }
            Type componentType = injectionKey as Type;

            UnityEngine.Object searchResult = UniInjectUtils.InvokeUnitySearchMethod(script, injectionData.searchMethod, componentType);
            if (searchResult == null)
            {
                LogErrorCannotBeInjected($"No instance of {componentType} found using {injectionData.searchMethod}.",
                                         script, type, injectionData.MemberInfo);
                return(1);
            }

            return(0);
        }
        private static int CheckInjectableFromBindings(MonoBehaviour script, Type type, List <IBinding> bindings, InjectionData injectionData)
        {
            // Injector type is created during scene injection.
            HashSet <Type> ignoredTypes = new HashSet <Type> {
                typeof(Injector)
            };

            foreach (object key in injectionData.InjectionKeys.Where(it => !ignoredTypes.Contains(it)))
            {
                List <IBinding> matchingBindings = bindings.Where(binding => object.Equals(binding.GetKey(), key)).ToList();
                if (matchingBindings.Count == 0)
                {
                    LogErrorCannotBeInjected($"Missing binding for key {key}",
                                             script, type, injectionData.MemberInfo);
                    return(1);
                }
                else if (matchingBindings.Count > 1)
                {
                    LogErrorCannotBeInjected($"Multiple bindings for key {key}",
                                             script, type, injectionData.MemberInfo);
                    return(1);
                }
            }
            return(0);
        }
Esempio n. 10
0
 private static int CheckInjectableFromBindings(MonoBehaviour script, Type type, List <IBinding> bindings, InjectionData injectionData)
 {
     foreach (object key in injectionData.InjectionKeys)
     {
         List <IBinding> matchingBindings = bindings.Where(binding => object.Equals(binding.GetKey(), key)).ToList();
         if (matchingBindings.Count == 0)
         {
             LogErrorCannotBeInjected($"Missing binding for key {key}",
                                      script, type, injectionData.MemberInfo);
             return(1);
         }
         else if (matchingBindings.Count > 1)
         {
             LogErrorCannotBeInjected($"Multiple bindings for key {key}",
                                      script, type, injectionData.MemberInfo);
             return(1);
         }
     }
     return(0);
 }