void InstallSceneInstallers(DiContainer container) { if (Installers.Where(x => x != null).IsEmpty()) { Log.Warn("No installers found while initializing CompositionRoot"); return; } foreach (var installer in Installers) { if (installer == null) { Log.Warn("Found null installer hooked up to CompositionRoot"); continue; } if (installer.enabled) { // The installers that are part of the scene are monobehaviours // and therefore were not created via Zenject and therefore do // not have their members injected // At the very least they will need the container injected but // they might also have some configuration passed from another // scene as well FieldsInjecter.Inject(container, installer); container.Bind <IInstaller>().To(installer); // Install this installer and also any other installers that it installs container.InstallInstallers(); Assert.That(!container.HasBinding <IInstaller>()); } } }
object InstantiateInternal( Type concreteType, params object[] constructorArgs) { Assert.That(!concreteType.DerivesFrom <UnityEngine.Component>(), "Error occurred while instantiating object of type '{0}'. Instantiator should not be used to create new mono behaviours. Must use GameObjectInstantiator, GameObjectFactory, or GameObject.Instantiate.", concreteType.Name()); var typeInfo = TypeAnalyzer.GetInfo(concreteType); if (typeInfo.InjectConstructor == null) { throw new ZenjectResolveException( "More than one or zero constructors found for type '{0}' when creating dependencies. Use one [Inject] attribute to specify which to use.".With(concreteType)); } var paramValues = new List <object>(); var extrasList = new List <object>(constructorArgs); Assert.That(!extrasList.Contains(null), "Null value given to factory constructor arguments. This is currently not allowed"); foreach (var injectInfo in typeInfo.ConstructorInjectables) { var found = false; foreach (var extra in extrasList) { if (extra.GetType().DerivesFromOrEqual(injectInfo.ContractType)) { found = true; paramValues.Add(extra); extrasList.Remove(extra); break; } } if (!found) { paramValues.Add(_container.Resolve(injectInfo)); } } object newObj; try { using (ProfileBlock.Start("{0}.{0}()".With(concreteType))) { newObj = typeInfo.InjectConstructor.Invoke(paramValues.ToArray()); } } catch (Exception e) { throw new ZenjectResolveException( "Error occurred while instantiating object with type '{0}'".With(concreteType.Name()), e); } FieldsInjecter.Inject(_container, newObj, extrasList, true, typeInfo); return(newObj); }
public static void InjectMonoBehaviour( DiContainer container, MonoBehaviour monoBehaviour, IEnumerable <object> extraArgs) { // null if monobehaviour link is broken if (monoBehaviour != null && monoBehaviour.enabled) { using (container.PushLookup(monoBehaviour.GetType())) { FieldsInjecter.Inject(container, monoBehaviour, extraArgs); } } }
public static void InjectMonoBehaviour( DiContainer container, Component component, IEnumerable <object> extraArgs) { // null if monobehaviour link is broken if (component != null) { using (container.PushLookup(component.GetType())) { FieldsInjecter.Inject(container, component, extraArgs); } } }
object InstantiateInternal( Type concreteType, params object[] constructorArgs) { ConstructorInfo method; var injectInfos = InjectablesFinder.GetConstructorInjectables(concreteType, out method); var paramValues = new List <object>(); var extrasList = new List <object>(constructorArgs); Assert.That(!extrasList.Contains(null), "Null value given to factory constructor arguments. This is currently not allowed"); foreach (var injectInfo in injectInfos) { var found = false; foreach (var extra in extrasList) { if (extra.GetType().DerivesFromOrEqual(injectInfo.ContractType)) { found = true; paramValues.Add(extra); extrasList.Remove(extra); break; } } if (!found) { paramValues.Add(_container.Resolve(injectInfo)); } } object newObj; try { newObj = method.Invoke(paramValues.ToArray()); } catch (Exception e) { throw new ZenjectResolveException( "Error occurred while instantiating object with type '{0}'".With(concreteType.Name()), e); } FieldsInjecter.Inject(_container, newObj, extrasList, true); return(newObj); }
object InstantiateInternal( Type concreteType, IEnumerable <TypeValuePair> extraArgMapParam) { Assert.That(!concreteType.DerivesFrom <UnityEngine.Component>(), "Error occurred while instantiating object of type '{0}'. Instantiator should not be used to create new mono behaviours. Must use GameObjectInstantiator, GameObjectFactory, or GameObject.Instantiate.", concreteType.Name()); var typeInfo = TypeAnalyzer.GetInfo(concreteType); if (typeInfo.InjectConstructor == null) { throw new ZenjectResolveException( "More than one or zero constructors found for type '{0}' when creating dependencies. Use one [Inject] attribute to specify which to use.".With(concreteType)); } // Make a copy since we remove from it below var extraArgMap = extraArgMapParam.ToList(); var paramValues = new List <object>(); foreach (var injectInfo in typeInfo.ConstructorInjectables) { object value; if (!InstantiateUtil.PopValueWithType(extraArgMap, injectInfo.ContractType, out value)) { value = _container.Resolve(injectInfo); } paramValues.Add(value); } object newObj; try { using (ProfileBlock.Start("{0}.{0}()", concreteType)) { newObj = typeInfo.InjectConstructor.Invoke(paramValues.ToArray()); } } catch (Exception e) { throw new ZenjectResolveException( "Error occurred while instantiating object with type '{0}'".With(concreteType.Name()), e); } FieldsInjecter.Inject(_container, newObj, extraArgMap, true, typeInfo); return(newObj); }
void Register() { if (Installers.IsEmpty()) { Debug.LogError("No installers found while initializing CompositionRoot"); return; } foreach (var installer in Installers) { // The installers that are part of the scene are monobehaviours // and therefore were not created via Zenject and therefore do // not have their members injected // At the very least they will need the container injected but // they might also have some configuration passed from another // scene FieldsInjecter.Inject(_container, installer); _container.Bind <IInstaller>().To(installer); } ZenUtil.InstallInstallers(_container); }
public static DiContainer CreateContainer(bool allowNullBindings, GameObject gameObj) { Assert.That(allowNullBindings || gameObj != null); var container = new DiContainer(); container.AllowNullBindings = allowNullBindings; container.Bind <IInstaller>().ToSingle <StandardUnityInstaller>(); container.Bind <GameObject>().To(gameObj) .WhenInjectedInto <StandardUnityInstaller>(); container.InstallInstallers(); Assert.That(!container.HasBinding <IInstaller>()); foreach (var installer in GetGlobalInstallers()) { FieldsInjecter.Inject(container, installer); container.Bind <IInstaller>().To(installer); container.InstallInstallers(); Assert.That(!container.HasBinding <IInstaller>()); } return(container); }