Exemplo n.º 1
0
        private static Treenode _Read(BinaryReader reader, ref int count)
        {
            Treenode ret = new Treenode();

            count++;

            ret.Flags = (Flags)reader.ReadByte();
            ret.DataType = (DataType)reader.ReadByte();

            if ((ret.Flags & Flags.ExtendedFlags) == Flags.ExtendedFlags) {
                ret.FlagsExtended = (FlagsExtended)reader.ReadInt32();
            }

            int titleLength = reader.ReadInt32();

            if (titleLength > 0) {
                ret.Title = Encoding.ASCII.GetString(reader.ReadBytes(titleLength)).TrimEnd(Treenode.TrimChars);
            }

            if (ret.DataType == DataType.Float) {
                double value = reader.ReadDouble();
            } else if (ret.DataType == DataType.ByteBlock) {
                int stringLength = reader.ReadInt32();

                ret.Data = reader.ReadBytes(stringLength);
            } else if (ret.DataType == DataType.Object) {
                // If the node is an object then the next 4 bytes are an Int32 containing the
                // number of child data nodes that it has (those accessed through the > symbol
                // that is specific to objects). The next node to be read is the first of these
                // which may itself have child nodes. This is how the tree is serialised.

                // TODO Still don't know the purpose of this node
                Treenode node = _Read(reader, ref count);

                // The number of object children access with > rather then the normal +
                int numChildren = reader.ReadInt32();

                while (numChildren > 0) {
                    Treenode child = _Read(reader, ref count);
                    numChildren--;

                    ret.AddDataChild(child);
                }
            } else if (ret.DataType == DataType.PointerCoupling) {
                int coupling = reader.ReadInt32();
            } else if (ret.DataType == DataType.None || ret.DataType == DataType.Undefined) {
                // Do nothing
            } else {
                throw new Exception("Data type was not recognised");
            }

            // If the HasBranch flag is set then this node is followed by an Int32 containing the
            // number of child nodes that it has. The next node to be read is the first of these
            // which may itself have child nodes. This is how the tree is serialised
            if ((ret.Flags & Flags.HasBranch) == Flags.HasBranch) {
                // TODO Still don't know the purpose of this node
                Treenode node = _Read(reader, ref count);

                int numChildren = reader.ReadInt32();

                while (numChildren > 0) {
                    Treenode child = _Read(reader, ref count);
                    numChildren--;

                    ret.AddNodeChild(child);
                }
            }

            return ret;
        }