Пример #1
0
        protected void InstallSceneBindings(InitialComponentsInjecter componentInjecter)
        {
            foreach (var binding in GetInjectableComponents().OfType <ZenjectBinding>())
            {
                if (binding == null)
                {
                    continue;
                }

                if (binding.Context == null)
                {
                    componentInjecter.InstallBinding(binding);
                }
            }

            // We'd prefer to use GameObject.FindObjectsOfType<ZenjectBinding>() here
            // instead but that doesn't find inactive gameobjects
            foreach (var binding in Resources.FindObjectsOfTypeAll <ZenjectBinding>())
            {
                if (binding == null)
                {
                    continue;
                }

                if (binding.Context == this)
                {
                    componentInjecter.InstallBinding(binding);
                }
            }
        }
Пример #2
0
        void InstallBindings(
            InstallerExtraArgs installerExtraArgs, InitialComponentsInjecter componentInjecter)
        {
            _container.DefaultParent = this.transform;

            _container.Bind <Context>().FromInstance(this);

            if (_kernel == null)
            {
                _container.Bind <MonoKernel>()
                .To <DefaultGameObjectKernel>().FromComponent(this.gameObject).AsSingle().NonLazy();
            }
            else
            {
                _container.Bind <MonoKernel>().FromInstance(_kernel).AsSingle().NonLazy();
            }

            InstallSceneBindings(componentInjecter);

            var extraArgsMap = new Dictionary <Type, List <TypeValuePair> >();

            if (installerExtraArgs != null)
            {
                extraArgsMap.Add(
                    installerExtraArgs.InstallerType, installerExtraArgs.ExtraArgs);
            }

            InstallInstallers(extraArgsMap);
        }
Пример #3
0
        public void RunInternal()
        {
            Assert.That(!_hasInitialized);
            _hasInitialized = true;

            Assert.IsNull(_container);

            var parentContainer = ParentContainer ?? ProjectContext.Instance.Container;

            // ParentContainer is optionally set temporarily before calling ZenUtil.LoadScene
            ParentContainer = null;

            _container = parentContainer.CreateSubContainer(IsValidating);

#if !UNITY_EDITOR
            Assert.That(!IsValidating);
#endif

            // This can happen if you run a decorated scene with immediately running a normal scene afterwards
            foreach (var decoratedScene in DecoratedScenes)
            {
                Assert.That(decoratedScene.isLoaded,
                            "Unexpected state in SceneContext - found unloaded decorated scene");
            }

            // Record all the injectable components in the scene BEFORE installing the installers
            // This is nice for cases where the user calls InstantiatePrefab<>, etc. in their installer
            // so that it doesn't inject on the game object twice
            // InitialComponentsInjecter will also guarantee that any component that is injected into
            // another component has itself been injected
            var componentInjecter = new InitialComponentsInjecter(
                _container, GetInjectableComponents().ToList());

            Log.Debug("SceneContext: Running installers...");

            _container.IsInstalling = true;

            try
            {
                InstallBindings(componentInjecter);
            }
            finally
            {
                _container.IsInstalling = false;
            }

            Log.Debug("SceneContext: Injecting components in the scene...");

            componentInjecter.LazyInjectComponents();

            Log.Debug("SceneContext: Resolving dependency roots...");

            Assert.That(_dependencyRoots.IsEmpty());
            _dependencyRoots.AddRange(_container.ResolveDependencyRoots());

            DecoratedScenes.Clear();

            Log.Debug("SceneContext: Initialized successfully");
        }
Пример #4
0
        void InstallBindings(InitialComponentsInjecter componentInjecter)
        {
            _container.DefaultParent = this.transform;

            _container.Bind(typeof(TickableManager), typeof(InitializableManager), typeof(DisposableManager))
            .ToSelf().AsSingle().InheritInSubContainers();

            _container.Bind <Context>().FromInstance(this);

            _container.Bind <ProjectKernel>().FromComponent(this.gameObject).AsSingle().NonLazy();

            InstallSceneBindings(componentInjecter);

            InstallInstallers();
        }
Пример #5
0
        void Initialize()
        {
            Log.Debug("Initializing ProjectContext");

            Assert.IsNull(_container);

            DontDestroyOnLoad(gameObject);

            bool isValidating = false;

#if UNITY_EDITOR
            isValidating = PersistentIsValidating;

            if (isValidating)
            {
                ValidationSceneDisabler.Instance.DisableEverything();
            }

            // Always default to false to avoid validating next time play is hit
            PersistentIsValidating = false;
#endif

            _container = new DiContainer(
                StaticContext.Container, isValidating);

            var componentInjecter = new InitialComponentsInjecter(
                _container, GetInjectableComponents().ToList());

            _container.IsInstalling = true;

            try
            {
                InstallBindings(componentInjecter);
            }
            finally
            {
                _container.IsInstalling = false;
            }

            componentInjecter.LazyInjectComponents();

            Assert.That(_dependencyRoots.IsEmpty());

            _dependencyRoots.AddRange(_container.ResolveDependencyRoots());
        }
Пример #6
0
        public void Construct(
            DiContainer parentContainer,
            [InjectOptional]
            InstallerExtraArgs installerExtraArgs)
        {
            Assert.IsNull(_container);

            _container = parentContainer.CreateSubContainer();

            var componentInjecter = new InitialComponentsInjecter(
                _container, GetInjectableComponents().ToList());

            foreach (var component in componentInjecter.Components)
            {
                if (component is MonoKernel)
                {
                    Assert.That(component == _kernel,
                                "Found MonoKernel derived class that is not hooked up to GameObjectContext.  If you use MonoKernel, you must indicate this to GameObjectContext by dragging and dropping it to the Kernel field in the inspector");
                }
            }

            _container.IsInstalling = true;

            try
            {
                InstallBindings(installerExtraArgs, componentInjecter);
            }
            finally
            {
                _container.IsInstalling = false;
            }

            Log.Debug("GameObjectContext: Injecting into child components...");

            componentInjecter.LazyInjectComponents();

            Assert.That(_dependencyRoots.IsEmpty());
            _dependencyRoots.AddRange(_container.ResolveDependencyRoots());

            Log.Debug("GameObjectContext: Initialized successfully");
        }
Пример #7
0
        public void Construct(
            DiContainer parentContainer,
            [InjectOptional]
            InstallerExtraArgs installerExtraArgs)
        {
            Assert.IsNull(_container);

            _container = parentContainer.CreateSubContainer();

            var componentInjecter = new InitialComponentsInjecter(
                _container, GetInjectableComponents().ToList());

            foreach (var component in componentInjecter.Components)
            {
                if (component is MonoKernel)
                {
                    Assert.That(component == _kernel,
                        "Found MonoKernel derived class that is not hooked up to GameObjectContext.  If you use MonoKernel, you must indicate this to GameObjectContext by dragging and dropping it to the Kernel field in the inspector");
                }
            }

            _container.IsInstalling = true;

            try
            {
                InstallBindings(installerExtraArgs, componentInjecter);
            }
            finally
            {
                _container.IsInstalling = false;
            }

            Log.Debug("GameObjectContext: Injecting into child components...");

            componentInjecter.LazyInjectComponents();

            Assert.That(_dependencyRoots.IsEmpty());
            _dependencyRoots.AddRange(_container.ResolveDependencyRoots());

            Log.Debug("GameObjectContext: Initialized successfully");
        }
Пример #8
0
        void InstallBindings(InitialComponentsInjecter componentInjecter)
        {
            if (_parentNewObjectsUnderRoot)
            {
                _container.DefaultParent = this.transform;
            }
            else
            {
                // This is necessary otherwise we inherit the project root DefaultParent
                _container.DefaultParent = null;
            }

            _container.Bind <Context>().FromInstance(this);
            _container.Bind <SceneContext>().FromInstance(this);

            InstallSceneBindings(componentInjecter);

            if (BeforeInstallHooks != null)
            {
                BeforeInstallHooks(_container);
                // Reset extra bindings for next time we change scenes
                BeforeInstallHooks = null;
            }

            _container.Bind <SceneKernel>().FromComponent(this.gameObject).AsSingle().NonLazy();

            _container.Bind <ZenjectSceneLoader>().AsSingle();

            InstallInstallers();

            if (AfterInstallHooks != null)
            {
                AfterInstallHooks(_container);
                // Reset extra bindings for next time we change scenes
                AfterInstallHooks = null;
            }
        }
Пример #9
0
        void InstallBindings(InitialComponentsInjecter componentInjecter)
        {
            _container.DefaultParent = this.transform;

            _container.Bind(typeof(TickableManager), typeof(InitializableManager), typeof(DisposableManager))
                .ToSelf().AsSingle().InheritInSubContainers();

            _container.Bind<Context>().FromInstance(this);

            _container.Bind<ProjectKernel>().FromComponent(this.gameObject).AsSingle().NonLazy();

            InstallSceneBindings(componentInjecter);

            InstallInstallers();
        }
Пример #10
0
        void Initialize()
        {
            Log.Debug("Initializing ProjectContext");

            Assert.IsNull(_container);

            DontDestroyOnLoad(gameObject);

            bool isValidating = false;

#if UNITY_EDITOR
            isValidating = PersistentIsValidating;

            if (isValidating)
            {
                ValidationSceneDisabler.Instance.DisableEverything();
            }

            // Always default to false to avoid validating next time play is hit
            PersistentIsValidating = false;
#endif

            _container = new DiContainer(
                StaticContext.Container, isValidating);

            var componentInjecter = new InitialComponentsInjecter(
                _container, GetInjectableComponents().ToList());

            _container.IsInstalling = true;

            try
            {
                InstallBindings(componentInjecter);
            }
            finally
            {
                _container.IsInstalling = false;
            }

            componentInjecter.LazyInjectComponents();

            Assert.That(_dependencyRoots.IsEmpty());

            _dependencyRoots.AddRange(_container.ResolveDependencyRoots());
        }
Пример #11
0
        void InstallBindings(InitialComponentsInjecter componentInjecter)
        {
            if (_parentNewObjectsUnderRoot)
            {
                _container.DefaultParent = this.transform;
            }
            else
            {
                // This is necessary otherwise we inherit the project root DefaultParent
                _container.DefaultParent = null;
            }

            _container.Bind<Context>().FromInstance(this);
            _container.Bind<SceneContext>().FromInstance(this);

            InstallSceneBindings(componentInjecter);

            if (BeforeInstallHooks != null)
            {
                BeforeInstallHooks(_container);
                // Reset extra bindings for next time we change scenes
                BeforeInstallHooks = null;
            }

            _container.Bind<SceneKernel>().FromComponent(this.gameObject).AsSingle().NonLazy();

            _container.Bind<ZenjectSceneLoader>().AsSingle();

            InstallInstallers();

            if (AfterInstallHooks != null)
            {
                AfterInstallHooks(_container);
                // Reset extra bindings for next time we change scenes
                AfterInstallHooks = null;
            }
        }
Пример #12
0
        public void RunInternal()
        {
            Assert.That(!_hasInitialized);
            _hasInitialized = true;

            Assert.IsNull(_container);

            var parentContainer = ParentContainer ?? ProjectContext.Instance.Container;

            // ParentContainer is optionally set temporarily before calling ZenUtil.LoadScene
            ParentContainer = null;

            _container = parentContainer.CreateSubContainer(IsValidating);

#if !UNITY_EDITOR
            Assert.That(!IsValidating);
#endif

            // This can happen if you run a decorated scene with immediately running a normal scene afterwards
            foreach (var decoratedScene in DecoratedScenes)
            {
                Assert.That(decoratedScene.isLoaded,
                    "Unexpected state in SceneContext - found unloaded decorated scene");
            }

            // Record all the injectable components in the scene BEFORE installing the installers
            // This is nice for cases where the user calls InstantiatePrefab<>, etc. in their installer
            // so that it doesn't inject on the game object twice
            // InitialComponentsInjecter will also guarantee that any component that is injected into
            // another component has itself been injected
            var componentInjecter = new InitialComponentsInjecter(
                _container, GetInjectableComponents().ToList());

            Log.Debug("SceneContext: Running installers...");

            _container.IsInstalling = true;

            try
            {
                InstallBindings(componentInjecter);
            }
            finally
            {
                _container.IsInstalling = false;
            }

            Log.Debug("SceneContext: Injecting components in the scene...");

            componentInjecter.LazyInjectComponents();

            Log.Debug("SceneContext: Resolving dependency roots...");

            Assert.That(_dependencyRoots.IsEmpty());
            _dependencyRoots.AddRange(_container.ResolveDependencyRoots());

            DecoratedScenes.Clear();

            Log.Debug("SceneContext: Initialized successfully");
        }
Пример #13
0
        protected void InstallSceneBindings(InitialComponentsInjecter componentInjecter)
        {
            foreach (var binding in GetInjectableComponents().OfType<ZenjectBinding>())
            {
                if (binding == null)
                {
                    continue;
                }

                if (binding.Context == null)
                {
                    componentInjecter.InstallBinding(binding);
                }
            }

            // We'd prefer to use GameObject.FindObjectsOfType<ZenjectBinding>() here
            // instead but that doesn't find inactive gameobjects
            foreach (var binding in Resources.FindObjectsOfTypeAll<ZenjectBinding>())
            {
                if (binding == null)
                {
                    continue;
                }

                if (binding.Context == this)
                {
                    componentInjecter.InstallBinding(binding);
                }
            }
        }
Пример #14
0
        void InstallBindings(
            InstallerExtraArgs installerExtraArgs, InitialComponentsInjecter componentInjecter)
        {
            _container.DefaultParent = this.transform;

            _container.Bind<Context>().FromInstance(this);

            if (_kernel == null)
            {
                _container.Bind<MonoKernel>()
                    .To<DefaultGameObjectKernel>().FromComponent(this.gameObject).AsSingle().NonLazy();
            }
            else
            {
                _container.Bind<MonoKernel>().FromInstance(_kernel).AsSingle().NonLazy();
            }

            InstallSceneBindings(componentInjecter);

            var extraArgsMap = new Dictionary<Type, List<TypeValuePair>>();

            if (installerExtraArgs != null)
            {
                extraArgsMap.Add(
                    installerExtraArgs.InstallerType, installerExtraArgs.ExtraArgs);
            }

            InstallInstallers();
        }