Esempio n. 1
0
        private void SerializeNode(BinaryWriter binaryWriter, TbXmlNode node)
        {
            TbXmlNodeTemplate nodeTemplate = GetNodeTemplate(node.templateId);

            binaryWriter.Write(node.id);

            binaryWriter.Write(node.templateId);

            binaryWriter.Write((ushort)node.childrenIds.Count);
            foreach (ushort childId in node.childrenIds)
            {
                binaryWriter.Write(childId);
            }

            int attributeIndex = 0;

            foreach (int attributeValue in node.attributeValues)
            {
                binaryWriter.Write(attributeValue);
                ++attributeIndex;
            }

            if (node.text == -1)
            {
                binaryWriter.Write((byte)0);
            }
            else
            {
                binaryWriter.Write((byte)1);
                binaryWriter.Write(node.text);
            }
        }
Esempio n. 2
0
        public byte[] SerializeXmlString(string xmlString)
        {
            if (string.IsNullOrEmpty(xmlString))
            {
                return(null);
            }

            nodeTemplates.Clear();
            nodes.Clear();
            stringPool.Clear();
            valuePool.Clear();
            nodeIdInc         = 0;
            nodeTemplateIdInc = 0;

            XmlDocument doc = new XmlDocument();

            doc.LoadXml(xmlString);

            TbXmlNode docNode = new TbXmlNode();

            docNode.childrenIds = new List <ushort>();

            XmlNodeList xmlNodeList = doc.ChildNodes;

            foreach (XmlNode xmlNode in xmlNodeList)
            {
                if (xmlNode.NodeType == XmlNodeType.Element)
                {
                    ProcessXmlNode(docNode, xmlNode);
                }
            }

            byte[] buffer = null;
            using (BinaryWriter binaryWriter = new BinaryWriter(new MemoryStream(), Encoding.UTF8))
            {
                Serialize(binaryWriter);

                buffer = new byte[binaryWriter.BaseStream.Length];
                binaryWriter.BaseStream.Position = 0;
                binaryWriter.BaseStream.Read(buffer, 0, (int)binaryWriter.BaseStream.Length);
                binaryWriter.BaseStream.Close();
                binaryWriter.BaseStream.Dispose();
                binaryWriter.Close();
            }
            return(buffer);
        }
Esempio n. 3
0
        public List <TbXmlNode> GetNodes(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(null);
            }

            List <TbXmlNode> resultNodes = null;
            int numChildren = childrenIds == null ? 0 : childrenIds.Count;

            string[] pathBlocks = path.Split('/');
            for (int childIndex = 0; childIndex < numChildren; ++childIndex)
            {
                TbXmlNode childNode = tbXml.nodes[childrenIds[childIndex]];
                GetNodesRecursive(pathBlocks, 0, ref pathBlocks[0], childNode, ref resultNodes);
            }

            return(resultNodes);
        }
Esempio n. 4
0
        public List <TbXmlNode> GetNodes(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(null);
            }
            List <TbXmlNode> result = null;
            int num = (this.childrenIds != null) ? this.childrenIds.Count : 0;

            string[] array = path.Split(new char[]
            {
                '/'
            });
            for (int i = 0; i < num; i++)
            {
                TbXmlNode currentNode = this.tbXml.nodes[(int)this.childrenIds[i]];
                this.GetNodesRecursive(array, 0, ref array[0], currentNode, ref result);
            }
            return(result);
        }
Esempio n. 5
0
        private void DeserializeNode(BinaryReader binaryReader, ushort index, TbXml tbXml)
        {
            TbXmlNode node = new TbXmlNode();

            tbXml.nodes.Add(node);

            node.id         = binaryReader.ReadUInt16();
            node.templateId = binaryReader.ReadUInt16();

            if (binaryReader.ReadByte() == 1)
            {
                ushort numChildren = binaryReader.ReadUInt16();
                if (numChildren > 0)
                {
                    node.childrenIds = new List <ushort>(numChildren);
                    for (int i = 0; i < numChildren; ++i)
                    {
                        node.childrenIds.Add(binaryReader.ReadUInt16());
                    }
                }
            }

            TbXmlNodeTemplate nodeTemplate  = tbXml.nodeTemplates[node.templateId];
            ushort            numAttributes = (ushort)(nodeTemplate.attributeNames == null ? 0 : nodeTemplate.attributeNames.Count);

            if (numAttributes > 0)
            {
                node.attributeValues = new List <int>(numAttributes);
                for (ushort i = 0; i < numAttributes; ++i)
                {
                    node.attributeValues.Add(binaryReader.ReadInt32());
                }
            }

            byte hasText = binaryReader.ReadByte();

            if (hasText == 1)
            {
                node.text = binaryReader.ReadInt32();
            }
        }
Esempio n. 6
0
        public byte[] SerializeXmlString(string xmlString)
        {
            if(string.IsNullOrEmpty(xmlString))
            {
                return null;
            }

            nodeTemplates.Clear();
            nodes.Clear();
            stringPool.Clear();
            valuePool.Clear();
            nodeIdInc = 0;
            nodeTemplateIdInc = 0;

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlString);

            TbXmlNode docNode = new TbXmlNode();
            docNode.childrenIds = new List<ushort>();

            XmlNodeList xmlNodeList = doc.ChildNodes;
            foreach(XmlNode xmlNode in xmlNodeList)
            {
                if(xmlNode.NodeType == XmlNodeType.Element)
                {
                    ProcessXmlNode(docNode, xmlNode);
                }
            }

            BinaryWriter binaryWriter = new BinaryWriter(new MemoryStream(), Encoding.UTF8);
            Serialize(binaryWriter);

            byte[] buffer = new byte[binaryWriter.BaseStream.Length];
            binaryWriter.BaseStream.Position = 0;
            binaryWriter.BaseStream.Read(buffer, 0, (int)binaryWriter.BaseStream.Length);
            binaryWriter.Close();
            binaryWriter.Dispose();

            return buffer;
        }
Esempio n. 7
0
        private void DeserializeNode(BinaryReader binaryReader, ushort index, TbXml tbXml)
        {
            TbXmlNode node = new TbXmlNode();
            tbXml.nodes.Add(node);

            node.id = binaryReader.ReadUInt16();
            node.templateId = binaryReader.ReadUInt16();

            if(binaryReader.ReadByte() == 1)
            {
                ushort numChildren = binaryReader.ReadUInt16();
                if(numChildren > 0)
                {
                    node.childrenIds = new List<ushort>(numChildren);
                    for(int i = 0; i < numChildren; ++i)
                    {
                        node.childrenIds.Add(binaryReader.ReadUInt16());
                    }
                }
            }

            TbXmlNodeTemplate nodeTemplate = tbXml.nodeTemplates[node.templateId];
            ushort numAttributes = (ushort)(nodeTemplate.attributeNames == null ? 0 : nodeTemplate.attributeNames.Count);
            if(numAttributes > 0)
            {
                node.attributeValues = new List<int>(numAttributes);
                for(ushort i = 0; i < numAttributes; ++i)
                {
                    node.attributeValues.Add(binaryReader.ReadInt32());
                }
            }

            byte hasText = binaryReader.ReadByte();
            if(hasText == 1)
            {
                node.text = binaryReader.ReadInt32();
            }
        }
Esempio n. 8
0
 private void GetNodesRecursive(string[] pathBlocks, int pathBlockIndex, ref string pathBlock, TbXmlNode currentNode, ref List <TbXmlNode> resultNodes)
 {
     if (tbXml.nodeTemplates[currentNode.templateId].name.Equals(pathBlock))
     {
         if (pathBlockIndex == pathBlocks.Length - 1)
         {
             if (resultNodes == null)
             {
                 resultNodes = new List <TbXmlNode>();
             }
             resultNodes.Add(currentNode);
         }
         else
         {
             List <ushort> childrenIds = currentNode.childrenIds;
             int           numChildren = childrenIds == null ? 0 : childrenIds.Count;
             for (int childIndex = 0; childIndex < numChildren; ++childIndex)
             {
                 GetNodesRecursive(pathBlocks, pathBlockIndex + 1, ref pathBlocks[pathBlockIndex + 1], tbXml.nodes[childrenIds[childIndex]], ref resultNodes);
             }
         }
     }
 }
Esempio n. 9
0
        private void ProcessXmlNode(TbXmlNode parentNode, XmlNode xmlNode)
        {
            TbXmlNodeTemplate nodeTemplate = GetNodeTemplate(xmlNode);

            if (nodeTemplate == null)
            {
                nodeTemplate = new TbXmlNodeTemplate();
                nodeTemplates.Add(nodeTemplate);
                nodeTemplate.attributeNames = new List <string>();
                nodeTemplate.attributeTypes = new List <TB_XML_ATTRIBUTE_TYPE>();
                nodeTemplate.id             = nodeTemplateIdInc++;
                nodeTemplate.name           = xmlNode.Name;
                foreach (XmlAttribute xmlAttribute in xmlNode.Attributes)
                {
                    string attributeString = xmlAttribute.Value;
                    double attributeDouble;
                    if (double.TryParse(attributeString, out attributeDouble))
                    {
                        nodeTemplate.attributeTypes.Add(TB_XML_ATTRIBUTE_TYPE.DOUBLE);
                    }
                    else
                    {
                        nodeTemplate.attributeTypes.Add(TB_XML_ATTRIBUTE_TYPE.STRING);
                    }
                    nodeTemplate.attributeNames.Add(xmlAttribute.Name);
                }
            }

            TbXmlNode node = new TbXmlNode();

            nodes.Add(node);
            node.attributeValues = new List <int>();
            node.childrenIds     = new List <ushort>();
            node.id         = nodeIdInc++;
            node.templateId = nodeTemplate.id;
            parentNode.childrenIds.Add(node.id);
            foreach (XmlAttribute xmlAttribute in xmlNode.Attributes)
            {
                string attributeString = xmlAttribute.Value;
                double attributeDouble;
                if (double.TryParse(attributeString, out attributeDouble))
                {
                    int valueIndex = MatchValueIndex(attributeDouble);
                    if (valueIndex == -1)
                    {
                        valuePool.Add(attributeDouble);
                        node.attributeValues.Add(valuePool.Count - 1);
                    }
                    else
                    {
                        node.attributeValues.Add(valueIndex);
                    }
                }
                else
                {
                    int stringIndex = MatchStringIndex(attributeString);
                    if (stringIndex == -1)
                    {
                        stringPool.Add(attributeString);
                        node.attributeValues.Add(stringPool.Count - 1);
                    }
                    else
                    {
                        node.attributeValues.Add(stringIndex);
                    }
                }
            }

            foreach (XmlNode subXmlNode in xmlNode.ChildNodes)
            {
                if (subXmlNode.NodeType == XmlNodeType.Element)
                {
                    ProcessXmlNode(node, subXmlNode);
                }
                else if (subXmlNode.NodeType == XmlNodeType.Text || subXmlNode.NodeType == XmlNodeType.CDATA)
                {
                    if (node.text == -1)
                    {
                        int stringIndex = MatchStringIndex(subXmlNode.Value);
                        if (stringIndex == -1)
                        {
                            stringPool.Add(subXmlNode.Value);
                            node.text = stringPool.Count - 1;
                        }
                        else
                        {
                            node.text = stringIndex;
                        }
                    }
                    else
                    {
                        Console.WriteLine("Ignore yyy of <nodeA>xxx<nodeB/>yyy<nodeA/>");
                        Console.WriteLine(subXmlNode.InnerText);
//                        UnityEngine.Debug.LogError("Ignore yyy of <nodeA>xxx<nodeB/>yyy<nodeA/>");
//                        UnityEngine.Debug.LogError(subXmlNode.InnerText);
                    }
                }
            }
        }
Esempio n. 10
0
 private void GetNodesRecursive(string[] pathBlocks, int pathBlockIndex, ref string pathBlock, TbXmlNode currentNode, ref List <TbXmlNode> resultNodes)
 {
     if (this.tbXml.nodeTemplates[(int)currentNode.templateId].name.Equals(pathBlock))
     {
         if (pathBlockIndex == pathBlocks.Length - 1)
         {
             if (resultNodes == null)
             {
                 resultNodes = new List <TbXmlNode>();
             }
             resultNodes.Add(currentNode);
         }
         else
         {
             List <ushort> list = currentNode.childrenIds;
             int           num  = (list != null) ? list.Count : 0;
             for (int i = 0; i < num; i++)
             {
                 this.GetNodesRecursive(pathBlocks, pathBlockIndex + 1, ref pathBlocks[pathBlockIndex + 1], this.tbXml.nodes[(int)list[i]], ref resultNodes);
             }
         }
     }
 }
Esempio n. 11
0
 private void GetNodesRecursive(string[] pathBlocks, int pathBlockIndex, ref string pathBlock, TbXmlNode currentNode, ref List<TbXmlNode> resultNodes)
 {
     if(tbXml.nodeTemplates[currentNode.templateId].name.Equals(pathBlock))
     {
         if(pathBlockIndex == pathBlocks.Length - 1)
         {
             if(resultNodes == null)
             {
                 resultNodes = new List<TbXmlNode>();
             }
             resultNodes.Add(currentNode);
         }
         else
         {
             List<ushort> childrenIds = currentNode.childrenIds;
             int numChildren = childrenIds == null ? 0 : childrenIds.Count;
             for(int childIndex = 0; childIndex < numChildren; ++childIndex)
             {
                 GetNodesRecursive(pathBlocks, pathBlockIndex + 1, ref pathBlocks[pathBlockIndex + 1], tbXml.nodes[childrenIds[childIndex]], ref resultNodes);
             }
         }
     }
 }
Esempio n. 12
0
        private void SerializeNode(BinaryWriter binaryWriter, TbXmlNode node)
        {
            binaryWriter.Write(node.id);

            binaryWriter.Write(node.templateId);

            if(node.childrenIds.Count == 0)
            {
                binaryWriter.Write((byte)0);
            }
            else
            {
                binaryWriter.Write((byte)1);
                binaryWriter.Write((ushort)node.childrenIds.Count);
                foreach(ushort childId in node.childrenIds)
                {
                    binaryWriter.Write(childId);
                }
            }

            int attributeIndex = 0;
            foreach(int attributeValue in node.attributeValues)
            {
                binaryWriter.Write(attributeValue);
                ++attributeIndex;
            }

            if(node.text == -1)
            {
                binaryWriter.Write((byte)0);
            }
            else
            {
                binaryWriter.Write((byte)1);
                binaryWriter.Write(node.text);
            }
        }
Esempio n. 13
0
        private void ProcessXmlNode(TbXmlNode parentNode, XmlNode xmlNode)
        {
            TbXmlNodeTemplate nodeTemplate = GetNodeTemplate(xmlNode);
            if(nodeTemplate == null)
            {
                nodeTemplate = new TbXmlNodeTemplate();
                nodeTemplates.Add(nodeTemplate);
                nodeTemplate.attributeNames = new List<string>();
                nodeTemplate.attributeTypes = new List<TB_XML_ATTRIBUTE_TYPE>();
                nodeTemplate.id = nodeTemplateIdInc++;
                nodeTemplate.name = xmlNode.Name;
                foreach(XmlAttribute xmlAttribute in xmlNode.Attributes)
                {
                    string attributeString = xmlAttribute.Value;
                    double attributeDouble;
                    if(double.TryParse(attributeString, out attributeDouble))
                    {
                        nodeTemplate.attributeTypes.Add(TB_XML_ATTRIBUTE_TYPE.DOUBLE);
                    }
                    else
                    {
                        nodeTemplate.attributeTypes.Add(TB_XML_ATTRIBUTE_TYPE.STRING);
                    }
                    nodeTemplate.attributeNames.Add(xmlAttribute.Name);
                }
            }

            TbXmlNode node = new TbXmlNode();
            nodes.Add(node);
            node.attributeValues = new List<int>();
            node.childrenIds = new List<ushort>();
            node.id = nodeIdInc++;
            node.templateId = nodeTemplate.id;
            parentNode.childrenIds.Add(node.id);
            foreach(XmlAttribute xmlAttribute in xmlNode.Attributes)
            {
                string attributeString = xmlAttribute.Value;
                double attributeDouble;
                if(double.TryParse(attributeString, out attributeDouble))
                {
                    int valueIndex = MatchValueIndex(attributeDouble);
                    if (valueIndex == -1)
                    {
                        valuePool.Add(attributeDouble);
                        node.attributeValues.Add(valuePool.Count - 1);
                    }
                    else
                    {
                        node.attributeValues.Add(valueIndex);
                    }
                }
                else
                {
                    int stringIndex = MatchStringIndex(attributeString);
                    if (stringIndex == -1)
                    {
                        stringPool.Add(attributeString);
                        node.attributeValues.Add(stringPool.Count - 1);
                    }
                    else
                    {
                        node.attributeValues.Add(stringIndex);
                    }
                }
            }

            foreach(XmlNode subXmlNode in xmlNode.ChildNodes)
            {
                if(subXmlNode.NodeType == XmlNodeType.Element)
                {
                    ProcessXmlNode(node, subXmlNode);
                }
                else if(subXmlNode.NodeType == XmlNodeType.Text || subXmlNode.NodeType == XmlNodeType.CDATA)
                {
                    if(node.text == -1)
                    {
                        int stringIndex = MatchStringIndex(subXmlNode.Value);
                        if (stringIndex == -1)
                        {
                            stringPool.Add(subXmlNode.Value);
                            node.text = stringPool.Count - 1;
                        }
                        else
                        {
                            node.text = stringIndex;
                        }
                    }
                    else
                    {
                        Console.WriteLine("Ignore yyy of <nodeA>xxx<nodeB/>yyy<nodeA/>");
                        Console.WriteLine(subXmlNode.InnerText);
            //                        UnityEngine.Debug.LogError("Ignore yyy of <nodeA>xxx<nodeB/>yyy<nodeA/>");
            //                        UnityEngine.Debug.LogError(subXmlNode.InnerText);
                    }
                }
            }
        }