예제 #1
0
        internal Path ConvertPathToRelative(Path globalPath)
        {
            // 1. Find last shared ancestor
            // 2. Drill up using ".." style (actually represented as "^")
            // 3. Re-build downward chain from common ancestor

            var ownPath = this.path;

            int minPathLength           = Math.Min(globalPath.length, ownPath.length);
            int lastSharedPathCompIndex = -1;

            for (int i = 0; i < minPathLength; ++i)
            {
                var ownComp   = ownPath.GetComponent(i);
                var otherComp = globalPath.GetComponent(i);

                if (ownComp.Equals(otherComp))
                {
                    lastSharedPathCompIndex = i;
                }
                else
                {
                    break;
                }
            }

            // No shared path components, so just use global path
            if (lastSharedPathCompIndex == -1)
            {
                return(globalPath);
            }

            int numUpwardsMoves = (ownPath.length - 1) - lastSharedPathCompIndex;

            var newPathComps = new List <Path.Component> ();

            for (int up = 0; up < numUpwardsMoves; ++up)
            {
                newPathComps.Add(Path.Component.ToParent());
            }

            for (int down = lastSharedPathCompIndex + 1; down < globalPath.length; ++down)
            {
                newPathComps.Add(globalPath.GetComponent(down));
            }

            var relativePath = new Path(newPathComps, relative: true);

            return(relativePath);
        }
예제 #2
0
        public SearchResult ContentAtPath(Path path, int partialPathStart = 0, int partialPathLength = -1)
        {
            if (partialPathLength == -1)
            {
                partialPathLength = path.length;
            }

            var result = new SearchResult();

            result.approximate = false;

            Container currentContainer = this;

            Runtime.Object currentObj = this;

            for (int i = partialPathStart; i < partialPathLength; ++i)
            {
                var comp = path.GetComponent(i);

                // Path component was wrong type
                if (currentContainer == null)
                {
                    result.approximate = true;
                    break;
                }

                var foundObj = currentContainer.ContentWithPathComponent(comp);

                // Couldn't resolve entire path?
                if (foundObj == null)
                {
                    result.approximate = true;
                    break;
                }

                currentObj       = foundObj;
                currentContainer = foundObj as Container;
            }

            result.obj = currentObj;

            return(result);
        }
예제 #3
0
        internal SearchResult ResolvePath(Path path)
        {
            if (path.isRelative)
            {
                Container nearestContainer = this as Container;
                if (!nearestContainer)
                {
                    Debug.Assert(this.parent != null, "Can't resolve relative path because we don't have a parent");
                    nearestContainer = this.parent as Container;
                    Debug.Assert(nearestContainer != null, "Expected parent to be a container");
                    Debug.Assert(path.GetComponent(0).isParent);
                    path = path.tail;
                }

                return(nearestContainer.ContentAtPath(path));
            }
            else
            {
                return(this.rootContentContainer.ContentAtPath(path));
            }
        }
예제 #4
0
파일: Container.cs 프로젝트: zhalesi/ink
        public Runtime.Object ContentAtPath(Path path, int partialPathLength = -1)
        {
            if (partialPathLength == -1)
            {
                partialPathLength = path.length;
            }

            Container currentContainer = this;

            Runtime.Object currentObj = this;

            for (int i = 0; i < partialPathLength; ++i)
            {
                var comp = path.GetComponent(i);
                if (currentContainer == null)
                {
                    throw new System.Exception("Path continued, but previous object wasn't a container: " + currentObj);
                }
                currentObj       = currentContainer.ContentWithPathComponent(comp);
                currentContainer = currentObj as Container;
            }

            return(currentObj);
        }