コード例 #1
0
        void FetchSnippets()
        {
            int[] codeSnippetAttribBeginIndexes = m_templateBody.AllIndexesOf(TemplatesManager.TemplateCodeSnippetAttribBegin);
            int[] codeSnippetAttribEndIndexes   = m_templateBody.AllIndexesOf(TemplatesManager.TemplateCodeSnippetAttribEnd);
            int[] codeSnippetEndIndexes         = m_templateBody.AllIndexesOf(TemplatesManager.TemplateCodeSnippetEnd);

            if (codeSnippetAttribBeginIndexes != null && codeSnippetAttribBeginIndexes.Length > 0 &&
                codeSnippetAttribEndIndexes != null && codeSnippetAttribEndIndexes.Length > 0 &&
                codeSnippetEndIndexes != null && codeSnippetEndIndexes.Length > 0 &&
                codeSnippetEndIndexes.Length == codeSnippetAttribBeginIndexes.Length &&
                codeSnippetAttribBeginIndexes.Length == codeSnippetAttribEndIndexes.Length)
            {
                for (int i = 0; i < codeSnippetAttribBeginIndexes.Length; i++)
                {
                    // get attributes
                    int      startAttribIndex  = codeSnippetAttribBeginIndexes[i] + TemplatesManager.TemplateCodeSnippetAttribBegin.Length;
                    int      lengthAttrib      = codeSnippetAttribEndIndexes[i] - startAttribIndex;
                    string   snippetAttribs    = m_templateBody.Substring(startAttribIndex, lengthAttrib);
                    string[] snippetAttribsArr = snippetAttribs.Split(IOUtils.FIELD_SEPARATOR);
                    if (snippetAttribsArr != null && snippetAttribsArr.Length > 0)
                    {
                        string attribName = snippetAttribsArr[(int)TemplateCodeSnippetInfoIdx.Name];
                        TemplateCodeSnippetType attribType = (TemplateCodeSnippetType)Enum.Parse(typeof(TemplateCodeSnippetType), snippetAttribsArr[(int)TemplateCodeSnippetInfoIdx.Type]);
                        if (m_snippetElementsDict.ContainsKey(attribName))
                        {
                            if (m_snippetElementsDict[attribName].Type != attribType)
                            {
                                if (DebugConsoleWindow.DeveloperMode)
                                {
                                    Debug.LogWarning("Found incompatible types for snippet " + attribName);
                                }
                            }
                        }
                        else
                        {
                            switch (attribType)
                            {
                            case TemplateCodeSnippetType.Toggle:
                            {
                                //Register must be done by first instantiang the correct type and register it on both containers
                                //Overrides don't work if we use the container reference into the other
                                TemplateCodeSnippetToggle newSnippet = ScriptableObject.CreateInstance <TemplateCodeSnippetToggle>();
                                newSnippet.Init(attribName, attribType);
                                m_snippetElementsDict.Add(attribName, newSnippet);
                                m_snippetElementsList.Add(newSnippet);
                            }
                            break;
                            }
                        }
                        // Add initial tag indentation
                        int indentationIndex = codeSnippetAttribBeginIndexes[i];
                        int lengthAdjust     = 0;
                        for ( ; indentationIndex > 0; indentationIndex--, lengthAdjust++)
                        {
                            if (m_templateBody[indentationIndex] == TemplatesManager.TemplateNewLine)
                            {
                                indentationIndex += 1;
                                lengthAdjust     -= 1;
                                break;
                            }
                        }

                        if (indentationIndex > 0)
                        {
                            string snippetId = m_templateBody.Substring(indentationIndex,
                                                                        codeSnippetEndIndexes[i] + TemplatesManager.TemplateCodeSnippetEnd.Length - codeSnippetAttribBeginIndexes[i] + lengthAdjust);

                            int snippetCodeStart  = codeSnippetAttribEndIndexes[i] + TemplatesManager.TemplateCodeSnippetAttribEnd.Length;
                            int snippetCodeLength = codeSnippetEndIndexes[i] - snippetCodeStart;
                            //Remove possible identation characters present between tag and last instruction
                            if (m_templateBody[snippetCodeStart + snippetCodeLength - 1] != TemplatesManager.TemplateNewLine)
                            {
                                for ( ; snippetCodeLength > 0; snippetCodeLength--)
                                {
                                    if (m_templateBody[snippetCodeStart + snippetCodeLength - 1] == TemplatesManager.TemplateNewLine)
                                    {
                                        break;
                                    }
                                }
                            }

                            if (snippetCodeLength > 0)
                            {
                                string snippetCode = m_templateBody.Substring(snippetCodeStart, snippetCodeLength);
                                TemplateCodeSnippetElement element = new TemplateCodeSnippetElement(snippetId, snippetCode);
                                m_snippetElementsDict[attribName].AddSnippet(element);
                            }
                        }
                    }
                }
            }
        }
コード例 #2
0
 public void AddSnippet(TemplateCodeSnippetElement element)
 {
     m_elements.Add(element);
 }