Exemplo n.º 1
0
        public ScopeArgConditionCopyNonLazyBinder FromComponentInChildren(
            bool excludeSelf = false, Func <TContract, bool> predicate = null, bool includeInactive = false)
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            return(FromMethodMultiple((ctx) => {
                Assert.That(ctx.ObjectType.DerivesFromOrEqual <MonoBehaviour>());
                Assert.IsNotNull(ctx.ObjectInstance);

                var res = ((MonoBehaviour)ctx.ObjectInstance).GetComponentsInChildren <TContract>(includeInactive)
                          .Where(x => !ReferenceEquals(x, ctx.ObjectInstance));

                if (excludeSelf)
                {
                    res = res.Where(x => (x as Component).gameObject != (ctx.ObjectInstance as Component).gameObject);
                }

                if (predicate != null)
                {
                    res = res.Where(predicate);
                }

                return res;
            }));
        }
Exemplo n.º 2
0
        public ScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentSibling()
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            BindInfo.RequireExplicitScope = false;

            // Don't know how it's created so can't assume here that it violates AsSingle
            BindInfo.MarkAsCreationBinding = false;

            SubFinalizer = new ScopableBindingFinalizer(
                BindInfo,
                (container, concreteType) => new MethodMultipleProviderUntyped(ctx =>
            {
                Assert.That(ctx.ObjectType.DerivesFromOrEqual <MonoBehaviour>(),
                            "Cannot use FromComponentSibling to inject data into non monobehaviours!");

                Assert.IsNotNull(ctx.ObjectInstance);

                var monoBehaviour = (MonoBehaviour)ctx.ObjectInstance;

                var match = monoBehaviour.GetComponent(concreteType);

                if (match == null)
                {
                    Assert.That(ctx.Optional,
                                "Could not find any component with type '{0}' through FromComponentSibling binding", concreteType);
                    return(Enumerable.Empty <object>());
                }

                return(new object[] { match });
            },
                                                                               container));

            return(this);
        }
Exemplo n.º 3
0
        protected ScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentsInHierarchyBase(
            Func <Component, bool> predicate = null, bool includeInactive = true)
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            BindInfo.RequireExplicitScope = true;

            // Don't know how it's created so can't assume here that it violates AsSingle
            BindInfo.MarkAsCreationBinding = false;

            SubFinalizer = new ScopableBindingFinalizer(
                BindInfo,
                (container, concreteType) => new MethodMultipleProviderUntyped(ctx =>
            {
                var res = container.Resolve <Context>().GetRootGameObjects()
                          .SelectMany(x => x.GetComponentsInChildren(concreteType, includeInactive))
                          .Where(x => !ReferenceEquals(x, ctx.ObjectInstance));

                if (predicate != null)
                {
                    res = res.Where(predicate);
                }

                return(res.Cast <object>());
            },
                                                                               container));

            return(this);
        }
Exemplo n.º 4
0
        public ScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentInHierarchy(
            bool includeInactive = true)
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            // Since this is a pretty heavy operation, let's require an explicit scope
            // Most of the time they should use AsCached or AsSingle
            BindInfo.RequireExplicitScope = true;

            // Don't know how it's created so can't assume here that it violates AsSingle
            BindInfo.MarkAsCreationBinding = false;

            SubFinalizer = new ScopableBindingFinalizer(
                BindInfo,
                (container, concreteType) => new MethodMultipleProviderUntyped(ctx =>
            {
                var match = container.Resolve <Context>().GetRootGameObjects()
                            .Select(x => x.GetComponentInChildren(concreteType, includeInactive))
                            .Where(x => x != null && !ReferenceEquals(x, ctx.ObjectInstance)).FirstOrDefault();

                if (match == null)
                {
                    Assert.That(ctx.Optional,
                                "Could not find any component with type '{0}' through FromComponentInHierarchy binding", concreteType);
                    return(Enumerable.Empty <object>());
                }

                return(new object[] { match });
            },
                                                                               container));

            return(this);
        }
Exemplo n.º 5
0
        public ScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentsSibling()
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            BindInfo.RequireExplicitScope = false;

            // Don't know how it's created so can't assume here that it violates AsSingle
            BindInfo.MarkAsCreationBinding = false;

            SubFinalizer = new ScopableBindingFinalizer(
                BindInfo,
                (container, concreteType) => new MethodMultipleProviderUntyped(ctx =>
            {
                Assert.That(ctx.ObjectType.DerivesFromOrEqual <MonoBehaviour>(),
                            "Cannot use FromComponentSibling to inject data into non monobehaviours!");

                Assert.IsNotNull(ctx.ObjectInstance);

                var monoBehaviour = (MonoBehaviour)ctx.ObjectInstance;

                return(monoBehaviour.GetComponents(concreteType)
                       .Where(x => !ReferenceEquals(x, monoBehaviour)).Cast <object>());
            },
                                                                               container));

            return(this);
        }
Exemplo n.º 6
0
        public ScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentInParents(
            bool excludeSelf = false, bool includeInactive = true)
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            // Use FromMethodMultiple so that we can return the empty list when context is optional
            return(FromMethodMultiple((ctx) =>
            {
                Assert.That(ctx.ObjectType.DerivesFromOrEqual <MonoBehaviour>(),
                            "Cannot use FromComponentInParents to inject data into non monobehaviours!");
                Assert.IsNotNull(ctx.ObjectInstance);

                var matches = ((MonoBehaviour)ctx.ObjectInstance).GetComponentsInParent <TContract>(includeInactive)
                              .Where(x => !ReferenceEquals(x, ctx.ObjectInstance));

                if (excludeSelf)
                {
                    matches = matches.Where(x => (x as Component).gameObject != (ctx.ObjectInstance as Component).gameObject);
                }

                var result = matches.FirstOrDefault();

                if (result == null)
                {
                    Assert.That(ctx.Optional, "Could not find component '{0}' through FromComponentInParents binding", typeof(TContract));
                    return Enumerable.Empty <TContract>();
                }

                return new TContract[] { result };
            }));
        }
Exemplo n.º 7
0
        public ScopeArgConditionCopyNonLazyBinder FromComponentInHierarchy()
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            return(FromMethodMultiple((ctx) =>
            {
                return ctx.Container.Resolve <Context>().GetRootGameObjects()
                .SelectMany(x => x.GetComponentsInChildren <TContract>())
                .Where(x => !ReferenceEquals(x, ctx.ObjectInstance));
            }));
        }
Exemplo n.º 8
0
        public NameTransformScopeArgConditionCopyNonLazyBinder FromComponentInNewPrefabResource(
            string resourcePath, GameObjectCreationParameters gameObjectInfo)
        {
            BindingUtil.AssertIsValidResourcePath(resourcePath);
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            BindInfo.RequireExplicitScope = true;
            SubFinalizer = new PrefabResourceBindingFinalizer(
                BindInfo, gameObjectInfo, resourcePath);

            return(new NameTransformScopeArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo));
        }
Exemplo n.º 9
0
        public NameTransformScopeArgConditionCopyNonLazyBinder FromComponentInNewPrefab(
            UnityEngine.Object prefab, GameObjectCreationParameters gameObjectInfo)
        {
            BindingUtil.AssertIsValidPrefab(prefab);
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            BindInfo.RequireExplicitScope = true;
            SubFinalizer = new PrefabBindingFinalizer(
                BindInfo, gameObjectInfo, prefab);

            return(new NameTransformScopeArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo));
        }
Exemplo n.º 10
0
        public ScopeArgConditionCopyNonLazyBinder FromComponentSibling()
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            return(FromMethodMultiple((ctx) =>
            {
                Assert.That(ctx.ObjectType.DerivesFromOrEqual <MonoBehaviour>());
                Assert.IsNotNull(ctx.ObjectInstance);

                return ((MonoBehaviour)ctx.ObjectInstance).GetComponents <TContract>()
                .Where(x => !ReferenceEquals(x, ctx.ObjectInstance));
            }));
        }
Exemplo n.º 11
0
        internal NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentInNewPrefab(
            UnityEngine.Object prefab, GameObjectCreationParameters gameObjectInfo)
        {
            BindingUtil.AssertIsValidPrefab(prefab);
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            BindInfo.RequireExplicitScope = true;
            SubFinalizer = new PrefabBindingFinalizer(
                BindInfo, gameObjectInfo, prefab,
                (contractType, instantiator) => new GetFromPrefabComponentProvider(contractType, instantiator, true));

            return(new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo));
        }
Exemplo n.º 12
0
        internal NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentsInNewPrefabResource(
            string resourcePath, GameObjectCreationParameters gameObjectInfo)
        {
            BindingUtil.AssertIsValidResourcePath(resourcePath);
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            BindInfo.RequireExplicitScope = true;
            SubFinalizer = new PrefabResourceBindingFinalizer(
                BindInfo, gameObjectInfo, resourcePath,
                (contractType, instantiator) => new GetFromPrefabComponentProvider(contractType, instantiator, false));

            return(new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo));
        }
Exemplo n.º 13
0
        public NameTransformConditionCopyNonLazyBinder FromComponentInNewPrefab(UnityEngine.Object prefab)
        {
            BindingUtil.AssertIsValidPrefab(prefab);
            BindingUtil.AssertIsInterfaceOrComponent(ContractType);

            var gameObjectInfo = new GameObjectCreationParameters();

            ProviderFunc =
                (container) => new GetFromPrefabComponentProvider(
                    ContractType,
                    new PrefabInstantiator(
                        container, gameObjectInfo,
                        ContractType, new List <TypeValuePair>(), new PrefabProvider(prefab)), true);

            return(new NameTransformConditionCopyNonLazyBinder(BindInfo, gameObjectInfo));
        }
Exemplo n.º 14
0
        public ScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentInHierarchy(
            bool includeInactive = true)
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            return(FromMethod((ctx) => {
                var res = BindContainer.Resolve <Context>().GetRootGameObjects()
                          .Select(x => x.GetComponentInChildren <TContract>(includeInactive))
                          .Where(x => x != null).FirstOrDefault();

                Assert.IsNotNull(res,
                                 "Could not find component '{0}' through FromComponentInHierarchy binding", typeof(TContract));

                return res;
            }));
        }
Exemplo n.º 15
0
        public ScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentSibling()
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            return(FromMethod((ctx) =>
            {
                Assert.That(ctx.ObjectType.DerivesFromOrEqual <MonoBehaviour>(),
                            "Cannot use FromComponentSibling to inject data into non monobehaviours!");
                Assert.IsNotNull(ctx.ObjectInstance);

                var match = ((MonoBehaviour)ctx.ObjectInstance).GetComponent <TContract>();
                Assert.IsNotNull(match,
                                 "Could not find component '{0}' through FromComponentSibling binding", typeof(TContract));
                return match;
            }));
        }
        public NameTransformConditionCopyNonLazyBinder FromComponentInNewPrefabResource(string resourcePath)
        {
            BindingUtil.AssertIsValidResourcePath(resourcePath);
            BindingUtil.AssertIsInterfaceOrComponent(ContractType);

            var gameObjectInfo = new GameObjectCreationParameters();

            ProviderFunc =
                (container) => new GetFromPrefabComponentProvider(
                    ContractType,
                    new PrefabInstantiator(
                        container, gameObjectInfo,
                        ContractType, new List <TypeValuePair>(), new PrefabProviderResource(resourcePath)));

            return(new NameTransformConditionCopyNonLazyBinder(BindInfo, gameObjectInfo));
        }
Exemplo n.º 17
0
        public ScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentInChildren(bool includeInactive = true)
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            return(FromMethod((ctx) => {
                Assert.That(ctx.ObjectType.DerivesFromOrEqual <MonoBehaviour>(),
                            "Cannot use FromComponentInChildren to inject data into non monobehaviours!");
                Assert.IsNotNull(ctx.ObjectInstance);

                var res = ((MonoBehaviour)ctx.ObjectInstance).GetComponentInChildren <TContract>(includeInactive);

                Assert.IsNotNull(res,
                                 "Could not find component '{0}' through FromComponentInChildren binding", typeof(TContract));

                return res;
            }));
        }
Exemplo n.º 18
0
        public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentInNewPrefabResource(string resourcePath)
        {
            BindingUtil.AssertIsValidResourcePath(resourcePath);
            BindingUtil.AssertIsInterfaceOrComponent(ContractType);

            var gameObjectInfo = new GameObjectCreationParameters();

            ProviderFunc =
                (container) => new GetFromPrefabComponentProvider(
                    ContractType,
                    new PrefabInstantiator(
                        container, gameObjectInfo,
                        ContractType, new [] { ContractType }, BindInfo.Arguments,
                        new PrefabProviderResource(resourcePath), BindInfo.InstantiatedCallback), true);

            return(new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo));
        }
Exemplo n.º 19
0
        public ScopeArgConditionCopyNonLazyBinder FromComponentInHierarchy(Func <TContract, bool> predicate = null, bool includeInactive = false)
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            return(FromMethodMultiple((ctx) => {
                var res = ctx.Container.Resolve <Context>().GetRootGameObjects()
                          .SelectMany(x => x.GetComponentsInChildren <TContract>(includeInactive))
                          .Where(x => !ReferenceEquals(x, ctx.ObjectInstance));

                if (predicate != null)
                {
                    res = res.Where(predicate);
                }

                return res;
            }));
        }
        public ConditionCopyNonLazyBinder FromComponentInHierarchy()
        {
            BindingUtil.AssertIsInterfaceOrComponent(ContractType);

            return(FromMethod((container) =>
            {
                var matches = container.Resolve <Context>().GetRootGameObjects()
                              .SelectMany(x => x.GetComponentsInChildren <TContract>()).ToList();

                Assert.That(!matches.IsEmpty(),
                            "Found zero matches when looking up type '{0}' using FromComponentInHierarchy for factory", ContractType);

                Assert.That(matches.Count() == 1,
                            "Found multiple matches when looking up type '{0}' using FromComponentInHierarchy for factory.  Only expected to find one!", ContractType);

                return matches.Single();
            }));
        }
Exemplo n.º 21
0
        public ScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentInHierarchy(
            bool includeInactive = true)
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            // Use FromMethodMultiple so that we can return the empty list when context is optional
            return(FromMethodMultiple((ctx) => {
                var res = BindContainer.Resolve <Context>().GetRootGameObjects()
                          .Select(x => x.GetComponentInChildren <TContract>(includeInactive))
                          .Where(x => x != null).FirstOrDefault();

                if (res == null)
                {
                    Assert.That(ctx.Optional, "Could not find component '{0}' through FromComponentInHierarchy binding", typeof(TContract));
                    return Enumerable.Empty <TContract>();
                }

                return new TContract[] { res };
            }));
        }
Exemplo n.º 22
0
        public ScopeArgConditionCopyNonLazyBinder FromComponentInParents(bool excludeSelf = false)
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            return(FromMethodMultiple((ctx) =>
            {
                var monoBehaviourContext = ctx.ToMonoBehaviourContext();
                Assert.IsNotNull(monoBehaviourContext);

                var res = monoBehaviourContext.GetComponentsInParent <TContract>()
                          .Where(x => !ReferenceEquals(x, ctx.ObjectInstance));

                if (excludeSelf)
                {
                    res = res.Where(x => (x as Component).gameObject != (ctx.ObjectInstance as Component).gameObject);
                }

                return res;
            }));
        }
Exemplo n.º 23
0
        public ScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentInChildren(bool includeInactive = true)
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            // Use FromMethodMultiple so that we can return the empty list when context is optional
            return(FromMethodMultiple((ctx) => {
                Assert.That(ctx.ObjectType.DerivesFromOrEqual <MonoBehaviour>(),
                            "Cannot use FromComponentInChildren to inject data into non monobehaviours!");
                Assert.IsNotNull(ctx.ObjectInstance);

                var res = ((MonoBehaviour)ctx.ObjectInstance).GetComponentInChildren <TContract>(includeInactive);

                if (res == null)
                {
                    Assert.That(ctx.Optional, "Could not find component '{0}' through FromComponentInChildren binding", typeof(TContract));
                    return Enumerable.Empty <TContract>();
                }

                return new TContract[] { res };
            }));
        }
Exemplo n.º 24
0
        protected ScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentsInChildrenBase(
            bool excludeSelf, Func <Component, bool> predicate, bool includeInactive)
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            BindInfo.RequireExplicitScope = false;

            // Don't know how it's created so can't assume here that it violates AsSingle
            BindInfo.MarkAsCreationBinding = false;

            SubFinalizer = new ScopableBindingFinalizer(
                BindInfo,
                (container, concreteType) => new MethodMultipleProviderUntyped(ctx =>
            {
                Assert.That(ctx.ObjectType.DerivesFromOrEqual <MonoBehaviour>(),
                            "Cannot use FromComponentsInChildren to inject data into non monobehaviours!");

                Assert.IsNotNull(ctx.ObjectInstance);

                var monoBehaviour = (MonoBehaviour)ctx.ObjectInstance;

                var res = monoBehaviour.GetComponentsInChildren(concreteType, includeInactive)
                          .Where(x => !ReferenceEquals(x, ctx.ObjectInstance));

                if (excludeSelf)
                {
                    res = res.Where(x => x.gameObject != monoBehaviour.gameObject);
                }

                if (predicate != null)
                {
                    res = res.Where(predicate);
                }

                return(res.Cast <object>());
            },
                                                                               container));

            return(this);
        }
Exemplo n.º 25
0
        public ScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentsInParents(
            bool excludeSelf = false, bool includeInactive = true)
        {
            BindingUtil.AssertIsInterfaceOrComponent(AllParentTypes);

            return(FromMethodMultiple((ctx) =>
            {
                Assert.That(ctx.ObjectType.DerivesFromOrEqual <MonoBehaviour>(),
                            "Cannot use FromComponentInParents to inject data into non monobehaviours!");
                Assert.IsNotNull(ctx.ObjectInstance);

                var res = ((MonoBehaviour)ctx.ObjectInstance).GetComponentsInParent <TContract>(includeInactive)
                          .Where(x => !ReferenceEquals(x, ctx.ObjectInstance));

                if (excludeSelf)
                {
                    res = res.Where(x => (x as Component).gameObject != (ctx.ObjectInstance as Component).gameObject);
                }

                return res;
            }));
        }