Esempio n. 1
0
        /// <summary>
        /// This method skips m_Modifications entry in prefab document and stores modifications in dictionaries
        ///
        /// After this method is executed, lexer current token is null or indent of next entry (after m_Modifications)
        /// </summary>
        private void GetModifications(IBuffer buffer, YamlLexer lexer, int parentIndentSize, Dictionary <FileID, string> names, Dictionary <FileID, int?> rootIndexes)
        {
            FileID curTarget       = null;
            string curPropertyPath = null;
            string curValue        = null;

            // Each property modifications is flow node:
            // - target: ..
            //   propertyPath: ..
            //   value:
            // Minus token means that new modification description is started
            // There are several entries in description. We are interested only
            // in target, propertyPath and value

            while (UnitySceneDataUtil.FindNextIndent(lexer))
            {
                var currentSize = lexer.TokenEnd - lexer.TokenStart;

                lexer.Advance();
                var tokenType = lexer.TokenType;


                if (tokenType == YamlTokenType.MINUS)
                {
                    currentSize++;
                    AddData();
                    lexer.Advance();
                }


                if (currentSize <= parentIndentSize)
                {
                    break;
                }

                UnitySceneDataUtil.SkipWhitespace(lexer);
                tokenType = lexer.TokenType;

                if (tokenType == YamlTokenType.NS_PLAIN_ONE_LINE_IN)
                {
                    var text = buffer.GetText(new TextRange(lexer.TokenStart, lexer.TokenEnd));
                    if (text.Equals(UnityYamlConstants.TargetProperty))
                    {
                        lexer.Advance();
                        UnitySceneDataUtil.SkipWhitespace(lexer);
                        lexer.Advance(); // skip column

                        // [TODO] handle prefab in prefab
                        curTarget = UnitySceneDataUtil.GetFileId(buffer, lexer).WithGuid(null);
                    }
                    else if (text.Equals(UnityYamlConstants.PropertyPathProperty))
                    {
                        lexer.Advance();
                        UnitySceneDataUtil.SkipWhitespace(lexer);
                        lexer.Advance();

                        curPropertyPath = UnitySceneDataUtil.GetPrimitiveValue(buffer, lexer);
                    }
                    else if (text.Equals(UnityYamlConstants.ValueProperty))
                    {
                        lexer.Advance();
                        UnitySceneDataUtil.SkipWhitespace(lexer);
                        lexer.Advance();

                        curValue = UnitySceneDataUtil.GetPrimitiveValue(buffer, lexer);
                    }
                }
            }

            AddData();

            void AddData()
            {
                if (curTarget != null)
                {
                    if (curPropertyPath != null && curPropertyPath.Equals(UnityYamlConstants.NameProperty))
                    {
                        names[curTarget] = curValue;
                    }
                    else if (curPropertyPath != null && curPropertyPath.Equals(UnityYamlConstants.RootOrderProperty))
                    {
                        rootIndexes[curTarget] = int.TryParse(curValue, out var r) ? (int?)r : null;
                    }

                    curTarget       = null;
                    curPropertyPath = null;
                    curValue        = null;
                }
            }
        }
Esempio n. 2
0
        public void AddPrefabModification(IBuffer buffer)
        {
            var anchor = UnitySceneDataUtil.GetAnchorFromBuffer(buffer);

            if (anchor == null)
            {
                return;
            }

            var lexer = new YamlLexer(buffer, false, false);

            lexer.Start();

            TokenNodeType currentToken;

            var transformParentId = FileID.Null;

            while ((currentToken = lexer.TokenType) != null)
            {
                if (currentToken == YamlTokenType.INDENT)
                {
                    var indentSize = lexer.TokenEnd - lexer.TokenStart;
                    lexer.Advance();
                    currentToken = lexer.TokenType;

                    if (currentToken == YamlTokenType.NS_PLAIN_ONE_LINE_IN)
                    {
                        var text = buffer.GetText(new TextRange(lexer.TokenStart, lexer.TokenEnd));
                        if (text.Equals(UnityYamlConstants.TransformParentProperty))
                        {
                            lexer.Advance();
                            UnitySceneDataUtil.SkipWhitespace(lexer);
                            currentToken = lexer.TokenType;

                            if (currentToken == YamlTokenType.COLON)
                            {
                                lexer.Advance();

                                var result = UnitySceneDataUtil.GetFileId(buffer, lexer);
                                if (result != null)
                                {
                                    transformParentId = result;
                                }
                            }
                        }
                        else if (text.Equals(UnityYamlConstants.ModificationsProperty))
                        {
                            var names       = new Dictionary <FileID, string>();
                            var rootIndexes = new Dictionary <FileID, int?>();
                            GetModifications(buffer, lexer, indentSize, names, rootIndexes);
                            var id = new FileID(null, anchor);
                            Elements.Add(id,
                                         new ModificationHierarchyElement(id, null, null, false, transformParentId, rootIndexes, names));
                            return;
                        }
                    }
                }
                else
                {
                    lexer.Advance();
                }
            }
        }