Beispiel #1
0
        protected void WriteRmlData(BinaryStream stream, NomadObject data)
        {
            byte[] rmlBuffer = null;

            using (var bs = new BinaryStream(1024))
            {
                var rmlData = new NomadRmlSerializer();
                rmlData.Serialize(bs, data);

                rmlBuffer = bs.ToArray();
            }

            var size = DescriptorTag.Create(rmlBuffer.Length);
            var next = DescriptorTag.Create(size);

            next.WriteTo(stream);
            size.WriteTo(stream);

            stream.Write(rmlBuffer, 0, rmlBuffer.Length);
        }
Beispiel #2
0
        public NomadObject ReadXmlObject(XElement xml, NomadObject parent = null)
        {
            Context.State = ContextStateType.Object;
            Context.ObjectIndex++;

            var name = xml.Name.LocalName;

            var id    = StringId.Parse(name);
            var isRml = false;

            if (parent != null)
            {
                isRml = parent.IsRml;
            }
            if (!isRml && (id == "RML_DATA"))
            {
                isRml = true;
            }

            if (isRml)
            {
                XElement rmlElem = null;

                foreach (var elem in xml.Elements())
                {
                    if (rmlElem != null)
                    {
                        throw new XmlException("Too many elements in RML_DATA node!");
                    }

                    rmlElem = elem;
                }

                if (rmlElem == null)
                {
                    throw new XmlException("Empty RML_DATA nodes are cancerous to your health!");
                }

                var rmlRoot = new NomadObject(true)
                {
                    Id = "RML_DATA"
                };

                ReadRmlObject(rmlElem, rmlRoot);

                byte[] rmlBuffer = null;

                using (var bs = new BinaryStream(1024))
                {
                    // don't write size yet
                    bs.Position += 4;

                    var rmlData = new NomadRmlSerializer();
                    rmlData.Serialize(bs, rmlRoot);

                    // write size
                    var rmlSize = (int)(bs.Position - 4);
                    bs.Position = 0;

                    bs.Write(rmlSize);

                    rmlBuffer = bs.ToArray();
                }

                if (parent != null)
                {
                    var rml = new NomadValue()
                    {
                        Id   = id,
                        Data = new AttributeData(DataType.RML, rmlBuffer),
                    };

                    parent.Attributes.Add(rml);
                }

                return(rmlRoot);
            }

            var result = new NomadObject(isRml)
            {
                Id = id
            };

            foreach (var attr in xml.Attributes())
            {
                ReadXmlAttribute(attr, result);
            }

            foreach (var node in xml.Elements())
            {
                ReadXmlObject(node, result);
            }

            if (parent != null)
            {
                parent.Children.Add(result);
            }

            return(result);
        }