Ejemplo n.º 1
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            Injectable injectable = attribute as Injectable;

            Object injectedReferenceValue = property.objectReferenceValue;

            if (property.propertyType == SerializedPropertyType.ObjectReference)
            {
                EditorGUI.BeginProperty(position, label, property);

                var fieldPosition = position;

                fieldPosition.height = 20;

                if (injectable.DisplayName != null)
                {
                    label.text = injectable.DisplayName;
                }

                injectedReferenceValue = EditorGUI.ObjectField(fieldPosition, label, property.objectReferenceValue, typeof(Object), true);

                if (injectedReferenceValue != null && injectedReferenceValue.GetType() == typeof(GameObject))
                {
                    GameObject injectedGameObject = (GameObject)injectedReferenceValue;

                    injectedReferenceValue = injectedGameObject.GetComponent(injectable.InjectedType);
                }

                else if (!injectable.IsValid(injectedReferenceValue))
                {
                    injectedReferenceValue = null;
                }

                error = false;
            }
            else
            {
                injectedReferenceValue = null;

                // Shows an error message if isn't an Object Reference
                ShowError("A reference type is needed, " + property.type + " isn't.", position);
            }

            if (!error)
            {
                if (injectedReferenceValue == null)
                {
                    ShowInfo("Must implement " + injectable.InjectedType.Name, position);
                }
                else
                {
                    ShowInfo("Injected [" + injectedReferenceValue.ToString() + "]", position, active: true);
                }
            }

            property.objectReferenceValue = injectedReferenceValue;

            EditorGUI.EndProperty();
        }
Ejemplo n.º 2
0
 private void TryRegisterProvider(Injectable injectable)
 {
     if (injectable.Type != null && injectable.Type.IsSubclassOf(typeof(Provider)) && injectable.Tag == null)
     {
         var key      = new InjectionKey(injectable.Type, null);
         var provider = ProviderFactory.CreateGeneric(injectable.Type);
         providers.Add(key, provider);
     }
 }
Ejemplo n.º 3
0
        public IResolver GetResolverForDependency(Injectable injectable)
        {
            var type = injectable.Attribute.GetType();

            if (resolvers.ContainsKey(type))
            {
                return(resolvers[type]);
            }
            return(null);
        }
Ejemplo n.º 4
0
        private void TryInject(Injectable injectable, object dependency)
        {
            if (!IsValidDependency(injectable, dependency))
            {
                Debug.LogError(string.Format("[{0}] {1} {2}", injectable.Object, "Could not find dependency for", injectable));
                return;
            }

            injectable.Inject(dependency);
        }
Ejemplo n.º 5
0
        public IProvider GetProviderForDependency(Injectable injectable)
        {
            var key = new InjectionKey(injectable.Type, injectable.Tag);

            if (providers.ContainsKey(key))
            {
                return(providers[key]);
            }
            return(null);
        }
Ejemplo n.º 6
0
        public object Resolve(Injectable injectable)
        {
            var attribute = (FindResourceOfTypeAttribute)injectable.Attribute;

            if (attribute.ComponentType == null)
            {
                return(Resources.LoadAll("", injectable.Type).FirstOrDefault());
            }
            //else
            return(Resources.LoadAll("", attribute.ComponentType).FirstOrDefault());
        }
Ejemplo n.º 7
0
 private void EvaluateInjectable(Injectable injectable)
 {
     if (injectable.Attribute is UnityConvenienceAttribute)
     {
         dependencyMap.RegisterResolvableDependent(injectable);
     }
     else if (injectable.Attribute is InjectAttribute)
     {
         dependencyMap.RegisterProvidableDependent(injectable);
     }
 }
Ejemplo n.º 8
0
        private void TryInjectResolvable(Injectable injectable)
        {
            var resolver = dependencyMap.GetResolverForDependency(injectable);

            if (resolver == null)
            {
                Debug.LogError(string.Format("[{0}] {1} {2}", injectable.Object, "Could not find resolver for", injectable));
                return;
            }
            var dependency = resolver.Resolve(injectable);

            TryInject(injectable, dependency);
        }
Ejemplo n.º 9
0
        private void EvaluateInjectableProvider(Injectable injectable, IProvider provider)
        {
            dependencyMap.RegisterProvider(injectable.Type, injectable.Tag, provider);

            if (injectable.Attribute is UnityConvenienceAttribute)
            {
                dependencyMap.RegisterResolvableDependent(injectable);
            }
            else
            {
                throw new InjectionException(injectable.Object, "A member cannot be annotated both with [Inject] and [Provides]" + injectable.Type);
            }
        }
        public object Resolve(Injectable injectable)
        {
            var attribute = (FindObjectOfTypeAttribute)injectable.Attribute;

            if (attribute.ComponentType == null)
            {
                return(Object.FindObjectOfType(injectable.Type));
            }
            else
            {
                return(Object.FindObjectOfType(attribute.ComponentType));
            }
        }
Ejemplo n.º 11
0
        private void TryInjectProvidable(Injectable injectable)
        {
            var provider = dependencyMap.GetProviderForDependency(injectable);

            if (provider == null)
            {
                Debug.LogError(string.Format("[{0}] {1} {2}", injectable.Object, "Could not find provider for", injectable));
                return;
            }

            var dependency = provider.Get();

            TryInject(injectable, dependency);
        }
Ejemplo n.º 12
0
 private void EvaluateAttributes(MemberInfo info, Injectable injectable, IProvider provider)
 {
     if (injectable != null && provider != null)
     {
         EvaluateInjectableProvider(injectable, provider);
     }
     else if (injectable != null)
     {
         EvaluateInjectable(injectable);
     }
     else if (provider != null)
     {
         EvaluateProvider(provider);
     }
 }
Ejemplo n.º 13
0
        public object Resolve(Injectable injectable)
        {
            var tag        = ((FindWithTagAttribute)injectable.Attribute).Tag;
            var gameObject = GameObject.FindWithTag(tag);

            if (gameObject == null)
            {
                return(null);
            }
            if (injectable.Type != typeof(GameObject))
            {
                return(gameObject.GetComponent(injectable.Type));
            }
            return(gameObject);
        }
Ejemplo n.º 14
0
        public void TestInjectorContainsAllInjectableFields()
        {
            var container  = new DiContainer();
            var injectable = new Injectable("A");

            container.Bind <IInjectable>().ToInstance(injectable);

            var instance = new TestClass();

            container.BindInstance(instance);

            container.TryResolveAll();

            Assert.IsNotNull(instance.Injectable);
            Assert.AreEqual("A", instance.Injectable.Name);
        }
Ejemplo n.º 15
0
        public object Resolve(Injectable injectable)
        {
            var attribute = (FindResourceOfTypeAttribute)injectable.Attribute;

            if (attribute.ComponentType == null)
            {
                //var resources = Resources.FindObjectsOfTypeAll(injectable.Type);
                var resources = Resources.LoadAll("", injectable.Type);
                return(resources?[0]);
            }
            else
            {
                //var resources = Resources.FindObjectsOfTypeAll(attribute.ComponentType);
                var resources = Resources.LoadAll("", attribute.ComponentType);  //.ToArray();
                return(resources?[0]);
            }
        }
Ejemplo n.º 16
0
        public object Resolve(Injectable injectable)
        {
            var name       = ((FindAttribute)injectable.Attribute).GameObjectName;
            var gameObject = GameObject.Find(name);

            if (gameObject == null)
            {
                return(null);
            }
            else if (injectable.Type != typeof(GameObject))
            {
                return(gameObject.GetComponent(injectable.Type));
            }
            else
            {
                return(gameObject);
            }
        }
Ejemplo n.º 17
0
        public object Resolve(Injectable injectable)
        {
            var monoBehaviour = injectable.Object as MonoBehaviour;

            if (monoBehaviour == null)
            {
                throw new InjectionException(injectable.Object, "[GetComponent] annotation on a non-MonoBehaviour");
            }

            var attribute = (GetComponentAttribute)injectable.Attribute;

            if (attribute.ComponentType == null)
            {
                return(monoBehaviour.GetComponent(injectable.Type));
            }
            else
            {
                return(monoBehaviour.GetComponent(attribute.ComponentType));
            }
        }
Ejemplo n.º 18
0
        public object Resolve(Injectable injectable)
        {
            var name = ((FindRelativeAttribute)injectable.Attribute).PathName;

            var parent    = ((MonoBehaviour)injectable.Object).transform;
            var transform = parent.FindRelativePath(name);

            if (transform == null)
            {
                return(null);
            }
            else if (injectable.Type.IsAssignableFrom(transform.GetType()))
            {
                return(transform);
            }
            else
            {
                return(transform.GetComponent(injectable.Type));
            }
        }
        public object Resolve(Injectable injectable)
        {
            var name = ((AnimatorStringHashAttribute)injectable.Attribute).HashName;

            return(Animator.StringToHash(name));
        }
Ejemplo n.º 20
0
 public void Inject(TTarget target, TInjectableValue value) => Injectable.Inject(target, value);
Ejemplo n.º 21
0
 public InjectableWrapper(Injectable injectable) : base(injectable.Type)
 {
     this._injectable = injectable;
 }
Ejemplo n.º 22
0
 private static bool IsValidDependency(Injectable injectable, object dependency)
 {
     return(dependency != null && !dependency.Equals(null) && injectable.Type.IsInstanceOfType(dependency));
 }
Ejemplo n.º 23
0
 public void RegisterResolvableDependent(Injectable injectable)
 {
     resolvableDependents.Add(injectable);
 }
Ejemplo n.º 24
0
 public void RegisterProvidableDependent(Injectable injectable)
 {
     providableDependents.Add(injectable);
     TryRegisterProvider(injectable);
 }
Ejemplo n.º 25
0
 public TReject Reject(TTarget target, TInjectableValue value) => Injectable.Reject(target, value);