public override object GetInstance(InjectContext context) { var obj = context.Container.InstantiateExplicit( GetTypeToInstantiate(context.MemberType), new List<TypeValuePair>(), context, null, true); Assert.That(obj != null); return obj; }
public InjectContext CreateInjectContext( DiContainer container, InjectContext currentContext, object targetInstance, string concreteIdentifier) { return new InjectContext( container, MemberType, Identifier, Optional, ObjectType, targetInstance, MemberName, currentContext, concreteIdentifier); }
public IEnumerable<ZenjectResolveException> ValidateBinding( Type componentType, InjectContext context) { if (!ContainsComponent(componentType)) { yield return new ZenjectResolveException( "Could not find component of type '{0}' in prefab with name '{1}' \nObject graph:\n{2}" .Fmt(componentType.Name(), _id.Prefab.name, context.GetObjectGraphString())); yield break; } // In most cases componentType will be a MonoBehaviour but we also want to allow interfaces // And in that case we can't validate it if (!componentType.IsAbstract()) { // Note that we always want to cache _container instead of using context.Container // since for singletons, the container they are accessed from should not determine // the container they are instantiated with // Transients can do that but not singletons foreach (var err in _container.ValidateObjectGraph(componentType, context)) { yield return err; } } }
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); }
public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(InjectContext context, List<TypeValuePair> args) { Assert.IsNotNull(context); if (_instances != null) { yield return _instances; yield break; } // This should only happen with constructor injection // Field or property injection should allow circular dependencies Assert.That(!_isCreatingInstance, "Found circular dependency when creating type '{0}'", _creator.GetInstanceType(context)); _isCreatingInstance = true; var runner = _creator.GetAllInstancesWithInjectSplit(context, args); // First get instance bool hasMore = runner.MoveNext(); _instances = runner.Current; Assert.IsNotNull(_instances); _isCreatingInstance = false; yield return _instances; // Now do injection while (hasMore) { hasMore = runner.MoveNext(); } }
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 override object GetInstance(InjectContext context) { Assert.That(_componentType.DerivesFromOrEqual(context.MemberType)); return context.Container.InstantiateComponentOnNewGameObjectExplicit( _componentType, _componentType.Name(), new List<TypeValuePair>(), context); }
public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(InjectContext context, List<TypeValuePair> args) { Assert.IsEmpty(args); Assert.IsNotNull(context); if (_container.IsValidating) { yield return new List<object>() { new ValidationMarker(context.MemberType) }; } else { var result = _method(context); if (result == null) { Assert.That(context.MemberType.IsPrimitive, "Invalid value returned from FromMethod. Expected non-null."); } else { Assert.That(result.GetType().DerivesFromOrEqual(context.MemberType)); } yield return new List<object>() { result }; } }
public IEnumerator<List<object>> GetAllInstancesWithInjectSplit( InjectContext context, List<TypeValuePair> args) { Assert.IsNotNull(context); var gameObjectRunner = _prefabInstantiator.Instantiate(args); // First get instance bool hasMore = gameObjectRunner.MoveNext(); var gameObject = gameObjectRunner.Current; var allComponents = gameObject.GetComponentsInChildren(_componentType); Assert.That(allComponents.Length >= 1, "Expected to find at least one component with type '{0}' on prefab '{1}'", _componentType, _prefabInstantiator.GetPrefab().name); yield return allComponents.Cast<object>().ToList(); // Now do injection while (hasMore) { hasMore = gameObjectRunner.MoveNext(); } }
public override object GetInstance(InjectContext context) { Assert.That(_componentType.DerivesFromOrEqual(context.MemberType)); if (_instance == null) { // This is valid sometimes //Assert.That(!_container.IsValidating, //"Tried to instantiate a MonoBehaviour with type '{0}' during validation. Object graph: {1}", _componentType, DiContainer.GetCurrentObjectGraph()); // Note that we always want to cache _container instead of using context.Container // since for singletons, the container they are accessed from should not determine // the container they are instantiated with // Transients can do that but not singletons var name = string.IsNullOrEmpty(_name) ? _componentType.Name() : _name; // We don't use the generic version here to avoid duplicate generic arguments to binder _instance = _container.InstantiateComponentOnNewGameObjectExplicit( _componentType, name, new List<TypeValuePair>(), context); Assert.IsNotNull(_instance); } return _instance; }
public object GetComponent(Type componentType, InjectContext context) { if (_rootObj == null) { Assert.IsNotNull(_id.ResourcePath); var prefab = (GameObject)Resources.Load(_id.ResourcePath); Assert.IsNotNull(prefab, "Could not find prefab at resource path '{0}'", _id.ResourcePath); _rootObj = (GameObject)GameObject.Instantiate(prefab); // Note that we always want to cache _container instead of using context.Container // since for singletons, the container they are accessed from should not determine // the container they are instantiated with // Transients can do that but not singletons _rootObj.transform.SetParent(_container.DefaultParent, false); _rootObj.SetActive(true); _container.InjectGameObject(_rootObj, true, false, new object[0], context); } var component = _rootObj.GetComponentInChildren(componentType); if (component == null) { throw new ZenjectResolveException( "Could not find component with type '{0}' in given singleton prefab".Fmt(componentType)); } return component; }
InjectContext GetSubContext(InjectContext parent) { var subContext = parent.CreateSubContext(_contractType, _identifier); subContext.Optional = _isOptional; return subContext; }
public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(InjectContext context, List<TypeValuePair> args) { Assert.IsEmpty(args); Assert.IsNotNull(context); Assert.That(_instanceType.DerivesFromOrEqual(context.MemberType)); yield return new List<object>() { _instance }; }
public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(InjectContext context, List<TypeValuePair> args) { Assert.IsEmpty(args); Assert.IsNotNull(context); Assert.That(_contractType.DerivesFromOrEqual(context.MemberType)); yield return _container.ResolveAll(GetSubContext(context)).Cast<object>().ToList(); }
public override object GetInstance(InjectContext context) { var obj = Resources.Load(_resourcePath, _concreteType); Assert.IsNotNull(obj, "Could not find resource at path '{0}' with type '{1}'", _resourcePath, _concreteType); return obj; }
public override object GetInstance(InjectContext context) { Assert.That(_componentType.DerivesFromOrEqual(context.MemberType)); var name = string.IsNullOrEmpty(_name) ? _componentType.Name() : _name; return context.Container.InstantiateComponentOnNewGameObjectExplicit( _componentType, name, new List<TypeValuePair>(), context); }
public object GetInstance(InjectContext context) { if (_instance == null) { InitInstance(context); Assert.IsNotNull(_instance); } return _instance; }
public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(InjectContext context, List<TypeValuePair> args) { Assert.IsNotNull(context); var subContainer = _subContainerCreator.CreateSubContainer(args); var subContext = CreateSubContext(context, subContainer); yield return subContainer.ResolveAll(subContext).Cast<object>().ToList(); }
public IEnumerator<List<object>> GetAllInstancesWithInjectSplit( InjectContext context, List<TypeValuePair> args) { Assert.IsEmpty(args); yield return new List<object>() { _container.CreateEmptyGameObject(_gameObjectName, _groupName) }; }
public override object GetInstance(InjectContext context) { Assert.That(_returnType.DerivesFromOrEqual(context.MemberType)); var obj = _method(context); Assert.That(obj != null, () => "Method provider returned null when looking up type '{0}'. \nObject graph:\n{1}".Fmt(_returnType.Name(), context.GetObjectGraphString())); return obj; }
public IEnumerable<ZenjectResolveException> ValidateBinding(InjectContext context) { // Note that we always want to cache _container instead of using context.Container // since for singletons, the container they are accessed from should not determine // the container they are instantiated with // Transients can do that but not singletons foreach (var err in _container.ValidateObjectGraph(_id.ConcreteType, context)) { yield return err; } }
public override IEnumerable<ZenjectResolveException> ValidateBinding(InjectContext context) { // Can't validate custom methods so assume they work if (_creator.HasInstance() || _creator.HasCustomCreateMethod) { // This would be the case if given an instance at binding time with ToSingle(instance) return Enumerable.Empty<ZenjectResolveException>(); } return _container.ValidateObjectGraph(GetInstanceType(), context, _creator.Id.Identifier); }
public override object GetInstance(InjectContext context) { object instance; if (!_instances.TryGetValue(context.MemberType, out instance)) { instance = typeof(Mock).GetMethod("Of", new Type[] { context.MemberType }).Invoke(null, null); _instances.Add(context.MemberType, instance); } return instance; }
InjectContext CreateSubContext( InjectContext parent, DiContainer subContainer) { var subContext = parent.CreateSubContext(_dependencyType, _identifier); subContext.Container = subContainer; // This is important to avoid infinite loops subContext.SourceType = InjectSources.Local; return subContext; }
public static object GetInstance( this IProvider creator, InjectContext context, List<TypeValuePair> args) { var allInstances = creator.GetAllInstances(context, args); Assert.That(!allInstances.IsEmpty(), "Provider returned zero instances when one was expected"); Assert.That(allInstances.Count == 1, "Provider returned multiple instances when only one was expected"); return allInstances[0]; }
public InjectContext( DiContainer container, Type memberType, string identifier, bool optional, Type objectType, object objectInstance, string memberName, InjectContext parentContext) { ObjectType = objectType; ObjectInstance = objectInstance; Identifier = identifier; MemberName = memberName; MemberType = memberType; Optional = optional; Container = container; BindingId = new BindingId(memberType, identifier); ParentContext = parentContext; }
public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(InjectContext context, List<TypeValuePair> args) { Assert.IsEmpty(args); Assert.IsNotNull(context); var obj = Resources.Load(_resourcePath, _resourceType); Assert.IsNotNull(obj, "Could not find resource at path '{0}' with type '{1}'", _resourcePath, _resourceType); yield return new List<object>() { obj }; // Are there any resource types which can be injected? }
public override IEnumerable<ZenjectResolveException> ValidateBinding(InjectContext context) { if (!_creator.ContainsComponent(_concreteType)) { yield return new ZenjectResolveException( "Could not find component of type '{0}' in prefab with name '{1}' \nObject graph:\n{2}" .Fmt(_concreteType.Name(), _creator.Prefab.name, context.GetObjectGraphString())); yield break; } foreach (var err in BindingValidator.ValidateObjectGraph(_container, _concreteType, context, null)) { yield return err; } }
public override object GetInstance(InjectContext context) { Assert.That(_componentType.DerivesFromOrEqual(context.MemberType)); if (_instance == null) { Assert.That(!_container.AllowNullBindings, "Tried to instantiate a MonoBehaviour with type '{0}' during validation. Object graph: {1}", _componentType, context.GetObjectGraphString()); _instance = _gameObject.AddComponent(_componentType); Assert.That(_instance != null); _container.Inject(_instance); } return _instance; }
void InitInstance(InjectContext context) { var concreteType = GetTypeToInstantiate(context.MemberType); bool autoInject = false; _instance = _container.InstantiateExplicit( concreteType, new List<TypeValuePair>(), context, _id.ConcreteIdentifier, autoInject); Assert.IsNotNull(_instance); // Inject after we've instantiated and set _instance so that we can support circular dependencies // as PostInject or field parameters _container.InjectExplicit( _instance, Enumerable.Empty<TypeValuePair>(), true, TypeAnalyzer.GetInfo(_instance.GetType()), context, _id.ConcreteIdentifier); }
public Type GetInstanceType(InjectContext context) { return(_componentType); }
public abstract IEnumerator <List <object> > GetAllInstancesWithInjectSplit( InjectContext context, List <TypeValuePair> args);
public Lazy(DiContainer container, InjectContext context) { _container = container; _context = context; }
static void OnDespawned(InjectContext that) { that.SetDefaults(); }
protected abstract GameObject GetGameObject(InjectContext context);
public Type GetInstanceType(InjectContext context) { return(_dependencyType); }
public Type GetInstanceType(InjectContext context) { return(typeof(TContract)); }
public override object GetInstance(InjectContext context) { Assert.That(typeof(T).DerivesFromOrEqual(context.MemberType)); return(Instantiator.Instantiate <T>(_template)); }
public static IEnumerable <ZenjectResolveException> ValidateContract(DiContainer container, InjectContext context) { var matches = container.GetProviderMatches(context); if (matches.Count == 1) { foreach (var error in matches.Single().ValidateBinding(context)) { yield return(error); } } else { if (ReflectionUtil.IsGenericList(context.MemberType)) { var subContext = context.ChangeMemberType(context.MemberType.GetGenericArguments().Single()); matches = container.GetProviderMatches(subContext); if (matches.IsEmpty()) { if (!context.Optional) { if (container.FallbackProvider != null) { foreach (var error in container.FallbackProvider.ValidateBinding(context)) { yield return(error); } } else { yield return(new ZenjectResolveException( "Could not find dependency with type 'List<{0}>'{1}. If the empty list is also valid, you can allow this by using the [InjectOptional] attribute.' \nObject graph:\n{2}" .Fmt( subContext.MemberType.Name(), (context.ObjectType == null ? "" : " when injecting into '{0}'".Fmt(context.ObjectType.Name())), context.GetObjectGraphString()))); } } } else { foreach (var match in matches) { foreach (var error in match.ValidateBinding(context)) { yield return(error); } } } } else { if (!context.Optional) { if (matches.IsEmpty()) { if (container.FallbackProvider != null) { foreach (var error in container.FallbackProvider.ValidateBinding(context)) { yield return(error); } } else { yield return(new ZenjectResolveException( "Could not find required dependency with type '{0}'{1} \nObject graph:\n{2}" .Fmt( context.MemberType.Name(), (context.ObjectType == null ? "" : " when injecting into '{0}'".Fmt(context.ObjectType.Name())), context.GetObjectGraphString()))); } } else { yield return(new ZenjectResolveException( "Found multiple matches when only one was expected for dependency with type '{0}'{1} \nObject graph:\n{2}" .Fmt( context.MemberType.Name(), (context.ObjectType == null ? "" : " when injecting into '{0}'".Fmt(context.ObjectType.Name())), context.GetObjectGraphString()))); } } } } }
public Type GetInstanceType(InjectContext context) { return(context.MemberType); }
protected override GameObject GetGameObject(InjectContext context) { return(_gameObject); }
public Type GetInstanceType(InjectContext context) { return(_concreteType); }
Foo GetFoo(InjectContext ctx) { return(new Foo()); }
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 = ZenPools.SpawnList <TypeValuePair>(); extraArgs.AllocFreeAddRange(_extraArguments); extraArgs.AllocFreeAddRange(args); _container.InjectExplicit(instance, _componentType, extraArgs, context, _concreteIdentifier); Assert.That(extraArgs.Count == 0); ZenPools.DespawnList <TypeValuePair>(extraArgs); if (_instantiateCallback != null) { _instantiateCallback(context, instance); } } finally { if (wasActive && ShouldToggleActive) { gameObj.SetActive(true); } } }; buffer.Add(instance); }
public override IEnumerable <ZenjectResolveException> ValidateBinding(InjectContext context) { return(BindingValidator.ValidateObjectGraph(_container, typeof(T))); }
public abstract List <object> GetAllInstancesWithInjectSplit( InjectContext context, List <TypeValuePair> args, out Action injectAction);
public override IEnumerable <ZenjectResolveException> ValidateBinding(InjectContext context) { return(BindingValidator.ValidateObjectGraph(_container, _componentType, context, null)); }
public override IEnumerable <ZenjectResolveException> ValidateBinding(Type contractType, InjectContext context) { return(Enumerable.Empty <ZenjectResolveException>()); }
public override IEnumerable <ZenjectResolveException> ValidateBinding(InjectContext context) { // Can't do much here if it's a method yield break; }
public Type GetInstanceType(InjectContext context) { return(_creator.GetInstanceType(context)); }
public IEnumerable <ZenjectResolveException> ValidateBinding(InjectContext context) { return(_container.ValidateObjectGraph(ComponentType, context)); }
public Type GetInstanceType(InjectContext context) { return(_resourceType); }
public Type GetInstanceType(InjectContext context) { return(typeof(GameObject)); }
public override IEnumerable <ZenjectResolveException> ValidateBinding(InjectContext context) { return(_lazyCreator.ValidateBinding(_componentType, context)); }
public override object GetInstance(InjectContext context) { Assert.That(_concreteType.DerivesFromOrEqual(context.MemberType)); return(_container.InstantiatePrefabResourceForComponent(_concreteType, _resourcePath)); }
public override object GetInstance(InjectContext context) { return(_lazyCreator.GetComponent(_componentType, context)); }
public override IEnumerable <ZenjectResolveException> ValidateBinding(InjectContext context) { return(_creator.ValidateBinding(context)); }
public override object GetInstance(InjectContext context) { return(_creator.GetInstance(context)); }
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(), _gameObjectBindInfo, context, out shouldMakeActive); Assert.IsNotNull(gameObject); injectAction = () => { var allArgs = ZenPools.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"); } Component targetComponent = null; if (_argumentTarget == null || allArgs.IsEmpty()) { _container.InjectGameObject(gameObject); } else { targetComponent = _container.InjectGameObjectForComponentExplicit( gameObject, _argumentTarget, allArgs, context, null); Assert.That(allArgs.Count == 0); } ZenPools.DespawnList <TypeValuePair>(allArgs); if (shouldMakeActive && !_container.IsValidating) { #if ZEN_INTERNAL_PROFILING using (ProfileTimers.CreateTimedBlock("User Code")) #endif { gameObject.SetActive(true); } } if (_instantiateCallback != null) { var callbackObjects = ZenPools.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); } ZenPools.DespawnHashSet(callbackObjects); } }; return(gameObject); }
public Type GetInstanceType(InjectContext context) { return(typeof(TValue)); }