Пример #1
0
        public GameObject Instantiate(List <TypeValuePair> args, out Action injectAction)
        {
            var  context = new InjectContext(_container, _argumentTarget, null);
            bool shouldMakeActive;
            var  gameObject = _container.CreateAndParentPrefab(
                GetPrefab(), _gameObjectBindInfo, context, out shouldMakeActive);

            Assert.IsNotNull(gameObject);

            injectAction = () =>
            {
                var allArgs = _extraArguments.Concat(args).ToList();

                if (_argumentTarget == null)
                {
                    Assert.That(
                        allArgs.IsEmpty(),
                        "Unexpected arguments provided to prefab instantiator.  Arguments are not allowed if binding multiple components in the same binding");
                }

                Component targetComponent = null;

                if (_argumentTarget == null || allArgs.IsEmpty())
                {
                    _container.InjectGameObject(gameObject);
                }
                else
                {
                    var injectArgs = new InjectArgs()
                    {
                        ExtraArgs          = allArgs,
                        Context            = context,
                        ConcreteIdentifier = null
                    };

                    targetComponent = _container.InjectGameObjectForComponentExplicit(
                        gameObject, _argumentTarget, injectArgs);
                }

                if (shouldMakeActive)
                {
                    gameObject.SetActive(true);
                }

                if (_instantiateCallback != null && _argumentTarget != null)
                {
                    if (targetComponent == null)
                    {
                        targetComponent = gameObject.GetComponentInChildren(_argumentTarget);
                    }

                    if (targetComponent != null)
                    {
                        _instantiateCallback(context, targetComponent);
                    }
                }
            };

            return(gameObject);
        }
Пример #2
0
        public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(InjectContext context, List<TypeValuePair> args)
        {
            Assert.IsNotNull(context);

            bool autoInject = false;

            var instanceType = GetTypeToCreate(context.MemberType);

            var injectArgs = new InjectArgs()
            {
                ExtraArgs = _extraArguments.Concat(args).ToList(),
                Context = context,
                ConcreteIdentifier = _concreteIdentifier,
                UseAllArgs = false,
            };

            var instance = _container.InstantiateExplicit(
                instanceType, autoInject, injectArgs);

            // Return before property/field/method injection to allow circular dependencies
            yield return new List<object>() { instance };

            injectArgs.UseAllArgs = true;

            _container.InjectExplicit(instance, instanceType, injectArgs);
        }
Пример #3
0
        public IEnumerator <GameObject> Instantiate(List <TypeValuePair> args)
        {
            var gameObject = _container.CreateAndParentPrefab(GetPrefab(), _gameObjectBindInfo);

            Assert.IsNotNull(gameObject);

            // Return it before inject so we can do circular dependencies
            yield return(gameObject);

            var allArgs = _extraArguments.Concat(args).ToList();

            if (_argumentTarget == null)
            {
                Assert.That(allArgs.IsEmpty(),
                            "Unexpected arguments provided to prefab instantiator.  Arguments are not allowed if binding multiple components in the same binding");
            }

            if (_argumentTarget == null || allArgs.IsEmpty())
            {
                _container.InjectGameObject(gameObject);
            }
            else
            {
                var injectArgs = new InjectArgs()
                {
                    ExtraArgs          = allArgs,
                    Context            = new InjectContext(_container, _argumentTarget, null),
                    ConcreteIdentifier = null,
                };

                _container.InjectGameObjectForComponentExplicit(
                    gameObject, _argumentTarget, injectArgs);
            }
        }
Пример #4
0
        public IEnumerator <List <object> > GetAllInstancesWithInjectSplit(InjectContext context, List <TypeValuePair> args)
        {
            Assert.IsNotNull(context);

            bool autoInject = false;

            var instanceType = GetTypeToCreate(context.MemberType);

            var injectArgs = new InjectArgs()
            {
                ExtraArgs          = _extraArguments.Concat(args).ToList(),
                Context            = context,
                ConcreteIdentifier = _concreteIdentifier,
            };

            var instance = _container.InstantiateExplicit(
                instanceType, autoInject, injectArgs);

            // Return before property/field/method injection to allow circular dependencies
            yield return(new List <object>()
            {
                instance
            });

            _container.InjectExplicit(instance, instanceType, injectArgs);
        }
        public IEnumerator <List <object> > GetAllInstancesWithInjectSplit(
            InjectContext context, List <TypeValuePair> args)
        {
            Assert.IsEmpty(args);

            Assert.IsNotNull(context);

            var objects = Resources.LoadAll(_resourcePath, _resourceType)
                          .Select(x => ScriptableObject.Instantiate(x)).Cast <object>().ToList();

            Assert.That(!objects.IsEmpty(),
                        "Could not find resource at path '{0}' with type '{1}'", _resourcePath, _resourceType);

            yield return(objects);

            var injectArgs = new InjectArgs()
            {
                ExtraArgs          = _extraArguments.Concat(args).ToList(),
                Context            = context,
                ConcreteIdentifier = _concreteIdentifier,
            };

            foreach (var obj in objects)
            {
                _container.InjectExplicit(
                    obj, _resourceType, injectArgs);
            }
        }
        public List <object> GetAllInstancesWithInjectSplit(
            InjectContext context, List <TypeValuePair> args, out Action injectAction)
        {
            Assert.IsNotNull(context);

            var instanceType = GetTypeToCreate(context.MemberType);

            var injectArgs = new InjectArgs()
            {
                ExtraArgs          = _extraArguments.Concat(args).ToList(),
                Context            = context,
                ConcreteIdentifier = _concreteIdentifier,
            };

            var instance = _container.InstantiateExplicit(
                instanceType, false, injectArgs);

            injectAction = () =>
            {
                _container.InjectExplicit(
                    instance, instanceType, injectArgs);

                if (_instantiateCallback != null)
                {
                    _instantiateCallback(context, instance);
                }
            };

            return(new List <object>()
            {
                instance
            });
        }
        public List <object> GetAllInstancesWithInjectSplit(
            InjectContext context, List <TypeValuePair> args, out Action injectAction)
        {
            Assert.IsNotNull(context);

            Assert.That(context.ObjectType.DerivesFrom <Component>(),
                        "Object '{0}' can only be injected into MonoBehaviour's since it was bound with 'FromNewComponentSibling'. Attempted to inject into non-MonoBehaviour '{1}'",
                        context.MemberType, context.ObjectType);

            object instance;

            if (!_container.IsValidating || TypeAnalyzer.ShouldAllowDuringValidation(_componentType))
            {
                var gameObj = ((Component)context.ObjectInstance).gameObject;

                instance = gameObj.GetComponent(_componentType);

                if (instance != null)
                {
                    injectAction = null;
                    return(new List <object>()
                    {
                        instance
                    });
                }

                instance = gameObj.AddComponent(_componentType);
            }
            else
            {
                instance = new ValidationMarker(_componentType);
            }

            // Note that we don't just use InstantiateComponentOnNewGameObjectExplicit here
            // because then circular references don't work

            var injectArgs = new InjectArgs()
            {
                ExtraArgs          = _extraArguments.Concat(args).ToList(),
                Context            = context,
                ConcreteIdentifier = _concreteIdentifier
            };

            injectAction = () =>
            {
                _container.InjectExplicit(instance, _componentType, injectArgs);

                Assert.That(injectArgs.ExtraArgs.IsEmpty());

                if (_instantiateCallback != null)
                {
                    _instantiateCallback(context, instance);
                }
            };

            return(new List <object>()
            {
                instance
            });
        }
Пример #8
0
        public List <object> GetAllInstancesWithInjectSplit(
            InjectContext context, List <TypeValuePair> args, out Action injectAction)
        {
            Assert.IsNotNull(context);

            bool autoInject = false;

            Type instanceType = GetTypeToCreate(context.MemberType);

            InjectArgs injectArgs = new InjectArgs()
            {
                ExtraArgs          = _extraArguments.Concat(args).ToList(),
                Context            = context,
                ConcreteIdentifier = _concreteIdentifier,
            };

            object instance = _container.InstantiateExplicit(
                instanceType, autoInject, injectArgs);

            injectAction = () => _container.InjectExplicit(instance, instanceType, injectArgs);
            return(new List <object>()
            {
                instance
            });
        }
        public IEnumerator <List <object> > GetAllInstancesWithInjectSplit(InjectContext context, List <TypeValuePair> args)
        {
            Assert.IsNotNull(context);

            Assert.That(context.ObjectType.DerivesFrom <Component>(),
                        "Object '{0}' can only be injected into MonoBehaviour's since it was bound with 'FromSiblingComponent'. Attempted to inject into non-MonoBehaviour '{1}'",
                        context.MemberType, context.ObjectType);

            object instance;

            if (!_container.IsValidating || DiContainer.CanCreateOrInjectDuringValidation(_componentType))
            {
                var gameObj = ((Component)context.ObjectInstance).gameObject;

                instance = gameObj.GetComponent(_componentType);

                if (instance != null)
                {
                    yield return(new List <object>()
                    {
                        instance
                    });

                    yield break;
                }

                instance = gameObj.AddComponent(_componentType);
            }
            else
            {
                instance = new ValidationMarker(_componentType);
            }

            // Note that we don't just use InstantiateComponentOnNewGameObjectExplicit here
            // because then circular references don't work
            yield return(new List <object>()
            {
                instance
            });

            var injectArgs = new InjectArgs()
            {
                ExtraArgs          = _extraArguments.Concat(args).ToList(),
                UseAllArgs         = true,
                Context            = context,
                ConcreteIdentifier = _concreteIdentifier,
            };

            _container.InjectExplicit(instance, _componentType, injectArgs);

            Assert.That(injectArgs.ExtraArgs.IsEmpty());
        }
        public IEnumerator <List <object> > GetAllInstancesWithInjectSplit(InjectContext context, List <TypeValuePair> args)
        {
            Assert.IsNotNull(context);

            object instance;

            // We still want to make sure we can get the game object during validation
            var gameObj = GetGameObject(context);

            if (!_container.IsValidating || DiContainer.CanCreateOrInjectDuringValidation(_componentType))
            {
                if (_componentType == typeof(Transform))
                // Treat transform as a special case because it's the one component that's always automatically added
                // Otherwise, calling AddComponent below will fail and return null
                // This is nice to allow doing things like
                //      Container.Bind<Transform>().FromNewComponentOnNewGameObject();
                {
                    instance = gameObj.transform;
                }
                else
                {
                    instance = gameObj.AddComponent(_componentType);
                }

                Assert.IsNotNull(instance);
            }
            else
            {
                instance = new ValidationMarker(_componentType);
            }

            // Note that we don't just use InstantiateComponentOnNewGameObjectExplicit here
            // because then circular references don't work
            yield return(new List <object>()
            {
                instance
            });

            var injectArgs = new InjectArgs()
            {
                ExtraArgs          = _extraArguments.Concat(args).ToList(),
                Context            = context,
                ConcreteIdentifier = _concreteIdentifier,
            };

            _container.InjectExplicit(instance, _componentType, injectArgs);

            Assert.That(injectArgs.ExtraArgs.IsEmpty());
        }
Пример #11
0
        public List <object> GetAllInstancesWithInjectSplit(
            InjectContext context, List <TypeValuePair> args, out Action injectAction)
        {
            Assert.IsNotNull(context);

            List <object> objects;

            if (_createNew)
            {
                objects = Resources.LoadAll(_resourcePath, _resourceType)
                          .Select(x => ScriptableObject.Instantiate(x)).Cast <object>().ToList();
            }
            else
            {
                objects = Resources.LoadAll(_resourcePath, _resourceType)
                          .Cast <object>().ToList();
            }

            Assert.That(!objects.IsEmpty(),
                        "Could not find resource at path '{0}' with type '{1}'", _resourcePath, _resourceType);

            var injectArgs = new InjectArgs()
            {
                ExtraArgs          = _extraArguments.Concat(args).ToList(),
                Context            = context,
                ConcreteIdentifier = _concreteIdentifier,
            };

            injectAction = () =>
            {
                foreach (var obj in objects)
                {
                    _container.InjectExplicit(
                        obj, _resourceType, injectArgs);

                    if (_instantiateCallback != null)
                    {
                        _instantiateCallback(context, obj);
                    }
                }
            };

            return(objects);
        }
Пример #12
0
        public IEnumerator <List <object> > GetAllInstancesWithInjectSplit(
            InjectContext context, List <TypeValuePair> args)
        {
            Assert.IsNotNull(context);

            List <object> objects;

            if (_createNew)
            {
                objects = new List <object> ()
                {
                    ScriptableObject.Instantiate(_resource)
                };
            }
            else
            {
                objects = new List <object> ()
                {
                    _resource
                };
            }

            Assert.That(!objects.IsEmpty(),
                        "Could not find object '{0}' with type '{1}'", _resource, _resourceType);

            yield return(objects);

            var injectArgs = new InjectArgs()
            {
                ExtraArgs          = _extraArguments.Concat(args).ToList(),
                Context            = context,
                ConcreteIdentifier = _concreteIdentifier,
            };

            foreach (var obj in objects)
            {
                _container.InjectExplicit(
                    obj, _resourceType, injectArgs);
            }
        }
Пример #13
0
        public List <object> GetAllInstancesWithInjectSplit(InjectContext context, List <TypeValuePair> args, out Action injectAction)
        {
            Assert.IsNotNull(context);

            List <object> objects;

            if (_createNew)
            {
                objects = new List <object>()
                {
                    UnityEngine.ScriptableObject.Instantiate(_resource)
                };
            }
            else
            {
                objects = new List <object>()
                {
                    _resource
                };
            }

            var injectArgs = new InjectArgs()
            {
                ExtraArgs          = _extraArguments.Concat(args).ToList(),
                Context            = context,
                ConcreteIdentifier = _concreteIdentifier,
            };

            injectAction = () =>
            {
                foreach (var obj in objects)
                {
                    _container.InjectExplicit(
                        obj, _resourceType, injectArgs);
                }
            };

            return(objects);
        }
Пример #14
0
        public IEnumerator <List <object> > GetAllInstancesWithInjectSplit(InjectContext context, List <TypeValuePair> args)
        {
            Assert.IsNotNull(context);

            object instance;

            // We still want to make sure we can get the game object during validation
            var gameObj = GetGameObject(context);

            if (!_container.IsValidating || DiContainer.CanCreateOrInjectDuringValidation(_componentType))
            {
                instance = gameObj.AddComponent(_componentType);
            }
            else
            {
                instance = new ValidationMarker(_componentType);
            }

            // Note that we don't just use InstantiateComponentOnNewGameObjectExplicit here
            // because then circular references don't work
            yield return(new List <object>()
            {
                instance
            });

            var injectArgs = new InjectArgs()
            {
                ExtraArgs          = _extraArguments.Concat(args).ToList(),
                UseAllArgs         = true,
                TypeInfo           = TypeAnalyzer.GetInfo(_componentType),
                Context            = context,
                ConcreteIdentifier = _concreteIdentifier,
            };

            _container.InjectExplicit(instance, injectArgs);

            Assert.That(injectArgs.ExtraArgs.IsEmpty());
        }
        public List <object> GetAllInstancesWithInjectSplit(
            InjectContext context, List <TypeValuePair> args, out Action injectAction)
        {
            Assert.IsNotNull(context);

            object instance;

            // We still want to make sure we can get the game object during validation
            var gameObj = GetGameObject(context);

            var wasActive = gameObj.activeSelf;

            if (wasActive && ShouldToggleActive)
            {
                // We need to do this in some cases to ensure that [Inject] always gets
                // called before awake / start
                gameObj.SetActive(false);
            }

            if (!_container.IsValidating || TypeAnalyzer.ShouldAllowDuringValidation(_componentType))
            {
                if (_componentType == typeof(Transform))
                // Treat transform as a special case because it's the one component that's always automatically added
                // Otherwise, calling AddComponent below will fail and return null
                // This is nice to allow doing things like
                //      Container.Bind<Transform>().FromNewComponentOnNewGameObject();
                {
                    instance = gameObj.transform;
                }
                else
                {
                    instance = gameObj.AddComponent(_componentType);
                }

                Assert.IsNotNull(instance);
            }
            else
            {
                instance = new ValidationMarker(_componentType);
            }

            injectAction = () =>
            {
                try
                {
                    var injectArgs = new InjectArgs()
                    {
                        ExtraArgs          = _extraArguments.Concat(args).ToList(),
                        Context            = context,
                        ConcreteIdentifier = _concreteIdentifier
                    };

                    _container.InjectExplicit(instance, _componentType, injectArgs);
                }
                finally
                {
                    if (wasActive && ShouldToggleActive)
                    {
                        gameObj.SetActive(true);
                    }
                }
            };
            return(new List <object>()
            {
                instance
            });
        }
        public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(InjectContext context, List<TypeValuePair> args)
        {
            Assert.IsNotNull(context);

            object instance;

            // We still want to make sure we can get the game object during validation
            var gameObj = GetGameObject(context);

            if (!_container.IsValidating || DiContainer.CanCreateOrInjectDuringValidation(_componentType))
            {
                instance = gameObj.AddComponent(_componentType);
            }
            else
            {
                instance = new ValidationMarker(_componentType);
            }

            // Note that we don't just use InstantiateComponentOnNewGameObjectExplicit here
            // because then circular references don't work
            yield return new List<object>() { instance };

            var injectArgs = new InjectArgs()
            {
                ExtraArgs = _extraArguments.Concat(args).ToList(),
                UseAllArgs = true,
                Context = context,
                ConcreteIdentifier = _concreteIdentifier,
            };

            _container.InjectExplicit(instance, _componentType, injectArgs);

            Assert.That(injectArgs.ExtraArgs.IsEmpty());
        }
        public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(InjectContext context, List<TypeValuePair> args)
        {
            Assert.IsNotNull(context);

            Assert.That(context.ObjectType.DerivesFrom<Component>(),
                "Object '{0}' can only be injected into MonoBehaviour's since it was bound with 'FromSiblingComponent'. Attempted to inject into non-MonoBehaviour '{1}'",
                context.MemberType, context.ObjectType);

            object instance;

            if (!_container.IsValidating || DiContainer.CanCreateOrInjectDuringValidation(_componentType))
            {
                var gameObj = ((Component)context.ObjectInstance).gameObject;

                instance = gameObj.GetComponent(_componentType);

                if (instance != null)
                {
                    yield return new List<object>() { instance };
                    yield break;
                }

                instance = gameObj.AddComponent(_componentType);
            }
            else
            {
                instance = new ValidationMarker(_componentType);
            }

            // Note that we don't just use InstantiateComponentOnNewGameObjectExplicit here
            // because then circular references don't work
            yield return new List<object>() { instance };

            var injectArgs = new InjectArgs()
            {
                ExtraArgs = _extraArguments.Concat(args).ToList(),
                UseAllArgs = true,
                Context = context,
                ConcreteIdentifier = _concreteIdentifier,
            };

            _container.InjectExplicit(instance, _componentType, injectArgs);

            Assert.That(injectArgs.ExtraArgs.IsEmpty());
        }