Exemplo n.º 1
0
        public void ReadElement(XmlNode element, XmlDocument document, List <string> dictionary)
        {
            var childCount = binaryReader.ReadInt16();
            var descriptor = new PackedXmlDataDescriptor(binaryReader.ReadInt32());

            var elements = new PackedXmlElementDescriptor[childCount];

            for (int i = 0; i < childCount; i++)
            {
                var nameIndex = binaryReader.ReadInt16();
                elements[i] = new PackedXmlElementDescriptor(nameIndex, dictionary[nameIndex], binaryReader.ReadInt32());
            }

            int offset = ReadElementData(element, document, dictionary, descriptor);

            foreach (var elementDescriptor in elements)
            {
                var     elementName = dictionary[elementDescriptor.nameIndex];
                XmlNode child       = document.CreateElement(elementName);
                offset = ReadElementData(child, document, dictionary, elementDescriptor, offset);
                element.AppendChild(child);
            }
        }
Exemplo n.º 2
0
        public int ReadElementData(XmlNode element, XmlDocument document, List <string> dictionary, PackedXmlDataDescriptor descriptor, int offset = 0)
        {
            int lengthInBytes = descriptor.end - offset;

            switch (descriptor.type)
            {
            case EPackedXmlDataType.Element:
                ReadElement(element, document, dictionary);
                break;

            case EPackedXmlDataType.String:
                element.InnerText = ReadString(lengthInBytes);
                break;

            case EPackedXmlDataType.Integer:
                element.InnerText = ReadNumber(lengthInBytes);
                break;

            case EPackedXmlDataType.Float:
                var floats = ReadFloats(lengthInBytes / 4)
                             .Select(f => f.ToString("0.000000", CultureInfo.InvariantCulture))
                             .ToList();

                if (floats.Count == 12)     //four rows of three floats
                {
                    for (int i = 0; i < 4; i++)
                    {
                        XmlNode row = document.CreateElement($"row{i}");
                        row.InnerText = string.Join(" ", floats.Skip(i * 3).Take(3));
                        element.AppendChild(row);
                    }
                }
                else
                {
                    element.InnerText = string.Join(" ", floats);
                }

                break;

            case EPackedXmlDataType.Boolean:
                //TODO is this correct?
                if (lengthInBytes != 1)
                {
                    element.InnerText = "false";
                }
                else
                {
                    if (binaryReader.ReadSByte() != 1)
                    {
                        throw new System.ArgumentException("Boolean error");
                    }
                    element.InnerText = "true";
                }
                break;

            case EPackedXmlDataType.Base64:
                element.InnerText = ReadBase64AsText(lengthInBytes);
                break;

            default:
                throw new ArgumentException($"Unknown type of element {element.Name}: {descriptor.type}");
            }
            if (element.Name == "weight" && element.InnerText == "10000")
            {
                lengthInBytes = lengthInBytes;
            }

            return(descriptor.end);
        }