Esempio n. 1
0
        public BindInfo SpawnBindInfo()
        {
            var bindInfo = UniDiPools.SpawnBindInfo();

            AddDisposable(bindInfo);
            return(bindInfo);
        }
        public DiContainer CreateSubContainer(List <TypeValuePair> args, InjectContext context, out Action injectAction)
        {
            var subContainer = _container.CreateSubContainer();

            SubContainerCreatorUtil.ApplyBindSettings(_containerBindInfo, subContainer);

            var extraArgs = UniDiPools.SpawnList <TypeValuePair>();

            extraArgs.AllocFreeAddRange(_extraArgs);
            extraArgs.AllocFreeAddRange(args);

            var installer = (InstallerBase)subContainer.InstantiateExplicit(
                _installerType, extraArgs);

            UniDiPools.DespawnList(extraArgs);

            installer.InstallBindings();

            injectAction = () =>
            {
                subContainer.ResolveRoots();
            };

            return(subContainer);
        }
Esempio n. 3
0
        public void GetAllInstancesWithInjectSplit(
            InjectContext context, List <TypeValuePair> args, out Action injectAction, List <object> buffer)
        {
            Assert.IsNotNull(context);

            var instanceType = GetTypeToCreate(context.MemberType);

            var extraArgs = UniDiPools.SpawnList <TypeValuePair>();

            extraArgs.AllocFreeAddRange(_extraArguments);
            extraArgs.AllocFreeAddRange(args);

            var instance = _container.InstantiateExplicit(instanceType, false, extraArgs, context, _concreteIdentifier);

            injectAction = () =>
            {
                _container.InjectExplicit(
                    instance, instanceType, extraArgs, context, _concreteIdentifier);

                Assert.That(extraArgs.Count == 0);
                UniDiPools.DespawnList(extraArgs);

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

            buffer.Add(instance);
        }
Esempio n. 4
0
        public void GetAllInstancesWithInjectSplit(
            InjectContext context, List <TypeValuePair> args, out Action injectAction, List <object> buffer)
        {
            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;

                var componentInstance = gameObj.GetComponent(_componentType);
                instance = componentInstance;

                // Use componentInstance so that it triggers unity's overloaded comparison operator
                // So if the component is there but missing then it returns null
                // (https://github.com/svermeulen/UniDi/issues/582)
                if (componentInstance != null)
                {
                    injectAction = null;
                    buffer.Add(instance);
                    return;
                }

                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

            injectAction = () =>
            {
                var extraArgs = UniDiPools.SpawnList <TypeValuePair>();

                extraArgs.AllocFreeAddRange(_extraArguments);
                extraArgs.AllocFreeAddRange(args);

                _container.InjectExplicit(instance, _componentType, extraArgs, context, _concreteIdentifier);

                Assert.That(extraArgs.IsEmpty());
                UniDiPools.DespawnList(extraArgs);

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

            buffer.Add(instance);
        }
Esempio n. 5
0
        protected override void AddInstallers(List <TypeValuePair> args, GameObjectContext context)
        {
            context.AddNormalInstaller(
                new ActionInstaller(subContainer =>
            {
                var extraArgs = UniDiPools.SpawnList <TypeValuePair>();

                extraArgs.AllocFreeAddRange(_extraArgs);
                extraArgs.AllocFreeAddRange(args);

                var installer = (InstallerBase)subContainer.InstantiateExplicit(
                    _installerType, extraArgs);

                UniDiPools.DespawnList(extraArgs);

                installer.InstallBindings();
            }));
        }
Esempio n. 6
0
        // Note that if multiple contract types are provided per concrete type,
        // it will re-use the same provider for each contract type
        // (each concrete type will have its own provider though)
        protected void RegisterProvidersForAllContractsPerConcreteType(
            DiContainer container,
            List <Type> concreteTypes,
            Func <DiContainer, Type, IProvider> providerFunc)
        {
            Assert.That(!BindInfo.ContractTypes.IsEmpty());
            Assert.That(!concreteTypes.IsEmpty());

            var providerMap = UniDiPools.SpawnDictionary <Type, IProvider>();

            try
            {
                foreach (var concreteType in concreteTypes)
                {
                    var provider = providerFunc(container, concreteType);

                    providerMap[concreteType] = provider;

                    if (BindInfo.MarkAsUniqueSingleton)
                    {
                        container.SingletonMarkRegistry.MarkSingleton(concreteType);
                    }
                    else if (BindInfo.MarkAsCreationBinding)
                    {
                        container.SingletonMarkRegistry.MarkNonSingleton(concreteType);
                    }
                }

                foreach (var contractType in BindInfo.ContractTypes)
                {
                    foreach (var concreteType in concreteTypes)
                    {
                        if (ValidateBindTypes(concreteType, contractType))
                        {
                            RegisterProvider(container, contractType, providerMap[concreteType]);
                        }
                    }
                }
            }
            finally
            {
                UniDiPools.DespawnDictionary(providerMap);
            }
        }
Esempio n. 7
0
        public static object GetInstance(
            this IProvider creator, InjectContext context, List <TypeValuePair> args)
        {
            var allInstances = UniDiPools.SpawnList <object>();

            try
            {
                creator.GetAllInstances(context, args, allInstances);

                Assert.That(allInstances.Count > 0,
                            "Provider returned zero instances when one was expected when looking up type '{0}'", context.MemberType);

                Assert.That(allInstances.Count == 1,
                            "Provider returned multiple instances when only one was expected when looking up type '{0}'", context.MemberType);

                return(allInstances[0]);
            }
            finally
            {
                UniDiPools.DespawnList(allInstances);
            }
        }
        public void GetAllInstancesWithInjectSplit(
            InjectContext context, List <TypeValuePair> args, out Action injectAction, List <object> buffer)
        {
            Assert.IsNotNull(context);

            if (_createNew)
            {
                buffer.Add(UnityEngine.ScriptableObject.Instantiate(_resource));
            }
            else
            {
                buffer.Add(_resource);
            }

            injectAction = () =>
            {
                for (int i = 0; i < buffer.Count; i++)
                {
                    var obj = buffer[i];

                    var extraArgs = UniDiPools.SpawnList <TypeValuePair>();

                    extraArgs.AllocFreeAddRange(_extraArguments);
                    extraArgs.AllocFreeAddRange(args);

                    _container.InjectExplicit(
                        obj, _resourceType, extraArgs, context, _concreteIdentifier);

                    UniDiPools.DespawnList(extraArgs);

                    if (_instantiateCallback != null)
                    {
                        _instantiateCallback(context, obj);
                    }
                }
            };
        }
Esempio n. 9
0
        public static object TryGetInstance(
            this IProvider creator, InjectContext context, List <TypeValuePair> args)
        {
            var allInstances = UniDiPools.SpawnList <object>();

            try
            {
                creator.GetAllInstances(context, args, allInstances);

                if (allInstances.Count == 0)
                {
                    return(null);
                }

                Assert.That(allInstances.Count == 1,
                            "Provider returned multiple instances when one or zero was expected");

                return(allInstances[0]);
            }
            finally
            {
                UniDiPools.DespawnList(allInstances);
            }
        }
        public void GetAllInstancesWithInjectSplit(
            InjectContext context, List <TypeValuePair> args, out Action injectAction, List <object> buffer)
        {
            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 extraArgs = UniDiPools.SpawnList <TypeValuePair>();

                    extraArgs.AllocFreeAddRange(_extraArguments);
                    extraArgs.AllocFreeAddRange(args);

                    _container.InjectExplicit(instance, _componentType, extraArgs, context, _concreteIdentifier);

                    Assert.That(extraArgs.Count == 0);

                    UniDiPools.DespawnList(extraArgs);

                    if (_instantiateCallback != null)
                    {
                        _instantiateCallback(context, instance);
                    }
                }
                finally
                {
                    if (wasActive && ShouldToggleActive)
                    {
                        gameObj.SetActive(true);
                    }
                }
            };

            buffer.Add(instance);
        }
Esempio n. 11
0
        public GameObject Instantiate(InjectContext context, List <TypeValuePair> args, out Action injectAction)
        {
            Assert.That(_argumentTarget == null || _argumentTarget.DerivesFromOrEqual(context.MemberType));

            bool shouldMakeActive;
            var  gameObject = _container.CreateAndParentPrefab(
                GetPrefab(context), _gameObjectBindInfo, context, out shouldMakeActive);

            Assert.IsNotNull(gameObject);

            injectAction = () =>
            {
                var allArgs = UniDiPools.SpawnList <TypeValuePair>();

                allArgs.AllocFreeAddRange(_extraArguments);
                allArgs.AllocFreeAddRange(args);

                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
                {
                    _container.InjectGameObjectForComponentExplicit(
                        gameObject, _argumentTarget, allArgs, context, null);

                    Assert.That(allArgs.Count == 0);
                }

                UniDiPools.DespawnList <TypeValuePair>(allArgs);

                if (shouldMakeActive && !_container.IsValidating)
                {
#if UNIDI_INTERNAL_PROFILING
                    using (ProfileTimers.CreateTimedBlock("User Code"))
#endif
                    {
                        gameObject.SetActive(true);
                    }
                }

                if (_instantiateCallback != null)
                {
                    var callbackObjects = UniDiPools.SpawnHashSet <object>();

                    foreach (var type in _instantiateCallbackTypes)
                    {
                        var obj = gameObject.GetComponentInChildren(type);

                        if (obj != null)
                        {
                            callbackObjects.Add(obj);
                        }
                    }

                    foreach (var obj in callbackObjects)
                    {
                        _instantiateCallback(context, obj);
                    }

                    UniDiPools.DespawnHashSet(callbackObjects);
                }
            };

            return(gameObject);
        }
Esempio n. 12
0
 public void Dispose()
 {
     UniDiPools.DespawnBindInfo(this);
 }
Esempio n. 13
0
 public void Dispose()
 {
     UniDiPools.DespawnStatement(this);
 }
Esempio n. 14
0
 public void Dispose()
 {
     UniDiPools.DespawnInjectContext(this);
 }