Exemplo n.º 1
0
        private bool resolveSiteFromAnywhere(IInjectionSite site)
        {
            if (string.IsNullOrEmpty(site.HintPath))
            {
                // if no hint path is given just search from the scene root objects
                return(resolveSiteFromRootObjects(site));
            }

            // search from the hint path object
            GameObject objectToSearchFrom = getObjectFromHint(site.HintPath);

            if (objectToSearchFrom == null)
            {
                if (site.Required)
                {
                    Debug.LogError($"Unable to inject dependency for {injectionSiteDebugString(site)} - " +
                                   $"Hint path '{site.HintPath}' did not correspond to a GameObject in the scene " +
                                   $"hierarchy");
                }

                return(false);
            }

            return(resolveSiteFromObject(site, objectToSearchFrom));
        }
Exemplo n.º 2
0
        private bool resolveSiteFromRootObjects(IInjectionSite site)
        {
            var rootObjects = getSceneRootObjects();

            switch (site.SearchMethod)
            {
            case Search.Breadth:
                return(resolveSiteBreadthFirst(site, rootObjects));

            case Search.Depth:
                return(resolveSiteDepthFirst(site, rootObjects));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemplo n.º 3
0
        private bool resolveSite(IInjectionSite site, MonoBehaviour script)
        {
            switch (site.FromSource)
            {
            case From.Anywhere:
                return(resolveSiteFromAnywhere(site));

            case From.Parents:
                return(resolveSiteFromParents(site, script));

            case From.Children:
                return(resolveSiteFromChildren(site, script));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemplo n.º 4
0
        private bool resolveSiteFromObject(IInjectionSite site, GameObject gameObject)
        {
            switch (site.SearchMethod)
            {
            case Search.Breadth:
                return(resolveSiteBreadthFirst(site, new List <GameObject> {
                    gameObject
                }));

            case Search.Depth:
                return(resolveSiteDepthFirst(site, new List <GameObject> {
                    gameObject
                }));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemplo n.º 5
0
        private Component findInjectionSource(IInjectionSite site, GameObject sourceObject)
        {
            var foundSites = findAllInjectionSources(site.SiteType, sourceObject).ToList();

            if (foundSites.Any())
            {
                if (foundSites.Count > 1)
                {
                    Debug.LogWarning($"Found more than one injection source on object " +
                                     $"'{sourceObject}' for {injectionSiteDebugString(site)} - potential " +
                                     $"ambiguity issues. Picking the first one.");
                }

                return(foundSites.First());
            }

            return(null);
        }
Exemplo n.º 6
0
        private bool resolveSiteBreadthFirst(IInjectionSite site, List <GameObject> rootNodes)
        {
            foreach (var gameObject in breadthFirstTraversal(rootNodes))
            {
                var source = findInjectionSource(site, gameObject);
                if (source != null)
                {
                    site.Resolve(source);
                    return(true);
                }
            }

            if (site.Required)
            {
                Debug.LogWarning("Unable to find dependency source from anywhere (used breadth-first strategy)");
            }

            return(false);
        }
Exemplo n.º 7
0
        private bool resolveSiteFromParents(IInjectionSite site, MonoBehaviour script)
        {
            // search for source upwards from script object
            foreach (var gameObject in ancestors(script.gameObject))
            {
                var source = findInjectionSource(site, gameObject);
                if (source != null)
                {
                    site.Resolve(source);
                    return(true);
                }
            }

            if (site.Required)
            {
                Debug.LogWarning("Unable to find dependency source in parents");
            }

            return(false);
        }
Exemplo n.º 8
0
        private bool resolveSiteFromChildren(IInjectionSite site, MonoBehaviour script)
        {
            IEnumerable <GameObject> children;

            switch (site.SearchMethod)
            {
            case Search.Breadth:
                children = breadthFirstTraversal(new List <GameObject> {
                    script.gameObject
                });
                break;

            case Search.Depth:
                children = depthFirstTraversal(new List <GameObject> {
                    script.gameObject
                });
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            foreach (var gameObject in children)
            {
                var source = findInjectionSource(site, gameObject);
                if (source != null)
                {
                    site.Resolve(source);
                    return(true);
                }
            }

            if (site.Required)
            {
                Debug.LogWarning("Unable to find dependency source in children");
            }

            return(false);
        }
Exemplo n.º 9
0
 private string injectionSiteDebugString(IInjectionSite site)
 {
     return($"object '{site.Script.gameObject.name}' at site {site.Script.name}.{site.Name} ({site.SiteType})");
 }