Beispiel #1
0
        public void Deserialize(BinaryStream stream, List <NodeObject> objRefs)
        {
            Offset = (int)stream.Position;

            // define reference type just in case we f**k up somehow
            var nD = DescriptorTag.Read(stream, ReferenceType.Index);

            if (nD.Type == DescriptorType.Reference)
            {
                throw new InvalidOperationException("Cannot deserialize an object reference directly!");
            }

            var nChildren = nD.Value;

            Children = new List <NodeObject>(nChildren);

            var hash = stream.ReadInt32();
            var name = StringHasher.ResolveHash(hash);

            if (name != null)
            {
                Name = name;
            }
            else
            {
                Hash = hash;
            }

            // add a reference to this object
            objRefs.Add(this);

            var aD     = DescriptorTag.Read(stream, ReferenceType.Index);
            var nAttrs = aD.Value;

            Attributes = new List <NodeAttribute>(nAttrs);

            if (nAttrs > 0)
            {
                for (int i = 0; i < nAttrs; i++)
                {
                    // hash and data inline
                    var attr = new NodeAttribute(stream, Name);
                    attr.Deserialize(stream);

                    Attributes.Add(attr);
                }
            }

            if (nChildren > 0)
            {
                // read children
                for (int n = 0; n < nChildren; n++)
                {
                    var cP = (int)stream.Position;
                    var cD = DescriptorTag.Read(stream, ReferenceType.Index);

                    // rip
                    if (cD.IsIndex)
                    {
                        var idx      = cD.Value;
                        var childRef = objRefs[idx];

                        Children.Add(childRef);
                    }
                    else
                    {
                        // move back
                        stream.Position = cP;

                        var child = new NodeObject(stream, objRefs);
                        Children.Add(child);
                    }
                }
            }
        }
Beispiel #2
0
        public override void Deserialize(BinaryStream stream)
        {
            var ptr = (int)stream.Position;

            var nD = DescriptorTag.Read(stream, ReferenceType.Offset);

            if (nD.IsOffset)
            {
                stream.Position = nD.Value;
                Deserialize(stream);

                stream.Position = (ptr + nD.Size);
            }
            else
            {
                Offset = ptr;

                var nChildren = nD.Value;

                Children = new List <NodeClass>(nChildren);

                var hash = stream.ReadInt32();
                var size = stream.ReadInt16();

                var name = StringHasher.ResolveHash(hash);

                if (name != null)
                {
                    Name = name;
                }
                else
                {
                    Hash = hash;
                }

                var attrsPtr = (int)stream.Position;
                var next     = (attrsPtr + size);

                if (size != 0)
                {
                    var nhD = DescriptorTag.Read(stream, ReferenceType.Offset);

                    var adjustPtr = false;

                    if (nhD.IsOffset)
                    {
                        stream.Position = nhD.Value;

                        // read again
                        nhD = DescriptorTag.Read(stream, ReferenceType.Offset);

                        if (nhD.IsOffset)
                        {
                            throw new InvalidOperationException("Cannot have nested offsets!");
                        }

                        // adjust ptr to attributes
                        attrsPtr += nhD.Size;
                        adjustPtr = true;
                    }

                    var nAttrs = nhD.Value;

                    Attributes = new List <NodeAttribute>(nAttrs);

                    for (int i = 0; i < nAttrs; i++)
                    {
                        var attr = new NodeAttribute(stream, Name);
                        Attributes.Add(attr);
                    }

                    // move to the attributes if needed
                    if (adjustPtr)
                    {
                        stream.Position = attrsPtr;
                    }

                    // deserialize attribute data
                    foreach (var attr in Attributes)
                    {
                        attr.Deserialize(stream);
                    }
                }
                else
                {
                    throw new NotImplementedException("Zero-length nodes are not covered under TrumpCare™.");
                }

                if (stream.Position != next)
                {
                    throw new InvalidOperationException("You dun f****d up, son!");
                }

                // read children
                for (int n = 0; n < nChildren; n++)
                {
                    var child = new NodeClass(stream);
                    Children.Add(child);
                }
            }
        }