static List <ZenjectResolveException> ValidateDecoratorCompRoot(SceneDecoratorCompositionRoot decoratorCompRoot, int maxErrors)
        {
            var sceneName = decoratorCompRoot.SceneName;
            var scenePath = UnityEditorUtil.GetScenePath(sceneName);

            if (scenePath == null)
            {
                return(new List <ZenjectResolveException>()
                {
                    new ZenjectResolveException(
                        "Could not find scene path for decorated scene '{0}'".Fmt(sceneName)),
                });
            }

            var rootObjectsBefore = UnityUtil.GetRootGameObjects();

            OpenSceneAdditive(scenePath);

            var newRootObjects = UnityUtil.GetRootGameObjects().Except(rootObjectsBefore);

            // Use finally to ensure we clean Up the data added from OpenSceneAdditive
            try
            {
                var previousBeforeInstallHook = SceneCompositionRoot.BeforeInstallHooks;
                SceneCompositionRoot.BeforeInstallHooks = (container) =>
                {
                    if (previousBeforeInstallHook != null)
                    {
                        previousBeforeInstallHook(container);
                    }

                    decoratorCompRoot.AddPreBindings(container);
                };

                var previousAfterInstallHook = SceneCompositionRoot.AfterInstallHooks;
                SceneCompositionRoot.AfterInstallHooks = (container) =>
                {
                    decoratorCompRoot.AddPostBindings(container);

                    if (previousAfterInstallHook != null)
                    {
                        previousAfterInstallHook(container);
                    }
                };

                var compRoot = newRootObjects.SelectMany(x => x.GetComponentsInChildren <SceneCompositionRoot>()).OnlyOrDefault();

                if (compRoot != null)
                {
                    return(ValidateCompRoot(compRoot, maxErrors));
                }

                var newDecoratorCompRoot = newRootObjects.SelectMany(x => x.GetComponentsInChildren <SceneDecoratorCompositionRoot>()).OnlyOrDefault();

                if (newDecoratorCompRoot != null)
                {
                    return(ValidateDecoratorCompRoot(newDecoratorCompRoot, maxErrors));
                }

                return(new List <ZenjectResolveException>()
                {
                    new ZenjectResolveException(
                        "Could not find composition root for decorated scene '{0}'".Fmt(sceneName)),
                });
            }
            finally
            {
#if UNITY_5_3
                EditorSceneManager.CloseScene(EditorSceneManager.GetSceneByPath(scenePath), true);
#else
                foreach (var newObject in newRootObjects)
                {
                    GameObject.DestroyImmediate(newObject);
                }
#endif
            }
        }
Пример #2
0
        static List<ZenjectResolveException> ValidateDecoratorCompRoot(SceneDecoratorCompositionRoot decoratorCompRoot, int maxErrors)
        {
            var sceneName = decoratorCompRoot.SceneName;
            var scenePath = UnityEditorUtil.GetScenePath(sceneName);

            if (scenePath == null)
            {
                return new List<ZenjectResolveException>()
                {
                    new ZenjectResolveException(
                        "Could not find scene path for decorated scene '{0}'".Fmt(sceneName)),
                };
            }

            var rootObjectsBefore = UnityUtil.GetRootGameObjects();

            OpenSceneAdditive(scenePath);

            var newRootObjects = UnityUtil.GetRootGameObjects().Except(rootObjectsBefore);

            // Use finally to ensure we clean up the data added from OpenSceneAdditive
            try
            {
                var previousBeforeInstallHook = SceneCompositionRoot.BeforeInstallHooks;
                SceneCompositionRoot.BeforeInstallHooks = (container) =>
                {
                    if (previousBeforeInstallHook != null)
                    {
                        previousBeforeInstallHook(container);
                    }

                    decoratorCompRoot.AddPreBindings(container);
                };

                var previousAfterInstallHook = SceneCompositionRoot.AfterInstallHooks;
                SceneCompositionRoot.AfterInstallHooks = (container) =>
                {
                    decoratorCompRoot.AddPostBindings(container);

                    if (previousAfterInstallHook != null)
                    {
                        previousAfterInstallHook(container);
                    }
                };

                var compRoot = newRootObjects.SelectMany(x => x.GetComponentsInChildren<SceneCompositionRoot>()).OnlyOrDefault();

                if (compRoot != null)
                {
                    return ValidateCompRoot(compRoot, maxErrors);
                }

                var newDecoratorCompRoot = newRootObjects.SelectMany(x => x.GetComponentsInChildren<SceneDecoratorCompositionRoot>()).OnlyOrDefault();

                if (newDecoratorCompRoot != null)
                {
                    return ValidateDecoratorCompRoot(newDecoratorCompRoot, maxErrors);
                }

                return new List<ZenjectResolveException>()
                {
                    new ZenjectResolveException(
                        "Could not find composition root for decorated scene '{0}'".Fmt(sceneName)),
                };
            }
            finally
            {
#if UNITY_5_3
                EditorSceneManager.CloseScene(EditorSceneManager.GetSceneByPath(scenePath), true);
#else
                foreach (var newObject in newRootObjects)
                {
                    GameObject.DestroyImmediate(newObject);
                }
#endif
            }
        }